2 * libuci - Library for the Unified Configuration Interface
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
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
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.
15 static bool uci_list_set_pos(struct uci_list *head, struct uci_list *ptr, int pos)
17 struct uci_list *old_head = ptr->prev;
18 struct uci_list *new_head = head;
19 struct uci_element *p = NULL;
22 uci_foreach_element(head, p) {
28 uci_list_add(new_head->next, ptr);
30 return (old_head != new_head);
33 static inline void uci_list_fixup(struct uci_list *ptr)
35 ptr->prev->next = ptr;
36 ptr->next->prev = ptr;
40 * uci_alloc_generic allocates a new uci_element with payload
41 * payload is appended to the struct to save memory and reduce fragmentation
43 __private struct uci_element *
44 uci_alloc_generic(struct uci_context *ctx, int type, const char *name, int size)
46 struct uci_element *e;
50 ptr = uci_malloc(ctx, datalen);
51 e = (struct uci_element *) ptr;
54 UCI_TRAP_SAVE(ctx, error);
55 e->name = uci_strdup(ctx, name);
56 UCI_TRAP_RESTORE(ctx);
58 uci_list_init(&e->list);
63 UCI_THROW(ctx, ctx->err);
70 uci_free_element(struct uci_element *e)
73 if (!uci_list_empty(&e->list))
74 uci_list_del(&e->list);
78 static struct uci_option *
79 uci_alloc_option(struct uci_section *s, const char *name, const char *value)
81 struct uci_package *p = s->package;
82 struct uci_context *ctx = p->ctx;
85 o = uci_alloc_element(ctx, option, name, strlen(value) + 1);
86 o->type = UCI_TYPE_STRING;
87 o->v.string = uci_dataptr(o);
89 strcpy(o->v.string, value);
90 uci_list_add(&s->options, &o->e.list);
96 uci_free_option(struct uci_option *o)
98 struct uci_element *e, *tmp;
101 case UCI_TYPE_STRING:
102 if ((o->v.string != uci_dataptr(o)) &&
103 (o->v.string != NULL))
107 uci_foreach_element_safe(&o->v.list, tmp, e) {
114 uci_free_element(&o->e);
117 static struct uci_option *
118 uci_alloc_list(struct uci_section *s, const char *name)
120 struct uci_package *p = s->package;
121 struct uci_context *ctx = p->ctx;
122 struct uci_option *o;
124 o = uci_alloc_element(ctx, option, name, 0);
125 o->type = UCI_TYPE_LIST;
127 uci_list_init(&o->v.list);
128 uci_list_add(&s->options, &o->e.list);
133 /* Based on an efficient hash function published by D. J. Bernstein */
134 static unsigned int djbhash(unsigned int hash, char *str)
136 int len = strlen(str);
143 for(i = 0; i < len; i++) {
144 hash = ((hash << 5) + hash) + str[i];
146 return (hash & 0x7FFFFFFF);
149 /* fix up an unnamed section, e.g. after adding options to it */
150 static void uci_fixup_section(struct uci_context *ctx, struct uci_section *s)
152 unsigned int hash = ~0U;
153 struct uci_element *e;
160 * Generate a name for unnamed sections. This is used as reference
161 * when locating or updating the section from apps/scripts.
162 * To make multiple concurrent versions somewhat safe for updating,
163 * the name is generated from a hash of its type and name/value
164 * pairs of its option, and it is prefixed by a counter value.
165 * If the order of the unnamed sections changes for some reason,
166 * updates to them will be rejected.
168 hash = djbhash(hash, s->type);
169 uci_foreach_element(&s->options, e) {
170 struct uci_option *o;
171 hash = djbhash(hash, e->name);
172 o = uci_to_option(e);
174 case UCI_TYPE_STRING:
175 hash = djbhash(hash, o->v.string);
181 sprintf(buf, "cfg%02x%04x", s->package->n_section, hash % (1 << 16));
182 s->e.name = uci_strdup(ctx, buf);
185 /* fix up option list HEAD pointers and pointer to section in options */
186 static void uci_section_fixup_options(struct uci_section *s, bool no_options)
188 struct uci_element *e;
192 * enforce empty list pointer state (s->next == s) when original
193 * section had no options in the first place
195 uci_list_init(&s->options);
199 /* fix pointers to HEAD at end/beginning of list */
200 uci_list_fixup(&s->options);
202 /* fix back pointer to section in options */
203 uci_foreach_element(&s->options, e) {
204 struct uci_option *o;
206 o = uci_to_option(e);
211 static struct uci_section *
212 uci_alloc_section(struct uci_package *p, const char *type, const char *name)
214 struct uci_context *ctx = p->ctx;
215 struct uci_section *s;
217 if (name && !name[0])
220 s = uci_alloc_element(ctx, section, name, strlen(type) + 1);
221 uci_list_init(&s->options);
222 s->type = uci_dataptr(s);
224 strcpy(s->type, type);
229 uci_list_add(&p->sections, &s->e.list);
235 uci_free_section(struct uci_section *s)
237 struct uci_element *o, *tmp;
239 uci_foreach_element_safe(&s->options, tmp, o) {
240 uci_free_option(uci_to_option(o));
242 if ((s->type != uci_dataptr(s)) &&
245 uci_free_element(&s->e);
248 __private struct uci_package *
249 uci_alloc_package(struct uci_context *ctx, const char *name)
251 struct uci_package *p;
253 p = uci_alloc_element(ctx, package, name, 0);
255 uci_list_init(&p->sections);
256 uci_list_init(&p->delta);
257 uci_list_init(&p->saved_delta);
262 uci_free_package(struct uci_package **package)
264 struct uci_element *e, *tmp;
265 struct uci_package *p = *package;
271 uci_foreach_element_safe(&p->sections, tmp, e) {
272 uci_free_section(uci_to_section(e));
274 uci_foreach_element_safe(&p->delta, tmp, e) {
275 uci_free_delta(uci_to_delta(e));
277 uci_foreach_element_safe(&p->saved_delta, tmp, e) {
278 uci_free_delta(uci_to_delta(e));
280 uci_free_element(&p->e);
285 uci_free_any(struct uci_element **e)
288 case UCI_TYPE_SECTION:
289 uci_free_section(uci_to_section(*e));
291 case UCI_TYPE_OPTION:
292 uci_free_option(uci_to_option(*e));
300 __private struct uci_element *
301 uci_lookup_list(struct uci_list *list, const char *name)
303 struct uci_element *e;
305 uci_foreach_element(list, e) {
306 if (!strcmp(e->name, name))
312 static struct uci_element *
313 uci_lookup_ext_section(struct uci_context *ctx, struct uci_ptr *ptr)
315 char *idxstr, *t, *section, *name;
316 struct uci_element *e = NULL;
317 struct uci_section *s;
320 section = uci_strdup(ctx, ptr->section);
321 name = idxstr = section + 1;
323 if (section[0] != '@')
326 /* parse the section index part */
327 idxstr = strchr(idxstr, '[');
333 t = strchr(idxstr, ']');
341 idx = strtol(idxstr, &t, 10);
347 else if (!uci_validate_type(name))
350 /* if the given index is negative, it specifies the section number from
351 * the end of the list */
354 uci_foreach_element(&ptr->p->sections, e) {
355 s = uci_to_section(e);
356 if (name && (strcmp(s->type, name) != 0))
365 uci_foreach_element(&ptr->p->sections, e) {
366 s = uci_to_section(e);
367 if (name && (strcmp(s->type, name) != 0))
379 memset(ptr, 0, sizeof(struct uci_ptr));
380 UCI_THROW(ctx, UCI_ERR_INVAL);
384 ptr->section = e->name;
389 uci_lookup_next(struct uci_context *ctx, struct uci_element **e, struct uci_list *list, const char *name)
393 *e = uci_lookup_list(list, name);
395 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
401 uci_lookup_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str, bool extended)
403 struct uci_element *e;
406 UCI_ASSERT(ctx, ptr != NULL);
409 UCI_INTERNAL(uci_parse_ptr, ctx, ptr, str);
411 ptr->flags |= UCI_LOOKUP_DONE;
413 /* look up the package first */
417 e = uci_lookup_list(&ctx->root, ptr->package);
420 UCI_INTERNAL(uci_load, ctx, ptr->package, &ptr->p);
423 ptr->last = &ptr->p->e;
425 ptr->p = uci_to_package(e);
429 if (!ptr->section && !ptr->s)
432 /* if the section name validates as a regular name, pass through
433 * to the regular uci_lookup function call */
436 } else if (ptr->flags & UCI_LOOKUP_EXTENDED) {
438 e = uci_lookup_ext_section(ctx, ptr);
440 UCI_THROW(ctx, UCI_ERR_INVAL);
442 e = uci_lookup_list(&ptr->p->sections, ptr->section);
449 ptr->s = uci_to_section(e);
452 e = uci_lookup_list(&ptr->s->options, ptr->option);
456 ptr->o = uci_to_option(e);
461 ptr->flags |= UCI_LOOKUP_COMPLETE;
466 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
467 /* not a chance here */
468 return UCI_ERR_NOTFOUND;
471 __private struct uci_element *
472 uci_expand_ptr(struct uci_context *ctx, struct uci_ptr *ptr, bool complete)
474 UCI_ASSERT(ctx, ptr != NULL);
476 if (!(ptr->flags & UCI_LOOKUP_DONE))
477 UCI_INTERNAL(uci_lookup_ptr, ctx, ptr, NULL, 1);
478 if (complete && !(ptr->flags & UCI_LOOKUP_COMPLETE))
479 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
480 UCI_ASSERT(ctx, ptr->p != NULL);
482 /* fill in missing string info */
483 if (ptr->p && !ptr->package)
484 ptr->package = ptr->p->e.name;
485 if (ptr->s && !ptr->section)
486 ptr->section = ptr->s->e.name;
487 if (ptr->o && !ptr->option)
488 ptr->option = ptr->o->e.name;
500 static void uci_add_element_list(struct uci_context *ctx, struct uci_ptr *ptr, bool internal)
502 struct uci_element *e;
503 struct uci_package *p;
506 if (!internal && p->has_delta)
507 uci_add_delta(ctx, &p->delta, UCI_CMD_LIST_ADD, ptr->section, ptr->option, ptr->value);
509 e = uci_alloc_generic(ctx, UCI_TYPE_ITEM, ptr->value, sizeof(struct uci_option));
510 uci_list_add(&ptr->o->v.list, &e->list);
513 int uci_rename(struct uci_context *ctx, struct uci_ptr *ptr)
515 /* NB: UCI_INTERNAL use means without delta tracking */
516 bool internal = ctx && ctx->internal;
517 struct uci_element *e;
518 struct uci_package *p;
523 e = uci_expand_ptr(ctx, ptr, true);
526 UCI_ASSERT(ctx, ptr->s);
527 UCI_ASSERT(ctx, ptr->value);
529 if (!internal && p->has_delta)
530 uci_add_delta(ctx, &p->delta, UCI_CMD_RENAME, ptr->section, ptr->option, ptr->value);
532 n = uci_strdup(ctx, ptr->value);
536 if (e->type == UCI_TYPE_SECTION)
537 uci_to_section(e)->anonymous = false;
542 int uci_reorder_section(struct uci_context *ctx, struct uci_section *s, int pos)
544 struct uci_package *p = s->package;
545 bool internal = ctx && ctx->internal;
546 bool changed = false;
551 changed = uci_list_set_pos(&s->package->sections, &s->e.list, pos);
552 if (!internal && p->has_delta && changed) {
553 sprintf(order, "%d", pos);
554 uci_add_delta(ctx, &p->delta, UCI_CMD_REORDER, s->e.name, NULL, order);
560 int uci_add_section(struct uci_context *ctx, struct uci_package *p, const char *type, struct uci_section **res)
562 bool internal = ctx && ctx->internal;
563 struct uci_section *s;
566 UCI_ASSERT(ctx, p != NULL);
567 s = uci_alloc_section(p, type, NULL);
568 if (s && s->anonymous)
569 uci_fixup_section(ctx, s);
571 if (!internal && p->has_delta)
572 uci_add_delta(ctx, &p->delta, UCI_CMD_ADD, s->e.name, NULL, type);
577 int uci_delete(struct uci_context *ctx, struct uci_ptr *ptr)
579 /* NB: pass on internal flag to uci_del_element */
580 bool internal = ctx && ctx->internal;
581 struct uci_package *p;
582 struct uci_element *e1, *e2, *tmp;
587 e1 = uci_expand_ptr(ctx, ptr, true);
590 UCI_ASSERT(ctx, ptr->s);
592 if (ptr->o && ptr->o->type == UCI_TYPE_LIST && ptr->value && *ptr->value) {
593 if (!sscanf(ptr->value, "%d", &index))
596 uci_foreach_element_safe(&ptr->o->v.list, tmp, e2) {
598 if (!internal && p->has_delta)
599 uci_add_delta(ctx, &p->delta, UCI_CMD_REMOVE, ptr->section, ptr->option, ptr->value);
600 uci_free_option(uci_to_option(e2));
609 if (!internal && p->has_delta)
610 uci_add_delta(ctx, &p->delta, UCI_CMD_REMOVE, ptr->section, ptr->option, NULL);
616 else if (ptr->section)
622 int uci_add_list(struct uci_context *ctx, struct uci_ptr *ptr)
624 /* NB: UCI_INTERNAL use means without delta tracking */
625 bool internal = ctx && ctx->internal;
626 struct uci_option *volatile prev = NULL;
627 const char *volatile value2 = NULL;
631 uci_expand_ptr(ctx, ptr, false);
632 UCI_ASSERT(ctx, ptr->s);
633 UCI_ASSERT(ctx, ptr->value);
636 switch (ptr->o->type) {
637 case UCI_TYPE_STRING:
638 /* we already have a string value, convert that to a list */
641 ptr->value = ptr->o->v.string;
644 uci_add_element_list(ctx, ptr, internal);
647 UCI_THROW(ctx, UCI_ERR_INVAL);
652 ptr->o = uci_alloc_list(ptr->s, ptr->option);
654 uci_add_element_list(ctx, ptr, true);
655 uci_free_option(prev);
658 uci_add_element_list(ctx, ptr, internal);
663 int uci_del_list(struct uci_context *ctx, struct uci_ptr *ptr)
665 /* NB: pass on internal flag to uci_del_element */
666 bool internal = ctx && ctx->internal;
667 struct uci_element *e, *tmp;
668 struct uci_package *p;
672 uci_expand_ptr(ctx, ptr, false);
673 UCI_ASSERT(ctx, ptr->s);
674 UCI_ASSERT(ctx, ptr->value);
676 if (!(ptr->o && ptr->option))
679 if ((ptr->o->type != UCI_TYPE_LIST))
683 if (!internal && p->has_delta)
684 uci_add_delta(ctx, &p->delta, UCI_CMD_LIST_DEL, ptr->section, ptr->option, ptr->value);
686 uci_foreach_element_safe(&ptr->o->v.list, tmp, e) {
687 if (!strcmp(ptr->value, uci_to_option(e)->e.name)) {
688 uci_free_option(uci_to_option(e));
695 int uci_set(struct uci_context *ctx, struct uci_ptr *ptr)
697 /* NB: UCI_INTERNAL use means without delta tracking */
698 bool internal = ctx && ctx->internal;
701 uci_expand_ptr(ctx, ptr, false);
702 UCI_ASSERT(ctx, ptr->value);
703 UCI_ASSERT(ctx, ptr->s || (!ptr->option && ptr->section));
704 if (!ptr->option && ptr->value[0]) {
705 UCI_ASSERT(ctx, uci_validate_type(ptr->value));
708 if (!ptr->o && ptr->s && ptr->option) {
709 struct uci_element *e;
710 e = uci_lookup_list(&ptr->s->options, ptr->option);
712 ptr->o = uci_to_option(e);
714 if (!ptr->value[0]) {
715 /* if setting a nonexistant option/section to a nonexistant value,
716 * exit without errors */
717 if (!(ptr->flags & UCI_LOOKUP_COMPLETE))
720 return uci_delete(ctx, ptr);
721 } else if (!ptr->o && ptr->option) { /* new option */
722 ptr->o = uci_alloc_option(ptr->s, ptr->option, ptr->value);
723 ptr->last = &ptr->o->e;
724 } else if (!ptr->s && ptr->section) { /* new section */
725 ptr->s = uci_alloc_section(ptr->p, ptr->value, ptr->section);
726 ptr->last = &ptr->s->e;
727 } else if (ptr->o && ptr->option) { /* update option */
728 struct uci_option *o;
730 if ((ptr->o->type == UCI_TYPE_STRING) &&
731 !strcmp(ptr->o->v.string, ptr->value))
735 ptr->o = uci_alloc_option(ptr->s, ptr->option, ptr->value);
737 ptr->last = &ptr->o->e;
738 } else if (ptr->s && ptr->section) { /* update section */
739 char *s = uci_strdup(ctx, ptr->value);
741 if (ptr->s->type == uci_dataptr(ptr->s)) {
742 /* drop the in-section storage of type name */
745 no_options = uci_list_empty(&ptr->s->options);
747 ptr->last = uci_realloc(ctx, ptr->s, sizeof(struct uci_section));
748 ptr->s = uci_to_section(ptr->last);
749 uci_list_fixup(&ptr->s->e.list);
750 uci_section_fixup_options(ptr->s, no_options);
756 UCI_THROW(ctx, UCI_ERR_INVAL);
759 if (!internal && ptr->p->has_delta)
760 uci_add_delta(ctx, &ptr->p->delta, UCI_CMD_CHANGE, ptr->section, ptr->option, ptr->value);
765 int uci_unload(struct uci_context *ctx, struct uci_package *p)
768 UCI_ASSERT(ctx, p != NULL);
770 uci_free_package(&p);