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 static void uci_list_set_pos(struct uci_list
*head
, struct uci_list
*ptr
, int pos
)
17 struct uci_list
*new_head
= head
;
18 struct uci_element
*p
= NULL
;
21 uci_foreach_element(head
, p
) {
27 uci_list_add(new_head
->next
, ptr
);
30 static inline void uci_list_fixup(struct uci_list
*ptr
)
32 ptr
->prev
->next
= ptr
;
33 ptr
->next
->prev
= ptr
;
37 * uci_alloc_generic allocates a new uci_element with payload
38 * payload is appended to the struct to save memory and reduce fragmentation
40 __private
struct uci_element
*
41 uci_alloc_generic(struct uci_context
*ctx
, int type
, const char *name
, int size
)
43 struct uci_element
*e
;
47 ptr
= uci_malloc(ctx
, datalen
);
48 e
= (struct uci_element
*) ptr
;
51 UCI_TRAP_SAVE(ctx
, error
);
52 e
->name
= uci_strdup(ctx
, name
);
53 UCI_TRAP_RESTORE(ctx
);
55 uci_list_init(&e
->list
);
60 UCI_THROW(ctx
, ctx
->err
);
67 uci_free_element(struct uci_element
*e
)
71 if (!uci_list_empty(&e
->list
))
72 uci_list_del(&e
->list
);
76 static struct uci_option
*
77 uci_alloc_option(struct uci_section
*s
, const char *name
, const char *value
)
79 struct uci_package
*p
= s
->package
;
80 struct uci_context
*ctx
= p
->ctx
;
83 o
= uci_alloc_element(ctx
, option
, name
, strlen(value
) + 1);
84 o
->type
= UCI_TYPE_STRING
;
85 o
->v
.string
= uci_dataptr(o
);
87 strcpy(o
->v
.string
, value
);
88 uci_list_add(&s
->options
, &o
->e
.list
);
94 uci_free_option(struct uci_option
*o
)
96 struct uci_element
*e
, *tmp
;
100 if ((o
->v
.string
!= uci_dataptr(o
)) &&
101 (o
->v
.string
!= NULL
))
105 uci_foreach_element_safe(&o
->v
.list
, tmp
, e
) {
112 uci_free_element(&o
->e
);
115 static struct uci_option
*
116 uci_alloc_list(struct uci_section
*s
, const char *name
)
118 struct uci_package
*p
= s
->package
;
119 struct uci_context
*ctx
= p
->ctx
;
120 struct uci_option
*o
;
122 o
= uci_alloc_element(ctx
, option
, name
, 0);
123 o
->type
= UCI_TYPE_LIST
;
125 uci_list_init(&o
->v
.list
);
126 uci_list_add(&s
->options
, &o
->e
.list
);
131 /* Based on an efficient hash function published by D. J. Bernstein */
132 static unsigned int djbhash(unsigned int hash
, char *str
)
134 int len
= strlen(str
);
141 for(i
= 0; i
< len
; i
++) {
142 hash
= ((hash
<< 5) + hash
) + str
[i
];
144 return (hash
& 0x7FFFFFFF);
147 /* fix up an unnamed section, e.g. after adding options to it */
148 __private
void uci_fixup_section(struct uci_context
*ctx
, struct uci_section
*s
)
150 unsigned int hash
= ~0;
151 struct uci_element
*e
;
158 * Generate a name for unnamed sections. This is used as reference
159 * when locating or updating the section from apps/scripts.
160 * To make multiple concurrent versions somewhat safe for updating,
161 * the name is generated from a hash of its type and name/value
162 * pairs of its option, and it is prefixed by a counter value.
163 * If the order of the unnamed sections changes for some reason,
164 * updates to them will be rejected.
166 hash
= djbhash(hash
, s
->type
);
167 uci_foreach_element(&s
->options
, e
) {
168 struct uci_option
*o
;
169 hash
= djbhash(hash
, e
->name
);
170 o
= uci_to_option(e
);
172 case UCI_TYPE_STRING
:
173 hash
= djbhash(hash
, o
->v
.string
);
179 sprintf(buf
, "cfg%02x%04x", ++s
->package
->n_section
, hash
% (1 << 16));
180 s
->e
.name
= uci_strdup(ctx
, buf
);
183 static struct uci_section
*
184 uci_alloc_section(struct uci_package
*p
, const char *type
, const char *name
)
186 struct uci_context
*ctx
= p
->ctx
;
187 struct uci_section
*s
;
189 if (name
&& !name
[0])
192 s
= uci_alloc_element(ctx
, section
, name
, strlen(type
) + 1);
193 uci_list_init(&s
->options
);
194 s
->type
= uci_dataptr(s
);
196 strcpy(s
->type
, type
);
201 uci_list_add(&p
->sections
, &s
->e
.list
);
207 uci_free_section(struct uci_section
*s
)
209 struct uci_element
*o
, *tmp
;
211 uci_foreach_element_safe(&s
->options
, tmp
, o
) {
212 uci_free_option(uci_to_option(o
));
214 if ((s
->type
!= uci_dataptr(s
)) &&
217 uci_free_element(&s
->e
);
220 __plugin
struct uci_package
*
221 uci_alloc_package(struct uci_context
*ctx
, const char *name
)
223 struct uci_package
*p
;
225 p
= uci_alloc_element(ctx
, package
, name
, 0);
227 uci_list_init(&p
->sections
);
228 uci_list_init(&p
->delta
);
229 uci_list_init(&p
->saved_delta
);
234 uci_free_package(struct uci_package
**package
)
236 struct uci_element
*e
, *tmp
;
237 struct uci_package
*p
= *package
;
244 uci_foreach_element_safe(&p
->sections
, tmp
, e
) {
245 uci_free_section(uci_to_section(e
));
247 uci_foreach_element_safe(&p
->delta
, tmp
, e
) {
248 uci_free_delta(uci_to_delta(e
));
250 uci_foreach_element_safe(&p
->saved_delta
, tmp
, e
) {
251 uci_free_delta(uci_to_delta(e
));
253 uci_free_element(&p
->e
);
258 uci_free_any(struct uci_element
**e
)
261 case UCI_TYPE_SECTION
:
262 uci_free_section(uci_to_section(*e
));
264 case UCI_TYPE_OPTION
:
265 uci_free_option(uci_to_option(*e
));
273 __private
struct uci_element
*
274 uci_lookup_list(struct uci_list
*list
, const char *name
)
276 struct uci_element
*e
;
278 uci_foreach_element(list
, e
) {
279 if (!strcmp(e
->name
, name
))
285 static struct uci_element
*
286 uci_lookup_ext_section(struct uci_context
*ctx
, struct uci_ptr
*ptr
)
288 char *idxstr
, *t
, *section
, *name
;
289 struct uci_element
*e
= NULL
;
290 struct uci_section
*s
;
293 section
= uci_strdup(ctx
, ptr
->section
);
294 name
= idxstr
= section
+ 1;
296 if (section
[0] != '@')
299 /* parse the section index part */
300 idxstr
= strchr(idxstr
, '[');
306 t
= strchr(idxstr
, ']');
314 idx
= strtol(idxstr
, &t
, 10);
320 else if (!uci_validate_type(name
))
323 /* if the given index is negative, it specifies the section number from
324 * the end of the list */
327 uci_foreach_element(&ptr
->p
->sections
, e
) {
328 s
= uci_to_section(e
);
329 if (name
&& (strcmp(s
->type
, name
) != 0))
338 uci_foreach_element(&ptr
->p
->sections
, e
) {
339 s
= uci_to_section(e
);
340 if (name
&& (strcmp(s
->type
, name
) != 0))
352 memset(ptr
, 0, sizeof(struct uci_ptr
));
353 UCI_THROW(ctx
, UCI_ERR_INVAL
);
357 ptr
->section
= e
->name
;
362 uci_lookup_next(struct uci_context
*ctx
, struct uci_element
**e
, struct uci_list
*list
, const char *name
)
366 *e
= uci_lookup_list(list
, name
);
368 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
374 uci_lookup_ptr(struct uci_context
*ctx
, struct uci_ptr
*ptr
, char *str
, bool extended
)
376 struct uci_element
*e
;
379 UCI_ASSERT(ctx
, ptr
!= NULL
);
382 UCI_INTERNAL(uci_parse_ptr
, ctx
, ptr
, str
);
384 ptr
->flags
|= UCI_LOOKUP_DONE
;
386 /* look up the package first */
390 e
= uci_lookup_list(&ctx
->root
, ptr
->package
);
393 UCI_INTERNAL(uci_load
, ctx
, ptr
->package
, &ptr
->p
);
396 ptr
->last
= &ptr
->p
->e
;
398 ptr
->p
= uci_to_package(e
);
402 if (!ptr
->section
&& !ptr
->s
)
405 /* if the section name validates as a regular name, pass through
406 * to the regular uci_lookup function call */
409 } else if (ptr
->flags
& UCI_LOOKUP_EXTENDED
) {
411 e
= uci_lookup_ext_section(ctx
, ptr
);
413 UCI_THROW(ctx
, UCI_ERR_INVAL
);
415 e
= uci_lookup_list(&ptr
->p
->sections
, ptr
->section
);
422 ptr
->s
= uci_to_section(e
);
425 e
= uci_lookup_list(&ptr
->s
->options
, ptr
->option
);
429 ptr
->o
= uci_to_option(e
);
434 ptr
->flags
|= UCI_LOOKUP_COMPLETE
;
439 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
443 __private
struct uci_element
*
444 uci_expand_ptr(struct uci_context
*ctx
, struct uci_ptr
*ptr
, bool complete
)
446 UCI_ASSERT(ctx
, ptr
!= NULL
);
448 if (!(ptr
->flags
& UCI_LOOKUP_DONE
))
449 UCI_INTERNAL(uci_lookup_ptr
, ctx
, ptr
, NULL
, 1);
450 if (complete
&& !(ptr
->flags
& UCI_LOOKUP_COMPLETE
))
451 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
452 UCI_ASSERT(ctx
, ptr
->p
!= NULL
);
454 /* fill in missing string info */
455 if (ptr
->p
&& !ptr
->package
)
456 ptr
->package
= ptr
->p
->e
.name
;
457 if (ptr
->s
&& !ptr
->section
)
458 ptr
->section
= ptr
->s
->e
.name
;
459 if (ptr
->o
&& !ptr
->option
)
460 ptr
->option
= ptr
->o
->e
.name
;
472 static void uci_add_element_list(struct uci_context
*ctx
, struct uci_ptr
*ptr
, bool internal
)
474 struct uci_element
*e
;
475 struct uci_package
*p
;
478 if (!internal
&& p
->has_delta
)
479 uci_add_delta(ctx
, &p
->delta
, UCI_CMD_LIST_ADD
, ptr
->section
, ptr
->option
, ptr
->value
);
481 e
= uci_alloc_generic(ctx
, UCI_TYPE_ITEM
, ptr
->value
, sizeof(struct uci_option
));
482 uci_list_add(&ptr
->o
->v
.list
, &e
->list
);
485 int uci_rename(struct uci_context
*ctx
, struct uci_ptr
*ptr
)
487 /* NB: UCI_INTERNAL use means without delta tracking */
488 bool internal
= ctx
->internal
;
489 struct uci_element
*e
;
490 struct uci_package
*p
;
495 e
= uci_expand_ptr(ctx
, ptr
, true);
498 UCI_ASSERT(ctx
, ptr
->s
);
499 UCI_ASSERT(ctx
, ptr
->value
);
501 if (!internal
&& p
->has_delta
)
502 uci_add_delta(ctx
, &p
->delta
, UCI_CMD_RENAME
, ptr
->section
, ptr
->option
, ptr
->value
);
504 n
= uci_strdup(ctx
, ptr
->value
);
509 if (e
->type
== UCI_TYPE_SECTION
)
510 uci_to_section(e
)->anonymous
= false;
515 int uci_reorder_section(struct uci_context
*ctx
, struct uci_section
*s
, int pos
)
517 struct uci_package
*p
= s
->package
;
522 uci_list_set_pos(&s
->package
->sections
, &s
->e
.list
, pos
);
523 if (!ctx
->internal
&& p
->has_delta
) {
524 sprintf(order
, "%d", pos
);
525 uci_add_delta(ctx
, &p
->delta
, UCI_CMD_REORDER
, s
->e
.name
, NULL
, order
);
531 int uci_add_section(struct uci_context
*ctx
, struct uci_package
*p
, const char *type
, struct uci_section
**res
)
533 bool internal
= ctx
->internal
;
534 struct uci_section
*s
;
537 UCI_ASSERT(ctx
, p
!= NULL
);
538 s
= uci_alloc_section(p
, type
, NULL
);
539 uci_fixup_section(ctx
, s
);
541 if (!internal
&& p
->has_delta
)
542 uci_add_delta(ctx
, &p
->delta
, UCI_CMD_ADD
, s
->e
.name
, NULL
, type
);
547 int uci_delete(struct uci_context
*ctx
, struct uci_ptr
*ptr
)
549 /* NB: pass on internal flag to uci_del_element */
550 bool internal
= ctx
->internal
;
551 struct uci_package
*p
;
552 struct uci_element
*e
;
556 e
= uci_expand_ptr(ctx
, ptr
, true);
559 UCI_ASSERT(ctx
, ptr
->s
);
561 if (!internal
&& p
->has_delta
)
562 uci_add_delta(ctx
, &p
->delta
, UCI_CMD_REMOVE
, ptr
->section
, ptr
->option
, NULL
);
568 else if (ptr
->section
)
574 int uci_add_list(struct uci_context
*ctx
, struct uci_ptr
*ptr
)
576 /* NB: UCI_INTERNAL use means without delta tracking */
577 bool internal
= ctx
->internal
;
578 struct uci_option
*prev
= NULL
;
579 const char *value2
= NULL
;
583 uci_expand_ptr(ctx
, ptr
, false);
584 UCI_ASSERT(ctx
, ptr
->s
);
585 UCI_ASSERT(ctx
, ptr
->value
);
588 switch (ptr
->o
->type
) {
589 case UCI_TYPE_STRING
:
590 /* we already have a string value, convert that to a list */
593 ptr
->value
= ptr
->o
->v
.string
;
596 uci_add_element_list(ctx
, ptr
, internal
);
599 UCI_THROW(ctx
, UCI_ERR_INVAL
);
604 ptr
->o
= uci_alloc_list(ptr
->s
, ptr
->option
);
606 uci_add_element_list(ctx
, ptr
, true);
607 uci_free_option(prev
);
610 uci_add_element_list(ctx
, ptr
, internal
);
615 int uci_set(struct uci_context
*ctx
, struct uci_ptr
*ptr
)
617 /* NB: UCI_INTERNAL use means without delta tracking */
618 bool internal
= ctx
->internal
;
621 uci_expand_ptr(ctx
, ptr
, false);
622 UCI_ASSERT(ctx
, ptr
->value
);
623 UCI_ASSERT(ctx
, ptr
->s
|| (!ptr
->option
&& ptr
->section
));
624 if (!ptr
->option
&& ptr
->value
[0]) {
625 UCI_ASSERT(ctx
, uci_validate_type(ptr
->value
));
628 if (!ptr
->o
&& ptr
->s
&& ptr
->option
) {
629 struct uci_element
*e
;
630 e
= uci_lookup_list(&ptr
->s
->options
, ptr
->option
);
632 ptr
->o
= uci_to_option(e
);
634 if (!ptr
->value
[0]) {
635 /* if setting a nonexistant option/section to a nonexistant value,
636 * exit without errors */
637 if (!(ptr
->flags
& UCI_LOOKUP_COMPLETE
))
640 return uci_delete(ctx
, ptr
);
641 } else if (!ptr
->o
&& ptr
->option
) { /* new option */
642 ptr
->o
= uci_alloc_option(ptr
->s
, ptr
->option
, ptr
->value
);
643 ptr
->last
= &ptr
->o
->e
;
644 } else if (!ptr
->s
&& ptr
->section
) { /* new section */
645 ptr
->s
= uci_alloc_section(ptr
->p
, ptr
->value
, ptr
->section
);
646 ptr
->last
= &ptr
->s
->e
;
647 } else if (ptr
->o
&& ptr
->option
) { /* update option */
648 if ((ptr
->o
->type
== UCI_TYPE_STRING
) &&
649 !strcmp(ptr
->o
->v
.string
, ptr
->value
))
651 uci_free_option(ptr
->o
);
652 ptr
->o
= uci_alloc_option(ptr
->s
, ptr
->option
, ptr
->value
);
653 ptr
->last
= &ptr
->o
->e
;
654 } else if (ptr
->s
&& ptr
->section
) { /* update section */
655 char *s
= uci_strdup(ctx
, ptr
->value
);
657 if (ptr
->s
->type
== uci_dataptr(ptr
->s
)) {
659 ptr
->last
= uci_realloc(ctx
, ptr
->s
, sizeof(struct uci_section
));
660 ptr
->s
= uci_to_section(ptr
->last
);
661 uci_list_fixup(&ptr
->s
->e
.list
);
667 UCI_THROW(ctx
, UCI_ERR_INVAL
);
670 if (!internal
&& ptr
->p
->has_delta
)
671 uci_add_delta(ctx
, &ptr
->p
->delta
, UCI_CMD_CHANGE
, ptr
->section
, ptr
->option
, ptr
->value
);
676 int uci_unload(struct uci_context
*ctx
, struct uci_package
*p
)
679 UCI_ASSERT(ctx
, p
!= NULL
);
681 uci_free_package(&p
);