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_DIRMODE 0700
36 #define UCI_FILEMODE 0600
54 struct uci_list
*next
;
55 struct uci_list
*prev
;
68 struct uci_parse_context
;
72 * uci_alloc_context: Allocate a new uci context
74 extern struct uci_context
*uci_alloc_context(void);
77 * uci_free_context: Free the uci context including all of its data
79 extern void uci_free_context(struct uci_context
*ctx
);
82 * uci_perror: Print the last uci error that occured
84 * @str: string to print before the error message
86 extern void uci_perror(struct uci_context
*ctx
, const char *str
);
89 * uci_geterror: Get an error string for the last uci error
91 * @dest: target pointer for the string
92 * @str: prefix for the error message
94 * Note: string must be freed by the caller
96 extern void uci_get_errorstr(struct uci_context
*ctx
, char **dest
, const char *str
);
99 * uci_import: Import uci config data from a stream
101 * @stream: file stream to import from
102 * @name: (optional) assume the config has the given name
103 * @package: (optional) store the last parsed config package in this variable
104 * @single: ignore the 'package' keyword and parse everything into a single package
106 * the name parameter is for config files that don't explicitly use the 'package <...>' keyword
107 * if 'package' points to a non-null struct pointer, enable history tracking and merge
109 extern int uci_import(struct uci_context
*ctx
, FILE *stream
, const char *name
, struct uci_package
**package
, bool single
);
112 * uci_export: Export one or all uci config packages
114 * @stream: output stream
115 * @package: (optional) uci config package to export
116 * @header: include the package header
118 extern int uci_export(struct uci_context
*ctx
, FILE *stream
, struct uci_package
*package
, bool header
);
121 * uci_load: Parse an uci config file and store it in the uci context
124 * @name: name of the config file (relative to the config directory)
125 * @package: store the loaded config package in this variable
127 extern int uci_load(struct uci_context
*ctx
, const char *name
, struct uci_package
**package
);
130 * uci_unload: Unload a config file from the uci context
133 * @package: pointer to the uci_package struct
135 extern int uci_unload(struct uci_context
*ctx
, struct uci_package
*p
);
138 * uci_lookup_ptr: Split an uci tuple string and look up an element tree
140 * @ptr: lookup result struct
141 * @str: uci tuple string to look up
142 * @extended: allow extended syntax lookup
144 * if extended is set to true, uci_lookup_ptr supports the following
148 * network.@interface[0].ifname ('ifname' option of the first interface section)
149 * network.@interface[-1] (last interface section)
150 * Note: uci_lookup_ext will automatically load a config package if necessary
152 extern int uci_lookup_ptr(struct uci_context
*ctx
, struct uci_ptr
*ptr
, char *str
, bool extended
);
155 * uci_add_section: Add an unnamed section
157 * @p: package to add the section to
158 * @type: section type
159 * @res: pointer to store a reference to the new section in
161 extern int uci_add_section(struct uci_context
*ctx
, struct uci_package
*p
, const char *type
, struct uci_section
**res
);
164 * uci_set: Set an element's value; create the element if necessary
168 * The updated/created element is stored in ptr->last
170 extern int uci_set(struct uci_context
*ctx
, struct uci_ptr
*ptr
);
173 * uci_add_list: Append a string to an element list
175 * @ptr: uci pointer (with value)
177 * Note: if the given option already contains a string value,
178 * it will be converted to an 1-element-list before appending the next element
180 extern int uci_add_list(struct uci_context
*ctx
, struct uci_ptr
*ptr
);
183 * uci_reorder: Reposition a section
185 * @s: uci section to reposition
186 * @pos: new position in the section list
188 extern int uci_reorder_section(struct uci_context
*ctx
, struct uci_section
*s
, int pos
);
191 * uci_rename: Rename an element
193 * @ptr: uci pointer (with value)
195 extern int uci_rename(struct uci_context
*ctx
, struct uci_ptr
*ptr
);
198 * uci_delete: Delete a section or option
202 extern int uci_delete(struct uci_context
*ctx
, struct uci_ptr
*ptr
);
205 * uci_save: save change history for a package
207 * @p: uci_package struct
209 extern int uci_save(struct uci_context
*ctx
, struct uci_package
*p
);
212 * uci_commit: commit changes to a package
214 * @p: uci_package struct pointer
215 * @overwrite: overwrite existing config data and flush history
217 * committing may reload the whole uci_package data,
218 * the supplied pointer is updated accordingly
220 extern int uci_commit(struct uci_context
*ctx
, struct uci_package
**p
, bool overwrite
);
223 * uci_list_configs: List available uci config files
226 * caller is responsible for freeing the allocated memory behind list
228 extern int uci_list_configs(struct uci_context
*ctx
, char ***list
);
231 * uci_set_savedir: override the default history save directory
233 * @dir: directory name
235 extern int uci_set_savedir(struct uci_context
*ctx
, const char *dir
);
238 * uci_set_savedir: override the default config storage directory
240 * @dir: directory name
242 extern int uci_set_confdir(struct uci_context
*ctx
, const char *dir
);
245 * uci_add_history_path: add a directory to the search path for change history files
247 * @dir: directory name
249 * This function allows you to add directories, which contain 'overlays'
250 * for the active config, that will never be committed.
252 extern int uci_add_history_path(struct uci_context
*ctx
, const char *dir
);
255 * uci_revert: revert all changes to a config item
259 extern int uci_revert(struct uci_context
*ctx
, struct uci_ptr
*ptr
);
262 * uci_parse_argument: parse a shell-style argument, with an arbitrary quoting style
264 * @stream: input stream
265 * @str: pointer to the current line (use NULL for parsing the next line)
266 * @result: pointer for the result
268 extern int uci_parse_argument(struct uci_context
*ctx
, FILE *stream
, char **str
, char **result
);
271 * uci_set_backend: change the default backend
273 * @name: name of the backend
275 * The default backend is "file", which uses /etc/config for config storage
277 extern int uci_set_backend(struct uci_context
*ctx
, const char *name
);
280 * uci_validate_text: validate a value string for uci options
283 * this function checks whether a given string is acceptable as value
286 extern bool uci_validate_text(const char *str
);
290 * uci_add_hook: add a uci hook
294 * NB: allocated and freed by the caller
296 extern int uci_add_hook(struct uci_context
*ctx
, const struct uci_hook_ops
*ops
);
299 * uci_remove_hook: remove a uci hook
303 extern int uci_remove_hook(struct uci_context
*ctx
, const struct uci_hook_ops
*ops
);
306 * uci_load_plugin: load an uci plugin
308 * @filename: path to the uci plugin
310 * NB: plugin will be unloaded automatically when the context is freed
312 int uci_load_plugin(struct uci_context
*ctx
, const char *filename
);
315 * uci_load_plugins: load all uci plugins from a directory
317 * @pattern: pattern of uci plugin files (optional)
319 * if pattern is NULL, then uci_load_plugins will call uci_load_plugin
320 * for uci_*.so in <prefix>/lib/
322 int uci_load_plugins(struct uci_context
*ctx
, const char *pattern
);
325 /* UCI data structures */
328 UCI_TYPE_HISTORY
= 1,
329 UCI_TYPE_PACKAGE
= 2,
330 UCI_TYPE_SECTION
= 3,
333 UCI_TYPE_BACKEND
= 6,
339 enum uci_option_type
{
345 UCI_FLAG_STRICT
= (1 << 0), /* strict mode for the parser */
346 UCI_FLAG_PERROR
= (1 << 1), /* print parser error messages */
347 UCI_FLAG_EXPORT_NAME
= (1 << 2), /* when exporting, name unnamed sections */
348 UCI_FLAG_SAVED_HISTORY
= (1 << 3), /* store the saved history in memory as well */
353 struct uci_list list
;
360 struct uci_element e
;
361 char **(*list_configs
)(struct uci_context
*ctx
);
362 struct uci_package
*(*load
)(struct uci_context
*ctx
, const char *name
);
363 void (*commit
)(struct uci_context
*ctx
, struct uci_package
**p
, bool overwrite
);
372 /* list of config packages */
373 struct uci_list root
;
375 /* parser context, use for error handling only */
376 struct uci_parse_context
*pctx
;
378 /* backend for import and export */
379 struct uci_backend
*backend
;
380 struct uci_list backends
;
382 /* uci runtime flags */
383 enum uci_flags flags
;
388 /* search path for history files */
389 struct uci_list history_path
;
395 bool internal
, nested
;
399 struct uci_list hooks
;
400 struct uci_list plugins
;
405 struct uci_element e
;
406 struct uci_list sections
;
407 struct uci_context
*ctx
;
412 struct uci_backend
*backend
;
415 struct uci_list history
;
416 struct uci_list saved_history
;
421 struct uci_element e
;
422 struct uci_list options
;
423 struct uci_package
*package
;
430 struct uci_element e
;
431 struct uci_section
*section
;
432 enum uci_option_type type
;
434 struct uci_list list
;
450 struct uci_element e
;
451 enum uci_command cmd
;
458 enum uci_type target
;
460 UCI_LOOKUP_DONE
= (1 << 0),
461 UCI_LOOKUP_COMPLETE
= (1 << 1),
462 UCI_LOOKUP_EXTENDED
= (1 << 2),
465 struct uci_package
*p
;
466 struct uci_section
*s
;
467 struct uci_option
*o
;
468 struct uci_element
*last
;
478 void (*load
)(const struct uci_hook_ops
*ops
, struct uci_package
*p
);
479 void (*set
)(const struct uci_hook_ops
*ops
, struct uci_package
*p
, struct uci_history
*e
);
484 struct uci_element e
;
485 const struct uci_hook_ops
*ops
;
488 struct uci_plugin_ops
490 int (*attach
)(struct uci_context
*ctx
);
491 void (*detach
)(struct uci_context
*ctx
);
496 struct uci_element e
;
497 const struct uci_plugin_ops
*ops
;
502 /* linked list handling */
504 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
508 * container_of - cast a member of a structure out to the containing structure
509 * @ptr: the pointer to the member.
510 * @type: the type of the container struct this is embedded in.
511 * @member: the name of the member within the struct.
514 #define container_of(ptr, type, member) \
515 ((type *) ((char *)ptr - offsetof(type,member)))
520 * uci_list_entry: casts an uci_list pointer to the containing struct.
521 * @_type: config, section or option
522 * @_ptr: pointer to the uci_list struct
524 #define list_to_element(ptr) \
525 container_of(ptr, struct uci_element, list)
528 * uci_foreach_entry: loop through a list of uci elements
529 * @_list: pointer to the uci_list struct
530 * @_ptr: iteration variable, struct uci_element
532 * use like a for loop, e.g:
533 * uci_foreach(&list, p) {
537 #define uci_foreach_element(_list, _ptr) \
538 for(_ptr = list_to_element((_list)->next); \
539 &_ptr->list != (_list); \
540 _ptr = list_to_element(_ptr->list.next))
543 * uci_foreach_entry_safe: like uci_foreach_safe, but safe for deletion
544 * @_list: pointer to the uci_list struct
545 * @_tmp: temporary variable, struct uci_element *
546 * @_ptr: iteration variable, struct uci_element *
548 * use like a for loop, e.g:
549 * uci_foreach(&list, p) {
553 #define uci_foreach_element_safe(_list, _tmp, _ptr) \
554 for(_ptr = list_to_element((_list)->next), \
555 _tmp = list_to_element(_ptr->list.next); \
556 &_ptr->list != (_list); \
557 _ptr = _tmp, _tmp = list_to_element(_ptr->list.next))
560 * uci_list_empty: returns true if a list is empty
563 #define uci_list_empty(list) ((list)->next == (list))
565 /* wrappers for dynamic type handling */
566 #define uci_type_backend UCI_TYPE_BACKEND
567 #define uci_type_history UCI_TYPE_HISTORY
568 #define uci_type_package UCI_TYPE_PACKAGE
569 #define uci_type_section UCI_TYPE_SECTION
570 #define uci_type_option UCI_TYPE_OPTION
571 #define uci_type_hook UCI_TYPE_HOOK
572 #define uci_type_plugin UCI_TYPE_PLUGIN
574 /* element typecasting */
575 #ifdef UCI_DEBUG_TYPECAST
576 static const char *uci_typestr
[] = {
577 [uci_type_backend
] = "backend",
578 [uci_type_history
] = "history",
579 [uci_type_package
] = "package",
580 [uci_type_section
] = "section",
581 [uci_type_option
] = "option",
582 [uci_type_hook
] = "hook",
583 [uci_type_plugin
] = "plugin",
586 static void uci_typecast_error(int from
, int to
)
588 fprintf(stderr
, "Invalid typecast from '%s' to '%s'\n", uci_typestr
[from
], uci_typestr
[to
]);
591 #define BUILD_CAST(_type) \
592 static inline struct uci_ ## _type *uci_to_ ## _type (struct uci_element *e) \
594 if (e->type != uci_type_ ## _type) { \
595 uci_typecast_error(e->type, uci_type_ ## _type); \
597 return (struct uci_ ## _type *) e; \
609 #define uci_to_backend(ptr) container_of(ptr, struct uci_backend, e)
610 #define uci_to_history(ptr) container_of(ptr, struct uci_history, e)
611 #define uci_to_package(ptr) container_of(ptr, struct uci_package, e)
612 #define uci_to_section(ptr) container_of(ptr, struct uci_section, e)
613 #define uci_to_option(ptr) container_of(ptr, struct uci_option, e)
614 #define uci_to_hook(ptr) container_of(ptr, struct uci_hook, e)
615 #define uci_to_plugin(ptr) container_of(ptr, struct uci_plugin, 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))