refactor, add uci_import
[project/uci.git] / file.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 ctx->pctx = NULL;
81 if (pctx->cfg) {
82 uci_list_del(&pctx->cfg->list);
83 uci_drop_config(pctx->cfg);
84 }
85 if (pctx->buf)
86 free(pctx->buf);
87 if (pctx->file)
88 fclose(pctx->file);
89
90 free(pctx);
91 }
92
93 /*
94 * move the string pointer forward until a non-whitespace character or
95 * EOL is reached
96 */
97 static void skip_whitespace(char **str)
98 {
99 while (**str && isspace(**str))
100 *str += 1;
101 }
102
103 static inline void addc(char **dest, char **src)
104 {
105 **dest = **src;
106 *dest += 1;
107 *src += 1;
108 }
109
110 static inline void parse_backslash(char **str, char **target)
111 {
112 /* skip backslash */
113 *str += 1;
114 /* FIXME: decode escaped characters? */
115 addc(target, str);
116 }
117
118 /*
119 * parse a double quoted string argument from the command line
120 */
121 static void parse_double_quote(struct uci_context *ctx, char **str, char **target)
122 {
123 char c;
124
125 /* skip quote character */
126 *str += 1;
127
128 while ((c = **str)) {
129 switch(c) {
130 case '\\':
131 parse_backslash(str, target);
132 continue;
133 case '"':
134 **target = 0;
135 *str += 1;
136 return;
137 default:
138 addc(target, str);
139 break;
140 }
141 }
142 ctx->pctx->reason = "unterminated \"";
143 ctx->pctx->byte = *str - ctx->pctx->buf;
144 UCI_THROW(ctx, UCI_ERR_PARSE);
145 }
146
147 /*
148 * parse a single quoted string argument from the command line
149 */
150 static void parse_single_quote(struct uci_context *ctx, char **str, char **target)
151 {
152 char c;
153 /* skip quote character */
154 *str += 1;
155
156 while ((c = **str)) {
157 switch(c) {
158 case '\'':
159 **target = 0;
160 *str += 1;
161 return;
162 default:
163 addc(target, str);
164 }
165 }
166 ctx->pctx->reason = "unterminated '";
167 ctx->pctx->byte = *str - ctx->pctx->buf;
168 UCI_THROW(ctx, UCI_ERR_PARSE);
169 }
170
171 /*
172 * parse a string from the command line and detect the quoting style
173 */
174 static void parse_str(struct uci_context *ctx, char **str, char **target)
175 {
176 do {
177 switch(**str) {
178 case '\\':
179 parse_backslash(str, target);
180 continue;
181 case '\'':
182 parse_single_quote(ctx, str, target);
183 break;
184 case '"':
185 parse_double_quote(ctx, str, target);
186 break;
187 case 0:
188 goto done;
189 default:
190 addc(target, str);
191 break;
192 }
193 } while (**str && !isspace(**str));
194 done:
195
196 /*
197 * if the string was unquoted and we've stopped at a whitespace
198 * character, skip to the next one, because the whitespace will
199 * be overwritten by a null byte here
200 */
201 if (**str)
202 *str += 1;
203
204 /* terminate the parsed string */
205 **target = 0;
206 }
207
208 /*
209 * extract the next argument from the command line
210 */
211 static char *next_arg(struct uci_context *ctx, char **str, bool required)
212 {
213 char *val;
214 char *ptr;
215
216 val = ptr = *str;
217 skip_whitespace(str);
218 parse_str(ctx, str, &ptr);
219 if (required && !*val) {
220 ctx->pctx->reason = "insufficient arguments";
221 ctx->pctx->byte = *str - ctx->pctx->buf;
222 UCI_THROW(ctx, UCI_ERR_PARSE);
223 }
224
225 return uci_strdup(ctx, val);
226 }
227
228 /*
229 * verify that the end of the line or command is reached.
230 * throw an error if extra arguments are given on the command line
231 */
232 static void assert_eol(struct uci_context *ctx, char **str)
233 {
234 char *tmp;
235
236 tmp = next_arg(ctx, str, false);
237 if (tmp && *tmp) {
238 ctx->pctx->reason = "too many arguments";
239 ctx->pctx->byte = tmp - ctx->pctx->buf;
240 UCI_THROW(ctx, UCI_ERR_PARSE);
241 }
242 }
243
244 /*
245 * switch to a different config, either triggered by uci_load, or by a
246 * 'package <...>' statement in the import file
247 */
248 static void uci_switch_config(struct uci_context *ctx)
249 {
250 struct uci_parse_context *pctx;
251 const char *name;
252
253 pctx = ctx->pctx;
254 name = pctx->name;
255
256 /* add the last config to main config file list */
257 if (pctx->cfg) {
258 uci_list_add(&ctx->root, &pctx->cfg->list);
259
260 pctx->cfg = NULL;
261 pctx->section = NULL;
262 }
263
264 if (!name)
265 return;
266
267 /*
268 * if an older config under the same name exists, unload it
269 * ignore errors here, e.g. if the config was not found
270 */
271 UCI_TRAP_SAVE(ctx, ignore);
272 uci_unload(ctx, name);
273 UCI_TRAP_RESTORE(ctx);
274 ignore:
275 ctx->errno = 0;
276
277 pctx->cfg = uci_alloc_config(ctx, name);
278 }
279
280 /*
281 * parse the 'config' uci command (open a section)
282 */
283 static void uci_parse_config(struct uci_context *ctx, char **str)
284 {
285 char *name = NULL;
286 char *type = NULL;
287
288 if (!ctx->pctx->cfg) {
289 if (!ctx->pctx->name) {
290 ctx->pctx->byte = *str - ctx->pctx->buf;
291 ctx->pctx->reason = "attempting to import a file without a package name";
292 UCI_THROW(ctx, UCI_ERR_PARSE);
293 }
294 uci_switch_config(ctx);
295 }
296
297 /* command string null-terminated by strtok */
298 *str += strlen(*str) + 1;
299
300 UCI_TRAP_SAVE(ctx, error);
301 type = next_arg(ctx, str, true);
302 name = next_arg(ctx, str, false);
303 assert_eol(ctx, str);
304 ctx->pctx->section = uci_add_section(ctx->pctx->cfg, type, name);
305 UCI_TRAP_RESTORE(ctx);
306 return;
307
308 error:
309 if (name)
310 free(name);
311 if (type)
312 free(type);
313 UCI_THROW(ctx, ctx->errno);
314 }
315
316 /*
317 * parse the 'option' uci command (open a value)
318 */
319 static void uci_parse_option(struct uci_context *ctx, char **str)
320 {
321 char *name = NULL;
322 char *value = NULL;
323
324 if (!ctx->pctx->section) {
325 ctx->pctx->byte = *str - ctx->pctx->buf;
326 ctx->pctx->reason = "option command found before the first section";
327 UCI_THROW(ctx, UCI_ERR_PARSE);
328 }
329 /* command string null-terminated by strtok */
330 *str += strlen(*str) + 1;
331
332 UCI_TRAP_SAVE(ctx, error);
333 name = next_arg(ctx, str, true);
334 value = next_arg(ctx, str, true);
335 assert_eol(ctx, str);
336 uci_add_option(ctx->pctx->section, name, value);
337 UCI_TRAP_RESTORE(ctx);
338 return;
339
340 error:
341 if (name)
342 free(name);
343 if (value)
344 free(value);
345 UCI_THROW(ctx, ctx->errno);
346 }
347
348
349 /*
350 * parse a complete input line, split up combined commands by ';'
351 */
352 static void uci_parse_line(struct uci_context *ctx)
353 {
354 struct uci_parse_context *pctx = ctx->pctx;
355 char *word, *brk;
356
357 for (word = strtok_r(pctx->buf, ";", &brk);
358 word;
359 word = strtok_r(NULL, ";", &brk)) {
360
361 char *pbrk;
362 word = strtok_r(word, " \t", &pbrk);
363
364 switch(word[0]) {
365 case 'c':
366 if ((word[1] == 0) || !strcmp(word + 1, "onfig"))
367 uci_parse_config(ctx, &word);
368 break;
369 case 'o':
370 if ((word[1] == 0) || !strcmp(word + 1, "ption"))
371 uci_parse_option(ctx, &word);
372 break;
373 default:
374 pctx->reason = "unterminated command";
375 pctx->byte = word - pctx->buf;
376 UCI_THROW(ctx, UCI_ERR_PARSE);
377 break;
378 }
379 }
380 }
381
382 int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_config **cfg)
383 {
384 struct uci_parse_context *pctx;
385
386 /* make sure no memory from previous parse attempts is leaked */
387 uci_parse_cleanup(ctx);
388
389 pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
390 ctx->pctx = pctx;
391 pctx->file = stream;
392
393 /*
394 * If 'name' was supplied, assume that the supplied stream does not contain
395 * the appropriate 'package <name>' string to specify the config name
396 * NB: the config file can still override the package name
397 */
398 if (name)
399 pctx->name = name;
400
401 while (!feof(pctx->file)) {
402 uci_getln(ctx);
403 if (pctx->buf[0])
404 uci_parse_line(ctx);
405 }
406
407 if (cfg)
408 *cfg = pctx->cfg;
409
410 pctx->name = NULL;
411 uci_switch_config(ctx);
412
413 /* no error happened, we can get rid of the parser context now */
414 uci_parse_cleanup(ctx);
415
416 return 0;
417 }
418
419 int uci_load(struct uci_context *ctx, const char *name, struct uci_config **cfg)
420 {
421 struct stat statbuf;
422 char *filename;
423 bool confpath;
424 FILE *file;
425
426 UCI_HANDLE_ERR(ctx);
427 UCI_ASSERT(ctx, name != NULL);
428
429 ignore:
430 ctx->errno = 0;
431
432 switch (name[0]) {
433 case '.':
434 case '/':
435 /* absolute/relative path outside of /etc/config */
436 filename = (char *) name;
437 confpath = false;
438 break;
439 default:
440 filename = uci_malloc(ctx, strlen(name) + sizeof(UCI_CONFDIR) + 2);
441 sprintf(filename, UCI_CONFDIR "/%s", name);
442 confpath = true;
443 break;
444 }
445
446 if ((stat(filename, &statbuf) < 0) ||
447 ((statbuf.st_mode & S_IFMT) != S_IFREG)) {
448 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
449 }
450
451 file = fopen(filename, "r");
452 if (filename != name)
453 free(filename);
454
455 if (!file)
456 UCI_THROW(ctx, UCI_ERR_IO);
457
458 return uci_import(ctx, file, name, cfg);
459 }
460