add support for listing config files
[project/uci.git] / parse.c
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 /*
16 * This file contains the code for parsing uci config files
17 */
18
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <ctype.h>
24
25 #define LINEBUF 128
26 #define LINEBUF_MAX 4096
27
28 /*
29 * Fetch a new line from the input stream and resize buffer if necessary
30 */
31 static void uci_getln(struct uci_context *ctx)
32 {
33 struct uci_parse_context *pctx = ctx->pctx;
34 char *p;
35 int ofs;
36
37 if (pctx->buf == NULL) {
38 pctx->buf = uci_malloc(ctx, LINEBUF);
39 pctx->bufsz = LINEBUF;
40 }
41
42 ofs = 0;
43 do {
44 p = &pctx->buf[ofs];
45 p[ofs] = 0;
46
47 p = fgets(p, pctx->bufsz - ofs, pctx->file);
48 if (!p || !p[ofs])
49 return;
50
51 ofs += strlen(p);
52 if (pctx->buf[ofs - 1] == '\n') {
53 pctx->line++;
54 pctx->buf[ofs - 1] = 0;
55 return;
56 }
57
58 if (pctx->bufsz > LINEBUF_MAX/2) {
59 pctx->reason = "line too long";
60 pctx->byte = LINEBUF_MAX;
61 UCI_THROW(ctx, UCI_ERR_PARSE);
62 }
63
64 pctx->bufsz *= 2;
65 pctx->buf = uci_realloc(ctx, pctx->buf, pctx->bufsz);
66 } while (1);
67 }
68
69 /*
70 * Clean up all extra memory used by the parser
71 */
72 static void uci_parse_cleanup(struct uci_context *ctx)
73 {
74 struct uci_parse_context *pctx;
75
76 pctx = ctx->pctx;
77 if (!pctx)
78 return;
79
80 if (pctx->cfg) {
81 uci_list_del(&pctx->cfg->list);
82 uci_drop_file(pctx->cfg);
83 }
84 if (pctx->buf)
85 free(pctx->buf);
86 if (pctx->file)
87 fclose(pctx->file);
88
89 free(pctx);
90 }
91
92 /*
93 * move the string pointer forward until a non-whitespace character or
94 * EOL is reached
95 */
96 static void skip_whitespace(char **str)
97 {
98 while (**str && isspace(**str))
99 *str += 1;
100 }
101
102 static inline void addc(char **dest, char **src)
103 {
104 **dest = **src;
105 *dest += 1;
106 *src += 1;
107 }
108
109 static inline void parse_backslash(char **str, char **target)
110 {
111 /* skip backslash */
112 *str += 1;
113 /* FIXME: decode escaped characters? */
114 addc(target, str);
115 }
116
117 /*
118 * parse a double quoted string argument from the command line
119 */
120 static void parse_double_quote(struct uci_context *ctx, char **str, char **target)
121 {
122 char c;
123
124 /* skip quote character */
125 *str += 1;
126
127 while ((c = **str)) {
128 switch(c) {
129 case '\\':
130 parse_backslash(str, target);
131 continue;
132 case '"':
133 **target = 0;
134 *str += 1;
135 return;
136 default:
137 addc(target, str);
138 break;
139 }
140 }
141 ctx->pctx->reason = "unterminated \"";
142 ctx->pctx->byte = *str - ctx->pctx->buf;
143 UCI_THROW(ctx, UCI_ERR_PARSE);
144 }
145
146 /*
147 * parse a single quoted string argument from the command line
148 */
149 static void parse_single_quote(struct uci_context *ctx, char **str, char **target)
150 {
151 char c;
152 /* skip quote character */
153 *str += 1;
154
155 while ((c = **str)) {
156 switch(c) {
157 case '\'':
158 **target = 0;
159 *str += 1;
160 return;
161 default:
162 addc(target, str);
163 }
164 }
165 ctx->pctx->reason = "unterminated '";
166 ctx->pctx->byte = *str - ctx->pctx->buf;
167 UCI_THROW(ctx, UCI_ERR_PARSE);
168 }
169
170 /*
171 * parse a string from the command line and detect the quoting style
172 */
173 static void parse_str(struct uci_context *ctx, char **str, char **target)
174 {
175 do {
176 switch(**str) {
177 case '\\':
178 parse_backslash(str, target);
179 continue;
180 case '\'':
181 parse_single_quote(ctx, str, target);
182 break;
183 case '"':
184 parse_double_quote(ctx, str, target);
185 break;
186 case 0:
187 goto done;
188 default:
189 addc(target, str);
190 break;
191 }
192 } while (**str && !isspace(**str));
193 done:
194
195 /*
196 * if the string was unquoted and we've stopped at a whitespace
197 * character, skip to the next one, because the whitespace will
198 * be overwritten by a null byte here
199 */
200 if (**str)
201 *str += 1;
202
203 /* terminate the parsed string */
204 **target = 0;
205 }
206
207 /*
208 * extract the next argument from the command line
209 */
210 static char *next_arg(struct uci_context *ctx, char **str, bool required)
211 {
212 char *val;
213 char *ptr;
214
215 val = ptr = *str;
216 skip_whitespace(str);
217 parse_str(ctx, str, &ptr);
218 if (required && !*val) {
219 ctx->pctx->reason = "insufficient arguments";
220 ctx->pctx->byte = *str - ctx->pctx->buf;
221 UCI_THROW(ctx, UCI_ERR_PARSE);
222 }
223
224 return uci_strdup(ctx, val);
225 }
226
227 /*
228 * verify that the end of the line or command is reached.
229 * throw an error if extra arguments are given on the command line
230 */
231 static void assert_eol(struct uci_context *ctx, char **str)
232 {
233 char *tmp;
234
235 tmp = next_arg(ctx, str, false);
236 if (tmp && *tmp) {
237 ctx->pctx->reason = "too many arguments";
238 ctx->pctx->byte = tmp - ctx->pctx->buf;
239 UCI_THROW(ctx, UCI_ERR_PARSE);
240 }
241 }
242
243 /*
244 * parse the 'config' uci command (open a section)
245 */
246 static void uci_parse_config(struct uci_context *ctx, char **str)
247 {
248 char *type, *name;
249
250 /* command string null-terminated by strtok */
251 *str += strlen(*str) + 1;
252
253 type = next_arg(ctx, str, true);
254 name = next_arg(ctx, str, false);
255 assert_eol(ctx, str);
256 }
257
258 /*
259 * parse the 'option' uci command (open a value)
260 */
261 static void uci_parse_option(struct uci_context *ctx, char **str)
262 {
263 char *name, *value;
264
265 /* command string null-terminated by strtok */
266 *str += strlen(*str) + 1;
267
268 name = next_arg(ctx, str, true);
269 value = next_arg(ctx, str, true);
270 assert_eol(ctx, str);
271 }
272
273 /*
274 * parse a complete input line, split up combined commands by ';'
275 */
276 static void uci_parse_line(struct uci_context *ctx)
277 {
278 struct uci_parse_context *pctx = ctx->pctx;
279 char *word, *brk;
280
281 for (word = strtok_r(pctx->buf, ";", &brk);
282 word;
283 word = strtok_r(NULL, ";", &brk)) {
284
285 char *pbrk;
286 word = strtok_r(word, " \t", &pbrk);
287
288 switch(word[0]) {
289 case 'c':
290 if ((word[1] == 0) || !strcmp(word + 1, "onfig"))
291 uci_parse_config(ctx, &word);
292 break;
293 case 'o':
294 if ((word[1] == 0) || !strcmp(word + 1, "ption"))
295 uci_parse_option(ctx, &word);
296 break;
297 default:
298 pctx->reason = "unterminated command";
299 pctx->byte = word - pctx->buf;
300 UCI_THROW(ctx, UCI_ERR_PARSE);
301 break;
302 }
303 }
304 }
305
306 int uci_load(struct uci_context *ctx, const char *name)
307 {
308 struct uci_parse_context *pctx;
309 struct stat statbuf;
310 char *filename;
311 bool confpath;
312
313 UCI_HANDLE_ERR(ctx);
314 UCI_ASSERT(ctx, name != NULL);
315
316 /* make sure no memory from previous parse attempts is leaked */
317 uci_parse_cleanup(ctx);
318
319 pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
320 ctx->pctx = pctx;
321
322 switch (name[0]) {
323 case '.':
324 case '/':
325 /* absolute/relative path outside of /etc/config */
326 filename = (char *) name;
327 confpath = false;
328 break;
329 default:
330 filename = uci_malloc(ctx, strlen(name) + sizeof(UCI_CONFDIR) + 2);
331 sprintf(filename, UCI_CONFDIR "/%s", name);
332 confpath = true;
333 break;
334 }
335
336 if ((stat(filename, &statbuf) < 0) ||
337 ((statbuf.st_mode & S_IFMT) != S_IFREG))
338 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
339
340 pctx->file = fopen(filename, "r");
341 if (filename != name)
342 free(filename);
343
344 if (!pctx->file)
345 UCI_THROW(ctx, UCI_ERR_IO);
346
347 pctx->cfg = uci_alloc_file(ctx, name);
348
349 while (!feof(pctx->file)) {
350 uci_getln(ctx);
351 if (pctx->buf[0])
352 uci_parse_line(ctx);
353 }
354
355 /* add to main config file list */
356 uci_list_add(&ctx->root, &pctx->cfg->list);
357 pctx->cfg = NULL;
358
359 /* no error happened, we can get rid of the parser context now */
360 uci_parse_cleanup(ctx);
361
362 return 0;
363 }
364