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
;
63 ptr
= uci_malloc(ctx
, size
+ strlen(name
) + 1);
64 e
= (struct uci_element
*) ptr
;
66 e
->name
= (char *) ptr
+ size
;
67 strcpy(e
->name
, name
);
68 uci_list_init(&e
->list
);
74 uci_free_element(struct uci_element
*e
)
76 if (!uci_list_empty(&e
->list
))
77 uci_list_del(&e
->list
);
81 static struct uci_option
*
82 uci_alloc_option(struct uci_section
*s
, const char *name
, const char *value
)
84 struct uci_package
*p
= s
->package
;
85 struct uci_context
*ctx
= p
->ctx
;
88 o
= uci_alloc_element(ctx
, option
, name
, strlen(value
) + 1);
89 o
->value
= uci_dataptr(o
);
91 strcpy(o
->value
, value
);
92 uci_list_add(&s
->options
, &o
->e
.list
);
98 uci_free_option(struct uci_option
*o
)
100 uci_free_element(&o
->e
);
103 static struct uci_section
*
104 uci_alloc_section(struct uci_package
*p
, const char *type
, const char *name
)
106 struct uci_context
*ctx
= p
->ctx
;
107 struct uci_section
*s
;
110 if (!name
|| !name
[0]) {
111 snprintf(buf
, 16, "cfg%d", p
->n_section
);
115 s
= uci_alloc_element(ctx
, section
, name
, strlen(type
) + 1);
116 s
->type
= uci_dataptr(s
);
118 strcpy(s
->type
, type
);
119 uci_list_init(&s
->options
);
120 uci_list_add(&p
->sections
, &s
->e
.list
);
126 uci_free_section(struct uci_section
*s
)
128 struct uci_element
*o
, *tmp
;
130 uci_foreach_element_safe(&s
->options
, tmp
, o
) {
131 uci_free_option(uci_to_option(o
));
133 uci_free_element(&s
->e
);
136 static struct uci_package
*
137 uci_alloc_package(struct uci_context
*ctx
, const char *name
)
139 struct uci_package
*p
;
141 p
= uci_alloc_element(ctx
, package
, name
, 0);
143 uci_list_init(&p
->sections
);
144 uci_list_init(&p
->history
);
149 uci_free_package(struct uci_package
*p
)
151 struct uci_element
*e
, *tmp
;
158 uci_foreach_element_safe(&p
->sections
, tmp
, e
) {
159 uci_free_section(uci_to_section(e
));
161 uci_free_element(&p
->e
);
164 /* record a change that was done to a package */
166 uci_add_history(struct uci_context
*ctx
, struct uci_package
*p
, int cmd
, char *section
, char *option
, char *value
)
168 struct uci_history
*h
= (struct uci_history
*) uci_malloc(ctx
, sizeof(struct uci_history
));
170 uci_list_init(&h
->list
);
172 h
->section
= section
;
175 uci_list_add(&p
->history
, &h
->list
);
179 static struct uci_element
*uci_lookup_list(struct uci_context
*ctx
, struct uci_list
*list
, const char *name
)
181 struct uci_element
*e
;
183 uci_foreach_element(list
, e
) {
184 if (!strcmp(e
->name
, name
))
190 int uci_lookup(struct uci_context
*ctx
, struct uci_element
**res
, struct uci_package
*p
, char *section
, char *option
)
192 struct uci_element
*e
;
193 struct uci_section
*s
;
194 struct uci_option
*o
;
197 UCI_ASSERT(ctx
, res
!= NULL
);
198 UCI_ASSERT(ctx
, p
!= NULL
);
199 UCI_ASSERT(ctx
, section
!= NULL
);
201 e
= uci_lookup_list(ctx
, &p
->sections
, section
);
206 s
= uci_to_section(e
);
207 e
= uci_lookup_list(ctx
, &s
->options
, option
);
214 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
218 int uci_set_element_value(struct uci_context
*ctx
, struct uci_element
**element
, char *value
)
222 struct uci_list
*list
;
223 struct uci_element
*e
;
226 UCI_ASSERT(ctx
, value
!= NULL
);
227 UCI_ASSERT(ctx
, element
!= NULL
);
228 UCI_ASSERT(ctx
, *element
!= NULL
);
230 /* what the 'value' of an element means depends on the type
231 * for a section, the 'value' means its type
232 * for an option, the 'value' means its value string
233 * when changing the value, shrink the element to its actual size
234 * (it may have been allocated with a bigger size, to include
236 * then duplicate the string passed on the command line and
237 * insert it into the structure.
242 case UCI_TYPE_SECTION
:
243 size
= sizeof(struct uci_section
);
245 case UCI_TYPE_OPTION
:
246 size
= sizeof(struct uci_option
);
249 UCI_THROW(ctx
, UCI_ERR_INVAL
);
253 uci_list_del(&e
->list
);
254 e
= uci_realloc(ctx
, e
, size
);
255 str
= uci_strdup(ctx
, value
);
256 uci_list_insert(list
, &e
->list
);
260 case UCI_TYPE_SECTION
:
261 uci_to_section(e
)->type
= value
;
263 case UCI_TYPE_OPTION
:
264 uci_to_option(e
)->value
= value
;
273 int uci_set(struct uci_context
*ctx
, char *package
, char *section
, char *option
, char *value
)
275 struct uci_element
*e
;
276 struct uci_package
*p
= NULL
;
277 struct uci_section
*s
= NULL
;
278 struct uci_option
*o
= NULL
;
279 struct uci_history
*h
;
282 UCI_ASSERT(ctx
, package
!= NULL
);
283 UCI_ASSERT(ctx
, section
!= NULL
);
286 * look up the package, section and option (if set)
287 * if the section/option is to be modified and it is not found
288 * create a new element in the appropriate list
290 e
= uci_lookup_list(ctx
, &ctx
->root
, package
);
294 p
= uci_to_package(e
);
295 e
= uci_lookup_list(ctx
, &p
->sections
, section
);
299 s
= uci_to_section(e
);
301 e
= uci_lookup_list(ctx
, &s
->options
, option
);
304 o
= uci_to_option(e
);
308 * no unknown element was supplied, assume that we can just update
316 uci_add_history(ctx
, p
, UCI_CMD_CHANGE
, section
, option
, value
);
317 return uci_set_element_value(ctx
, &e
, value
);
321 * the entry that we need to update was not found,
322 * check if the search failed prematurely.
323 * this can happen if the package was not found, or if
324 * an option was supplied, but the section wasn't found
326 if (!p
|| (!s
&& option
))
327 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
329 /* now add the missing entry */
330 uci_add_history(ctx
, p
, UCI_CMD_ADD
, section
, option
, value
);
332 uci_alloc_option(s
, option
, value
);
334 uci_alloc_section(p
, value
, section
);
339 int uci_unload(struct uci_context
*ctx
, struct uci_package
*p
)
341 struct uci_element
*e
;
344 UCI_ASSERT(ctx
, p
!= NULL
);