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 delta 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_ptr will automatically load a config package if necessary
151 * @str must not be constant, as it will be modified and used for the strings inside @ptr,
152 * thus it must also be available as long as @ptr is in use.
154 extern int uci_lookup_ptr(struct uci_context
*ctx
, struct uci_ptr
*ptr
, char *str
, bool extended
);
157 * uci_add_section: Add an unnamed section
159 * @p: package to add the section to
160 * @type: section type
161 * @res: pointer to store a reference to the new section in
163 extern int uci_add_section(struct uci_context
*ctx
, struct uci_package
*p
, const char *type
, struct uci_section
**res
);
166 * uci_set: Set an element's value; create the element if necessary
170 * The updated/created element is stored in ptr->last
172 extern int uci_set(struct uci_context
*ctx
, struct uci_ptr
*ptr
);
175 * uci_add_list: Append a string to an element list
177 * @ptr: uci pointer (with value)
179 * Note: if the given option already contains a string value,
180 * it will be converted to an 1-element-list before appending the next element
182 extern int uci_add_list(struct uci_context
*ctx
, struct uci_ptr
*ptr
);
185 * uci_reorder: Reposition a section
187 * @s: uci section to reposition
188 * @pos: new position in the section list
190 extern int uci_reorder_section(struct uci_context
*ctx
, struct uci_section
*s
, int pos
);
193 * uci_rename: Rename an element
195 * @ptr: uci pointer (with value)
197 extern int uci_rename(struct uci_context
*ctx
, struct uci_ptr
*ptr
);
200 * uci_delete: Delete a section or option
204 extern int uci_delete(struct uci_context
*ctx
, struct uci_ptr
*ptr
);
207 * uci_save: save change delta for a package
209 * @p: uci_package struct
211 extern int uci_save(struct uci_context
*ctx
, struct uci_package
*p
);
214 * uci_commit: commit changes to a package
216 * @p: uci_package struct pointer
217 * @overwrite: overwrite existing config data and flush delta
219 * committing may reload the whole uci_package data,
220 * the supplied pointer is updated accordingly
222 extern int uci_commit(struct uci_context
*ctx
, struct uci_package
**p
, bool overwrite
);
225 * uci_list_configs: List available uci config files
228 * caller is responsible for freeing the allocated memory behind list
230 extern int uci_list_configs(struct uci_context
*ctx
, char ***list
);
233 * uci_set_savedir: override the default delta save directory
235 * @dir: directory name
237 extern int uci_set_savedir(struct uci_context
*ctx
, const char *dir
);
240 * uci_set_savedir: override the default config storage directory
242 * @dir: directory name
244 extern int uci_set_confdir(struct uci_context
*ctx
, const char *dir
);
247 * uci_add_delta_path: add a directory to the search path for change delta files
249 * @dir: directory name
251 * This function allows you to add directories, which contain 'overlays'
252 * for the active config, that will never be committed.
254 extern int uci_add_delta_path(struct uci_context
*ctx
, const char *dir
);
257 * uci_revert: revert all changes to a config item
261 extern int uci_revert(struct uci_context
*ctx
, struct uci_ptr
*ptr
);
264 * uci_parse_argument: parse a shell-style argument, with an arbitrary quoting style
266 * @stream: input stream
267 * @str: pointer to the current line (use NULL for parsing the next line)
268 * @result: pointer for the result
270 extern int uci_parse_argument(struct uci_context
*ctx
, FILE *stream
, char **str
, char **result
);
273 * uci_set_backend: change the default backend
275 * @name: name of the backend
277 * The default backend is "file", which uses /etc/config for config storage
279 extern int uci_set_backend(struct uci_context
*ctx
, const char *name
);
282 * uci_validate_text: validate a value string for uci options
285 * this function checks whether a given string is acceptable as value
288 extern bool uci_validate_text(const char *str
);
292 * uci_add_hook: add a uci hook
296 * NB: allocated and freed by the caller
298 extern int uci_add_hook(struct uci_context
*ctx
, const struct uci_hook_ops
*ops
);
301 * uci_remove_hook: remove a uci hook
305 extern int uci_remove_hook(struct uci_context
*ctx
, const struct uci_hook_ops
*ops
);
308 * uci_load_plugin: load an uci plugin
310 * @filename: path to the uci plugin
312 * NB: plugin will be unloaded automatically when the context is freed
314 int uci_load_plugin(struct uci_context
*ctx
, const char *filename
);
317 * uci_load_plugins: load all uci plugins from a directory
319 * @pattern: pattern of uci plugin files (optional)
321 * if pattern is NULL, then uci_load_plugins will call uci_load_plugin
322 * for uci_*.so in <prefix>/lib/
324 int uci_load_plugins(struct uci_context
*ctx
, const char *pattern
);
327 * uci_parse_ptr: parse a uci string into a uci_ptr
329 * @ptr: target data structure
330 * @str: string to parse
332 * str is modified by this function
334 int uci_parse_ptr(struct uci_context
*ctx
, struct uci_ptr
*ptr
, char *str
);
336 /* UCI data structures */
340 UCI_TYPE_PACKAGE
= 2,
341 UCI_TYPE_SECTION
= 3,
344 UCI_TYPE_BACKEND
= 6,
350 enum uci_option_type
{
356 UCI_FLAG_STRICT
= (1 << 0), /* strict mode for the parser */
357 UCI_FLAG_PERROR
= (1 << 1), /* print parser error messages */
358 UCI_FLAG_EXPORT_NAME
= (1 << 2), /* when exporting, name unnamed sections */
359 UCI_FLAG_SAVED_DELTA
= (1 << 3), /* store the saved delta in memory as well */
364 struct uci_list list
;
371 struct uci_element e
;
372 char **(*list_configs
)(struct uci_context
*ctx
);
373 struct uci_package
*(*load
)(struct uci_context
*ctx
, const char *name
);
374 void (*commit
)(struct uci_context
*ctx
, struct uci_package
**p
, bool overwrite
);
383 /* list of config packages */
384 struct uci_list root
;
386 /* parser context, use for error handling only */
387 struct uci_parse_context
*pctx
;
389 /* backend for import and export */
390 struct uci_backend
*backend
;
391 struct uci_list backends
;
393 /* uci runtime flags */
394 enum uci_flags flags
;
399 /* search path for delta files */
400 struct uci_list delta_path
;
406 bool internal
, nested
;
410 struct uci_list hooks
;
411 struct uci_list plugins
;
416 struct uci_element e
;
417 struct uci_list sections
;
418 struct uci_context
*ctx
;
423 struct uci_backend
*backend
;
426 struct uci_list delta
;
427 struct uci_list saved_delta
;
432 struct uci_element e
;
433 struct uci_list options
;
434 struct uci_package
*package
;
441 struct uci_element e
;
442 struct uci_section
*section
;
443 enum uci_option_type type
;
445 struct uci_list list
;
461 struct uci_element e
;
462 enum uci_command cmd
;
469 enum uci_type target
;
471 UCI_LOOKUP_DONE
= (1 << 0),
472 UCI_LOOKUP_COMPLETE
= (1 << 1),
473 UCI_LOOKUP_EXTENDED
= (1 << 2),
476 struct uci_package
*p
;
477 struct uci_section
*s
;
478 struct uci_option
*o
;
479 struct uci_element
*last
;
489 void (*load
)(const struct uci_hook_ops
*ops
, struct uci_package
*p
);
490 void (*set
)(const struct uci_hook_ops
*ops
, struct uci_package
*p
, struct uci_delta
*e
);
495 struct uci_element e
;
496 const struct uci_hook_ops
*ops
;
499 struct uci_plugin_ops
501 int (*attach
)(struct uci_context
*ctx
);
502 void (*detach
)(struct uci_context
*ctx
);
507 struct uci_element e
;
508 const struct uci_plugin_ops
*ops
;
513 /* linked list handling */
515 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
519 * container_of - cast a member of a structure out to the containing structure
520 * @ptr: the pointer to the member.
521 * @type: the type of the container struct this is embedded in.
522 * @member: the name of the member within the struct.
525 #define container_of(ptr, type, member) \
526 ((type *) ((char *)ptr - offsetof(type,member)))
531 * uci_list_entry: casts an uci_list pointer to the containing struct.
532 * @_type: config, section or option
533 * @_ptr: pointer to the uci_list struct
535 #define list_to_element(ptr) \
536 container_of(ptr, struct uci_element, list)
539 * uci_foreach_entry: loop through a list of uci elements
540 * @_list: pointer to the uci_list struct
541 * @_ptr: iteration variable, struct uci_element
543 * use like a for loop, e.g:
544 * uci_foreach(&list, p) {
548 #define uci_foreach_element(_list, _ptr) \
549 for(_ptr = list_to_element((_list)->next); \
550 &_ptr->list != (_list); \
551 _ptr = list_to_element(_ptr->list.next))
554 * uci_foreach_entry_safe: like uci_foreach_safe, but safe for deletion
555 * @_list: pointer to the uci_list struct
556 * @_tmp: temporary variable, struct uci_element *
557 * @_ptr: iteration variable, struct uci_element *
559 * use like a for loop, e.g:
560 * uci_foreach(&list, p) {
564 #define uci_foreach_element_safe(_list, _tmp, _ptr) \
565 for(_ptr = list_to_element((_list)->next), \
566 _tmp = list_to_element(_ptr->list.next); \
567 &_ptr->list != (_list); \
568 _ptr = _tmp, _tmp = list_to_element(_ptr->list.next))
571 * uci_list_empty: returns true if a list is empty
574 #define uci_list_empty(list) ((list)->next == (list))
576 /* wrappers for dynamic type handling */
577 #define uci_type_backend UCI_TYPE_BACKEND
578 #define uci_type_delta UCI_TYPE_DELTA
579 #define uci_type_package UCI_TYPE_PACKAGE
580 #define uci_type_section UCI_TYPE_SECTION
581 #define uci_type_option UCI_TYPE_OPTION
582 #define uci_type_hook UCI_TYPE_HOOK
583 #define uci_type_plugin UCI_TYPE_PLUGIN
585 /* element typecasting */
586 #ifdef UCI_DEBUG_TYPECAST
587 static const char *uci_typestr
[] = {
588 [uci_type_backend
] = "backend",
589 [uci_type_delta
] = "delta",
590 [uci_type_package
] = "package",
591 [uci_type_section
] = "section",
592 [uci_type_option
] = "option",
593 [uci_type_hook
] = "hook",
594 [uci_type_plugin
] = "plugin",
597 static void uci_typecast_error(int from
, int to
)
599 fprintf(stderr
, "Invalid typecast from '%s' to '%s'\n", uci_typestr
[from
], uci_typestr
[to
]);
602 #define BUILD_CAST(_type) \
603 static inline struct uci_ ## _type *uci_to_ ## _type (struct uci_element *e) \
605 if (e->type != uci_type_ ## _type) { \
606 uci_typecast_error(e->type, uci_type_ ## _type); \
608 return (struct uci_ ## _type *) e; \
620 #define uci_to_backend(ptr) container_of(ptr, struct uci_backend, e)
621 #define uci_to_delta(ptr) container_of(ptr, struct uci_delta, e)
622 #define uci_to_package(ptr) container_of(ptr, struct uci_package, e)
623 #define uci_to_section(ptr) container_of(ptr, struct uci_section, e)
624 #define uci_to_option(ptr) container_of(ptr, struct uci_option, e)
625 #define uci_to_hook(ptr) container_of(ptr, struct uci_hook, e)
626 #define uci_to_plugin(ptr) container_of(ptr, struct uci_plugin, e)
630 * uci_alloc_element: allocate a generic uci_element, reserve a buffer and typecast
632 * @type: {package,section,option}
633 * @name: string containing the name of the element
634 * @datasize: additional buffer size to reserve at the end of the struct
636 #define uci_alloc_element(ctx, type, name, datasize) \
637 uci_to_ ## type (uci_alloc_generic(ctx, uci_type_ ## type, name, sizeof(struct uci_ ## type) + datasize))
639 #define uci_dataptr(ptr) \
640 (((char *) ptr) + sizeof(*ptr))