remove internal usage of redundant uci_ptr.last
[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 #include <stddef.h>
38
39 #define UCI_CONFDIR "/etc/config"
40 #define UCI_SAVEDIR "/tmp/.uci"
41 #define UCI_DIRMODE 0700
42 #define UCI_FILEMODE 0600
43
44 enum
45 {
46 UCI_OK = 0,
47 UCI_ERR_MEM,
48 UCI_ERR_INVAL,
49 UCI_ERR_NOTFOUND,
50 UCI_ERR_IO,
51 UCI_ERR_PARSE,
52 UCI_ERR_DUPLICATE,
53 UCI_ERR_UNKNOWN,
54 UCI_ERR_LAST
55 };
56
57 struct uci_list;
58 struct uci_list
59 {
60 struct uci_list *next;
61 struct uci_list *prev;
62 };
63
64 struct uci_ptr;
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 * This function returns UCI_ERR_NOTFOUND if the package specified in the tuple
160 * string cannot be found. Otherwise it will return UCI_OK.
161 *
162 * Note that failures in looking up other parts, if they are also specfied,
163 * including section and option, will also have a return value UCI_OK but with
164 * ptr->flags * UCI_LOOKUP_COMPLETE not set.
165 */
166 extern int uci_lookup_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str, bool extended);
167
168 /**
169 * uci_add_section: Add an unnamed section
170 * @ctx: uci context
171 * @p: package to add the section to
172 * @type: section type
173 * @res: pointer to store a reference to the new section in
174 */
175 extern int uci_add_section(struct uci_context *ctx, struct uci_package *p, const char *type, struct uci_section **res);
176
177 /**
178 * uci_set: Set an element's value; create the element if necessary
179 * @ctx: uci context
180 * @ptr: uci pointer
181 *
182 * The updated/created element is stored in ptr->last
183 */
184 extern int uci_set(struct uci_context *ctx, struct uci_ptr *ptr);
185
186 /**
187 * uci_add_list: Append a string to an element list
188 * @ctx: uci context
189 * @ptr: uci pointer (with value)
190 *
191 * Note: if the given option already contains a string value,
192 * it will be converted to an 1-element-list before appending the next element
193 */
194 extern int uci_add_list(struct uci_context *ctx, struct uci_ptr *ptr);
195
196 /**
197 * uci_del_list: Remove a string from an element list
198 * @ctx: uci context
199 * @ptr: uci pointer (with value)
200 *
201 */
202 extern int uci_del_list(struct uci_context *ctx, struct uci_ptr *ptr);
203
204 /**
205 * uci_reorder: Reposition a section
206 * @ctx: uci context
207 * @s: uci section to reposition
208 * @pos: new position in the section list
209 */
210 extern int uci_reorder_section(struct uci_context *ctx, struct uci_section *s, int pos);
211
212 /**
213 * uci_rename: Rename an element
214 * @ctx: uci context
215 * @ptr: uci pointer (with value)
216 */
217 extern int uci_rename(struct uci_context *ctx, struct uci_ptr *ptr);
218
219 /**
220 * uci_delete: Delete a section or option
221 * @ctx: uci context
222 * @ptr: uci pointer
223 */
224 extern int uci_delete(struct uci_context *ctx, struct uci_ptr *ptr);
225
226 /**
227 * uci_save: save change delta for a package
228 * @ctx: uci context
229 * @p: uci_package struct
230 */
231 extern int uci_save(struct uci_context *ctx, struct uci_package *p);
232
233 /**
234 * uci_commit: commit changes to a package
235 * @ctx: uci context
236 * @p: uci_package struct pointer
237 * @overwrite: overwrite existing config data and flush delta
238 *
239 * committing may reload the whole uci_package data,
240 * the supplied pointer is updated accordingly
241 */
242 extern int uci_commit(struct uci_context *ctx, struct uci_package **p, bool overwrite);
243
244 /**
245 * uci_list_configs: List available uci config files
246 * @ctx: uci context
247 *
248 * caller is responsible for freeing the allocated memory behind list
249 */
250 extern int uci_list_configs(struct uci_context *ctx, char ***list);
251
252 /**
253 * uci_set_savedir: override the default delta save directory
254 * @ctx: uci context
255 * @dir: directory name
256 *
257 * This will also try adding the specified dir to the end of delta pathes.
258 */
259 extern int uci_set_savedir(struct uci_context *ctx, const char *dir);
260
261 /**
262 * uci_set_savedir: override the default config storage directory
263 * @ctx: uci context
264 * @dir: directory name
265 */
266 extern int uci_set_confdir(struct uci_context *ctx, const char *dir);
267
268 /**
269 * uci_add_delta_path: add a directory to the search path for change delta files
270 * @ctx: uci context
271 * @dir: directory name
272 *
273 * This function allows you to add directories, which contain 'overlays'
274 * for the active config, that will never be committed.
275 *
276 * Adding a duplicate directory will cause UCI_ERR_DUPLICATE be returned.
277 */
278 extern int uci_add_delta_path(struct uci_context *ctx, const char *dir);
279
280 /**
281 * uci_revert: revert all changes to a config item
282 * @ctx: uci context
283 * @ptr: uci pointer
284 */
285 extern int uci_revert(struct uci_context *ctx, struct uci_ptr *ptr);
286
287 /**
288 * uci_parse_argument: parse a shell-style argument, with an arbitrary quoting style
289 * @ctx: uci context
290 * @stream: input stream
291 * @str: pointer to the current line (use NULL for parsing the next line)
292 * @result: pointer for the result
293 */
294 extern int uci_parse_argument(struct uci_context *ctx, FILE *stream, char **str, char **result);
295
296 /**
297 * uci_set_backend: change the default backend
298 * @ctx: uci context
299 * @name: name of the backend
300 *
301 * The default backend is "file", which uses /etc/config for config storage
302 */
303 extern int uci_set_backend(struct uci_context *ctx, const char *name);
304
305 /**
306 * uci_validate_text: validate a value string for uci options
307 * @str: value
308 *
309 * this function checks whether a given string is acceptable as value
310 * for uci options
311 */
312 extern bool uci_validate_text(const char *str);
313
314 /**
315 * uci_parse_ptr: parse a uci string into a uci_ptr
316 * @ctx: uci context
317 * @ptr: target data structure
318 * @str: string to parse
319 *
320 * str is modified by this function
321 */
322 int uci_parse_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str);
323
324 /**
325 * uci_lookup_next: lookup a child element
326 * @ctx: uci context
327 * @e: target element pointer
328 * @list: list of elements
329 * @name: name of the child element
330 *
331 * if parent is NULL, the function looks up the package with the given name
332 */
333 int uci_lookup_next(struct uci_context *ctx, struct uci_element **e, struct uci_list *list, const char *name);
334
335 /**
336 * uci_parse_section: look up a set of options
337 * @s: uci section
338 * @opts: list of options to look up
339 * @n_opts: number of options to look up
340 * @tb: array of pointers to found options
341 */
342 void uci_parse_section(struct uci_section *s, const struct uci_parse_option *opts,
343 int n_opts, struct uci_option **tb);
344
345 /**
346 * uci_hash_options: build a hash over a list of options
347 * @tb: list of option pointers
348 * @n_opts: number of options
349 */
350 uint32_t uci_hash_options(struct uci_option **tb, int n_opts);
351
352
353 /* UCI data structures */
354 enum uci_type {
355 UCI_TYPE_UNSPEC = 0,
356 UCI_TYPE_DELTA = 1,
357 UCI_TYPE_PACKAGE = 2,
358 UCI_TYPE_SECTION = 3,
359 UCI_TYPE_OPTION = 4,
360 UCI_TYPE_PATH = 5,
361 UCI_TYPE_BACKEND = 6,
362 UCI_TYPE_ITEM = 7,
363 UCI_TYPE_HOOK = 8,
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
427 struct uci_package
428 {
429 struct uci_element e;
430 struct uci_list sections;
431 struct uci_context *ctx;
432 bool has_delta;
433 char *path;
434
435 /* private: */
436 struct uci_backend *backend;
437 void *priv;
438 int n_section;
439 struct uci_list delta;
440 struct uci_list saved_delta;
441 };
442
443 struct uci_section
444 {
445 struct uci_element e;
446 struct uci_list options;
447 struct uci_package *package;
448 bool anonymous;
449 char *type;
450 };
451
452 struct uci_option
453 {
454 struct uci_element e;
455 struct uci_section *section;
456 enum uci_option_type type;
457 union {
458 struct uci_list list;
459 char *string;
460 } v;
461 };
462
463 /*
464 * UCI_CMD_ADD is used for anonymous sections or list values
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 UCI_CMD_LIST_DEL,
474 __UCI_CMD_MAX,
475 __UCI_CMD_LAST = __UCI_CMD_MAX - 1
476 };
477 extern char const uci_command_char[];
478
479 struct uci_delta
480 {
481 struct uci_element e;
482 enum uci_command cmd;
483 char *section;
484 char *value;
485 };
486
487 struct uci_ptr
488 {
489 enum uci_type target;
490 enum {
491 UCI_LOOKUP_DONE = (1 << 0),
492 UCI_LOOKUP_COMPLETE = (1 << 1),
493 UCI_LOOKUP_EXTENDED = (1 << 2),
494 } flags;
495
496 struct uci_package *p;
497 struct uci_section *s;
498 struct uci_option *o;
499 struct uci_element *last;
500
501 const char *package;
502 const char *section;
503 const char *option;
504 const char *value;
505 };
506
507 struct uci_parse_option {
508 const char *name;
509 enum uci_option_type type;
510 };
511
512
513 /**
514 * container_of - cast a member of a structure out to the containing structure
515 * @ptr: the pointer to the member.
516 * @type: the type of the container struct this is embedded in.
517 * @member: the name of the member within the struct.
518 */
519 #ifndef container_of
520 #define container_of(ptr, type, member) \
521 ((type *) ((char *)ptr - offsetof(type,member)))
522 #endif
523
524
525 /**
526 * uci_list_entry: casts an uci_list pointer to the containing struct.
527 * @_type: config, section or option
528 * @_ptr: pointer to the uci_list struct
529 */
530 #define list_to_element(ptr) \
531 container_of(ptr, struct uci_element, list)
532
533 /**
534 * uci_foreach_entry: loop through a list of uci elements
535 * @_list: pointer to the uci_list struct
536 * @_ptr: iteration variable, struct uci_element
537 *
538 * use like a for loop, e.g:
539 * uci_foreach(&list, p) {
540 * ...
541 * }
542 */
543 #define uci_foreach_element(_list, _ptr) \
544 for(_ptr = list_to_element((_list)->next); \
545 &_ptr->list != (_list); \
546 _ptr = list_to_element(_ptr->list.next))
547
548 /**
549 * uci_foreach_entry_safe: like uci_foreach_safe, but safe for deletion
550 * @_list: pointer to the uci_list struct
551 * @_tmp: temporary variable, struct uci_element *
552 * @_ptr: iteration variable, struct uci_element *
553 *
554 * use like a for loop, e.g:
555 * uci_foreach(&list, p) {
556 * ...
557 * }
558 */
559 #define uci_foreach_element_safe(_list, _tmp, _ptr) \
560 for(_ptr = list_to_element((_list)->next), \
561 _tmp = list_to_element(_ptr->list.next); \
562 &_ptr->list != (_list); \
563 _ptr = _tmp, _tmp = list_to_element(_ptr->list.next))
564
565 /**
566 * uci_list_empty: returns true if a list is empty
567 * @list: list head
568 */
569 #define uci_list_empty(list) ((list)->next == (list))
570
571 /* wrappers for dynamic type handling */
572 #define uci_type_backend UCI_TYPE_BACKEND
573 #define uci_type_delta UCI_TYPE_DELTA
574 #define uci_type_package UCI_TYPE_PACKAGE
575 #define uci_type_section UCI_TYPE_SECTION
576 #define uci_type_option UCI_TYPE_OPTION
577
578 /* element typecasting */
579 #ifdef UCI_DEBUG_TYPECAST
580 static const char *uci_typestr[] = {
581 [uci_type_backend] = "backend",
582 [uci_type_delta] = "delta",
583 [uci_type_package] = "package",
584 [uci_type_section] = "section",
585 [uci_type_option] = "option",
586 };
587
588 static void uci_typecast_error(int from, int to)
589 {
590 fprintf(stderr, "Invalid typecast from '%s' to '%s'\n", uci_typestr[from], uci_typestr[to]);
591 }
592
593 #define BUILD_CAST(_type) \
594 static inline struct uci_ ## _type *uci_to_ ## _type (struct uci_element *e) \
595 { \
596 if (e->type != uci_type_ ## _type) { \
597 uci_typecast_error(e->type, uci_type_ ## _type); \
598 } \
599 return (struct uci_ ## _type *) e; \
600 }
601
602 BUILD_CAST(backend)
603 BUILD_CAST(delta)
604 BUILD_CAST(package)
605 BUILD_CAST(section)
606 BUILD_CAST(option)
607
608 #else
609 #define uci_to_backend(ptr) container_of(ptr, struct uci_backend, e)
610 #define uci_to_delta(ptr) container_of(ptr, struct uci_delta, e)
611 #define uci_to_package(ptr) container_of(ptr, struct uci_package, e)
612 #define uci_to_section(ptr) container_of(ptr, struct uci_section, e)
613 #define uci_to_option(ptr) container_of(ptr, struct uci_option, e)
614 #endif
615
616 #define uci_dataptr(ptr) \
617 (((char *) ptr) + sizeof(*ptr))
618
619 /**
620 * uci_lookup_package: look up a package
621 * @ctx: uci context
622 * @name: name of the package
623 */
624 static inline struct uci_package *
625 uci_lookup_package(struct uci_context *ctx, const char *name)
626 {
627 struct uci_element *e = NULL;
628 if (uci_lookup_next(ctx, &e, &ctx->root, name) == 0)
629 return uci_to_package(e);
630 else
631 return NULL;
632 }
633
634 /**
635 * uci_lookup_section: look up a section
636 * @ctx: uci context
637 * @p: package that the section belongs to
638 * @name: name of the section
639 */
640 static inline struct uci_section *
641 uci_lookup_section(struct uci_context *ctx, struct uci_package *p, const char *name)
642 {
643 struct uci_element *e = NULL;
644 if (uci_lookup_next(ctx, &e, &p->sections, name) == 0)
645 return uci_to_section(e);
646 else
647 return NULL;
648 }
649
650 /**
651 * uci_lookup_option: look up an option
652 * @ctx: uci context
653 * @section: section that the option belongs to
654 * @name: name of the option
655 */
656 static inline struct uci_option *
657 uci_lookup_option(struct uci_context *ctx, struct uci_section *s, const char *name)
658 {
659 struct uci_element *e = NULL;
660 if (uci_lookup_next(ctx, &e, &s->options, name) == 0)
661 return uci_to_option(e);
662 else
663 return NULL;
664 }
665
666 static inline const char *
667 uci_lookup_option_string(struct uci_context *ctx, struct uci_section *s, const char *name)
668 {
669 struct uci_option *o;
670
671 o = uci_lookup_option(ctx, s, name);
672 if (!o || o->type != UCI_TYPE_STRING)
673 return NULL;
674
675 return o->v.string;
676 }
677
678 #ifndef BITS_PER_LONG
679 #define BITS_PER_LONG (8 * sizeof(unsigned long))
680 #endif
681
682 static inline void uci_bitfield_set(unsigned long *bits, int bit)
683 {
684 bits[bit / BITS_PER_LONG] |= (1UL << (bit % BITS_PER_LONG));
685 }
686
687 #ifdef __cplusplus
688 }
689 #endif
690
691 #endif