687454f6989a68549b4d4fcfcb87898ddb918bce
[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_package;
42 struct uci_section;
43 struct uci_option;
44 struct uci_history;
45 struct uci_parse_context;
46
47
48 /**
49 * uci_alloc: Allocate a new uci context
50 */
51 extern struct uci_context *uci_alloc(void);
52
53 /**
54 * uci_free: Free the uci context including all of its data
55 */
56 extern void uci_free(struct uci_context *ctx);
57
58 /**
59 * uci_perror: Print the last uci error that occured
60 * @ctx: uci context
61 * @str: string to print before the error message
62 */
63 extern void uci_perror(struct uci_context *ctx, const char *str);
64
65 /**
66 * uci_import: Import uci config data from a stream
67 * @ctx: uci context
68 * @stream: file stream to import from
69 * @name: (optional) assume the config has the given name
70 * @package: (optional) store the last parsed config package in this variable
71 *
72 * the name parameter is for config files that don't explicitly use the 'package <...>' keyword
73 */
74 extern int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package);
75
76 /**
77 * uci_export: Export one or all uci config packages
78 * @ctx: uci context
79 * @stream: output stream
80 * @package: (optional) uci config package to export
81 */
82 extern int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package);
83
84 /**
85 * uci_load: Parse an uci config file and store it in the uci context
86 *
87 * @ctx: uci context
88 * @name: name of the config file (relative to the config directory)
89 * @package: store the loaded config package in this variable
90 */
91 extern int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package);
92
93 /**
94 * uci_unload: Unload a config file from the uci context
95 *
96 * @ctx: uci context
97 * @name: name of the config file
98 */
99 extern int uci_unload(struct uci_context *ctx, const char *name);
100
101 /**
102 * uci_cleanup: Clean up after an error
103 *
104 * @ctx: uci context
105 */
106 extern int uci_cleanup(struct uci_context *ctx);
107
108 /**
109 * uci_list_configs: List available uci config files
110 *
111 * @ctx: uci context
112 */
113 extern char **uci_list_configs(struct uci_context *ctx);
114
115 /* UCI data structures */
116
117 struct uci_context
118 {
119 /* list of config packages */
120 struct uci_list root;
121
122 /* parser context, use for error handling only */
123 struct uci_parse_context *pctx;
124
125 /* private: */
126 int errno;
127 jmp_buf trap;
128 char *buf;
129 int bufsz;
130 };
131
132 struct uci_parse_context
133 {
134 /* error context */
135 const char *reason;
136 int line;
137 int byte;
138
139 /* private: */
140 struct uci_package *package;
141 struct uci_section *section;
142 FILE *file;
143 const char *name;
144 char *buf;
145 int bufsz;
146 };
147
148 struct uci_package
149 {
150 struct uci_list list;
151 struct uci_list sections;
152 struct uci_context *ctx;
153 char *name;
154 /* private: */
155 int n_section;
156 };
157
158 struct uci_section
159 {
160 struct uci_list list;
161 struct uci_list options;
162 struct uci_package *package;
163 char *type;
164 char *name;
165 };
166
167 struct uci_option
168 {
169 struct uci_list list;
170 struct uci_section *section;
171 char *name;
172 char *value;
173 };
174
175 enum uci_type {
176 UCI_TYPE_PACKAGE,
177 UCI_TYPE_SECTION,
178 UCI_TYPE_OPTION
179 };
180
181 enum uci_command {
182 UCI_CMD_ADD,
183 UCI_CMD_REMOVE,
184 UCI_CMD_CHANGE
185 };
186
187 struct uci_history
188 {
189 struct uci_list list;
190 enum uci_command cmd;
191 enum uci_type type;
192 union {
193 struct {
194 char *name;
195 } p;
196 struct {
197 char *type;
198 char *name;
199 } c;
200 struct {
201 char *name;
202 char *value;
203 } o;
204 } data;
205 };
206
207 /* linked list handling */
208 #ifndef offsetof
209 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
210 #endif
211
212 /* returns true if a list is empty */
213 #define uci_list_empty(list) ((list)->next == (list))
214
215 /**
216 * uci_list_entry: casts an uci_list pointer to the containing struct.
217 * @_type: config, section or option
218 * @_ptr: pointer to the uci_list struct
219 */
220 #define uci_list_entry(_type, _ptr) \
221 ((struct uci_ ## _type *) ((char *)(_ptr) - offsetof(struct uci_ ## _type,list)))
222
223 /**
224 * uci_foreach_entry: loop through a list of configs, sections or options
225 * @_type: see uci_list_entry
226 * @_list: pointer to the uci_list struct
227 * @_ptr: iteration variable
228 *
229 * use like a for loop, e.g:
230 * uci_foreach(section, &list, p) {
231 * ...
232 * }
233 */
234 #define uci_foreach_entry(_type, _list, _ptr) \
235 for(_ptr = uci_list_entry(_type, (_list)->next); \
236 &_ptr->list != (_list); \
237 _ptr = uci_list_entry(_type, _ptr->list.next))
238
239 #endif