uci_revert api cleanup
[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 /* initialize a list head/item */
16 static inline void uci_list_init(struct uci_list *ptr)
17 {
18         ptr->prev = ptr;
19         ptr->next = ptr;
20 }
21
22 /* inserts a new list entry after a given entry */
23 static inline void uci_list_insert(struct uci_list *list, struct uci_list *ptr)
24 {
25         list->next->prev = ptr;
26         ptr->prev = list;
27         ptr->next = list->next;
28         list->next = ptr;
29 }
30
31 /* inserts a new list entry at the tail of the list */
32 static inline void uci_list_add(struct uci_list *head, struct uci_list *ptr)
33 {
34         /* NB: head->prev points at the tail */
35         uci_list_insert(head->prev, ptr);
36 }
37
38 static inline void uci_list_del(struct uci_list *ptr)
39 {
40         struct uci_list *next, *prev;
41
42         next = ptr->next;
43         prev = ptr->prev;
44
45         prev->next = next;
46         next->prev = prev;
47
48         uci_list_init(ptr);
49 }
50
51 /* 
52  * uci_alloc_generic allocates a new uci_element with payload
53  * payload is appended to the struct to save memory and reduce fragmentation
54  */
55 static struct uci_element *
56 uci_alloc_generic(struct uci_context *ctx, int type, const char *name, int size)
57 {
58         struct uci_element *e;
59         int datalen = size;
60         void *ptr;
61
62         ptr = uci_malloc(ctx, datalen);
63         e = (struct uci_element *) ptr;
64         e->type = type;
65         if (name) {
66                 UCI_TRAP_SAVE(ctx, error);
67                 e->name = uci_strdup(ctx, name);
68                 UCI_TRAP_RESTORE(ctx);
69         }
70         uci_list_init(&e->list);
71         goto done;
72
73 error:
74         free(ptr);
75         UCI_THROW(ctx, ctx->err);
76
77 done:
78         return e;
79 }
80
81 static void
82 uci_free_element(struct uci_element *e)
83 {
84         if (e->name)
85                 free(e->name);
86         if (!uci_list_empty(&e->list))
87                 uci_list_del(&e->list);
88         free(e);
89 }
90
91 static struct uci_option *
92 uci_alloc_option(struct uci_section *s, const char *name, const char *value)
93 {
94         struct uci_package *p = s->package;
95         struct uci_context *ctx = p->ctx;
96         struct uci_option *o;
97
98         o = uci_alloc_element(ctx, option, name, strlen(value) + 1);
99         o->type = UCI_TYPE_STRING;
100         o->v.string = uci_dataptr(o);
101         o->section = s;
102         strcpy(o->v.string, value);
103         uci_list_add(&s->options, &o->e.list);
104
105         return o;
106 }
107
108 static inline void
109 uci_free_option(struct uci_option *o)
110 {
111         struct uci_element *e, *tmp;
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         case UCI_TYPE_LIST:
120                 uci_foreach_element_safe(&o->v.list, tmp, e) {
121                         uci_free_element(e);
122                 }
123                 break;
124         default:
125                 break;
126         }
127         uci_free_element(&o->e);
128 }
129
130 static struct uci_option *
131 uci_alloc_list(struct uci_section *s, const char *name)
132 {
133         struct uci_package *p = s->package;
134         struct uci_context *ctx = p->ctx;
135         struct uci_option *o;
136
137         o = uci_alloc_element(ctx, option, name, 0);
138         o->type = UCI_TYPE_LIST;
139         o->section = s;
140         uci_list_init(&o->v.list);
141         uci_list_add(&s->options, &o->e.list);
142
143         return o;
144 }
145
146 /* fix up an unnamed section, e.g. after adding options to it */
147 static void uci_fixup_section(struct uci_context *ctx, struct uci_section *s)
148 {
149         unsigned int hash = ~0;
150         struct uci_element *e;
151         char buf[16];
152
153         if (!s || s->e.name)
154                 return;
155
156         /*
157          * Generate a name for unnamed sections. This is used as reference
158          * when locating or updating the section from apps/scripts.
159          * To make multiple concurrent versions somewhat safe for updating,
160          * the name is generated from a hash of its type and name/value
161          * pairs of its option, and it is prefixed by a counter value.
162          * If the order of the unnamed sections changes for some reason,
163          * updates to them will be rejected.
164          */
165         hash = djbhash(hash, s->type);
166         uci_foreach_element(&s->options, e) {
167                 struct uci_option *o;
168                 hash = djbhash(hash, e->name);
169                 o = uci_to_option(e);
170                 switch(o->type) {
171                 case UCI_TYPE_STRING:
172                         hash = djbhash(hash, o->v.string);
173                         break;
174                 default:
175                         break;
176                 }
177         }
178         sprintf(buf, "cfg%02x%04x", ++s->package->n_section, hash % (1 << 16));
179         s->e.name = uci_strdup(ctx, buf);
180 }
181
182 static struct uci_section *
183 uci_alloc_section(struct uci_package *p, const char *type, const char *name)
184 {
185         struct uci_context *ctx = p->ctx;
186         struct uci_section *s;
187
188         if (name && !name[0])
189                 name = NULL;
190
191         s = uci_alloc_element(ctx, section, name, strlen(type) + 1);
192         uci_list_init(&s->options);
193         s->type = uci_dataptr(s);
194         s->package = p;
195         strcpy(s->type, type);
196         if (name == NULL)
197                 s->anonymous = true;
198         p->n_section++;
199
200         uci_list_add(&p->sections, &s->e.list);
201
202         return s;
203 }
204
205 static void
206 uci_free_section(struct uci_section *s)
207 {
208         struct uci_element *o, *tmp;
209
210         uci_foreach_element_safe(&s->options, tmp, o) {
211                 uci_free_option(uci_to_option(o));
212         }
213         if ((s->type != uci_dataptr(s)) &&
214                 (s->type != NULL))
215                 free(s->type);
216         uci_free_element(&s->e);
217 }
218
219 __plugin struct uci_package *
220 uci_alloc_package(struct uci_context *ctx, const char *name)
221 {
222         struct uci_package *p;
223
224         p = uci_alloc_element(ctx, package, name, 0);
225         p->ctx = ctx;
226         uci_list_init(&p->sections);
227         uci_list_init(&p->history);
228         uci_list_init(&p->saved_history);
229         return p;
230 }
231
232 static void
233 uci_free_package(struct uci_package **package)
234 {
235         struct uci_element *e, *tmp;
236         struct uci_package *p = *package;
237
238         if(!p)
239                 return;
240
241         if (p->path)
242                 free(p->path);
243         uci_foreach_element_safe(&p->sections, tmp, e) {
244                 uci_free_section(uci_to_section(e));
245         }
246         uci_foreach_element_safe(&p->history, tmp, e) {
247                 uci_free_history(uci_to_history(e));
248         }
249         uci_foreach_element_safe(&p->saved_history, tmp, e) {
250                 uci_free_history(uci_to_history(e));
251         }
252         uci_free_element(&p->e);
253         *package = NULL;
254 }
255
256 static void
257 uci_free_any(struct uci_element **e)
258 {
259         switch((*e)->type) {
260         case UCI_TYPE_SECTION:
261                 uci_free_section(uci_to_section(*e));
262                 break;
263         case UCI_TYPE_OPTION:
264                 uci_free_option(uci_to_option(*e));
265                 break;
266         default:
267                 break;
268         }
269         *e = NULL;
270 }
271
272 static inline struct uci_element *
273 uci_lookup_list(struct uci_list *list, const char *name)
274 {
275         struct uci_element *e;
276
277         uci_foreach_element(list, e) {
278                 if (!strcmp(e->name, name))
279                         return e;
280         }
281         return NULL;
282 }
283
284 static struct uci_element *
285 uci_lookup_ext_section(struct uci_context *ctx, struct uci_ptr *ptr)
286 {
287         char *idxstr, *t, *section, *name;
288         struct uci_element *e = NULL;
289         struct uci_section *s;
290         int idx, c;
291
292         section = uci_strdup(ctx, ptr->section);
293         name = idxstr = section + 1;
294
295         if (section[0] != '@')
296                 goto error;
297
298         /* parse the section index part */
299         idxstr = strchr(idxstr, '[');
300         if (!idxstr)
301                 goto error;
302         *idxstr = 0;
303         idxstr++;
304
305         t = strchr(idxstr, ']');
306         if (!t)
307                 goto error;
308         if (t[1] != 0)
309                 goto error;
310         *t = 0;
311
312         t = NULL;
313         idx = strtol(idxstr, &t, 10);
314         if (t && *t)
315                 goto error;
316
317         if (!*name)
318                 name = NULL;
319         else if (!uci_validate_str(name, false))
320                 goto error;
321
322         /* if the given index is negative, it specifies the section number from 
323          * the end of the list */
324         if (idx < 0) {
325                 c = 0;
326                 uci_foreach_element(&ptr->p->sections, e) {
327                         s = uci_to_section(e);
328                         if (name && (strcmp(s->type, name) != 0))
329                                 continue;
330
331                         c++;
332                 }
333                 idx += c;
334         }
335
336         c = 0;
337         uci_foreach_element(&ptr->p->sections, e) {
338                 s = uci_to_section(e);
339                 if (name && (strcmp(s->type, name) != 0))
340                         continue;
341
342                 if (idx == c)
343                         goto done;
344                 c++;
345         }
346         e = NULL;
347         goto done;
348
349 error:
350         e = NULL;
351         memset(ptr, 0, sizeof(struct uci_ptr));
352         UCI_THROW(ctx, UCI_ERR_INVAL);
353 done:
354         free(section);
355         ptr->section = e->name;
356         return e;
357 }
358
359 int
360 uci_lookup_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str, bool extended)
361 {
362         struct uci_element *e;
363
364         UCI_HANDLE_ERR(ctx);
365         UCI_ASSERT(ctx, ptr != NULL);
366
367         if (str)
368                 UCI_INTERNAL(uci_parse_ptr, ctx, ptr, str);
369
370         ptr->flags |= UCI_LOOKUP_DONE;
371
372         /* look up the package first */
373         e = uci_lookup_list(&ctx->root, ptr->package);
374         if (!e) {
375                 UCI_INTERNAL(uci_load, ctx, ptr->package, &ptr->p);
376                 if (!ptr->p)
377                         goto notfound;
378                 ptr->last = &ptr->p->e;
379         } else {
380                 ptr->p = uci_to_package(e);
381                 ptr->last = e;
382         }
383
384         if (!ptr->section)
385                 goto complete;
386
387         /* if the section name validates as a regular name, pass through
388          * to the regular uci_lookup function call */
389         if (ptr->flags & UCI_LOOKUP_EXTENDED)
390                 e = uci_lookup_ext_section(ctx, ptr);
391         else
392                 e = uci_lookup_list(&ptr->p->sections, ptr->section);
393
394         if (!e)
395                 goto abort;
396
397         ptr->last = e;
398         ptr->s = uci_to_section(e);
399
400         if (ptr->option) {
401                 e = uci_lookup_list(&ptr->s->options, ptr->option);
402                 if (!e)
403                         goto abort;
404
405                 ptr->o = uci_to_option(e);
406                 ptr->last = e;
407         }
408
409 complete:
410         ptr->flags |= UCI_LOOKUP_COMPLETE;
411 abort:
412         return 0;
413
414 notfound:
415         UCI_THROW(ctx, UCI_ERR_NOTFOUND);
416         return 0;
417 }
418
419 int
420 uci_fill_ptr(struct uci_context *ctx, struct uci_ptr *ptr, struct uci_element *e, bool complete)
421 {
422         UCI_HANDLE_ERR(ctx);
423         UCI_ASSERT(ctx, ptr != NULL);
424         UCI_ASSERT(ctx, e != NULL);
425
426         memset(ptr, 0, sizeof(struct uci_ptr));
427         switch(e->type) {
428         case UCI_TYPE_OPTION:
429                 ptr->o = uci_to_option(e);
430                 goto fill_option;
431         case UCI_TYPE_SECTION:
432                 ptr->s = uci_to_section(e);
433                 goto fill_section;
434         case UCI_TYPE_PACKAGE:
435                 ptr->p = uci_to_package(e);
436                 goto fill_package;
437         default:
438                 UCI_THROW(ctx, UCI_ERR_INVAL);
439         }
440
441 fill_option:
442         ptr->option = ptr->o->e.name;
443         ptr->s = ptr->o->section;
444 fill_section:
445         ptr->section = ptr->s->e.name;
446         ptr->p = ptr->s->package;
447 fill_package:
448         ptr->package = ptr->p->e.name;
449
450         ptr->flags |= UCI_LOOKUP_DONE;
451         if (complete)
452                 ptr->flags |= UCI_LOOKUP_COMPLETE;
453
454         return 0;
455 }
456
457 static struct uci_element *
458 expand_ptr(struct uci_context *ctx, struct uci_ptr *ptr, bool complete)
459 {
460         UCI_ASSERT(ctx, ptr != NULL);
461
462         if (!(ptr->flags & UCI_LOOKUP_DONE))
463                 uci_lookup_ptr(ctx, ptr, NULL, 1);
464         if (complete && !(ptr->flags & UCI_LOOKUP_COMPLETE))
465                 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
466         UCI_ASSERT(ctx, ptr->p != NULL);
467
468         /* fill in missing string info */
469         if (ptr->p && !ptr->package)
470                 ptr->package = ptr->p->e.name;
471         if (ptr->s && !ptr->section)
472                 ptr->section = ptr->s->e.name;
473         if (ptr->o && !ptr->option)
474                 ptr->option = ptr->o->e.name;
475
476         if (ptr->o)
477                 return &ptr->o->e;
478         if (ptr->s)
479                 return &ptr->s->e;
480         if (ptr->p)
481                 return &ptr->p->e;
482         else
483                 return NULL;
484 }
485
486 static void uci_add_element_list(struct uci_context *ctx, struct uci_ptr *ptr, bool internal)
487 {
488         struct uci_element *e;
489         struct uci_package *p;
490
491         p = ptr->p;
492         if (!internal && p->has_history)
493                 uci_add_history(ctx, &p->history, UCI_CMD_LIST_ADD, ptr->section, ptr->option, ptr->value);
494
495         e = uci_alloc_generic(ctx, UCI_TYPE_ITEM, ptr->value, sizeof(struct uci_option));
496         uci_list_add(&ptr->o->v.list, &e->list);
497 }
498
499 int uci_set_element_value(struct uci_context *ctx, struct uci_element **element, const char *value)
500 {
501         /* NB: UCI_INTERNAL use means without history tracking */
502         bool internal = ctx->internal;
503         struct uci_list *list;
504         struct uci_element *e;
505         struct uci_package *p;
506         struct uci_section *s;
507         struct uci_option *o;
508         char *section;
509         char *option;
510         char *str;
511         int size = 0;
512
513         UCI_HANDLE_ERR(ctx);
514         UCI_ASSERT(ctx, (element != NULL) && (*element != NULL));
515
516         /* what the 'value' of an element means depends on the type
517          * for a section, the 'value' means its type
518          * for an option, the 'value' means its value string
519          * when changing the value, shrink the element to its actual size
520          * (it may have been allocated with a bigger size, to include
521          *  its buffer)
522          * then duplicate the string passed on the command line and
523          * insert it into the structure.
524          */
525         e = *element;
526         list = e->list.prev;
527
528         switch(e->type) {
529         case UCI_TYPE_SECTION:
530                 UCI_ASSERT(ctx, uci_validate_str(value, false));
531                 size = sizeof(struct uci_section);
532                 s = uci_to_section(e);
533                 section = e->name;
534                 option = NULL;
535                 /* matches the currently set value */
536                 if (!strcmp(value, s->type))
537                         return 0;
538                 break;
539
540         case UCI_TYPE_OPTION:
541                 UCI_ASSERT(ctx, value != NULL);
542                 o = uci_to_option(e);
543                 s = o->section;
544                 section = s->e.name;
545                 option = o->e.name;
546                 switch(o->type) {
547                 case UCI_TYPE_STRING:
548                         size = sizeof(struct uci_option);
549                         /* matches the currently set value */
550                         if (!strcmp(value, o->v.string))
551                                 return 0;
552                         break;
553                 default:
554                         /* default action for non-string datatypes is to delete
555                          * the existing entry, then re-create it as a string */
556                         break;
557                 }
558                 break;
559
560         default:
561                 UCI_THROW(ctx, UCI_ERR_INVAL);
562                 return 0;
563         }
564         p = s->package;
565         if (!internal && p->has_history)
566                 uci_add_history(ctx, &p->history, UCI_CMD_CHANGE, section, option, value);
567
568         if ((e->type == UCI_TYPE_OPTION) && (size == 0)) {
569                 o = uci_alloc_option(s, option, value);
570                 uci_free_any(&e);
571                 *element = &o->e;
572                 goto done;
573         }
574
575         uci_list_del(&e->list);
576         e = uci_realloc(ctx, e, size);
577         str = uci_strdup(ctx, value);
578         uci_list_insert(list, &e->list);
579         *element = e;
580
581         switch(e->type) {
582         case UCI_TYPE_SECTION:
583                 uci_to_section(e)->type = str;
584                 break;
585         case UCI_TYPE_OPTION:
586                 uci_to_option(e)->v.string = str;
587                 break;
588         default:
589                 break;
590         }
591
592 done:
593         return 0;
594 }
595
596 int uci_rename(struct uci_context *ctx, struct uci_ptr *ptr)
597 {
598         /* NB: UCI_INTERNAL use means without history tracking */
599         bool internal = ctx->internal;
600         struct uci_element *e;
601         struct uci_package *p;
602         char *n;
603
604         UCI_HANDLE_ERR(ctx);
605
606         e = expand_ptr(ctx, ptr, true);
607         p = ptr->p;
608
609         UCI_ASSERT(ctx, ptr->s);
610         UCI_ASSERT(ctx, ptr->value);
611
612         if (!internal && p->has_history)
613                 uci_add_history(ctx, &p->history, UCI_CMD_RENAME, ptr->section, ptr->option, ptr->value);
614
615         n = uci_strdup(ctx, ptr->value);
616         if (e->name)
617                 free(e->name);
618         e->name = n;
619
620         return 0;
621 }
622
623 int uci_add_section(struct uci_context *ctx, struct uci_package *p, const char *type, struct uci_section **res)
624 {
625         bool internal = ctx->internal;
626         struct uci_section *s;
627
628         UCI_HANDLE_ERR(ctx);
629         UCI_ASSERT(ctx, p != NULL);
630         s = uci_alloc_section(p, type, NULL);
631         uci_fixup_section(ctx, s);
632         *res = s;
633         if (!internal && p->has_history)
634                 uci_add_history(ctx, &p->history, UCI_CMD_ADD, s->e.name, NULL, type);
635
636         return 0;
637 }
638
639 int uci_delete(struct uci_context *ctx, struct uci_ptr *ptr)
640 {
641         /* NB: pass on internal flag to uci_del_element */
642         bool internal = ctx->internal;
643         struct uci_package *p;
644         struct uci_element *e;
645
646         UCI_HANDLE_ERR(ctx);
647
648         e = expand_ptr(ctx, ptr, true);
649         p = ptr->p;
650
651         UCI_ASSERT(ctx, ptr->s);
652
653         if (!internal && p->has_history)
654                 uci_add_history(ctx, &p->history, UCI_CMD_REMOVE, ptr->section, ptr->option, NULL);
655
656         uci_free_any(&e);
657         return 0;
658 }
659
660 int uci_add_list(struct uci_context *ctx, struct uci_ptr *ptr)
661 {
662         /* NB: UCI_INTERNAL use means without history tracking */
663         bool internal = ctx->internal;
664         struct uci_option *prev = NULL;
665         const char *value2 = NULL;
666
667         UCI_HANDLE_ERR(ctx);
668
669         expand_ptr(ctx, ptr, false);
670         UCI_ASSERT(ctx, ptr->s);
671         UCI_ASSERT(ctx, ptr->value);
672
673         if (ptr->o) {
674                 switch (ptr->o->type) {
675                 case UCI_TYPE_STRING:
676                         /* we already have a string value, convert that to a list */
677                         prev = ptr->o;
678                         value2 = ptr->value;
679                         ptr->value = ptr->o->v.string;
680                         break;
681                 case UCI_TYPE_LIST:
682                         uci_add_element_list(ctx, ptr, internal);
683                         return 0;
684                 default:
685                         UCI_THROW(ctx, UCI_ERR_INVAL);
686                         break;
687                 }
688         }
689
690         ptr->o = uci_alloc_list(ptr->s, ptr->option);
691         if (prev) {
692                 uci_add_element_list(ctx, ptr, true);
693                 uci_free_option(prev);
694                 ptr->value = value2;
695         }
696         uci_add_element_list(ctx, ptr, internal);
697
698         return 0;
699 }
700
701 int uci_set(struct uci_context *ctx, struct uci_package *p, const char *section, const char *option, const char *value, struct uci_element **result)
702 {
703         /* NB: UCI_INTERNAL use means without history tracking */
704         bool internal = ctx->internal;
705         struct uci_element *e = NULL;
706         struct uci_section *s = NULL;
707         struct uci_option *o = NULL;
708
709         UCI_HANDLE_ERR(ctx);
710         UCI_ASSERT(ctx, p != NULL);
711         UCI_ASSERT(ctx, uci_validate_name(section));
712         if (option) {
713                 UCI_ASSERT(ctx, uci_validate_name(option));
714                 UCI_ASSERT(ctx, value != NULL);
715         } else {
716                 UCI_ASSERT(ctx, uci_validate_str(value, false));
717         }
718
719         /*
720          * look up the package, section and option (if set)
721          * if the section/option is to be modified and it is not found
722          * create a new element in the appropriate list
723          */
724         e = uci_lookup_list(&p->sections, section);
725         if (!e)
726                 goto notfound;
727
728         s = uci_to_section(e);
729         if (ctx->pctx && ctx->pctx->merge)
730                 ctx->pctx->section = s;
731
732         if (option) {
733                 e = uci_lookup_list(&s->options, option);
734                 if (!e)
735                         goto notfound;
736                 o = uci_to_option(e);
737         }
738
739         /* 
740          * no unknown element was supplied, assume that we can just update 
741          * an existing entry
742          */
743         if (o)
744                 e = &o->e;
745         else
746                 e = &s->e;
747         if (result)
748                 *result = e;
749         else
750                 result = &e;
751
752         ctx->internal = internal;
753         return uci_set_element_value(ctx, result, value);
754
755 notfound:
756         /* 
757          * the entry that we need to update was not found,
758          * check if the search failed prematurely.
759          * this can happen if the package was not found, or if
760          * an option was supplied, but the section wasn't found
761          */
762         if (!p || (!s && option))
763                 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
764
765         /* now add the missing entry */
766         if (!internal && p->has_history)
767                 uci_add_history(ctx, &p->history, UCI_CMD_CHANGE, section, option, value);
768         if (s) {
769                 o = uci_alloc_option(s, option, value);
770                 if (result)
771                         *result = &o->e;
772         } else {
773                 s = uci_alloc_section(p, value, section);
774                 if (result)
775                         *result = &s->e;
776                 if (ctx->pctx && ctx->pctx->merge)
777                         ctx->pctx->section = s;
778         }
779
780         return 0;
781 }
782
783 int uci_unload(struct uci_context *ctx, struct uci_package *p)
784 {
785         UCI_HANDLE_ERR(ctx);
786         UCI_ASSERT(ctx, p != NULL);
787
788         uci_free_package(&p);
789         return 0;
790 }
791