bump library version to 0.5
[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->err);
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 /* fix up an unnamed section, e.g. after adding options to it */
119 static void uci_fixup_section(struct uci_context *ctx, struct uci_section *s)
120 {
121         unsigned int hash = ~0;
122         struct uci_element *e;
123         char buf[16];
124
125         if (!s || s->e.name)
126                 return;
127
128         /*
129          * Generate a name for unnamed sections. This is used as reference
130          * when locating or updating the section from apps/scripts.
131          * To make multiple concurrent versions somewhat safe for updating,
132          * the name is generated from a hash of its type and name/value
133          * pairs of its option, and it is prefixed by a counter value.
134          * If the order of the unnamed sections changes for some reason,
135          * updates to them will be rejected.
136          */
137         hash = djbhash(hash, s->type);
138         uci_foreach_element(&s->options, e) {
139                 hash = djbhash(hash, e->name);
140                 hash = djbhash(hash, uci_to_option(e)->value);
141         }
142         sprintf(buf, "cfg%02x%04x", ++s->package->n_section, hash % (1 << 16));
143         s->e.name = uci_strdup(ctx, buf);
144 }
145
146 static struct uci_section *
147 uci_alloc_section(struct uci_package *p, const char *type, const char *name)
148 {
149         struct uci_context *ctx = p->ctx;
150         struct uci_section *s;
151
152         if (name && !name[0])
153                 name = NULL;
154
155         s = uci_alloc_element(ctx, section, name, strlen(type) + 1);
156         uci_list_init(&s->options);
157         s->type = uci_dataptr(s);
158         s->package = p;
159         strcpy(s->type, type);
160         if (name == NULL)
161                 s->anonymous = true;
162         p->n_section++;
163
164         uci_list_add(&p->sections, &s->e.list);
165
166         return s;
167 }
168
169 static void
170 uci_free_section(struct uci_section *s)
171 {
172         struct uci_element *o, *tmp;
173
174         uci_foreach_element_safe(&s->options, tmp, o) {
175                 uci_free_option(uci_to_option(o));
176         }
177         if ((s->type != uci_dataptr(s)) &&
178                 (s->type != NULL))
179                 free(s->type);
180         uci_free_element(&s->e);
181 }
182
183 __plugin struct uci_package *
184 uci_alloc_package(struct uci_context *ctx, const char *name)
185 {
186         struct uci_package *p;
187
188         p = uci_alloc_element(ctx, package, name, 0);
189         p->ctx = ctx;
190         uci_list_init(&p->sections);
191         uci_list_init(&p->history);
192         uci_list_init(&p->saved_history);
193         return p;
194 }
195
196 static void
197 uci_free_package(struct uci_package **package)
198 {
199         struct uci_element *e, *tmp;
200         struct uci_package *p = *package;
201
202         if(!p)
203                 return;
204
205         if (p->path)
206                 free(p->path);
207         uci_foreach_element_safe(&p->sections, tmp, e) {
208                 uci_free_section(uci_to_section(e));
209         }
210         uci_foreach_element_safe(&p->history, tmp, e) {
211                 uci_free_history(uci_to_history(e));
212         }
213         uci_foreach_element_safe(&p->saved_history, tmp, e) {
214                 uci_free_history(uci_to_history(e));
215         }
216         uci_free_element(&p->e);
217         *package = NULL;
218 }
219
220 static struct uci_element *uci_lookup_list(struct uci_list *list, const char *name)
221 {
222         struct uci_element *e;
223
224         uci_foreach_element(list, e) {
225                 if (!strcmp(e->name, name))
226                         return e;
227         }
228         return NULL;
229 }
230
231 int uci_lookup_ext(struct uci_context *ctx, struct uci_element **res, char *ptr)
232 {
233         struct uci_package *p = NULL;
234         struct uci_element *e;
235         struct uci_section *s;
236         char *package = NULL;
237         char *section = NULL;
238         char *option = NULL;
239         char *idxstr, *t;
240         int idx, c;
241
242         UCI_HANDLE_ERR(ctx);
243         UCI_ASSERT(ctx, res != NULL);
244         UCI_ASSERT(ctx, ptr != NULL);
245
246         UCI_INTERNAL(uci_parse_tuple, ctx, ptr, &package, &section, &option, NULL);
247
248         /* look up the package first */
249         e = uci_lookup_list(&ctx->root, package);
250         if (!e) {
251                 UCI_INTERNAL(uci_load, ctx, package, &p);
252                 if (!p)
253                         goto notfound;
254                 e = &p->e;
255         } else {
256                 p = uci_to_package(e);
257         }
258
259         if (!section)
260                 goto done;
261
262         /* if the section name validates as a regular name, pass through
263          * to the regular uci_lookup function call */
264         if (!*section || uci_validate_name(section)) {
265                 UCI_INTERNAL(uci_lookup, ctx, &e, p, section, option);
266                 goto done;
267         }
268
269         /* name did not validate, that means we have an extended lookup call
270          * parse it here. for now only the section index syntax is supported */
271         if (section[0] != '@')
272                 goto error;
273
274         section++;
275
276         /* parse the section index part */
277         idxstr = strchr(section, '[');
278         if (!idxstr)
279                 goto error;
280         *idxstr = 0;
281         idxstr++;
282
283         t = strchr(idxstr, ']');
284         if (!t)
285                 goto error;
286         if (t[1] != 0)
287                 goto error;
288         *t = 0;
289
290         t = NULL;
291         idx = strtol(idxstr, &t, 10);
292         if (t && *t)
293                 goto error;
294
295         if (!*section)
296                 section = NULL;
297         if (section && !uci_validate_str(section, false))
298                 goto error;
299
300         /* if the given index is negative, it specifies the section number from 
301          * the end of the list */
302         if (idx < 0) {
303                 c = 0;
304                 uci_foreach_element(&p->sections, e) {
305                         s = uci_to_section(e);
306                         if (section && (strcmp(s->type, section) != 0))
307                                 continue;
308
309                         c++;
310                 }
311                 idx += c;
312         }
313
314         c = 0;
315         uci_foreach_element(&p->sections, e) {
316                 s = uci_to_section(e);
317                 if (section && (strcmp(s->type, section) != 0))
318                         continue;
319
320                 if (idx == c)
321                         goto found;
322                 c++;
323         }
324         goto notfound;
325
326 found:
327         if (option)
328                 e = uci_lookup_list(&s->options, option);
329 done:
330         *res = e;
331         return 0;
332
333 notfound:
334         UCI_THROW(ctx, UCI_ERR_NOTFOUND);
335 error:
336         UCI_THROW(ctx, UCI_ERR_INVAL);
337         return 0;
338 }
339
340 int uci_lookup(struct uci_context *ctx, struct uci_element **res, struct uci_package *p, const char *section, const char *option)
341 {
342         struct uci_element *e;
343         struct uci_section *s;
344
345         UCI_HANDLE_ERR(ctx);
346         UCI_ASSERT(ctx, res != NULL);
347         UCI_ASSERT(ctx, p != NULL);
348         UCI_ASSERT(ctx, section && uci_validate_name(section));
349         if (option)
350                 UCI_ASSERT(ctx, uci_validate_name(option));
351
352         e = uci_lookup_list(&p->sections, section);
353         if (!e)
354                 goto notfound;
355
356         if (option) {
357                 s = uci_to_section(e);
358                 e = uci_lookup_list(&s->options, option);
359                 if (!e)
360                         goto notfound;
361         }
362
363         *res = e;
364         return 0;
365
366 notfound:
367         UCI_THROW(ctx, UCI_ERR_NOTFOUND);
368         return 0;
369 }
370
371 int uci_del_element(struct uci_context *ctx, struct uci_element *e)
372 {
373         /* NB: UCI_INTERNAL use means without history tracking */
374         bool internal = ctx->internal;
375         struct uci_package *p = NULL;
376         struct uci_section *s = NULL;
377         struct uci_option *o = NULL;
378         struct uci_element *i, *tmp;
379         char *option = NULL;
380
381         UCI_HANDLE_ERR(ctx);
382         UCI_ASSERT(ctx, e != NULL);
383
384         switch(e->type) {
385         case UCI_TYPE_SECTION:
386                 s = uci_to_section(e);
387                 uci_foreach_element_safe(&s->options, tmp, i) {
388                         uci_del_element(ctx, i);
389                 }
390                 break;
391         case UCI_TYPE_OPTION:
392                 o = uci_to_option(e);
393                 s = o->section;
394                 p = s->package;
395                 option = e->name;
396                 break;
397         default:
398                 UCI_THROW(ctx, UCI_ERR_INVAL);
399                 break;
400         }
401
402         p = s->package;
403         if (!internal && p->has_history)
404                 uci_add_history(ctx, &p->history, UCI_CMD_REMOVE, s->e.name, option, NULL);
405
406         switch(e->type) {
407         case UCI_TYPE_SECTION:
408                 uci_free_section(s);
409                 break;
410         case UCI_TYPE_OPTION:
411                 uci_free_option(o);
412                 break;
413         default:
414                 break;
415         }
416         return 0;
417 }
418
419 int uci_set_element_value(struct uci_context *ctx, struct uci_element **element, const char *value)
420 {
421         /* NB: UCI_INTERNAL use means without history tracking */
422         bool internal = ctx->internal;
423         struct uci_list *list;
424         struct uci_element *e;
425         struct uci_package *p;
426         struct uci_section *s;
427         struct uci_option *o;
428         char *section;
429         char *option;
430         char *str;
431         int size;
432
433         UCI_HANDLE_ERR(ctx);
434         UCI_ASSERT(ctx, (element != NULL) && (*element != NULL));
435
436         /* what the 'value' of an element means depends on the type
437          * for a section, the 'value' means its type
438          * for an option, the 'value' means its value string
439          * when changing the value, shrink the element to its actual size
440          * (it may have been allocated with a bigger size, to include
441          *  its buffer)
442          * then duplicate the string passed on the command line and
443          * insert it into the structure.
444          */
445         e = *element;
446         list = e->list.prev;
447         switch(e->type) {
448         case UCI_TYPE_SECTION:
449                 UCI_ASSERT(ctx, uci_validate_str(value, false));
450                 size = sizeof(struct uci_section);
451                 s = uci_to_section(e);
452                 section = e->name;
453                 option = NULL;
454                 /* matches the currently set value */
455                 if (!strcmp(value, s->type))
456                         return 0;
457                 break;
458         case UCI_TYPE_OPTION:
459                 UCI_ASSERT(ctx, value != NULL);
460                 size = sizeof(struct uci_option);
461                 o = uci_to_option(e);
462                 s = o->section;
463                 section = s->e.name;
464                 option = o->e.name;
465                 /* matches the currently set value */
466                 if (!strcmp(value, o->value))
467                         return 0;
468                 break;
469         default:
470                 UCI_THROW(ctx, UCI_ERR_INVAL);
471                 return 0;
472         }
473         p = s->package;
474         if (!internal && p->has_history)
475                 uci_add_history(ctx, &p->history, UCI_CMD_CHANGE, section, option, value);
476
477         uci_list_del(&e->list);
478         e = uci_realloc(ctx, e, size);
479         str = uci_strdup(ctx, value);
480         uci_list_insert(list, &e->list);
481         *element = e;
482
483         switch(e->type) {
484         case UCI_TYPE_SECTION:
485                 uci_to_section(e)->type = str;
486                 break;
487         case UCI_TYPE_OPTION:
488                 uci_to_option(e)->value = str;
489                 break;
490         default:
491                 break;
492         }
493
494         return 0;
495 }
496
497 int uci_rename(struct uci_context *ctx, struct uci_package *p, char *section, char *option, char *name)
498 {
499         /* NB: UCI_INTERNAL use means without history tracking */
500         bool internal = ctx->internal;
501         struct uci_element *e;
502
503         UCI_HANDLE_ERR(ctx);
504
505         /* NB: p, section, option validated by uci_lookup */
506         UCI_INTERNAL(uci_lookup, ctx, &e, p, section, option);
507
508         if (!internal && p->has_history)
509                 uci_add_history(ctx, &p->history, UCI_CMD_RENAME, section, option, name);
510
511         name = uci_strdup(ctx, name);
512         if (e->name)
513                 free(e->name);
514         e->name = name;
515
516         return 0;
517 }
518
519 int uci_add_section(struct uci_context *ctx, struct uci_package *p, const char *type, struct uci_section **res)
520 {
521         bool internal = ctx->internal;
522         struct uci_section *s;
523
524         UCI_HANDLE_ERR(ctx);
525         UCI_ASSERT(ctx, p != NULL);
526         s = uci_alloc_section(p, type, NULL);
527         uci_fixup_section(ctx, s);
528         *res = s;
529         if (!internal && p->has_history)
530                 uci_add_history(ctx, &p->history, UCI_CMD_ADD, s->e.name, NULL, type);
531
532         return 0;
533 }
534
535 int uci_delete(struct uci_context *ctx, struct uci_package *p, const char *section, const char *option)
536 {
537         /* NB: pass on internal flag to uci_del_element */
538         bool internal = ctx->internal;
539         struct uci_element *e;
540
541         UCI_HANDLE_ERR(ctx);
542
543         /* NB: p, section, option validated by uci_lookup */
544         UCI_INTERNAL(uci_lookup, ctx, &e, p, section, option);
545
546         ctx->internal = internal;
547         return uci_del_element(ctx, e);
548 }
549
550 int uci_set(struct uci_context *ctx, struct uci_package *p, const char *section, const char *option, const char *value, struct uci_element **result)
551 {
552         /* NB: UCI_INTERNAL use means without history tracking */
553         bool internal = ctx->internal;
554         struct uci_element *e = NULL;
555         struct uci_section *s = NULL;
556         struct uci_option *o = NULL;
557
558         UCI_HANDLE_ERR(ctx);
559         UCI_ASSERT(ctx, p != NULL);
560         UCI_ASSERT(ctx, uci_validate_name(section));
561         if (option) {
562                 UCI_ASSERT(ctx, uci_validate_name(option));
563                 UCI_ASSERT(ctx, value != NULL);
564         } else {
565                 UCI_ASSERT(ctx, uci_validate_str(value, false));
566         }
567
568         /*
569          * look up the package, section and option (if set)
570          * if the section/option is to be modified and it is not found
571          * create a new element in the appropriate list
572          */
573         e = uci_lookup_list(&p->sections, section);
574         if (!e)
575                 goto notfound;
576
577         s = uci_to_section(e);
578         if (ctx->pctx && ctx->pctx->merge)
579                 ctx->pctx->section = s;
580
581         if (option) {
582                 e = uci_lookup_list(&s->options, option);
583                 if (!e)
584                         goto notfound;
585                 o = uci_to_option(e);
586         }
587
588         /* 
589          * no unknown element was supplied, assume that we can just update 
590          * an existing entry
591          */
592         if (o)
593                 e = &o->e;
594         else
595                 e = &s->e;
596         if (result)
597                 *result = e;
598         else
599                 result = &e;
600
601         ctx->internal = internal;
602         return uci_set_element_value(ctx, result, value);
603
604 notfound:
605         /* 
606          * the entry that we need to update was not found,
607          * check if the search failed prematurely.
608          * this can happen if the package was not found, or if
609          * an option was supplied, but the section wasn't found
610          */
611         if (!p || (!s && option))
612                 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
613
614         /* now add the missing entry */
615         if (!internal && p->has_history)
616                 uci_add_history(ctx, &p->history, UCI_CMD_CHANGE, section, option, value);
617         if (s) {
618                 o = uci_alloc_option(s, option, value);
619                 if (result)
620                         *result = &o->e;
621         } else {
622                 s = uci_alloc_section(p, value, section);
623                 if (result)
624                         *result = &s->e;
625                 if (ctx->pctx && ctx->pctx->merge)
626                         ctx->pctx->section = s;
627         }
628
629         return 0;
630 }
631
632 int uci_unload(struct uci_context *ctx, struct uci_package *p)
633 {
634         UCI_HANDLE_ERR(ctx);
635         UCI_ASSERT(ctx, p != NULL);
636
637         uci_free_package(&p);
638         return 0;
639 }
640