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