c6b7ebc87743e12f2bb499ac93050461a3c0c5a1
[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 after a given entry */
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 /*
54 * uci_alloc_generic allocates a new uci_element with payload
55 * payload is appended to the struct to save memory and reduce fragmentation
56 */
57 static struct uci_element *
58 uci_alloc_generic(struct uci_context *ctx, int type, const char *name, int size)
59 {
60 struct uci_element *e;
61 int datalen = size;
62 void *ptr;
63
64 ptr = uci_malloc(ctx, datalen);
65 e = (struct uci_element *) ptr;
66 e->type = type;
67 if (name) {
68 UCI_TRAP_SAVE(ctx, error);
69 e->name = uci_strdup(ctx, name);
70 UCI_TRAP_RESTORE(ctx);
71 }
72 uci_list_init(&e->list);
73 goto done;
74
75 error:
76 free(ptr);
77 UCI_THROW(ctx, ctx->errno);
78
79 done:
80 return e;
81 }
82
83 static void
84 uci_free_element(struct uci_element *e)
85 {
86 if (e->name)
87 free(e->name);
88 if (!uci_list_empty(&e->list))
89 uci_list_del(&e->list);
90 free(e);
91 }
92
93 static struct uci_option *
94 uci_alloc_option(struct uci_section *s, const char *name, const char *value)
95 {
96 struct uci_package *p = s->package;
97 struct uci_context *ctx = p->ctx;
98 struct uci_option *o;
99
100 o = uci_alloc_element(ctx, option, name, strlen(value) + 1);
101 o->value = uci_dataptr(o);
102 o->section = s;
103 strcpy(o->value, value);
104 uci_list_add(&s->options, &o->e.list);
105
106 return o;
107 }
108
109 static inline void
110 uci_free_option(struct uci_option *o)
111 {
112 if ((o->value != uci_dataptr(o)) &&
113 (o->value != NULL))
114 free(o->value);
115 uci_free_element(&o->e);
116 }
117
118 static struct uci_section *
119 uci_alloc_section(struct uci_package *p, const char *type, const char *name)
120 {
121 struct uci_context *ctx = p->ctx;
122 struct uci_section *s;
123
124 if (name && !name[0])
125 name = NULL;
126
127 s = uci_alloc_element(ctx, section, name, strlen(type) + 1);
128 uci_list_init(&s->options);
129 s->type = uci_dataptr(s);
130 s->package = p;
131 strcpy(s->type, type);
132 if (name == NULL)
133 s->anonymous = true;
134
135 uci_list_add(&p->sections, &s->e.list);
136
137 return s;
138 }
139
140 static void
141 uci_free_section(struct uci_section *s)
142 {
143 struct uci_element *o, *tmp;
144
145 uci_foreach_element_safe(&s->options, tmp, o) {
146 uci_free_option(uci_to_option(o));
147 }
148 if ((s->type != uci_dataptr(s)) &&
149 (s->type != NULL))
150 free(s->type);
151 uci_free_element(&s->e);
152 }
153
154 static struct uci_package *
155 uci_alloc_package(struct uci_context *ctx, const char *name)
156 {
157 struct uci_package *p;
158
159 p = uci_alloc_element(ctx, package, name, 0);
160 p->ctx = ctx;
161 uci_list_init(&p->sections);
162 uci_list_init(&p->history);
163 uci_list_init(&p->saved_history);
164 return p;
165 }
166
167 static void
168 uci_free_package(struct uci_package **package)
169 {
170 struct uci_element *e, *tmp;
171 struct uci_package *p = *package;
172
173 if(!p)
174 return;
175
176 if (p->path)
177 free(p->path);
178 uci_foreach_element_safe(&p->sections, tmp, e) {
179 uci_free_section(uci_to_section(e));
180 }
181 uci_foreach_element_safe(&p->history, tmp, e) {
182 uci_free_history(uci_to_history(e));
183 }
184 uci_foreach_element_safe(&p->saved_history, tmp, e) {
185 uci_free_history(uci_to_history(e));
186 }
187 uci_free_element(&p->e);
188 *package = NULL;
189 }
190
191 static struct uci_element *uci_lookup_list(struct uci_context *ctx, struct uci_list *list, const char *name)
192 {
193 struct uci_element *e;
194
195 uci_foreach_element(list, e) {
196 if (!strcmp(e->name, name))
197 return e;
198 }
199 return NULL;
200 }
201
202 int uci_lookup(struct uci_context *ctx, struct uci_element **res, struct uci_package *p, char *section, char *option)
203 {
204 struct uci_element *e;
205 struct uci_section *s;
206
207 UCI_HANDLE_ERR(ctx);
208 UCI_ASSERT(ctx, res != NULL);
209 UCI_ASSERT(ctx, p != NULL);
210 UCI_ASSERT(ctx, uci_validate_name(section));
211 if (option)
212 UCI_ASSERT(ctx, uci_validate_name(option));
213
214 e = uci_lookup_list(ctx, &p->sections, section);
215 if (!e)
216 goto notfound;
217
218 if (option) {
219 s = uci_to_section(e);
220 e = uci_lookup_list(ctx, &s->options, option);
221 if (!e)
222 goto notfound;
223 }
224
225 *res = e;
226 return 0;
227
228 notfound:
229 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
230 return 0;
231 }
232
233 int uci_del_element(struct uci_context *ctx, struct uci_element *e)
234 {
235 /* NB: UCI_INTERNAL use means without history tracking */
236 bool internal = ctx->internal;
237 struct uci_package *p = NULL;
238 struct uci_section *s = NULL;
239 struct uci_option *o = NULL;
240 struct uci_element *i, *tmp;
241 char *option = NULL;
242
243 UCI_HANDLE_ERR(ctx);
244 UCI_ASSERT(ctx, e != NULL);
245
246 switch(e->type) {
247 case UCI_TYPE_SECTION:
248 s = uci_to_section(e);
249 uci_foreach_element_safe(&s->options, tmp, i) {
250 uci_del_element(ctx, i);
251 }
252 break;
253 case UCI_TYPE_OPTION:
254 o = uci_to_option(e);
255 s = o->section;
256 p = s->package;
257 option = e->name;
258 break;
259 default:
260 UCI_THROW(ctx, UCI_ERR_INVAL);
261 break;
262 }
263
264 p = s->package;
265 if (!internal && p->confdir)
266 uci_add_history(ctx, &p->history, UCI_CMD_REMOVE, s->e.name, option, NULL);
267
268 switch(e->type) {
269 case UCI_TYPE_SECTION:
270 uci_free_section(s);
271 break;
272 case UCI_TYPE_OPTION:
273 uci_free_option(o);
274 break;
275 default:
276 break;
277 }
278 return 0;
279 }
280
281 int uci_set_element_value(struct uci_context *ctx, struct uci_element **element, char *value)
282 {
283 /* NB: UCI_INTERNAL use means without history tracking */
284 bool internal = ctx->internal;
285 struct uci_list *list;
286 struct uci_element *e;
287 struct uci_package *p;
288 struct uci_section *s;
289 char *section;
290 char *option;
291 char *str;
292 int size;
293
294 UCI_HANDLE_ERR(ctx);
295 UCI_ASSERT(ctx, (element != NULL) && (*element != NULL));
296
297 /* what the 'value' of an element means depends on the type
298 * for a section, the 'value' means its type
299 * for an option, the 'value' means its value string
300 * when changing the value, shrink the element to its actual size
301 * (it may have been allocated with a bigger size, to include
302 * its buffer)
303 * then duplicate the string passed on the command line and
304 * insert it into the structure.
305 */
306 e = *element;
307 list = e->list.prev;
308 switch(e->type) {
309 case UCI_TYPE_SECTION:
310 UCI_ASSERT(ctx, uci_validate_str(value, false));
311 size = sizeof(struct uci_section);
312 s = uci_to_section(e);
313 section = e->name;
314 option = NULL;
315 break;
316 case UCI_TYPE_OPTION:
317 UCI_ASSERT(ctx, value != NULL);
318 size = sizeof(struct uci_option);
319 s = uci_to_option(e)->section;
320 section = s->e.name;
321 option = e->name;
322 break;
323 default:
324 UCI_THROW(ctx, UCI_ERR_INVAL);
325 return 0;
326 }
327 p = s->package;
328 if (!internal && p->confdir)
329 uci_add_history(ctx, &p->history, UCI_CMD_CHANGE, section, option, value);
330
331 uci_list_del(&e->list);
332 e = uci_realloc(ctx, e, size);
333 str = uci_strdup(ctx, value);
334 uci_list_insert(list, &e->list);
335 *element = e;
336
337 switch(e->type) {
338 case UCI_TYPE_SECTION:
339 uci_to_section(e)->type = str;
340 break;
341 case UCI_TYPE_OPTION:
342 uci_to_option(e)->value = str;
343 break;
344 default:
345 break;
346 }
347
348 return 0;
349 }
350
351 int uci_rename(struct uci_context *ctx, struct uci_package *p, char *section, char *option, char *name)
352 {
353 /* NB: UCI_INTERNAL use means without history tracking */
354 bool internal = ctx->internal;
355 struct uci_element *e;
356
357 UCI_HANDLE_ERR(ctx);
358
359 /* NB: p, section, option validated by uci_lookup */
360 UCI_INTERNAL(uci_lookup, ctx, &e, p, section, option);
361
362 if (!internal && p->confdir)
363 uci_add_history(ctx, &p->history, UCI_CMD_RENAME, section, option, name);
364
365 name = uci_strdup(ctx, name);
366 if (e->name)
367 free(e->name);
368 e->name = name;
369
370 return 0;
371 }
372
373
374 int uci_delete(struct uci_context *ctx, struct uci_package *p, char *section, char *option)
375 {
376 /* NB: pass on internal flag to uci_del_element */
377 bool internal = ctx->internal;
378 struct uci_element *e;
379
380 UCI_HANDLE_ERR(ctx);
381
382 /* NB: p, section, option validated by uci_lookup */
383 UCI_INTERNAL(uci_lookup, ctx, &e, p, section, option);
384
385 ctx->internal = internal;
386 return uci_del_element(ctx, e);
387 }
388
389 int uci_set(struct uci_context *ctx, struct uci_package *p, char *section, char *option, char *value, struct uci_element **result)
390 {
391 /* NB: UCI_INTERNAL use means without history tracking */
392 bool internal = ctx->internal;
393 struct uci_element *e = NULL;
394 struct uci_section *s = NULL;
395 struct uci_option *o = NULL;
396
397 UCI_HANDLE_ERR(ctx);
398 UCI_ASSERT(ctx, p != NULL);
399 UCI_ASSERT(ctx, uci_validate_name(section));
400 if (option) {
401 UCI_ASSERT(ctx, uci_validate_name(option));
402 UCI_ASSERT(ctx, value != NULL);
403 } else {
404 UCI_ASSERT(ctx, uci_validate_str(value, false));
405 }
406
407 /*
408 * look up the package, section and option (if set)
409 * if the section/option is to be modified and it is not found
410 * create a new element in the appropriate list
411 */
412 e = uci_lookup_list(ctx, &p->sections, section);
413 if (!e)
414 goto notfound;
415
416 s = uci_to_section(e);
417 if (ctx->pctx && ctx->pctx->merge)
418 ctx->pctx->section = s;
419
420 if (option) {
421 e = uci_lookup_list(ctx, &s->options, option);
422 if (!e)
423 goto notfound;
424 o = uci_to_option(e);
425 }
426
427 /*
428 * no unknown element was supplied, assume that we can just update
429 * an existing entry
430 */
431 if (o)
432 e = &o->e;
433 else
434 e = &s->e;
435 if (result)
436 *result = e;
437 else
438 result = &e;
439
440 ctx->internal = internal;
441 return uci_set_element_value(ctx, result, value);
442
443 notfound:
444 /*
445 * the entry that we need to update was not found,
446 * check if the search failed prematurely.
447 * this can happen if the package was not found, or if
448 * an option was supplied, but the section wasn't found
449 */
450 if (!p || (!s && option))
451 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
452
453 /* now add the missing entry */
454 if (!internal && p->confdir)
455 uci_add_history(ctx, &p->history, UCI_CMD_ADD, section, option, value);
456 if (s) {
457 o = uci_alloc_option(s, option, value);
458 if (result)
459 *result = &o->e;
460 } else {
461 s = uci_alloc_section(p, value, section);
462 if (result)
463 *result = &s->e;
464 if (ctx->pctx && ctx->pctx->merge)
465 ctx->pctx->section = s;
466 }
467
468 return 0;
469 }
470
471 int uci_unload(struct uci_context *ctx, struct uci_package *p)
472 {
473 UCI_HANDLE_ERR(ctx);
474 UCI_ASSERT(ctx, p != NULL);
475
476 uci_free_package(&p);
477 return 0;
478 }
479