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