e6d631c43bbafeb6272e6c0256978394191dd9a2
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 Lesser General Public License for more details.
15 static bool uci_list_set_pos(struct uci_list
*head
, struct uci_list
*ptr
, int pos
)
17 struct uci_list
*old_head
= ptr
->prev
;
18 struct uci_list
*new_head
= head
;
19 struct uci_element
*p
= NULL
;
22 uci_foreach_element(head
, p
) {
28 uci_list_add(new_head
->next
, ptr
);
30 return (old_head
!= new_head
);
33 static inline void uci_list_fixup(struct uci_list
*ptr
)
35 ptr
->prev
->next
= ptr
;
36 ptr
->next
->prev
= ptr
;
40 * uci_alloc_generic allocates a new uci_element with payload
41 * payload is appended to the struct to save memory and reduce fragmentation
43 __private
struct uci_element
*
44 uci_alloc_generic(struct uci_context
*ctx
, int type
, const char *name
, int size
)
46 struct uci_element
*e
;
50 ptr
= uci_malloc(ctx
, datalen
);
51 e
= (struct uci_element
*) ptr
;
54 UCI_TRAP_SAVE(ctx
, error
);
55 e
->name
= uci_strdup(ctx
, name
);
56 UCI_TRAP_RESTORE(ctx
);
58 uci_list_init(&e
->list
);
63 UCI_THROW(ctx
, ctx
->err
);
70 uci_free_element(struct uci_element
*e
)
73 if (!uci_list_empty(&e
->list
))
74 uci_list_del(&e
->list
);
78 static struct uci_option
*
79 uci_alloc_option(struct uci_section
*s
, const char *name
, const char *value
, struct uci_list
*after
)
81 struct uci_package
*p
= s
->package
;
82 struct uci_context
*ctx
= p
->ctx
;
85 o
= uci_alloc_element(ctx
, option
, name
, strlen(value
) + 1);
86 o
->type
= UCI_TYPE_STRING
;
87 o
->v
.string
= uci_dataptr(o
);
89 strcpy(o
->v
.string
, value
);
90 uci_list_insert(after
? after
: s
->options
.prev
, &o
->e
.list
);
96 uci_free_option(struct uci_option
*o
)
98 struct uci_element
*e
, *tmp
;
101 case UCI_TYPE_STRING
:
102 if ((o
->v
.string
!= uci_dataptr(o
)) &&
103 (o
->v
.string
!= NULL
))
107 uci_foreach_element_safe(&o
->v
.list
, tmp
, e
) {
114 uci_free_element(&o
->e
);
117 static struct uci_option
*
118 uci_alloc_list(struct uci_section
*s
, const char *name
, struct uci_list
*after
)
120 struct uci_package
*p
= s
->package
;
121 struct uci_context
*ctx
= p
->ctx
;
122 struct uci_option
*o
;
124 o
= uci_alloc_element(ctx
, option
, name
, 0);
125 o
->type
= UCI_TYPE_LIST
;
127 uci_list_init(&o
->v
.list
);
128 uci_list_insert(after
? after
: s
->options
.prev
, &o
->e
.list
);
133 /* Based on an efficient hash function published by D. J. Bernstein */
134 static unsigned int djbhash(unsigned int hash
, char *str
)
136 int len
= strlen(str
);
143 for(i
= 0; i
< len
; i
++) {
144 hash
= ((hash
<< 5) + hash
) + str
[i
];
146 return (hash
& 0x7FFFFFFF);
149 /* fix up an unnamed section, e.g. after adding options to it */
150 static void uci_fixup_section(struct uci_context
*ctx
, struct uci_section
*s
)
152 unsigned int hash
= ~0U;
153 struct uci_element
*e
;
160 * Generate a name for unnamed sections. This is used as reference
161 * when locating or updating the section from apps/scripts.
162 * To make multiple concurrent versions somewhat safe for updating,
163 * the name is generated from a hash of its type and name/value
164 * pairs of its option, and it is prefixed by a counter value.
165 * If the order of the unnamed sections changes for some reason,
166 * updates to them will be rejected.
168 hash
= djbhash(hash
, s
->type
);
169 uci_foreach_element(&s
->options
, e
) {
170 struct uci_option
*o
;
171 hash
= djbhash(hash
, e
->name
);
172 o
= uci_to_option(e
);
174 case UCI_TYPE_STRING
:
175 hash
= djbhash(hash
, o
->v
.string
);
181 sprintf(buf
, "cfg%02x%04x", s
->package
->n_section
, hash
% (1 << 16));
182 s
->e
.name
= uci_strdup(ctx
, buf
);
185 /* fix up option list HEAD pointers and pointer to section in options */
186 static void uci_section_fixup_options(struct uci_section
*s
, bool no_options
)
188 struct uci_element
*e
;
192 * enforce empty list pointer state (s->next == s) when original
193 * section had no options in the first place
195 uci_list_init(&s
->options
);
199 /* fix pointers to HEAD at end/beginning of list */
200 uci_list_fixup(&s
->options
);
202 /* fix back pointer to section in options */
203 uci_foreach_element(&s
->options
, e
) {
204 struct uci_option
*o
;
206 o
= uci_to_option(e
);
211 static struct uci_section
*
212 uci_alloc_section(struct uci_package
*p
, const char *type
, const char *name
)
214 struct uci_context
*ctx
= p
->ctx
;
215 struct uci_section
*s
;
217 if (name
&& !name
[0])
220 s
= uci_alloc_element(ctx
, section
, name
, strlen(type
) + 1);
221 uci_list_init(&s
->options
);
222 s
->type
= uci_dataptr(s
);
224 strcpy(s
->type
, type
);
229 uci_list_add(&p
->sections
, &s
->e
.list
);
235 uci_free_section(struct uci_section
*s
)
237 struct uci_element
*o
, *tmp
;
239 uci_foreach_element_safe(&s
->options
, tmp
, o
) {
240 uci_free_option(uci_to_option(o
));
242 if ((s
->type
!= uci_dataptr(s
)) &&
245 uci_free_element(&s
->e
);
248 __private
struct uci_package
*
249 uci_alloc_package(struct uci_context
*ctx
, const char *name
)
251 struct uci_package
*p
;
253 p
= uci_alloc_element(ctx
, package
, name
, 0);
255 uci_list_init(&p
->sections
);
256 uci_list_init(&p
->delta
);
257 uci_list_init(&p
->saved_delta
);
262 uci_free_package(struct uci_package
**package
)
264 struct uci_element
*e
, *tmp
;
265 struct uci_package
*p
= *package
;
271 uci_foreach_element_safe(&p
->sections
, tmp
, e
) {
272 uci_free_section(uci_to_section(e
));
274 uci_foreach_element_safe(&p
->delta
, tmp
, e
) {
275 uci_free_delta(uci_to_delta(e
));
277 uci_foreach_element_safe(&p
->saved_delta
, tmp
, e
) {
278 uci_free_delta(uci_to_delta(e
));
280 uci_free_element(&p
->e
);
285 uci_free_any(struct uci_element
**e
)
288 case UCI_TYPE_SECTION
:
289 uci_free_section(uci_to_section(*e
));
291 case UCI_TYPE_OPTION
:
292 uci_free_option(uci_to_option(*e
));
300 __private
struct uci_element
*
301 uci_lookup_list(struct uci_list
*list
, const char *name
)
303 struct uci_element
*e
;
305 uci_foreach_element(list
, e
) {
306 if (!strcmp(e
->name
, name
))
312 static struct uci_element
*
313 uci_lookup_ext_section(struct uci_context
*ctx
, struct uci_ptr
*ptr
)
315 char *idxstr
, *t
, *section
, *name
;
316 struct uci_element
*e
= NULL
;
317 struct uci_section
*s
;
320 section
= uci_strdup(ctx
, ptr
->section
);
321 name
= idxstr
= section
+ 1;
323 if (section
[0] != '@')
326 /* parse the section index part */
327 idxstr
= strchr(idxstr
, '[');
333 t
= strchr(idxstr
, ']');
341 idx
= strtol(idxstr
, &t
, 10);
347 else if (!uci_validate_type(name
))
350 /* if the given index is negative, it specifies the section number from
351 * the end of the list */
354 uci_foreach_element(&ptr
->p
->sections
, e
) {
355 s
= uci_to_section(e
);
356 if (name
&& (strcmp(s
->type
, name
) != 0))
365 uci_foreach_element(&ptr
->p
->sections
, e
) {
366 s
= uci_to_section(e
);
367 if (name
&& (strcmp(s
->type
, name
) != 0))
379 memset(ptr
, 0, sizeof(struct uci_ptr
));
380 UCI_THROW(ctx
, UCI_ERR_INVAL
);
384 ptr
->section
= e
->name
;
389 uci_lookup_next(struct uci_context
*ctx
, struct uci_element
**e
, struct uci_list
*list
, const char *name
)
393 *e
= uci_lookup_list(list
, name
);
395 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
401 uci_lookup_ptr(struct uci_context
*ctx
, struct uci_ptr
*ptr
, char *str
, bool extended
)
403 struct uci_element
*e
;
406 UCI_ASSERT(ctx
, ptr
!= NULL
);
409 UCI_INTERNAL(uci_parse_ptr
, ctx
, ptr
, str
);
411 ptr
->flags
|= UCI_LOOKUP_DONE
;
413 /* look up the package first */
417 e
= uci_lookup_list(&ctx
->root
, ptr
->package
);
420 UCI_INTERNAL(uci_load
, ctx
, ptr
->package
, &ptr
->p
);
423 ptr
->last
= &ptr
->p
->e
;
425 ptr
->p
= uci_to_package(e
);
429 if (!ptr
->section
&& !ptr
->s
)
432 /* if the section name validates as a regular name, pass through
433 * to the regular uci_lookup function call */
436 } else if (ptr
->flags
& UCI_LOOKUP_EXTENDED
) {
438 e
= uci_lookup_ext_section(ctx
, ptr
);
440 UCI_THROW(ctx
, UCI_ERR_INVAL
);
442 e
= uci_lookup_list(&ptr
->p
->sections
, ptr
->section
);
449 ptr
->s
= uci_to_section(e
);
452 e
= uci_lookup_list(&ptr
->s
->options
, ptr
->option
);
456 ptr
->o
= uci_to_option(e
);
461 ptr
->flags
|= UCI_LOOKUP_COMPLETE
;
466 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
467 /* not a chance here */
468 return UCI_ERR_NOTFOUND
;
471 __private
struct uci_element
*
472 uci_expand_ptr(struct uci_context
*ctx
, struct uci_ptr
*ptr
, bool complete
)
474 UCI_ASSERT(ctx
, ptr
!= NULL
);
476 if (!(ptr
->flags
& UCI_LOOKUP_DONE
))
477 UCI_INTERNAL(uci_lookup_ptr
, ctx
, ptr
, NULL
, 1);
478 if (complete
&& !(ptr
->flags
& UCI_LOOKUP_COMPLETE
))
479 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
480 UCI_ASSERT(ctx
, ptr
->p
!= NULL
);
482 /* fill in missing string info */
483 if (ptr
->p
&& !ptr
->package
)
484 ptr
->package
= ptr
->p
->e
.name
;
485 if (ptr
->s
&& !ptr
->section
)
486 ptr
->section
= ptr
->s
->e
.name
;
487 if (ptr
->o
&& !ptr
->option
)
488 ptr
->option
= ptr
->o
->e
.name
;
500 int uci_rename(struct uci_context
*ctx
, struct uci_ptr
*ptr
)
502 /* NB: UCI_INTERNAL use means without delta tracking */
503 bool internal
= ctx
&& ctx
->internal
;
504 struct uci_element
*e
;
505 struct uci_package
*p
;
510 e
= uci_expand_ptr(ctx
, ptr
, true);
513 UCI_ASSERT(ctx
, ptr
->s
);
514 UCI_ASSERT(ctx
, ptr
->value
);
516 if (!internal
&& p
->has_delta
)
517 uci_add_delta(ctx
, &p
->delta
, UCI_CMD_RENAME
, ptr
->section
, ptr
->option
, ptr
->value
);
519 n
= uci_strdup(ctx
, ptr
->value
);
523 if (e
->type
== UCI_TYPE_SECTION
)
524 uci_to_section(e
)->anonymous
= false;
529 int uci_reorder_section(struct uci_context
*ctx
, struct uci_section
*s
, int pos
)
531 struct uci_package
*p
= s
->package
;
532 bool internal
= ctx
&& ctx
->internal
;
533 bool changed
= false;
538 changed
= uci_list_set_pos(&s
->package
->sections
, &s
->e
.list
, pos
);
539 if (!internal
&& p
->has_delta
&& changed
) {
540 sprintf(order
, "%d", pos
);
541 uci_add_delta(ctx
, &p
->delta
, UCI_CMD_REORDER
, s
->e
.name
, NULL
, order
);
547 int uci_add_section(struct uci_context
*ctx
, struct uci_package
*p
, const char *type
, struct uci_section
**res
)
549 bool internal
= ctx
&& ctx
->internal
;
550 struct uci_section
*s
;
553 UCI_ASSERT(ctx
, p
!= NULL
);
554 s
= uci_alloc_section(p
, type
, NULL
);
555 if (s
&& s
->anonymous
)
556 uci_fixup_section(ctx
, s
);
558 if (!internal
&& p
->has_delta
)
559 uci_add_delta(ctx
, &p
->delta
, UCI_CMD_ADD
, s
->e
.name
, NULL
, type
);
564 int uci_delete(struct uci_context
*ctx
, struct uci_ptr
*ptr
)
566 /* NB: pass on internal flag to uci_del_element */
567 bool internal
= ctx
&& ctx
->internal
;
568 struct uci_package
*p
;
569 struct uci_element
*e1
, *e2
, *tmp
;
574 e1
= uci_expand_ptr(ctx
, ptr
, true);
577 UCI_ASSERT(ctx
, ptr
->s
);
579 if (ptr
->o
&& ptr
->o
->type
== UCI_TYPE_LIST
&& ptr
->value
&& *ptr
->value
) {
580 if (!sscanf(ptr
->value
, "%d", &index
))
583 uci_foreach_element_safe(&ptr
->o
->v
.list
, tmp
, e2
) {
585 if (!internal
&& p
->has_delta
)
586 uci_add_delta(ctx
, &p
->delta
, UCI_CMD_REMOVE
, ptr
->section
, ptr
->option
, ptr
->value
);
587 uci_free_option(uci_to_option(e2
));
596 if (!internal
&& p
->has_delta
)
597 uci_add_delta(ctx
, &p
->delta
, UCI_CMD_REMOVE
, ptr
->section
, ptr
->option
, NULL
);
603 else if (ptr
->section
)
609 int uci_add_list(struct uci_context
*ctx
, struct uci_ptr
*ptr
)
611 /* NB: UCI_INTERNAL use means without delta tracking */
612 bool internal
= ctx
&& ctx
->internal
;
613 struct uci_element
*volatile e1
= NULL
, *volatile e2
= NULL
;
617 uci_expand_ptr(ctx
, ptr
, false);
618 UCI_ASSERT(ctx
, ptr
->s
);
619 UCI_ASSERT(ctx
, ptr
->value
);
621 if (ptr
->o
&& ptr
->o
->type
!= UCI_TYPE_LIST
&& ptr
->o
->type
!= UCI_TYPE_STRING
) {
622 UCI_THROW(ctx
, UCI_ERR_INVAL
);
625 /* create new item */
626 e1
= uci_alloc_generic(ctx
, UCI_TYPE_ITEM
, ptr
->value
, sizeof(struct uci_option
));
629 /* create new list */
630 UCI_TRAP_SAVE(ctx
, error
);
631 ptr
->o
= uci_alloc_list(ptr
->s
, ptr
->option
, NULL
);
632 UCI_TRAP_RESTORE(ctx
);
633 ptr
->last
= &ptr
->o
->e
;
634 } else if (ptr
->o
->type
== UCI_TYPE_STRING
) {
635 /* create new list and add old string value as item to list */
636 struct uci_option
*old
= ptr
->o
;
637 UCI_TRAP_SAVE(ctx
, error
);
638 e2
= uci_alloc_generic(ctx
, UCI_TYPE_ITEM
, old
->v
.string
, sizeof(struct uci_option
));
639 ptr
->o
= uci_alloc_list(ptr
->s
, ptr
->option
, &old
->e
.list
);
640 UCI_TRAP_RESTORE(ctx
);
641 uci_list_add(&ptr
->o
->v
.list
, &e2
->list
);
643 /* remove old option */
644 if (ptr
->option
== old
->e
.name
)
645 ptr
->option
= ptr
->o
->e
.name
;
646 uci_free_option(old
);
647 ptr
->last
= &ptr
->o
->e
;
650 /* add new item to list */
651 uci_list_add(&ptr
->o
->v
.list
, &e1
->list
);
653 if (!internal
&& ptr
->p
->has_delta
)
654 uci_add_delta(ctx
, &ptr
->p
->delta
, UCI_CMD_LIST_ADD
, ptr
->section
, ptr
->option
, ptr
->value
);
659 uci_free_element(e1
);
661 uci_free_element(e2
);
662 UCI_THROW(ctx
, ctx
->err
);
665 int uci_del_list(struct uci_context
*ctx
, struct uci_ptr
*ptr
)
667 /* NB: pass on internal flag to uci_del_element */
668 bool internal
= ctx
&& ctx
->internal
;
669 struct uci_element
*e
, *tmp
;
670 struct uci_package
*p
;
674 uci_expand_ptr(ctx
, ptr
, false);
675 UCI_ASSERT(ctx
, ptr
->s
);
676 UCI_ASSERT(ctx
, ptr
->value
);
678 if (!(ptr
->o
&& ptr
->option
))
681 if ((ptr
->o
->type
!= UCI_TYPE_LIST
))
685 if (!internal
&& p
->has_delta
)
686 uci_add_delta(ctx
, &p
->delta
, UCI_CMD_LIST_DEL
, ptr
->section
, ptr
->option
, ptr
->value
);
688 uci_foreach_element_safe(&ptr
->o
->v
.list
, tmp
, e
) {
689 if (!strcmp(ptr
->value
, uci_to_option(e
)->e
.name
)) {
690 uci_free_option(uci_to_option(e
));
697 int uci_set(struct uci_context
*ctx
, struct uci_ptr
*ptr
)
699 /* NB: UCI_INTERNAL use means without delta tracking */
700 bool internal
= ctx
&& ctx
->internal
;
703 uci_expand_ptr(ctx
, ptr
, false);
704 UCI_ASSERT(ctx
, ptr
->value
);
705 UCI_ASSERT(ctx
, ptr
->s
|| (!ptr
->option
&& ptr
->section
));
706 if (!ptr
->option
&& ptr
->value
[0]) {
707 UCI_ASSERT(ctx
, uci_validate_type(ptr
->value
));
710 if (!ptr
->o
&& ptr
->s
&& ptr
->option
) {
711 struct uci_element
*e
;
712 e
= uci_lookup_list(&ptr
->s
->options
, ptr
->option
);
714 ptr
->o
= uci_to_option(e
);
716 if (!ptr
->value
[0]) {
717 /* if setting a nonexistant option/section to a nonexistant value,
718 * exit without errors */
719 if (!(ptr
->flags
& UCI_LOOKUP_COMPLETE
))
722 return uci_delete(ctx
, ptr
);
723 } else if (!ptr
->o
&& ptr
->option
) { /* new option */
724 ptr
->o
= uci_alloc_option(ptr
->s
, ptr
->option
, ptr
->value
, NULL
);
725 ptr
->last
= &ptr
->o
->e
;
726 } else if (!ptr
->s
&& ptr
->section
) { /* new section */
727 ptr
->s
= uci_alloc_section(ptr
->p
, ptr
->value
, ptr
->section
);
728 ptr
->last
= &ptr
->s
->e
;
729 } else if (ptr
->o
&& ptr
->option
) { /* update option */
730 if (ptr
->o
->type
== UCI_TYPE_STRING
&& !strcmp(ptr
->o
->v
.string
, ptr
->value
))
733 if (ptr
->o
->type
== UCI_TYPE_STRING
&& strlen(ptr
->o
->v
.string
) == strlen(ptr
->value
)) {
734 strcpy(ptr
->o
->v
.string
, ptr
->value
);
736 struct uci_option
*old
= ptr
->o
;
737 ptr
->o
= uci_alloc_option(ptr
->s
, ptr
->option
, ptr
->value
, &old
->e
.list
);
738 if (ptr
->option
== old
->e
.name
)
739 ptr
->option
= ptr
->o
->e
.name
;
740 uci_free_option(old
);
741 ptr
->last
= &ptr
->o
->e
;
743 } else if (ptr
->s
&& ptr
->section
) { /* update section */
744 char *s
= uci_strdup(ctx
, ptr
->value
);
746 if (ptr
->s
->type
== uci_dataptr(ptr
->s
)) {
747 /* drop the in-section storage of type name */
750 no_options
= uci_list_empty(&ptr
->s
->options
);
752 ptr
->last
= uci_realloc(ctx
, ptr
->s
, sizeof(struct uci_section
));
753 ptr
->s
= uci_to_section(ptr
->last
);
754 uci_list_fixup(&ptr
->s
->e
.list
);
755 uci_section_fixup_options(ptr
->s
, no_options
);
761 UCI_THROW(ctx
, UCI_ERR_INVAL
);
764 if (!internal
&& ptr
->p
->has_delta
)
765 uci_add_delta(ctx
, &ptr
->p
->delta
, UCI_CMD_CHANGE
, ptr
->section
, ptr
->option
, ptr
->value
);
770 int uci_unload(struct uci_context
*ctx
, struct uci_package
*p
)
773 UCI_ASSERT(ctx
, p
!= NULL
);
775 uci_free_package(&p
);