more plugin support
[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         /* private: */
316         const void *ptr;
317         void *priv;
318 };
319
320 struct uci_context
321 {
322         /* list of config packages */
323         struct uci_list root;
324
325         /* parser context, use for error handling only */
326         struct uci_parse_context *pctx;
327
328         /* backend for import and export */
329         struct uci_backend *backend;
330         struct uci_list backends;
331
332         /* uci runtime flags */
333         enum uci_flags flags;
334
335         char *confdir;
336         char *savedir;
337
338         /* search path for history files */
339         struct uci_list history_path;
340
341         /* private: */
342         int errno;
343         const char *func;
344         jmp_buf trap;
345         bool internal;
346         char *buf;
347         int bufsz;
348 };
349
350 struct uci_package
351 {
352         struct uci_element e;
353         struct uci_list sections;
354         struct uci_context *ctx;
355         bool has_history;
356         char *path;
357
358         /* private: */
359         struct uci_backend *backend;
360         void *priv;
361         int n_section;
362         struct uci_list history;
363         struct uci_list saved_history;
364 };
365
366 struct uci_section
367 {
368         struct uci_element e;
369         struct uci_list options;
370         struct uci_package *package;
371         bool anonymous;
372         char *type;
373 };
374
375 struct uci_option
376 {
377         struct uci_element e;
378         struct uci_section *section;
379         char *value;
380 };
381
382 enum uci_command {
383         UCI_CMD_ADD,
384         UCI_CMD_REMOVE,
385         UCI_CMD_CHANGE,
386         UCI_CMD_RENAME
387 };
388
389 struct uci_history
390 {
391         struct uci_element e;
392         enum uci_command cmd;
393         char *section;
394         char *value;
395 };
396
397
398 /* linked list handling */
399 #ifndef offsetof
400 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
401 #endif
402
403 /**
404  * container_of - cast a member of a structure out to the containing structure
405  * @ptr:    the pointer to the member.
406  * @type:   the type of the container struct this is embedded in.
407  * @member: the name of the member within the struct.
408  */
409 #define container_of(ptr, type, member) \
410         ((type *) ((char *)ptr - offsetof(type,member)))
411
412
413 /**
414  * uci_list_entry: casts an uci_list pointer to the containing struct.
415  * @_type: config, section or option
416  * @_ptr: pointer to the uci_list struct
417  */
418 #define list_to_element(ptr) \
419         container_of(ptr, struct uci_element, list)
420
421 /**
422  * uci_foreach_entry: loop through a list of uci elements
423  * @_list: pointer to the uci_list struct
424  * @_ptr: iteration variable, struct uci_element
425  *
426  * use like a for loop, e.g:
427  *   uci_foreach(&list, p) {
428  *      ...
429  *   }
430  */
431 #define uci_foreach_element(_list, _ptr)                \
432         for(_ptr = list_to_element((_list)->next);      \
433                 &_ptr->list != (_list);                 \
434                 _ptr = list_to_element(_ptr->list.next))
435
436 /**
437  * uci_foreach_entry_safe: like uci_foreach_safe, but safe for deletion
438  * @_list: pointer to the uci_list struct
439  * @_tmp: temporary variable, struct uci_element *
440  * @_ptr: iteration variable, struct uci_element *
441  *
442  * use like a for loop, e.g:
443  *   uci_foreach(&list, p) {
444  *      ...
445  *   }
446  */
447 #define uci_foreach_element_safe(_list, _tmp, _ptr)             \
448         for(_ptr = list_to_element((_list)->next),              \
449                 _tmp = list_to_element(_ptr->list.next);        \
450                 &_ptr->list != (_list);                 \
451                 _ptr = _tmp, _tmp = list_to_element(_ptr->list.next))
452
453 /**
454  * uci_list_empty: returns true if a list is empty
455  * @list: list head
456  */
457 #define uci_list_empty(list) ((list)->next == (list))
458
459 /* wrappers for dynamic type handling */
460 #define uci_type_backend UCI_TYPE_BACKEND
461 #define uci_type_history UCI_TYPE_HISTORY
462 #define uci_type_package UCI_TYPE_PACKAGE
463 #define uci_type_section UCI_TYPE_SECTION
464 #define uci_type_option UCI_TYPE_OPTION
465
466 /* element typecasting */
467 #ifdef UCI_DEBUG_TYPECAST
468 static const char *uci_typestr[] = {
469         [uci_type_backend] = "backend",
470         [uci_type_history] = "history",
471         [uci_type_package] = "package",
472         [uci_type_section] = "section",
473         [uci_type_option] = "option",
474 };
475
476 static void uci_typecast_error(int from, int to)
477 {
478         fprintf(stderr, "Invalid typecast from '%s' to '%s'\n", uci_typestr[from], uci_typestr[to]);
479 }
480
481 #define BUILD_CAST(_type) \
482         static inline struct uci_ ## _type *uci_to_ ## _type (struct uci_element *e) \
483         { \
484                 if (e->type != uci_type_ ## _type) { \
485                         uci_typecast_error(e->type, uci_type_ ## _type); \
486                 } \
487                 return (struct uci_ ## _type *) e; \
488         }
489
490 BUILD_CAST(backend)
491 BUILD_CAST(history)
492 BUILD_CAST(package)
493 BUILD_CAST(section)
494 BUILD_CAST(option)
495
496 #else
497 #define uci_to_backend(ptr) container_of(ptr, struct uci_backend, e)
498 #define uci_to_history(ptr) container_of(ptr, struct uci_history, e)
499 #define uci_to_package(ptr) container_of(ptr, struct uci_package, e)
500 #define uci_to_section(ptr) container_of(ptr, struct uci_section, e)
501 #define uci_to_option(ptr)  container_of(ptr, struct uci_option, e)
502 #endif
503
504 /**
505  * uci_alloc_element: allocate a generic uci_element, reserve a buffer and typecast
506  * @ctx: uci context
507  * @type: {package,section,option}
508  * @name: string containing the name of the element
509  * @datasize: additional buffer size to reserve at the end of the struct
510  */
511 #define uci_alloc_element(ctx, type, name, datasize) \
512         uci_to_ ## type (uci_alloc_generic(ctx, uci_type_ ## type, name, sizeof(struct uci_ ## type) + datasize))
513
514 #define uci_dataptr(ptr) \
515         (((char *) ptr) + sizeof(*ptr))
516
517 #endif