Fix skipping directories in uci_list_config_files
[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 Lesser General Public License for more details.
13  */
14
15 #ifndef __LIBUCI_H
16 #define __LIBUCI_H
17
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21
22 #include "uci_config.h"
23
24 /*
25  * you can use these defines to enable debugging behavior for
26  * apps compiled against libuci:
27  *
28  * #define UCI_DEBUG_TYPECAST:
29  *   enable uci_element typecast checking at run time
30  *
31  */
32
33 #include <stdbool.h>
34 #include <setjmp.h>
35 #include <stdio.h>
36 #include <stdint.h>
37
38 #define UCI_CONFDIR "/etc/config"
39 #define UCI_SAVEDIR "/tmp/.uci"
40 #define UCI_DIRMODE 0700
41 #define UCI_FILEMODE 0600
42
43 enum
44 {
45         UCI_OK = 0,
46         UCI_ERR_MEM,
47         UCI_ERR_INVAL,
48         UCI_ERR_NOTFOUND,
49         UCI_ERR_IO,
50         UCI_ERR_PARSE,
51         UCI_ERR_DUPLICATE,
52         UCI_ERR_UNKNOWN,
53         UCI_ERR_LAST
54 };
55
56 struct uci_list;
57 struct uci_list
58 {
59         struct uci_list *next;
60         struct uci_list *prev;
61 };
62
63 struct uci_ptr;
64 struct uci_element;
65 struct uci_package;
66 struct uci_section;
67 struct uci_option;
68 struct uci_delta;
69 struct uci_context;
70 struct uci_backend;
71 struct uci_parse_option;
72 struct uci_parse_context;
73
74
75 /**
76  * uci_alloc_context: Allocate a new uci context
77  */
78 extern struct uci_context *uci_alloc_context(void);
79
80 /**
81  * uci_free_context: Free the uci context including all of its data
82  */
83 extern void uci_free_context(struct uci_context *ctx);
84
85 /**
86  * uci_perror: Print the last uci error that occured
87  * @ctx: uci context
88  * @str: string to print before the error message
89  */
90 extern void uci_perror(struct uci_context *ctx, const char *str);
91
92 /**
93  * uci_geterror: Get an error string for the last uci error
94  * @ctx: uci context
95  * @dest: target pointer for the string
96  * @str: prefix for the error message
97  *
98  * Note: string must be freed by the caller
99  */
100 extern void uci_get_errorstr(struct uci_context *ctx, char **dest, const char *str);
101
102 /**
103  * uci_import: Import uci config data from a stream
104  * @ctx: uci context
105  * @stream: file stream to import from
106  * @name: (optional) assume the config has the given name
107  * @package: (optional) store the last parsed config package in this variable
108  * @single: ignore the 'package' keyword and parse everything into a single package
109  *
110  * the name parameter is for config files that don't explicitly use the 'package <...>' keyword
111  * if 'package' points to a non-null struct pointer, enable delta tracking and merge
112  */
113 extern int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package, bool single);
114
115 /**
116  * uci_export: Export one or all uci config packages
117  * @ctx: uci context
118  * @stream: output stream
119  * @package: (optional) uci config package to export
120  * @header: include the package header
121  */
122 extern int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package, bool header);
123
124 /**
125  * uci_load: Parse an uci config file and store it in the uci context
126  *
127  * @ctx: uci context
128  * @name: name of the config file (relative to the config directory)
129  * @package: store the loaded config package in this variable
130  */
131 extern int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package);
132
133 /**
134  * uci_unload: Unload a config file from the uci context
135  *
136  * @ctx: uci context
137  * @package: pointer to the uci_package struct
138  */
139 extern int uci_unload(struct uci_context *ctx, struct uci_package *p);
140
141 /**
142  * uci_lookup_ptr: Split an uci tuple string and look up an element tree
143  * @ctx: uci context
144  * @ptr: lookup result struct
145  * @str: uci tuple string to look up
146  * @extended: allow extended syntax lookup
147  *
148  * if extended is set to true, uci_lookup_ptr supports the following
149  * extended syntax:
150  *
151  * Examples:
152  *   network.@interface[0].ifname ('ifname' option of the first interface section)
153  *   network.@interface[-1]       (last interface section)
154  * Note: uci_lookup_ptr will automatically load a config package if necessary
155  * @str must not be constant, as it will be modified and used for the strings inside @ptr,
156  * thus it must also be available as long as @ptr is in use.
157  *
158  * This function returns UCI_ERR_NOTFOUND if the package specified in the tuple
159  * string cannot be found.  Otherwise it will return UCI_OK.
160  *
161  * Note that failures in looking up other parts, if they are also specfied,
162  * including section and option, will also have a return value UCI_OK but with
163  * ptr->flags * UCI_LOOKUP_COMPLETE not set.
164  */
165 extern int uci_lookup_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str, bool extended);
166
167 /**
168  * uci_add_section: Add an unnamed section
169  * @ctx: uci context
170  * @p: package to add the section to
171  * @type: section type
172  * @res: pointer to store a reference to the new section in
173  */
174 extern int uci_add_section(struct uci_context *ctx, struct uci_package *p, const char *type, struct uci_section **res);
175
176 /**
177  * uci_set: Set an element's value; create the element if necessary
178  * @ctx: uci context
179  * @ptr: uci pointer
180  *
181  * The updated/created element is stored in ptr->last
182  */
183 extern int uci_set(struct uci_context *ctx, struct uci_ptr *ptr);
184
185 /**
186  * uci_add_list: Append a string to an element list
187  * @ctx: uci context
188  * @ptr: uci pointer (with value)
189  *
190  * Note: if the given option already contains a string value,
191  * it will be converted to an 1-element-list before appending the next element
192  */
193 extern int uci_add_list(struct uci_context *ctx, struct uci_ptr *ptr);
194
195 /**
196  * uci_del_list: Remove a string from an element list
197  * @ctx: uci context
198  * @ptr: uci pointer (with value)
199  *
200  */
201 extern int uci_del_list(struct uci_context *ctx, struct uci_ptr *ptr);
202
203 /**
204  * uci_reorder: Reposition a section
205  * @ctx: uci context
206  * @s: uci section to reposition
207  * @pos: new position in the section list
208  */
209 extern int uci_reorder_section(struct uci_context *ctx, struct uci_section *s, int pos);
210
211 /**
212  * uci_rename: Rename an element
213  * @ctx: uci context
214  * @ptr: uci pointer (with value)
215  */
216 extern int uci_rename(struct uci_context *ctx, struct uci_ptr *ptr);
217
218 /**
219  * uci_delete: Delete a section or option
220  * @ctx: uci context
221  * @ptr: uci pointer
222  */
223 extern int uci_delete(struct uci_context *ctx, struct uci_ptr *ptr);
224
225 /**
226  * uci_save: save change delta for a package
227  * @ctx: uci context
228  * @p: uci_package struct
229  */
230 extern int uci_save(struct uci_context *ctx, struct uci_package *p);
231
232 /**
233  * uci_commit: commit changes to a package
234  * @ctx: uci context
235  * @p: uci_package struct pointer
236  * @overwrite: overwrite existing config data and flush delta
237  *
238  * committing may reload the whole uci_package data,
239  * the supplied pointer is updated accordingly
240  */
241 extern int uci_commit(struct uci_context *ctx, struct uci_package **p, bool overwrite);
242
243 /**
244  * uci_list_configs: List available uci config files
245  * @ctx: uci context
246  *
247  * caller is responsible for freeing the allocated memory behind list
248  */
249 extern int uci_list_configs(struct uci_context *ctx, char ***list);
250
251 /**
252  * uci_set_savedir: override the default delta save directory
253  * @ctx: uci context
254  * @dir: directory name
255  *
256  * This will also try adding the specified dir to the end of delta pathes.
257  */
258 extern int uci_set_savedir(struct uci_context *ctx, const char *dir);
259
260 /**
261  * uci_set_savedir: override the default config storage directory
262  * @ctx: uci context
263  * @dir: directory name
264  */
265 extern int uci_set_confdir(struct uci_context *ctx, const char *dir);
266
267 /**
268  * uci_add_delta_path: add a directory to the search path for change delta files
269  * @ctx: uci context
270  * @dir: directory name
271  *
272  * This function allows you to add directories, which contain 'overlays'
273  * for the active config, that will never be committed.
274  *
275  * Adding a duplicate directory will cause UCI_ERR_DUPLICATE be returned.
276  */
277 extern int uci_add_delta_path(struct uci_context *ctx, const char *dir);
278
279 /**
280  * uci_revert: revert all changes to a config item
281  * @ctx: uci context
282  * @ptr: uci pointer
283  */
284 extern int uci_revert(struct uci_context *ctx, struct uci_ptr *ptr);
285
286 /**
287  * uci_parse_argument: parse a shell-style argument, with an arbitrary quoting style
288  * @ctx: uci context
289  * @stream: input stream
290  * @str: pointer to the current line (use NULL for parsing the next line)
291  * @result: pointer for the result
292  */
293 extern int uci_parse_argument(struct uci_context *ctx, FILE *stream, char **str, char **result);
294
295 /**
296  * uci_set_backend: change the default backend
297  * @ctx: uci context
298  * @name: name of the backend
299  *
300  * The default backend is "file", which uses /etc/config for config storage
301  */
302 extern int uci_set_backend(struct uci_context *ctx, const char *name);
303
304 /**
305  * uci_validate_text: validate a value string for uci options
306  * @str: value
307  *
308  * this function checks whether a given string is acceptable as value
309  * for uci options
310  */
311 extern bool uci_validate_text(const char *str);
312
313 /**
314  * uci_parse_ptr: parse a uci string into a uci_ptr
315  * @ctx: uci context
316  * @ptr: target data structure
317  * @str: string to parse
318  *
319  * str is modified by this function
320  */
321 int uci_parse_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str);
322
323 /**
324  * uci_lookup_next: lookup a child element
325  * @ctx: uci context
326  * @e: target element pointer
327  * @list: list of elements
328  * @name: name of the child element
329  *
330  * if parent is NULL, the function looks up the package with the given name
331  */
332 int uci_lookup_next(struct uci_context *ctx, struct uci_element **e, struct uci_list *list, const char *name);
333
334 /**
335  * uci_parse_section: look up a set of options
336  * @s: uci section
337  * @opts: list of options to look up
338  * @n_opts: number of options to look up
339  * @tb: array of pointers to found options
340  */
341 void uci_parse_section(struct uci_section *s, const struct uci_parse_option *opts,
342                        int n_opts, struct uci_option **tb);
343
344 /**
345  * uci_hash_options: build a hash over a list of options
346  * @tb: list of option pointers
347  * @n_opts: number of options
348  */
349 uint32_t uci_hash_options(struct uci_option **tb, int n_opts);
350
351
352 /* UCI data structures */
353 enum uci_type {
354         UCI_TYPE_UNSPEC = 0,
355         UCI_TYPE_DELTA = 1,
356         UCI_TYPE_PACKAGE = 2,
357         UCI_TYPE_SECTION = 3,
358         UCI_TYPE_OPTION = 4,
359         UCI_TYPE_PATH = 5,
360         UCI_TYPE_BACKEND = 6,
361         UCI_TYPE_ITEM = 7,
362         UCI_TYPE_HOOK = 8,
363 };
364
365 enum uci_option_type {
366         UCI_TYPE_STRING = 0,
367         UCI_TYPE_LIST = 1,
368 };
369
370 enum uci_flags {
371         UCI_FLAG_STRICT =        (1 << 0), /* strict mode for the parser */
372         UCI_FLAG_PERROR =        (1 << 1), /* print parser error messages */
373         UCI_FLAG_EXPORT_NAME =   (1 << 2), /* when exporting, name unnamed sections */
374         UCI_FLAG_SAVED_DELTA = (1 << 3), /* store the saved delta in memory as well */
375 };
376
377 struct uci_element
378 {
379         struct uci_list list;
380         enum uci_type type;
381         char *name;
382 };
383
384 struct uci_backend
385 {
386         struct uci_element e;
387         char **(*list_configs)(struct uci_context *ctx);
388         struct uci_package *(*load)(struct uci_context *ctx, const char *name);
389         void (*commit)(struct uci_context *ctx, struct uci_package **p, bool overwrite);
390
391         /* private: */
392         const void *ptr;
393         void *priv;
394 };
395
396 struct uci_context
397 {
398         /* list of config packages */
399         struct uci_list root;
400
401         /* parser context, use for error handling only */
402         struct uci_parse_context *pctx;
403
404         /* backend for import and export */
405         struct uci_backend *backend;
406         struct uci_list backends;
407
408         /* uci runtime flags */
409         enum uci_flags flags;
410
411         char *confdir;
412         char *savedir;
413
414         /* search path for delta files */
415         struct uci_list delta_path;
416
417         /* private: */
418         int err;
419         const char *func;
420         jmp_buf trap;
421         bool internal, nested;
422         char *buf;
423         int bufsz;
424 };
425
426 struct uci_package
427 {
428         struct uci_element e;
429         struct uci_list sections;
430         struct uci_context *ctx;
431         bool has_delta;
432         char *path;
433
434         /* private: */
435         struct uci_backend *backend;
436         void *priv;
437         int n_section;
438         struct uci_list delta;
439         struct uci_list saved_delta;
440 };
441
442 struct uci_section
443 {
444         struct uci_element e;
445         struct uci_list options;
446         struct uci_package *package;
447         bool anonymous;
448         char *type;
449 };
450
451 struct uci_option
452 {
453         struct uci_element e;
454         struct uci_section *section;
455         enum uci_option_type type;
456         union {
457                 struct uci_list list;
458                 char *string;
459         } v;
460 };
461
462 /*
463  * UCI_CMD_ADD is used for anonymous sections or list values
464  */
465 enum uci_command {
466         UCI_CMD_ADD,
467         UCI_CMD_REMOVE,
468         UCI_CMD_CHANGE,
469         UCI_CMD_RENAME,
470         UCI_CMD_REORDER,
471         UCI_CMD_LIST_ADD,
472         UCI_CMD_LIST_DEL,
473         __UCI_CMD_MAX,
474         __UCI_CMD_LAST = __UCI_CMD_MAX - 1
475 };
476 extern char const uci_command_char[];
477
478 struct uci_delta
479 {
480         struct uci_element e;
481         enum uci_command cmd;
482         char *section;
483         char *value;
484 };
485
486 struct uci_ptr
487 {
488         enum uci_type target;
489         enum {
490                 UCI_LOOKUP_DONE =     (1 << 0),
491                 UCI_LOOKUP_COMPLETE = (1 << 1),
492                 UCI_LOOKUP_EXTENDED = (1 << 2),
493         } flags;
494
495         struct uci_package *p;
496         struct uci_section *s;
497         struct uci_option *o;
498         struct uci_element *last;
499
500         const char *package;
501         const char *section;
502         const char *option;
503         const char *value;
504 };
505
506 struct uci_parse_option {
507         const char *name;
508         enum uci_option_type type;
509 };
510
511
512 /* linked list handling */
513 #ifndef offsetof
514 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
515 #endif
516
517 /**
518  * container_of - cast a member of a structure out to the containing structure
519  * @ptr:    the pointer to the member.
520  * @type:   the type of the container struct this is embedded in.
521  * @member: the name of the member within the struct.
522  */
523 #ifndef container_of
524 #define container_of(ptr, type, member) \
525         ((type *) ((char *)ptr - offsetof(type,member)))
526 #endif
527
528
529 /**
530  * uci_list_entry: casts an uci_list pointer to the containing struct.
531  * @_type: config, section or option
532  * @_ptr: pointer to the uci_list struct
533  */
534 #define list_to_element(ptr) \
535         container_of(ptr, struct uci_element, list)
536
537 /**
538  * uci_foreach_entry: loop through a list of uci elements
539  * @_list: pointer to the uci_list struct
540  * @_ptr: iteration variable, struct uci_element
541  *
542  * use like a for loop, e.g:
543  *   uci_foreach(&list, p) {
544  *      ...
545  *   }
546  */
547 #define uci_foreach_element(_list, _ptr)                \
548         for(_ptr = list_to_element((_list)->next);      \
549                 &_ptr->list != (_list);                 \
550                 _ptr = list_to_element(_ptr->list.next))
551
552 /**
553  * uci_foreach_entry_safe: like uci_foreach_safe, but safe for deletion
554  * @_list: pointer to the uci_list struct
555  * @_tmp: temporary variable, struct uci_element *
556  * @_ptr: iteration variable, struct uci_element *
557  *
558  * use like a for loop, e.g:
559  *   uci_foreach(&list, p) {
560  *      ...
561  *   }
562  */
563 #define uci_foreach_element_safe(_list, _tmp, _ptr)             \
564         for(_ptr = list_to_element((_list)->next),              \
565                 _tmp = list_to_element(_ptr->list.next);        \
566                 &_ptr->list != (_list);                 \
567                 _ptr = _tmp, _tmp = list_to_element(_ptr->list.next))
568
569 /**
570  * uci_list_empty: returns true if a list is empty
571  * @list: list head
572  */
573 #define uci_list_empty(list) ((list)->next == (list))
574
575 /* wrappers for dynamic type handling */
576 #define uci_type_backend UCI_TYPE_BACKEND
577 #define uci_type_delta UCI_TYPE_DELTA
578 #define uci_type_package UCI_TYPE_PACKAGE
579 #define uci_type_section UCI_TYPE_SECTION
580 #define uci_type_option UCI_TYPE_OPTION
581
582 /* element typecasting */
583 #ifdef UCI_DEBUG_TYPECAST
584 static const char *uci_typestr[] = {
585         [uci_type_backend] = "backend",
586         [uci_type_delta] = "delta",
587         [uci_type_package] = "package",
588         [uci_type_section] = "section",
589         [uci_type_option] = "option",
590 };
591
592 static void uci_typecast_error(int from, int to)
593 {
594         fprintf(stderr, "Invalid typecast from '%s' to '%s'\n", uci_typestr[from], uci_typestr[to]);
595 }
596
597 #define BUILD_CAST(_type) \
598         static inline struct uci_ ## _type *uci_to_ ## _type (struct uci_element *e) \
599         { \
600                 if (e->type != uci_type_ ## _type) { \
601                         uci_typecast_error(e->type, uci_type_ ## _type); \
602                 } \
603                 return (struct uci_ ## _type *) e; \
604         }
605
606 BUILD_CAST(backend)
607 BUILD_CAST(delta)
608 BUILD_CAST(package)
609 BUILD_CAST(section)
610 BUILD_CAST(option)
611
612 #else
613 #define uci_to_backend(ptr) container_of(ptr, struct uci_backend, e)
614 #define uci_to_delta(ptr) container_of(ptr, struct uci_delta, e)
615 #define uci_to_package(ptr) container_of(ptr, struct uci_package, e)
616 #define uci_to_section(ptr) container_of(ptr, struct uci_section, e)
617 #define uci_to_option(ptr)  container_of(ptr, struct uci_option, e)
618 #endif
619
620 /**
621  * uci_alloc_element: allocate a generic uci_element, reserve a buffer and typecast
622  * @ctx: uci context
623  * @type: {package,section,option}
624  * @name: string containing the name of the element
625  * @datasize: additional buffer size to reserve at the end of the struct
626  */
627 #define uci_alloc_element(ctx, type, name, datasize) \
628         uci_to_ ## type (uci_alloc_generic(ctx, uci_type_ ## type, name, sizeof(struct uci_ ## type) + datasize))
629
630 #define uci_dataptr(ptr) \
631         (((char *) ptr) + sizeof(*ptr))
632
633 /**
634  * uci_lookup_package: look up a package
635  * @ctx: uci context
636  * @name: name of the package
637  */
638 static inline struct uci_package *
639 uci_lookup_package(struct uci_context *ctx, const char *name)
640 {
641         struct uci_element *e = NULL;
642         if (uci_lookup_next(ctx, &e, &ctx->root, name) == 0)
643                 return uci_to_package(e);
644         else
645                 return NULL;
646 }
647
648 /**
649  * uci_lookup_section: look up a section
650  * @ctx: uci context
651  * @p: package that the section belongs to
652  * @name: name of the section
653  */
654 static inline struct uci_section *
655 uci_lookup_section(struct uci_context *ctx, struct uci_package *p, const char *name)
656 {
657         struct uci_element *e = NULL;
658         if (uci_lookup_next(ctx, &e, &p->sections, name) == 0)
659                 return uci_to_section(e);
660         else
661                 return NULL;
662 }
663
664 /**
665  * uci_lookup_option: look up an option
666  * @ctx: uci context
667  * @section: section that the option belongs to
668  * @name: name of the option
669  */
670 static inline struct uci_option *
671 uci_lookup_option(struct uci_context *ctx, struct uci_section *s, const char *name)
672 {
673         struct uci_element *e = NULL;
674         if (uci_lookup_next(ctx, &e, &s->options, name) == 0)
675                 return uci_to_option(e);
676         else
677                 return NULL;
678 }
679
680 static inline const char *
681 uci_lookup_option_string(struct uci_context *ctx, struct uci_section *s, const char *name)
682 {
683         struct uci_option *o;
684
685         o = uci_lookup_option(ctx, s, name);
686         if (!o || o->type != UCI_TYPE_STRING)
687                 return NULL;
688
689         return o->v.string;
690 }
691
692 #ifdef __cplusplus
693 }
694 #endif
695
696 #endif