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
) {
26 uci_list_add(new_head
, ptr
);
29 static inline void uci_list_fixup(struct uci_list
*ptr
)
31 ptr
->prev
->next
= ptr
;
32 ptr
->next
->prev
= ptr
;
36 * uci_alloc_generic allocates a new uci_element with payload
37 * payload is appended to the struct to save memory and reduce fragmentation
39 __private
struct uci_element
*
40 uci_alloc_generic(struct uci_context
*ctx
, int type
, const char *name
, int size
)
42 struct uci_element
*e
;
46 ptr
= uci_malloc(ctx
, datalen
);
47 e
= (struct uci_element
*) ptr
;
50 UCI_TRAP_SAVE(ctx
, error
);
51 e
->name
= uci_strdup(ctx
, name
);
52 UCI_TRAP_RESTORE(ctx
);
54 uci_list_init(&e
->list
);
59 UCI_THROW(ctx
, ctx
->err
);
66 uci_free_element(struct uci_element
*e
)
70 if (!uci_list_empty(&e
->list
))
71 uci_list_del(&e
->list
);
75 static struct uci_option
*
76 uci_alloc_option(struct uci_section
*s
, const char *name
, const char *value
)
78 struct uci_package
*p
= s
->package
;
79 struct uci_context
*ctx
= p
->ctx
;
82 o
= uci_alloc_element(ctx
, option
, name
, strlen(value
) + 1);
83 o
->type
= UCI_TYPE_STRING
;
84 o
->v
.string
= uci_dataptr(o
);
86 strcpy(o
->v
.string
, value
);
87 uci_list_add(&s
->options
, &o
->e
.list
);
93 uci_free_option(struct uci_option
*o
)
95 struct uci_element
*e
, *tmp
;
99 if ((o
->v
.string
!= uci_dataptr(o
)) &&
100 (o
->v
.string
!= NULL
))
104 uci_foreach_element_safe(&o
->v
.list
, tmp
, e
) {
111 uci_free_element(&o
->e
);
114 static struct uci_option
*
115 uci_alloc_list(struct uci_section
*s
, const char *name
)
117 struct uci_package
*p
= s
->package
;
118 struct uci_context
*ctx
= p
->ctx
;
119 struct uci_option
*o
;
121 o
= uci_alloc_element(ctx
, option
, name
, 0);
122 o
->type
= UCI_TYPE_LIST
;
124 uci_list_init(&o
->v
.list
);
125 uci_list_add(&s
->options
, &o
->e
.list
);
130 /* Based on an efficient hash function published by D. J. Bernstein */
131 static unsigned int djbhash(unsigned int hash
, char *str
)
133 int len
= strlen(str
);
140 for(i
= 0; i
< len
; i
++) {
141 hash
= ((hash
<< 5) + hash
) + str
[i
];
143 return (hash
& 0x7FFFFFFF);
146 /* fix up an unnamed section, e.g. after adding options to it */
147 __private
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
->delta
);
228 uci_list_init(&p
->saved_delta
);
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
->delta
, tmp
, e
) {
247 uci_free_delta(uci_to_delta(e
));
249 uci_foreach_element_safe(&p
->saved_delta
, tmp
, e
) {
250 uci_free_delta(uci_to_delta(e
));
252 uci_free_element(&p
->e
);
257 uci_free_any(struct uci_element
**e
)
260 case UCI_TYPE_SECTION
:
261 uci_free_section(uci_to_section(*e
));
263 case UCI_TYPE_OPTION
:
264 uci_free_option(uci_to_option(*e
));
272 __private
struct uci_element
*
273 uci_lookup_list(struct uci_list
*list
, const char *name
)
275 struct uci_element
*e
;
277 uci_foreach_element(list
, e
) {
278 if (!strcmp(e
->name
, name
))
284 static struct uci_element
*
285 uci_lookup_ext_section(struct uci_context
*ctx
, struct uci_ptr
*ptr
)
287 char *idxstr
, *t
, *section
, *name
;
288 struct uci_element
*e
= NULL
;
289 struct uci_section
*s
;
292 section
= uci_strdup(ctx
, ptr
->section
);
293 name
= idxstr
= section
+ 1;
295 if (section
[0] != '@')
298 /* parse the section index part */
299 idxstr
= strchr(idxstr
, '[');
305 t
= strchr(idxstr
, ']');
313 idx
= strtol(idxstr
, &t
, 10);
319 else if (!uci_validate_type(name
))
322 /* if the given index is negative, it specifies the section number from
323 * the end of the list */
326 uci_foreach_element(&ptr
->p
->sections
, e
) {
327 s
= uci_to_section(e
);
328 if (name
&& (strcmp(s
->type
, name
) != 0))
337 uci_foreach_element(&ptr
->p
->sections
, e
) {
338 s
= uci_to_section(e
);
339 if (name
&& (strcmp(s
->type
, name
) != 0))
351 memset(ptr
, 0, sizeof(struct uci_ptr
));
352 UCI_THROW(ctx
, UCI_ERR_INVAL
);
356 ptr
->section
= e
->name
;
361 uci_lookup_ptr(struct uci_context
*ctx
, struct uci_ptr
*ptr
, char *str
, bool extended
)
363 struct uci_element
*e
;
366 UCI_ASSERT(ctx
, ptr
!= NULL
);
369 UCI_INTERNAL(uci_parse_ptr
, ctx
, ptr
, str
);
371 ptr
->flags
|= UCI_LOOKUP_DONE
;
373 /* look up the package first */
377 e
= uci_lookup_list(&ctx
->root
, ptr
->package
);
380 UCI_INTERNAL(uci_load
, ctx
, ptr
->package
, &ptr
->p
);
383 ptr
->last
= &ptr
->p
->e
;
385 ptr
->p
= uci_to_package(e
);
389 if (!ptr
->section
&& !ptr
->s
)
392 /* if the section name validates as a regular name, pass through
393 * to the regular uci_lookup function call */
396 } else if (ptr
->flags
& UCI_LOOKUP_EXTENDED
) {
398 e
= uci_lookup_ext_section(ctx
, ptr
);
400 UCI_THROW(ctx
, UCI_ERR_INVAL
);
402 e
= uci_lookup_list(&ptr
->p
->sections
, ptr
->section
);
409 ptr
->s
= uci_to_section(e
);
412 e
= uci_lookup_list(&ptr
->s
->options
, ptr
->option
);
416 ptr
->o
= uci_to_option(e
);
421 ptr
->flags
|= UCI_LOOKUP_COMPLETE
;
426 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
430 __private
struct uci_element
*
431 uci_expand_ptr(struct uci_context
*ctx
, struct uci_ptr
*ptr
, bool complete
)
433 UCI_ASSERT(ctx
, ptr
!= NULL
);
435 if (!(ptr
->flags
& UCI_LOOKUP_DONE
))
436 UCI_INTERNAL(uci_lookup_ptr
, ctx
, ptr
, NULL
, 1);
437 if (complete
&& !(ptr
->flags
& UCI_LOOKUP_COMPLETE
))
438 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
439 UCI_ASSERT(ctx
, ptr
->p
!= NULL
);
441 /* fill in missing string info */
442 if (ptr
->p
&& !ptr
->package
)
443 ptr
->package
= ptr
->p
->e
.name
;
444 if (ptr
->s
&& !ptr
->section
)
445 ptr
->section
= ptr
->s
->e
.name
;
446 if (ptr
->o
&& !ptr
->option
)
447 ptr
->option
= ptr
->o
->e
.name
;
459 static void uci_add_element_list(struct uci_context
*ctx
, struct uci_ptr
*ptr
, bool internal
)
461 struct uci_element
*e
;
462 struct uci_package
*p
;
465 if (!internal
&& p
->has_delta
)
466 uci_add_delta(ctx
, &p
->delta
, UCI_CMD_LIST_ADD
, ptr
->section
, ptr
->option
, ptr
->value
);
468 e
= uci_alloc_generic(ctx
, UCI_TYPE_ITEM
, ptr
->value
, sizeof(struct uci_option
));
469 uci_list_add(&ptr
->o
->v
.list
, &e
->list
);
472 int uci_rename(struct uci_context
*ctx
, struct uci_ptr
*ptr
)
474 /* NB: UCI_INTERNAL use means without delta tracking */
475 bool internal
= ctx
->internal
;
476 struct uci_element
*e
;
477 struct uci_package
*p
;
482 e
= uci_expand_ptr(ctx
, ptr
, true);
485 UCI_ASSERT(ctx
, ptr
->s
);
486 UCI_ASSERT(ctx
, ptr
->value
);
488 if (!internal
&& p
->has_delta
)
489 uci_add_delta(ctx
, &p
->delta
, UCI_CMD_RENAME
, ptr
->section
, ptr
->option
, ptr
->value
);
491 n
= uci_strdup(ctx
, ptr
->value
);
496 if (e
->type
== UCI_TYPE_SECTION
)
497 uci_to_section(e
)->anonymous
= false;
502 int uci_reorder_section(struct uci_context
*ctx
, struct uci_section
*s
, int pos
)
504 struct uci_package
*p
= s
->package
;
509 uci_list_set_pos(&s
->package
->sections
, &s
->e
.list
, pos
);
510 if (!ctx
->internal
&& p
->has_delta
) {
511 sprintf(order
, "%d", pos
);
512 uci_add_delta(ctx
, &p
->delta
, UCI_CMD_REORDER
, s
->e
.name
, NULL
, order
);
518 int uci_add_section(struct uci_context
*ctx
, struct uci_package
*p
, const char *type
, struct uci_section
**res
)
520 bool internal
= ctx
->internal
;
521 struct uci_section
*s
;
524 UCI_ASSERT(ctx
, p
!= NULL
);
525 s
= uci_alloc_section(p
, type
, NULL
);
526 uci_fixup_section(ctx
, s
);
528 if (!internal
&& p
->has_delta
)
529 uci_add_delta(ctx
, &p
->delta
, UCI_CMD_ADD
, s
->e
.name
, NULL
, type
);
534 int uci_delete(struct uci_context
*ctx
, struct uci_ptr
*ptr
)
536 /* NB: pass on internal flag to uci_del_element */
537 bool internal
= ctx
->internal
;
538 struct uci_package
*p
;
539 struct uci_element
*e
;
543 e
= uci_expand_ptr(ctx
, ptr
, true);
546 UCI_ASSERT(ctx
, ptr
->s
);
548 if (!internal
&& p
->has_delta
)
549 uci_add_delta(ctx
, &p
->delta
, UCI_CMD_REMOVE
, ptr
->section
, ptr
->option
, NULL
);
555 else if (ptr
->section
)
561 int uci_add_list(struct uci_context
*ctx
, struct uci_ptr
*ptr
)
563 /* NB: UCI_INTERNAL use means without delta tracking */
564 bool internal
= ctx
->internal
;
565 struct uci_option
*prev
= NULL
;
566 const char *value2
= NULL
;
570 uci_expand_ptr(ctx
, ptr
, false);
571 UCI_ASSERT(ctx
, ptr
->s
);
572 UCI_ASSERT(ctx
, ptr
->value
);
575 switch (ptr
->o
->type
) {
576 case UCI_TYPE_STRING
:
577 /* we already have a string value, convert that to a list */
580 ptr
->value
= ptr
->o
->v
.string
;
583 uci_add_element_list(ctx
, ptr
, internal
);
586 UCI_THROW(ctx
, UCI_ERR_INVAL
);
591 ptr
->o
= uci_alloc_list(ptr
->s
, ptr
->option
);
593 uci_add_element_list(ctx
, ptr
, true);
594 uci_free_option(prev
);
597 uci_add_element_list(ctx
, ptr
, internal
);
602 int uci_set(struct uci_context
*ctx
, struct uci_ptr
*ptr
)
604 /* NB: UCI_INTERNAL use means without delta tracking */
605 bool internal
= ctx
->internal
;
608 uci_expand_ptr(ctx
, ptr
, false);
609 UCI_ASSERT(ctx
, ptr
->value
);
610 UCI_ASSERT(ctx
, ptr
->s
|| (!ptr
->option
&& ptr
->section
));
611 if (!ptr
->option
&& ptr
->value
[0]) {
612 UCI_ASSERT(ctx
, uci_validate_type(ptr
->value
));
615 if (!ptr
->o
&& ptr
->s
&& ptr
->option
) {
616 struct uci_element
*e
;
617 e
= uci_lookup_list(&ptr
->s
->options
, ptr
->option
);
619 ptr
->o
= uci_to_option(e
);
621 if (!ptr
->value
[0]) {
622 /* if setting a nonexistant option/section to a nonexistant value,
623 * exit without errors */
624 if (!(ptr
->flags
& UCI_LOOKUP_COMPLETE
))
627 return uci_delete(ctx
, ptr
);
628 } else if (!ptr
->o
&& ptr
->option
) { /* new option */
629 ptr
->o
= uci_alloc_option(ptr
->s
, ptr
->option
, ptr
->value
);
630 ptr
->last
= &ptr
->o
->e
;
631 } else if (!ptr
->s
&& ptr
->section
) { /* new section */
632 ptr
->s
= uci_alloc_section(ptr
->p
, ptr
->value
, ptr
->section
);
633 ptr
->last
= &ptr
->s
->e
;
634 } else if (ptr
->o
&& ptr
->option
) { /* update option */
635 if ((ptr
->o
->type
== UCI_TYPE_STRING
) &&
636 !strcmp(ptr
->o
->v
.string
, ptr
->value
))
638 uci_free_option(ptr
->o
);
639 ptr
->o
= uci_alloc_option(ptr
->s
, ptr
->option
, ptr
->value
);
640 ptr
->last
= &ptr
->o
->e
;
641 } else if (ptr
->s
&& ptr
->section
) { /* update section */
642 char *s
= uci_strdup(ctx
, ptr
->value
);
644 if (ptr
->s
->type
== uci_dataptr(ptr
->s
)) {
646 ptr
->last
= uci_realloc(ctx
, ptr
->s
, sizeof(struct uci_section
));
647 ptr
->s
= uci_to_section(ptr
->last
);
648 uci_list_fixup(&ptr
->s
->e
.list
);
654 UCI_THROW(ctx
, UCI_ERR_INVAL
);
657 if (!internal
&& ptr
->p
->has_delta
)
658 uci_add_delta(ctx
, &ptr
->p
->delta
, UCI_CMD_CHANGE
, ptr
->section
, ptr
->option
, ptr
->value
);
663 int uci_unload(struct uci_context
*ctx
, struct uci_package
*p
)
666 UCI_ASSERT(ctx
, p
!= NULL
);
668 uci_free_package(&p
);