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
;
73 struct uci_parse_option
;
74 struct uci_parse_context
;
78 * uci_alloc_context: Allocate a new uci context
80 extern struct uci_context
*uci_alloc_context(void);
83 * uci_free_context: Free the uci context including all of its data
85 extern void uci_free_context(struct uci_context
*ctx
);
88 * uci_perror: Print the last uci error that occured
90 * @str: string to print before the error message
92 extern void uci_perror(struct uci_context
*ctx
, const char *str
);
95 * uci_geterror: Get an error string for the last uci error
97 * @dest: target pointer for the string
98 * @str: prefix for the error message
100 * Note: string must be freed by the caller
102 extern void uci_get_errorstr(struct uci_context
*ctx
, char **dest
, const char *str
);
105 * uci_import: Import uci config data from a stream
107 * @stream: file stream to import from
108 * @name: (optional) assume the config has the given name
109 * @package: (optional) store the last parsed config package in this variable
110 * @single: ignore the 'package' keyword and parse everything into a single package
112 * the name parameter is for config files that don't explicitly use the 'package <...>' keyword
113 * if 'package' points to a non-null struct pointer, enable delta tracking and merge
115 extern int uci_import(struct uci_context
*ctx
, FILE *stream
, const char *name
, struct uci_package
**package
, bool single
);
118 * uci_export: Export one or all uci config packages
120 * @stream: output stream
121 * @package: (optional) uci config package to export
122 * @header: include the package header
124 extern int uci_export(struct uci_context
*ctx
, FILE *stream
, struct uci_package
*package
, bool header
);
127 * uci_load: Parse an uci config file and store it in the uci context
130 * @name: name of the config file (relative to the config directory)
131 * @package: store the loaded config package in this variable
133 extern int uci_load(struct uci_context
*ctx
, const char *name
, struct uci_package
**package
);
136 * uci_unload: Unload a config file from the uci context
139 * @package: pointer to the uci_package struct
141 extern int uci_unload(struct uci_context
*ctx
, struct uci_package
*p
);
144 * uci_lookup_ptr: Split an uci tuple string and look up an element tree
146 * @ptr: lookup result struct
147 * @str: uci tuple string to look up
148 * @extended: allow extended syntax lookup
150 * if extended is set to true, uci_lookup_ptr supports the following
154 * network.@interface[0].ifname ('ifname' option of the first interface section)
155 * network.@interface[-1] (last interface section)
156 * Note: uci_lookup_ptr will automatically load a config package if necessary
157 * @str must not be constant, as it will be modified and used for the strings inside @ptr,
158 * thus it must also be available as long as @ptr is in use.
160 extern int uci_lookup_ptr(struct uci_context
*ctx
, struct uci_ptr
*ptr
, char *str
, bool extended
);
163 * uci_add_section: Add an unnamed section
165 * @p: package to add the section to
166 * @type: section type
167 * @res: pointer to store a reference to the new section in
169 extern int uci_add_section(struct uci_context
*ctx
, struct uci_package
*p
, const char *type
, struct uci_section
**res
);
172 * uci_set: Set an element's value; create the element if necessary
176 * The updated/created element is stored in ptr->last
178 extern int uci_set(struct uci_context
*ctx
, struct uci_ptr
*ptr
);
181 * uci_add_list: Append a string to an element list
183 * @ptr: uci pointer (with value)
185 * Note: if the given option already contains a string value,
186 * it will be converted to an 1-element-list before appending the next element
188 extern int uci_add_list(struct uci_context
*ctx
, struct uci_ptr
*ptr
);
191 * uci_del_list: Remove a string from an element list
193 * @ptr: uci pointer (with value)
196 extern int uci_del_list(struct uci_context
*ctx
, struct uci_ptr
*ptr
);
199 * uci_reorder: Reposition a section
201 * @s: uci section to reposition
202 * @pos: new position in the section list
204 extern int uci_reorder_section(struct uci_context
*ctx
, struct uci_section
*s
, int pos
);
207 * uci_rename: Rename an element
209 * @ptr: uci pointer (with value)
211 extern int uci_rename(struct uci_context
*ctx
, struct uci_ptr
*ptr
);
214 * uci_delete: Delete a section or option
218 extern int uci_delete(struct uci_context
*ctx
, struct uci_ptr
*ptr
);
221 * uci_save: save change delta for a package
223 * @p: uci_package struct
225 extern int uci_save(struct uci_context
*ctx
, struct uci_package
*p
);
228 * uci_commit: commit changes to a package
230 * @p: uci_package struct pointer
231 * @overwrite: overwrite existing config data and flush delta
233 * committing may reload the whole uci_package data,
234 * the supplied pointer is updated accordingly
236 extern int uci_commit(struct uci_context
*ctx
, struct uci_package
**p
, bool overwrite
);
239 * uci_list_configs: List available uci config files
242 * caller is responsible for freeing the allocated memory behind list
244 extern int uci_list_configs(struct uci_context
*ctx
, char ***list
);
247 * uci_set_savedir: override the default delta save directory
249 * @dir: directory name
251 extern int uci_set_savedir(struct uci_context
*ctx
, const char *dir
);
254 * uci_set_savedir: override the default config storage directory
256 * @dir: directory name
258 extern int uci_set_confdir(struct uci_context
*ctx
, const char *dir
);
261 * uci_add_delta_path: add a directory to the search path for change delta files
263 * @dir: directory name
265 * This function allows you to add directories, which contain 'overlays'
266 * for the active config, that will never be committed.
268 extern int uci_add_delta_path(struct uci_context
*ctx
, const char *dir
);
271 * uci_revert: revert all changes to a config item
275 extern int uci_revert(struct uci_context
*ctx
, struct uci_ptr
*ptr
);
278 * uci_parse_argument: parse a shell-style argument, with an arbitrary quoting style
280 * @stream: input stream
281 * @str: pointer to the current line (use NULL for parsing the next line)
282 * @result: pointer for the result
284 extern int uci_parse_argument(struct uci_context
*ctx
, FILE *stream
, char **str
, char **result
);
287 * uci_set_backend: change the default backend
289 * @name: name of the backend
291 * The default backend is "file", which uses /etc/config for config storage
293 extern int uci_set_backend(struct uci_context
*ctx
, const char *name
);
296 * uci_validate_text: validate a value string for uci options
299 * this function checks whether a given string is acceptable as value
302 extern bool uci_validate_text(const char *str
);
306 * uci_add_hook: add a uci hook
310 * NB: allocated and freed by the caller
312 extern int uci_add_hook(struct uci_context
*ctx
, const struct uci_hook_ops
*ops
);
315 * uci_remove_hook: remove a uci hook
319 extern int uci_remove_hook(struct uci_context
*ctx
, const struct uci_hook_ops
*ops
);
322 * uci_load_plugin: load an uci plugin
324 * @filename: path to the uci plugin
326 * NB: plugin will be unloaded automatically when the context is freed
328 int uci_load_plugin(struct uci_context
*ctx
, const char *filename
);
331 * uci_load_plugins: load all uci plugins from a directory
333 * @pattern: pattern of uci plugin files (optional)
335 * if pattern is NULL, then uci_load_plugins will call uci_load_plugin
336 * for uci_*.so in <prefix>/lib/
338 int uci_load_plugins(struct uci_context
*ctx
, const char *pattern
);
341 * uci_parse_ptr: parse a uci string into a uci_ptr
343 * @ptr: target data structure
344 * @str: string to parse
346 * str is modified by this function
348 int uci_parse_ptr(struct uci_context
*ctx
, struct uci_ptr
*ptr
, char *str
);
351 * uci_lookup_next: lookup a child element
353 * @e: target element pointer
354 * @list: list of elements
355 * @name: name of the child element
357 * if parent is NULL, the function looks up the package with the given name
359 int uci_lookup_next(struct uci_context
*ctx
, struct uci_element
**e
, struct uci_list
*list
, const char *name
);
362 * uci_parse_section: look up a set of options
364 * @opts: list of options to look up
365 * @n_opts: number of options to look up
366 * @tb: array of pointers to found options
368 void uci_parse_section(struct uci_section
*s
, const struct uci_parse_option
*opts
,
369 int n_opts
, struct uci_option
**tb
);
372 * uci_hash_options: build a hash over a list of options
373 * @tb: list of option pointers
374 * @n_opts: number of options
376 uint32_t uci_hash_options(struct uci_option
**tb
, int n_opts
);
379 /* UCI data structures */
383 UCI_TYPE_PACKAGE
= 2,
384 UCI_TYPE_SECTION
= 3,
387 UCI_TYPE_BACKEND
= 6,
393 enum uci_option_type
{
399 UCI_FLAG_STRICT
= (1 << 0), /* strict mode for the parser */
400 UCI_FLAG_PERROR
= (1 << 1), /* print parser error messages */
401 UCI_FLAG_EXPORT_NAME
= (1 << 2), /* when exporting, name unnamed sections */
402 UCI_FLAG_SAVED_DELTA
= (1 << 3), /* store the saved delta in memory as well */
407 struct uci_list list
;
414 struct uci_element e
;
415 char **(*list_configs
)(struct uci_context
*ctx
);
416 struct uci_package
*(*load
)(struct uci_context
*ctx
, const char *name
);
417 void (*commit
)(struct uci_context
*ctx
, struct uci_package
**p
, bool overwrite
);
426 /* list of config packages */
427 struct uci_list root
;
429 /* parser context, use for error handling only */
430 struct uci_parse_context
*pctx
;
432 /* backend for import and export */
433 struct uci_backend
*backend
;
434 struct uci_list backends
;
436 /* uci runtime flags */
437 enum uci_flags flags
;
442 /* search path for delta files */
443 struct uci_list delta_path
;
449 bool internal
, nested
;
453 struct uci_list hooks
;
454 struct uci_list plugins
;
459 struct uci_element e
;
460 struct uci_list sections
;
461 struct uci_context
*ctx
;
466 struct uci_backend
*backend
;
469 struct uci_list delta
;
470 struct uci_list saved_delta
;
475 struct uci_element e
;
476 struct uci_list options
;
477 struct uci_package
*package
;
484 struct uci_element e
;
485 struct uci_section
*section
;
486 enum uci_option_type type
;
488 struct uci_list list
;
505 struct uci_element e
;
506 enum uci_command cmd
;
513 enum uci_type target
;
515 UCI_LOOKUP_DONE
= (1 << 0),
516 UCI_LOOKUP_COMPLETE
= (1 << 1),
517 UCI_LOOKUP_EXTENDED
= (1 << 2),
520 struct uci_package
*p
;
521 struct uci_section
*s
;
522 struct uci_option
*o
;
523 struct uci_element
*last
;
533 void (*load
)(const struct uci_hook_ops
*ops
, struct uci_package
*p
);
534 void (*set
)(const struct uci_hook_ops
*ops
, struct uci_package
*p
, struct uci_delta
*e
);
539 struct uci_element e
;
540 const struct uci_hook_ops
*ops
;
543 struct uci_plugin_ops
545 int (*attach
)(struct uci_context
*ctx
);
546 void (*detach
)(struct uci_context
*ctx
);
551 struct uci_element e
;
552 const struct uci_plugin_ops
*ops
;
556 struct uci_parse_option
{
558 enum uci_option_type type
;
562 /* linked list handling */
564 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
568 * container_of - cast a member of a structure out to the containing structure
569 * @ptr: the pointer to the member.
570 * @type: the type of the container struct this is embedded in.
571 * @member: the name of the member within the struct.
574 #define container_of(ptr, type, member) \
575 ((type *) ((char *)ptr - offsetof(type,member)))
580 * uci_list_entry: casts an uci_list pointer to the containing struct.
581 * @_type: config, section or option
582 * @_ptr: pointer to the uci_list struct
584 #define list_to_element(ptr) \
585 container_of(ptr, struct uci_element, list)
588 * uci_foreach_entry: loop through a list of uci elements
589 * @_list: pointer to the uci_list struct
590 * @_ptr: iteration variable, struct uci_element
592 * use like a for loop, e.g:
593 * uci_foreach(&list, p) {
597 #define uci_foreach_element(_list, _ptr) \
598 for(_ptr = list_to_element((_list)->next); \
599 &_ptr->list != (_list); \
600 _ptr = list_to_element(_ptr->list.next))
603 * uci_foreach_entry_safe: like uci_foreach_safe, but safe for deletion
604 * @_list: pointer to the uci_list struct
605 * @_tmp: temporary variable, struct uci_element *
606 * @_ptr: iteration variable, struct uci_element *
608 * use like a for loop, e.g:
609 * uci_foreach(&list, p) {
613 #define uci_foreach_element_safe(_list, _tmp, _ptr) \
614 for(_ptr = list_to_element((_list)->next), \
615 _tmp = list_to_element(_ptr->list.next); \
616 &_ptr->list != (_list); \
617 _ptr = _tmp, _tmp = list_to_element(_ptr->list.next))
620 * uci_list_empty: returns true if a list is empty
623 #define uci_list_empty(list) ((list)->next == (list))
625 /* wrappers for dynamic type handling */
626 #define uci_type_backend UCI_TYPE_BACKEND
627 #define uci_type_delta UCI_TYPE_DELTA
628 #define uci_type_package UCI_TYPE_PACKAGE
629 #define uci_type_section UCI_TYPE_SECTION
630 #define uci_type_option UCI_TYPE_OPTION
631 #define uci_type_hook UCI_TYPE_HOOK
632 #define uci_type_plugin UCI_TYPE_PLUGIN
634 /* element typecasting */
635 #ifdef UCI_DEBUG_TYPECAST
636 static const char *uci_typestr
[] = {
637 [uci_type_backend
] = "backend",
638 [uci_type_delta
] = "delta",
639 [uci_type_package
] = "package",
640 [uci_type_section
] = "section",
641 [uci_type_option
] = "option",
642 [uci_type_hook
] = "hook",
643 [uci_type_plugin
] = "plugin",
646 static void uci_typecast_error(int from
, int to
)
648 fprintf(stderr
, "Invalid typecast from '%s' to '%s'\n", uci_typestr
[from
], uci_typestr
[to
]);
651 #define BUILD_CAST(_type) \
652 static inline struct uci_ ## _type *uci_to_ ## _type (struct uci_element *e) \
654 if (e->type != uci_type_ ## _type) { \
655 uci_typecast_error(e->type, uci_type_ ## _type); \
657 return (struct uci_ ## _type *) e; \
669 #define uci_to_backend(ptr) container_of(ptr, struct uci_backend, e)
670 #define uci_to_delta(ptr) container_of(ptr, struct uci_delta, e)
671 #define uci_to_package(ptr) container_of(ptr, struct uci_package, e)
672 #define uci_to_section(ptr) container_of(ptr, struct uci_section, e)
673 #define uci_to_option(ptr) container_of(ptr, struct uci_option, e)
674 #define uci_to_hook(ptr) container_of(ptr, struct uci_hook, e)
675 #define uci_to_plugin(ptr) container_of(ptr, struct uci_plugin, e)
679 * uci_alloc_element: allocate a generic uci_element, reserve a buffer and typecast
681 * @type: {package,section,option}
682 * @name: string containing the name of the element
683 * @datasize: additional buffer size to reserve at the end of the struct
685 #define uci_alloc_element(ctx, type, name, datasize) \
686 uci_to_ ## type (uci_alloc_generic(ctx, uci_type_ ## type, name, sizeof(struct uci_ ## type) + datasize))
688 #define uci_dataptr(ptr) \
689 (((char *) ptr) + sizeof(*ptr))
692 * uci_lookup_package: look up a package
694 * @name: name of the package
696 static inline struct uci_package
*
697 uci_lookup_package(struct uci_context
*ctx
, const char *name
)
699 struct uci_element
*e
= NULL
;
700 if (uci_lookup_next(ctx
, &e
, &ctx
->root
, name
) == 0)
701 return uci_to_package(e
);
707 * uci_lookup_section: look up a section
709 * @p: package that the section belongs to
710 * @name: name of the section
712 static inline struct uci_section
*
713 uci_lookup_section(struct uci_context
*ctx
, struct uci_package
*p
, const char *name
)
715 struct uci_element
*e
= NULL
;
716 if (uci_lookup_next(ctx
, &e
, &p
->sections
, name
) == 0)
717 return uci_to_section(e
);
723 * uci_lookup_option: look up an option
725 * @section: section that the option belongs to
726 * @name: name of the option
728 static inline struct uci_option
*
729 uci_lookup_option(struct uci_context
*ctx
, struct uci_section
*s
, const char *name
)
731 struct uci_element
*e
= NULL
;
732 if (uci_lookup_next(ctx
, &e
, &s
->options
, name
) == 0)
733 return uci_to_option(e
);
738 static inline const char *
739 uci_lookup_option_string(struct uci_context
*ctx
, struct uci_section
*s
, const char *name
)
741 struct uci_option
*o
;
743 o
= uci_lookup_option(ctx
, s
, name
);
744 if (!o
|| o
->type
!= UCI_TYPE_STRING
)