change __uci_list_add to uci_list_insert and remove an unnecessary parameter
[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_insert(struct uci_list *list, struct uci_list *ptr)
26 {
27 list->next->prev = ptr;
28 ptr->prev = list;
29 ptr->next = list->next;
30 list->next = ptr;
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_insert(head->prev, 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 uci_list_init(ptr);
51 }
52
53 static struct uci_element *
54 uci_alloc_generic(struct uci_context *ctx, int type, const char *name, int size)
55 {
56 struct uci_element *e;
57 void *ptr;
58
59 ptr = uci_malloc(ctx, size + strlen(name) + 1);
60 e = (struct uci_element *) ptr;
61 e->type = type;
62 e->name = (char *) ptr + size;
63 strcpy(e->name, name);
64 uci_list_init(&e->list);
65
66 return e;
67 }
68
69 static void
70 uci_free_element(struct uci_element *e)
71 {
72 if (!e)
73 return;
74
75 if (!uci_list_empty(&e->list))
76 uci_list_del(&e->list);
77 free(e);
78 }
79
80 static struct uci_option *
81 uci_alloc_option(struct uci_section *s, const char *name, const char *value)
82 {
83 struct uci_package *p = s->package;
84 struct uci_context *ctx = p->ctx;
85 struct uci_option *o;
86
87 o = uci_alloc_element(ctx, option, name, strlen(value) + 1);
88 o->value = uci_dataptr(o);
89 o->section = s;
90 strcpy(o->value, value);
91 uci_list_add(&s->options, &o->e.list);
92
93 return o;
94 }
95
96 static inline void
97 uci_free_option(struct uci_option *o)
98 {
99 uci_free_element(&o->e);
100 }
101
102 static struct uci_section *
103 uci_alloc_section(struct uci_package *p, const char *type, const char *name)
104 {
105 struct uci_context *ctx = p->ctx;
106 struct uci_section *s;
107 char buf[16];
108
109 if (!name || !name[0]) {
110 snprintf(buf, 16, "cfg%d", p->n_section);
111 name = buf;
112 }
113
114 s = uci_alloc_element(ctx, section, name, strlen(type) + 1);
115 s->type = uci_dataptr(s);
116 s->package = p;
117 strcpy(s->type, type);
118 uci_list_init(&s->options);
119 uci_list_add(&p->sections, &s->e.list);
120
121 return s;
122 }
123
124 static void
125 uci_free_section(struct uci_section *s)
126 {
127 struct uci_element *o, *tmp;
128
129 uci_foreach_element_safe(&s->options, tmp, o) {
130 uci_free_option(uci_to_option(o));
131 }
132 uci_free_element(&s->e);
133 }
134
135 static struct uci_package *
136 uci_alloc_package(struct uci_context *ctx, const char *name)
137 {
138 struct uci_package *p;
139
140 p = uci_alloc_element(ctx, package, name, 0);
141 p->ctx = ctx;
142 uci_list_init(&p->sections);
143 return p;
144 }
145
146 static void
147 uci_free_package(struct uci_package *p)
148 {
149 struct uci_element *e, *tmp;
150
151 if(!p)
152 return;
153
154 uci_foreach_element_safe(&p->sections, tmp, e) {
155 uci_free_section(uci_to_section(e));
156 }
157 uci_free_element(&p->e);
158 }
159
160 static struct uci_element *uci_lookup_list(struct uci_context *ctx, struct uci_list *list, char *name)
161 {
162 struct uci_element *e;
163
164 uci_foreach_element(list, e) {
165 if (!strcmp(e->name, name))
166 return e;
167 }
168 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
169 }
170
171 int uci_lookup(struct uci_context *ctx, struct uci_element **res, char *package, char *section, char *option)
172 {
173 struct uci_element *e;
174 struct uci_package *p;
175 struct uci_section *s;
176 struct uci_option *o;
177
178 UCI_HANDLE_ERR(ctx);
179 UCI_ASSERT(ctx, res != NULL);
180 UCI_ASSERT(ctx, package != NULL);
181
182 e = uci_lookup_list(ctx, &ctx->root, package);
183 if (!section)
184 goto found;
185
186 p = uci_to_package(e);
187 e = uci_lookup_list(ctx, &p->sections, section);
188 if (!option)
189 goto found;
190
191 s = uci_to_section(e);
192 e = uci_lookup_list(ctx, &s->options, option);
193
194 found:
195 *res = e;
196 return 0;
197 }
198
199 int uci_unload(struct uci_context *ctx, const char *name)
200 {
201 struct uci_element *e;
202
203 UCI_HANDLE_ERR(ctx);
204 UCI_ASSERT(ctx, name != NULL);
205
206 uci_foreach_element(&ctx->root, e) {
207 if (!strcmp(e->name, name))
208 goto found;
209 }
210 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
211
212 found:
213 uci_free_package(uci_to_package(e));
214
215 return 0;
216 }
217
218 static inline char *get_filename(char *path)
219 {
220 char *p;
221
222 p = strrchr(path, '/');
223 p++;
224 if (!*p)
225 return NULL;
226 return p;
227 }
228
229 char **uci_list_configs(struct uci_context *ctx)
230 {
231 char **configs;
232 glob_t globbuf;
233 int size, i;
234 char *buf;
235
236 if (glob(UCI_CONFDIR "/*", GLOB_MARK, NULL, &globbuf) != 0)
237 return NULL;
238
239 size = sizeof(char *) * (globbuf.gl_pathc + 1);
240 for(i = 0; i < globbuf.gl_pathc; i++) {
241 char *p;
242
243 p = get_filename(globbuf.gl_pathv[i]);
244 if (!p)
245 continue;
246
247 size += strlen(p) + 1;
248 }
249
250 configs = malloc(size);
251 if (!configs)
252 return NULL;
253
254 memset(configs, 0, size);
255 buf = (char *) &configs[globbuf.gl_pathc + 1];
256 for(i = 0; i < globbuf.gl_pathc; i++) {
257 char *p;
258
259 p = get_filename(globbuf.gl_pathv[i]);
260 if (!p)
261 continue;
262
263 configs[i] = buf;
264 strcpy(buf, p);
265 buf += strlen(buf) + 1;
266 }
267 return configs;
268 }
269
270