fix missing api change
[project/uci.git] / list.c
diff --git a/list.c b/list.c
index 0cb6dad..cd995fe 100644 (file)
--- a/list.c
+++ b/list.c
@@ -48,6 +48,20 @@ 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;
@@ -322,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 
@@ -358,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;
 }
 
@@ -466,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);
@@ -526,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;
 }
 
@@ -563,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;
 }
 
@@ -616,17 +656,33 @@ int uci_set(struct uci_context *ctx, struct uci_ptr *ptr)
        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));
+       if (!ptr->option && ptr->value[0]) {
+               UCI_ASSERT(ctx, uci_validate_type(ptr->value));
+       }
+
+       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;
 
-       if (!ptr->o && ptr->option) { /* new option */
+               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;