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 void uci_list_set_pos(struct uci_list *head, struct uci_list *ptr, int pos)
17 struct uci_list *new_head = head;
18 struct uci_element *p = NULL;
21 uci_foreach_element(head, p) {
27 uci_list_add(new_head->next, ptr);
30 static inline void uci_list_fixup(struct uci_list *ptr)
32 ptr->prev->next = ptr;
33 ptr->next->prev = ptr;
37 * uci_alloc_generic allocates a new uci_element with payload
38 * payload is appended to the struct to save memory and reduce fragmentation
40 __private struct uci_element *
41 uci_alloc_generic(struct uci_context *ctx, int type, const char *name, int size)
43 struct uci_element *e;
47 ptr = uci_malloc(ctx, datalen);
48 e = (struct uci_element *) ptr;
51 UCI_TRAP_SAVE(ctx, error);
52 e->name = uci_strdup(ctx, name);
53 UCI_TRAP_RESTORE(ctx);
55 uci_list_init(&e->list);
60 UCI_THROW(ctx, ctx->err);
67 uci_free_element(struct uci_element *e)
71 if (!uci_list_empty(&e->list))
72 uci_list_del(&e->list);
76 static struct uci_option *
77 uci_alloc_option(struct uci_section *s, const char *name, const char *value)
79 struct uci_package *p = s->package;
80 struct uci_context *ctx = p->ctx;
83 o = uci_alloc_element(ctx, option, name, strlen(value) + 1);
84 o->type = UCI_TYPE_STRING;
85 o->v.string = uci_dataptr(o);
87 strcpy(o->v.string, value);
88 uci_list_add(&s->options, &o->e.list);
94 uci_free_option(struct uci_option *o)
96 struct uci_element *e, *tmp;
100 if ((o->v.string != uci_dataptr(o)) &&
101 (o->v.string != NULL))
105 uci_foreach_element_safe(&o->v.list, tmp, e) {
112 uci_free_element(&o->e);
115 static struct uci_option *
116 uci_alloc_list(struct uci_section *s, const char *name)
118 struct uci_package *p = s->package;
119 struct uci_context *ctx = p->ctx;
120 struct uci_option *o;
122 o = uci_alloc_element(ctx, option, name, 0);
123 o->type = UCI_TYPE_LIST;
125 uci_list_init(&o->v.list);
126 uci_list_add(&s->options, &o->e.list);
131 /* Based on an efficient hash function published by D. J. Bernstein */
132 static unsigned int djbhash(unsigned int hash, char *str)
134 int len = strlen(str);
141 for(i = 0; i < len; i++) {
142 hash = ((hash << 5) + hash) + str[i];
144 return (hash & 0x7FFFFFFF);
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)
150 unsigned int hash = ~0;
151 struct uci_element *e;
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.
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);
172 case UCI_TYPE_STRING:
173 hash = djbhash(hash, o->v.string);
179 sprintf(buf, "cfg%02x%04x", ++s->package->n_section, hash % (1 << 16));
180 s->e.name = uci_strdup(ctx, buf);
183 static struct uci_section *
184 uci_alloc_section(struct uci_package *p, const char *type, const char *name)
186 struct uci_context *ctx = p->ctx;
187 struct uci_section *s;
189 if (name && !name[0])
192 s = uci_alloc_element(ctx, section, name, strlen(type) + 1);
193 uci_list_init(&s->options);
194 s->type = uci_dataptr(s);
196 strcpy(s->type, type);
201 uci_list_add(&p->sections, &s->e.list);
207 uci_free_section(struct uci_section *s)
209 struct uci_element *o, *tmp;
211 uci_foreach_element_safe(&s->options, tmp, o) {
212 uci_free_option(uci_to_option(o));
214 if ((s->type != uci_dataptr(s)) &&
217 uci_free_element(&s->e);
220 __private struct uci_package *
221 uci_alloc_package(struct uci_context *ctx, const char *name)
223 struct uci_package *p;
225 p = uci_alloc_element(ctx, package, name, 0);
227 uci_list_init(&p->sections);
228 uci_list_init(&p->delta);
229 uci_list_init(&p->saved_delta);
234 uci_free_package(struct uci_package **package)
236 struct uci_element *e, *tmp;
237 struct uci_package *p = *package;
244 uci_foreach_element_safe(&p->sections, tmp, e) {
245 uci_free_section(uci_to_section(e));
247 uci_foreach_element_safe(&p->delta, tmp, e) {
248 uci_free_delta(uci_to_delta(e));
250 uci_foreach_element_safe(&p->saved_delta, tmp, e) {
251 uci_free_delta(uci_to_delta(e));
253 uci_free_element(&p->e);
258 uci_free_any(struct uci_element **e)
261 case UCI_TYPE_SECTION:
262 uci_free_section(uci_to_section(*e));
264 case UCI_TYPE_OPTION:
265 uci_free_option(uci_to_option(*e));
273 __private struct uci_element *
274 uci_lookup_list(struct uci_list *list, const char *name)
276 struct uci_element *e;
278 uci_foreach_element(list, e) {
279 if (!strcmp(e->name, name))
285 static struct uci_element *
286 uci_lookup_ext_section(struct uci_context *ctx, struct uci_ptr *ptr)
288 char *idxstr, *t, *section, *name;
289 struct uci_element *e = NULL;
290 struct uci_section *s;
293 section = uci_strdup(ctx, ptr->section);
294 name = idxstr = section + 1;
296 if (section[0] != '@')
299 /* parse the section index part */
300 idxstr = strchr(idxstr, '[');
306 t = strchr(idxstr, ']');
314 idx = strtol(idxstr, &t, 10);
320 else if (!uci_validate_type(name))
323 /* if the given index is negative, it specifies the section number from
324 * the end of the list */
327 uci_foreach_element(&ptr->p->sections, e) {
328 s = uci_to_section(e);
329 if (name && (strcmp(s->type, name) != 0))
338 uci_foreach_element(&ptr->p->sections, e) {
339 s = uci_to_section(e);
340 if (name && (strcmp(s->type, name) != 0))
352 memset(ptr, 0, sizeof(struct uci_ptr));
353 UCI_THROW(ctx, UCI_ERR_INVAL);
357 ptr->section = e->name;
362 uci_lookup_next(struct uci_context *ctx, struct uci_element **e, struct uci_list *list, const char *name)
366 *e = uci_lookup_list(list, name);
368 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
374 uci_lookup_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str, bool extended)
376 struct uci_element *e;
379 UCI_ASSERT(ctx, ptr != NULL);
382 UCI_INTERNAL(uci_parse_ptr, ctx, ptr, str);
384 ptr->flags |= UCI_LOOKUP_DONE;
386 /* look up the package first */
390 e = uci_lookup_list(&ctx->root, ptr->package);
393 UCI_INTERNAL(uci_load, ctx, ptr->package, &ptr->p);
396 ptr->last = &ptr->p->e;
398 ptr->p = uci_to_package(e);
402 if (!ptr->section && !ptr->s)
405 /* if the section name validates as a regular name, pass through
406 * to the regular uci_lookup function call */
409 } else if (ptr->flags & UCI_LOOKUP_EXTENDED) {
411 e = uci_lookup_ext_section(ctx, ptr);
413 UCI_THROW(ctx, UCI_ERR_INVAL);
415 e = uci_lookup_list(&ptr->p->sections, ptr->section);
422 ptr->s = uci_to_section(e);
425 e = uci_lookup_list(&ptr->s->options, ptr->option);
429 ptr->o = uci_to_option(e);
434 ptr->flags |= UCI_LOOKUP_COMPLETE;
439 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
443 __private struct uci_element *
444 uci_expand_ptr(struct uci_context *ctx, struct uci_ptr *ptr, bool complete)
446 UCI_ASSERT(ctx, ptr != NULL);
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);
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;
472 static void uci_add_element_list(struct uci_context *ctx, struct uci_ptr *ptr, bool internal)
474 struct uci_element *e;
475 struct uci_package *p;
478 if (!internal && p->has_delta)
479 uci_add_delta(ctx, &p->delta, UCI_CMD_LIST_ADD, ptr->section, ptr->option, ptr->value);
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);
485 int uci_rename(struct uci_context *ctx, struct uci_ptr *ptr)
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;
495 e = uci_expand_ptr(ctx, ptr, true);
498 UCI_ASSERT(ctx, ptr->s);
499 UCI_ASSERT(ctx, ptr->value);
501 if (!internal && p->has_delta)
502 uci_add_delta(ctx, &p->delta, UCI_CMD_RENAME, ptr->section, ptr->option, ptr->value);
504 n = uci_strdup(ctx, ptr->value);
509 if (e->type == UCI_TYPE_SECTION)
510 uci_to_section(e)->anonymous = false;
515 int uci_reorder_section(struct uci_context *ctx, struct uci_section *s, int pos)
517 struct uci_package *p = s->package;
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);
531 int uci_add_section(struct uci_context *ctx, struct uci_package *p, const char *type, struct uci_section **res)
533 bool internal = ctx && ctx->internal;
534 struct uci_section *s;
537 UCI_ASSERT(ctx, p != NULL);
538 s = uci_alloc_section(p, type, NULL);
539 uci_fixup_section(ctx, s);
541 if (!internal && p->has_delta)
542 uci_add_delta(ctx, &p->delta, UCI_CMD_ADD, s->e.name, NULL, type);
547 int uci_delete(struct uci_context *ctx, struct uci_ptr *ptr)
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;
557 e1 = uci_expand_ptr(ctx, ptr, true);
560 UCI_ASSERT(ctx, ptr->s);
562 if (ptr->value && ptr->o && ptr->o->type == UCI_TYPE_LIST) {
563 if (!sscanf(ptr->value, "%d", &index))
566 uci_foreach_element_safe(&ptr->o->v.list, tmp, e2) {
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));
579 if (!internal && p->has_delta)
580 uci_add_delta(ctx, &p->delta, UCI_CMD_REMOVE, ptr->section, ptr->option, NULL);
586 else if (ptr->section)
592 int uci_add_list(struct uci_context *ctx, struct uci_ptr *ptr)
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;
601 uci_expand_ptr(ctx, ptr, false);
602 UCI_ASSERT(ctx, ptr->s);
603 UCI_ASSERT(ctx, ptr->value);
606 switch (ptr->o->type) {
607 case UCI_TYPE_STRING:
608 /* we already have a string value, convert that to a list */
611 ptr->value = ptr->o->v.string;
614 uci_add_element_list(ctx, ptr, internal);
617 UCI_THROW(ctx, UCI_ERR_INVAL);
622 ptr->o = uci_alloc_list(ptr->s, ptr->option);
624 uci_add_element_list(ctx, ptr, true);
625 uci_free_option(prev);
628 uci_add_element_list(ctx, ptr, internal);
633 int uci_del_list(struct uci_context *ctx, struct uci_ptr *ptr)
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;
642 uci_expand_ptr(ctx, ptr, false);
643 UCI_ASSERT(ctx, ptr->s);
644 UCI_ASSERT(ctx, ptr->value);
646 if (!(ptr->o && ptr->option))
649 if ((ptr->o->type != UCI_TYPE_LIST))
653 if (!internal && p->has_delta)
654 uci_add_delta(ctx, &p->delta, UCI_CMD_LIST_DEL, ptr->section, ptr->option, ptr->value);
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));
665 int uci_set(struct uci_context *ctx, struct uci_ptr *ptr)
667 /* NB: UCI_INTERNAL use means without delta tracking */
668 bool internal = ctx && ctx->internal;
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));
678 if (!ptr->o && ptr->s && ptr->option) {
679 struct uci_element *e;
680 e = uci_lookup_list(&ptr->s->options, ptr->option);
682 ptr->o = uci_to_option(e);
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))
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))
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);
707 if (ptr->s->type == uci_dataptr(ptr->s)) {
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);
717 UCI_THROW(ctx, UCI_ERR_INVAL);
720 if (!internal && ptr->p->has_delta)
721 uci_add_delta(ctx, &ptr->p->delta, UCI_CMD_CHANGE, ptr->section, ptr->option, ptr->value);
726 int uci_unload(struct uci_context *ctx, struct uci_package *p)
729 UCI_ASSERT(ctx, p != NULL);
731 uci_free_package(&p);