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.
18 #include "uci_config.h"
21 * you can use these defines to enable debugging behavior for
22 * apps compiled against libuci:
24 * #define UCI_DEBUG_TYPECAST:
25 * enable uci_element typecast checking at run time
33 #define UCI_CONFDIR "/etc/config"
34 #define UCI_SAVEDIR "/tmp/.uci"
35 #define UCI_FILEMODE 0600
53 struct uci_list *next;
54 struct uci_list *prev;
64 struct uci_parse_context;
68 * uci_parse_tuple: Parse an uci tuple
71 * @package: output package pointer
72 * @section: output section pointer
73 * @option: output option pointer
74 * @value: output value pointer
76 * format: <package>[.<section>[.<option>]][=<value>]
78 extern int uci_parse_tuple(struct uci_context *ctx, char *str, char **package, char **section, char **option, char **value);
81 * uci_alloc_context: Allocate a new uci context
83 extern struct uci_context *uci_alloc_context(void);
86 * uci_free_context: Free the uci context including all of its data
88 extern void uci_free_context(struct uci_context *ctx);
91 * uci_perror: Print the last uci error that occured
93 * @str: string to print before the error message
95 extern void uci_perror(struct uci_context *ctx, const char *str);
98 * uci_import: Import uci config data from a stream
100 * @stream: file stream to import from
101 * @name: (optional) assume the config has the given name
102 * @package: (optional) store the last parsed config package in this variable
103 * @single: ignore the 'package' keyword and parse everything into a single package
105 * the name parameter is for config files that don't explicitly use the 'package <...>' keyword
106 * if 'package' points to a non-null struct pointer, enable history tracking and merge
108 extern int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package, bool single);
111 * uci_export: Export one or all uci config packages
113 * @stream: output stream
114 * @package: (optional) uci config package to export
115 * @header: include the package header
117 extern int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package, bool header);
120 * uci_load: Parse an uci config file and store it in the uci context
123 * @name: name of the config file (relative to the config directory)
124 * @package: store the loaded config package in this variable
126 extern int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package);
129 * uci_unload: Unload a config file from the uci context
132 * @package: pointer to the uci_package struct
134 extern int uci_unload(struct uci_context *ctx, struct uci_package *p);
137 * uci_lookup: Look up an uci element
140 * @res: where to store the result
141 * @package: uci_package struct
142 * @section: config section (optional)
143 * @option: option to search for (optional)
145 * If section is omitted, then a pointer to the config package is returned
146 * If option is omitted, then a pointer to the config section is returned
148 extern int uci_lookup(struct uci_context *ctx, struct uci_element **res, struct uci_package *package, char *section, char *option);
151 * uci_add_section: Add an unnamed section
153 * @p: package to add the section to
154 * @type: section type
155 * @res: pointer to store a reference to the new section in
157 extern int uci_add_section(struct uci_context *ctx, struct uci_package *p, char *type, struct uci_section **res);
160 * uci_set_element_value: Replace an element's value with a new one
162 * @element: pointer to an uci_element struct pointer
165 * Only valid for uci_option and uci_section. Will replace the type string
166 * when used with an uci_section
168 extern int uci_set_element_value(struct uci_context *ctx, struct uci_element **element, char *value);
171 * uci_set: Set an element's value; create the element if necessary
173 * @package: package name
174 * @section: section name
175 * @option: option name
176 * @value: value (option) or type (section)
177 * @result: store the updated element in this variable (optional)
179 extern int uci_set(struct uci_context *ctx, struct uci_package *p, char *section, char *option, char *value, struct uci_element **result);
182 * uci_rename: Rename an element
184 * @package: package name
185 * @section: section name
186 * @option: option name
189 extern int uci_rename(struct uci_context *ctx, struct uci_package *p, char *section, char *option, char *name);
192 * uci_delete_element: Delete a section or option
194 * @e: element (section or option)
196 extern int uci_delete_element(struct uci_context *ctx, struct uci_element *e);
199 * uci_delete: Delete a section or option
202 * @section: section name
203 * @option: option name (optional)
205 extern int uci_delete(struct uci_context *ctx, struct uci_package *p, char *section, char *option);
208 * uci_save: save change history for a package
210 * @p: uci_package struct
212 extern int uci_save(struct uci_context *ctx, struct uci_package *p);
215 * uci_commit: commit changes to a package
217 * @p: uci_package struct pointer
218 * @overwrite: overwrite existing config data and flush history
220 * committing may reload the whole uci_package data,
221 * the supplied pointer is updated accordingly
223 extern int uci_commit(struct uci_context *ctx, struct uci_package **p, bool overwrite);
226 * uci_list_configs: List available uci config files
229 * caller is responsible for freeing the allocated memory behind list
231 extern int uci_list_configs(struct uci_context *ctx, char ***list);
234 * uci_set_savedir: override the default history save directory
236 * @dir: directory name
238 extern int uci_set_savedir(struct uci_context *ctx, const char *dir);
241 * uci_set_savedir: override the default config storage directory
243 * @dir: directory name
245 extern int uci_set_confdir(struct uci_context *ctx, const char *dir);
248 * uci_add_history_path: add a directory to the search path for change history files
250 * @dir: directory name
252 * This function allows you to add directories, which contain 'overlays'
253 * for the active config, that will never be committed.
255 extern int uci_add_history_path(struct uci_context *ctx, const char *dir);
258 * uci_revert: revert all changes to a config item
260 * @p: pointer to a uci_package struct ptr (will be replaced by the revert)
261 * @section: section name (optional)
262 * @option option name (optional)
264 extern int uci_revert(struct uci_context *ctx, struct uci_package **p, char *section, char *option);
267 * uci_parse_argument: parse a shell-style argument, with an arbitrary quoting style
269 * @stream: input stream
270 * @str: pointer to the current line (use NULL for parsing the next line)
271 * @result: pointer for the result
273 extern int uci_parse_argument(struct uci_context *ctx, FILE *stream, char **str, char **result);
276 * uci_set_backend: change the default backend
278 * @name: name of the backend
280 * The default backend is "file", which uses /etc/config for config storage
282 extern int uci_set_backend(struct uci_context *ctx, const char *name);
284 /* UCI data structures */
286 UCI_TYPE_HISTORY = 0,
287 UCI_TYPE_PACKAGE = 1,
288 UCI_TYPE_SECTION = 2,
291 UCI_TYPE_BACKEND = 5,
295 UCI_FLAG_STRICT = (1 << 0), /* strict mode for the parser */
296 UCI_FLAG_PERROR = (1 << 1), /* print parser error messages */
297 UCI_FLAG_EXPORT_NAME = (1 << 2), /* when exporting, name unnamed sections */
298 UCI_FLAG_SAVED_HISTORY = (1 << 3), /* store the saved history in memory as well */
303 struct uci_list list;
310 struct uci_element e;
311 char **(*list_configs)(struct uci_context *ctx);
312 struct uci_package *(*load)(struct uci_context *ctx, const char *name);
313 void (*commit)(struct uci_context *ctx, struct uci_package **p, bool overwrite);
322 /* list of config packages */
323 struct uci_list root;
325 /* parser context, use for error handling only */
326 struct uci_parse_context *pctx;
328 /* backend for import and export */
329 struct uci_backend *backend;
330 struct uci_list backends;
332 /* uci runtime flags */
333 enum uci_flags flags;
338 /* search path for history files */
339 struct uci_list history_path;
352 struct uci_element e;
353 struct uci_list sections;
354 struct uci_context *ctx;
359 struct uci_backend *backend;
362 struct uci_list history;
363 struct uci_list saved_history;
368 struct uci_element e;
369 struct uci_list options;
370 struct uci_package *package;
377 struct uci_element e;
378 struct uci_section *section;
391 struct uci_element e;
392 enum uci_command cmd;
398 /* linked list handling */
400 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
404 * container_of - cast a member of a structure out to the containing structure
405 * @ptr: the pointer to the member.
406 * @type: the type of the container struct this is embedded in.
407 * @member: the name of the member within the struct.
409 #define container_of(ptr, type, member) \
410 ((type *) ((char *)ptr - offsetof(type,member)))
414 * uci_list_entry: casts an uci_list pointer to the containing struct.
415 * @_type: config, section or option
416 * @_ptr: pointer to the uci_list struct
418 #define list_to_element(ptr) \
419 container_of(ptr, struct uci_element, list)
422 * uci_foreach_entry: loop through a list of uci elements
423 * @_list: pointer to the uci_list struct
424 * @_ptr: iteration variable, struct uci_element
426 * use like a for loop, e.g:
427 * uci_foreach(&list, p) {
431 #define uci_foreach_element(_list, _ptr) \
432 for(_ptr = list_to_element((_list)->next); \
433 &_ptr->list != (_list); \
434 _ptr = list_to_element(_ptr->list.next))
437 * uci_foreach_entry_safe: like uci_foreach_safe, but safe for deletion
438 * @_list: pointer to the uci_list struct
439 * @_tmp: temporary variable, struct uci_element *
440 * @_ptr: iteration variable, struct uci_element *
442 * use like a for loop, e.g:
443 * uci_foreach(&list, p) {
447 #define uci_foreach_element_safe(_list, _tmp, _ptr) \
448 for(_ptr = list_to_element((_list)->next), \
449 _tmp = list_to_element(_ptr->list.next); \
450 &_ptr->list != (_list); \
451 _ptr = _tmp, _tmp = list_to_element(_ptr->list.next))
454 * uci_list_empty: returns true if a list is empty
457 #define uci_list_empty(list) ((list)->next == (list))
459 /* wrappers for dynamic type handling */
460 #define uci_type_backend UCI_TYPE_BACKEND
461 #define uci_type_history UCI_TYPE_HISTORY
462 #define uci_type_package UCI_TYPE_PACKAGE
463 #define uci_type_section UCI_TYPE_SECTION
464 #define uci_type_option UCI_TYPE_OPTION
466 /* element typecasting */
467 #ifdef UCI_DEBUG_TYPECAST
468 static const char *uci_typestr[] = {
469 [uci_type_backend] = "backend",
470 [uci_type_history] = "history",
471 [uci_type_package] = "package",
472 [uci_type_section] = "section",
473 [uci_type_option] = "option",
476 static void uci_typecast_error(int from, int to)
478 fprintf(stderr, "Invalid typecast from '%s' to '%s'\n", uci_typestr[from], uci_typestr[to]);
481 #define BUILD_CAST(_type) \
482 static inline struct uci_ ## _type *uci_to_ ## _type (struct uci_element *e) \
484 if (e->type != uci_type_ ## _type) { \
485 uci_typecast_error(e->type, uci_type_ ## _type); \
487 return (struct uci_ ## _type *) e; \
497 #define uci_to_backend(ptr) container_of(ptr, struct uci_backend, e)
498 #define uci_to_history(ptr) container_of(ptr, struct uci_history, e)
499 #define uci_to_package(ptr) container_of(ptr, struct uci_package, e)
500 #define uci_to_section(ptr) container_of(ptr, struct uci_section, e)
501 #define uci_to_option(ptr) container_of(ptr, struct uci_option, e)
505 * uci_alloc_element: allocate a generic uci_element, reserve a buffer and typecast
507 * @type: {package,section,option}
508 * @name: string containing the name of the element
509 * @datasize: additional buffer size to reserve at the end of the struct
511 #define uci_alloc_element(ctx, type, name, datasize) \
512 uci_to_ ## type (uci_alloc_generic(ctx, uci_type_ ## type, name, sizeof(struct uci_ ## type) + datasize))
514 #define uci_dataptr(ptr) \
515 (((char *) ptr) + sizeof(*ptr))