+ UCI_HANDLE_ERR(ctx);
+ UCI_ASSERT(ctx, value != NULL);
+ UCI_ASSERT(ctx, element != NULL);
+ UCI_ASSERT(ctx, *element != NULL);
+
+ /* what the 'value' of an element means depends on the type
+ * for a section, the 'value' means its type
+ * for an option, the 'value' means its value string
+ * when changing the value, shrink the element to its actual size
+ * (it may have been allocated with a bigger size, to include
+ * its buffer)
+ * then duplicate the string passed on the command line and
+ * insert it into the structure.
+ */
+ e = *element;
+ list = e->list.prev;
+ switch(e->type) {
+ case UCI_TYPE_SECTION:
+ size = sizeof(struct uci_section);
+ s = uci_to_section(e);
+ section = e->name;
+ option = NULL;
+ break;
+ case UCI_TYPE_OPTION:
+ size = sizeof(struct uci_option);
+ s = uci_to_option(e)->section;
+ section = s->e.name;
+ option = e->name;
+ break;
+ default:
+ UCI_THROW(ctx, UCI_ERR_INVAL);
+ return 0;
+ }
+ p = s->package;
+ if (!internal)
+ uci_add_history(ctx, p, UCI_CMD_CHANGE, section, option, value);
+
+ uci_list_del(&e->list);
+ e = uci_realloc(ctx, e, size);
+ str = uci_strdup(ctx, value);
+ uci_list_insert(list, &e->list);
+ *element = e;
+
+ switch(e->type) {
+ case UCI_TYPE_SECTION:
+ uci_to_section(e)->type = str;
+ break;
+ case UCI_TYPE_OPTION:
+ uci_to_option(e)->value = str;
+ break;
+ default:
+ break;
+ }
+
+ return 0;