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