-int uci_set_element_value(struct uci_context *ctx, struct uci_element **element, const 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;
- struct uci_option *o;
- char *section;
- char *option;
- char *str;
- int size = 0;
-
- 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;
- /* matches the currently set value */
- if (!strcmp(value, s->type))
- return 0;
- break;
-
- case UCI_TYPE_OPTION:
- UCI_ASSERT(ctx, value != NULL);
- o = uci_to_option(e);
- s = o->section;
- section = s->e.name;
- option = o->e.name;
- switch(o->type) {
- case UCI_TYPE_STRING:
- size = sizeof(struct uci_option);
- /* matches the currently set value */
- if (!strcmp(value, o->v.string))
- return 0;
- break;
- default:
- /* default action for non-string datatypes is to delete
- * the existing entry, then re-create it as a string */
- break;
- }
- break;
-
- default:
- UCI_THROW(ctx, UCI_ERR_INVAL);
- return 0;
- }
- p = s->package;
- if (!internal && p->has_history)
- uci_add_history(ctx, &p->history, UCI_CMD_CHANGE, section, option, value);
-
- if ((e->type == UCI_TYPE_OPTION) && (size == 0)) {
- o = uci_alloc_option(s, option, value);
- uci_free_any(&e);
- *element = &o->e;
- goto done;
- }
-
- 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)->v.string = str;
- break;
- default:
- break;
- }
-
-done:
- return 0;
-}
-