8d73f11405ebad863a4c1fa5228b83c7f6620ccf
[project/uci.git] / list.c
1 /*
2  * libuci - Library for the Unified Configuration Interface
3  * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
4  *
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
8  *
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.
13  */
14
15 #include <glob.h>
16
17 /* initialize a list head/item */
18 static inline void uci_list_init(struct uci_list *ptr)
19 {
20         ptr->prev = ptr;
21         ptr->next = ptr;
22 }
23
24 /* inserts a new list entry between two consecutive entries */
25 static inline void __uci_list_add(struct uci_list *prev, struct uci_list *next, struct uci_list *ptr)
26 {
27         prev->next = ptr;
28         next->prev = ptr;
29         ptr->prev = prev;
30         ptr->next = next;
31 }
32
33 /* inserts a new list entry at the tail of the list */
34 static inline void uci_list_add(struct uci_list *head, struct uci_list *ptr)
35 {
36         /* NB: head->prev points at the tail */
37         __uci_list_add(head->prev, head, ptr);
38 }
39
40 static inline void uci_list_del(struct uci_list *ptr)
41 {
42         struct uci_list *next, *prev;
43
44         next = ptr->next;
45         prev = ptr->prev;
46
47         prev->next = next;
48         next->prev = prev;
49 }
50
51 static void uci_drop_option(struct uci_option *option)
52 {
53         if (!option)
54                 return;
55         if (option->name)
56                 free(option->name);
57         if (option->value)
58                 free(option->value);
59         free(option);
60 }
61
62 static struct uci_option *uci_add_option(struct uci_section *section, const char *name, const char *value)
63 {
64         struct uci_config *cfg = section->config;
65         struct uci_context *ctx = cfg->ctx;
66         struct uci_option *option = NULL;
67
68         UCI_TRAP_SAVE(ctx, error);
69         option = (struct uci_option *) uci_malloc(ctx, sizeof(struct uci_option));
70         option->name = uci_strdup(ctx, name);
71         option->value = uci_strdup(ctx, value);
72         uci_list_add(&section->options, &option->list);
73         UCI_TRAP_RESTORE(ctx);
74
75 error:
76         uci_drop_option(option);
77         UCI_THROW(ctx, ctx->errno);
78         return NULL;
79 }
80
81 static void uci_drop_section(struct uci_section *section)
82 {
83         struct uci_option *opt;
84
85         if (!section)
86                 return;
87
88         uci_foreach_entry(option, &section->options, opt) {
89                 uci_list_del(&opt->list);
90                 uci_drop_option(opt);
91         }
92
93         if (section->name)
94                 free(section->name);
95         if (section->type)
96                 free(section->type);
97         free(section);
98 }
99
100 static struct uci_section *uci_add_section(struct uci_config *cfg, const char *type, const char *name)
101 {
102         struct uci_section *section = NULL;
103         struct uci_context *ctx = cfg->ctx;
104
105         UCI_TRAP_SAVE(ctx, error);
106         section = (struct uci_section *) uci_malloc(ctx, sizeof(struct uci_section));
107         section->config = cfg;
108         uci_list_init(&section->list);
109         uci_list_init(&section->options);
110         section->type = uci_strdup(ctx, type);
111         if (name)
112                 section->name = uci_strdup(ctx, name);
113         uci_list_add(&cfg->sections, &section->list);
114         UCI_TRAP_RESTORE(ctx);
115
116         return section;
117
118 error:
119         uci_drop_section(section);
120         UCI_THROW(ctx, ctx->errno);
121         return NULL;
122 }
123
124 static void uci_drop_config(struct uci_config *cfg)
125 {
126         struct uci_section *s;
127
128         if(!cfg)
129                 return;
130
131         uci_foreach_entry(section, &cfg->sections, s) {
132                 uci_list_del(&s->list);
133                 uci_drop_section(s);
134         }
135
136         if (cfg->name)
137                 free(cfg->name);
138         free(cfg);
139 }
140
141
142 static struct uci_config *uci_alloc_config(struct uci_context *ctx, const char *name)
143 {
144         struct uci_config *cfg = NULL;
145
146         UCI_TRAP_SAVE(ctx, error);
147         cfg = (struct uci_config *) uci_malloc(ctx, sizeof(struct uci_config));
148         uci_list_init(&cfg->list);
149         uci_list_init(&cfg->sections);
150         cfg->name = uci_strdup(ctx, name);
151         cfg->ctx = ctx;
152         UCI_TRAP_RESTORE(ctx);
153         return cfg;
154
155 error:
156         uci_drop_config(cfg);
157         UCI_THROW(ctx, ctx->errno);
158         return NULL;
159 }
160
161 int uci_unload(struct uci_context *ctx, const char *name)
162 {
163         struct uci_config *cfg;
164
165         UCI_HANDLE_ERR(ctx);
166         UCI_ASSERT(ctx, name != NULL);
167
168         uci_foreach_entry(config, &ctx->root, cfg) {
169                 if (!strcmp(cfg->name, name))
170                         goto found;
171         }
172         UCI_THROW(ctx, UCI_ERR_NOTFOUND);
173
174 found:
175         uci_list_del(&cfg->list);
176         uci_drop_config(cfg);
177
178         return 0;
179 }
180
181 char **uci_list_configs(struct uci_context *ctx)
182 {
183         char **configs;
184         glob_t globbuf;
185         int size, i;
186         char *buf;
187
188         if (glob(UCI_CONFDIR "/*", GLOB_MARK, NULL, &globbuf) != 0)
189                 return NULL;
190
191         size = sizeof(char *) * (globbuf.gl_pathc + 1);
192         for(i = 0; i < globbuf.gl_pathc; i++)
193                 size += strlen(globbuf.gl_pathv[i]) + 1;
194
195         configs = malloc(size);
196         if (!configs)
197                 return NULL;
198
199         memset(configs, 0, size);
200         buf = (char *) &configs[globbuf.gl_pathc + 1];
201         for(i = 0; i < globbuf.gl_pathc; i++) {
202                 configs[i] = buf;
203                 strcpy(buf, globbuf.gl_pathv[i]);
204                 buf += strlen(buf) + 1;
205         }
206         return configs;
207 }
208