2 * libuci - Library for the Unified Configuration Interface
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
5 * this program is free software; you can redistribute it and/or modify
6 * it under the terms of the gnu lesser general public license version 2.1
7 * as published by the free software foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 /* initialize a list head/item */
16 static inline void uci_list_init(struct uci_list *ptr)
22 /* inserts a new list entry between two consecutive entries */
23 static inline void __uci_list_add(struct uci_list *prev, struct uci_list *next, struct uci_list *ptr)
31 /* inserts a new list entry at the tail of the list */
32 static inline void uci_list_add(struct uci_list *head, struct uci_list *ptr)
34 /* NB: head->prev points at the tail */
35 __uci_list_add(head->prev, head, ptr);
38 static inline void uci_list_del(struct uci_list *ptr)
40 struct uci_list *next, *prev;
49 static void uci_drop_section(struct uci_section *section)
53 /* TODO: drop options */
61 static struct uci_section *uci_add_section(struct uci_config *cfg, const char *type, const char *name)
63 struct uci_section *section = NULL;
64 struct uci_context *ctx = cfg->ctx;
66 UCI_TRAP_SAVE(ctx, error)
67 section = (struct uci_section *) uci_malloc(ctx, sizeof(struct uci_section));
68 section->config = cfg;
69 uci_list_init(§ion->list);
70 uci_list_init(§ion->options);
71 uci_list_add(&cfg->sections, §ion->list);
72 section->type = uci_strdup(ctx, type);
74 section->name = uci_strdup(ctx, name);
75 UCI_TRAP_RESTORE(ctx);
80 uci_drop_section(section);
81 UCI_THROW(ctx, ctx->errno);
85 static void uci_drop_file(struct uci_config *cfg)
87 struct uci_section *s;
91 uci_foreach_entry(section, &cfg->sections, s) {
100 static struct uci_config *uci_alloc_file(struct uci_context *ctx, const char *name)
102 struct uci_config *cfg = NULL;
104 UCI_TRAP_SAVE(ctx, error)
105 cfg = (struct uci_config *) uci_malloc(ctx, sizeof(struct uci_config));
106 uci_list_init(&cfg->list);
107 uci_list_init(&cfg->sections);
108 cfg->name = uci_strdup(ctx, name);
110 UCI_TRAP_RESTORE(ctx);
115 UCI_THROW(ctx, ctx->errno);