static void uci_parse_config(struct uci_context *ctx, char **str)
{
struct uci_parse_context *pctx = ctx->pctx;
+ struct uci_element *e;
+ struct uci_ptr ptr;
char *name = NULL;
char *type = NULL;
name = next_arg(ctx, str, false, true);
assert_eol(ctx, str);
- if (pctx->merge) {
- UCI_NESTED(uci_set, ctx, pctx->package, name, NULL, type, NULL);
- } else
- pctx->section = uci_alloc_section(pctx->package, type, name);
+ if (!name) {
+ ctx->internal = !pctx->merge;
+ UCI_NESTED(uci_add_section, ctx, pctx->package, type, &pctx->section);
+ } else {
+ UCI_NESTED(uci_fill_ptr, ctx, &ptr, &pctx->package->e, false);
+ e = uci_lookup_list(&pctx->package->sections, name);
+ if (e)
+ ptr.s = uci_to_section(e);
+ ptr.section = name;
+ ptr.value = type;
+
+ ctx->internal = !pctx->merge;
+ UCI_NESTED(uci_set, ctx, &ptr);
+ pctx->section = uci_to_section(ptr.last);
+ }
}
/*
* parse the 'option' uci command (open a value)
*/
-static void uci_parse_option(struct uci_context *ctx, char **str)
-{
- struct uci_parse_context *pctx = ctx->pctx;
- char *name = NULL;
- char *value = NULL;
-
- if (!pctx->section)
- uci_parse_error(ctx, *str, "option command found before the first section");
-
- /* command string null-terminated by strtok */
- *str += strlen(*str) + 1;
-
- name = next_arg(ctx, str, true, true);
- value = next_arg(ctx, str, false, false);
- assert_eol(ctx, str);
-
- if (pctx->merge) {
- UCI_NESTED(uci_set, ctx, pctx->package, pctx->section->e.name, name, value, NULL);
- } else
- uci_alloc_option(pctx->section, name, value);
-}
-
-static void uci_parse_list(struct uci_context *ctx, char **str)
+static void uci_parse_option(struct uci_context *ctx, char **str, bool list)
{
struct uci_parse_context *pctx = ctx->pctx;
+ struct uci_element *e;
struct uci_ptr ptr;
char *name = NULL;
char *value = NULL;
if (!pctx->section)
- uci_parse_error(ctx, *str, "list command found before the first section");
+ uci_parse_error(ctx, *str, "option/list command found before the first section");
/* command string null-terminated by strtok */
*str += strlen(*str) + 1;
assert_eol(ctx, str);
UCI_NESTED(uci_fill_ptr, ctx, &ptr, &pctx->section->e, false);
+ e = uci_lookup_list(&pctx->section->options, name);
+ if (e)
+ ptr.o = uci_to_option(e);
ptr.option = name;
ptr.value = value;
- UCI_INTERNAL(uci_lookup_ptr, ctx, &ptr, NULL, false);
-
ctx->internal = !pctx->merge;
- UCI_NESTED(uci_add_list, ctx, &ptr);
+ if (list)
+ UCI_NESTED(uci_add_list, ctx, &ptr);
+ else
+ UCI_NESTED(uci_set, ctx, &ptr);
}
-
/*
* parse a complete input line, split up combined commands by ';'
*/
break;
case 'o':
if ((word[1] == 0) || !strcmp(word + 1, "ption"))
- uci_parse_option(ctx, &word);
+ uci_parse_option(ctx, &word, false);
else
goto invalid;
break;
case 'l':
if ((word[1] == 0) || !strcmp(word + 1, "ist"))
- uci_parse_list(ctx, &word);
+ uci_parse_option(ctx, &word, true);
else
goto invalid;
break;
uci_list_init(ptr);
}
+static inline void uci_list_fixup(struct uci_list *ptr)
+{
+ ptr->prev->next = ptr;
+ ptr->next->prev = ptr;
+}
+
/*
* uci_alloc_generic allocates a new uci_element with payload
* payload is appended to the struct to save memory and reduce fragmentation
uci_list_add(&ptr->o->v.list, &e->list);
}
-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;
-}
-
int uci_rename(struct uci_context *ctx, struct uci_ptr *ptr)
{
/* NB: UCI_INTERNAL use means without history tracking */
return 0;
}
-int uci_set(struct uci_context *ctx, struct uci_package *p, const char *section, const char *option, const char *value, struct uci_element **result)
+int uci_set(struct uci_context *ctx, struct uci_ptr *ptr)
{
/* NB: UCI_INTERNAL use means without history tracking */
bool internal = ctx->internal;
- struct uci_element *e = NULL;
- struct uci_section *s = NULL;
- struct uci_option *o = NULL;
UCI_HANDLE_ERR(ctx);
- UCI_ASSERT(ctx, p != NULL);
- UCI_ASSERT(ctx, uci_validate_name(section));
- if (option) {
- UCI_ASSERT(ctx, uci_validate_name(option));
- UCI_ASSERT(ctx, value != NULL);
- } else {
- UCI_ASSERT(ctx, uci_validate_str(value, false));
- }
-
- /*
- * look up the package, section and option (if set)
- * if the section/option is to be modified and it is not found
- * create a new element in the appropriate list
- */
- e = uci_lookup_list(&p->sections, section);
- if (!e)
- goto notfound;
-
- s = uci_to_section(e);
- if (ctx->pctx && ctx->pctx->merge)
- ctx->pctx->section = s;
-
- if (option) {
- e = uci_lookup_list(&s->options, option);
- if (!e)
- goto notfound;
- o = uci_to_option(e);
+ expand_ptr(ctx, ptr, false);
+ UCI_ASSERT(ctx, ptr->value);
+ UCI_ASSERT(ctx, ptr->s || (!ptr->option && ptr->section));
+ if (!ptr->option) {
+ UCI_ASSERT(ctx, uci_validate_str(ptr->value, false));
}
- /*
- * no unknown element was supplied, assume that we can just update
- * an existing entry
- */
- if (o)
- e = &o->e;
- else
- e = &s->e;
- if (result)
- *result = e;
- else
- result = &e;
-
- ctx->internal = internal;
- return uci_set_element_value(ctx, result, value);
-
-notfound:
- /*
- * the entry that we need to update was not found,
- * check if the search failed prematurely.
- * this can happen if the package was not found, or if
- * an option was supplied, but the section wasn't found
- */
- if (!p || (!s && option))
- UCI_THROW(ctx, UCI_ERR_NOTFOUND);
-
- /* now add the missing entry */
- if (!internal && p->has_history)
- uci_add_history(ctx, &p->history, UCI_CMD_CHANGE, section, option, value);
- if (s) {
- o = uci_alloc_option(s, option, value);
- if (result)
- *result = &o->e;
+ if (!ptr->o && ptr->option) { /* new option */
+ ptr->o = uci_alloc_option(ptr->s, ptr->option, ptr->value);
+ ptr->last = &ptr->o->e;
+ } else if (!ptr->s && ptr->section) { /* new section */
+ ptr->s = uci_alloc_section(ptr->p, ptr->value, ptr->section);
+ ptr->last = &ptr->s->e;
+ } else if (ptr->o && ptr->option) { /* update option */
+ uci_free_option(ptr->o);
+ ptr->o = uci_alloc_option(ptr->s, ptr->option, ptr->value);
+ ptr->last = &ptr->o->e;
+ } else if (ptr->s && ptr->section) { /* update section */
+ char *s = uci_strdup(ctx, ptr->value);
+
+ if (ptr->s->type == uci_dataptr(ptr->s)) {
+ ptr->last = NULL;
+ ptr->last = uci_realloc(ctx, ptr->s, sizeof(struct uci_section));
+ ptr->s = uci_to_section(ptr->last);
+ uci_list_fixup(&ptr->s->e.list);
+ } else {
+ free(ptr->s->type);
+ }
+ ptr->s->type = s;
} else {
- s = uci_alloc_section(p, value, section);
- if (result)
- *result = &s->e;
- if (ctx->pctx && ctx->pctx->merge)
- ctx->pctx->section = s;
+ UCI_THROW(ctx, UCI_ERR_INVAL);
}
+ if (!internal && ptr->p->has_history)
+ uci_add_history(ctx, &ptr->p->history, UCI_CMD_CHANGE, ptr->section, ptr->option, ptr->value);
+
return 0;
}