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 *ptr
)
269 struct uci_package
*p
= NULL
;
270 struct uci_element
*e
;
271 struct uci_section
*s
;
272 char *package
= NULL
;
273 char *section
= NULL
;
279 UCI_ASSERT(ctx
, res
!= NULL
);
280 UCI_ASSERT(ctx
, ptr
!= NULL
);
282 UCI_INTERNAL(uci_parse_tuple
, ctx
, ptr
, &package
, §ion
, &option
, NULL
);
284 /* look up the package first */
285 e
= uci_lookup_list(&ctx
->root
, package
);
287 UCI_INTERNAL(uci_load
, ctx
, package
, &p
);
292 p
= uci_to_package(e
);
298 /* if the section name validates as a regular name, pass through
299 * to the regular uci_lookup function call */
300 if (!*section
|| uci_validate_name(section
)) {
301 UCI_INTERNAL(uci_lookup
, ctx
, &e
, p
, section
, option
);
305 /* name did not validate, that means we have an extended lookup call
306 * parse it here. for now only the section index syntax is supported */
307 if (section
[0] != '@')
312 /* parse the section index part */
313 idxstr
= strchr(section
, '[');
319 t
= strchr(idxstr
, ']');
327 idx
= strtol(idxstr
, &t
, 10);
333 if (section
&& !uci_validate_str(section
, false))
336 /* if the given index is negative, it specifies the section number from
337 * the end of the list */
340 uci_foreach_element(&p
->sections
, e
) {
341 s
= uci_to_section(e
);
342 if (section
&& (strcmp(s
->type
, section
) != 0))
351 uci_foreach_element(&p
->sections
, e
) {
352 s
= uci_to_section(e
);
353 if (section
&& (strcmp(s
->type
, section
) != 0))
364 e
= uci_lookup_list(&s
->options
, option
);
370 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
372 UCI_THROW(ctx
, UCI_ERR_INVAL
);
376 int uci_lookup(struct uci_context
*ctx
, struct uci_element
**res
, struct uci_package
*p
, const char *section
, const char *option
)
378 struct uci_element
*e
;
379 struct uci_section
*s
;
382 UCI_ASSERT(ctx
, res
!= NULL
);
383 UCI_ASSERT(ctx
, p
!= NULL
);
384 UCI_ASSERT(ctx
, section
&& uci_validate_name(section
));
386 UCI_ASSERT(ctx
, uci_validate_name(option
));
388 e
= uci_lookup_list(&p
->sections
, section
);
393 s
= uci_to_section(e
);
394 e
= uci_lookup_list(&s
->options
, option
);
403 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
407 int uci_del_element(struct uci_context
*ctx
, struct uci_element
*e
)
409 /* NB: UCI_INTERNAL use means without history tracking */
410 bool internal
= ctx
->internal
;
411 struct uci_package
*p
= NULL
;
412 struct uci_section
*s
= NULL
;
413 struct uci_option
*o
= NULL
;
414 struct uci_element
*i
, *tmp
;
418 UCI_ASSERT(ctx
, e
!= NULL
);
421 case UCI_TYPE_SECTION
:
422 s
= uci_to_section(e
);
423 uci_foreach_element_safe(&s
->options
, tmp
, i
) {
424 uci_del_element(ctx
, i
);
427 case UCI_TYPE_OPTION
:
428 o
= uci_to_option(e
);
434 UCI_THROW(ctx
, UCI_ERR_INVAL
);
439 if (!internal
&& p
->has_history
)
440 uci_add_history(ctx
, &p
->history
, UCI_CMD_REMOVE
, s
->e
.name
, option
, NULL
);
443 case UCI_TYPE_SECTION
:
446 case UCI_TYPE_OPTION
:
456 int uci_add_element_list(struct uci_context
*ctx
, struct uci_option
*o
, const char *value
)
458 struct uci_element
*e
;
459 struct uci_package
*p
;
460 struct uci_section
*s
;
461 bool internal
= ctx
->internal
;
464 UCI_ASSERT(ctx
, (o
!= NULL
) && (o
->type
== UCI_TYPE_LIST
) && uci_validate_text(value
));
469 if (!internal
&& p
->has_history
)
470 uci_add_history(ctx
, &p
->history
, UCI_CMD_LIST_ADD
, s
->e
.name
, o
->e
.name
, value
);
472 e
= uci_alloc_generic(ctx
, UCI_TYPE_ITEM
, value
, sizeof(struct uci_option
));
473 uci_list_add(&o
->v
.list
, &e
->list
);
478 int uci_set_element_value(struct uci_context
*ctx
, struct uci_element
**element
, const char *value
)
480 /* NB: UCI_INTERNAL use means without history tracking */
481 bool internal
= ctx
->internal
;
482 struct uci_list
*list
;
483 struct uci_element
*e
;
484 struct uci_package
*p
;
485 struct uci_section
*s
;
486 struct uci_option
*o
;
493 UCI_ASSERT(ctx
, (element
!= NULL
) && (*element
!= NULL
));
495 /* what the 'value' of an element means depends on the type
496 * for a section, the 'value' means its type
497 * for an option, the 'value' means its value string
498 * when changing the value, shrink the element to its actual size
499 * (it may have been allocated with a bigger size, to include
501 * then duplicate the string passed on the command line and
502 * insert it into the structure.
508 case UCI_TYPE_SECTION
:
509 UCI_ASSERT(ctx
, uci_validate_str(value
, false));
510 size
= sizeof(struct uci_section
);
511 s
= uci_to_section(e
);
514 /* matches the currently set value */
515 if (!strcmp(value
, s
->type
))
519 case UCI_TYPE_OPTION
:
520 UCI_ASSERT(ctx
, value
!= NULL
);
521 o
= uci_to_option(e
);
526 case UCI_TYPE_STRING
:
527 size
= sizeof(struct uci_option
);
528 /* matches the currently set value */
529 if (!strcmp(value
, o
->v
.string
))
533 /* default action for non-string datatypes is to delete
534 * the existing entry, then re-create it as a string */
540 UCI_THROW(ctx
, UCI_ERR_INVAL
);
544 if (!internal
&& p
->has_history
)
545 uci_add_history(ctx
, &p
->history
, UCI_CMD_CHANGE
, section
, option
, value
);
547 if ((e
->type
== UCI_TYPE_OPTION
) && (size
== 0)) {
548 o
= uci_alloc_option(s
, option
, value
);
549 UCI_INTERNAL(uci_del_element
, ctx
, e
);
554 uci_list_del(&e
->list
);
555 e
= uci_realloc(ctx
, e
, size
);
556 str
= uci_strdup(ctx
, value
);
557 uci_list_insert(list
, &e
->list
);
561 case UCI_TYPE_SECTION
:
562 uci_to_section(e
)->type
= str
;
564 case UCI_TYPE_OPTION
:
565 uci_to_option(e
)->v
.string
= str
;
575 int uci_rename(struct uci_context
*ctx
, struct uci_package
*p
, char *section
, char *option
, char *name
)
577 /* NB: UCI_INTERNAL use means without history tracking */
578 bool internal
= ctx
->internal
;
579 struct uci_element
*e
;
583 /* NB: p, section, option validated by uci_lookup */
584 UCI_INTERNAL(uci_lookup
, ctx
, &e
, p
, section
, option
);
586 if (!internal
&& p
->has_history
)
587 uci_add_history(ctx
, &p
->history
, UCI_CMD_RENAME
, section
, option
, name
);
589 name
= uci_strdup(ctx
, name
);
597 int uci_add_section(struct uci_context
*ctx
, struct uci_package
*p
, const char *type
, struct uci_section
**res
)
599 bool internal
= ctx
->internal
;
600 struct uci_section
*s
;
603 UCI_ASSERT(ctx
, p
!= NULL
);
604 s
= uci_alloc_section(p
, type
, NULL
);
605 uci_fixup_section(ctx
, s
);
607 if (!internal
&& p
->has_history
)
608 uci_add_history(ctx
, &p
->history
, UCI_CMD_ADD
, s
->e
.name
, NULL
, type
);
613 int uci_delete(struct uci_context
*ctx
, struct uci_package
*p
, const char *section
, const char *option
)
615 /* NB: pass on internal flag to uci_del_element */
616 bool internal
= ctx
->internal
;
617 struct uci_element
*e
;
621 /* NB: p, section, option validated by uci_lookup */
622 UCI_INTERNAL(uci_lookup
, ctx
, &e
, p
, section
, option
);
624 ctx
->internal
= internal
;
625 return uci_del_element(ctx
, e
);
628 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
)
630 /* NB: UCI_INTERNAL use means without history tracking */
631 bool internal
= ctx
->internal
;
632 struct uci_element
*e
;
633 struct uci_section
*s
;
634 struct uci_option
*o
;
635 struct uci_option
*prev
= NULL
;
636 const char *value2
= NULL
;
639 UCI_ASSERT(ctx
, p
&& section
&& option
&& value
&& uci_validate_text(value
));
641 /* look up the section first */
642 UCI_INTERNAL(uci_lookup
, ctx
, &e
, p
, section
, NULL
);
643 s
= uci_to_section(e
);
645 e
= uci_lookup_list(&s
->options
, option
);
647 o
= uci_to_option(e
);
649 case UCI_TYPE_STRING
:
650 /* we already have a string value, let's convert that to a list */
659 ctx
->internal
= internal
;
660 return uci_add_element_list(ctx
, o
, value
);
662 UCI_THROW(ctx
, UCI_ERR_INVAL
);
667 o
= uci_alloc_list(s
, option
);
671 UCI_INTERNAL(uci_add_element_list
, ctx
, o
, value
);
672 uci_free_option(prev
);
676 ctx
->internal
= internal
;
677 return uci_add_element_list(ctx
, o
, value
);
680 int uci_set(struct uci_context
*ctx
, struct uci_package
*p
, const char *section
, const char *option
, const char *value
, struct uci_element
**result
)
682 /* NB: UCI_INTERNAL use means without history tracking */
683 bool internal
= ctx
->internal
;
684 struct uci_element
*e
= NULL
;
685 struct uci_section
*s
= NULL
;
686 struct uci_option
*o
= NULL
;
689 UCI_ASSERT(ctx
, p
!= NULL
);
690 UCI_ASSERT(ctx
, uci_validate_name(section
));
692 UCI_ASSERT(ctx
, uci_validate_name(option
));
693 UCI_ASSERT(ctx
, value
!= NULL
);
695 UCI_ASSERT(ctx
, uci_validate_str(value
, false));
699 * look up the package, section and option (if set)
700 * if the section/option is to be modified and it is not found
701 * create a new element in the appropriate list
703 e
= uci_lookup_list(&p
->sections
, section
);
707 s
= uci_to_section(e
);
708 if (ctx
->pctx
&& ctx
->pctx
->merge
)
709 ctx
->pctx
->section
= s
;
712 e
= uci_lookup_list(&s
->options
, option
);
715 o
= uci_to_option(e
);
719 * no unknown element was supplied, assume that we can just update
731 ctx
->internal
= internal
;
732 return uci_set_element_value(ctx
, result
, value
);
736 * the entry that we need to update was not found,
737 * check if the search failed prematurely.
738 * this can happen if the package was not found, or if
739 * an option was supplied, but the section wasn't found
741 if (!p
|| (!s
&& option
))
742 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
744 /* now add the missing entry */
745 if (!internal
&& p
->has_history
)
746 uci_add_history(ctx
, &p
->history
, UCI_CMD_CHANGE
, section
, option
, value
);
748 o
= uci_alloc_option(s
, option
, value
);
752 s
= uci_alloc_section(p
, value
, section
);
755 if (ctx
->pctx
&& ctx
->pctx
->merge
)
756 ctx
->pctx
->section
= s
;
762 int uci_unload(struct uci_context
*ctx
, struct uci_package
*p
)
765 UCI_ASSERT(ctx
, p
!= NULL
);
767 uci_free_package(&p
);