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