+int uci_set_element_value(struct uci_context *ctx, struct uci_element **element, char *value)
+{
+ /* NB: UCI_INTERNAL use means without history tracking */
+ bool internal = ctx->internal;
+ struct uci_list *list;
+ struct uci_element *e;
+ struct uci_package *p;
+ struct uci_section *s;
+ char *section;
+ char *option;
+ char *str;
+ int size;
+
+ UCI_HANDLE_ERR(ctx);
+ UCI_ASSERT(ctx, (element != NULL) && (*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:
+ UCI_ASSERT(ctx, uci_validate_str(value, false));
+ size = sizeof(struct uci_section);
+ s = uci_to_section(e);
+ section = e->name;
+ option = NULL;
+ break;
+ case UCI_TYPE_OPTION:
+ UCI_ASSERT(ctx, value != NULL);
+ 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 && p->confdir)
+ uci_add_history(ctx, &p->history, 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;
+ }