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->errno);
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 static struct uci_section *
119 uci_alloc_section(struct uci_package *p, const char *type, const char *name)
121 struct uci_context *ctx = p->ctx;
122 struct uci_section *s;
124 if (name && !name[0])
127 s = uci_alloc_element(ctx, section, name, strlen(type) + 1);
128 uci_list_init(&s->options);
129 s->type = uci_dataptr(s);
131 strcpy(s->type, type);
135 uci_list_add(&p->sections, &s->e.list);
141 uci_free_section(struct uci_section *s)
143 struct uci_element *o, *tmp;
145 uci_foreach_element_safe(&s->options, tmp, o) {
146 uci_free_option(uci_to_option(o));
148 if ((s->type != uci_dataptr(s)) &&
151 uci_free_element(&s->e);
154 /* record a change that was done to a package */
156 uci_add_history(struct uci_context *ctx, struct uci_package *p, int cmd, char *section, char *option, char *value)
158 struct uci_history *h;
159 int size = strlen(section) + 1;
166 size += strlen(section) + 1;
168 h = uci_alloc_element(ctx, history, option, size);
169 ptr = uci_dataptr(h);
171 h->section = strcpy(ptr, section);
173 ptr += strlen(ptr) + 1;
174 h->value = strcpy(ptr, value);
176 uci_list_add(&p->history, &h->e.list);
180 uci_free_history(struct uci_history *h)
184 if ((h->section != NULL) &&
185 (h->section != uci_dataptr(h))) {
189 uci_free_element(&h->e);
192 static struct uci_package *
193 uci_alloc_package(struct uci_context *ctx, const char *name)
195 struct uci_package *p;
197 p = uci_alloc_element(ctx, package, name, 0);
199 uci_list_init(&p->sections);
200 uci_list_init(&p->history);
205 uci_free_package(struct uci_package **package)
207 struct uci_element *e, *tmp;
208 struct uci_package *p = *package;
215 uci_foreach_element_safe(&p->sections, tmp, e) {
216 uci_free_section(uci_to_section(e));
218 uci_foreach_element_safe(&p->history, tmp, e) {
219 uci_free_history(uci_to_history(e));
221 uci_free_element(&p->e);
225 static struct uci_element *uci_lookup_list(struct uci_context *ctx, struct uci_list *list, const char *name)
227 struct uci_element *e;
229 uci_foreach_element(list, e) {
230 if (!strcmp(e->name, name))
236 int uci_lookup(struct uci_context *ctx, struct uci_element **res, struct uci_package *p, char *section, char *option)
238 struct uci_element *e;
239 struct uci_section *s;
240 struct uci_option *o;
243 UCI_ASSERT(ctx, res != NULL);
244 UCI_ASSERT(ctx, p != NULL);
245 UCI_ASSERT(ctx, section != NULL);
247 e = uci_lookup_list(ctx, &p->sections, section);
252 s = uci_to_section(e);
253 e = uci_lookup_list(ctx, &s->options, option);
260 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
264 int uci_del_element(struct uci_context *ctx, struct uci_element *e)
266 /* NB: UCI_INTERNAL use means without history tracking */
267 bool internal = ctx->internal;
268 struct uci_package *p = NULL;
269 struct uci_section *s = NULL;
270 struct uci_option *o = NULL;
271 struct uci_element *i, *tmp;
275 UCI_ASSERT(ctx, e != NULL);
278 case UCI_TYPE_SECTION:
279 s = uci_to_section(e);
280 uci_foreach_element_safe(&s->options, tmp, i) {
281 uci_del_element(ctx, i);
284 case UCI_TYPE_OPTION:
285 o = uci_to_option(e);
291 UCI_THROW(ctx, UCI_ERR_INVAL);
297 uci_add_history(ctx, p, UCI_CMD_REMOVE, s->e.name, option, NULL);
300 case UCI_TYPE_SECTION:
303 case UCI_TYPE_OPTION:
312 int uci_set_element_value(struct uci_context *ctx, struct uci_element **element, char *value)
314 /* NB: UCI_INTERNAL use means without history tracking */
315 bool internal = ctx->internal;
316 struct uci_list *list;
317 struct uci_element *e;
318 struct uci_package *p;
319 struct uci_section *s;
326 UCI_ASSERT(ctx, value != NULL);
327 UCI_ASSERT(ctx, element != NULL);
328 UCI_ASSERT(ctx, *element != NULL);
330 /* what the 'value' of an element means depends on the type
331 * for a section, the 'value' means its type
332 * for an option, the 'value' means its value string
333 * when changing the value, shrink the element to its actual size
334 * (it may have been allocated with a bigger size, to include
336 * then duplicate the string passed on the command line and
337 * insert it into the structure.
342 case UCI_TYPE_SECTION:
343 size = sizeof(struct uci_section);
344 s = uci_to_section(e);
348 case UCI_TYPE_OPTION:
349 size = sizeof(struct uci_option);
350 s = uci_to_option(e)->section;
355 UCI_THROW(ctx, UCI_ERR_INVAL);
360 uci_add_history(ctx, p, UCI_CMD_CHANGE, section, option, value);
362 uci_list_del(&e->list);
363 e = uci_realloc(ctx, e, size);
364 str = uci_strdup(ctx, value);
365 uci_list_insert(list, &e->list);
369 case UCI_TYPE_SECTION:
370 uci_to_section(e)->type = str;
372 case UCI_TYPE_OPTION:
373 uci_to_option(e)->value = str;
382 int uci_rename(struct uci_context *ctx, struct uci_package *p, char *section, char *option, char *name)
384 /* NB: UCI_INTERNAL use means without history tracking */
385 bool internal = ctx->internal;
386 struct uci_element *e;
387 struct uci_section *s = NULL;
388 struct uci_option *o = NULL;
391 UCI_ASSERT(ctx, p != NULL);
392 UCI_ASSERT(ctx, section != NULL);
394 UCI_INTERNAL(uci_lookup, ctx, &e, p, section, option);
397 uci_add_history(ctx, p, UCI_CMD_RENAME, section, option, name);
399 name = uci_strdup(ctx, name);
408 int uci_delete(struct uci_context *ctx, struct uci_package *p, char *section, char *option)
410 /* NB: pass on internal flag to uci_del_element */
411 bool internal = ctx->internal;
412 struct uci_element *e;
413 struct uci_section *s = NULL;
414 struct uci_option *o = NULL;
417 UCI_ASSERT(ctx, p != NULL);
418 UCI_ASSERT(ctx, section != NULL);
420 UCI_INTERNAL(uci_lookup, ctx, &e, p, section, option);
423 return uci_del_element(ctx, e);
424 UCI_INTERNAL(uci_del_element, ctx, e);
429 int uci_set(struct uci_context *ctx, struct uci_package *p, char *section, char *option, char *value)
431 /* NB: UCI_INTERNAL use means without history tracking */
432 bool internal = ctx->internal;
433 struct uci_element *e = NULL;
434 struct uci_section *s = NULL;
435 struct uci_option *o = NULL;
436 struct uci_history *h;
439 UCI_ASSERT(ctx, p != NULL);
440 UCI_ASSERT(ctx, section != NULL);
441 UCI_ASSERT(ctx, value != NULL);
444 * look up the package, section and option (if set)
445 * if the section/option is to be modified and it is not found
446 * create a new element in the appropriate list
448 e = uci_lookup_list(ctx, &p->sections, section);
452 s = uci_to_section(e);
454 e = uci_lookup_list(ctx, &s->options, option);
457 o = uci_to_option(e);
458 } else if (internal && ctx->pctx) {
459 ctx->pctx->section = s;
463 * no unknown element was supplied, assume that we can just update
472 return uci_set_element_value(ctx, &e, value);
474 UCI_INTERNAL(uci_set_element_value, ctx, &e, value);
479 * the entry that we need to update was not found,
480 * check if the search failed prematurely.
481 * this can happen if the package was not found, or if
482 * an option was supplied, but the section wasn't found
484 if (!p || (!s && option))
485 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
487 /* now add the missing entry */
489 uci_add_history(ctx, p, UCI_CMD_ADD, section, option, value);
491 uci_alloc_option(s, option, value);
493 uci_alloc_section(p, value, section);
498 int uci_unload(struct uci_context *ctx, struct uci_package *p)
500 struct uci_element *e;
503 UCI_ASSERT(ctx, p != NULL);
505 uci_free_package(&p);