add uci_lookup function
[project/uci.git] / uci.h
1 /*
2 * libuci - Library for the Unified Configuration Interface
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
4 *
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
8 *
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.
13 */
14
15 #ifndef __LIBUCI_H
16 #define __LIBUCI_H
17
18 /*
19 * you can use these defines to enable debugging behavior for
20 * apps compiled against libuci:
21 *
22 * #define UCI_DEBUG_TYPECAST:
23 * enable uci_element typecast checking at run time
24 *
25 */
26
27 #ifdef DEBUG_ALL
28 #define UCI_DEBUG
29 #define UCI_DEBUG_TYPECAST
30 #endif
31
32 #include <setjmp.h>
33 #include <stdio.h>
34
35 #define UCI_CONFDIR "/etc/config"
36
37 enum
38 {
39 UCI_OK = 0,
40 UCI_ERR_MEM,
41 UCI_ERR_INVAL,
42 UCI_ERR_NOTFOUND,
43 UCI_ERR_IO,
44 UCI_ERR_PARSE,
45 UCI_ERR_UNKNOWN,
46 UCI_ERR_LAST
47 };
48
49 struct uci_list
50 {
51 void *next;
52 void *prev;
53 };
54
55 struct uci_element;
56 struct uci_package;
57 struct uci_section;
58 struct uci_option;
59 struct uci_history;
60 struct uci_parse_context;
61
62
63 /**
64 * uci_alloc_context: Allocate a new uci context
65 */
66 extern struct uci_context *uci_alloc_context(void);
67
68 /**
69 * uci_free_context: Free the uci context including all of its data
70 */
71 extern void uci_free_context(struct uci_context *ctx);
72
73 /**
74 * uci_perror: Print the last uci error that occured
75 * @ctx: uci context
76 * @str: string to print before the error message
77 */
78 extern void uci_perror(struct uci_context *ctx, const char *str);
79
80 /**
81 * uci_import: Import uci config data from a stream
82 * @ctx: uci context
83 * @stream: file stream to import from
84 * @name: (optional) assume the config has the given name
85 * @package: (optional) store the last parsed config package in this variable
86 *
87 * the name parameter is for config files that don't explicitly use the 'package <...>' keyword
88 */
89 extern int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package);
90
91 /**
92 * uci_export: Export one or all uci config packages
93 * @ctx: uci context
94 * @stream: output stream
95 * @package: (optional) uci config package to export
96 */
97 extern int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package);
98
99 /**
100 * uci_load: Parse an uci config file and store it in the uci context
101 *
102 * @ctx: uci context
103 * @name: name of the config file (relative to the config directory)
104 * @package: store the loaded config package in this variable
105 */
106 extern int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package);
107
108 /**
109 * uci_unload: Unload a config file from the uci context
110 *
111 * @ctx: uci context
112 * @name: name of the config file
113 */
114 extern int uci_unload(struct uci_context *ctx, const char *name);
115
116 /**
117 * uci_cleanup: Clean up after an error
118 *
119 * @ctx: uci context
120 */
121 extern int uci_cleanup(struct uci_context *ctx);
122
123 /**
124 * uci_lookup: Look up an uci element
125 *
126 * @ctx: uci context
127 * @res: where to store the result
128 * @package: config package
129 * @section: config section (optional)
130 * @option: option to search for (optional)
131 *
132 * If section is omitted, then a pointer to the config package is returned
133 * If option is omitted, then a pointer to the config section is returned
134 */
135 extern int uci_lookup(struct uci_context *ctx, struct uci_element **res, char *package, char *section, char *option);
136
137 /**
138 * uci_list_configs: List available uci config files
139 *
140 * @ctx: uci context
141 */
142 extern char **uci_list_configs(struct uci_context *ctx);
143
144 /* UCI data structures */
145 enum uci_type {
146 uci_type_package = 0,
147 uci_type_section = 1,
148 uci_type_option = 2
149 };
150
151 struct uci_element
152 {
153 struct uci_list list;
154 enum uci_type type;
155 char *name;
156 };
157
158 struct uci_context
159 {
160 /* list of config packages */
161 struct uci_list root;
162
163 /* parser context, use for error handling only */
164 struct uci_parse_context *pctx;
165
166 /* private: */
167 int errno;
168 jmp_buf trap;
169 char *buf;
170 int bufsz;
171 };
172
173 struct uci_parse_context
174 {
175 /* error context */
176 const char *reason;
177 int line;
178 int byte;
179
180 /* private: */
181 struct uci_package *package;
182 struct uci_section *section;
183 FILE *file;
184 const char *name;
185 char *buf;
186 int bufsz;
187 };
188
189 struct uci_package
190 {
191 struct uci_element e;
192 struct uci_list sections;
193 struct uci_context *ctx;
194 /* private: */
195 int n_section;
196 };
197
198 struct uci_section
199 {
200 struct uci_element e;
201 struct uci_list options;
202 struct uci_package *package;
203 char *type;
204 };
205
206 struct uci_option
207 {
208 struct uci_element e;
209 struct uci_section *section;
210 char *value;
211 };
212
213 enum uci_command {
214 UCI_CMD_ADD,
215 UCI_CMD_REMOVE,
216 UCI_CMD_CHANGE
217 };
218
219 struct uci_history
220 {
221 struct uci_list list;
222 enum uci_command cmd;
223 union {
224 struct uci_element element;
225 struct uci_package package;
226 struct uci_section section;
227 struct uci_option option;
228 } data;
229 };
230
231 /* linked list handling */
232 #ifndef offsetof
233 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
234 #endif
235
236 /**
237 * container_of - cast a member of a structure out to the containing structure
238 * @ptr: the pointer to the member.
239 * @type: the type of the container struct this is embedded in.
240 * @member: the name of the member within the struct.
241 */
242 #define container_of(ptr, type, member) \
243 ((type *) ((char *)ptr - offsetof(type,member)))
244
245
246 /**
247 * uci_list_entry: casts an uci_list pointer to the containing struct.
248 * @_type: config, section or option
249 * @_ptr: pointer to the uci_list struct
250 */
251 #define element_to(type, ptr) \
252 container_of(ptr, struct uci_ ## type, e)
253
254 #define list_to_element(ptr) \
255 container_of(ptr, struct uci_element, list)
256
257 /**
258 * uci_foreach_entry: loop through a list of uci elements
259 * @_list: pointer to the uci_list struct
260 * @_ptr: iteration variable, struct uci_element
261 *
262 * use like a for loop, e.g:
263 * uci_foreach(&list, p) {
264 * ...
265 * }
266 */
267 #define uci_foreach_element(_list, _ptr) \
268 for(_ptr = list_to_element((_list)->next); \
269 &_ptr->list != (_list); \
270 _ptr = list_to_element(_ptr->list.next))
271
272 /**
273 * uci_foreach_entry_safe: like uci_foreach_safe, but safe for deletion
274 * @_list: pointer to the uci_list struct
275 * @_tmp: temporary variable, struct uci_element *
276 * @_ptr: iteration variable, struct uci_element *
277 *
278 * use like a for loop, e.g:
279 * uci_foreach(&list, p) {
280 * ...
281 * }
282 */
283 #define uci_foreach_element_safe(_list, _tmp, _ptr) \
284 for(_ptr = list_to_element((_list)->next), \
285 _tmp = list_to_element(_ptr->list.next); \
286 &_ptr->list != (_list); \
287 _ptr = _tmp, _tmp = list_to_element(_ptr->list.next))
288
289 /* returns true if a list is empty */
290 #define uci_list_empty(list) ((list)->next == (list))
291
292 /* element typecasting */
293 #ifdef UCI_DEBUG_TYPECAST
294 static const char *uci_typestr[] = {
295 [uci_type_package] = "package",
296 [uci_type_section] = "section",
297 [uci_type_option] = "option"
298 };
299
300 static void uci_typecast_error(int from, int to)
301 {
302 fprintf(stderr, "Invalid typecast from '%s' to '%s'\n", uci_typestr[from], uci_typestr[to]);
303 }
304
305 #define BUILD_CAST(_type) \
306 static inline struct uci_ ## _type *uci_to_ ## _type (struct uci_element *e) \
307 { \
308 if (e->type != uci_type_ ## _type) { \
309 uci_typecast_error(e->type, uci_type_ ## _type); \
310 } \
311 return (struct uci_ ## _type *) e; \
312 }
313
314 BUILD_CAST(package)
315 BUILD_CAST(section)
316 BUILD_CAST(option)
317
318 #else
319 #define uci_to_package(ptr) container_of(ptr, struct uci_package, e)
320 #define uci_to_section(ptr) container_of(ptr, struct uci_section, e)
321 #define uci_to_option(ptr) container_of(ptr, struct uci_option, e)
322 #endif
323
324 /**
325 * uci_alloc_element: allocate a generic uci_element, reserve a buffer and typecast
326 * @ctx: uci context
327 * @type: {package,section,option}
328 * @name: string containing the name of the element
329 * @datasize: additional buffer size to reserve at the end of the struct
330 */
331 #define uci_alloc_element(ctx, type, name, datasize) \
332 uci_to_ ## type (uci_alloc_generic(ctx, uci_type_ ## type, name, sizeof(struct uci_ ## type) + datasize))
333
334 #define uci_dataptr(ptr) \
335 (((char *) ptr) + sizeof(*ptr))
336
337 #endif