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 Lesser General Public License for more details.
22 #include "uci_config.h"
25 * you can use these defines to enable debugging behavior for
26 * apps compiled against libuci:
28 * #define UCI_DEBUG_TYPECAST:
29 * enable uci_element typecast checking at run time
38 #define UCI_CONFDIR "/etc/config"
39 #define UCI_SAVEDIR "/tmp/.uci"
40 #define UCI_DIRMODE 0700
41 #define UCI_FILEMODE 0600
59 struct uci_list *next;
60 struct uci_list *prev;
71 struct uci_parse_option;
72 struct uci_parse_context;
76 * uci_alloc_context: Allocate a new uci context
78 extern struct uci_context *uci_alloc_context(void);
81 * uci_free_context: Free the uci context including all of its data
83 extern void uci_free_context(struct uci_context *ctx);
86 * uci_perror: Print the last uci error that occured
88 * @str: string to print before the error message
90 extern void uci_perror(struct uci_context *ctx, const char *str);
93 * uci_geterror: Get an error string for the last uci error
95 * @dest: target pointer for the string
96 * @str: prefix for the error message
98 * Note: string must be freed by the caller
100 extern void uci_get_errorstr(struct uci_context *ctx, char **dest, const char *str);
103 * uci_import: Import uci config data from a stream
105 * @stream: file stream to import from
106 * @name: (optional) assume the config has the given name
107 * @package: (optional) store the last parsed config package in this variable
108 * @single: ignore the 'package' keyword and parse everything into a single package
110 * the name parameter is for config files that don't explicitly use the 'package <...>' keyword
111 * if 'package' points to a non-null struct pointer, enable delta tracking and merge
113 extern int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package, bool single);
116 * uci_export: Export one or all uci config packages
118 * @stream: output stream
119 * @package: (optional) uci config package to export
120 * @header: include the package header
122 extern int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package, bool header);
125 * uci_load: Parse an uci config file and store it in the uci context
128 * @name: name of the config file (relative to the config directory)
129 * @package: store the loaded config package in this variable
131 extern int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package);
134 * uci_unload: Unload a config file from the uci context
137 * @package: pointer to the uci_package struct
139 extern int uci_unload(struct uci_context *ctx, struct uci_package *p);
142 * uci_lookup_ptr: Split an uci tuple string and look up an element tree
144 * @ptr: lookup result struct
145 * @str: uci tuple string to look up
146 * @extended: allow extended syntax lookup
148 * if extended is set to true, uci_lookup_ptr supports the following
152 * network.@interface[0].ifname ('ifname' option of the first interface section)
153 * network.@interface[-1] (last interface section)
154 * Note: uci_lookup_ptr will automatically load a config package if necessary
155 * @str must not be constant, as it will be modified and used for the strings inside @ptr,
156 * thus it must also be available as long as @ptr is in use.
158 * This function returns UCI_ERR_NOTFOUND if the package specified in the tuple
159 * string cannot be found. Otherwise it will return UCI_OK.
161 * Note that failures in looking up other parts, if they are also specfied,
162 * including section and option, will also have a return value UCI_OK but with
163 * ptr->flags * UCI_LOOKUP_COMPLETE not set.
165 extern int uci_lookup_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str, bool extended);
168 * uci_add_section: Add an unnamed section
170 * @p: package to add the section to
171 * @type: section type
172 * @res: pointer to store a reference to the new section in
174 extern int uci_add_section(struct uci_context *ctx, struct uci_package *p, const char *type, struct uci_section **res);
177 * uci_set: Set an element's value; create the element if necessary
181 * The updated/created element is stored in ptr->last
183 extern int uci_set(struct uci_context *ctx, struct uci_ptr *ptr);
186 * uci_add_list: Append a string to an element list
188 * @ptr: uci pointer (with value)
190 * Note: if the given option already contains a string value,
191 * it will be converted to an 1-element-list before appending the next element
193 extern int uci_add_list(struct uci_context *ctx, struct uci_ptr *ptr);
196 * uci_del_list: Remove a string from an element list
198 * @ptr: uci pointer (with value)
201 extern int uci_del_list(struct uci_context *ctx, struct uci_ptr *ptr);
204 * uci_reorder: Reposition a section
206 * @s: uci section to reposition
207 * @pos: new position in the section list
209 extern int uci_reorder_section(struct uci_context *ctx, struct uci_section *s, int pos);
212 * uci_rename: Rename an element
214 * @ptr: uci pointer (with value)
216 extern int uci_rename(struct uci_context *ctx, struct uci_ptr *ptr);
219 * uci_delete: Delete a section or option
223 extern int uci_delete(struct uci_context *ctx, struct uci_ptr *ptr);
226 * uci_save: save change delta for a package
228 * @p: uci_package struct
230 extern int uci_save(struct uci_context *ctx, struct uci_package *p);
233 * uci_commit: commit changes to a package
235 * @p: uci_package struct pointer
236 * @overwrite: overwrite existing config data and flush delta
238 * committing may reload the whole uci_package data,
239 * the supplied pointer is updated accordingly
241 extern int uci_commit(struct uci_context *ctx, struct uci_package **p, bool overwrite);
244 * uci_list_configs: List available uci config files
247 * caller is responsible for freeing the allocated memory behind list
249 extern int uci_list_configs(struct uci_context *ctx, char ***list);
252 * uci_set_savedir: override the default delta save directory
254 * @dir: directory name
256 extern int uci_set_savedir(struct uci_context *ctx, const char *dir);
259 * uci_set_savedir: override the default config storage directory
261 * @dir: directory name
263 extern int uci_set_confdir(struct uci_context *ctx, const char *dir);
266 * uci_add_delta_path: add a directory to the search path for change delta files
268 * @dir: directory name
270 * This function allows you to add directories, which contain 'overlays'
271 * for the active config, that will never be committed.
272 * Caller of this API should ensure that no duplicate entries (including the
273 * default search path, e.g. `UCI_SAVEDIR') should be added.
275 extern int uci_add_delta_path(struct uci_context *ctx, const char *dir);
278 * uci_revert: revert all changes to a config item
282 extern int uci_revert(struct uci_context *ctx, struct uci_ptr *ptr);
285 * uci_parse_argument: parse a shell-style argument, with an arbitrary quoting style
287 * @stream: input stream
288 * @str: pointer to the current line (use NULL for parsing the next line)
289 * @result: pointer for the result
291 extern int uci_parse_argument(struct uci_context *ctx, FILE *stream, char **str, char **result);
294 * uci_set_backend: change the default backend
296 * @name: name of the backend
298 * The default backend is "file", which uses /etc/config for config storage
300 extern int uci_set_backend(struct uci_context *ctx, const char *name);
303 * uci_validate_text: validate a value string for uci options
306 * this function checks whether a given string is acceptable as value
309 extern bool uci_validate_text(const char *str);
312 * uci_parse_ptr: parse a uci string into a uci_ptr
314 * @ptr: target data structure
315 * @str: string to parse
317 * str is modified by this function
319 int uci_parse_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str);
322 * uci_lookup_next: lookup a child element
324 * @e: target element pointer
325 * @list: list of elements
326 * @name: name of the child element
328 * if parent is NULL, the function looks up the package with the given name
330 int uci_lookup_next(struct uci_context *ctx, struct uci_element **e, struct uci_list *list, const char *name);
333 * uci_parse_section: look up a set of options
335 * @opts: list of options to look up
336 * @n_opts: number of options to look up
337 * @tb: array of pointers to found options
339 void uci_parse_section(struct uci_section *s, const struct uci_parse_option *opts,
340 int n_opts, struct uci_option **tb);
343 * uci_hash_options: build a hash over a list of options
344 * @tb: list of option pointers
345 * @n_opts: number of options
347 uint32_t uci_hash_options(struct uci_option **tb, int n_opts);
350 /* UCI data structures */
354 UCI_TYPE_PACKAGE = 2,
355 UCI_TYPE_SECTION = 3,
358 UCI_TYPE_BACKEND = 6,
363 enum uci_option_type {
369 UCI_FLAG_STRICT = (1 << 0), /* strict mode for the parser */
370 UCI_FLAG_PERROR = (1 << 1), /* print parser error messages */
371 UCI_FLAG_EXPORT_NAME = (1 << 2), /* when exporting, name unnamed sections */
372 UCI_FLAG_SAVED_DELTA = (1 << 3), /* store the saved delta in memory as well */
377 struct uci_list list;
384 struct uci_element e;
385 char **(*list_configs)(struct uci_context *ctx);
386 struct uci_package *(*load)(struct uci_context *ctx, const char *name);
387 void (*commit)(struct uci_context *ctx, struct uci_package **p, bool overwrite);
396 /* list of config packages */
397 struct uci_list root;
399 /* parser context, use for error handling only */
400 struct uci_parse_context *pctx;
402 /* backend for import and export */
403 struct uci_backend *backend;
404 struct uci_list backends;
406 /* uci runtime flags */
407 enum uci_flags flags;
412 /* search path for delta files */
413 struct uci_list delta_path;
419 bool internal, nested;
426 struct uci_element e;
427 struct uci_list sections;
428 struct uci_context *ctx;
433 struct uci_backend *backend;
436 struct uci_list delta;
437 struct uci_list saved_delta;
442 struct uci_element e;
443 struct uci_list options;
444 struct uci_package *package;
451 struct uci_element e;
452 struct uci_section *section;
453 enum uci_option_type type;
455 struct uci_list list;
461 * UCI_CMD_ADD is used for anonymous sections or list values
472 __UCI_CMD_LAST = __UCI_CMD_MAX - 1
474 extern char const uci_command_char[];
478 struct uci_element e;
479 enum uci_command cmd;
486 enum uci_type target;
488 UCI_LOOKUP_DONE = (1 << 0),
489 UCI_LOOKUP_COMPLETE = (1 << 1),
490 UCI_LOOKUP_EXTENDED = (1 << 2),
493 struct uci_package *p;
494 struct uci_section *s;
495 struct uci_option *o;
496 struct uci_element *last;
504 struct uci_parse_option {
506 enum uci_option_type type;
510 /* linked list handling */
512 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
516 * container_of - cast a member of a structure out to the containing structure
517 * @ptr: the pointer to the member.
518 * @type: the type of the container struct this is embedded in.
519 * @member: the name of the member within the struct.
522 #define container_of(ptr, type, member) \
523 ((type *) ((char *)ptr - offsetof(type,member)))
528 * uci_list_entry: casts an uci_list pointer to the containing struct.
529 * @_type: config, section or option
530 * @_ptr: pointer to the uci_list struct
532 #define list_to_element(ptr) \
533 container_of(ptr, struct uci_element, list)
536 * uci_foreach_entry: loop through a list of uci elements
537 * @_list: pointer to the uci_list struct
538 * @_ptr: iteration variable, struct uci_element
540 * use like a for loop, e.g:
541 * uci_foreach(&list, p) {
545 #define uci_foreach_element(_list, _ptr) \
546 for(_ptr = list_to_element((_list)->next); \
547 &_ptr->list != (_list); \
548 _ptr = list_to_element(_ptr->list.next))
551 * uci_foreach_entry_safe: like uci_foreach_safe, but safe for deletion
552 * @_list: pointer to the uci_list struct
553 * @_tmp: temporary variable, struct uci_element *
554 * @_ptr: iteration variable, struct uci_element *
556 * use like a for loop, e.g:
557 * uci_foreach(&list, p) {
561 #define uci_foreach_element_safe(_list, _tmp, _ptr) \
562 for(_ptr = list_to_element((_list)->next), \
563 _tmp = list_to_element(_ptr->list.next); \
564 &_ptr->list != (_list); \
565 _ptr = _tmp, _tmp = list_to_element(_ptr->list.next))
568 * uci_list_empty: returns true if a list is empty
571 #define uci_list_empty(list) ((list)->next == (list))
573 /* wrappers for dynamic type handling */
574 #define uci_type_backend UCI_TYPE_BACKEND
575 #define uci_type_delta UCI_TYPE_DELTA
576 #define uci_type_package UCI_TYPE_PACKAGE
577 #define uci_type_section UCI_TYPE_SECTION
578 #define uci_type_option UCI_TYPE_OPTION
580 /* element typecasting */
581 #ifdef UCI_DEBUG_TYPECAST
582 static const char *uci_typestr[] = {
583 [uci_type_backend] = "backend",
584 [uci_type_delta] = "delta",
585 [uci_type_package] = "package",
586 [uci_type_section] = "section",
587 [uci_type_option] = "option",
590 static void uci_typecast_error(int from, int to)
592 fprintf(stderr, "Invalid typecast from '%s' to '%s'\n", uci_typestr[from], uci_typestr[to]);
595 #define BUILD_CAST(_type) \
596 static inline struct uci_ ## _type *uci_to_ ## _type (struct uci_element *e) \
598 if (e->type != uci_type_ ## _type) { \
599 uci_typecast_error(e->type, uci_type_ ## _type); \
601 return (struct uci_ ## _type *) e; \
611 #define uci_to_backend(ptr) container_of(ptr, struct uci_backend, e)
612 #define uci_to_delta(ptr) container_of(ptr, struct uci_delta, e)
613 #define uci_to_package(ptr) container_of(ptr, struct uci_package, e)
614 #define uci_to_section(ptr) container_of(ptr, struct uci_section, e)
615 #define uci_to_option(ptr) container_of(ptr, struct uci_option, e)
619 * uci_alloc_element: allocate a generic uci_element, reserve a buffer and typecast
621 * @type: {package,section,option}
622 * @name: string containing the name of the element
623 * @datasize: additional buffer size to reserve at the end of the struct
625 #define uci_alloc_element(ctx, type, name, datasize) \
626 uci_to_ ## type (uci_alloc_generic(ctx, uci_type_ ## type, name, sizeof(struct uci_ ## type) + datasize))
628 #define uci_dataptr(ptr) \
629 (((char *) ptr) + sizeof(*ptr))
632 * uci_lookup_package: look up a package
634 * @name: name of the package
636 static inline struct uci_package *
637 uci_lookup_package(struct uci_context *ctx, const char *name)
639 struct uci_element *e = NULL;
640 if (uci_lookup_next(ctx, &e, &ctx->root, name) == 0)
641 return uci_to_package(e);
647 * uci_lookup_section: look up a section
649 * @p: package that the section belongs to
650 * @name: name of the section
652 static inline struct uci_section *
653 uci_lookup_section(struct uci_context *ctx, struct uci_package *p, const char *name)
655 struct uci_element *e = NULL;
656 if (uci_lookup_next(ctx, &e, &p->sections, name) == 0)
657 return uci_to_section(e);
663 * uci_lookup_option: look up an option
665 * @section: section that the option belongs to
666 * @name: name of the option
668 static inline struct uci_option *
669 uci_lookup_option(struct uci_context *ctx, struct uci_section *s, const char *name)
671 struct uci_element *e = NULL;
672 if (uci_lookup_next(ctx, &e, &s->options, name) == 0)
673 return uci_to_option(e);
678 static inline const char *
679 uci_lookup_option_string(struct uci_context *ctx, struct uci_section *s, const char *name)
681 struct uci_option *o;
683 o = uci_lookup_option(ctx, s, name);
684 if (!o || o->type != UCI_TYPE_STRING)