066a9fd789008474e25b7aecd476857018756318
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 between two consecutive entries */
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
;
53 static struct uci_element
*
54 uci_alloc_generic(struct uci_context
*ctx
, int type
, const char *name
, int size
)
56 struct uci_element
*e
;
59 ptr
= uci_malloc(ctx
, size
+ strlen(name
) + 1);
60 e
= (struct uci_element
*) ptr
;
62 e
->name
= (char *) ptr
+ size
;
63 strcpy(e
->name
, name
);
64 uci_list_init(&e
->list
);
70 uci_free_element(struct uci_element
*e
)
75 if (!uci_list_empty(&e
->list
))
76 uci_list_del(&e
->list
);
80 static struct uci_option
*
81 uci_alloc_option(struct uci_section
*s
, const char *name
, const char *value
)
83 struct uci_package
*p
= s
->package
;
84 struct uci_context
*ctx
= p
->ctx
;
87 o
= uci_alloc_element(ctx
, option
, name
, strlen(value
) + 1);
88 o
->value
= uci_dataptr(o
);
90 strcpy(o
->value
, value
);
91 uci_list_add(&s
->options
, &o
->e
.list
);
97 uci_free_option(struct uci_option
*o
)
99 uci_free_element(&o
->e
);
102 static struct uci_section
*
103 uci_alloc_section(struct uci_package
*p
, const char *type
, const char *name
)
105 struct uci_context
*ctx
= p
->ctx
;
106 struct uci_section
*s
;
109 if (!name
|| !name
[0]) {
110 snprintf(buf
, 16, "cfg%d", p
->n_section
);
114 s
= uci_alloc_element(ctx
, section
, name
, strlen(type
) + 1);
115 s
->type
= uci_dataptr(s
);
117 strcpy(s
->type
, type
);
118 uci_list_init(&s
->options
);
119 uci_list_add(&p
->sections
, &s
->e
.list
);
125 uci_free_section(struct uci_section
*s
)
127 struct uci_element
*o
, *tmp
;
129 uci_foreach_element_safe(&s
->options
, tmp
, o
) {
130 uci_free_option(uci_to_option(o
));
132 uci_free_element(&s
->e
);
135 static struct uci_package
*
136 uci_alloc_package(struct uci_context
*ctx
, const char *name
)
138 struct uci_package
*p
;
140 p
= uci_alloc_element(ctx
, package
, name
, 0);
142 uci_list_init(&p
->sections
);
147 uci_free_package(struct uci_package
*p
)
149 struct uci_element
*e
, *tmp
;
154 uci_foreach_element_safe(&p
->sections
, tmp
, e
) {
155 uci_free_section(uci_to_section(e
));
157 uci_free_element(&p
->e
);
160 static struct uci_element
*uci_lookup_list(struct uci_context
*ctx
, struct uci_list
*list
, char *name
)
162 struct uci_element
*e
;
164 uci_foreach_element(list
, e
) {
165 if (!strcmp(e
->name
, name
))
168 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
171 int uci_lookup(struct uci_context
*ctx
, struct uci_element
**res
, char *package
, char *section
, char *option
)
173 struct uci_element
*e
;
174 struct uci_package
*p
;
175 struct uci_section
*s
;
176 struct uci_option
*o
;
179 UCI_ASSERT(ctx
, res
!= NULL
);
180 UCI_ASSERT(ctx
, package
!= NULL
);
182 e
= uci_lookup_list(ctx
, &ctx
->root
, package
);
186 p
= uci_to_package(e
);
187 e
= uci_lookup_list(ctx
, &p
->sections
, section
);
191 s
= uci_to_section(e
);
192 e
= uci_lookup_list(ctx
, &s
->options
, option
);
199 int uci_unload(struct uci_context
*ctx
, const char *name
)
201 struct uci_element
*e
;
204 UCI_ASSERT(ctx
, name
!= NULL
);
206 uci_foreach_element(&ctx
->root
, e
) {
207 if (!strcmp(e
->name
, name
))
210 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
213 uci_free_package(uci_to_package(e
));