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 /* initialize a list head/item */
16 static inline void uci_list_init(struct uci_list *ptr)
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)
25 list->next->prev = ptr;
27 ptr->next = list->next;
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)
34 /* NB: head->prev points at the tail */
35 uci_list_insert(head->prev, ptr);
38 static inline void uci_list_del(struct uci_list *ptr)
40 struct uci_list *next, *prev;
51 static inline void uci_list_fixup(struct uci_list *ptr)
53 ptr->prev->next = ptr;
54 ptr->next->prev = ptr;
58 * uci_alloc_generic allocates a new uci_element with payload
59 * payload is appended to the struct to save memory and reduce fragmentation
61 static struct uci_element *
62 uci_alloc_generic(struct uci_context *ctx, int type, const char *name, int size)
64 struct uci_element *e;
68 ptr = uci_malloc(ctx, datalen);
69 e = (struct uci_element *) ptr;
72 UCI_TRAP_SAVE(ctx, error);
73 e->name = uci_strdup(ctx, name);
74 UCI_TRAP_RESTORE(ctx);
76 uci_list_init(&e->list);
81 UCI_THROW(ctx, ctx->err);
88 uci_free_element(struct uci_element *e)
92 if (!uci_list_empty(&e->list))
93 uci_list_del(&e->list);
97 static struct uci_option *
98 uci_alloc_option(struct uci_section *s, const char *name, const char *value)
100 struct uci_package *p = s->package;
101 struct uci_context *ctx = p->ctx;
102 struct uci_option *o;
104 o = uci_alloc_element(ctx, option, name, strlen(value) + 1);
105 o->type = UCI_TYPE_STRING;
106 o->v.string = uci_dataptr(o);
108 strcpy(o->v.string, value);
109 uci_list_add(&s->options, &o->e.list);
115 uci_free_option(struct uci_option *o)
117 struct uci_element *e, *tmp;
120 case UCI_TYPE_STRING:
121 if ((o->v.string != uci_dataptr(o)) &&
122 (o->v.string != NULL))
126 uci_foreach_element_safe(&o->v.list, tmp, e) {
133 uci_free_element(&o->e);
136 static struct uci_option *
137 uci_alloc_list(struct uci_section *s, const char *name)
139 struct uci_package *p = s->package;
140 struct uci_context *ctx = p->ctx;
141 struct uci_option *o;
143 o = uci_alloc_element(ctx, option, name, 0);
144 o->type = UCI_TYPE_LIST;
146 uci_list_init(&o->v.list);
147 uci_list_add(&s->options, &o->e.list);
152 /* fix up an unnamed section, e.g. after adding options to it */
153 static void uci_fixup_section(struct uci_context *ctx, struct uci_section *s)
155 unsigned int hash = ~0;
156 struct uci_element *e;
163 * Generate a name for unnamed sections. This is used as reference
164 * when locating or updating the section from apps/scripts.
165 * To make multiple concurrent versions somewhat safe for updating,
166 * the name is generated from a hash of its type and name/value
167 * pairs of its option, and it is prefixed by a counter value.
168 * If the order of the unnamed sections changes for some reason,
169 * updates to them will be rejected.
171 hash = djbhash(hash, s->type);
172 uci_foreach_element(&s->options, e) {
173 struct uci_option *o;
174 hash = djbhash(hash, e->name);
175 o = uci_to_option(e);
177 case UCI_TYPE_STRING:
178 hash = djbhash(hash, o->v.string);
184 sprintf(buf, "cfg%02x%04x", ++s->package->n_section, hash % (1 << 16));
185 s->e.name = uci_strdup(ctx, buf);
188 static struct uci_section *
189 uci_alloc_section(struct uci_package *p, const char *type, const char *name)
191 struct uci_context *ctx = p->ctx;
192 struct uci_section *s;
194 if (name && !name[0])
197 s = uci_alloc_element(ctx, section, name, strlen(type) + 1);
198 uci_list_init(&s->options);
199 s->type = uci_dataptr(s);
201 strcpy(s->type, type);
206 uci_list_add(&p->sections, &s->e.list);
212 uci_free_section(struct uci_section *s)
214 struct uci_element *o, *tmp;
216 uci_foreach_element_safe(&s->options, tmp, o) {
217 uci_free_option(uci_to_option(o));
219 if ((s->type != uci_dataptr(s)) &&
222 uci_free_element(&s->e);
225 __plugin struct uci_package *
226 uci_alloc_package(struct uci_context *ctx, const char *name)
228 struct uci_package *p;
230 p = uci_alloc_element(ctx, package, name, 0);
232 uci_list_init(&p->sections);
233 uci_list_init(&p->history);
234 uci_list_init(&p->saved_history);
239 uci_free_package(struct uci_package **package)
241 struct uci_element *e, *tmp;
242 struct uci_package *p = *package;
249 uci_foreach_element_safe(&p->sections, tmp, e) {
250 uci_free_section(uci_to_section(e));
252 uci_foreach_element_safe(&p->history, tmp, e) {
253 uci_free_history(uci_to_history(e));
255 uci_foreach_element_safe(&p->saved_history, tmp, e) {
256 uci_free_history(uci_to_history(e));
258 uci_free_element(&p->e);
263 uci_free_any(struct uci_element **e)
266 case UCI_TYPE_SECTION:
267 uci_free_section(uci_to_section(*e));
269 case UCI_TYPE_OPTION:
270 uci_free_option(uci_to_option(*e));
278 static inline struct uci_element *
279 uci_lookup_list(struct uci_list *list, const char *name)
281 struct uci_element *e;
283 uci_foreach_element(list, e) {
284 if (!strcmp(e->name, name))
290 static struct uci_element *
291 uci_lookup_ext_section(struct uci_context *ctx, struct uci_ptr *ptr)
293 char *idxstr, *t, *section, *name;
294 struct uci_element *e = NULL;
295 struct uci_section *s;
298 section = uci_strdup(ctx, ptr->section);
299 name = idxstr = section + 1;
301 if (section[0] != '@')
304 /* parse the section index part */
305 idxstr = strchr(idxstr, '[');
311 t = strchr(idxstr, ']');
319 idx = strtol(idxstr, &t, 10);
325 else if (!uci_validate_str(name, false))
328 /* if the given index is negative, it specifies the section number from
329 * the end of the list */
332 uci_foreach_element(&ptr->p->sections, e) {
333 s = uci_to_section(e);
334 if (name && (strcmp(s->type, name) != 0))
343 uci_foreach_element(&ptr->p->sections, e) {
344 s = uci_to_section(e);
345 if (name && (strcmp(s->type, name) != 0))
357 memset(ptr, 0, sizeof(struct uci_ptr));
358 UCI_THROW(ctx, UCI_ERR_INVAL);
362 ptr->section = e->name;
367 uci_lookup_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str, bool extended)
369 struct uci_element *e;
372 UCI_ASSERT(ctx, ptr != NULL);
375 UCI_INTERNAL(uci_parse_ptr, ctx, ptr, str);
377 ptr->flags |= UCI_LOOKUP_DONE;
379 /* look up the package first */
380 e = uci_lookup_list(&ctx->root, ptr->package);
382 UCI_INTERNAL(uci_load, ctx, ptr->package, &ptr->p);
385 ptr->last = &ptr->p->e;
387 ptr->p = uci_to_package(e);
394 /* if the section name validates as a regular name, pass through
395 * to the regular uci_lookup function call */
396 if (ptr->flags & UCI_LOOKUP_EXTENDED)
397 e = uci_lookup_ext_section(ctx, ptr);
399 e = uci_lookup_list(&ptr->p->sections, ptr->section);
405 ptr->s = uci_to_section(e);
408 e = uci_lookup_list(&ptr->s->options, ptr->option);
412 ptr->o = uci_to_option(e);
417 ptr->flags |= UCI_LOOKUP_COMPLETE;
422 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
427 uci_fill_ptr(struct uci_context *ctx, struct uci_ptr *ptr, struct uci_element *e, bool complete)
430 UCI_ASSERT(ctx, ptr != NULL);
431 UCI_ASSERT(ctx, e != NULL);
433 memset(ptr, 0, sizeof(struct uci_ptr));
435 case UCI_TYPE_OPTION:
436 ptr->o = uci_to_option(e);
438 case UCI_TYPE_SECTION:
439 ptr->s = uci_to_section(e);
441 case UCI_TYPE_PACKAGE:
442 ptr->p = uci_to_package(e);
445 UCI_THROW(ctx, UCI_ERR_INVAL);
449 ptr->option = ptr->o->e.name;
450 ptr->s = ptr->o->section;
452 ptr->section = ptr->s->e.name;
453 ptr->p = ptr->s->package;
455 ptr->package = ptr->p->e.name;
457 ptr->flags |= UCI_LOOKUP_DONE;
459 ptr->flags |= UCI_LOOKUP_COMPLETE;
464 static struct uci_element *
465 expand_ptr(struct uci_context *ctx, struct uci_ptr *ptr, bool complete)
467 UCI_ASSERT(ctx, ptr != NULL);
469 if (!(ptr->flags & UCI_LOOKUP_DONE))
470 UCI_INTERNAL(uci_lookup_ptr, ctx, ptr, NULL, 1);
471 if (complete && !(ptr->flags & UCI_LOOKUP_COMPLETE))
472 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
473 UCI_ASSERT(ctx, ptr->p != NULL);
475 /* fill in missing string info */
476 if (ptr->p && !ptr->package)
477 ptr->package = ptr->p->e.name;
478 if (ptr->s && !ptr->section)
479 ptr->section = ptr->s->e.name;
480 if (ptr->o && !ptr->option)
481 ptr->option = ptr->o->e.name;
493 static void uci_add_element_list(struct uci_context *ctx, struct uci_ptr *ptr, bool internal)
495 struct uci_element *e;
496 struct uci_package *p;
499 if (!internal && p->has_history)
500 uci_add_history(ctx, &p->history, UCI_CMD_LIST_ADD, ptr->section, ptr->option, ptr->value);
502 e = uci_alloc_generic(ctx, UCI_TYPE_ITEM, ptr->value, sizeof(struct uci_option));
503 uci_list_add(&ptr->o->v.list, &e->list);
506 int uci_rename(struct uci_context *ctx, struct uci_ptr *ptr)
508 /* NB: UCI_INTERNAL use means without history tracking */
509 bool internal = ctx->internal;
510 struct uci_element *e;
511 struct uci_package *p;
516 e = expand_ptr(ctx, ptr, true);
519 UCI_ASSERT(ctx, ptr->s);
520 UCI_ASSERT(ctx, ptr->value);
522 if (!internal && p->has_history)
523 uci_add_history(ctx, &p->history, UCI_CMD_RENAME, ptr->section, ptr->option, ptr->value);
525 n = uci_strdup(ctx, ptr->value);
533 int uci_add_section(struct uci_context *ctx, struct uci_package *p, const char *type, struct uci_section **res)
535 bool internal = ctx->internal;
536 struct uci_section *s;
539 UCI_ASSERT(ctx, p != NULL);
540 s = uci_alloc_section(p, type, NULL);
541 uci_fixup_section(ctx, s);
543 if (!internal && p->has_history)
544 uci_add_history(ctx, &p->history, UCI_CMD_ADD, s->e.name, NULL, type);
549 int uci_delete(struct uci_context *ctx, struct uci_ptr *ptr)
551 /* NB: pass on internal flag to uci_del_element */
552 bool internal = ctx->internal;
553 struct uci_package *p;
554 struct uci_element *e;
558 e = expand_ptr(ctx, ptr, true);
561 UCI_ASSERT(ctx, ptr->s);
563 if (!internal && p->has_history)
564 uci_add_history(ctx, &p->history, UCI_CMD_REMOVE, ptr->section, ptr->option, NULL);
570 int uci_add_list(struct uci_context *ctx, struct uci_ptr *ptr)
572 /* NB: UCI_INTERNAL use means without history tracking */
573 bool internal = ctx->internal;
574 struct uci_option *prev = NULL;
575 const char *value2 = NULL;
579 expand_ptr(ctx, ptr, false);
580 UCI_ASSERT(ctx, ptr->s);
581 UCI_ASSERT(ctx, ptr->value);
584 switch (ptr->o->type) {
585 case UCI_TYPE_STRING:
586 /* we already have a string value, convert that to a list */
589 ptr->value = ptr->o->v.string;
592 uci_add_element_list(ctx, ptr, internal);
595 UCI_THROW(ctx, UCI_ERR_INVAL);
600 ptr->o = uci_alloc_list(ptr->s, ptr->option);
602 uci_add_element_list(ctx, ptr, true);
603 uci_free_option(prev);
606 uci_add_element_list(ctx, ptr, internal);
611 int uci_set(struct uci_context *ctx, struct uci_ptr *ptr)
613 /* NB: UCI_INTERNAL use means without history tracking */
614 bool internal = ctx->internal;
617 expand_ptr(ctx, ptr, false);
618 UCI_ASSERT(ctx, ptr->value);
619 UCI_ASSERT(ctx, ptr->s || (!ptr->option && ptr->section));
621 UCI_ASSERT(ctx, uci_validate_str(ptr->value, false));
624 if (!ptr->o && ptr->s && ptr->option) {
625 struct uci_element *e;
626 e = uci_lookup_list(&ptr->s->options, ptr->option);
628 ptr->o = uci_to_option(e);
630 if (!ptr->o && ptr->option) { /* new option */
631 ptr->o = uci_alloc_option(ptr->s, ptr->option, ptr->value);
632 ptr->last = &ptr->o->e;
633 } else if (!ptr->s && ptr->section) { /* new section */
634 ptr->s = uci_alloc_section(ptr->p, ptr->value, ptr->section);
635 ptr->last = &ptr->s->e;
636 } else if (ptr->o && ptr->option) { /* update option */
637 if ((ptr->o->type == UCI_TYPE_STRING) &&
638 !strcmp(ptr->o->v.string, ptr->value))
640 uci_free_option(ptr->o);
641 ptr->o = uci_alloc_option(ptr->s, ptr->option, ptr->value);
642 ptr->last = &ptr->o->e;
643 } else if (ptr->s && ptr->section) { /* update section */
644 char *s = uci_strdup(ctx, ptr->value);
646 if (ptr->s->type == uci_dataptr(ptr->s)) {
648 ptr->last = uci_realloc(ctx, ptr->s, sizeof(struct uci_section));
649 ptr->s = uci_to_section(ptr->last);
650 uci_list_fixup(&ptr->s->e.list);
656 UCI_THROW(ctx, UCI_ERR_INVAL);
659 if (!internal && ptr->p->has_history)
660 uci_add_history(ctx, &ptr->p->history, UCI_CMD_CHANGE, ptr->section, ptr->option, ptr->value);
665 int uci_unload(struct uci_context *ctx, struct uci_package *p)
668 UCI_ASSERT(ctx, p != NULL);
670 uci_free_package(&p);