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