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);
361 ptr->section = e->name;
366 uci_lookup_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str, bool extended)
368 struct uci_element *e;
371 UCI_ASSERT(ctx, ptr != NULL);
374 UCI_INTERNAL(uci_parse_ptr, ctx, ptr, str);
376 ptr->flags |= UCI_LOOKUP_DONE;
378 /* look up the package first */
379 e = uci_lookup_list(&ctx->root, ptr->package);
381 UCI_INTERNAL(uci_load, ctx, ptr->package, &ptr->p);
384 ptr->last = &ptr->p->e;
386 ptr->p = uci_to_package(e);
393 /* if the section name validates as a regular name, pass through
394 * to the regular uci_lookup function call */
395 if (ptr->flags & UCI_LOOKUP_EXTENDED)
396 e = uci_lookup_ext_section(ctx, ptr);
398 e = uci_lookup_list(&ptr->p->sections, ptr->section);
404 ptr->s = uci_to_section(e);
407 e = uci_lookup_list(&ptr->s->options, ptr->option);
411 ptr->o = uci_to_option(e);
416 ptr->flags |= UCI_LOOKUP_COMPLETE;
421 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
426 uci_fill_ptr(struct uci_context *ctx, struct uci_ptr *ptr, struct uci_element *e, bool complete)
429 UCI_ASSERT(ctx, ptr != NULL);
430 UCI_ASSERT(ctx, e != NULL);
432 memset(ptr, 0, sizeof(struct uci_ptr));
434 case UCI_TYPE_OPTION:
435 ptr->o = uci_to_option(e);
437 case UCI_TYPE_SECTION:
438 ptr->s = uci_to_section(e);
440 case UCI_TYPE_PACKAGE:
441 ptr->p = uci_to_package(e);
444 UCI_THROW(ctx, UCI_ERR_INVAL);
448 ptr->option = ptr->o->e.name;
449 ptr->s = ptr->o->section;
451 ptr->section = ptr->s->e.name;
452 ptr->p = ptr->s->package;
454 ptr->package = ptr->p->e.name;
456 ptr->flags |= UCI_LOOKUP_DONE;
458 ptr->flags |= UCI_LOOKUP_COMPLETE;
463 static struct uci_element *
464 expand_ptr(struct uci_context *ctx, struct uci_ptr *ptr, bool complete)
466 UCI_ASSERT(ctx, ptr != NULL);
468 if (!(ptr->flags & UCI_LOOKUP_DONE))
469 uci_lookup_ptr(ctx, ptr, NULL, 1);
470 if (complete && !(ptr->flags & UCI_LOOKUP_COMPLETE))
471 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
472 UCI_ASSERT(ctx, ptr->p != NULL);
474 /* fill in missing string info */
475 if (ptr->p && !ptr->package)
476 ptr->package = ptr->p->e.name;
477 if (ptr->s && !ptr->section)
478 ptr->section = ptr->s->e.name;
479 if (ptr->o && !ptr->option)
480 ptr->option = ptr->o->e.name;
492 static void uci_add_element_list(struct uci_context *ctx, struct uci_ptr *ptr, bool internal)
494 struct uci_element *e;
495 struct uci_package *p;
498 if (!internal && p->has_history)
499 uci_add_history(ctx, &p->history, UCI_CMD_LIST_ADD, ptr->section, ptr->option, ptr->value);
501 e = uci_alloc_generic(ctx, UCI_TYPE_ITEM, ptr->value, sizeof(struct uci_option));
502 uci_list_add(&ptr->o->v.list, &e->list);
505 int uci_rename(struct uci_context *ctx, struct uci_ptr *ptr)
507 /* NB: UCI_INTERNAL use means without history tracking */
508 bool internal = ctx->internal;
509 struct uci_element *e;
510 struct uci_package *p;
515 e = expand_ptr(ctx, ptr, true);
518 UCI_ASSERT(ctx, ptr->s);
519 UCI_ASSERT(ctx, ptr->value);
521 if (!internal && p->has_history)
522 uci_add_history(ctx, &p->history, UCI_CMD_RENAME, ptr->section, ptr->option, ptr->value);
524 n = uci_strdup(ctx, ptr->value);
532 int uci_add_section(struct uci_context *ctx, struct uci_package *p, const char *type, struct uci_section **res)
534 bool internal = ctx->internal;
535 struct uci_section *s;
538 UCI_ASSERT(ctx, p != NULL);
539 s = uci_alloc_section(p, type, NULL);
540 uci_fixup_section(ctx, s);
542 if (!internal && p->has_history)
543 uci_add_history(ctx, &p->history, UCI_CMD_ADD, s->e.name, NULL, type);
548 int uci_delete(struct uci_context *ctx, struct uci_ptr *ptr)
550 /* NB: pass on internal flag to uci_del_element */
551 bool internal = ctx->internal;
552 struct uci_package *p;
553 struct uci_element *e;
557 e = expand_ptr(ctx, ptr, true);
560 UCI_ASSERT(ctx, ptr->s);
562 if (!internal && p->has_history)
563 uci_add_history(ctx, &p->history, UCI_CMD_REMOVE, ptr->section, ptr->option, NULL);
569 int uci_add_list(struct uci_context *ctx, struct uci_ptr *ptr)
571 /* NB: UCI_INTERNAL use means without history tracking */
572 bool internal = ctx->internal;
573 struct uci_option *prev = NULL;
574 const char *value2 = NULL;
578 expand_ptr(ctx, ptr, false);
579 UCI_ASSERT(ctx, ptr->s);
580 UCI_ASSERT(ctx, ptr->value);
583 switch (ptr->o->type) {
584 case UCI_TYPE_STRING:
585 /* we already have a string value, convert that to a list */
588 ptr->value = ptr->o->v.string;
591 uci_add_element_list(ctx, ptr, internal);
594 UCI_THROW(ctx, UCI_ERR_INVAL);
599 ptr->o = uci_alloc_list(ptr->s, ptr->option);
601 uci_add_element_list(ctx, ptr, true);
602 uci_free_option(prev);
605 uci_add_element_list(ctx, ptr, internal);
610 int uci_set(struct uci_context *ctx, struct uci_ptr *ptr)
612 /* NB: UCI_INTERNAL use means without history tracking */
613 bool internal = ctx->internal;
616 expand_ptr(ctx, ptr, false);
617 UCI_ASSERT(ctx, ptr->value);
618 UCI_ASSERT(ctx, ptr->s || (!ptr->option && ptr->section));
620 UCI_ASSERT(ctx, uci_validate_str(ptr->value, false));
623 if (!ptr->o && ptr->s && ptr->option) {
624 struct uci_element *e;
625 e = uci_lookup_list(&ptr->s->options, ptr->option);
627 ptr->o = uci_to_option(e);
629 if (!ptr->o && ptr->option) { /* new option */
630 ptr->o = uci_alloc_option(ptr->s, ptr->option, ptr->value);
631 ptr->last = &ptr->o->e;
632 } else if (!ptr->s && ptr->section) { /* new section */
633 ptr->s = uci_alloc_section(ptr->p, ptr->value, ptr->section);
634 ptr->last = &ptr->s->e;
635 } else if (ptr->o && ptr->option) { /* update option */
636 if ((ptr->o->type == UCI_TYPE_STRING) &&
637 !strcmp(ptr->o->v.string, ptr->value))
639 uci_free_option(ptr->o);
640 ptr->o = uci_alloc_option(ptr->s, ptr->option, ptr->value);
641 ptr->last = &ptr->o->e;
642 } else if (ptr->s && ptr->section) { /* update section */
643 char *s = uci_strdup(ctx, ptr->value);
645 if (ptr->s->type == uci_dataptr(ptr->s)) {
647 ptr->last = uci_realloc(ctx, ptr->s, sizeof(struct uci_section));
648 ptr->s = uci_to_section(ptr->last);
649 uci_list_fixup(&ptr->s->e.list);
655 UCI_THROW(ctx, UCI_ERR_INVAL);
658 if (!internal && ptr->p->has_history)
659 uci_add_history(ctx, &ptr->p->history, UCI_CMD_CHANGE, ptr->section, ptr->option, ptr->value);
664 int uci_unload(struct uci_context *ctx, struct uci_package *p)
667 UCI_ASSERT(ctx, p != NULL);
669 uci_free_package(&p);