d7b9f69d1d3989877c78f64de25cb8e2843be5c8
[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 struct uci_element *uci_lookup_list(struct uci_list *list, const char *name)
257 {
258 struct uci_element *e;
259
260 uci_foreach_element(list, e) {
261 if (!strcmp(e->name, name))
262 return e;
263 }
264 return NULL;
265 }
266
267 int uci_lookup_ext(struct uci_context *ctx, struct uci_element **res, char *str)
268 {
269 struct uci_package *p = NULL;
270 struct uci_element *e;
271 struct uci_section *s;
272 struct uci_ptr ptr;
273 char *idxstr, *t;
274 int idx, c;
275
276 UCI_HANDLE_ERR(ctx);
277 UCI_ASSERT(ctx, res != NULL);
278 UCI_ASSERT(ctx, str != NULL);
279
280 UCI_INTERNAL(uci_parse_ptr, ctx, &ptr, str);
281
282 /* look up the package first */
283 e = uci_lookup_list(&ctx->root, ptr.package);
284 if (!e) {
285 UCI_INTERNAL(uci_load, ctx, ptr.package, &p);
286 if (!p)
287 goto notfound;
288 e = &p->e;
289 } else {
290 p = uci_to_package(e);
291 }
292
293 if (!ptr.section)
294 goto done;
295
296 /* if the section name validates as a regular name, pass through
297 * to the regular uci_lookup function call */
298 if (!(ptr.flags & UCI_LOOKUP_EXTENDED)) {
299 UCI_INTERNAL(uci_lookup, ctx, &e, p, ptr.section, ptr.option);
300 goto done;
301 }
302
303 /* name did not validate, that means we have an extended lookup call
304 * parse it here. for now only the section index syntax is supported */
305 if (ptr.section[0] != '@')
306 goto error;
307
308 ptr.section++;
309
310 /* parse the section index part */
311 idxstr = strchr(ptr.section, '[');
312 if (!idxstr)
313 goto error;
314 *idxstr = 0;
315 idxstr++;
316
317 t = strchr(idxstr, ']');
318 if (!t)
319 goto error;
320 if (t[1] != 0)
321 goto error;
322 *t = 0;
323
324 t = NULL;
325 idx = strtol(idxstr, &t, 10);
326 if (t && *t)
327 goto error;
328
329 if (!*ptr.section)
330 ptr.section = NULL;
331 if (ptr.section && !uci_validate_str(ptr.section, false))
332 goto error;
333
334 /* if the given index is negative, it specifies the section number from
335 * the end of the list */
336 if (idx < 0) {
337 c = 0;
338 uci_foreach_element(&p->sections, e) {
339 s = uci_to_section(e);
340 if (ptr.section && (strcmp(s->type, ptr.section) != 0))
341 continue;
342
343 c++;
344 }
345 idx += c;
346 }
347
348 c = 0;
349 uci_foreach_element(&p->sections, e) {
350 s = uci_to_section(e);
351 if (ptr.section && (strcmp(s->type, ptr.section) != 0))
352 continue;
353
354 if (idx == c)
355 goto found;
356 c++;
357 }
358 goto notfound;
359
360 found:
361 if (ptr.option)
362 e = uci_lookup_list(&s->options, ptr.option);
363 done:
364 *res = e;
365 return 0;
366
367 notfound:
368 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
369 error:
370 UCI_THROW(ctx, UCI_ERR_INVAL);
371 return 0;
372 }
373
374 int uci_lookup(struct uci_context *ctx, struct uci_element **res, struct uci_package *p, const char *section, const char *option)
375 {
376 struct uci_element *e;
377 struct uci_section *s;
378
379 UCI_HANDLE_ERR(ctx);
380 UCI_ASSERT(ctx, res != NULL);
381 UCI_ASSERT(ctx, p != NULL);
382 UCI_ASSERT(ctx, section && uci_validate_name(section));
383 if (option)
384 UCI_ASSERT(ctx, uci_validate_name(option));
385
386 e = uci_lookup_list(&p->sections, section);
387 if (!e)
388 goto notfound;
389
390 if (option) {
391 s = uci_to_section(e);
392 e = uci_lookup_list(&s->options, option);
393 if (!e)
394 goto notfound;
395 }
396
397 *res = e;
398 return 0;
399
400 notfound:
401 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
402 return 0;
403 }
404
405 int uci_del_element(struct uci_context *ctx, struct uci_element *e)
406 {
407 /* NB: UCI_INTERNAL use means without history tracking */
408 bool internal = ctx->internal;
409 struct uci_package *p = NULL;
410 struct uci_section *s = NULL;
411 struct uci_option *o = NULL;
412 struct uci_element *i, *tmp;
413 char *option = NULL;
414
415 UCI_HANDLE_ERR(ctx);
416 UCI_ASSERT(ctx, e != NULL);
417
418 switch(e->type) {
419 case UCI_TYPE_SECTION:
420 s = uci_to_section(e);
421 uci_foreach_element_safe(&s->options, tmp, i) {
422 uci_del_element(ctx, i);
423 }
424 break;
425 case UCI_TYPE_OPTION:
426 o = uci_to_option(e);
427 s = o->section;
428 p = s->package;
429 option = e->name;
430 break;
431 default:
432 UCI_THROW(ctx, UCI_ERR_INVAL);
433 break;
434 }
435
436 p = s->package;
437 if (!internal && p->has_history)
438 uci_add_history(ctx, &p->history, UCI_CMD_REMOVE, s->e.name, option, NULL);
439
440 switch(e->type) {
441 case UCI_TYPE_SECTION:
442 uci_free_section(s);
443 break;
444 case UCI_TYPE_OPTION:
445 uci_free_option(o);
446 break;
447 default:
448 break;
449 }
450
451 return 0;
452 }
453
454 int uci_add_element_list(struct uci_context *ctx, struct uci_option *o, const char *value)
455 {
456 struct uci_element *e;
457 struct uci_package *p;
458 struct uci_section *s;
459 bool internal = ctx->internal;
460
461 UCI_HANDLE_ERR(ctx);
462 UCI_ASSERT(ctx, (o != NULL) && (o->type == UCI_TYPE_LIST) && uci_validate_text(value));
463
464 s = o->section;
465 p = s->package;
466
467 if (!internal && p->has_history)
468 uci_add_history(ctx, &p->history, UCI_CMD_LIST_ADD, s->e.name, o->e.name, value);
469
470 e = uci_alloc_generic(ctx, UCI_TYPE_ITEM, value, sizeof(struct uci_option));
471 uci_list_add(&o->v.list, &e->list);
472
473 return 0;
474 }
475
476 int uci_set_element_value(struct uci_context *ctx, struct uci_element **element, const char *value)
477 {
478 /* NB: UCI_INTERNAL use means without history tracking */
479 bool internal = ctx->internal;
480 struct uci_list *list;
481 struct uci_element *e;
482 struct uci_package *p;
483 struct uci_section *s;
484 struct uci_option *o;
485 char *section;
486 char *option;
487 char *str;
488 int size = 0;
489
490 UCI_HANDLE_ERR(ctx);
491 UCI_ASSERT(ctx, (element != NULL) && (*element != NULL));
492
493 /* what the 'value' of an element means depends on the type
494 * for a section, the 'value' means its type
495 * for an option, the 'value' means its value string
496 * when changing the value, shrink the element to its actual size
497 * (it may have been allocated with a bigger size, to include
498 * its buffer)
499 * then duplicate the string passed on the command line and
500 * insert it into the structure.
501 */
502 e = *element;
503 list = e->list.prev;
504
505 switch(e->type) {
506 case UCI_TYPE_SECTION:
507 UCI_ASSERT(ctx, uci_validate_str(value, false));
508 size = sizeof(struct uci_section);
509 s = uci_to_section(e);
510 section = e->name;
511 option = NULL;
512 /* matches the currently set value */
513 if (!strcmp(value, s->type))
514 return 0;
515 break;
516
517 case UCI_TYPE_OPTION:
518 UCI_ASSERT(ctx, value != NULL);
519 o = uci_to_option(e);
520 s = o->section;
521 section = s->e.name;
522 option = o->e.name;
523 switch(o->type) {
524 case UCI_TYPE_STRING:
525 size = sizeof(struct uci_option);
526 /* matches the currently set value */
527 if (!strcmp(value, o->v.string))
528 return 0;
529 break;
530 default:
531 /* default action for non-string datatypes is to delete
532 * the existing entry, then re-create it as a string */
533 break;
534 }
535 break;
536
537 default:
538 UCI_THROW(ctx, UCI_ERR_INVAL);
539 return 0;
540 }
541 p = s->package;
542 if (!internal && p->has_history)
543 uci_add_history(ctx, &p->history, UCI_CMD_CHANGE, section, option, value);
544
545 if ((e->type == UCI_TYPE_OPTION) && (size == 0)) {
546 o = uci_alloc_option(s, option, value);
547 UCI_INTERNAL(uci_del_element, ctx, e);
548 *element = &o->e;
549 goto done;
550 }
551
552 uci_list_del(&e->list);
553 e = uci_realloc(ctx, e, size);
554 str = uci_strdup(ctx, value);
555 uci_list_insert(list, &e->list);
556 *element = e;
557
558 switch(e->type) {
559 case UCI_TYPE_SECTION:
560 uci_to_section(e)->type = str;
561 break;
562 case UCI_TYPE_OPTION:
563 uci_to_option(e)->v.string = str;
564 break;
565 default:
566 break;
567 }
568
569 done:
570 return 0;
571 }
572
573 int uci_rename(struct uci_context *ctx, struct uci_package *p, char *section, char *option, char *name)
574 {
575 /* NB: UCI_INTERNAL use means without history tracking */
576 bool internal = ctx->internal;
577 struct uci_element *e;
578
579 UCI_HANDLE_ERR(ctx);
580
581 /* NB: p, section, option validated by uci_lookup */
582 UCI_INTERNAL(uci_lookup, ctx, &e, p, section, option);
583
584 if (!internal && p->has_history)
585 uci_add_history(ctx, &p->history, UCI_CMD_RENAME, section, option, name);
586
587 name = uci_strdup(ctx, name);
588 if (e->name)
589 free(e->name);
590 e->name = name;
591
592 return 0;
593 }
594
595 int uci_add_section(struct uci_context *ctx, struct uci_package *p, const char *type, struct uci_section **res)
596 {
597 bool internal = ctx->internal;
598 struct uci_section *s;
599
600 UCI_HANDLE_ERR(ctx);
601 UCI_ASSERT(ctx, p != NULL);
602 s = uci_alloc_section(p, type, NULL);
603 uci_fixup_section(ctx, s);
604 *res = s;
605 if (!internal && p->has_history)
606 uci_add_history(ctx, &p->history, UCI_CMD_ADD, s->e.name, NULL, type);
607
608 return 0;
609 }
610
611 int uci_delete(struct uci_context *ctx, struct uci_package *p, const char *section, const char *option)
612 {
613 /* NB: pass on internal flag to uci_del_element */
614 bool internal = ctx->internal;
615 struct uci_element *e;
616
617 UCI_HANDLE_ERR(ctx);
618
619 /* NB: p, section, option validated by uci_lookup */
620 UCI_INTERNAL(uci_lookup, ctx, &e, p, section, option);
621
622 ctx->internal = internal;
623 return uci_del_element(ctx, e);
624 }
625
626 int uci_add_list(struct uci_context *ctx, struct uci_package *p, const char *section, const char *option, const char *value, struct uci_option **result)
627 {
628 /* NB: UCI_INTERNAL use means without history tracking */
629 bool internal = ctx->internal;
630 struct uci_element *e;
631 struct uci_section *s;
632 struct uci_option *o;
633 struct uci_option *prev = NULL;
634 const char *value2 = NULL;
635
636 UCI_HANDLE_ERR(ctx);
637 UCI_ASSERT(ctx, p && section && option && value && uci_validate_text(value));
638
639 /* look up the section first */
640 UCI_INTERNAL(uci_lookup, ctx, &e, p, section, NULL);
641 s = uci_to_section(e);
642
643 e = uci_lookup_list(&s->options, option);
644 if (e) {
645 o = uci_to_option(e);
646 switch (o->type) {
647 case UCI_TYPE_STRING:
648 /* we already have a string value, let's convert that to a list */
649 prev = o;
650 value2 = value;
651 value = o->v.string;
652 break;
653 case UCI_TYPE_LIST:
654 if (result)
655 *result = o;
656
657 ctx->internal = internal;
658 return uci_add_element_list(ctx, o, value);
659 default:
660 UCI_THROW(ctx, UCI_ERR_INVAL);
661 break;
662 }
663 }
664
665 o = uci_alloc_list(s, option);
666 if (result)
667 *result = o;
668 if (prev) {
669 UCI_INTERNAL(uci_add_element_list, ctx, o, value);
670 uci_free_option(prev);
671 value = value2;
672 }
673
674 ctx->internal = internal;
675 return uci_add_element_list(ctx, o, value);
676 }
677
678 int uci_set(struct uci_context *ctx, struct uci_package *p, const char *section, const char *option, const char *value, struct uci_element **result)
679 {
680 /* NB: UCI_INTERNAL use means without history tracking */
681 bool internal = ctx->internal;
682 struct uci_element *e = NULL;
683 struct uci_section *s = NULL;
684 struct uci_option *o = NULL;
685
686 UCI_HANDLE_ERR(ctx);
687 UCI_ASSERT(ctx, p != NULL);
688 UCI_ASSERT(ctx, uci_validate_name(section));
689 if (option) {
690 UCI_ASSERT(ctx, uci_validate_name(option));
691 UCI_ASSERT(ctx, value != NULL);
692 } else {
693 UCI_ASSERT(ctx, uci_validate_str(value, false));
694 }
695
696 /*
697 * look up the package, section and option (if set)
698 * if the section/option is to be modified and it is not found
699 * create a new element in the appropriate list
700 */
701 e = uci_lookup_list(&p->sections, section);
702 if (!e)
703 goto notfound;
704
705 s = uci_to_section(e);
706 if (ctx->pctx && ctx->pctx->merge)
707 ctx->pctx->section = s;
708
709 if (option) {
710 e = uci_lookup_list(&s->options, option);
711 if (!e)
712 goto notfound;
713 o = uci_to_option(e);
714 }
715
716 /*
717 * no unknown element was supplied, assume that we can just update
718 * an existing entry
719 */
720 if (o)
721 e = &o->e;
722 else
723 e = &s->e;
724 if (result)
725 *result = e;
726 else
727 result = &e;
728
729 ctx->internal = internal;
730 return uci_set_element_value(ctx, result, value);
731
732 notfound:
733 /*
734 * the entry that we need to update was not found,
735 * check if the search failed prematurely.
736 * this can happen if the package was not found, or if
737 * an option was supplied, but the section wasn't found
738 */
739 if (!p || (!s && option))
740 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
741
742 /* now add the missing entry */
743 if (!internal && p->has_history)
744 uci_add_history(ctx, &p->history, UCI_CMD_CHANGE, section, option, value);
745 if (s) {
746 o = uci_alloc_option(s, option, value);
747 if (result)
748 *result = &o->e;
749 } else {
750 s = uci_alloc_section(p, value, section);
751 if (result)
752 *result = &s->e;
753 if (ctx->pctx && ctx->pctx->merge)
754 ctx->pctx->section = s;
755 }
756
757 return 0;
758 }
759
760 int uci_unload(struct uci_context *ctx, struct uci_package *p)
761 {
762 UCI_HANDLE_ERR(ctx);
763 UCI_ASSERT(ctx, p != NULL);
764
765 uci_free_package(&p);
766 return 0;
767 }
768