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
39 #define UCI_CONFDIR "/etc/config"
40 #define UCI_SAVEDIR "/tmp/.uci"
41 #define UCI_DIRMODE 0700
42 #define UCI_FILEMODE 0600
60 struct uci_list
*next
;
61 struct uci_list
*prev
;
72 struct uci_parse_option
;
73 struct uci_parse_context
;
77 * uci_alloc_context: Allocate a new uci context
79 extern struct uci_context
*uci_alloc_context(void);
82 * uci_free_context: Free the uci context including all of its data
84 extern void uci_free_context(struct uci_context
*ctx
);
87 * uci_perror: Print the last uci error that occured
89 * @str: string to print before the error message
91 extern void uci_perror(struct uci_context
*ctx
, const char *str
);
94 * uci_geterror: Get an error string for the last uci error
96 * @dest: target pointer for the string
97 * @str: prefix for the error message
99 * Note: string must be freed by the caller
101 extern void uci_get_errorstr(struct uci_context
*ctx
, char **dest
, const char *str
);
104 * uci_import: Import uci config data from a stream
106 * @stream: file stream to import from
107 * @name: (optional) assume the config has the given name
108 * @package: (optional) store the last parsed config package in this variable
109 * @single: ignore the 'package' keyword and parse everything into a single package
111 * the name parameter is for config files that don't explicitly use the 'package <...>' keyword
112 * if 'package' points to a non-null struct pointer, enable delta tracking and merge
114 extern int uci_import(struct uci_context
*ctx
, FILE *stream
, const char *name
, struct uci_package
**package
, bool single
);
117 * uci_export: Export one or all uci config packages
119 * @stream: output stream
120 * @package: (optional) uci config package to export
121 * @header: include the package header
123 extern int uci_export(struct uci_context
*ctx
, FILE *stream
, struct uci_package
*package
, bool header
);
126 * uci_load: Parse an uci config file and store it in the uci context
129 * @name: name of the config file (relative to the config directory)
130 * @package: store the loaded config package in this variable
132 extern int uci_load(struct uci_context
*ctx
, const char *name
, struct uci_package
**package
);
135 * uci_unload: Unload a config file from the uci context
138 * @package: pointer to the uci_package struct
140 extern int uci_unload(struct uci_context
*ctx
, struct uci_package
*p
);
143 * uci_lookup_ptr: Split an uci tuple string and look up an element tree
145 * @ptr: lookup result struct
146 * @str: uci tuple string to look up
147 * @extended: allow extended syntax lookup
149 * if extended is set to true, uci_lookup_ptr supports the following
153 * network.@interface[0].ifname ('ifname' option of the first interface section)
154 * network.@interface[-1] (last interface section)
155 * Note: uci_lookup_ptr will automatically load a config package if necessary
156 * @str must not be constant, as it will be modified and used for the strings inside @ptr,
157 * thus it must also be available as long as @ptr is in use.
159 * This function returns UCI_ERR_NOTFOUND if the package specified in the tuple
160 * string cannot be found. Otherwise it will return UCI_OK.
162 * Note that failures in looking up other parts, if they are also specfied,
163 * including section and option, will also have a return value UCI_OK but with
164 * ptr->flags * UCI_LOOKUP_COMPLETE not set.
166 extern int uci_lookup_ptr(struct uci_context
*ctx
, struct uci_ptr
*ptr
, char *str
, bool extended
);
169 * uci_add_section: Add an unnamed section
171 * @p: package to add the section to
172 * @type: section type
173 * @res: pointer to store a reference to the new section in
175 extern int uci_add_section(struct uci_context
*ctx
, struct uci_package
*p
, const char *type
, struct uci_section
**res
);
178 * uci_set: Set an element's value; create the element if necessary
182 * The updated/created element is stored in ptr->last
184 extern int uci_set(struct uci_context
*ctx
, struct uci_ptr
*ptr
);
187 * uci_add_list: Append a string to an element list
189 * @ptr: uci pointer (with value)
191 * Note: if the given option already contains a string value,
192 * it will be converted to an 1-element-list before appending the next element
194 extern int uci_add_list(struct uci_context
*ctx
, struct uci_ptr
*ptr
);
197 * uci_del_list: Remove a string from an element list
199 * @ptr: uci pointer (with value)
202 extern int uci_del_list(struct uci_context
*ctx
, struct uci_ptr
*ptr
);
205 * uci_reorder: Reposition a section
207 * @s: uci section to reposition
208 * @pos: new position in the section list
210 extern int uci_reorder_section(struct uci_context
*ctx
, struct uci_section
*s
, int pos
);
213 * uci_rename: Rename an element
215 * @ptr: uci pointer (with value)
217 extern int uci_rename(struct uci_context
*ctx
, struct uci_ptr
*ptr
);
220 * uci_delete: Delete a section or option
224 extern int uci_delete(struct uci_context
*ctx
, struct uci_ptr
*ptr
);
227 * uci_save: save change delta for a package
229 * @p: uci_package struct
231 extern int uci_save(struct uci_context
*ctx
, struct uci_package
*p
);
234 * uci_commit: commit changes to a package
236 * @p: uci_package struct pointer
237 * @overwrite: overwrite existing config data and flush delta
239 * committing may reload the whole uci_package data,
240 * the supplied pointer is updated accordingly
242 extern int uci_commit(struct uci_context
*ctx
, struct uci_package
**p
, bool overwrite
);
245 * uci_list_configs: List available uci config files
248 * caller is responsible for freeing the allocated memory behind list
250 extern int uci_list_configs(struct uci_context
*ctx
, char ***list
);
253 * uci_set_savedir: override the default delta save directory
255 * @dir: directory name
257 * This will also try adding the specified dir to the end of delta pathes.
259 extern int uci_set_savedir(struct uci_context
*ctx
, const char *dir
);
262 * uci_set_savedir: override the default config storage directory
264 * @dir: directory name
266 extern int uci_set_confdir(struct uci_context
*ctx
, const char *dir
);
269 * uci_add_delta_path: add a directory to the search path for change delta files
271 * @dir: directory name
273 * This function allows you to add directories, which contain 'overlays'
274 * for the active config, that will never be committed.
276 * Adding a duplicate directory will cause UCI_ERR_DUPLICATE be returned.
278 extern int uci_add_delta_path(struct uci_context
*ctx
, const char *dir
);
281 * uci_revert: revert all changes to a config item
285 extern int uci_revert(struct uci_context
*ctx
, struct uci_ptr
*ptr
);
288 * uci_parse_argument: parse a shell-style argument, with an arbitrary quoting style
290 * @stream: input stream
291 * @str: pointer to the current line (use NULL for parsing the next line)
292 * @result: pointer for the result
294 extern int uci_parse_argument(struct uci_context
*ctx
, FILE *stream
, char **str
, char **result
);
297 * uci_set_backend: change the default backend
299 * @name: name of the backend
301 * The default backend is "file", which uses /etc/config for config storage
303 extern int uci_set_backend(struct uci_context
*ctx
, const char *name
);
306 * uci_validate_text: validate a value string for uci options
309 * this function checks whether a given string is acceptable as value
312 extern bool uci_validate_text(const char *str
);
315 * uci_parse_ptr: parse a uci string into a uci_ptr
317 * @ptr: target data structure
318 * @str: string to parse
320 * str is modified by this function
322 int uci_parse_ptr(struct uci_context
*ctx
, struct uci_ptr
*ptr
, char *str
);
325 * uci_lookup_next: lookup a child element
327 * @e: target element pointer
328 * @list: list of elements
329 * @name: name of the child element
331 * if parent is NULL, the function looks up the package with the given name
333 int uci_lookup_next(struct uci_context
*ctx
, struct uci_element
**e
, struct uci_list
*list
, const char *name
);
336 * uci_parse_section: look up a set of options
338 * @opts: list of options to look up
339 * @n_opts: number of options to look up
340 * @tb: array of pointers to found options
342 void uci_parse_section(struct uci_section
*s
, const struct uci_parse_option
*opts
,
343 int n_opts
, struct uci_option
**tb
);
346 * uci_hash_options: build a hash over a list of options
347 * @tb: list of option pointers
348 * @n_opts: number of options
350 uint32_t uci_hash_options(struct uci_option
**tb
, int n_opts
);
353 /* UCI data structures */
357 UCI_TYPE_PACKAGE
= 2,
358 UCI_TYPE_SECTION
= 3,
361 UCI_TYPE_BACKEND
= 6,
366 enum uci_option_type
{
372 UCI_FLAG_STRICT
= (1 << 0), /* strict mode for the parser */
373 UCI_FLAG_PERROR
= (1 << 1), /* print parser error messages */
374 UCI_FLAG_EXPORT_NAME
= (1 << 2), /* when exporting, name unnamed sections */
375 UCI_FLAG_SAVED_DELTA
= (1 << 3), /* store the saved delta in memory as well */
380 struct uci_list list
;
387 struct uci_element e
;
388 char **(*list_configs
)(struct uci_context
*ctx
);
389 struct uci_package
*(*load
)(struct uci_context
*ctx
, const char *name
);
390 void (*commit
)(struct uci_context
*ctx
, struct uci_package
**p
, bool overwrite
);
399 /* list of config packages */
400 struct uci_list root
;
402 /* parser context, use for error handling only */
403 struct uci_parse_context
*pctx
;
405 /* backend for import and export */
406 struct uci_backend
*backend
;
407 struct uci_list backends
;
409 /* uci runtime flags */
410 enum uci_flags flags
;
415 /* search path for delta files */
416 struct uci_list delta_path
;
422 bool internal
, nested
;
429 struct uci_element e
;
430 struct uci_list sections
;
431 struct uci_context
*ctx
;
436 struct uci_backend
*backend
;
439 struct uci_list delta
;
440 struct uci_list saved_delta
;
445 struct uci_element e
;
446 struct uci_list options
;
447 struct uci_package
*package
;
454 struct uci_element e
;
455 struct uci_section
*section
;
456 enum uci_option_type type
;
458 struct uci_list list
;
464 * UCI_CMD_ADD is used for anonymous sections or list values
475 __UCI_CMD_LAST
= __UCI_CMD_MAX
- 1
477 extern char const uci_command_char
[];
481 struct uci_element e
;
482 enum uci_command cmd
;
489 enum uci_type target
;
491 UCI_LOOKUP_DONE
= (1 << 0),
492 UCI_LOOKUP_COMPLETE
= (1 << 1),
493 UCI_LOOKUP_EXTENDED
= (1 << 2),
496 struct uci_package
*p
;
497 struct uci_section
*s
;
498 struct uci_option
*o
;
499 struct uci_element
*last
;
507 struct uci_parse_option
{
509 enum uci_option_type type
;
514 * container_of - cast a member of a structure out to the containing structure
515 * @ptr: the pointer to the member.
516 * @type: the type of the container struct this is embedded in.
517 * @member: the name of the member within the struct.
520 #define container_of(ptr, type, member) \
521 ((type *) ((char *)ptr - offsetof(type,member)))
526 * uci_list_entry: casts an uci_list pointer to the containing struct.
527 * @_type: config, section or option
528 * @_ptr: pointer to the uci_list struct
530 #define list_to_element(ptr) \
531 container_of(ptr, struct uci_element, list)
534 * uci_foreach_entry: loop through a list of uci elements
535 * @_list: pointer to the uci_list struct
536 * @_ptr: iteration variable, struct uci_element
538 * use like a for loop, e.g:
539 * uci_foreach(&list, p) {
543 #define uci_foreach_element(_list, _ptr) \
544 for(_ptr = list_to_element((_list)->next); \
545 &_ptr->list != (_list); \
546 _ptr = list_to_element(_ptr->list.next))
549 * uci_foreach_entry_safe: like uci_foreach_safe, but safe for deletion
550 * @_list: pointer to the uci_list struct
551 * @_tmp: temporary variable, struct uci_element *
552 * @_ptr: iteration variable, struct uci_element *
554 * use like a for loop, e.g:
555 * uci_foreach(&list, p) {
559 #define uci_foreach_element_safe(_list, _tmp, _ptr) \
560 for(_ptr = list_to_element((_list)->next), \
561 _tmp = list_to_element(_ptr->list.next); \
562 &_ptr->list != (_list); \
563 _ptr = _tmp, _tmp = list_to_element(_ptr->list.next))
566 * uci_list_empty: returns true if a list is empty
569 #define uci_list_empty(list) ((list)->next == (list))
571 /* wrappers for dynamic type handling */
572 #define uci_type_backend UCI_TYPE_BACKEND
573 #define uci_type_delta UCI_TYPE_DELTA
574 #define uci_type_package UCI_TYPE_PACKAGE
575 #define uci_type_section UCI_TYPE_SECTION
576 #define uci_type_option UCI_TYPE_OPTION
578 /* element typecasting */
579 #ifdef UCI_DEBUG_TYPECAST
580 static const char *uci_typestr
[] = {
581 [uci_type_backend
] = "backend",
582 [uci_type_delta
] = "delta",
583 [uci_type_package
] = "package",
584 [uci_type_section
] = "section",
585 [uci_type_option
] = "option",
588 static void uci_typecast_error(int from
, int to
)
590 fprintf(stderr
, "Invalid typecast from '%s' to '%s'\n", uci_typestr
[from
], uci_typestr
[to
]);
593 #define BUILD_CAST(_type) \
594 static inline struct uci_ ## _type *uci_to_ ## _type (struct uci_element *e) \
596 if (e->type != uci_type_ ## _type) { \
597 uci_typecast_error(e->type, uci_type_ ## _type); \
599 return (struct uci_ ## _type *) e; \
609 #define uci_to_backend(ptr) container_of(ptr, struct uci_backend, e)
610 #define uci_to_delta(ptr) container_of(ptr, struct uci_delta, 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)
616 #define uci_dataptr(ptr) \
617 (((char *) ptr) + sizeof(*ptr))
620 * uci_lookup_package: look up a package
622 * @name: name of the package
624 static inline struct uci_package
*
625 uci_lookup_package(struct uci_context
*ctx
, const char *name
)
627 struct uci_element
*e
= NULL
;
628 if (uci_lookup_next(ctx
, &e
, &ctx
->root
, name
) == 0)
629 return uci_to_package(e
);
635 * uci_lookup_section: look up a section
637 * @p: package that the section belongs to
638 * @name: name of the section
640 static inline struct uci_section
*
641 uci_lookup_section(struct uci_context
*ctx
, struct uci_package
*p
, const char *name
)
643 struct uci_element
*e
= NULL
;
644 if (uci_lookup_next(ctx
, &e
, &p
->sections
, name
) == 0)
645 return uci_to_section(e
);
651 * uci_lookup_option: look up an option
653 * @section: section that the option belongs to
654 * @name: name of the option
656 static inline struct uci_option
*
657 uci_lookup_option(struct uci_context
*ctx
, struct uci_section
*s
, const char *name
)
659 struct uci_element
*e
= NULL
;
660 if (uci_lookup_next(ctx
, &e
, &s
->options
, name
) == 0)
661 return uci_to_option(e
);
666 static inline const char *
667 uci_lookup_option_string(struct uci_context
*ctx
, struct uci_section
*s
, const char *name
)
669 struct uci_option
*o
;
671 o
= uci_lookup_option(ctx
, s
, name
);
672 if (!o
|| o
->type
!= UCI_TYPE_STRING
)
678 #ifndef BITS_PER_LONG
679 #define BITS_PER_LONG (8 * sizeof(unsigned long))
682 static inline void uci_bitfield_set(unsigned long *bits
, int bit
)
684 bits
[bit
/ BITS_PER_LONG
] |= (1UL << (bit
% BITS_PER_LONG
));