X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fuci.git;a=blobdiff_plain;f=list.c;h=cd995fee2ff931da9d55d40f882555dc4e48058f;hp=83f016e858b418d48cb550b81faa48e42f95ac5b;hb=ec7b1de53e16991b071c892fac149b4bfe73c539;hpb=b485972f3a40be1627fb2bdd9904889a155f12e0 diff --git a/list.c b/list.c index 83f016e..cd995fe 100644 --- a/list.c +++ b/list.c @@ -48,6 +48,26 @@ static inline void uci_list_del(struct uci_list *ptr) uci_list_init(ptr); } +static inline void uci_list_set_pos(struct uci_list *head, struct uci_list *ptr, int pos) +{ + struct uci_list *new_head = head; + struct uci_element *p = NULL; + + uci_list_del(ptr); + uci_foreach_element(head, p) { + new_head = &p->list; + if (pos-- <= 0) + break; + } + uci_list_add(new_head, 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 @@ -316,7 +336,7 @@ uci_lookup_ext_section(struct uci_context *ctx, struct uci_ptr *ptr) if (!*name) name = NULL; - else if (!uci_validate_str(name, false)) + else if (!uci_validate_type(name)) goto error; /* if the given index is negative, it specifies the section number from @@ -352,7 +372,8 @@ error: UCI_THROW(ctx, UCI_ERR_INVAL); done: free(section); - ptr->section = e->name; + if (e) + ptr->section = e->name; return e; } @@ -460,7 +481,7 @@ expand_ptr(struct uci_context *ctx, struct uci_ptr *ptr, bool complete) UCI_ASSERT(ctx, ptr != NULL); if (!(ptr->flags & UCI_LOOKUP_DONE)) - uci_lookup_ptr(ctx, ptr, NULL, 1); + UCI_INTERNAL(uci_lookup_ptr, ctx, ptr, NULL, 1); if (complete && !(ptr->flags & UCI_LOOKUP_COMPLETE)) UCI_THROW(ctx, UCI_ERR_NOTFOUND); UCI_ASSERT(ctx, ptr->p != NULL); @@ -496,103 +517,6 @@ static void uci_add_element_list(struct uci_context *ctx, struct uci_ptr *ptr, b 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 */ @@ -617,6 +541,25 @@ int uci_rename(struct uci_context *ctx, struct uci_ptr *ptr) free(e->name); e->name = n; + if (e->type == UCI_TYPE_SECTION) + uci_to_section(e)->anonymous = false; + + return 0; +} + +int uci_reorder_section(struct uci_context *ctx, struct uci_section *s, int pos) +{ + struct uci_package *p = s->package; + char order[32]; + + UCI_HANDLE_ERR(ctx); + + uci_list_set_pos(&s->package->sections, &s->e.list, pos); + if (!ctx->internal && p->has_history) { + sprintf(order, "%d", pos); + uci_add_history(ctx, &p->history, UCI_CMD_REORDER, s->e.name, NULL, order); + } + return 0; } @@ -654,6 +597,12 @@ int uci_delete(struct uci_context *ctx, struct uci_ptr *ptr) uci_add_history(ctx, &p->history, UCI_CMD_REMOVE, ptr->section, ptr->option, NULL); uci_free_any(&e); + + if (ptr->option) + ptr->o = NULL; + else if (ptr->section) + ptr->s = NULL; + return 0; } @@ -698,85 +647,64 @@ int uci_add_list(struct uci_context *ctx, struct uci_ptr *ptr) 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)); + expand_ptr(ctx, ptr, false); + UCI_ASSERT(ctx, ptr->value); + UCI_ASSERT(ctx, ptr->s || (!ptr->option && ptr->section)); + if (!ptr->option && ptr->value[0]) { + UCI_ASSERT(ctx, uci_validate_type(ptr->value)); } - /* - * 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); + if (!ptr->o && ptr->s && ptr->option) { + struct uci_element *e; + e = uci_lookup_list(&ptr->s->options, ptr->option); + if (e) + ptr->o = uci_to_option(e); } + if (!ptr->value[0]) { + /* if setting a nonexistant option/section to a nonexistant value, + * exit without errors */ + if (!(ptr->flags & UCI_LOOKUP_COMPLETE)) + return 0; - /* - * 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; + return uci_delete(ctx, ptr); + } else 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 */ + if ((ptr->o->type == UCI_TYPE_STRING) && + !strcmp(ptr->o->v.string, ptr->value)) + return 0; + 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; }