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 static 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 /* fix up an unnamed section, e.g. after adding options to it */
131 __private
void uci_fixup_section(struct uci_context
*ctx
, struct uci_section
*s
)
133 unsigned int hash
= ~0;
134 struct uci_element
*e
;
141 * Generate a name for unnamed sections. This is used as reference
142 * when locating or updating the section from apps/scripts.
143 * To make multiple concurrent versions somewhat safe for updating,
144 * the name is generated from a hash of its type and name/value
145 * pairs of its option, and it is prefixed by a counter value.
146 * If the order of the unnamed sections changes for some reason,
147 * updates to them will be rejected.
149 hash
= djbhash(hash
, s
->type
);
150 uci_foreach_element(&s
->options
, e
) {
151 struct uci_option
*o
;
152 hash
= djbhash(hash
, e
->name
);
153 o
= uci_to_option(e
);
155 case UCI_TYPE_STRING
:
156 hash
= djbhash(hash
, o
->v
.string
);
162 sprintf(buf
, "cfg%02x%04x", ++s
->package
->n_section
, hash
% (1 << 16));
163 s
->e
.name
= uci_strdup(ctx
, buf
);
166 static struct uci_section
*
167 uci_alloc_section(struct uci_package
*p
, const char *type
, const char *name
)
169 struct uci_context
*ctx
= p
->ctx
;
170 struct uci_section
*s
;
172 if (name
&& !name
[0])
175 s
= uci_alloc_element(ctx
, section
, name
, strlen(type
) + 1);
176 uci_list_init(&s
->options
);
177 s
->type
= uci_dataptr(s
);
179 strcpy(s
->type
, type
);
184 uci_list_add(&p
->sections
, &s
->e
.list
);
190 uci_free_section(struct uci_section
*s
)
192 struct uci_element
*o
, *tmp
;
194 uci_foreach_element_safe(&s
->options
, tmp
, o
) {
195 uci_free_option(uci_to_option(o
));
197 if ((s
->type
!= uci_dataptr(s
)) &&
200 uci_free_element(&s
->e
);
203 __plugin
struct uci_package
*
204 uci_alloc_package(struct uci_context
*ctx
, const char *name
)
206 struct uci_package
*p
;
208 p
= uci_alloc_element(ctx
, package
, name
, 0);
210 uci_list_init(&p
->sections
);
211 uci_list_init(&p
->history
);
212 uci_list_init(&p
->saved_history
);
217 uci_free_package(struct uci_package
**package
)
219 struct uci_element
*e
, *tmp
;
220 struct uci_package
*p
= *package
;
227 uci_foreach_element_safe(&p
->sections
, tmp
, e
) {
228 uci_free_section(uci_to_section(e
));
230 uci_foreach_element_safe(&p
->history
, tmp
, e
) {
231 uci_free_history(uci_to_history(e
));
233 uci_foreach_element_safe(&p
->saved_history
, tmp
, e
) {
234 uci_free_history(uci_to_history(e
));
236 uci_free_element(&p
->e
);
241 uci_free_any(struct uci_element
**e
)
244 case UCI_TYPE_SECTION
:
245 uci_free_section(uci_to_section(*e
));
247 case UCI_TYPE_OPTION
:
248 uci_free_option(uci_to_option(*e
));
256 __private
struct uci_element
*
257 uci_lookup_list(struct uci_list
*list
, const char *name
)
259 struct uci_element
*e
;
261 uci_foreach_element(list
, e
) {
262 if (!strcmp(e
->name
, name
))
268 static struct uci_element
*
269 uci_lookup_ext_section(struct uci_context
*ctx
, struct uci_ptr
*ptr
)
271 char *idxstr
, *t
, *section
, *name
;
272 struct uci_element
*e
= NULL
;
273 struct uci_section
*s
;
276 section
= uci_strdup(ctx
, ptr
->section
);
277 name
= idxstr
= section
+ 1;
279 if (section
[0] != '@')
282 /* parse the section index part */
283 idxstr
= strchr(idxstr
, '[');
289 t
= strchr(idxstr
, ']');
297 idx
= strtol(idxstr
, &t
, 10);
303 else if (!uci_validate_type(name
))
306 /* if the given index is negative, it specifies the section number from
307 * the end of the list */
310 uci_foreach_element(&ptr
->p
->sections
, e
) {
311 s
= uci_to_section(e
);
312 if (name
&& (strcmp(s
->type
, name
) != 0))
321 uci_foreach_element(&ptr
->p
->sections
, e
) {
322 s
= uci_to_section(e
);
323 if (name
&& (strcmp(s
->type
, name
) != 0))
335 memset(ptr
, 0, sizeof(struct uci_ptr
));
336 UCI_THROW(ctx
, UCI_ERR_INVAL
);
340 ptr
->section
= e
->name
;
345 uci_lookup_ptr(struct uci_context
*ctx
, struct uci_ptr
*ptr
, char *str
, bool extended
)
347 struct uci_element
*e
;
350 UCI_ASSERT(ctx
, ptr
!= NULL
);
353 UCI_INTERNAL(uci_parse_ptr
, ctx
, ptr
, str
);
355 ptr
->flags
|= UCI_LOOKUP_DONE
;
357 /* look up the package first */
358 e
= uci_lookup_list(&ctx
->root
, ptr
->package
);
360 UCI_INTERNAL(uci_load
, ctx
, ptr
->package
, &ptr
->p
);
363 ptr
->last
= &ptr
->p
->e
;
365 ptr
->p
= uci_to_package(e
);
372 /* if the section name validates as a regular name, pass through
373 * to the regular uci_lookup function call */
374 if (ptr
->flags
& UCI_LOOKUP_EXTENDED
)
375 e
= uci_lookup_ext_section(ctx
, ptr
);
377 e
= uci_lookup_list(&ptr
->p
->sections
, ptr
->section
);
383 ptr
->s
= uci_to_section(e
);
386 e
= uci_lookup_list(&ptr
->s
->options
, ptr
->option
);
390 ptr
->o
= uci_to_option(e
);
395 ptr
->flags
|= UCI_LOOKUP_COMPLETE
;
400 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
404 static struct uci_element
*
405 expand_ptr(struct uci_context
*ctx
, struct uci_ptr
*ptr
, bool complete
)
407 UCI_ASSERT(ctx
, ptr
!= NULL
);
409 if (!(ptr
->flags
& UCI_LOOKUP_DONE
))
410 UCI_INTERNAL(uci_lookup_ptr
, ctx
, ptr
, NULL
, 1);
411 if (complete
&& !(ptr
->flags
& UCI_LOOKUP_COMPLETE
))
412 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
413 UCI_ASSERT(ctx
, ptr
->p
!= NULL
);
415 /* fill in missing string info */
416 if (ptr
->p
&& !ptr
->package
)
417 ptr
->package
= ptr
->p
->e
.name
;
418 if (ptr
->s
&& !ptr
->section
)
419 ptr
->section
= ptr
->s
->e
.name
;
420 if (ptr
->o
&& !ptr
->option
)
421 ptr
->option
= ptr
->o
->e
.name
;
433 static void uci_add_element_list(struct uci_context
*ctx
, struct uci_ptr
*ptr
, bool internal
)
435 struct uci_element
*e
;
436 struct uci_package
*p
;
439 if (!internal
&& p
->has_history
)
440 uci_add_history(ctx
, &p
->history
, UCI_CMD_LIST_ADD
, ptr
->section
, ptr
->option
, ptr
->value
);
442 e
= uci_alloc_generic(ctx
, UCI_TYPE_ITEM
, ptr
->value
, sizeof(struct uci_option
));
443 uci_list_add(&ptr
->o
->v
.list
, &e
->list
);
446 int uci_rename(struct uci_context
*ctx
, struct uci_ptr
*ptr
)
448 /* NB: UCI_INTERNAL use means without history tracking */
449 bool internal
= ctx
->internal
;
450 struct uci_element
*e
;
451 struct uci_package
*p
;
456 e
= expand_ptr(ctx
, ptr
, true);
459 UCI_ASSERT(ctx
, ptr
->s
);
460 UCI_ASSERT(ctx
, ptr
->value
);
462 if (!internal
&& p
->has_history
)
463 uci_add_history(ctx
, &p
->history
, UCI_CMD_RENAME
, ptr
->section
, ptr
->option
, ptr
->value
);
465 n
= uci_strdup(ctx
, ptr
->value
);
470 if (e
->type
== UCI_TYPE_SECTION
)
471 uci_to_section(e
)->anonymous
= false;
476 int uci_reorder_section(struct uci_context
*ctx
, struct uci_section
*s
, int pos
)
478 struct uci_package
*p
= s
->package
;
483 uci_list_set_pos(&s
->package
->sections
, &s
->e
.list
, pos
);
484 if (!ctx
->internal
&& p
->has_history
) {
485 sprintf(order
, "%d", pos
);
486 uci_add_history(ctx
, &p
->history
, UCI_CMD_REORDER
, s
->e
.name
, NULL
, order
);
492 int uci_add_section(struct uci_context
*ctx
, struct uci_package
*p
, const char *type
, struct uci_section
**res
)
494 bool internal
= ctx
->internal
;
495 struct uci_section
*s
;
498 UCI_ASSERT(ctx
, p
!= NULL
);
499 s
= uci_alloc_section(p
, type
, NULL
);
500 uci_fixup_section(ctx
, s
);
502 if (!internal
&& p
->has_history
)
503 uci_add_history(ctx
, &p
->history
, UCI_CMD_ADD
, s
->e
.name
, NULL
, type
);
508 int uci_delete(struct uci_context
*ctx
, struct uci_ptr
*ptr
)
510 /* NB: pass on internal flag to uci_del_element */
511 bool internal
= ctx
->internal
;
512 struct uci_package
*p
;
513 struct uci_element
*e
;
517 e
= expand_ptr(ctx
, ptr
, true);
520 UCI_ASSERT(ctx
, ptr
->s
);
522 if (!internal
&& p
->has_history
)
523 uci_add_history(ctx
, &p
->history
, UCI_CMD_REMOVE
, ptr
->section
, ptr
->option
, NULL
);
529 else if (ptr
->section
)
535 int uci_add_list(struct uci_context
*ctx
, struct uci_ptr
*ptr
)
537 /* NB: UCI_INTERNAL use means without history tracking */
538 bool internal
= ctx
->internal
;
539 struct uci_option
*prev
= NULL
;
540 const char *value2
= NULL
;
544 expand_ptr(ctx
, ptr
, false);
545 UCI_ASSERT(ctx
, ptr
->s
);
546 UCI_ASSERT(ctx
, ptr
->value
);
549 switch (ptr
->o
->type
) {
550 case UCI_TYPE_STRING
:
551 /* we already have a string value, convert that to a list */
554 ptr
->value
= ptr
->o
->v
.string
;
557 uci_add_element_list(ctx
, ptr
, internal
);
560 UCI_THROW(ctx
, UCI_ERR_INVAL
);
565 ptr
->o
= uci_alloc_list(ptr
->s
, ptr
->option
);
567 uci_add_element_list(ctx
, ptr
, true);
568 uci_free_option(prev
);
571 uci_add_element_list(ctx
, ptr
, internal
);
576 int uci_set(struct uci_context
*ctx
, struct uci_ptr
*ptr
)
578 /* NB: UCI_INTERNAL use means without history tracking */
579 bool internal
= ctx
->internal
;
582 expand_ptr(ctx
, ptr
, false);
583 UCI_ASSERT(ctx
, ptr
->value
);
584 UCI_ASSERT(ctx
, ptr
->s
|| (!ptr
->option
&& ptr
->section
));
585 if (!ptr
->option
&& ptr
->value
[0]) {
586 UCI_ASSERT(ctx
, uci_validate_type(ptr
->value
));
589 if (!ptr
->o
&& ptr
->s
&& ptr
->option
) {
590 struct uci_element
*e
;
591 e
= uci_lookup_list(&ptr
->s
->options
, ptr
->option
);
593 ptr
->o
= uci_to_option(e
);
595 if (!ptr
->value
[0]) {
596 /* if setting a nonexistant option/section to a nonexistant value,
597 * exit without errors */
598 if (!(ptr
->flags
& UCI_LOOKUP_COMPLETE
))
601 return uci_delete(ctx
, ptr
);
602 } else if (!ptr
->o
&& ptr
->option
) { /* new option */
603 ptr
->o
= uci_alloc_option(ptr
->s
, ptr
->option
, ptr
->value
);
604 ptr
->last
= &ptr
->o
->e
;
605 } else if (!ptr
->s
&& ptr
->section
) { /* new section */
606 ptr
->s
= uci_alloc_section(ptr
->p
, ptr
->value
, ptr
->section
);
607 ptr
->last
= &ptr
->s
->e
;
608 } else if (ptr
->o
&& ptr
->option
) { /* update option */
609 if ((ptr
->o
->type
== UCI_TYPE_STRING
) &&
610 !strcmp(ptr
->o
->v
.string
, ptr
->value
))
612 uci_free_option(ptr
->o
);
613 ptr
->o
= uci_alloc_option(ptr
->s
, ptr
->option
, ptr
->value
);
614 ptr
->last
= &ptr
->o
->e
;
615 } else if (ptr
->s
&& ptr
->section
) { /* update section */
616 char *s
= uci_strdup(ctx
, ptr
->value
);
618 if (ptr
->s
->type
== uci_dataptr(ptr
->s
)) {
620 ptr
->last
= uci_realloc(ctx
, ptr
->s
, sizeof(struct uci_section
));
621 ptr
->s
= uci_to_section(ptr
->last
);
622 uci_list_fixup(&ptr
->s
->e
.list
);
628 UCI_THROW(ctx
, UCI_ERR_INVAL
);
631 if (!internal
&& ptr
->p
->has_history
)
632 uci_add_history(ctx
, &ptr
->p
->history
, UCI_CMD_CHANGE
, ptr
->section
, ptr
->option
, ptr
->value
);
637 int uci_unload(struct uci_context
*ctx
, struct uci_package
*p
)
640 UCI_ASSERT(ctx
, p
!= NULL
);
642 uci_free_package(&p
);