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
;
52 * uci_alloc_generic allocates a new uci_element with payload
53 * payload is appended to the struct to save memory and reduce fragmentation
55 static struct uci_element
*
56 uci_alloc_generic(struct uci_context
*ctx
, int type
, const char *name
, int size
)
58 struct uci_element
*e
;
62 ptr
= uci_malloc(ctx
, datalen
);
63 e
= (struct uci_element
*) ptr
;
66 UCI_TRAP_SAVE(ctx
, error
);
67 e
->name
= uci_strdup(ctx
, name
);
68 UCI_TRAP_RESTORE(ctx
);
70 uci_list_init(&e
->list
);
75 UCI_THROW(ctx
, ctx
->err
);
82 uci_free_element(struct uci_element
*e
)
86 if (!uci_list_empty(&e
->list
))
87 uci_list_del(&e
->list
);
91 static struct uci_option
*
92 uci_alloc_option(struct uci_section
*s
, const char *name
, const char *value
)
94 struct uci_package
*p
= s
->package
;
95 struct uci_context
*ctx
= p
->ctx
;
98 o
= uci_alloc_element(ctx
, option
, name
, strlen(value
) + 1);
99 o
->type
= UCI_TYPE_STRING
;
100 o
->v
.string
= uci_dataptr(o
);
102 strcpy(o
->v
.string
, value
);
103 uci_list_add(&s
->options
, &o
->e
.list
);
109 uci_free_option(struct uci_option
*o
)
111 struct uci_element
*e
, *tmp
;
114 case UCI_TYPE_STRING
:
115 if ((o
->v
.string
!= uci_dataptr(o
)) &&
116 (o
->v
.string
!= NULL
))
120 uci_foreach_element_safe(&o
->v
.list
, tmp
, e
) {
127 uci_free_element(&o
->e
);
130 static struct uci_option
*
131 uci_alloc_list(struct uci_section
*s
, const char *name
)
133 struct uci_package
*p
= s
->package
;
134 struct uci_context
*ctx
= p
->ctx
;
135 struct uci_option
*o
;
137 o
= uci_alloc_element(ctx
, option
, name
, 0);
138 o
->type
= UCI_TYPE_LIST
;
140 uci_list_init(&o
->v
.list
);
141 uci_list_add(&s
->options
, &o
->e
.list
);
146 /* fix up an unnamed section, e.g. after adding options to it */
147 static void uci_fixup_section(struct uci_context
*ctx
, struct uci_section
*s
)
149 unsigned int hash
= ~0;
150 struct uci_element
*e
;
157 * Generate a name for unnamed sections. This is used as reference
158 * when locating or updating the section from apps/scripts.
159 * To make multiple concurrent versions somewhat safe for updating,
160 * the name is generated from a hash of its type and name/value
161 * pairs of its option, and it is prefixed by a counter value.
162 * If the order of the unnamed sections changes for some reason,
163 * updates to them will be rejected.
165 hash
= djbhash(hash
, s
->type
);
166 uci_foreach_element(&s
->options
, e
) {
167 struct uci_option
*o
;
168 hash
= djbhash(hash
, e
->name
);
169 o
= uci_to_option(e
);
171 case UCI_TYPE_STRING
:
172 hash
= djbhash(hash
, o
->v
.string
);
178 sprintf(buf
, "cfg%02x%04x", ++s
->package
->n_section
, hash
% (1 << 16));
179 s
->e
.name
= uci_strdup(ctx
, buf
);
182 static struct uci_section
*
183 uci_alloc_section(struct uci_package
*p
, const char *type
, const char *name
)
185 struct uci_context
*ctx
= p
->ctx
;
186 struct uci_section
*s
;
188 if (name
&& !name
[0])
191 s
= uci_alloc_element(ctx
, section
, name
, strlen(type
) + 1);
192 uci_list_init(&s
->options
);
193 s
->type
= uci_dataptr(s
);
195 strcpy(s
->type
, type
);
200 uci_list_add(&p
->sections
, &s
->e
.list
);
206 uci_free_section(struct uci_section
*s
)
208 struct uci_element
*o
, *tmp
;
210 uci_foreach_element_safe(&s
->options
, tmp
, o
) {
211 uci_free_option(uci_to_option(o
));
213 if ((s
->type
!= uci_dataptr(s
)) &&
216 uci_free_element(&s
->e
);
219 __plugin
struct uci_package
*
220 uci_alloc_package(struct uci_context
*ctx
, const char *name
)
222 struct uci_package
*p
;
224 p
= uci_alloc_element(ctx
, package
, name
, 0);
226 uci_list_init(&p
->sections
);
227 uci_list_init(&p
->history
);
228 uci_list_init(&p
->saved_history
);
233 uci_free_package(struct uci_package
**package
)
235 struct uci_element
*e
, *tmp
;
236 struct uci_package
*p
= *package
;
243 uci_foreach_element_safe(&p
->sections
, tmp
, e
) {
244 uci_free_section(uci_to_section(e
));
246 uci_foreach_element_safe(&p
->history
, tmp
, e
) {
247 uci_free_history(uci_to_history(e
));
249 uci_foreach_element_safe(&p
->saved_history
, tmp
, e
) {
250 uci_free_history(uci_to_history(e
));
252 uci_free_element(&p
->e
);
256 static struct uci_element
*uci_lookup_list(struct uci_list
*list
, const char *name
)
258 struct uci_element
*e
;
260 uci_foreach_element(list
, e
) {
261 if (!strcmp(e
->name
, name
))
267 int uci_lookup_ext(struct uci_context
*ctx
, struct uci_element
**res
, char *str
)
269 struct uci_package
*p
= NULL
;
270 struct uci_element
*e
;
271 struct uci_section
*s
;
277 UCI_ASSERT(ctx
, res
!= NULL
);
278 UCI_ASSERT(ctx
, str
!= NULL
);
280 UCI_INTERNAL(uci_parse_ptr
, ctx
, &ptr
, str
);
282 /* look up the package first */
283 e
= uci_lookup_list(&ctx
->root
, ptr
.package
);
285 UCI_INTERNAL(uci_load
, ctx
, ptr
.package
, &p
);
290 p
= uci_to_package(e
);
296 /* if the section name validates as a regular name, pass through
297 * to the regular uci_lookup function call */
298 if (!(ptr
.flags
& UCI_LOOKUP_EXTENDED
)) {
299 UCI_INTERNAL(uci_lookup
, ctx
, &e
, p
, ptr
.section
, ptr
.option
);
303 /* name did not validate, that means we have an extended lookup call
304 * parse it here. for now only the section index syntax is supported */
305 if (ptr
.section
[0] != '@')
310 /* parse the section index part */
311 idxstr
= strchr(ptr
.section
, '[');
317 t
= strchr(idxstr
, ']');
325 idx
= strtol(idxstr
, &t
, 10);
331 if (ptr
.section
&& !uci_validate_str(ptr
.section
, false))
334 /* if the given index is negative, it specifies the section number from
335 * the end of the list */
338 uci_foreach_element(&p
->sections
, e
) {
339 s
= uci_to_section(e
);
340 if (ptr
.section
&& (strcmp(s
->type
, ptr
.section
) != 0))
349 uci_foreach_element(&p
->sections
, e
) {
350 s
= uci_to_section(e
);
351 if (ptr
.section
&& (strcmp(s
->type
, ptr
.section
) != 0))
362 e
= uci_lookup_list(&s
->options
, ptr
.option
);
368 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
370 UCI_THROW(ctx
, UCI_ERR_INVAL
);
374 int uci_lookup(struct uci_context
*ctx
, struct uci_element
**res
, struct uci_package
*p
, const char *section
, const char *option
)
376 struct uci_element
*e
;
377 struct uci_section
*s
;
380 UCI_ASSERT(ctx
, res
!= NULL
);
381 UCI_ASSERT(ctx
, p
!= NULL
);
382 UCI_ASSERT(ctx
, section
&& uci_validate_name(section
));
384 UCI_ASSERT(ctx
, uci_validate_name(option
));
386 e
= uci_lookup_list(&p
->sections
, section
);
391 s
= uci_to_section(e
);
392 e
= uci_lookup_list(&s
->options
, option
);
401 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
405 int uci_del_element(struct uci_context
*ctx
, struct uci_element
*e
)
407 /* NB: UCI_INTERNAL use means without history tracking */
408 bool internal
= ctx
->internal
;
409 struct uci_package
*p
= NULL
;
410 struct uci_section
*s
= NULL
;
411 struct uci_option
*o
= NULL
;
412 struct uci_element
*i
, *tmp
;
416 UCI_ASSERT(ctx
, e
!= NULL
);
419 case UCI_TYPE_SECTION
:
420 s
= uci_to_section(e
);
421 uci_foreach_element_safe(&s
->options
, tmp
, i
) {
422 uci_del_element(ctx
, i
);
425 case UCI_TYPE_OPTION
:
426 o
= uci_to_option(e
);
432 UCI_THROW(ctx
, UCI_ERR_INVAL
);
437 if (!internal
&& p
->has_history
)
438 uci_add_history(ctx
, &p
->history
, UCI_CMD_REMOVE
, s
->e
.name
, option
, NULL
);
441 case UCI_TYPE_SECTION
:
444 case UCI_TYPE_OPTION
:
454 int uci_add_element_list(struct uci_context
*ctx
, struct uci_option
*o
, const char *value
)
456 struct uci_element
*e
;
457 struct uci_package
*p
;
458 struct uci_section
*s
;
459 bool internal
= ctx
->internal
;
462 UCI_ASSERT(ctx
, (o
!= NULL
) && (o
->type
== UCI_TYPE_LIST
) && uci_validate_text(value
));
467 if (!internal
&& p
->has_history
)
468 uci_add_history(ctx
, &p
->history
, UCI_CMD_LIST_ADD
, s
->e
.name
, o
->e
.name
, value
);
470 e
= uci_alloc_generic(ctx
, UCI_TYPE_ITEM
, value
, sizeof(struct uci_option
));
471 uci_list_add(&o
->v
.list
, &e
->list
);
476 int uci_set_element_value(struct uci_context
*ctx
, struct uci_element
**element
, const char *value
)
478 /* NB: UCI_INTERNAL use means without history tracking */
479 bool internal
= ctx
->internal
;
480 struct uci_list
*list
;
481 struct uci_element
*e
;
482 struct uci_package
*p
;
483 struct uci_section
*s
;
484 struct uci_option
*o
;
491 UCI_ASSERT(ctx
, (element
!= NULL
) && (*element
!= NULL
));
493 /* what the 'value' of an element means depends on the type
494 * for a section, the 'value' means its type
495 * for an option, the 'value' means its value string
496 * when changing the value, shrink the element to its actual size
497 * (it may have been allocated with a bigger size, to include
499 * then duplicate the string passed on the command line and
500 * insert it into the structure.
506 case UCI_TYPE_SECTION
:
507 UCI_ASSERT(ctx
, uci_validate_str(value
, false));
508 size
= sizeof(struct uci_section
);
509 s
= uci_to_section(e
);
512 /* matches the currently set value */
513 if (!strcmp(value
, s
->type
))
517 case UCI_TYPE_OPTION
:
518 UCI_ASSERT(ctx
, value
!= NULL
);
519 o
= uci_to_option(e
);
524 case UCI_TYPE_STRING
:
525 size
= sizeof(struct uci_option
);
526 /* matches the currently set value */
527 if (!strcmp(value
, o
->v
.string
))
531 /* default action for non-string datatypes is to delete
532 * the existing entry, then re-create it as a string */
538 UCI_THROW(ctx
, UCI_ERR_INVAL
);
542 if (!internal
&& p
->has_history
)
543 uci_add_history(ctx
, &p
->history
, UCI_CMD_CHANGE
, section
, option
, value
);
545 if ((e
->type
== UCI_TYPE_OPTION
) && (size
== 0)) {
546 o
= uci_alloc_option(s
, option
, value
);
547 UCI_INTERNAL(uci_del_element
, ctx
, e
);
552 uci_list_del(&e
->list
);
553 e
= uci_realloc(ctx
, e
, size
);
554 str
= uci_strdup(ctx
, value
);
555 uci_list_insert(list
, &e
->list
);
559 case UCI_TYPE_SECTION
:
560 uci_to_section(e
)->type
= str
;
562 case UCI_TYPE_OPTION
:
563 uci_to_option(e
)->v
.string
= str
;
573 int uci_rename(struct uci_context
*ctx
, struct uci_package
*p
, char *section
, char *option
, char *name
)
575 /* NB: UCI_INTERNAL use means without history tracking */
576 bool internal
= ctx
->internal
;
577 struct uci_element
*e
;
581 /* NB: p, section, option validated by uci_lookup */
582 UCI_INTERNAL(uci_lookup
, ctx
, &e
, p
, section
, option
);
584 if (!internal
&& p
->has_history
)
585 uci_add_history(ctx
, &p
->history
, UCI_CMD_RENAME
, section
, option
, name
);
587 name
= uci_strdup(ctx
, name
);
595 int uci_add_section(struct uci_context
*ctx
, struct uci_package
*p
, const char *type
, struct uci_section
**res
)
597 bool internal
= ctx
->internal
;
598 struct uci_section
*s
;
601 UCI_ASSERT(ctx
, p
!= NULL
);
602 s
= uci_alloc_section(p
, type
, NULL
);
603 uci_fixup_section(ctx
, s
);
605 if (!internal
&& p
->has_history
)
606 uci_add_history(ctx
, &p
->history
, UCI_CMD_ADD
, s
->e
.name
, NULL
, type
);
611 int uci_delete(struct uci_context
*ctx
, struct uci_package
*p
, const char *section
, const char *option
)
613 /* NB: pass on internal flag to uci_del_element */
614 bool internal
= ctx
->internal
;
615 struct uci_element
*e
;
619 /* NB: p, section, option validated by uci_lookup */
620 UCI_INTERNAL(uci_lookup
, ctx
, &e
, p
, section
, option
);
622 ctx
->internal
= internal
;
623 return uci_del_element(ctx
, e
);
626 int uci_add_list(struct uci_context
*ctx
, struct uci_package
*p
, const char *section
, const char *option
, const char *value
, struct uci_option
**result
)
628 /* NB: UCI_INTERNAL use means without history tracking */
629 bool internal
= ctx
->internal
;
630 struct uci_element
*e
;
631 struct uci_section
*s
;
632 struct uci_option
*o
;
633 struct uci_option
*prev
= NULL
;
634 const char *value2
= NULL
;
637 UCI_ASSERT(ctx
, p
&& section
&& option
&& value
&& uci_validate_text(value
));
639 /* look up the section first */
640 UCI_INTERNAL(uci_lookup
, ctx
, &e
, p
, section
, NULL
);
641 s
= uci_to_section(e
);
643 e
= uci_lookup_list(&s
->options
, option
);
645 o
= uci_to_option(e
);
647 case UCI_TYPE_STRING
:
648 /* we already have a string value, let's convert that to a list */
657 ctx
->internal
= internal
;
658 return uci_add_element_list(ctx
, o
, value
);
660 UCI_THROW(ctx
, UCI_ERR_INVAL
);
665 o
= uci_alloc_list(s
, option
);
669 UCI_INTERNAL(uci_add_element_list
, ctx
, o
, value
);
670 uci_free_option(prev
);
674 ctx
->internal
= internal
;
675 return uci_add_element_list(ctx
, o
, value
);
678 int uci_set(struct uci_context
*ctx
, struct uci_package
*p
, const char *section
, const char *option
, const char *value
, struct uci_element
**result
)
680 /* NB: UCI_INTERNAL use means without history tracking */
681 bool internal
= ctx
->internal
;
682 struct uci_element
*e
= NULL
;
683 struct uci_section
*s
= NULL
;
684 struct uci_option
*o
= NULL
;
687 UCI_ASSERT(ctx
, p
!= NULL
);
688 UCI_ASSERT(ctx
, uci_validate_name(section
));
690 UCI_ASSERT(ctx
, uci_validate_name(option
));
691 UCI_ASSERT(ctx
, value
!= NULL
);
693 UCI_ASSERT(ctx
, uci_validate_str(value
, false));
697 * look up the package, section and option (if set)
698 * if the section/option is to be modified and it is not found
699 * create a new element in the appropriate list
701 e
= uci_lookup_list(&p
->sections
, section
);
705 s
= uci_to_section(e
);
706 if (ctx
->pctx
&& ctx
->pctx
->merge
)
707 ctx
->pctx
->section
= s
;
710 e
= uci_lookup_list(&s
->options
, option
);
713 o
= uci_to_option(e
);
717 * no unknown element was supplied, assume that we can just update
729 ctx
->internal
= internal
;
730 return uci_set_element_value(ctx
, result
, value
);
734 * the entry that we need to update was not found,
735 * check if the search failed prematurely.
736 * this can happen if the package was not found, or if
737 * an option was supplied, but the section wasn't found
739 if (!p
|| (!s
&& option
))
740 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
742 /* now add the missing entry */
743 if (!internal
&& p
->has_history
)
744 uci_add_history(ctx
, &p
->history
, UCI_CMD_CHANGE
, section
, option
, value
);
746 o
= uci_alloc_option(s
, option
, value
);
750 s
= uci_alloc_section(p
, value
, section
);
753 if (ctx
->pctx
&& ctx
->pctx
->merge
)
754 ctx
->pctx
->section
= s
;
760 int uci_unload(struct uci_context
*ctx
, struct uci_package
*p
)
763 UCI_ASSERT(ctx
, p
!= NULL
);
765 uci_free_package(&p
);