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;
65 datalen += strlen(name) + 1;
66 ptr = uci_malloc(ctx, datalen);
67 e = (struct uci_element *) ptr;
70 e->name = (char *) ptr + size;
71 strcpy(e->name, name);
73 uci_list_init(&e->list);
79 uci_free_element(struct uci_element *e)
81 if (!uci_list_empty(&e->list))
82 uci_list_del(&e->list);
86 static struct uci_option *
87 uci_alloc_option(struct uci_section *s, const char *name, const char *value)
89 struct uci_package *p = s->package;
90 struct uci_context *ctx = p->ctx;
93 o = uci_alloc_element(ctx, option, name, strlen(value) + 1);
94 o->value = uci_dataptr(o);
96 strcpy(o->value, value);
97 uci_list_add(&s->options, &o->e.list);
103 uci_free_option(struct uci_option *o)
105 uci_free_element(&o->e);
108 static struct uci_section *
109 uci_alloc_section(struct uci_package *p, const char *type, const char *name)
111 struct uci_context *ctx = p->ctx;
112 struct uci_section *s;
115 if (!name || !name[0]) {
116 snprintf(buf, 16, "cfg%d", p->n_section);
120 s = uci_alloc_element(ctx, section, name, strlen(type) + 1);
121 s->type = uci_dataptr(s);
123 strcpy(s->type, type);
124 uci_list_init(&s->options);
125 uci_list_add(&p->sections, &s->e.list);
131 uci_free_section(struct uci_section *s)
133 struct uci_element *o, *tmp;
135 uci_foreach_element_safe(&s->options, tmp, o) {
136 uci_free_option(uci_to_option(o));
138 uci_free_element(&s->e);
141 static struct uci_package *
142 uci_alloc_package(struct uci_context *ctx, const char *name)
144 struct uci_package *p;
146 p = uci_alloc_element(ctx, package, name, 0);
148 uci_list_init(&p->sections);
149 uci_list_init(&p->history);
154 uci_free_package(struct uci_package *p)
156 struct uci_element *e, *tmp;
163 uci_foreach_element_safe(&p->sections, tmp, e) {
164 uci_free_section(uci_to_section(e));
166 uci_free_element(&p->e);
169 /* record a change that was done to a package */
171 uci_add_history(struct uci_context *ctx, struct uci_package *p, int cmd, char *section, char *option, char *value)
173 struct uci_history *h;
177 h = uci_alloc_element(ctx, history, option, strlen(section) + strlen(value) + 2);
178 ptr = uci_dataptr(h);
180 h->section = strcpy(ptr, section);
181 ptr += strlen(ptr) + 1;
182 h->value = strcpy(ptr, value);
183 uci_list_add(&p->history, &h->e.list);
186 static struct uci_element *uci_lookup_list(struct uci_context *ctx, struct uci_list *list, const char *name)
188 struct uci_element *e;
190 uci_foreach_element(list, e) {
191 if (!strcmp(e->name, name))
197 int uci_lookup(struct uci_context *ctx, struct uci_element **res, struct uci_package *p, char *section, char *option)
199 struct uci_element *e;
200 struct uci_section *s;
201 struct uci_option *o;
204 UCI_ASSERT(ctx, res != NULL);
205 UCI_ASSERT(ctx, p != NULL);
206 UCI_ASSERT(ctx, section != NULL);
208 e = uci_lookup_list(ctx, &p->sections, section);
213 s = uci_to_section(e);
214 e = uci_lookup_list(ctx, &s->options, option);
221 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
225 int uci_set_element_value(struct uci_context *ctx, struct uci_element **element, char *value)
227 bool internal = ctx->internal;
228 struct uci_list *list;
229 struct uci_element *e;
230 struct uci_package *p;
231 struct uci_section *s;
238 UCI_ASSERT(ctx, value != NULL);
239 UCI_ASSERT(ctx, element != NULL);
240 UCI_ASSERT(ctx, *element != NULL);
242 /* what the 'value' of an element means depends on the type
243 * for a section, the 'value' means its type
244 * for an option, the 'value' means its value string
245 * when changing the value, shrink the element to its actual size
246 * (it may have been allocated with a bigger size, to include
248 * then duplicate the string passed on the command line and
249 * insert it into the structure.
254 case UCI_TYPE_SECTION:
255 size = sizeof(struct uci_section);
256 s = uci_to_section(e);
260 case UCI_TYPE_OPTION:
261 size = sizeof(struct uci_option);
262 s = uci_to_option(e)->section;
267 UCI_THROW(ctx, UCI_ERR_INVAL);
272 uci_add_history(ctx, p, UCI_CMD_CHANGE, section, option, value);
274 uci_list_del(&e->list);
275 e = uci_realloc(ctx, e, size);
276 str = uci_strdup(ctx, value);
277 uci_list_insert(list, &e->list);
281 case UCI_TYPE_SECTION:
282 uci_to_section(e)->type = str;
284 case UCI_TYPE_OPTION:
285 uci_to_option(e)->value = str;
294 int uci_set(struct uci_context *ctx, char *package, char *section, char *option, char *value)
296 struct uci_element *e;
297 struct uci_package *p = NULL;
298 struct uci_section *s = NULL;
299 struct uci_option *o = NULL;
300 struct uci_history *h;
301 bool internal = ctx->internal;
304 UCI_ASSERT(ctx, package != NULL);
305 UCI_ASSERT(ctx, section != NULL);
306 UCI_ASSERT(ctx, value != NULL);
309 * look up the package, section and option (if set)
310 * if the section/option is to be modified and it is not found
311 * create a new element in the appropriate list
313 e = uci_lookup_list(ctx, &ctx->root, package);
317 p = uci_to_package(e);
318 e = uci_lookup_list(ctx, &p->sections, section);
322 s = uci_to_section(e);
324 e = uci_lookup_list(ctx, &s->options, option);
327 o = uci_to_option(e);
331 * no unknown element was supplied, assume that we can just update
340 return uci_set_element_value(ctx, &e, value);
342 UCI_INTERNAL(uci_set_element_value, ctx, &e, value);
347 * the entry that we need to update was not found,
348 * check if the search failed prematurely.
349 * this can happen if the package was not found, or if
350 * an option was supplied, but the section wasn't found
352 if (!p || (!s && option))
353 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
355 /* now add the missing entry */
357 uci_add_history(ctx, p, UCI_CMD_ADD, section, option, value);
359 uci_alloc_option(s, option, value);
361 uci_alloc_section(p, value, section);
366 int uci_unload(struct uci_context *ctx, struct uci_package *p)
368 struct uci_element *e;
371 UCI_ASSERT(ctx, p != NULL);