remove internal usage of redundant uci_ptr.last
[project/uci.git] / list.c
diff --git a/list.c b/list.c
index 3e8a87c7d7f75febe05828cc563a8ff6a4ef5f1f..304c9e1fd1b279181345e841e28430b485eb76d6 100644 (file)
--- a/list.c
+++ b/list.c
@@ -616,7 +616,6 @@ int uci_add_list(struct uci_context *ctx, struct uci_ptr *ptr)
                UCI_TRAP_SAVE(ctx, error);
                ptr->o = uci_alloc_list(ptr->s, ptr->option, NULL);
                UCI_TRAP_RESTORE(ctx);
-               ptr->last = &ptr->o->e;
        } else if (ptr->o->type == UCI_TYPE_STRING) {
                /* create new list and add old string value as item to list */
                struct uci_option *old = ptr->o;
@@ -630,7 +629,6 @@ int uci_add_list(struct uci_context *ctx, struct uci_ptr *ptr)
                if (ptr->option == old->e.name)
                        ptr->option = ptr->o->e.name;
                uci_free_option(old);
-               ptr->last = &ptr->o->e;
        }
 
        /* add new item to list */
@@ -708,10 +706,8 @@ int uci_set(struct uci_context *ctx, struct uci_ptr *ptr)
                return uci_delete(ctx, ptr);
        } else if (!ptr->o && ptr->option) { /* new option */
                ptr->o = uci_alloc_option(ptr->s, ptr->option, ptr->value, NULL);
-               ptr->last = &ptr->o->e;
        } else if (!ptr->s && ptr->section) { /* new section */
                ptr->s = uci_alloc_section(ptr->p, ptr->value, ptr->section, NULL);
-               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;
@@ -724,17 +720,22 @@ int uci_set(struct uci_context *ctx, struct uci_ptr *ptr)
                        if (ptr->option == old->e.name)
                                ptr->option = ptr->o->e.name;
                        uci_free_option(old);
-                       ptr->last = &ptr->o->e;
                }
        } else if (ptr->s && ptr->section) { /* update section */
-               struct uci_section *old = ptr->s;
-               ptr->s = uci_alloc_section(ptr->p, ptr->value, old->e.name, &old->e.list);
-               uci_section_transfer_options(ptr->s, old);
-               if (ptr->section == old->e.name)
-                       ptr->section = ptr->s->e.name;
-               uci_free_section(old);
-               ptr->s->package->n_section--;
-               ptr->last = &ptr->s->e;
+               if (!strcmp(ptr->s->type, ptr->value))
+                       return 0;
+
+               if (strlen(ptr->s->type) == strlen(ptr->value)) {
+                       strcpy(ptr->s->type, ptr->value);
+               } else {
+                       struct uci_section *old = ptr->s;
+                       ptr->s = uci_alloc_section(ptr->p, ptr->value, old->e.name, &old->e.list);
+                       uci_section_transfer_options(ptr->s, old);
+                       if (ptr->section == old->e.name)
+                               ptr->section = ptr->s->e.name;
+                       uci_free_section(old);
+                       ptr->s->package->n_section--;
+               }
        } else {
                UCI_THROW(ctx, UCI_ERR_INVAL);
        }