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