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