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
->value
= uci_dataptr(o
);
103 strcpy(o
->value
, value
);
104 uci_list_add(&s
->options
, &o
->e
.list
);
110 uci_free_option(struct uci_option
*o
)
112 if ((o
->value
!= uci_dataptr(o
)) &&
115 uci_free_element(&o
->e
);
118 /* fix up an unnamed section, e.g. after adding options to it */
119 static void uci_fixup_section(struct uci_context
*ctx
, struct uci_section
*s
)
121 unsigned int hash
= ~0;
122 struct uci_element
*e
;
129 * Generate a name for unnamed sections. This is used as reference
130 * when locating or updating the section from apps/scripts.
131 * To make multiple concurrent versions somewhat safe for updating,
132 * the name is generated from a hash of its type and name/value
133 * pairs of its option, and it is prefixed by a counter value.
134 * If the order of the unnamed sections changes for some reason,
135 * updates to them will be rejected.
137 hash
= djbhash(hash
, s
->type
);
138 uci_foreach_element(&s
->options
, e
) {
139 hash
= djbhash(hash
, e
->name
);
140 hash
= djbhash(hash
, uci_to_option(e
)->value
);
142 sprintf(buf
, "cfg%02x%04x", ++s
->package
->n_section
, hash
% (1 << 16));
143 s
->e
.name
= uci_strdup(ctx
, buf
);
146 static struct uci_section
*
147 uci_alloc_section(struct uci_package
*p
, const char *type
, const char *name
)
149 struct uci_context
*ctx
= p
->ctx
;
150 struct uci_section
*s
;
152 if (name
&& !name
[0])
155 s
= uci_alloc_element(ctx
, section
, name
, strlen(type
) + 1);
156 uci_list_init(&s
->options
);
157 s
->type
= uci_dataptr(s
);
159 strcpy(s
->type
, type
);
164 uci_list_add(&p
->sections
, &s
->e
.list
);
170 uci_free_section(struct uci_section
*s
)
172 struct uci_element
*o
, *tmp
;
174 uci_foreach_element_safe(&s
->options
, tmp
, o
) {
175 uci_free_option(uci_to_option(o
));
177 if ((s
->type
!= uci_dataptr(s
)) &&
180 uci_free_element(&s
->e
);
183 __plugin
struct uci_package
*
184 uci_alloc_package(struct uci_context
*ctx
, const char *name
)
186 struct uci_package
*p
;
188 p
= uci_alloc_element(ctx
, package
, name
, 0);
190 uci_list_init(&p
->sections
);
191 uci_list_init(&p
->history
);
192 uci_list_init(&p
->saved_history
);
197 uci_free_package(struct uci_package
**package
)
199 struct uci_element
*e
, *tmp
;
200 struct uci_package
*p
= *package
;
207 uci_foreach_element_safe(&p
->sections
, tmp
, e
) {
208 uci_free_section(uci_to_section(e
));
210 uci_foreach_element_safe(&p
->history
, tmp
, e
) {
211 uci_free_history(uci_to_history(e
));
213 uci_foreach_element_safe(&p
->saved_history
, tmp
, e
) {
214 uci_free_history(uci_to_history(e
));
216 uci_free_element(&p
->e
);
220 static struct uci_element
*uci_lookup_list(struct uci_list
*list
, const char *name
)
222 struct uci_element
*e
;
224 uci_foreach_element(list
, e
) {
225 if (!strcmp(e
->name
, name
))
231 int uci_lookup(struct uci_context
*ctx
, struct uci_element
**res
, struct uci_package
*p
, const char *section
, const char *option
)
233 struct uci_element
*e
;
234 struct uci_section
*s
;
237 UCI_ASSERT(ctx
, res
!= NULL
);
238 UCI_ASSERT(ctx
, p
!= NULL
);
239 UCI_ASSERT(ctx
, section
&& uci_validate_name(section
));
241 UCI_ASSERT(ctx
, uci_validate_name(option
));
243 e
= uci_lookup_list(&p
->sections
, section
);
248 s
= uci_to_section(e
);
249 e
= uci_lookup_list(&s
->options
, option
);
258 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
262 int uci_del_element(struct uci_context
*ctx
, struct uci_element
*e
)
264 /* NB: UCI_INTERNAL use means without history tracking */
265 bool internal
= ctx
->internal
;
266 struct uci_package
*p
= NULL
;
267 struct uci_section
*s
= NULL
;
268 struct uci_option
*o
= NULL
;
269 struct uci_element
*i
, *tmp
;
273 UCI_ASSERT(ctx
, e
!= NULL
);
276 case UCI_TYPE_SECTION
:
277 s
= uci_to_section(e
);
278 uci_foreach_element_safe(&s
->options
, tmp
, i
) {
279 uci_del_element(ctx
, i
);
282 case UCI_TYPE_OPTION
:
283 o
= uci_to_option(e
);
289 UCI_THROW(ctx
, UCI_ERR_INVAL
);
294 if (!internal
&& p
->has_history
)
295 uci_add_history(ctx
, &p
->history
, UCI_CMD_REMOVE
, s
->e
.name
, option
, NULL
);
298 case UCI_TYPE_SECTION
:
301 case UCI_TYPE_OPTION
:
310 int uci_set_element_value(struct uci_context
*ctx
, struct uci_element
**element
, const char *value
)
312 /* NB: UCI_INTERNAL use means without history tracking */
313 bool internal
= ctx
->internal
;
314 struct uci_list
*list
;
315 struct uci_element
*e
;
316 struct uci_package
*p
;
317 struct uci_section
*s
;
318 struct uci_option
*o
;
325 UCI_ASSERT(ctx
, (element
!= NULL
) && (*element
!= NULL
));
327 /* what the 'value' of an element means depends on the type
328 * for a section, the 'value' means its type
329 * for an option, the 'value' means its value string
330 * when changing the value, shrink the element to its actual size
331 * (it may have been allocated with a bigger size, to include
333 * then duplicate the string passed on the command line and
334 * insert it into the structure.
339 case UCI_TYPE_SECTION
:
340 UCI_ASSERT(ctx
, uci_validate_str(value
, false));
341 size
= sizeof(struct uci_section
);
342 s
= uci_to_section(e
);
345 /* matches the currently set value */
346 if (!strcmp(value
, s
->type
))
349 case UCI_TYPE_OPTION
:
350 UCI_ASSERT(ctx
, value
!= NULL
);
351 size
= sizeof(struct uci_option
);
352 o
= uci_to_option(e
);
356 /* matches the currently set value */
357 if (!strcmp(value
, o
->value
))
361 UCI_THROW(ctx
, UCI_ERR_INVAL
);
365 if (!internal
&& p
->has_history
)
366 uci_add_history(ctx
, &p
->history
, UCI_CMD_CHANGE
, section
, option
, value
);
368 uci_list_del(&e
->list
);
369 e
= uci_realloc(ctx
, e
, size
);
370 str
= uci_strdup(ctx
, value
);
371 uci_list_insert(list
, &e
->list
);
375 case UCI_TYPE_SECTION
:
376 uci_to_section(e
)->type
= str
;
378 case UCI_TYPE_OPTION
:
379 uci_to_option(e
)->value
= str
;
388 int uci_rename(struct uci_context
*ctx
, struct uci_package
*p
, char *section
, char *option
, char *name
)
390 /* NB: UCI_INTERNAL use means without history tracking */
391 bool internal
= ctx
->internal
;
392 struct uci_element
*e
;
396 /* NB: p, section, option validated by uci_lookup */
397 UCI_INTERNAL(uci_lookup
, ctx
, &e
, p
, section
, option
);
399 if (!internal
&& p
->has_history
)
400 uci_add_history(ctx
, &p
->history
, UCI_CMD_RENAME
, section
, option
, name
);
402 name
= uci_strdup(ctx
, name
);
410 int uci_add_section(struct uci_context
*ctx
, struct uci_package
*p
, const char *type
, struct uci_section
**res
)
412 bool internal
= ctx
->internal
;
413 struct uci_section
*s
;
416 UCI_ASSERT(ctx
, p
!= NULL
);
417 s
= uci_alloc_section(p
, type
, NULL
);
418 uci_fixup_section(ctx
, s
);
420 if (!internal
&& p
->has_history
)
421 uci_add_history(ctx
, &p
->history
, UCI_CMD_ADD
, s
->e
.name
, NULL
, type
);
426 int uci_delete(struct uci_context
*ctx
, struct uci_package
*p
, const char *section
, const char *option
)
428 /* NB: pass on internal flag to uci_del_element */
429 bool internal
= ctx
->internal
;
430 struct uci_element
*e
;
434 /* NB: p, section, option validated by uci_lookup */
435 UCI_INTERNAL(uci_lookup
, ctx
, &e
, p
, section
, option
);
437 ctx
->internal
= internal
;
438 return uci_del_element(ctx
, e
);
441 int uci_set(struct uci_context
*ctx
, struct uci_package
*p
, const char *section
, const char *option
, const char *value
, struct uci_element
**result
)
443 /* NB: UCI_INTERNAL use means without history tracking */
444 bool internal
= ctx
->internal
;
445 struct uci_element
*e
= NULL
;
446 struct uci_section
*s
= NULL
;
447 struct uci_option
*o
= NULL
;
450 UCI_ASSERT(ctx
, p
!= NULL
);
451 UCI_ASSERT(ctx
, uci_validate_name(section
));
453 UCI_ASSERT(ctx
, uci_validate_name(option
));
454 UCI_ASSERT(ctx
, value
!= NULL
);
456 UCI_ASSERT(ctx
, uci_validate_str(value
, false));
460 * look up the package, section and option (if set)
461 * if the section/option is to be modified and it is not found
462 * create a new element in the appropriate list
464 e
= uci_lookup_list(&p
->sections
, section
);
468 s
= uci_to_section(e
);
469 if (ctx
->pctx
&& ctx
->pctx
->merge
)
470 ctx
->pctx
->section
= s
;
473 e
= uci_lookup_list(&s
->options
, option
);
476 o
= uci_to_option(e
);
480 * no unknown element was supplied, assume that we can just update
492 ctx
->internal
= internal
;
493 return uci_set_element_value(ctx
, result
, value
);
497 * the entry that we need to update was not found,
498 * check if the search failed prematurely.
499 * this can happen if the package was not found, or if
500 * an option was supplied, but the section wasn't found
502 if (!p
|| (!s
&& option
))
503 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
505 /* now add the missing entry */
506 if (!internal
&& p
->has_history
)
507 uci_add_history(ctx
, &p
->history
, UCI_CMD_CHANGE
, section
, option
, value
);
509 o
= uci_alloc_option(s
, option
, value
);
513 s
= uci_alloc_section(p
, value
, section
);
516 if (ctx
->pctx
&& ctx
->pctx
->merge
)
517 ctx
->pctx
->section
= s
;
523 int uci_unload(struct uci_context
*ctx
, struct uci_package
*p
)
526 UCI_ASSERT(ctx
, p
!= NULL
);
528 uci_free_package(&p
);