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.
21 #define UCI_CONFDIR "/etc/config"
45 struct uci_parse_context;
49 * uci_alloc: Allocate a new uci context
51 extern struct uci_context *uci_alloc(void);
54 * uci_free: Free the uci context including all of its data
56 extern void uci_free(struct uci_context *ctx);
59 * uci_perror: Print the last uci error that occured
61 * @str: string to print before the error message
63 extern void uci_perror(struct uci_context *ctx, const char *str);
66 * uci_import: Import uci config data from a stream
68 * @stream: file stream to import from
69 * @name: (optional) assume the config has the given name
70 * @package: (optional) store the last parsed config package in this variable
72 * the name parameter is for config files that don't explicitly use the 'package <...>' keyword
74 extern int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package);
77 * uci_export: Export one or all uci config packages
79 * @stream: output stream
80 * @package: (optional) uci config package to export
82 extern int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package);
85 * uci_load: Parse an uci config file and store it in the uci context
88 * @name: name of the config file (relative to the config directory)
89 * @package: store the loaded config package in this variable
91 extern int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package);
94 * uci_unload: Unload a config file from the uci context
97 * @name: name of the config file
99 extern int uci_unload(struct uci_context *ctx, const char *name);
102 * uci_cleanup: Clean up after an error
106 extern int uci_cleanup(struct uci_context *ctx);
109 * uci_list_configs: List available uci config files
113 extern char **uci_list_configs();
115 /* UCI data structures */
119 /* list of config packages */
120 struct uci_list root;
122 /* parser context, use for error handling only */
123 struct uci_parse_context *pctx;
132 struct uci_parse_context
140 struct uci_package *package;
141 struct uci_section *section;
150 struct uci_list list;
151 struct uci_list sections;
152 struct uci_context *ctx;
160 struct uci_list list;
161 struct uci_list options;
162 struct uci_package *package;
169 struct uci_list list;
170 struct uci_section *section;
189 struct uci_list list;
190 enum uci_command cmd;
207 /* linked list handling */
209 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
212 /* returns true if a list is empty */
213 #define uci_list_empty(list) ((list)->next == (list))
216 * uci_list_entry: casts an uci_list pointer to the containing struct.
217 * @_type: config, section or option
218 * @_ptr: pointer to the uci_list struct
220 #define uci_list_entry(_type, _ptr) \
221 ((struct uci_ ## _type *) ((char *)(_ptr) - offsetof(struct uci_ ## _type,list)))
224 * uci_foreach_entry: loop through a list of configs, sections or options
225 * @_type: see uci_list_entry
226 * @_list: pointer to the uci_list struct
227 * @_ptr: iteration variable
229 * use like a for loop, e.g:
230 * uci_foreach(section, &list, p) {
234 #define uci_foreach_entry(_type, _list, _ptr) \
235 for(_ptr = uci_list_entry(_type, (_list)->next); \
236 &_ptr->list != (_list); \
237 _ptr = uci_list_entry(_type, _ptr->list.next))