int settings_set(SettingsPair* pair, const char* value) { if (pair->type == HS_Int) { int new_value; // parse value to int, if possible if (1 == sscanf(value, "%d", &new_value)) { if (new_value == pair->value.i) { // nothing would be changed return 0; } pair->value.i = new_value; } else { return HERBST_INVALID_ARGUMENT; } } else if (pair->type == HS_String) { if (!strcmp(pair->value.str->str, value)) { // nothing would be changed return 0; } g_string_assign(pair->value.str, value); } else if (pair->type == HS_Compatiblity) { HSAttribute* attr = hsattribute_parse_path(pair->value.compat.write); GString* out = g_string_new(""); int status = hsattribute_assign(attr, value, out); if (0 != status) { HSError("Error when assigning: %s\n", out->str); } g_string_free(out, true); return status; } // on successful change, call callback if (pair->on_change) { pair->on_change(); } return 0; }
int hsattribute_set_command(int argc, char* argv[], GString* output) { if (argc < 3) { return HERBST_NEED_MORE_ARGS; } HSAttribute* attr = hsattribute_parse_path_verbose(argv[1], output); if (!attr) { return HERBST_INVALID_ARGUMENT; } return hsattribute_assign(attr, argv[2], output); }
int attr_command(int argc, char* argv[], GString* output) { char* path = (argc < 2) ? "" : argv[1]; char* unparsable; GString* errormsg = g_string_new(""); HSObject* obj = hsobject_parse_path_verbose(path, &unparsable, errormsg); HSAttribute* attribute = NULL; if (strcmp("", unparsable)) { // if object could not be parsed, try attribute attribute = hsattribute_parse_path_verbose(path, errormsg); obj = NULL; } if (!obj && !attribute) { // if nothing was found g_string_append(output, errormsg->str); g_string_free(errormsg, true); return HERBST_INVALID_ARGUMENT; } else { g_string_free(errormsg, true); } char* new_value = (argc >= 3) ? argv[2] : NULL; if (obj && new_value) { g_string_append_printf(output, "%s: Can not assign value \"%s\" to object \"%s\",", argv[0], new_value, path); } else if (obj && !new_value) { // list children int childcount = g_list_length(obj->children); g_string_append_printf(output, "%d children%c\n", childcount, childcount ? ':' : '.'); g_list_foreach(obj->children, (GFunc) print_child_name, output); if (childcount > 0) { g_string_append_printf(output, "\n"); } // list attributes g_string_append_printf(output, "%zu attributes", obj->attribute_count); if (obj->attribute_count > 0) { g_string_append_printf(output, ":\n"); g_string_append_printf(output, " .---- type\n"); g_string_append_printf(output, " | .-- writeable\n"); g_string_append_printf(output, " V V\n"); } else { g_string_append_printf(output, ".\n"); } for (int i = 0; i < obj->attribute_count; i++) { HSAttribute* a = obj->attributes + i; char write = (a->on_change == ATTR_READ_ONLY) ? '-' : 'w'; char t = hsattribute_type_indicator(a->type); g_string_append_printf(output, " %c %c %-20s = ", t, write, a->name); if (a->type == HSATTR_TYPE_STRING) { g_string_append_c(output, '\"'); } hsattribute_append_to_string(a, output); if (a->type == HSATTR_TYPE_STRING) { g_string_append_c(output, '\"'); } g_string_append_c(output, '\n'); } } else if (new_value) { // && (attribute) return hsattribute_assign(attribute, new_value, output); } else { // !new_value && (attribute) hsattribute_append_to_string(attribute, output); } return 0; }