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