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 = ~0;
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 static struct uci_section *
186 uci_alloc_section(struct uci_package *p, const char *type, const char *name)
188 struct uci_context *ctx = p->ctx;
189 struct uci_section *s;
191 if (name && !name[0])
194 s = uci_alloc_element(ctx, section, name, strlen(type) + 1);
195 uci_list_init(&s->options);
196 s->type = uci_dataptr(s);
198 strcpy(s->type, type);
203 uci_list_add(&p->sections, &s->e.list);
209 uci_free_section(struct uci_section *s)
211 struct uci_element *o, *tmp;
213 uci_foreach_element_safe(&s->options, tmp, o) {
214 uci_free_option(uci_to_option(o));
216 if ((s->type != uci_dataptr(s)) &&
219 uci_free_element(&s->e);
222 __private struct uci_package *
223 uci_alloc_package(struct uci_context *ctx, const char *name)
225 struct uci_package *p;
227 p = uci_alloc_element(ctx, package, name, 0);
229 uci_list_init(&p->sections);
230 uci_list_init(&p->delta);
231 uci_list_init(&p->saved_delta);
236 uci_free_package(struct uci_package **package)
238 struct uci_element *e, *tmp;
239 struct uci_package *p = *package;
245 uci_foreach_element_safe(&p->sections, tmp, e) {
246 uci_free_section(uci_to_section(e));
248 uci_foreach_element_safe(&p->delta, tmp, e) {
249 uci_free_delta(uci_to_delta(e));
251 uci_foreach_element_safe(&p->saved_delta, tmp, e) {
252 uci_free_delta(uci_to_delta(e));
254 uci_free_element(&p->e);
259 uci_free_any(struct uci_element **e)
262 case UCI_TYPE_SECTION:
263 uci_free_section(uci_to_section(*e));
265 case UCI_TYPE_OPTION:
266 uci_free_option(uci_to_option(*e));
274 __private struct uci_element *
275 uci_lookup_list(struct uci_list *list, const char *name)
277 struct uci_element *e;
279 uci_foreach_element(list, e) {
280 if (!strcmp(e->name, name))
286 static struct uci_element *
287 uci_lookup_ext_section(struct uci_context *ctx, struct uci_ptr *ptr)
289 char *idxstr, *t, *section, *name;
290 struct uci_element *e = NULL;
291 struct uci_section *s;
294 section = uci_strdup(ctx, ptr->section);
295 name = idxstr = section + 1;
297 if (section[0] != '@')
300 /* parse the section index part */
301 idxstr = strchr(idxstr, '[');
307 t = strchr(idxstr, ']');
315 idx = strtol(idxstr, &t, 10);
321 else if (!uci_validate_type(name))
324 /* if the given index is negative, it specifies the section number from
325 * the end of the list */
328 uci_foreach_element(&ptr->p->sections, e) {
329 s = uci_to_section(e);
330 if (name && (strcmp(s->type, name) != 0))
339 uci_foreach_element(&ptr->p->sections, e) {
340 s = uci_to_section(e);
341 if (name && (strcmp(s->type, name) != 0))
353 memset(ptr, 0, sizeof(struct uci_ptr));
354 UCI_THROW(ctx, UCI_ERR_INVAL);
358 ptr->section = e->name;
363 uci_lookup_next(struct uci_context *ctx, struct uci_element **e, struct uci_list *list, const char *name)
367 *e = uci_lookup_list(list, name);
369 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
375 uci_lookup_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str, bool extended)
377 struct uci_element *e;
380 UCI_ASSERT(ctx, ptr != NULL);
383 UCI_INTERNAL(uci_parse_ptr, ctx, ptr, str);
385 ptr->flags |= UCI_LOOKUP_DONE;
387 /* look up the package first */
391 e = uci_lookup_list(&ctx->root, ptr->package);
394 UCI_INTERNAL(uci_load, ctx, ptr->package, &ptr->p);
397 ptr->last = &ptr->p->e;
399 ptr->p = uci_to_package(e);
403 if (!ptr->section && !ptr->s)
406 /* if the section name validates as a regular name, pass through
407 * to the regular uci_lookup function call */
410 } else if (ptr->flags & UCI_LOOKUP_EXTENDED) {
412 e = uci_lookup_ext_section(ctx, ptr);
414 UCI_THROW(ctx, UCI_ERR_INVAL);
416 e = uci_lookup_list(&ptr->p->sections, ptr->section);
423 ptr->s = uci_to_section(e);
426 e = uci_lookup_list(&ptr->s->options, ptr->option);
430 ptr->o = uci_to_option(e);
435 ptr->flags |= UCI_LOOKUP_COMPLETE;
440 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
441 /* not a chance here */
442 return UCI_ERR_NOTFOUND;
445 __private struct uci_element *
446 uci_expand_ptr(struct uci_context *ctx, struct uci_ptr *ptr, bool complete)
448 UCI_ASSERT(ctx, ptr != NULL);
450 if (!(ptr->flags & UCI_LOOKUP_DONE))
451 UCI_INTERNAL(uci_lookup_ptr, ctx, ptr, NULL, 1);
452 if (complete && !(ptr->flags & UCI_LOOKUP_COMPLETE))
453 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
454 UCI_ASSERT(ctx, ptr->p != NULL);
456 /* fill in missing string info */
457 if (ptr->p && !ptr->package)
458 ptr->package = ptr->p->e.name;
459 if (ptr->s && !ptr->section)
460 ptr->section = ptr->s->e.name;
461 if (ptr->o && !ptr->option)
462 ptr->option = ptr->o->e.name;
474 static void uci_add_element_list(struct uci_context *ctx, struct uci_ptr *ptr, bool internal)
476 struct uci_element *e;
477 struct uci_package *p;
480 if (!internal && p->has_delta)
481 uci_add_delta(ctx, &p->delta, UCI_CMD_LIST_ADD, ptr->section, ptr->option, ptr->value);
483 e = uci_alloc_generic(ctx, UCI_TYPE_ITEM, ptr->value, sizeof(struct uci_option));
484 uci_list_add(&ptr->o->v.list, &e->list);
487 int uci_rename(struct uci_context *ctx, struct uci_ptr *ptr)
489 /* NB: UCI_INTERNAL use means without delta tracking */
490 bool internal = ctx && ctx->internal;
491 struct uci_element *e;
492 struct uci_package *p;
497 e = uci_expand_ptr(ctx, ptr, true);
500 UCI_ASSERT(ctx, ptr->s);
501 UCI_ASSERT(ctx, ptr->value);
503 if (!internal && p->has_delta)
504 uci_add_delta(ctx, &p->delta, UCI_CMD_RENAME, ptr->section, ptr->option, ptr->value);
506 n = uci_strdup(ctx, ptr->value);
510 if (e->type == UCI_TYPE_SECTION)
511 uci_to_section(e)->anonymous = false;
516 int uci_reorder_section(struct uci_context *ctx, struct uci_section *s, int pos)
518 struct uci_package *p = s->package;
519 bool internal = ctx && ctx->internal;
520 bool changed = false;
525 changed = uci_list_set_pos(&s->package->sections, &s->e.list, pos);
526 if (!internal && p->has_delta && changed) {
527 sprintf(order, "%d", pos);
528 uci_add_delta(ctx, &p->delta, UCI_CMD_REORDER, s->e.name, NULL, order);
534 int uci_add_section(struct uci_context *ctx, struct uci_package *p, const char *type, struct uci_section **res)
536 bool internal = ctx && ctx->internal;
537 struct uci_section *s;
540 UCI_ASSERT(ctx, p != NULL);
541 s = uci_alloc_section(p, type, NULL);
542 if (s && s->anonymous)
543 uci_fixup_section(ctx, s);
545 if (!internal && p->has_delta)
546 uci_add_delta(ctx, &p->delta, UCI_CMD_ADD, s->e.name, NULL, type);
551 int uci_delete(struct uci_context *ctx, struct uci_ptr *ptr)
553 /* NB: pass on internal flag to uci_del_element */
554 bool internal = ctx && ctx->internal;
555 struct uci_package *p;
556 struct uci_element *e1, *e2, *tmp;
561 e1 = uci_expand_ptr(ctx, ptr, true);
564 UCI_ASSERT(ctx, ptr->s);
566 if (ptr->o && ptr->o->type == UCI_TYPE_LIST && ptr->value && *ptr->value) {
567 if (!sscanf(ptr->value, "%d", &index))
570 uci_foreach_element_safe(&ptr->o->v.list, tmp, e2) {
572 if (!internal && p->has_delta)
573 uci_add_delta(ctx, &p->delta, UCI_CMD_REMOVE, ptr->section, ptr->option, ptr->value);
574 uci_free_option(uci_to_option(e2));
583 if (!internal && p->has_delta)
584 uci_add_delta(ctx, &p->delta, UCI_CMD_REMOVE, ptr->section, ptr->option, NULL);
590 else if (ptr->section)
596 int uci_add_list(struct uci_context *ctx, struct uci_ptr *ptr)
598 /* NB: UCI_INTERNAL use means without delta tracking */
599 bool internal = ctx && ctx->internal;
600 struct uci_option *prev = NULL;
601 const char *value2 = NULL;
605 uci_expand_ptr(ctx, ptr, false);
606 UCI_ASSERT(ctx, ptr->s);
607 UCI_ASSERT(ctx, ptr->value);
610 switch (ptr->o->type) {
611 case UCI_TYPE_STRING:
612 /* we already have a string value, convert that to a list */
615 ptr->value = ptr->o->v.string;
618 uci_add_element_list(ctx, ptr, internal);
621 UCI_THROW(ctx, UCI_ERR_INVAL);
626 ptr->o = uci_alloc_list(ptr->s, ptr->option);
628 uci_add_element_list(ctx, ptr, true);
629 uci_free_option(prev);
632 uci_add_element_list(ctx, ptr, internal);
637 int uci_del_list(struct uci_context *ctx, struct uci_ptr *ptr)
639 /* NB: pass on internal flag to uci_del_element */
640 bool internal = ctx && ctx->internal;
641 struct uci_element *e, *tmp;
642 struct uci_package *p;
646 uci_expand_ptr(ctx, ptr, false);
647 UCI_ASSERT(ctx, ptr->s);
648 UCI_ASSERT(ctx, ptr->value);
650 if (!(ptr->o && ptr->option))
653 if ((ptr->o->type != UCI_TYPE_LIST))
657 if (!internal && p->has_delta)
658 uci_add_delta(ctx, &p->delta, UCI_CMD_LIST_DEL, ptr->section, ptr->option, ptr->value);
660 uci_foreach_element_safe(&ptr->o->v.list, tmp, e) {
661 if (!strcmp(ptr->value, uci_to_option(e)->e.name)) {
662 uci_free_option(uci_to_option(e));
669 int uci_set(struct uci_context *ctx, struct uci_ptr *ptr)
671 /* NB: UCI_INTERNAL use means without delta tracking */
672 bool internal = ctx && ctx->internal;
675 uci_expand_ptr(ctx, ptr, false);
676 UCI_ASSERT(ctx, ptr->value);
677 UCI_ASSERT(ctx, ptr->s || (!ptr->option && ptr->section));
678 if (!ptr->option && ptr->value[0]) {
679 UCI_ASSERT(ctx, uci_validate_type(ptr->value));
682 if (!ptr->o && ptr->s && ptr->option) {
683 struct uci_element *e;
684 e = uci_lookup_list(&ptr->s->options, ptr->option);
686 ptr->o = uci_to_option(e);
688 if (!ptr->value[0]) {
689 /* if setting a nonexistant option/section to a nonexistant value,
690 * exit without errors */
691 if (!(ptr->flags & UCI_LOOKUP_COMPLETE))
694 return uci_delete(ctx, ptr);
695 } else if (!ptr->o && ptr->option) { /* new option */
696 ptr->o = uci_alloc_option(ptr->s, ptr->option, ptr->value);
697 ptr->last = &ptr->o->e;
698 } else if (!ptr->s && ptr->section) { /* new section */
699 ptr->s = uci_alloc_section(ptr->p, ptr->value, ptr->section);
700 ptr->last = &ptr->s->e;
701 } else if (ptr->o && ptr->option) { /* update option */
702 struct uci_option *o;
704 if ((ptr->o->type == UCI_TYPE_STRING) &&
705 !strcmp(ptr->o->v.string, ptr->value))
709 ptr->o = uci_alloc_option(ptr->s, ptr->option, ptr->value);
711 ptr->last = &ptr->o->e;
712 } else if (ptr->s && ptr->section) { /* update section */
713 char *s = uci_strdup(ctx, ptr->value);
715 if (ptr->s->type == uci_dataptr(ptr->s)) {
717 ptr->last = uci_realloc(ctx, ptr->s, sizeof(struct uci_section));
718 ptr->s = uci_to_section(ptr->last);
719 uci_list_fixup(&ptr->s->e.list);
725 UCI_THROW(ctx, UCI_ERR_INVAL);
728 if (!internal && ptr->p->has_delta)
729 uci_add_delta(ctx, &ptr->p->delta, UCI_CMD_CHANGE, ptr->section, ptr->option, ptr->value);
734 int uci_unload(struct uci_context *ctx, struct uci_package *p)
737 UCI_ASSERT(ctx, p != NULL);
739 uci_free_package(&p);