add feature flags for the build process, improve automatic rebuild
[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 #include "uci_config.h"
19
20 /*
21  * you can use these defines to enable debugging behavior for
22  * apps compiled against libuci:
23  *
24  * #define UCI_DEBUG_TYPECAST:
25  *   enable uci_element typecast checking at run time
26  *
27  */
28
29 #include <stdbool.h>
30 #include <setjmp.h>
31 #include <stdio.h>
32
33 #define UCI_CONFDIR "/etc/config"
34 #define UCI_SAVEDIR "/tmp/.uci"
35 #define UCI_FILEMODE    0600
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_DUPLICATE,
46         UCI_ERR_UNKNOWN,
47         UCI_ERR_LAST
48 };
49
50 struct uci_list;
51 struct uci_list
52 {
53         struct uci_list *next;
54         struct uci_list *prev;
55 };
56
57 struct uci_element;
58 struct uci_package;
59 struct uci_section;
60 struct uci_option;
61 struct uci_history;
62 struct uci_context;
63 struct uci_backend;
64 struct uci_parse_context;
65
66
67 /**
68  * uci_parse_tuple: Parse an uci tuple
69  * @ctx: uci context
70  * @str: input string
71  * @package: output package pointer
72  * @section: output section pointer
73  * @option: output option pointer
74  * @value: output value pointer
75  *
76  * format: <package>[.<section>[.<option>]][=<value>]
77  */
78 extern int uci_parse_tuple(struct uci_context *ctx, char *str, char **package, char **section, char **option, char **value);
79
80 /**
81  * uci_alloc_context: Allocate a new uci context
82  */
83 extern struct uci_context *uci_alloc_context(void);
84
85 /**
86  * uci_free_context: Free the uci context including all of its data
87  */
88 extern void uci_free_context(struct uci_context *ctx);
89
90 /**
91  * uci_perror: Print the last uci error that occured
92  * @ctx: uci context
93  * @str: string to print before the error message
94  */
95 extern void uci_perror(struct uci_context *ctx, const char *str);
96
97 /**
98  * uci_import: Import uci config data from a stream
99  * @ctx: uci context
100  * @stream: file stream to import from
101  * @name: (optional) assume the config has the given name
102  * @package: (optional) store the last parsed config package in this variable
103  * @single: ignore the 'package' keyword and parse everything into a single package
104  *
105  * the name parameter is for config files that don't explicitly use the 'package <...>' keyword
106  * if 'package' points to a non-null struct pointer, enable history tracking and merge 
107  */
108 extern int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package, bool single);
109
110 /**
111  * uci_export: Export one or all uci config packages
112  * @ctx: uci context
113  * @stream: output stream
114  * @package: (optional) uci config package to export
115  * @header: include the package header
116  */
117 extern int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package, bool header);
118
119 /**
120  * uci_load: Parse an uci config file and store it in the uci context
121  *
122  * @ctx: uci context
123  * @name: name of the config file (relative to the config directory)
124  * @package: store the loaded config package in this variable
125  */
126 extern int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package);
127
128 /**
129  * uci_unload: Unload a config file from the uci context
130  *
131  * @ctx: uci context
132  * @package: pointer to the uci_package struct
133  */
134 extern int uci_unload(struct uci_context *ctx, struct uci_package *p);
135
136 /**
137  * uci_lookup: Look up an uci element
138  *
139  * @ctx: uci context
140  * @res: where to store the result
141  * @package: uci_package struct 
142  * @section: config section (optional)
143  * @option: option to search for (optional)
144  *
145  * If section is omitted, then a pointer to the config package is returned
146  * If option is omitted, then a pointer to the config section is returned
147  */
148 extern int uci_lookup(struct uci_context *ctx, struct uci_element **res, struct uci_package *package, char *section, char *option);
149
150 /**
151  * uci_add_section: Add an unnamed section
152  * @ctx: uci context
153  * @p: package to add the section to
154  * @type: section type
155  * @res: pointer to store a reference to the new section in
156  */
157 extern int uci_add_section(struct uci_context *ctx, struct uci_package *p, char *type, struct uci_section **res);
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  * @result: store the updated element in this variable (optional)
178  */
179 extern int uci_set(struct uci_context *ctx, struct uci_package *p, char *section, char *option, char *value, struct uci_element **result);
180
181 /**
182  * uci_rename: Rename an element
183  * @ctx: uci context
184  * @package: package name
185  * @section: section name
186  * @option: option name
187  * @name: new name
188  */
189 extern int uci_rename(struct uci_context *ctx, struct uci_package *p, char *section, char *option, char *name);
190
191 /**
192  * uci_delete_element: Delete a section or option
193  * @ctx: uci context
194  * @e: element (section or option)
195  */
196 extern int uci_delete_element(struct uci_context *ctx, struct uci_element *e);
197
198 /**
199  * uci_delete: Delete a section or option
200  * @ctx: uci context
201  * @p: uci package
202  * @section: section name
203  * @option: option name (optional)
204  */
205 extern int uci_delete(struct uci_context *ctx, struct uci_package *p, char *section, char *option);
206
207 /**
208  * uci_save: save change history for a package
209  * @ctx: uci context
210  * @p: uci_package struct
211  */
212 extern int uci_save(struct uci_context *ctx, struct uci_package *p);
213
214 /**
215  * uci_commit: commit changes to a package
216  * @ctx: uci context
217  * @p: uci_package struct pointer
218  * @overwrite: overwrite existing config data and flush history
219  *
220  * committing may reload the whole uci_package data,
221  * the supplied pointer is updated accordingly
222  */
223 extern int uci_commit(struct uci_context *ctx, struct uci_package **p, bool overwrite);
224
225 /**
226  * uci_list_configs: List available uci config files
227  * @ctx: uci context
228  *
229  * caller is responsible for freeing the allocated memory behind list
230  */
231 extern int uci_list_configs(struct uci_context *ctx, char ***list);
232
233 /** 
234  * uci_set_savedir: override the default history save directory
235  * @ctx: uci context
236  * @dir: directory name
237  */
238 extern int uci_set_savedir(struct uci_context *ctx, const char *dir);
239
240 /** 
241  * uci_set_savedir: override the default config storage directory
242  * @ctx: uci context
243  * @dir: directory name
244  */
245 extern int uci_set_confdir(struct uci_context *ctx, const char *dir);
246
247 /**
248  * uci_add_history_path: add a directory to the search path for change history files
249  * @ctx: uci context
250  * @dir: directory name
251  *
252  * This function allows you to add directories, which contain 'overlays'
253  * for the active config, that will never be committed.
254  */
255 extern int uci_add_history_path(struct uci_context *ctx, const char *dir);
256
257 /**
258  * uci_revert: revert all changes to a config item
259  * @ctx: uci context
260  * @p: pointer to a uci_package struct ptr (will be replaced by the revert)
261  * @section: section name (optional)
262  * @option option name (optional)
263  */
264 extern int uci_revert(struct uci_context *ctx, struct uci_package **p, char *section, char *option);
265
266 /**
267  * uci_parse_argument: parse a shell-style argument, with an arbitrary quoting style
268  * @ctx: uci context
269  * @stream: input stream
270  * @str: pointer to the current line (use NULL for parsing the next line)
271  * @result: pointer for the result
272  */
273 extern int uci_parse_argument(struct uci_context *ctx, FILE *stream, char **str, char **result);
274
275 /**
276  * uci_set_backend: change the default backend
277  * @ctx: uci context
278  * @name: name of the backend
279  *
280  * The default backend is "file", which uses /etc/config for config storage
281  */
282 extern int uci_set_backend(struct uci_context *ctx, const char *name);
283
284 /* UCI data structures */
285 enum uci_type {
286         UCI_TYPE_HISTORY = 0,
287         UCI_TYPE_PACKAGE = 1,
288         UCI_TYPE_SECTION = 2,
289         UCI_TYPE_OPTION = 3,
290         UCI_TYPE_PATH = 4,
291         UCI_TYPE_BACKEND = 5,
292 };
293
294 enum uci_flags {
295         UCI_FLAG_STRICT =        (1 << 0), /* strict mode for the parser */
296         UCI_FLAG_PERROR =        (1 << 1), /* print parser error messages */
297         UCI_FLAG_EXPORT_NAME =   (1 << 2), /* when exporting, name unnamed sections */
298         UCI_FLAG_SAVED_HISTORY = (1 << 3), /* store the saved history in memory as well */
299 };
300
301 struct uci_element
302 {
303         struct uci_list list;
304         enum uci_type type;
305         char *name;
306 };
307
308 struct uci_backend
309 {
310         struct uci_element e;
311         char **(*list_configs)(struct uci_context *ctx);
312         struct uci_package *(*load)(struct uci_context *ctx, const char *name);
313         void (*commit)(struct uci_context *ctx, struct uci_package **p, bool overwrite);
314 };
315
316 struct uci_context
317 {
318         /* list of config packages */
319         struct uci_list root;
320
321         /* parser context, use for error handling only */
322         struct uci_parse_context *pctx;
323
324         /* backend for import and export */
325         struct uci_backend *backend;
326         struct uci_list backends;
327
328         /* uci runtime flags */
329         enum uci_flags flags;
330
331         char *confdir;
332         char *savedir;
333
334         /* search path for history files */
335         struct uci_list history_path;
336
337         /* private: */
338         int errno;
339         const char *func;
340         jmp_buf trap;
341         bool internal;
342         char *buf;
343         int bufsz;
344 };
345
346 struct uci_package
347 {
348         struct uci_element e;
349         struct uci_list sections;
350         struct uci_context *ctx;
351         bool has_history;
352         char *path;
353
354         /* private: */
355         struct uci_backend *backend;
356         void *priv;
357         int n_section;
358         struct uci_list history;
359         struct uci_list saved_history;
360 };
361
362 struct uci_section
363 {
364         struct uci_element e;
365         struct uci_list options;
366         struct uci_package *package;
367         bool anonymous;
368         char *type;
369 };
370
371 struct uci_option
372 {
373         struct uci_element e;
374         struct uci_section *section;
375         char *value;
376 };
377
378 enum uci_command {
379         UCI_CMD_ADD,
380         UCI_CMD_REMOVE,
381         UCI_CMD_CHANGE,
382         UCI_CMD_RENAME
383 };
384
385 struct uci_history
386 {
387         struct uci_element e;
388         enum uci_command cmd;
389         char *section;
390         char *value;
391 };
392
393 #define UCI_BACKEND(_var, _name, ...)   \
394 struct uci_backend _var = {             \
395         .e.list = {                     \
396                 .next = &_var.e.list,   \
397                 .prev = &_var.e.list,   \
398         },                              \
399         .e.name = _name,                \
400         .e.type = UCI_TYPE_BACKEND,     \
401         __VA_ARGS__                     \
402 }
403
404
405 /* linked list handling */
406 #ifndef offsetof
407 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
408 #endif
409
410 /**
411  * container_of - cast a member of a structure out to the containing structure
412  * @ptr:    the pointer to the member.
413  * @type:   the type of the container struct this is embedded in.
414  * @member: the name of the member within the struct.
415  */
416 #define container_of(ptr, type, member) \
417         ((type *) ((char *)ptr - offsetof(type,member)))
418
419
420 /**
421  * uci_list_entry: casts an uci_list pointer to the containing struct.
422  * @_type: config, section or option
423  * @_ptr: pointer to the uci_list struct
424  */
425 #define list_to_element(ptr) \
426         container_of(ptr, struct uci_element, list)
427
428 /**
429  * uci_foreach_entry: loop through a list of uci elements
430  * @_list: pointer to the uci_list struct
431  * @_ptr: iteration variable, struct uci_element
432  *
433  * use like a for loop, e.g:
434  *   uci_foreach(&list, p) {
435  *      ...
436  *   }
437  */
438 #define uci_foreach_element(_list, _ptr)                \
439         for(_ptr = list_to_element((_list)->next);      \
440                 &_ptr->list != (_list);                 \
441                 _ptr = list_to_element(_ptr->list.next))
442
443 /**
444  * uci_foreach_entry_safe: like uci_foreach_safe, but safe for deletion
445  * @_list: pointer to the uci_list struct
446  * @_tmp: temporary variable, struct uci_element *
447  * @_ptr: iteration variable, struct uci_element *
448  *
449  * use like a for loop, e.g:
450  *   uci_foreach(&list, p) {
451  *      ...
452  *   }
453  */
454 #define uci_foreach_element_safe(_list, _tmp, _ptr)             \
455         for(_ptr = list_to_element((_list)->next),              \
456                 _tmp = list_to_element(_ptr->list.next);        \
457                 &_ptr->list != (_list);                 \
458                 _ptr = _tmp, _tmp = list_to_element(_ptr->list.next))
459
460 /**
461  * uci_list_empty: returns true if a list is empty
462  * @list: list head
463  */
464 #define uci_list_empty(list) ((list)->next == (list))
465
466 /* wrappers for dynamic type handling */
467 #define uci_type_backend UCI_TYPE_BACKEND
468 #define uci_type_history UCI_TYPE_HISTORY
469 #define uci_type_package UCI_TYPE_PACKAGE
470 #define uci_type_section UCI_TYPE_SECTION
471 #define uci_type_option UCI_TYPE_OPTION
472
473 /* element typecasting */
474 #ifdef UCI_DEBUG_TYPECAST
475 static const char *uci_typestr[] = {
476         [uci_type_backend] = "backend",
477         [uci_type_history] = "history",
478         [uci_type_package] = "package",
479         [uci_type_section] = "section",
480         [uci_type_option] = "option",
481 };
482
483 static void uci_typecast_error(int from, int to)
484 {
485         fprintf(stderr, "Invalid typecast from '%s' to '%s'\n", uci_typestr[from], uci_typestr[to]);
486 }
487
488 #define BUILD_CAST(_type) \
489         static inline struct uci_ ## _type *uci_to_ ## _type (struct uci_element *e) \
490         { \
491                 if (e->type != uci_type_ ## _type) { \
492                         uci_typecast_error(e->type, uci_type_ ## _type); \
493                 } \
494                 return (struct uci_ ## _type *) e; \
495         }
496
497 BUILD_CAST(backend)
498 BUILD_CAST(history)
499 BUILD_CAST(package)
500 BUILD_CAST(section)
501 BUILD_CAST(option)
502
503 #else
504 #define uci_to_backend(ptr) container_of(ptr, struct uci_backend, e)
505 #define uci_to_history(ptr) container_of(ptr, struct uci_history, e)
506 #define uci_to_package(ptr) container_of(ptr, struct uci_package, e)
507 #define uci_to_section(ptr) container_of(ptr, struct uci_section, e)
508 #define uci_to_option(ptr)  container_of(ptr, struct uci_option, e)
509 #endif
510
511 /**
512  * uci_alloc_element: allocate a generic uci_element, reserve a buffer and typecast
513  * @ctx: uci context
514  * @type: {package,section,option}
515  * @name: string containing the name of the element
516  * @datasize: additional buffer size to reserve at the end of the struct
517  */
518 #define uci_alloc_element(ctx, type, name, datasize) \
519         uci_to_ ## type (uci_alloc_generic(ctx, uci_type_ ## type, name, sizeof(struct uci_ ## type) + datasize))
520
521 #define uci_dataptr(ptr) \
522         (((char *) ptr) + sizeof(*ptr))
523
524 #endif