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.
19 * you can use these defines to enable debugging behavior for
20 * apps compiled against libuci:
22 * #define UCI_DEBUG_TYPECAST:
23 * enable uci_element typecast checking at run time
31 #define UCI_CONFDIR "/etc/config"
55 struct uci_parse_context;
59 * uci_alloc: Allocate a new uci context
61 extern struct uci_context *uci_alloc(void);
64 * uci_free: Free the uci context including all of its data
66 extern void uci_free(struct uci_context *ctx);
69 * uci_perror: Print the last uci error that occured
71 * @str: string to print before the error message
73 extern void uci_perror(struct uci_context *ctx, const char *str);
76 * uci_import: Import uci config data from a stream
78 * @stream: file stream to import from
79 * @name: (optional) assume the config has the given name
80 * @package: (optional) store the last parsed config package in this variable
82 * the name parameter is for config files that don't explicitly use the 'package <...>' keyword
84 extern int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package);
87 * uci_export: Export one or all uci config packages
89 * @stream: output stream
90 * @package: (optional) uci config package to export
92 extern int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package);
95 * uci_load: Parse an uci config file and store it in the uci context
98 * @name: name of the config file (relative to the config directory)
99 * @package: store the loaded config package in this variable
101 extern int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package);
104 * uci_unload: Unload a config file from the uci context
107 * @name: name of the config file
109 extern int uci_unload(struct uci_context *ctx, const char *name);
112 * uci_cleanup: Clean up after an error
116 extern int uci_cleanup(struct uci_context *ctx);
119 * uci_list_configs: List available uci config files
123 extern char **uci_list_configs(struct uci_context *ctx);
125 /* UCI data structures */
134 struct uci_list list;
141 /* list of config packages */
142 struct uci_list root;
144 /* parser context, use for error handling only */
145 struct uci_parse_context *pctx;
154 struct uci_parse_context
162 struct uci_package *package;
163 struct uci_section *section;
172 struct uci_element e;
173 struct uci_list sections;
174 struct uci_context *ctx;
181 struct uci_element e;
182 struct uci_list options;
183 struct uci_package *package;
189 struct uci_element e;
190 struct uci_section *section;
202 struct uci_list list;
203 enum uci_command cmd;
205 struct uci_element element;
206 struct uci_package package;
207 struct uci_section section;
208 struct uci_option option;
212 /* linked list handling */
214 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
218 * container_of - cast a member of a structure out to the containing structure
219 * @ptr: the pointer to the member.
220 * @type: the type of the container struct this is embedded in.
221 * @member: the name of the member within the struct.
223 #define container_of(ptr, type, member) \
224 ((type *) ((char *)ptr - offsetof(type,member)))
228 * uci_list_entry: casts an uci_list pointer to the containing struct.
229 * @_type: config, section or option
230 * @_ptr: pointer to the uci_list struct
232 #define element_to(type, ptr) \
233 container_of(ptr, struct uci_ ## type, e)
235 #define list_to_element(ptr) \
236 container_of(ptr, struct uci_element, list)
239 * uci_foreach_entry: loop through a list of uci elements
240 * @_list: pointer to the uci_list struct
241 * @_ptr: iteration variable, struct uci_element
243 * use like a for loop, e.g:
244 * uci_foreach(&list, p) {
248 #define uci_foreach_element(_list, _ptr) \
249 for(_ptr = list_to_element((_list)->next); \
250 &_ptr->list != (_list); \
251 _ptr = list_to_element(_ptr->list.next))
254 * uci_foreach_entry_safe: like uci_foreach_safe, but safe for deletion
255 * @_list: pointer to the uci_list struct
256 * @_tmp: temporary variable, struct uci_element *
257 * @_ptr: iteration variable, struct uci_element *
259 * use like a for loop, e.g:
260 * uci_foreach(&list, p) {
264 #define uci_foreach_element_safe(_list, _tmp, _ptr) \
265 for(_ptr = list_to_element((_list)->next), \
266 _tmp = list_to_element(_ptr->list.next); \
267 &_ptr->list != (_list); \
268 _ptr = _tmp, _tmp = list_to_element(_ptr->list.next))
270 /* returns true if a list is empty */
271 #define uci_list_empty(list) ((list)->next == (list))
273 /* element typecasting */
274 #ifdef UCI_DEBUG_TYPECAST
275 static const char *uci_typestr[] = {
276 [UCI_TYPE_PACKAGE] = "package",
277 [UCI_TYPE_SECTION] = "section",
278 [UCI_TYPE_OPTION] = "option"
281 static void uci_typecast_error(int from, int to)
283 fprintf(stderr, "Invalid typecast from '%s' to '%s'\n", uci_typestr[from], uci_typestr[to]);
286 #define BUILD_CAST(type, val) \
287 static inline struct uci_ ## type *uci_to_ ## type (struct uci_element *e) \
289 if (e->type != val) { \
290 uci_typecast_error(e->type, val); \
292 return (struct uci_ ## type *) e; \
295 BUILD_CAST(package, UCI_TYPE_PACKAGE)
296 BUILD_CAST(section, UCI_TYPE_SECTION)
297 BUILD_CAST(option, UCI_TYPE_OPTION)
300 #define uci_to_package(ptr) container_of(ptr, struct uci_package, e)
301 #define uci_to_section(ptr) container_of(ptr, struct uci_section, e)
302 #define uci_to_option(ptr) container_of(ptr, struct uci_option, e)
306 * uci_alloc_element: allocate a generic uci_element, reserve a buffer and typecast
308 * @type: {package,section,option}
309 * @name: string containing the name of the element
310 * @datasize: additional buffer size to reserve at the end of the struct
312 #define uci_alloc_element(ctx, type, name, datasize) \
313 uci_to_ ## type (uci_alloc_generic(ctx, name, sizeof(struct uci_ ## type) + datasize))
315 #define uci_dataptr(ptr) \
316 (((char *) ptr) + sizeof(*ptr))