some helpful comments
[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 static struct uci_package *
148 uci_alloc_package(struct uci_context *ctx, const char *name)
149 {
150         struct uci_package *p;
151
152         p = uci_alloc_element(ctx, package, name, 0);
153         p->ctx = ctx;
154         uci_list_init(&p->sections);
155         uci_list_init(&p->history);
156         return p;
157 }
158
159 static void
160 uci_free_package(struct uci_package *p)
161 {
162         struct uci_element *e, *tmp;
163
164         if(!p)
165                 return;
166
167         if (p->path)
168                 free(p->path);
169         uci_foreach_element_safe(&p->sections, tmp, e) {
170                 uci_free_section(uci_to_section(e));
171         }
172         uci_free_element(&p->e);
173 }
174
175 /* record a change that was done to a package */
176 static void
177 uci_add_history(struct uci_context *ctx, struct uci_package *p, int cmd, char *section, char *option, char *value)
178 {
179         struct uci_history *h;
180         int size = strlen(section) + 1;
181         char *ptr;
182
183         if (!p->confdir)
184                 return;
185
186         if (value)
187                 size += strlen(section) + 1;
188
189         h = uci_alloc_element(ctx, history, option, size);
190         ptr = uci_dataptr(h);
191         h->cmd = cmd;
192         h->section = strcpy(ptr, section);
193         if (value) {
194                 ptr += strlen(ptr) + 1;
195                 h->value = strcpy(ptr, value);
196         }
197         uci_list_add(&p->history, &h->e.list);
198 }
199
200 static void
201 uci_free_history(struct uci_history *h)
202 {
203         if (!h)
204                 return;
205         if ((h->section != NULL) &&
206                 (h->section != uci_dataptr(h))) {
207                 free(h->section);
208                 free(h->value);
209         }
210         uci_free_element(&h->e);
211 }
212
213 static struct uci_element *uci_lookup_list(struct uci_context *ctx, struct uci_list *list, const char *name)
214 {
215         struct uci_element *e;
216
217         uci_foreach_element(list, e) {
218                 if (!strcmp(e->name, name))
219                         return e;
220         }
221         return NULL;
222 }
223
224 int uci_lookup(struct uci_context *ctx, struct uci_element **res, struct uci_package *p, char *section, char *option)
225 {
226         struct uci_element *e;
227         struct uci_section *s;
228         struct uci_option *o;
229
230         UCI_HANDLE_ERR(ctx);
231         UCI_ASSERT(ctx, res != NULL);
232         UCI_ASSERT(ctx, p != NULL);
233         UCI_ASSERT(ctx, section != NULL);
234
235         e = uci_lookup_list(ctx, &p->sections, section);
236         if (!e)
237                 goto notfound;
238
239         if (option) {
240                 s = uci_to_section(e);
241                 e = uci_lookup_list(ctx, &s->options, option);
242         }
243
244         *res = e;
245         return 0;
246
247 notfound:
248         UCI_THROW(ctx, UCI_ERR_NOTFOUND);
249         return 0;
250 }
251
252 int uci_del_element(struct uci_context *ctx, struct uci_element *e)
253 {
254         /* NB: UCI_INTERNAL use means without history tracking */
255         bool internal = ctx->internal;
256         struct uci_package *p = NULL;
257         struct uci_section *s = NULL;
258         struct uci_option *o = NULL;
259         struct uci_element *i, *tmp;
260         char *option = NULL;
261
262         UCI_HANDLE_ERR(ctx);
263         UCI_ASSERT(ctx, e != NULL);
264
265         switch(e->type) {
266         case UCI_TYPE_SECTION:
267                 s = uci_to_section(e);
268                 uci_foreach_element_safe(&s->options, tmp, i) {
269                         uci_del_element(ctx, i);
270                 }
271                 break;
272         case UCI_TYPE_OPTION:
273                 o = uci_to_option(e);
274                 s = o->section;
275                 p = s->package;
276                 option = e->name;
277                 break;
278         default:
279                 UCI_THROW(ctx, UCI_ERR_INVAL);
280                 break;
281         }
282
283         p = s->package;
284         if (!internal)
285                 uci_add_history(ctx, p, UCI_CMD_REMOVE, s->e.name, option, NULL);
286
287         switch(e->type) {
288         case UCI_TYPE_SECTION:
289                 uci_free_section(s);
290                 break;
291         case UCI_TYPE_OPTION:
292                 uci_free_option(o);
293                 break;
294         default:
295                 break;
296         }
297         return 0;
298 }
299
300 int uci_set_element_value(struct uci_context *ctx, struct uci_element **element, char *value)
301 {
302         /* NB: UCI_INTERNAL use means without history tracking */
303         bool internal = ctx->internal;
304         struct uci_list *list;
305         struct uci_element *e;
306         struct uci_package *p;
307         struct uci_section *s;
308         char *section;
309         char *option;
310         char *str;
311         int size;
312
313         UCI_HANDLE_ERR(ctx);
314         UCI_ASSERT(ctx, value != NULL);
315         UCI_ASSERT(ctx, element != NULL);
316         UCI_ASSERT(ctx, *element != NULL);
317
318         /* what the 'value' of an element means depends on the type
319          * for a section, the 'value' means its type
320          * for an option, the 'value' means its value string
321          * when changing the value, shrink the element to its actual size
322          * (it may have been allocated with a bigger size, to include
323          *  its buffer)
324          * then duplicate the string passed on the command line and
325          * insert it into the structure.
326          */
327         e = *element;
328         list = e->list.prev;
329         switch(e->type) {
330         case UCI_TYPE_SECTION:
331                 size = sizeof(struct uci_section);
332                 s = uci_to_section(e);
333                 section = e->name;
334                 option = NULL;
335                 break;
336         case UCI_TYPE_OPTION:
337                 size = sizeof(struct uci_option);
338                 s = uci_to_option(e)->section;
339                 section = s->e.name;
340                 option = e->name;
341                 break;
342         default:
343                 UCI_THROW(ctx, UCI_ERR_INVAL);
344                 return 0;
345         }
346         p = s->package;
347         if (!internal)
348                 uci_add_history(ctx, p, UCI_CMD_CHANGE, section, option, value);
349
350         uci_list_del(&e->list);
351         e = uci_realloc(ctx, e, size);
352         str = uci_strdup(ctx, value);
353         uci_list_insert(list, &e->list);
354         *element = e;
355
356         switch(e->type) {
357         case UCI_TYPE_SECTION:
358                 uci_to_section(e)->type = str;
359                 break;
360         case UCI_TYPE_OPTION:
361                 uci_to_option(e)->value = str;
362                 break;
363         default:
364                 break;
365         }
366
367         return 0;
368 }
369
370 int uci_del(struct uci_context *ctx, struct uci_package *p, char *section, char *option)
371 {
372         /* NB: pass on internal flag to uci_del_element */
373         bool internal = ctx->internal;
374         struct uci_element *e;
375         struct uci_section *s = NULL;
376         struct uci_option *o = NULL;
377
378         UCI_HANDLE_ERR(ctx);
379         UCI_ASSERT(ctx, p != NULL);
380         UCI_ASSERT(ctx, section != NULL);
381
382         UCI_INTERNAL(uci_lookup, ctx, &e, p, section, option);
383
384         if (!internal)
385                 return uci_del_element(ctx, e);
386         UCI_INTERNAL(uci_del_element, ctx, e);
387
388         return 0;
389 }
390
391 int uci_set(struct uci_context *ctx, struct uci_package *p, char *section, char *option, char *value)
392 {
393         /* NB: UCI_INTERNAL use means without history tracking */
394         bool internal = ctx->internal;
395         struct uci_element *e = NULL;
396         struct uci_section *s = NULL;
397         struct uci_option *o = NULL;
398         struct uci_history *h;
399
400         UCI_HANDLE_ERR(ctx);
401         UCI_ASSERT(ctx, p != NULL);
402         UCI_ASSERT(ctx, section != NULL);
403         UCI_ASSERT(ctx, value != NULL);
404
405         /*
406          * look up the package, section and option (if set)
407          * if the section/option is to be modified and it is not found
408          * create a new element in the appropriate list
409          */
410         UCI_INTERNAL(uci_lookup, ctx, &e, p, section, NULL);
411         s = uci_to_section(e);
412         if (option) {
413                 e = uci_lookup_list(ctx, &s->options, option);
414                 if (!e)
415                         goto notfound;
416                 o = uci_to_option(e);
417         }
418
419         /* 
420          * no unknown element was supplied, assume that we can just update 
421          * an existing entry
422          */
423         if (o)
424                 e = &o->e;
425         else
426                 e = &s->e;
427
428         if (!internal)
429                 return uci_set_element_value(ctx, &e, value);
430
431         UCI_INTERNAL(uci_set_element_value, ctx, &e, value);
432         return 0;
433
434 notfound:
435         /* 
436          * the entry that we need to update was not found,
437          * check if the search failed prematurely.
438          * this can happen if the package was not found, or if
439          * an option was supplied, but the section wasn't found
440          */
441         if (!p || (!s && option))
442                 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
443
444         /* now add the missing entry */
445         if (!internal)
446                 uci_add_history(ctx, p, UCI_CMD_ADD, section, option, value);
447         if (s)
448                 uci_alloc_option(s, option, value);
449         else
450                 uci_alloc_section(p, value, section);
451
452         return 0;
453 }
454
455 int uci_unload(struct uci_context *ctx, struct uci_package *p)
456 {
457         struct uci_element *e;
458
459         UCI_HANDLE_ERR(ctx);
460         UCI_ASSERT(ctx, p != NULL);
461
462         uci_free_package(p);
463         return 0;
464 }
465