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_set_pos(struct uci_list
*head
, struct uci_list
*ptr
, int pos
)
53 struct uci_list
*new_head
= head
;
54 struct uci_element
*p
= NULL
;
57 uci_foreach_element(head
, p
) {
62 uci_list_add(new_head
, ptr
);
65 static inline void uci_list_fixup(struct uci_list
*ptr
)
67 ptr
->prev
->next
= ptr
;
68 ptr
->next
->prev
= ptr
;
72 * uci_alloc_generic allocates a new uci_element with payload
73 * payload is appended to the struct to save memory and reduce fragmentation
75 static struct uci_element
*
76 uci_alloc_generic(struct uci_context
*ctx
, int type
, const char *name
, int size
)
78 struct uci_element
*e
;
82 ptr
= uci_malloc(ctx
, datalen
);
83 e
= (struct uci_element
*) ptr
;
86 UCI_TRAP_SAVE(ctx
, error
);
87 e
->name
= uci_strdup(ctx
, name
);
88 UCI_TRAP_RESTORE(ctx
);
90 uci_list_init(&e
->list
);
95 UCI_THROW(ctx
, ctx
->err
);
102 uci_free_element(struct uci_element
*e
)
106 if (!uci_list_empty(&e
->list
))
107 uci_list_del(&e
->list
);
111 static struct uci_option
*
112 uci_alloc_option(struct uci_section
*s
, const char *name
, const char *value
)
114 struct uci_package
*p
= s
->package
;
115 struct uci_context
*ctx
= p
->ctx
;
116 struct uci_option
*o
;
118 o
= uci_alloc_element(ctx
, option
, name
, strlen(value
) + 1);
119 o
->type
= UCI_TYPE_STRING
;
120 o
->v
.string
= uci_dataptr(o
);
122 strcpy(o
->v
.string
, value
);
123 uci_list_add(&s
->options
, &o
->e
.list
);
129 uci_free_option(struct uci_option
*o
)
131 struct uci_element
*e
, *tmp
;
134 case UCI_TYPE_STRING
:
135 if ((o
->v
.string
!= uci_dataptr(o
)) &&
136 (o
->v
.string
!= NULL
))
140 uci_foreach_element_safe(&o
->v
.list
, tmp
, e
) {
147 uci_free_element(&o
->e
);
150 static struct uci_option
*
151 uci_alloc_list(struct uci_section
*s
, const char *name
)
153 struct uci_package
*p
= s
->package
;
154 struct uci_context
*ctx
= p
->ctx
;
155 struct uci_option
*o
;
157 o
= uci_alloc_element(ctx
, option
, name
, 0);
158 o
->type
= UCI_TYPE_LIST
;
160 uci_list_init(&o
->v
.list
);
161 uci_list_add(&s
->options
, &o
->e
.list
);
166 /* fix up an unnamed section, e.g. after adding options to it */
167 static void uci_fixup_section(struct uci_context
*ctx
, struct uci_section
*s
)
169 unsigned int hash
= ~0;
170 struct uci_element
*e
;
177 * Generate a name for unnamed sections. This is used as reference
178 * when locating or updating the section from apps/scripts.
179 * To make multiple concurrent versions somewhat safe for updating,
180 * the name is generated from a hash of its type and name/value
181 * pairs of its option, and it is prefixed by a counter value.
182 * If the order of the unnamed sections changes for some reason,
183 * updates to them will be rejected.
185 hash
= djbhash(hash
, s
->type
);
186 uci_foreach_element(&s
->options
, e
) {
187 struct uci_option
*o
;
188 hash
= djbhash(hash
, e
->name
);
189 o
= uci_to_option(e
);
191 case UCI_TYPE_STRING
:
192 hash
= djbhash(hash
, o
->v
.string
);
198 sprintf(buf
, "cfg%02x%04x", ++s
->package
->n_section
, hash
% (1 << 16));
199 s
->e
.name
= uci_strdup(ctx
, buf
);
202 static struct uci_section
*
203 uci_alloc_section(struct uci_package
*p
, const char *type
, const char *name
)
205 struct uci_context
*ctx
= p
->ctx
;
206 struct uci_section
*s
;
208 if (name
&& !name
[0])
211 s
= uci_alloc_element(ctx
, section
, name
, strlen(type
) + 1);
212 uci_list_init(&s
->options
);
213 s
->type
= uci_dataptr(s
);
215 strcpy(s
->type
, type
);
220 uci_list_add(&p
->sections
, &s
->e
.list
);
226 uci_free_section(struct uci_section
*s
)
228 struct uci_element
*o
, *tmp
;
230 uci_foreach_element_safe(&s
->options
, tmp
, o
) {
231 uci_free_option(uci_to_option(o
));
233 if ((s
->type
!= uci_dataptr(s
)) &&
236 uci_free_element(&s
->e
);
239 __plugin
struct uci_package
*
240 uci_alloc_package(struct uci_context
*ctx
, const char *name
)
242 struct uci_package
*p
;
244 p
= uci_alloc_element(ctx
, package
, name
, 0);
246 uci_list_init(&p
->sections
);
247 uci_list_init(&p
->history
);
248 uci_list_init(&p
->saved_history
);
253 uci_free_package(struct uci_package
**package
)
255 struct uci_element
*e
, *tmp
;
256 struct uci_package
*p
= *package
;
263 uci_foreach_element_safe(&p
->sections
, tmp
, e
) {
264 uci_free_section(uci_to_section(e
));
266 uci_foreach_element_safe(&p
->history
, tmp
, e
) {
267 uci_free_history(uci_to_history(e
));
269 uci_foreach_element_safe(&p
->saved_history
, tmp
, e
) {
270 uci_free_history(uci_to_history(e
));
272 uci_free_element(&p
->e
);
277 uci_free_any(struct uci_element
**e
)
280 case UCI_TYPE_SECTION
:
281 uci_free_section(uci_to_section(*e
));
283 case UCI_TYPE_OPTION
:
284 uci_free_option(uci_to_option(*e
));
292 static inline struct uci_element
*
293 uci_lookup_list(struct uci_list
*list
, const char *name
)
295 struct uci_element
*e
;
297 uci_foreach_element(list
, e
) {
298 if (!strcmp(e
->name
, name
))
304 static struct uci_element
*
305 uci_lookup_ext_section(struct uci_context
*ctx
, struct uci_ptr
*ptr
)
307 char *idxstr
, *t
, *section
, *name
;
308 struct uci_element
*e
= NULL
;
309 struct uci_section
*s
;
312 section
= uci_strdup(ctx
, ptr
->section
);
313 name
= idxstr
= section
+ 1;
315 if (section
[0] != '@')
318 /* parse the section index part */
319 idxstr
= strchr(idxstr
, '[');
325 t
= strchr(idxstr
, ']');
333 idx
= strtol(idxstr
, &t
, 10);
339 else if (!uci_validate_type(name
))
342 /* if the given index is negative, it specifies the section number from
343 * the end of the list */
346 uci_foreach_element(&ptr
->p
->sections
, e
) {
347 s
= uci_to_section(e
);
348 if (name
&& (strcmp(s
->type
, name
) != 0))
357 uci_foreach_element(&ptr
->p
->sections
, e
) {
358 s
= uci_to_section(e
);
359 if (name
&& (strcmp(s
->type
, name
) != 0))
371 memset(ptr
, 0, sizeof(struct uci_ptr
));
372 UCI_THROW(ctx
, UCI_ERR_INVAL
);
376 ptr
->section
= e
->name
;
381 uci_lookup_ptr(struct uci_context
*ctx
, struct uci_ptr
*ptr
, char *str
, bool extended
)
383 struct uci_element
*e
;
386 UCI_ASSERT(ctx
, ptr
!= NULL
);
389 UCI_INTERNAL(uci_parse_ptr
, ctx
, ptr
, str
);
391 ptr
->flags
|= UCI_LOOKUP_DONE
;
393 /* look up the package first */
394 e
= uci_lookup_list(&ctx
->root
, ptr
->package
);
396 UCI_INTERNAL(uci_load
, ctx
, ptr
->package
, &ptr
->p
);
399 ptr
->last
= &ptr
->p
->e
;
401 ptr
->p
= uci_to_package(e
);
408 /* if the section name validates as a regular name, pass through
409 * to the regular uci_lookup function call */
410 if (ptr
->flags
& UCI_LOOKUP_EXTENDED
)
411 e
= uci_lookup_ext_section(ctx
, ptr
);
413 e
= uci_lookup_list(&ptr
->p
->sections
, ptr
->section
);
419 ptr
->s
= uci_to_section(e
);
422 e
= uci_lookup_list(&ptr
->s
->options
, ptr
->option
);
426 ptr
->o
= uci_to_option(e
);
431 ptr
->flags
|= UCI_LOOKUP_COMPLETE
;
436 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
441 uci_fill_ptr(struct uci_context
*ctx
, struct uci_ptr
*ptr
, struct uci_element
*e
, bool complete
)
444 UCI_ASSERT(ctx
, ptr
!= NULL
);
445 UCI_ASSERT(ctx
, e
!= NULL
);
447 memset(ptr
, 0, sizeof(struct uci_ptr
));
449 case UCI_TYPE_OPTION
:
450 ptr
->o
= uci_to_option(e
);
452 case UCI_TYPE_SECTION
:
453 ptr
->s
= uci_to_section(e
);
455 case UCI_TYPE_PACKAGE
:
456 ptr
->p
= uci_to_package(e
);
459 UCI_THROW(ctx
, UCI_ERR_INVAL
);
463 ptr
->option
= ptr
->o
->e
.name
;
464 ptr
->s
= ptr
->o
->section
;
466 ptr
->section
= ptr
->s
->e
.name
;
467 ptr
->p
= ptr
->s
->package
;
469 ptr
->package
= ptr
->p
->e
.name
;
471 ptr
->flags
|= UCI_LOOKUP_DONE
;
473 ptr
->flags
|= UCI_LOOKUP_COMPLETE
;
478 static struct uci_element
*
479 expand_ptr(struct uci_context
*ctx
, struct uci_ptr
*ptr
, bool complete
)
481 UCI_ASSERT(ctx
, ptr
!= NULL
);
483 if (!(ptr
->flags
& UCI_LOOKUP_DONE
))
484 UCI_INTERNAL(uci_lookup_ptr
, ctx
, ptr
, NULL
, 1);
485 if (complete
&& !(ptr
->flags
& UCI_LOOKUP_COMPLETE
))
486 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
487 UCI_ASSERT(ctx
, ptr
->p
!= NULL
);
489 /* fill in missing string info */
490 if (ptr
->p
&& !ptr
->package
)
491 ptr
->package
= ptr
->p
->e
.name
;
492 if (ptr
->s
&& !ptr
->section
)
493 ptr
->section
= ptr
->s
->e
.name
;
494 if (ptr
->o
&& !ptr
->option
)
495 ptr
->option
= ptr
->o
->e
.name
;
507 static void uci_add_element_list(struct uci_context
*ctx
, struct uci_ptr
*ptr
, bool internal
)
509 struct uci_element
*e
;
510 struct uci_package
*p
;
513 if (!internal
&& p
->has_history
)
514 uci_add_history(ctx
, &p
->history
, UCI_CMD_LIST_ADD
, ptr
->section
, ptr
->option
, ptr
->value
);
516 e
= uci_alloc_generic(ctx
, UCI_TYPE_ITEM
, ptr
->value
, sizeof(struct uci_option
));
517 uci_list_add(&ptr
->o
->v
.list
, &e
->list
);
520 int uci_rename(struct uci_context
*ctx
, struct uci_ptr
*ptr
)
522 /* NB: UCI_INTERNAL use means without history tracking */
523 bool internal
= ctx
->internal
;
524 struct uci_element
*e
;
525 struct uci_package
*p
;
530 e
= expand_ptr(ctx
, ptr
, true);
533 UCI_ASSERT(ctx
, ptr
->s
);
534 UCI_ASSERT(ctx
, ptr
->value
);
536 if (!internal
&& p
->has_history
)
537 uci_add_history(ctx
, &p
->history
, UCI_CMD_RENAME
, ptr
->section
, ptr
->option
, ptr
->value
);
539 n
= uci_strdup(ctx
, ptr
->value
);
544 if (e
->type
== UCI_TYPE_SECTION
)
545 uci_to_section(e
)->anonymous
= false;
550 int uci_reorder_section(struct uci_context
*ctx
, struct uci_section
*s
, int pos
)
552 struct uci_package
*p
= s
->package
;
557 uci_list_set_pos(&s
->package
->sections
, &s
->e
.list
, pos
);
558 if (!ctx
->internal
&& p
->has_history
) {
559 sprintf(order
, "%d", pos
);
560 uci_add_history(ctx
, &p
->history
, UCI_CMD_REORDER
, s
->e
.name
, NULL
, order
);
566 int uci_add_section(struct uci_context
*ctx
, struct uci_package
*p
, const char *type
, struct uci_section
**res
)
568 bool internal
= ctx
->internal
;
569 struct uci_section
*s
;
572 UCI_ASSERT(ctx
, p
!= NULL
);
573 s
= uci_alloc_section(p
, type
, NULL
);
574 uci_fixup_section(ctx
, s
);
576 if (!internal
&& p
->has_history
)
577 uci_add_history(ctx
, &p
->history
, UCI_CMD_ADD
, s
->e
.name
, NULL
, type
);
582 int uci_delete(struct uci_context
*ctx
, struct uci_ptr
*ptr
)
584 /* NB: pass on internal flag to uci_del_element */
585 bool internal
= ctx
->internal
;
586 struct uci_package
*p
;
587 struct uci_element
*e
;
591 e
= expand_ptr(ctx
, ptr
, true);
594 UCI_ASSERT(ctx
, ptr
->s
);
596 if (!internal
&& p
->has_history
)
597 uci_add_history(ctx
, &p
->history
, 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 history tracking */
612 bool internal
= ctx
->internal
;
613 struct uci_option
*prev
= NULL
;
614 const char *value2
= NULL
;
618 expand_ptr(ctx
, ptr
, false);
619 UCI_ASSERT(ctx
, ptr
->s
);
620 UCI_ASSERT(ctx
, ptr
->value
);
623 switch (ptr
->o
->type
) {
624 case UCI_TYPE_STRING
:
625 /* we already have a string value, convert that to a list */
628 ptr
->value
= ptr
->o
->v
.string
;
631 uci_add_element_list(ctx
, ptr
, internal
);
634 UCI_THROW(ctx
, UCI_ERR_INVAL
);
639 ptr
->o
= uci_alloc_list(ptr
->s
, ptr
->option
);
641 uci_add_element_list(ctx
, ptr
, true);
642 uci_free_option(prev
);
645 uci_add_element_list(ctx
, ptr
, internal
);
650 int uci_set(struct uci_context
*ctx
, struct uci_ptr
*ptr
)
652 /* NB: UCI_INTERNAL use means without history tracking */
653 bool internal
= ctx
->internal
;
656 expand_ptr(ctx
, ptr
, false);
657 UCI_ASSERT(ctx
, ptr
->value
);
658 UCI_ASSERT(ctx
, ptr
->s
|| (!ptr
->option
&& ptr
->section
));
659 if (!ptr
->option
&& ptr
->value
[0]) {
660 UCI_ASSERT(ctx
, uci_validate_type(ptr
->value
));
663 if (!ptr
->o
&& ptr
->s
&& ptr
->option
) {
664 struct uci_element
*e
;
665 e
= uci_lookup_list(&ptr
->s
->options
, ptr
->option
);
667 ptr
->o
= uci_to_option(e
);
669 if (!ptr
->value
[0]) {
670 /* if setting a nonexistant option/section to a nonexistant value,
671 * exit without errors */
672 if (!(ptr
->flags
& UCI_LOOKUP_COMPLETE
))
675 return uci_delete(ctx
, ptr
);
676 } else if (!ptr
->o
&& ptr
->option
) { /* new option */
677 ptr
->o
= uci_alloc_option(ptr
->s
, ptr
->option
, ptr
->value
);
678 ptr
->last
= &ptr
->o
->e
;
679 } else if (!ptr
->s
&& ptr
->section
) { /* new section */
680 ptr
->s
= uci_alloc_section(ptr
->p
, ptr
->value
, ptr
->section
);
681 ptr
->last
= &ptr
->s
->e
;
682 } else if (ptr
->o
&& ptr
->option
) { /* update option */
683 if ((ptr
->o
->type
== UCI_TYPE_STRING
) &&
684 !strcmp(ptr
->o
->v
.string
, ptr
->value
))
686 uci_free_option(ptr
->o
);
687 ptr
->o
= uci_alloc_option(ptr
->s
, ptr
->option
, ptr
->value
);
688 ptr
->last
= &ptr
->o
->e
;
689 } else if (ptr
->s
&& ptr
->section
) { /* update section */
690 char *s
= uci_strdup(ctx
, ptr
->value
);
692 if (ptr
->s
->type
== uci_dataptr(ptr
->s
)) {
694 ptr
->last
= uci_realloc(ctx
, ptr
->s
, sizeof(struct uci_section
));
695 ptr
->s
= uci_to_section(ptr
->last
);
696 uci_list_fixup(&ptr
->s
->e
.list
);
702 UCI_THROW(ctx
, UCI_ERR_INVAL
);
705 if (!internal
&& ptr
->p
->has_history
)
706 uci_add_history(ctx
, &ptr
->p
->history
, UCI_CMD_CHANGE
, ptr
->section
, ptr
->option
, ptr
->value
);
711 int uci_unload(struct uci_context
*ctx
, struct uci_package
*p
)
714 UCI_ASSERT(ctx
, p
!= NULL
);
716 uci_free_package(&p
);