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 if ((o
->value
!= uci_dataptr(o
)) &&
108 uci_free_element(&o
->e
);
111 static struct uci_section
*
112 uci_alloc_section(struct uci_package
*p
, const char *type
, const char *name
)
114 struct uci_context
*ctx
= p
->ctx
;
115 struct uci_section
*s
;
118 if (!name
|| !name
[0]) {
119 snprintf(buf
, 16, "cfg%d", p
->n_section
);
123 s
= uci_alloc_element(ctx
, section
, name
, strlen(type
) + 1);
124 s
->type
= uci_dataptr(s
);
126 strcpy(s
->type
, type
);
127 uci_list_init(&s
->options
);
128 uci_list_add(&p
->sections
, &s
->e
.list
);
134 uci_free_section(struct uci_section
*s
)
136 struct uci_element
*o
, *tmp
;
138 uci_foreach_element_safe(&s
->options
, tmp
, o
) {
139 uci_free_option(uci_to_option(o
));
141 if ((s
->type
!= uci_dataptr(s
)) &&
144 uci_free_element(&s
->e
);
147 static struct uci_package
*
148 uci_alloc_package(struct uci_context
*ctx
, const char *name
)
150 struct uci_package
*p
;
152 p
= uci_alloc_element(ctx
, package
, name
, 0);
154 uci_list_init(&p
->sections
);
155 uci_list_init(&p
->history
);
160 uci_free_package(struct uci_package
*p
)
162 struct uci_element
*e
, *tmp
;
169 uci_foreach_element_safe(&p
->sections
, tmp
, e
) {
170 uci_free_section(uci_to_section(e
));
172 uci_free_element(&p
->e
);
175 /* record a change that was done to a package */
177 uci_add_history(struct uci_context
*ctx
, struct uci_package
*p
, int cmd
, char *section
, char *option
, char *value
)
179 struct uci_history
*h
;
180 int size
= strlen(section
) + 1;
187 size
+= strlen(section
) + 1;
189 h
= uci_alloc_element(ctx
, history
, option
, size
);
190 ptr
= uci_dataptr(h
);
192 h
->section
= strcpy(ptr
, section
);
194 ptr
+= strlen(ptr
) + 1;
195 h
->value
= strcpy(ptr
, value
);
197 uci_list_add(&p
->history
, &h
->e
.list
);
200 static struct uci_element
*uci_lookup_list(struct uci_context
*ctx
, struct uci_list
*list
, const char *name
)
202 struct uci_element
*e
;
204 uci_foreach_element(list
, e
) {
205 if (!strcmp(e
->name
, name
))
211 int uci_lookup(struct uci_context
*ctx
, struct uci_element
**res
, struct uci_package
*p
, char *section
, char *option
)
213 struct uci_element
*e
;
214 struct uci_section
*s
;
215 struct uci_option
*o
;
218 UCI_ASSERT(ctx
, res
!= NULL
);
219 UCI_ASSERT(ctx
, p
!= NULL
);
220 UCI_ASSERT(ctx
, section
!= NULL
);
222 e
= uci_lookup_list(ctx
, &p
->sections
, section
);
227 s
= uci_to_section(e
);
228 e
= uci_lookup_list(ctx
, &s
->options
, option
);
235 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
239 int uci_del_element(struct uci_context
*ctx
, struct uci_element
*e
)
241 bool internal
= ctx
->internal
;
242 struct uci_package
*p
= NULL
;
243 struct uci_section
*s
= NULL
;
244 struct uci_option
*o
= NULL
;
245 struct uci_element
*i
, *tmp
;
249 UCI_ASSERT(ctx
, e
!= NULL
);
252 case UCI_TYPE_SECTION
:
253 s
= uci_to_section(e
);
254 uci_foreach_element_safe(&s
->options
, tmp
, i
) {
255 uci_del_element(ctx
, i
);
258 case UCI_TYPE_OPTION
:
259 o
= uci_to_option(e
);
265 UCI_THROW(ctx
, UCI_ERR_INVAL
);
271 uci_add_history(ctx
, p
, UCI_CMD_REMOVE
, s
->e
.name
, option
, NULL
);
274 case UCI_TYPE_SECTION
:
277 case UCI_TYPE_OPTION
:
286 int uci_set_element_value(struct uci_context
*ctx
, struct uci_element
**element
, char *value
)
288 bool internal
= ctx
->internal
;
289 struct uci_list
*list
;
290 struct uci_element
*e
;
291 struct uci_package
*p
;
292 struct uci_section
*s
;
299 UCI_ASSERT(ctx
, value
!= NULL
);
300 UCI_ASSERT(ctx
, element
!= NULL
);
301 UCI_ASSERT(ctx
, *element
!= NULL
);
303 /* what the 'value' of an element means depends on the type
304 * for a section, the 'value' means its type
305 * for an option, the 'value' means its value string
306 * when changing the value, shrink the element to its actual size
307 * (it may have been allocated with a bigger size, to include
309 * then duplicate the string passed on the command line and
310 * insert it into the structure.
315 case UCI_TYPE_SECTION
:
316 size
= sizeof(struct uci_section
);
317 s
= uci_to_section(e
);
321 case UCI_TYPE_OPTION
:
322 size
= sizeof(struct uci_option
);
323 s
= uci_to_option(e
)->section
;
328 UCI_THROW(ctx
, UCI_ERR_INVAL
);
333 uci_add_history(ctx
, p
, UCI_CMD_CHANGE
, section
, option
, value
);
335 uci_list_del(&e
->list
);
336 e
= uci_realloc(ctx
, e
, size
);
337 str
= uci_strdup(ctx
, value
);
338 uci_list_insert(list
, &e
->list
);
342 case UCI_TYPE_SECTION
:
343 uci_to_section(e
)->type
= str
;
345 case UCI_TYPE_OPTION
:
346 uci_to_option(e
)->value
= str
;
355 int uci_del(struct uci_context
*ctx
, struct uci_package
*p
, char *section
, char *option
)
357 bool internal
= ctx
->internal
;
358 struct uci_element
*e
;
359 struct uci_section
*s
= NULL
;
360 struct uci_option
*o
= NULL
;
363 UCI_ASSERT(ctx
, p
!= NULL
);
364 UCI_ASSERT(ctx
, section
!= NULL
);
366 UCI_INTERNAL(uci_lookup
, ctx
, &e
, p
, section
, option
);
369 return uci_del_element(ctx
, e
);
370 UCI_INTERNAL(uci_del_element
, ctx
, e
);
375 int uci_set(struct uci_context
*ctx
, struct uci_package
*p
, char *section
, char *option
, char *value
)
377 bool internal
= ctx
->internal
;
378 struct uci_element
*e
= NULL
;
379 struct uci_section
*s
= NULL
;
380 struct uci_option
*o
= NULL
;
381 struct uci_history
*h
;
384 UCI_ASSERT(ctx
, p
!= NULL
);
385 UCI_ASSERT(ctx
, section
!= NULL
);
386 UCI_ASSERT(ctx
, value
!= NULL
);
389 * look up the package, section and option (if set)
390 * if the section/option is to be modified and it is not found
391 * create a new element in the appropriate list
393 UCI_INTERNAL(uci_lookup
, ctx
, &e
, p
, section
, NULL
);
394 s
= uci_to_section(e
);
396 e
= uci_lookup_list(ctx
, &s
->options
, option
);
399 o
= uci_to_option(e
);
403 * no unknown element was supplied, assume that we can just update
412 return uci_set_element_value(ctx
, &e
, value
);
414 UCI_INTERNAL(uci_set_element_value
, ctx
, &e
, value
);
419 * the entry that we need to update was not found,
420 * check if the search failed prematurely.
421 * this can happen if the package was not found, or if
422 * an option was supplied, but the section wasn't found
424 if (!p
|| (!s
&& option
))
425 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
427 /* now add the missing entry */
429 uci_add_history(ctx
, p
, UCI_CMD_ADD
, section
, option
, value
);
431 uci_alloc_option(s
, option
, value
);
433 uci_alloc_section(p
, value
, section
);
438 int uci_unload(struct uci_context
*ctx
, struct uci_package
*p
)
440 struct uci_element
*e
;
443 UCI_ASSERT(ctx
, p
!= NULL
);