+notfound:
+ UCI_THROW(ctx, UCI_ERR_NOTFOUND);
+ /* not a chance here */
+ return UCI_ERR_NOTFOUND;
+}
+
+__private struct uci_element *
+uci_expand_ptr(struct uci_context *ctx, struct uci_ptr *ptr, bool complete)
+{
+ UCI_ASSERT(ctx, ptr != NULL);
+
+ if (!(ptr->flags & UCI_LOOKUP_DONE))
+ 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);
+
+ /* fill in missing string info */
+ if (ptr->p && !ptr->package)
+ ptr->package = ptr->p->e.name;
+ if (ptr->s && !ptr->section)
+ ptr->section = ptr->s->e.name;
+ if (ptr->o && !ptr->option)
+ ptr->option = ptr->o->e.name;
+
+ if (ptr->o)
+ return &ptr->o->e;
+ if (ptr->s)
+ return &ptr->s->e;
+ if (ptr->p)
+ return &ptr->p->e;
+ else
+ return NULL;
+}
+
+static void uci_add_element_list(struct uci_context *ctx, struct uci_ptr *ptr, bool internal)
+{
+ struct uci_element *e;
+ struct uci_package *p;
+
+ p = ptr->p;
+ if (!internal && p->has_delta)
+ uci_add_delta(ctx, &p->delta, UCI_CMD_LIST_ADD, ptr->section, ptr->option, ptr->value);
+
+ e = uci_alloc_generic(ctx, UCI_TYPE_ITEM, ptr->value, sizeof(struct uci_option));
+ uci_list_add(&ptr->o->v.list, &e->list);
+}
+
+int uci_rename(struct uci_context *ctx, struct uci_ptr *ptr)
+{
+ /* NB: UCI_INTERNAL use means without delta tracking */
+ bool internal = ctx && ctx->internal;
+ struct uci_element *e;
+ struct uci_package *p;
+ char *n;
+
+ UCI_HANDLE_ERR(ctx);
+
+ e = uci_expand_ptr(ctx, ptr, true);
+ p = ptr->p;
+
+ UCI_ASSERT(ctx, ptr->s);
+ UCI_ASSERT(ctx, ptr->value);
+
+ if (!internal && p->has_delta)
+ uci_add_delta(ctx, &p->delta, UCI_CMD_RENAME, ptr->section, ptr->option, ptr->value);
+
+ n = uci_strdup(ctx, ptr->value);
+ 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;
+ bool internal = ctx && ctx->internal;
+ char order[32];
+
+ UCI_HANDLE_ERR(ctx);
+
+ uci_list_set_pos(&s->package->sections, &s->e.list, pos);
+ if (!internal && p->has_delta) {
+ sprintf(order, "%d", pos);
+ uci_add_delta(ctx, &p->delta, UCI_CMD_REORDER, s->e.name, NULL, order);