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