]> pilppa.org Git - uci.git/blobdiff - list.c
add section reordering to libuci and cli
[uci.git] / list.c
diff --git a/list.c b/list.c
index 9486ffba17d1f9b871d3709921fb8e499bfd9dfe..adff0176402f74c1fbe846900e2094595f35deff 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;
@@ -533,6 +547,22 @@ int uci_rename(struct uci_context *ctx, struct uci_ptr *ptr)
        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;
+}
+
 int uci_add_section(struct uci_context *ctx, struct uci_package *p, const char *type, struct uci_section **res)
 {
        bool internal = ctx->internal;