replace the existing build system with cmake
[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 #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
37 #define UCI_CONFDIR "/etc/config"
38 #define UCI_SAVEDIR "/tmp/.uci"
39 #define UCI_DIRMODE 0700
40 #define UCI_FILEMODE 0600
41
42 enum
43 {
44 UCI_OK = 0,
45 UCI_ERR_MEM,
46 UCI_ERR_INVAL,
47 UCI_ERR_NOTFOUND,
48 UCI_ERR_IO,
49 UCI_ERR_PARSE,
50 UCI_ERR_DUPLICATE,
51 UCI_ERR_UNKNOWN,
52 UCI_ERR_LAST
53 };
54
55 struct uci_list;
56 struct uci_list
57 {
58 struct uci_list *next;
59 struct uci_list *prev;
60 };
61
62 struct uci_ptr;
63 struct uci_plugin;
64 struct uci_hook_ops;
65 struct uci_element;
66 struct uci_package;
67 struct uci_section;
68 struct uci_option;
69 struct uci_delta;
70 struct uci_context;
71 struct uci_backend;
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 extern int uci_lookup_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str, bool extended);
159
160 /**
161 * uci_add_section: Add an unnamed section
162 * @ctx: uci context
163 * @p: package to add the section to
164 * @type: section type
165 * @res: pointer to store a reference to the new section in
166 */
167 extern int uci_add_section(struct uci_context *ctx, struct uci_package *p, const char *type, struct uci_section **res);
168
169 /**
170 * uci_set: Set an element's value; create the element if necessary
171 * @ctx: uci context
172 * @ptr: uci pointer
173 *
174 * The updated/created element is stored in ptr->last
175 */
176 extern int uci_set(struct uci_context *ctx, struct uci_ptr *ptr);
177
178 /**
179 * uci_add_list: Append a string to an element list
180 * @ctx: uci context
181 * @ptr: uci pointer (with value)
182 *
183 * Note: if the given option already contains a string value,
184 * it will be converted to an 1-element-list before appending the next element
185 */
186 extern int uci_add_list(struct uci_context *ctx, struct uci_ptr *ptr);
187
188 /**
189 * uci_reorder: Reposition a section
190 * @ctx: uci context
191 * @s: uci section to reposition
192 * @pos: new position in the section list
193 */
194 extern int uci_reorder_section(struct uci_context *ctx, struct uci_section *s, int pos);
195
196 /**
197 * uci_rename: Rename an element
198 * @ctx: uci context
199 * @ptr: uci pointer (with value)
200 */
201 extern int uci_rename(struct uci_context *ctx, struct uci_ptr *ptr);
202
203 /**
204 * uci_delete: Delete a section or option
205 * @ctx: uci context
206 * @ptr: uci pointer
207 */
208 extern int uci_delete(struct uci_context *ctx, struct uci_ptr *ptr);
209
210 /**
211 * uci_save: save change delta for a package
212 * @ctx: uci context
213 * @p: uci_package struct
214 */
215 extern int uci_save(struct uci_context *ctx, struct uci_package *p);
216
217 /**
218 * uci_commit: commit changes to a package
219 * @ctx: uci context
220 * @p: uci_package struct pointer
221 * @overwrite: overwrite existing config data and flush delta
222 *
223 * committing may reload the whole uci_package data,
224 * the supplied pointer is updated accordingly
225 */
226 extern int uci_commit(struct uci_context *ctx, struct uci_package **p, bool overwrite);
227
228 /**
229 * uci_list_configs: List available uci config files
230 * @ctx: uci context
231 *
232 * caller is responsible for freeing the allocated memory behind list
233 */
234 extern int uci_list_configs(struct uci_context *ctx, char ***list);
235
236 /**
237 * uci_set_savedir: override the default delta save directory
238 * @ctx: uci context
239 * @dir: directory name
240 */
241 extern int uci_set_savedir(struct uci_context *ctx, const char *dir);
242
243 /**
244 * uci_set_savedir: override the default config storage directory
245 * @ctx: uci context
246 * @dir: directory name
247 */
248 extern int uci_set_confdir(struct uci_context *ctx, const char *dir);
249
250 /**
251 * uci_add_delta_path: add a directory to the search path for change delta files
252 * @ctx: uci context
253 * @dir: directory name
254 *
255 * This function allows you to add directories, which contain 'overlays'
256 * for the active config, that will never be committed.
257 */
258 extern int uci_add_delta_path(struct uci_context *ctx, const char *dir);
259
260 /**
261 * uci_revert: revert all changes to a config item
262 * @ctx: uci context
263 * @ptr: uci pointer
264 */
265 extern int uci_revert(struct uci_context *ctx, struct uci_ptr *ptr);
266
267 /**
268 * uci_parse_argument: parse a shell-style argument, with an arbitrary quoting style
269 * @ctx: uci context
270 * @stream: input stream
271 * @str: pointer to the current line (use NULL for parsing the next line)
272 * @result: pointer for the result
273 */
274 extern int uci_parse_argument(struct uci_context *ctx, FILE *stream, char **str, char **result);
275
276 /**
277 * uci_set_backend: change the default backend
278 * @ctx: uci context
279 * @name: name of the backend
280 *
281 * The default backend is "file", which uses /etc/config for config storage
282 */
283 extern int uci_set_backend(struct uci_context *ctx, const char *name);
284
285 /**
286 * uci_validate_text: validate a value string for uci options
287 * @str: value
288 *
289 * this function checks whether a given string is acceptable as value
290 * for uci options
291 */
292 extern bool uci_validate_text(const char *str);
293
294
295 /**
296 * uci_add_hook: add a uci hook
297 * @ctx: uci context
298 * @ops: uci hook ops
299 *
300 * NB: allocated and freed by the caller
301 */
302 extern int uci_add_hook(struct uci_context *ctx, const struct uci_hook_ops *ops);
303
304 /**
305 * uci_remove_hook: remove a uci hook
306 * @ctx: uci context
307 * @ops: uci hook ops
308 */
309 extern int uci_remove_hook(struct uci_context *ctx, const struct uci_hook_ops *ops);
310
311 /**
312 * uci_load_plugin: load an uci plugin
313 * @ctx: uci context
314 * @filename: path to the uci plugin
315 *
316 * NB: plugin will be unloaded automatically when the context is freed
317 */
318 int uci_load_plugin(struct uci_context *ctx, const char *filename);
319
320 /**
321 * uci_load_plugins: load all uci plugins from a directory
322 * @ctx: uci context
323 * @pattern: pattern of uci plugin files (optional)
324 *
325 * if pattern is NULL, then uci_load_plugins will call uci_load_plugin
326 * for uci_*.so in <prefix>/lib/
327 */
328 int uci_load_plugins(struct uci_context *ctx, const char *pattern);
329
330 /**
331 * uci_parse_ptr: parse a uci string into a uci_ptr
332 * @ctx: uci context
333 * @ptr: target data structure
334 * @str: string to parse
335 *
336 * str is modified by this function
337 */
338 int uci_parse_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str);
339
340 /**
341 * uci_lookup_next: lookup a child element
342 * @ctx: uci context
343 * @e: target element pointer
344 * @list: list of elements
345 * @name: name of the child element
346 *
347 * if parent is NULL, the function looks up the package with the given name
348 */
349 int uci_lookup_next(struct uci_context *ctx, struct uci_element **e, struct uci_list *list, const char *name);
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 UCI_TYPE_PLUGIN = 9,
364 };
365
366 enum uci_option_type {
367 UCI_TYPE_STRING = 0,
368 UCI_TYPE_LIST = 1,
369 };
370
371 enum uci_flags {
372 UCI_FLAG_STRICT = (1 << 0), /* strict mode for the parser */
373 UCI_FLAG_PERROR = (1 << 1), /* print parser error messages */
374 UCI_FLAG_EXPORT_NAME = (1 << 2), /* when exporting, name unnamed sections */
375 UCI_FLAG_SAVED_DELTA = (1 << 3), /* store the saved delta in memory as well */
376 };
377
378 struct uci_element
379 {
380 struct uci_list list;
381 enum uci_type type;
382 char *name;
383 };
384
385 struct uci_backend
386 {
387 struct uci_element e;
388 char **(*list_configs)(struct uci_context *ctx);
389 struct uci_package *(*load)(struct uci_context *ctx, const char *name);
390 void (*commit)(struct uci_context *ctx, struct uci_package **p, bool overwrite);
391
392 /* private: */
393 const void *ptr;
394 void *priv;
395 };
396
397 struct uci_context
398 {
399 /* list of config packages */
400 struct uci_list root;
401
402 /* parser context, use for error handling only */
403 struct uci_parse_context *pctx;
404
405 /* backend for import and export */
406 struct uci_backend *backend;
407 struct uci_list backends;
408
409 /* uci runtime flags */
410 enum uci_flags flags;
411
412 char *confdir;
413 char *savedir;
414
415 /* search path for delta files */
416 struct uci_list delta_path;
417
418 /* private: */
419 int err;
420 const char *func;
421 jmp_buf trap;
422 bool internal, nested;
423 char *buf;
424 int bufsz;
425
426 struct uci_list hooks;
427 struct uci_list plugins;
428 };
429
430 struct uci_package
431 {
432 struct uci_element e;
433 struct uci_list sections;
434 struct uci_context *ctx;
435 bool has_delta;
436 char *path;
437
438 /* private: */
439 struct uci_backend *backend;
440 void *priv;
441 int n_section;
442 struct uci_list delta;
443 struct uci_list saved_delta;
444 };
445
446 struct uci_section
447 {
448 struct uci_element e;
449 struct uci_list options;
450 struct uci_package *package;
451 bool anonymous;
452 char *type;
453 };
454
455 struct uci_option
456 {
457 struct uci_element e;
458 struct uci_section *section;
459 enum uci_option_type type;
460 union {
461 struct uci_list list;
462 char *string;
463 } v;
464 };
465
466 enum uci_command {
467 UCI_CMD_ADD,
468 UCI_CMD_REMOVE,
469 UCI_CMD_CHANGE,
470 UCI_CMD_RENAME,
471 UCI_CMD_REORDER,
472 UCI_CMD_LIST_ADD,
473 };
474
475 struct uci_delta
476 {
477 struct uci_element e;
478 enum uci_command cmd;
479 char *section;
480 char *value;
481 };
482
483 struct uci_ptr
484 {
485 enum uci_type target;
486 enum {
487 UCI_LOOKUP_DONE = (1 << 0),
488 UCI_LOOKUP_COMPLETE = (1 << 1),
489 UCI_LOOKUP_EXTENDED = (1 << 2),
490 } flags;
491
492 struct uci_package *p;
493 struct uci_section *s;
494 struct uci_option *o;
495 struct uci_element *last;
496
497 const char *package;
498 const char *section;
499 const char *option;
500 const char *value;
501 };
502
503 struct uci_hook_ops
504 {
505 void (*load)(const struct uci_hook_ops *ops, struct uci_package *p);
506 void (*set)(const struct uci_hook_ops *ops, struct uci_package *p, struct uci_delta *e);
507 };
508
509 struct uci_hook
510 {
511 struct uci_element e;
512 const struct uci_hook_ops *ops;
513 };
514
515 struct uci_plugin_ops
516 {
517 int (*attach)(struct uci_context *ctx);
518 void (*detach)(struct uci_context *ctx);
519 };
520
521 struct uci_plugin
522 {
523 struct uci_element e;
524 const struct uci_plugin_ops *ops;
525 void *dlh;
526 };
527
528
529 /* linked list handling */
530 #ifndef offsetof
531 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
532 #endif
533
534 /**
535 * container_of - cast a member of a structure out to the containing structure
536 * @ptr: the pointer to the member.
537 * @type: the type of the container struct this is embedded in.
538 * @member: the name of the member within the struct.
539 */
540 #ifndef container_of
541 #define container_of(ptr, type, member) \
542 ((type *) ((char *)ptr - offsetof(type,member)))
543 #endif
544
545
546 /**
547 * uci_list_entry: casts an uci_list pointer to the containing struct.
548 * @_type: config, section or option
549 * @_ptr: pointer to the uci_list struct
550 */
551 #define list_to_element(ptr) \
552 container_of(ptr, struct uci_element, list)
553
554 /**
555 * uci_foreach_entry: loop through a list of uci elements
556 * @_list: pointer to the uci_list struct
557 * @_ptr: iteration variable, struct uci_element
558 *
559 * use like a for loop, e.g:
560 * uci_foreach(&list, p) {
561 * ...
562 * }
563 */
564 #define uci_foreach_element(_list, _ptr) \
565 for(_ptr = list_to_element((_list)->next); \
566 &_ptr->list != (_list); \
567 _ptr = list_to_element(_ptr->list.next))
568
569 /**
570 * uci_foreach_entry_safe: like uci_foreach_safe, but safe for deletion
571 * @_list: pointer to the uci_list struct
572 * @_tmp: temporary variable, struct uci_element *
573 * @_ptr: iteration variable, struct uci_element *
574 *
575 * use like a for loop, e.g:
576 * uci_foreach(&list, p) {
577 * ...
578 * }
579 */
580 #define uci_foreach_element_safe(_list, _tmp, _ptr) \
581 for(_ptr = list_to_element((_list)->next), \
582 _tmp = list_to_element(_ptr->list.next); \
583 &_ptr->list != (_list); \
584 _ptr = _tmp, _tmp = list_to_element(_ptr->list.next))
585
586 /**
587 * uci_list_empty: returns true if a list is empty
588 * @list: list head
589 */
590 #define uci_list_empty(list) ((list)->next == (list))
591
592 /* wrappers for dynamic type handling */
593 #define uci_type_backend UCI_TYPE_BACKEND
594 #define uci_type_delta UCI_TYPE_DELTA
595 #define uci_type_package UCI_TYPE_PACKAGE
596 #define uci_type_section UCI_TYPE_SECTION
597 #define uci_type_option UCI_TYPE_OPTION
598 #define uci_type_hook UCI_TYPE_HOOK
599 #define uci_type_plugin UCI_TYPE_PLUGIN
600
601 /* element typecasting */
602 #ifdef UCI_DEBUG_TYPECAST
603 static const char *uci_typestr[] = {
604 [uci_type_backend] = "backend",
605 [uci_type_delta] = "delta",
606 [uci_type_package] = "package",
607 [uci_type_section] = "section",
608 [uci_type_option] = "option",
609 [uci_type_hook] = "hook",
610 [uci_type_plugin] = "plugin",
611 };
612
613 static void uci_typecast_error(int from, int to)
614 {
615 fprintf(stderr, "Invalid typecast from '%s' to '%s'\n", uci_typestr[from], uci_typestr[to]);
616 }
617
618 #define BUILD_CAST(_type) \
619 static inline struct uci_ ## _type *uci_to_ ## _type (struct uci_element *e) \
620 { \
621 if (e->type != uci_type_ ## _type) { \
622 uci_typecast_error(e->type, uci_type_ ## _type); \
623 } \
624 return (struct uci_ ## _type *) e; \
625 }
626
627 BUILD_CAST(backend)
628 BUILD_CAST(delta)
629 BUILD_CAST(package)
630 BUILD_CAST(section)
631 BUILD_CAST(option)
632 BUILD_CAST(hook)
633 BUILD_CAST(plugin)
634
635 #else
636 #define uci_to_backend(ptr) container_of(ptr, struct uci_backend, e)
637 #define uci_to_delta(ptr) container_of(ptr, struct uci_delta, e)
638 #define uci_to_package(ptr) container_of(ptr, struct uci_package, e)
639 #define uci_to_section(ptr) container_of(ptr, struct uci_section, e)
640 #define uci_to_option(ptr) container_of(ptr, struct uci_option, e)
641 #define uci_to_hook(ptr) container_of(ptr, struct uci_hook, e)
642 #define uci_to_plugin(ptr) container_of(ptr, struct uci_plugin, e)
643 #endif
644
645 /**
646 * uci_alloc_element: allocate a generic uci_element, reserve a buffer and typecast
647 * @ctx: uci context
648 * @type: {package,section,option}
649 * @name: string containing the name of the element
650 * @datasize: additional buffer size to reserve at the end of the struct
651 */
652 #define uci_alloc_element(ctx, type, name, datasize) \
653 uci_to_ ## type (uci_alloc_generic(ctx, uci_type_ ## type, name, sizeof(struct uci_ ## type) + datasize))
654
655 #define uci_dataptr(ptr) \
656 (((char *) ptr) + sizeof(*ptr))
657
658 /**
659 * uci_lookup_package: look up a package
660 * @ctx: uci context
661 * @name: name of the package
662 */
663 static inline struct uci_package *
664 uci_lookup_package(struct uci_context *ctx, const char *name)
665 {
666 struct uci_element *e = NULL;
667 if (uci_lookup_next(ctx, &e, &ctx->root, name) == 0)
668 return uci_to_package(e);
669 else
670 return NULL;
671 }
672
673 /**
674 * uci_lookup_section: look up a section
675 * @ctx: uci context
676 * @p: package that the section belongs to
677 * @name: name of the section
678 */
679 static inline struct uci_section *
680 uci_lookup_section(struct uci_context *ctx, struct uci_package *p, const char *name)
681 {
682 struct uci_element *e = NULL;
683 if (uci_lookup_next(ctx, &e, &p->sections, name) == 0)
684 return uci_to_section(e);
685 else
686 return NULL;
687 }
688
689 /**
690 * uci_lookup_option: look up an option
691 * @ctx: uci context
692 * @section: section that the option belongs to
693 * @name: name of the option
694 */
695 static inline struct uci_option *
696 uci_lookup_option(struct uci_context *ctx, struct uci_section *s, const char *name)
697 {
698 struct uci_element *e = NULL;
699 if (uci_lookup_next(ctx, &e, &s->options, name) == 0)
700 return uci_to_option(e);
701 else
702 return NULL;
703 }
704
705 static inline const char *
706 uci_lookup_option_string(struct uci_context *ctx, struct uci_section *s, const char *name)
707 {
708 struct uci_option *o;
709
710 o = uci_lookup_option(ctx, s, name);
711 if (!o || o->type != UCI_TYPE_STRING)
712 return NULL;
713
714 return o->v.string;
715 }
716
717 #ifdef __cplusplus
718 }
719 #endif
720
721 #endif