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