96bebf5ebce0e62599503cff3c14f66a3316eb76
[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 /*
19  * you can use these defines to enable debugging behavior for
20  * apps compiled against libuci:
21  *
22  * #define UCI_DEBUG_TYPECAST:
23  *   enable uci_element typecast checking at run time
24  *
25  */
26
27
28 #include <setjmp.h>
29 #include <stdio.h>
30
31 #define UCI_CONFDIR "/etc/config"
32
33 enum
34 {
35         UCI_OK = 0,
36         UCI_ERR_MEM,
37         UCI_ERR_INVAL,
38         UCI_ERR_NOTFOUND,
39         UCI_ERR_IO,
40         UCI_ERR_PARSE,
41         UCI_ERR_UNKNOWN,
42         UCI_ERR_LAST
43 };
44
45 struct uci_list
46 {
47         void *next;
48         void *prev;
49 };
50
51 struct uci_package;
52 struct uci_section;
53 struct uci_option;
54 struct uci_history;
55 struct uci_parse_context;
56
57
58 /**
59  * uci_alloc: Allocate a new uci context
60  */
61 extern struct uci_context *uci_alloc(void);
62
63 /**
64  * uci_free: Free the uci context including all of its data
65  */
66 extern void uci_free(struct uci_context *ctx);
67
68 /**
69  * uci_perror: Print the last uci error that occured
70  * @ctx: uci context
71  * @str: string to print before the error message
72  */
73 extern void uci_perror(struct uci_context *ctx, const char *str);
74
75 /**
76  * uci_import: Import uci config data from a stream
77  * @ctx: uci context
78  * @stream: file stream to import from
79  * @name: (optional) assume the config has the given name
80  * @package: (optional) store the last parsed config package in this variable
81  *
82  * the name parameter is for config files that don't explicitly use the 'package <...>' keyword
83  */
84 extern int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package);
85
86 /**
87  * uci_export: Export one or all uci config packages
88  * @ctx: uci context
89  * @stream: output stream
90  * @package: (optional) uci config package to export
91  */
92 extern int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package);
93
94 /**
95  * uci_load: Parse an uci config file and store it in the uci context
96  *
97  * @ctx: uci context
98  * @name: name of the config file (relative to the config directory)
99  * @package: store the loaded config package in this variable
100  */
101 extern int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package);
102
103 /**
104  * uci_unload: Unload a config file from the uci context
105  *
106  * @ctx: uci context
107  * @name: name of the config file
108  */
109 extern int uci_unload(struct uci_context *ctx, const char *name);
110
111 /**
112  * uci_cleanup: Clean up after an error
113  *
114  * @ctx: uci context
115  */
116 extern int uci_cleanup(struct uci_context *ctx);
117
118 /**
119  * uci_list_configs: List available uci config files
120  *
121  * @ctx: uci context
122  */
123 extern char **uci_list_configs(struct uci_context *ctx);
124
125 /* UCI data structures */
126 enum uci_type {
127         UCI_TYPE_PACKAGE,
128         UCI_TYPE_SECTION,
129         UCI_TYPE_OPTION
130 };
131
132 struct uci_element
133 {
134         struct uci_list list;
135         enum uci_type type;
136         char *name;
137 };
138
139 struct uci_context
140 {
141         /* list of config packages */
142         struct uci_list root;
143
144         /* parser context, use for error handling only */
145         struct uci_parse_context *pctx;
146
147         /* private: */
148         int errno;
149         jmp_buf trap;
150         char *buf;
151         int bufsz;
152 };
153
154 struct uci_parse_context
155 {
156         /* error context */
157         const char *reason;
158         int line;
159         int byte;
160
161         /* private: */
162         struct uci_package *package;
163         struct uci_section *section;
164         FILE *file;
165         const char *name;
166         char *buf;
167         int bufsz;
168 };
169
170 struct uci_package
171 {
172         struct uci_element e;
173         struct uci_list sections;
174         struct uci_context *ctx;
175         /* private: */
176         int n_section;
177 };
178
179 struct uci_section
180 {
181         struct uci_element e;
182         struct uci_list options;
183         struct uci_package *package;
184         char *type;
185 };
186
187 struct uci_option
188 {
189         struct uci_element e;
190         struct uci_section *section;
191         char *value;
192 };
193
194 enum uci_command {
195         UCI_CMD_ADD,
196         UCI_CMD_REMOVE,
197         UCI_CMD_CHANGE
198 };
199
200 struct uci_history
201 {
202         struct uci_list list;
203         enum uci_command cmd;
204         union {
205                 struct uci_element element;
206                 struct uci_package package;
207                 struct uci_section section;
208                 struct uci_option option;
209         } data;
210 };
211
212 /* linked list handling */
213 #ifndef offsetof
214 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
215 #endif
216
217 /**
218  * container_of - cast a member of a structure out to the containing structure
219  * @ptr:    the pointer to the member.
220  * @type:   the type of the container struct this is embedded in.
221  * @member: the name of the member within the struct.
222  */
223 #define container_of(ptr, type, member) \
224         ((type *) ((char *)ptr - offsetof(type,member)))
225
226
227 /**
228  * uci_list_entry: casts an uci_list pointer to the containing struct.
229  * @_type: config, section or option
230  * @_ptr: pointer to the uci_list struct
231  */
232 #define element_to(type, ptr) \
233         container_of(ptr, struct uci_ ## type, e)
234
235 #define list_to_element(ptr) \
236         container_of(ptr, struct uci_element, list)
237
238 /**
239  * uci_foreach_entry: loop through a list of uci elements
240  * @_list: pointer to the uci_list struct
241  * @_ptr: iteration variable, struct uci_element
242  *
243  * use like a for loop, e.g:
244  *   uci_foreach(&list, p) {
245  *      ...
246  *   }
247  */
248 #define uci_foreach_element(_list, _ptr)                \
249         for(_ptr = list_to_element((_list)->next);      \
250                 &_ptr->list != (_list);                 \
251                 _ptr = list_to_element(_ptr->list.next))
252
253 /**
254  * uci_foreach_entry_safe: like uci_foreach_safe, but safe for deletion
255  * @_list: pointer to the uci_list struct
256  * @_tmp: temporary variable, struct uci_element *
257  * @_ptr: iteration variable, struct uci_element *
258  *
259  * use like a for loop, e.g:
260  *   uci_foreach(&list, p) {
261  *      ...
262  *   }
263  */
264 #define uci_foreach_element_safe(_list, _tmp, _ptr)             \
265         for(_ptr = list_to_element((_list)->next),              \
266                 _tmp = list_to_element(_ptr->list.next);        \
267                 &_ptr->list != (_list);                 \
268                 _ptr = _tmp, _tmp = list_to_element(_ptr->list.next))
269
270 /* returns true if a list is empty */
271 #define uci_list_empty(list) ((list)->next == (list))
272
273 /* element typecasting */
274 #ifdef UCI_DEBUG_TYPECAST
275 static const char *uci_typestr[] = {
276         [UCI_TYPE_PACKAGE] = "package",
277         [UCI_TYPE_SECTION] = "section",
278         [UCI_TYPE_OPTION] = "option"
279 }
280
281 static void uci_typecast_error(int from, int to)
282 {
283         fprintf(stderr, "Invalid typecast from '%s' to '%s'\n", uci_typestr[from], uci_typestr[to]);
284 }
285
286 #define BUILD_CAST(type, val) \
287         static inline struct uci_ ## type *uci_to_ ## type (struct uci_element *e) \
288         { \
289                 if (e->type != val) { \
290                         uci_typecast_error(e->type, val); \
291                 } \
292                 return (struct uci_ ## type *) e; \
293         }
294
295 BUILD_CAST(package, UCI_TYPE_PACKAGE)
296 BUILD_CAST(section, UCI_TYPE_SECTION)
297 BUILD_CAST(option,  UCI_TYPE_OPTION)
298
299 #else
300 #define uci_to_package(ptr) container_of(ptr, struct uci_package, e)
301 #define uci_to_section(ptr) container_of(ptr, struct uci_section, e)
302 #define uci_to_option(ptr)  container_of(ptr, struct uci_option, e)
303 #endif
304
305 /**
306  * uci_alloc_element: allocate a generic uci_element, reserve a buffer and typecast
307  * @ctx: uci context
308  * @type: {package,section,option}
309  * @name: string containing the name of the element
310  * @datasize: additional buffer size to reserve at the end of the struct
311  */
312 #define uci_alloc_element(ctx, type, name, datasize) \
313         uci_to_ ## type (uci_alloc_generic(ctx, name, sizeof(struct uci_ ## type) + datasize))
314
315 #define uci_dataptr(ptr) \
316         (((char *) ptr) + sizeof(*ptr))
317
318 #endif