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.
17 /* initialize a list head/item */
18 static inline void uci_list_init(struct uci_list
*ptr
)
24 /* inserts a new list entry after a given entry */
25 static inline void uci_list_insert(struct uci_list
*list
, struct uci_list
*ptr
)
27 list
->next
->prev
= ptr
;
29 ptr
->next
= list
->next
;
33 /* inserts a new list entry at the tail of the list */
34 static inline void uci_list_add(struct uci_list
*head
, struct uci_list
*ptr
)
36 /* NB: head->prev points at the tail */
37 uci_list_insert(head
->prev
, ptr
);
40 static inline void uci_list_del(struct uci_list
*ptr
)
42 struct uci_list
*next
, *prev
;
54 * uci_alloc_generic allocates a new uci_element with payload
55 * payload is appended to the struct to save memory and reduce fragmentation
57 static struct uci_element
*
58 uci_alloc_generic(struct uci_context
*ctx
, int type
, const char *name
, int size
)
60 struct uci_element
*e
;
64 ptr
= uci_malloc(ctx
, datalen
);
65 e
= (struct uci_element
*) ptr
;
68 UCI_TRAP_SAVE(ctx
, error
);
69 e
->name
= uci_strdup(ctx
, name
);
70 UCI_TRAP_RESTORE(ctx
);
72 uci_list_init(&e
->list
);
77 UCI_THROW(ctx
, ctx
->err
);
84 uci_free_element(struct uci_element
*e
)
88 if (!uci_list_empty(&e
->list
))
89 uci_list_del(&e
->list
);
93 static struct uci_option
*
94 uci_alloc_option(struct uci_section
*s
, const char *name
, const char *value
)
96 struct uci_package
*p
= s
->package
;
97 struct uci_context
*ctx
= p
->ctx
;
100 o
= uci_alloc_element(ctx
, option
, name
, strlen(value
) + 1);
101 o
->type
= UCI_TYPE_STRING
;
102 o
->v
.string
= uci_dataptr(o
);
104 strcpy(o
->v
.string
, value
);
105 uci_list_add(&s
->options
, &o
->e
.list
);
111 uci_free_option(struct uci_option
*o
)
114 case UCI_TYPE_STRING
:
115 if ((o
->v
.string
!= uci_dataptr(o
)) &&
116 (o
->v
.string
!= NULL
))
122 uci_free_element(&o
->e
);
125 /* fix up an unnamed section, e.g. after adding options to it */
126 static void uci_fixup_section(struct uci_context
*ctx
, struct uci_section
*s
)
128 unsigned int hash
= ~0;
129 struct uci_element
*e
;
136 * Generate a name for unnamed sections. This is used as reference
137 * when locating or updating the section from apps/scripts.
138 * To make multiple concurrent versions somewhat safe for updating,
139 * the name is generated from a hash of its type and name/value
140 * pairs of its option, and it is prefixed by a counter value.
141 * If the order of the unnamed sections changes for some reason,
142 * updates to them will be rejected.
144 hash
= djbhash(hash
, s
->type
);
145 uci_foreach_element(&s
->options
, e
) {
146 struct uci_option
*o
;
147 hash
= djbhash(hash
, e
->name
);
148 o
= uci_to_option(e
);
150 case UCI_TYPE_STRING
:
151 hash
= djbhash(hash
, o
->v
.string
);
157 sprintf(buf
, "cfg%02x%04x", ++s
->package
->n_section
, hash
% (1 << 16));
158 s
->e
.name
= uci_strdup(ctx
, buf
);
161 static struct uci_section
*
162 uci_alloc_section(struct uci_package
*p
, const char *type
, const char *name
)
164 struct uci_context
*ctx
= p
->ctx
;
165 struct uci_section
*s
;
167 if (name
&& !name
[0])
170 s
= uci_alloc_element(ctx
, section
, name
, strlen(type
) + 1);
171 uci_list_init(&s
->options
);
172 s
->type
= uci_dataptr(s
);
174 strcpy(s
->type
, type
);
179 uci_list_add(&p
->sections
, &s
->e
.list
);
185 uci_free_section(struct uci_section
*s
)
187 struct uci_element
*o
, *tmp
;
189 uci_foreach_element_safe(&s
->options
, tmp
, o
) {
190 uci_free_option(uci_to_option(o
));
192 if ((s
->type
!= uci_dataptr(s
)) &&
195 uci_free_element(&s
->e
);
198 __plugin
struct uci_package
*
199 uci_alloc_package(struct uci_context
*ctx
, const char *name
)
201 struct uci_package
*p
;
203 p
= uci_alloc_element(ctx
, package
, name
, 0);
205 uci_list_init(&p
->sections
);
206 uci_list_init(&p
->history
);
207 uci_list_init(&p
->saved_history
);
212 uci_free_package(struct uci_package
**package
)
214 struct uci_element
*e
, *tmp
;
215 struct uci_package
*p
= *package
;
222 uci_foreach_element_safe(&p
->sections
, tmp
, e
) {
223 uci_free_section(uci_to_section(e
));
225 uci_foreach_element_safe(&p
->history
, tmp
, e
) {
226 uci_free_history(uci_to_history(e
));
228 uci_foreach_element_safe(&p
->saved_history
, tmp
, e
) {
229 uci_free_history(uci_to_history(e
));
231 uci_free_element(&p
->e
);
235 static struct uci_element
*uci_lookup_list(struct uci_list
*list
, const char *name
)
237 struct uci_element
*e
;
239 uci_foreach_element(list
, e
) {
240 if (!strcmp(e
->name
, name
))
246 int uci_lookup_ext(struct uci_context
*ctx
, struct uci_element
**res
, char *ptr
)
248 struct uci_package
*p
= NULL
;
249 struct uci_element
*e
;
250 struct uci_section
*s
;
251 char *package
= NULL
;
252 char *section
= NULL
;
258 UCI_ASSERT(ctx
, res
!= NULL
);
259 UCI_ASSERT(ctx
, ptr
!= NULL
);
261 UCI_INTERNAL(uci_parse_tuple
, ctx
, ptr
, &package
, §ion
, &option
, NULL
);
263 /* look up the package first */
264 e
= uci_lookup_list(&ctx
->root
, package
);
266 UCI_INTERNAL(uci_load
, ctx
, package
, &p
);
271 p
= uci_to_package(e
);
277 /* if the section name validates as a regular name, pass through
278 * to the regular uci_lookup function call */
279 if (!*section
|| uci_validate_name(section
)) {
280 UCI_INTERNAL(uci_lookup
, ctx
, &e
, p
, section
, option
);
284 /* name did not validate, that means we have an extended lookup call
285 * parse it here. for now only the section index syntax is supported */
286 if (section
[0] != '@')
291 /* parse the section index part */
292 idxstr
= strchr(section
, '[');
298 t
= strchr(idxstr
, ']');
306 idx
= strtol(idxstr
, &t
, 10);
312 if (section
&& !uci_validate_str(section
, false))
315 /* if the given index is negative, it specifies the section number from
316 * the end of the list */
319 uci_foreach_element(&p
->sections
, e
) {
320 s
= uci_to_section(e
);
321 if (section
&& (strcmp(s
->type
, section
) != 0))
330 uci_foreach_element(&p
->sections
, e
) {
331 s
= uci_to_section(e
);
332 if (section
&& (strcmp(s
->type
, section
) != 0))
343 e
= uci_lookup_list(&s
->options
, option
);
349 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
351 UCI_THROW(ctx
, UCI_ERR_INVAL
);
355 int uci_lookup(struct uci_context
*ctx
, struct uci_element
**res
, struct uci_package
*p
, const char *section
, const char *option
)
357 struct uci_element
*e
;
358 struct uci_section
*s
;
361 UCI_ASSERT(ctx
, res
!= NULL
);
362 UCI_ASSERT(ctx
, p
!= NULL
);
363 UCI_ASSERT(ctx
, section
&& uci_validate_name(section
));
365 UCI_ASSERT(ctx
, uci_validate_name(option
));
367 e
= uci_lookup_list(&p
->sections
, section
);
372 s
= uci_to_section(e
);
373 e
= uci_lookup_list(&s
->options
, option
);
382 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
386 int uci_del_element(struct uci_context
*ctx
, struct uci_element
*e
)
388 /* NB: UCI_INTERNAL use means without history tracking */
389 bool internal
= ctx
->internal
;
390 struct uci_package
*p
= NULL
;
391 struct uci_section
*s
= NULL
;
392 struct uci_option
*o
= NULL
;
393 struct uci_element
*i
, *tmp
;
397 UCI_ASSERT(ctx
, e
!= NULL
);
400 case UCI_TYPE_SECTION
:
401 s
= uci_to_section(e
);
402 uci_foreach_element_safe(&s
->options
, tmp
, i
) {
403 uci_del_element(ctx
, i
);
406 case UCI_TYPE_OPTION
:
407 o
= uci_to_option(e
);
413 UCI_THROW(ctx
, UCI_ERR_INVAL
);
418 if (!internal
&& p
->has_history
)
419 uci_add_history(ctx
, &p
->history
, UCI_CMD_REMOVE
, s
->e
.name
, option
, NULL
);
422 case UCI_TYPE_SECTION
:
425 case UCI_TYPE_OPTION
:
434 int uci_set_element_value(struct uci_context
*ctx
, struct uci_element
**element
, const char *value
)
436 /* NB: UCI_INTERNAL use means without history tracking */
437 bool internal
= ctx
->internal
;
438 struct uci_list
*list
;
439 struct uci_element
*e
;
440 struct uci_package
*p
;
441 struct uci_section
*s
;
442 struct uci_option
*o
;
449 UCI_ASSERT(ctx
, (element
!= NULL
) && (*element
!= NULL
));
451 /* what the 'value' of an element means depends on the type
452 * for a section, the 'value' means its type
453 * for an option, the 'value' means its value string
454 * when changing the value, shrink the element to its actual size
455 * (it may have been allocated with a bigger size, to include
457 * then duplicate the string passed on the command line and
458 * insert it into the structure.
463 case UCI_TYPE_SECTION
:
464 UCI_ASSERT(ctx
, uci_validate_str(value
, false));
465 size
= sizeof(struct uci_section
);
466 s
= uci_to_section(e
);
469 /* matches the currently set value */
470 if (!strcmp(value
, s
->type
))
473 case UCI_TYPE_OPTION
:
474 UCI_ASSERT(ctx
, value
!= NULL
);
475 size
= sizeof(struct uci_option
);
476 o
= uci_to_option(e
);
480 if (o
->type
!= UCI_TYPE_STRING
)
481 UCI_THROW(ctx
, UCI_ERR_INVAL
);
482 /* matches the currently set value */
483 if (!strcmp(value
, o
->v
.string
))
487 UCI_THROW(ctx
, UCI_ERR_INVAL
);
491 if (!internal
&& p
->has_history
)
492 uci_add_history(ctx
, &p
->history
, UCI_CMD_CHANGE
, section
, option
, value
);
494 uci_list_del(&e
->list
);
495 e
= uci_realloc(ctx
, e
, size
);
496 str
= uci_strdup(ctx
, value
);
497 uci_list_insert(list
, &e
->list
);
501 case UCI_TYPE_SECTION
:
502 uci_to_section(e
)->type
= str
;
504 case UCI_TYPE_OPTION
:
505 uci_to_option(e
)->v
.string
= str
;
514 int uci_rename(struct uci_context
*ctx
, struct uci_package
*p
, char *section
, char *option
, char *name
)
516 /* NB: UCI_INTERNAL use means without history tracking */
517 bool internal
= ctx
->internal
;
518 struct uci_element
*e
;
522 /* NB: p, section, option validated by uci_lookup */
523 UCI_INTERNAL(uci_lookup
, ctx
, &e
, p
, section
, option
);
525 if (!internal
&& p
->has_history
)
526 uci_add_history(ctx
, &p
->history
, UCI_CMD_RENAME
, section
, option
, name
);
528 name
= uci_strdup(ctx
, name
);
536 int uci_add_section(struct uci_context
*ctx
, struct uci_package
*p
, const char *type
, struct uci_section
**res
)
538 bool internal
= ctx
->internal
;
539 struct uci_section
*s
;
542 UCI_ASSERT(ctx
, p
!= NULL
);
543 s
= uci_alloc_section(p
, type
, NULL
);
544 uci_fixup_section(ctx
, s
);
546 if (!internal
&& p
->has_history
)
547 uci_add_history(ctx
, &p
->history
, UCI_CMD_ADD
, s
->e
.name
, NULL
, type
);
552 int uci_delete(struct uci_context
*ctx
, struct uci_package
*p
, const char *section
, const char *option
)
554 /* NB: pass on internal flag to uci_del_element */
555 bool internal
= ctx
->internal
;
556 struct uci_element
*e
;
560 /* NB: p, section, option validated by uci_lookup */
561 UCI_INTERNAL(uci_lookup
, ctx
, &e
, p
, section
, option
);
563 ctx
->internal
= internal
;
564 return uci_del_element(ctx
, e
);
567 int uci_set(struct uci_context
*ctx
, struct uci_package
*p
, const char *section
, const char *option
, const char *value
, struct uci_element
**result
)
569 /* NB: UCI_INTERNAL use means without history tracking */
570 bool internal
= ctx
->internal
;
571 struct uci_element
*e
= NULL
;
572 struct uci_section
*s
= NULL
;
573 struct uci_option
*o
= NULL
;
576 UCI_ASSERT(ctx
, p
!= NULL
);
577 UCI_ASSERT(ctx
, uci_validate_name(section
));
579 UCI_ASSERT(ctx
, uci_validate_name(option
));
580 UCI_ASSERT(ctx
, value
!= NULL
);
582 UCI_ASSERT(ctx
, uci_validate_str(value
, false));
586 * look up the package, section and option (if set)
587 * if the section/option is to be modified and it is not found
588 * create a new element in the appropriate list
590 e
= uci_lookup_list(&p
->sections
, section
);
594 s
= uci_to_section(e
);
595 if (ctx
->pctx
&& ctx
->pctx
->merge
)
596 ctx
->pctx
->section
= s
;
599 e
= uci_lookup_list(&s
->options
, option
);
602 o
= uci_to_option(e
);
606 * no unknown element was supplied, assume that we can just update
618 ctx
->internal
= internal
;
619 return uci_set_element_value(ctx
, result
, value
);
623 * the entry that we need to update was not found,
624 * check if the search failed prematurely.
625 * this can happen if the package was not found, or if
626 * an option was supplied, but the section wasn't found
628 if (!p
|| (!s
&& option
))
629 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
631 /* now add the missing entry */
632 if (!internal
&& p
->has_history
)
633 uci_add_history(ctx
, &p
->history
, UCI_CMD_CHANGE
, section
, option
, value
);
635 o
= uci_alloc_option(s
, option
, value
);
639 s
= uci_alloc_section(p
, value
, section
);
642 if (ctx
->pctx
&& ctx
->pctx
->merge
)
643 ctx
->pctx
->section
= s
;
649 int uci_unload(struct uci_context
*ctx
, struct uci_package
*p
)
652 UCI_ASSERT(ctx
, p
!= NULL
);
654 uci_free_package(&p
);