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