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