print the reason for parse errors in error messages
[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 General Public License for more details.
13 */
14
15 #ifndef __LIBUCI_H
16 #define __LIBUCI_H
17
18 #include <setjmp.h>
19 #include <stdio.h>
20
21 #define UCI_CONFDIR "/etc/config"
22
23 enum
24 {
25 UCI_OK = 0,
26 UCI_ERR_MEM,
27 UCI_ERR_INVAL,
28 UCI_ERR_NOTFOUND,
29 UCI_ERR_IO,
30 UCI_ERR_PARSE,
31 UCI_ERR_UNKNOWN,
32 UCI_ERR_LAST
33 };
34
35 struct uci_list
36 {
37 void *next;
38 void *prev;
39 };
40
41 struct uci_config;
42 struct uci_section;
43 struct uci_option;
44 struct uci_parse_context;
45
46
47 /**
48 * uci_alloc: Allocate a new uci context
49 */
50 extern struct uci_context *uci_alloc(void);
51
52 /**
53 * uci_perror: Print the last uci error that occured
54 * @ctx: uci context
55 * @str: string to print before the error message
56 */
57 extern void uci_perror(struct uci_context *ctx, const char *str);
58
59 /**
60 * uci_load: Parse an uci config file and store it in the uci context
61 *
62 * @ctx: uci context
63 * @name: name of the config file (relative to the config directory)
64 */
65 int uci_load(struct uci_context *ctx, const char *name);
66
67 /**
68 * uci_cleanup: Clean up after an error
69 *
70 * @ctx: uci context
71 */
72 int uci_cleanup(struct uci_context *ctx);
73
74
75 /* UCI data structures */
76
77 struct uci_context
78 {
79 struct uci_list root;
80
81 /* for error handling only */
82 struct uci_parse_context *pctx;
83
84 /* private: */
85 int errno;
86 jmp_buf trap;
87 jmp_buf trap_saved;
88 int saved;
89 };
90
91 struct uci_parse_context
92 {
93 int line;
94 int byte;
95
96 /* private: */
97 struct uci_config *cfg;
98 FILE *file;
99 char *buf;
100 char *reason;
101 int bufsz;
102 };
103
104 struct uci_config
105 {
106 struct uci_list list;
107 struct uci_list sections;
108 struct uci_context *ctx;
109 char *name;
110 };
111
112 struct uci_section
113 {
114 struct uci_list list;
115 struct uci_list options;
116 struct uci_config *config;
117 char *type;
118 char *name;
119 };
120
121 struct uci_option
122 {
123 struct uci_list list;
124 struct uci_section *section;
125 char *name;
126 char *value;
127 };
128
129 /* linked list handling */
130 #ifndef offsetof
131 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
132 #endif
133
134 #define uci_list_empty(list) (list->next == ptr)
135 #define uci_list_entry(_type, _ptr) \
136 ((struct uci_ ## _type *) ((char *)(_ptr) - offsetof(struct uci_ ## _type,list)))
137
138 #define uci_foreach_entry(_type, _list, _ptr) \
139 for(_ptr = uci_list_entry(_type, (_list)->next); \
140 &_ptr->list != (_list); \
141 _ptr = uci_list_entry(_type, _ptr->list.next))
142
143 #endif