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