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 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) {
26 uci_list_add(new_head, ptr);
29 static inline void uci_list_fixup(struct uci_list *ptr)
31 ptr->prev->next = ptr;
32 ptr->next->prev = ptr;
36 * uci_alloc_generic allocates a new uci_element with payload
37 * payload is appended to the struct to save memory and reduce fragmentation
39 __private struct uci_element *
40 uci_alloc_generic(struct uci_context *ctx, int type, const char *name, int size)
42 struct uci_element *e;
46 ptr = uci_malloc(ctx, datalen);
47 e = (struct uci_element *) ptr;
50 UCI_TRAP_SAVE(ctx, error);
51 e->name = uci_strdup(ctx, name);
52 UCI_TRAP_RESTORE(ctx);
54 uci_list_init(&e->list);
59 UCI_THROW(ctx, ctx->err);
66 uci_free_element(struct uci_element *e)
70 if (!uci_list_empty(&e->list))
71 uci_list_del(&e->list);
75 struct uci_option *uci_alloc_option(struct uci_section *s, const char *name, const char *value)
77 struct uci_package *p = s->package;
78 struct uci_context *ctx = p->ctx;
81 o = uci_alloc_element(ctx, option, name, strlen(value) + 1);
82 o->type = UCI_TYPE_STRING;
83 o->v.string = uci_dataptr(o);
85 strcpy(o->v.string, value);
86 uci_list_add(&s->options, &o->e.list);
92 uci_free_option(struct uci_option *o)
94 struct uci_element *e, *tmp;
98 if ((o->v.string != uci_dataptr(o)) &&
99 (o->v.string != NULL))
103 uci_foreach_element_safe(&o->v.list, tmp, e) {
110 uci_free_element(&o->e);
113 static struct uci_option *
114 uci_alloc_list(struct uci_section *s, const char *name)
116 struct uci_package *p = s->package;
117 struct uci_context *ctx = p->ctx;
118 struct uci_option *o;
120 o = uci_alloc_element(ctx, option, name, 0);
121 o->type = UCI_TYPE_LIST;
123 uci_list_init(&o->v.list);
124 uci_list_add(&s->options, &o->e.list);
129 /* Based on an efficient hash function published by D. J. Bernstein */
130 static unsigned int djbhash(unsigned int hash, char *str)
132 int len = strlen(str);
139 for(i = 0; i < len; i++) {
140 hash = ((hash << 5) + hash) + str[i];
142 return (hash & 0x7FFFFFFF);
145 /* fix up an unnamed section, e.g. after adding options to it */
146 __private void uci_fixup_section(struct uci_context *ctx, struct uci_section *s)
148 unsigned int hash = ~0;
149 struct uci_element *e;
156 * Generate a name for unnamed sections. This is used as reference
157 * when locating or updating the section from apps/scripts.
158 * To make multiple concurrent versions somewhat safe for updating,
159 * the name is generated from a hash of its type and name/value
160 * pairs of its option, and it is prefixed by a counter value.
161 * If the order of the unnamed sections changes for some reason,
162 * updates to them will be rejected.
164 hash = djbhash(hash, s->type);
165 uci_foreach_element(&s->options, e) {
166 struct uci_option *o;
167 hash = djbhash(hash, e->name);
168 o = uci_to_option(e);
170 case UCI_TYPE_STRING:
171 hash = djbhash(hash, o->v.string);
177 sprintf(buf, "cfg%02x%04x", ++s->package->n_section, hash % (1 << 16));
178 s->e.name = uci_strdup(ctx, buf);
181 static struct uci_section *
182 uci_alloc_section(struct uci_package *p, const char *type, const char *name)
184 struct uci_context *ctx = p->ctx;
185 struct uci_section *s;
187 if (name && !name[0])
190 s = uci_alloc_element(ctx, section, name, strlen(type) + 1);
191 uci_list_init(&s->options);
192 s->type = uci_dataptr(s);
194 strcpy(s->type, type);
199 uci_list_add(&p->sections, &s->e.list);
205 uci_free_section(struct uci_section *s)
207 struct uci_element *o, *tmp;
209 uci_foreach_element_safe(&s->options, tmp, o) {
210 uci_free_option(uci_to_option(o));
212 if ((s->type != uci_dataptr(s)) &&
215 uci_free_element(&s->e);
218 __plugin struct uci_package *
219 uci_alloc_package(struct uci_context *ctx, const char *name)
221 struct uci_package *p;
223 p = uci_alloc_element(ctx, package, name, 0);
225 uci_list_init(&p->sections);
226 uci_list_init(&p->delta);
227 uci_list_init(&p->saved_delta);
232 uci_free_package(struct uci_package **package)
234 struct uci_element *e, *tmp;
235 struct uci_package *p = *package;
242 uci_foreach_element_safe(&p->sections, tmp, e) {
243 uci_free_section(uci_to_section(e));
245 uci_foreach_element_safe(&p->delta, tmp, e) {
246 uci_free_delta(uci_to_delta(e));
248 uci_foreach_element_safe(&p->saved_delta, tmp, e) {
249 uci_free_delta(uci_to_delta(e));
251 uci_free_element(&p->e);
256 uci_free_any(struct uci_element **e)
259 case UCI_TYPE_SECTION:
260 uci_free_section(uci_to_section(*e));
262 case UCI_TYPE_OPTION:
263 uci_free_option(uci_to_option(*e));
271 __private struct uci_element *
272 uci_lookup_list(struct uci_list *list, const char *name)
274 struct uci_element *e;
276 uci_foreach_element(list, e) {
277 if (!strcmp(e->name, name))
283 static struct uci_element *
284 uci_lookup_ext_section(struct uci_context *ctx, struct uci_ptr *ptr)
286 char *idxstr, *t, *section, *name;
287 struct uci_element *e = NULL;
288 struct uci_section *s;
291 section = uci_strdup(ctx, ptr->section);
292 name = idxstr = section + 1;
294 if (section[0] != '@')
297 /* parse the section index part */
298 idxstr = strchr(idxstr, '[');
304 t = strchr(idxstr, ']');
312 idx = strtol(idxstr, &t, 10);
318 else if (!uci_validate_type(name))
321 /* if the given index is negative, it specifies the section number from
322 * the end of the list */
325 uci_foreach_element(&ptr->p->sections, e) {
326 s = uci_to_section(e);
327 if (name && (strcmp(s->type, name) != 0))
336 uci_foreach_element(&ptr->p->sections, e) {
337 s = uci_to_section(e);
338 if (name && (strcmp(s->type, name) != 0))
350 memset(ptr, 0, sizeof(struct uci_ptr));
351 UCI_THROW(ctx, UCI_ERR_INVAL);
355 ptr->section = e->name;
360 uci_lookup_next(struct uci_context *ctx, struct uci_element **e, struct uci_list *list, const char *name)
364 *e = uci_lookup_list(list, name);
366 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
372 uci_lookup_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str, bool extended)
374 struct uci_element *e;
377 UCI_ASSERT(ctx, ptr != NULL);
380 UCI_INTERNAL(uci_parse_ptr, ctx, ptr, str);
382 ptr->flags |= UCI_LOOKUP_DONE;
384 /* look up the package first */
388 e = uci_lookup_list(&ctx->root, ptr->package);
391 UCI_INTERNAL(uci_load, ctx, ptr->package, &ptr->p);
394 ptr->last = &ptr->p->e;
396 ptr->p = uci_to_package(e);
400 if (!ptr->section && !ptr->s)
403 /* if the section name validates as a regular name, pass through
404 * to the regular uci_lookup function call */
407 } else if (ptr->flags & UCI_LOOKUP_EXTENDED) {
409 e = uci_lookup_ext_section(ctx, ptr);
411 UCI_THROW(ctx, UCI_ERR_INVAL);
413 e = uci_lookup_list(&ptr->p->sections, ptr->section);
420 ptr->s = uci_to_section(e);
423 e = uci_lookup_list(&ptr->s->options, ptr->option);
427 ptr->o = uci_to_option(e);
432 ptr->flags |= UCI_LOOKUP_COMPLETE;
437 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
441 __private struct uci_element *
442 uci_expand_ptr(struct uci_context *ctx, struct uci_ptr *ptr, bool complete)
444 UCI_ASSERT(ctx, ptr != NULL);
446 if (!(ptr->flags & UCI_LOOKUP_DONE))
447 UCI_INTERNAL(uci_lookup_ptr, ctx, ptr, NULL, 1);
448 if (complete && !(ptr->flags & UCI_LOOKUP_COMPLETE))
449 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
450 UCI_ASSERT(ctx, ptr->p != NULL);
452 /* fill in missing string info */
453 if (ptr->p && !ptr->package)
454 ptr->package = ptr->p->e.name;
455 if (ptr->s && !ptr->section)
456 ptr->section = ptr->s->e.name;
457 if (ptr->o && !ptr->option)
458 ptr->option = ptr->o->e.name;
470 static void uci_add_element_list(struct uci_context *ctx, struct uci_ptr *ptr, bool internal)
472 struct uci_element *e;
473 struct uci_package *p;
476 if (!internal && p->has_delta)
477 uci_add_delta(ctx, &p->delta, UCI_CMD_LIST_ADD, ptr->section, ptr->option, ptr->value);
479 e = uci_alloc_generic(ctx, UCI_TYPE_ITEM, ptr->value, sizeof(struct uci_option));
480 uci_list_add(&ptr->o->v.list, &e->list);
483 int uci_rename(struct uci_context *ctx, struct uci_ptr *ptr)
485 /* NB: UCI_INTERNAL use means without delta tracking */
486 bool internal = ctx->internal;
487 struct uci_element *e;
488 struct uci_package *p;
493 e = uci_expand_ptr(ctx, ptr, true);
496 UCI_ASSERT(ctx, ptr->s);
497 UCI_ASSERT(ctx, ptr->value);
499 if (!internal && p->has_delta)
500 uci_add_delta(ctx, &p->delta, UCI_CMD_RENAME, ptr->section, ptr->option, ptr->value);
502 n = uci_strdup(ctx, ptr->value);
507 if (e->type == UCI_TYPE_SECTION)
508 uci_to_section(e)->anonymous = false;
513 int uci_reorder_section(struct uci_context *ctx, struct uci_section *s, int pos)
515 struct uci_package *p = s->package;
520 uci_list_set_pos(&s->package->sections, &s->e.list, pos);
521 if (!ctx->internal && p->has_delta) {
522 sprintf(order, "%d", pos);
523 uci_add_delta(ctx, &p->delta, UCI_CMD_REORDER, s->e.name, NULL, order);
529 int uci_add_section(struct uci_context *ctx, struct uci_package *p, const char *type, struct uci_section **res)
531 bool internal = ctx->internal;
532 struct uci_section *s;
535 UCI_ASSERT(ctx, p != NULL);
536 s = uci_alloc_section(p, type, NULL);
537 uci_fixup_section(ctx, s);
539 if (!internal && p->has_delta)
540 uci_add_delta(ctx, &p->delta, UCI_CMD_ADD, s->e.name, NULL, type);
545 int uci_add_named_section(struct uci_context *ctx, struct uci_package *p, const char *type, const char *name, struct uci_section **res)
547 bool internal = ctx->internal;
548 struct uci_section *s;
551 UCI_ASSERT(ctx, p != NULL);
552 s = uci_alloc_section(p, type, name);
553 uci_fixup_section(ctx, s);
555 if (!internal && p->has_delta)
556 uci_add_delta(ctx, &p->delta, UCI_CMD_ADD, s->e.name, NULL, type);
561 int uci_delete(struct uci_context *ctx, struct uci_ptr *ptr)
563 /* NB: pass on internal flag to uci_del_element */
564 bool internal = ctx->internal;
565 struct uci_package *p;
566 struct uci_element *e;
570 e = uci_expand_ptr(ctx, ptr, true);
573 UCI_ASSERT(ctx, ptr->s);
575 if (!internal && p->has_delta)
576 uci_add_delta(ctx, &p->delta, UCI_CMD_REMOVE, ptr->section, ptr->option, NULL);
582 else if (ptr->section)
588 int uci_add_list(struct uci_context *ctx, struct uci_ptr *ptr)
590 /* NB: UCI_INTERNAL use means without delta tracking */
591 bool internal = ctx->internal;
592 struct uci_option *prev = NULL;
593 const char *value2 = NULL;
597 uci_expand_ptr(ctx, ptr, false);
598 UCI_ASSERT(ctx, ptr->s);
599 UCI_ASSERT(ctx, ptr->value);
602 switch (ptr->o->type) {
603 case UCI_TYPE_STRING:
604 /* we already have a string value, convert that to a list */
607 ptr->value = ptr->o->v.string;
610 uci_add_element_list(ctx, ptr, internal);
613 UCI_THROW(ctx, UCI_ERR_INVAL);
618 ptr->o = uci_alloc_list(ptr->s, ptr->option);
620 uci_add_element_list(ctx, ptr, true);
621 uci_free_option(prev);
624 uci_add_element_list(ctx, ptr, internal);
629 int uci_set(struct uci_context *ctx, struct uci_ptr *ptr)
631 /* NB: UCI_INTERNAL use means without delta tracking */
632 bool internal = ctx->internal;
635 uci_expand_ptr(ctx, ptr, false);
636 UCI_ASSERT(ctx, ptr->value);
637 UCI_ASSERT(ctx, ptr->s || (!ptr->option && ptr->section));
638 if (!ptr->option && ptr->value[0]) {
639 UCI_ASSERT(ctx, uci_validate_type(ptr->value));
642 if (!ptr->o && ptr->s && ptr->option) {
643 struct uci_element *e;
644 e = uci_lookup_list(&ptr->s->options, ptr->option);
646 ptr->o = uci_to_option(e);
648 if (!ptr->value[0]) {
649 /* if setting a nonexistant option/section to a nonexistant value,
650 * exit without errors */
651 if (!(ptr->flags & UCI_LOOKUP_COMPLETE))
654 return uci_delete(ctx, ptr);
655 } else if (!ptr->o && ptr->option) { /* new option */
656 ptr->o = uci_alloc_option(ptr->s, ptr->option, ptr->value);
657 ptr->last = &ptr->o->e;
658 } else if (!ptr->s && ptr->section) { /* new section */
659 ptr->s = uci_alloc_section(ptr->p, ptr->value, ptr->section);
660 ptr->last = &ptr->s->e;
661 } else if (ptr->o && ptr->option) { /* update option */
662 if ((ptr->o->type == UCI_TYPE_STRING) &&
663 !strcmp(ptr->o->v.string, ptr->value))
665 uci_free_option(ptr->o);
666 ptr->o = uci_alloc_option(ptr->s, ptr->option, ptr->value);
667 ptr->last = &ptr->o->e;
668 } else if (ptr->s && ptr->section) { /* update section */
669 char *s = uci_strdup(ctx, ptr->value);
671 if (ptr->s->type == uci_dataptr(ptr->s)) {
673 ptr->last = uci_realloc(ctx, ptr->s, sizeof(struct uci_section));
674 ptr->s = uci_to_section(ptr->last);
675 uci_list_fixup(&ptr->s->e.list);
681 UCI_THROW(ctx, UCI_ERR_INVAL);
684 if (!internal && ptr->p->has_delta)
685 uci_add_delta(ctx, &ptr->p->delta, UCI_CMD_CHANGE, ptr->section, ptr->option, ptr->value);
690 int uci_unload(struct uci_context *ctx, struct uci_package *p)
693 UCI_ASSERT(ctx, p != NULL);
695 uci_free_package(&p);