fix list parsing with anonymous sections
[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 #define _GNU_SOURCE
20 #include <sys/types.h>
21 #include <sys/file.h>
22 #include <stdbool.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <ctype.h>
27 #include <glob.h>
28
29 static struct uci_backend uci_file_backend;
30
31 /*
32 * verify that the end of the line or command is reached.
33 * throw an error if extra arguments are given on the command line
34 */
35 static void assert_eol(struct uci_context *ctx, char **str)
36 {
37 char *tmp;
38
39 skip_whitespace(ctx, str);
40 tmp = next_arg(ctx, str, false, false);
41 if (*tmp && (ctx->flags & UCI_FLAG_STRICT))
42 uci_parse_error(ctx, *str, "too many arguments");
43 }
44
45 /*
46 * switch to a different config, either triggered by uci_load, or by a
47 * 'package <...>' statement in the import file
48 */
49 static void uci_switch_config(struct uci_context *ctx)
50 {
51 struct uci_parse_context *pctx;
52 struct uci_element *e;
53 const char *name;
54
55 pctx = ctx->pctx;
56 name = pctx->name;
57
58 /* add the last config to main config file list */
59 if (pctx->package) {
60 pctx->package->backend = ctx->backend;
61 uci_list_add(&ctx->root, &pctx->package->e.list);
62
63 pctx->package = NULL;
64 pctx->section = NULL;
65 }
66
67 if (!name)
68 return;
69
70 /*
71 * if an older config under the same name exists, unload it
72 * ignore errors here, e.g. if the config was not found
73 */
74 e = uci_lookup_list(&ctx->root, name);
75 if (e)
76 UCI_THROW(ctx, UCI_ERR_DUPLICATE);
77 pctx->package = uci_alloc_package(ctx, name);
78 }
79
80 /*
81 * parse the 'package' uci command (next config package)
82 */
83 static void uci_parse_package(struct uci_context *ctx, char **str, bool single)
84 {
85 char *name = NULL;
86
87 /* command string null-terminated by strtok */
88 *str += strlen(*str) + 1;
89
90 name = next_arg(ctx, str, true, true);
91 assert_eol(ctx, str);
92 if (single)
93 return;
94
95 ctx->pctx->name = name;
96 uci_switch_config(ctx);
97 }
98
99 /*
100 * parse the 'config' uci command (open a section)
101 */
102 static void uci_parse_config(struct uci_context *ctx, char **str)
103 {
104 struct uci_parse_context *pctx = ctx->pctx;
105 char *name = NULL;
106 char *type = NULL;
107
108 uci_fixup_section(ctx, ctx->pctx->section);
109 if (!ctx->pctx->package) {
110 if (!ctx->pctx->name)
111 uci_parse_error(ctx, *str, "attempting to import a file without a package name");
112
113 uci_switch_config(ctx);
114 }
115
116 /* command string null-terminated by strtok */
117 *str += strlen(*str) + 1;
118
119 type = next_arg(ctx, str, true, false);
120 if (!uci_validate_str(type, false))
121 uci_parse_error(ctx, type, "invalid character in field");
122 name = next_arg(ctx, str, false, true);
123 assert_eol(ctx, str);
124
125 if (pctx->merge) {
126 UCI_TRAP_SAVE(ctx, error);
127 if (uci_set(ctx, pctx->package, name, NULL, type, NULL) != UCI_OK)
128 goto error;
129 UCI_TRAP_RESTORE(ctx);
130 return;
131 error:
132 UCI_THROW(ctx, ctx->err);
133 } else
134 pctx->section = uci_alloc_section(pctx->package, type, name);
135 }
136
137 /*
138 * parse the 'option' uci command (open a value)
139 */
140 static void uci_parse_option(struct uci_context *ctx, char **str)
141 {
142 struct uci_parse_context *pctx = ctx->pctx;
143 char *name = NULL;
144 char *value = NULL;
145
146 if (!pctx->section)
147 uci_parse_error(ctx, *str, "option command found before the first section");
148
149 /* command string null-terminated by strtok */
150 *str += strlen(*str) + 1;
151
152 name = next_arg(ctx, str, true, true);
153 value = next_arg(ctx, str, false, false);
154 assert_eol(ctx, str);
155
156 if (pctx->merge) {
157 UCI_TRAP_SAVE(ctx, error);
158 uci_set(ctx, pctx->package, pctx->section->e.name, name, value, NULL);
159 UCI_TRAP_RESTORE(ctx);
160 return;
161 error:
162 UCI_THROW(ctx, ctx->err);
163 } else
164 uci_alloc_option(pctx->section, name, value);
165 }
166
167 static void uci_parse_list(struct uci_context *ctx, char **str)
168 {
169 struct uci_parse_context *pctx = ctx->pctx;
170 char *name = NULL;
171 char *value = NULL;
172
173 if (!pctx->section)
174 uci_parse_error(ctx, *str, "list command found before the first section");
175
176 /* command string null-terminated by strtok */
177 *str += strlen(*str) + 1;
178
179 name = next_arg(ctx, str, true, true);
180 value = next_arg(ctx, str, false, false);
181 assert_eol(ctx, str);
182
183 if (pctx->merge) {
184 UCI_TRAP_SAVE(ctx, error);
185 uci_add_list(ctx, pctx->package, pctx->section->e.name, name, value, NULL);
186 UCI_TRAP_RESTORE(ctx);
187 return;
188 error:
189 UCI_THROW(ctx, ctx->err);
190 } else {
191 struct uci_option *o;
192 struct uci_element *e;
193
194 e = uci_lookup_list(&pctx->section->options, name);
195 o = uci_to_option(e);
196 if (!o) {
197 o = uci_alloc_list(pctx->section, name);
198 } else {
199 if (o->type != UCI_TYPE_LIST)
200 uci_parse_error(ctx, *str, "conflicting list/option keywords");
201 }
202 UCI_INTERNAL(uci_add_element_list, ctx, o, value);
203 }
204 }
205
206
207 /*
208 * parse a complete input line, split up combined commands by ';'
209 */
210 static void uci_parse_line(struct uci_context *ctx, bool single)
211 {
212 struct uci_parse_context *pctx = ctx->pctx;
213 char *word, *brk;
214
215 word = pctx->buf;
216 do {
217 brk = NULL;
218 word = strtok_r(word, " \t", &brk);
219 if (!word)
220 return;
221
222 switch(word[0]) {
223 case 0:
224 case '#':
225 return;
226 case 'p':
227 if ((word[1] == 0) || !strcmp(word + 1, "ackage"))
228 uci_parse_package(ctx, &word, single);
229 else
230 goto invalid;
231 break;
232 case 'c':
233 if ((word[1] == 0) || !strcmp(word + 1, "onfig"))
234 uci_parse_config(ctx, &word);
235 else
236 goto invalid;
237 break;
238 case 'o':
239 if ((word[1] == 0) || !strcmp(word + 1, "ption"))
240 uci_parse_option(ctx, &word);
241 else
242 goto invalid;
243 break;
244 case 'l':
245 if ((word[1] == 0) || !strcmp(word + 1, "ist"))
246 uci_parse_list(ctx, &word);
247 else
248 goto invalid;
249 break;
250 default:
251 goto invalid;
252 }
253 continue;
254 invalid:
255 uci_parse_error(ctx, word, "invalid command");
256 } while (1);
257 }
258
259 /* max number of characters that escaping adds to the string */
260 #define UCI_QUOTE_ESCAPE "'\\''"
261
262 /*
263 * escape an uci string for export
264 */
265 static char *uci_escape(struct uci_context *ctx, const char *str)
266 {
267 const char *end;
268 int ofs = 0;
269
270 if (!ctx->buf) {
271 ctx->bufsz = LINEBUF;
272 ctx->buf = malloc(LINEBUF);
273 }
274
275 while (1) {
276 int len;
277
278 end = strchr(str, '\'');
279 if (!end)
280 end = str + strlen(str);
281 len = end - str;
282
283 /* make sure that we have enough room in the buffer */
284 while (ofs + len + sizeof(UCI_QUOTE_ESCAPE) + 1 > ctx->bufsz) {
285 ctx->bufsz *= 2;
286 ctx->buf = uci_realloc(ctx, ctx->buf, ctx->bufsz);
287 }
288
289 /* copy the string until the character before the quote */
290 memcpy(&ctx->buf[ofs], str, len);
291 ofs += len;
292
293 /* end of string? return the buffer */
294 if (*end == 0)
295 break;
296
297 memcpy(&ctx->buf[ofs], UCI_QUOTE_ESCAPE, sizeof(UCI_QUOTE_ESCAPE));
298 ofs += strlen(&ctx->buf[ofs]);
299 str = end + 1;
300 }
301
302 ctx->buf[ofs] = 0;
303 return ctx->buf;
304 }
305
306 /*
307 * export a single config package to a file stream
308 */
309 static void uci_export_package(struct uci_package *p, FILE *stream, bool header)
310 {
311 struct uci_context *ctx = p->ctx;
312 struct uci_element *s, *o, *i;
313
314 if (header)
315 fprintf(stream, "package '%s'\n", uci_escape(ctx, p->e.name));
316 uci_foreach_element(&p->sections, s) {
317 struct uci_section *sec = uci_to_section(s);
318 fprintf(stream, "\nconfig '%s'", uci_escape(ctx, sec->type));
319 if (!sec->anonymous || (ctx->flags & UCI_FLAG_EXPORT_NAME))
320 fprintf(stream, " '%s'", uci_escape(ctx, sec->e.name));
321 fprintf(stream, "\n");
322 uci_foreach_element(&sec->options, o) {
323 struct uci_option *opt = uci_to_option(o);
324 switch(opt->type) {
325 case UCI_TYPE_STRING:
326 fprintf(stream, "\toption '%s'", uci_escape(ctx, opt->e.name));
327 fprintf(stream, " '%s'\n", uci_escape(ctx, opt->v.string));
328 break;
329 case UCI_TYPE_LIST:
330 uci_foreach_element(&opt->v.list, i) {
331 fprintf(stream, "\tlist '%s'", uci_escape(ctx, opt->e.name));
332 fprintf(stream, " '%s'\n", uci_escape(ctx, i->name));
333 }
334 break;
335 default:
336 fprintf(stream, "\t# unknown type for option '%s'\n", uci_escape(ctx, opt->e.name));
337 break;
338 }
339 }
340 }
341 fprintf(stream, "\n");
342 }
343
344 int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package, bool header)
345 {
346 struct uci_element *e;
347
348 UCI_HANDLE_ERR(ctx);
349 UCI_ASSERT(ctx, stream != NULL);
350
351 if (package)
352 uci_export_package(package, stream, header);
353 else {
354 uci_foreach_element(&ctx->root, e) {
355 uci_export_package(uci_to_package(e), stream, header);
356 }
357 }
358
359 return 0;
360 }
361
362 int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package, bool single)
363 {
364 struct uci_parse_context *pctx;
365 UCI_HANDLE_ERR(ctx);
366
367 /* make sure no memory from previous parse attempts is leaked */
368 uci_cleanup(ctx);
369
370 uci_alloc_parse_context(ctx);
371 pctx = ctx->pctx;
372 pctx->file = stream;
373 if (*package && single) {
374 pctx->package = *package;
375 pctx->merge = true;
376 }
377
378 /*
379 * If 'name' was supplied, assume that the supplied stream does not contain
380 * the appropriate 'package <name>' string to specify the config name
381 * NB: the config file can still override the package name
382 */
383 if (name) {
384 UCI_ASSERT(ctx, uci_validate_str(name, false));
385 pctx->name = name;
386 }
387
388 while (!feof(pctx->file)) {
389 uci_getln(ctx, 0);
390 UCI_TRAP_SAVE(ctx, error);
391 if (pctx->buf[0])
392 uci_parse_line(ctx, single);
393 UCI_TRAP_RESTORE(ctx);
394 continue;
395 error:
396 if (ctx->flags & UCI_FLAG_PERROR)
397 uci_perror(ctx, NULL);
398 if ((ctx->err != UCI_ERR_PARSE) ||
399 (ctx->flags & UCI_FLAG_STRICT))
400 UCI_THROW(ctx, ctx->err);
401 }
402
403 uci_fixup_section(ctx, ctx->pctx->section);
404 if (!pctx->package && name)
405 uci_switch_config(ctx);
406 if (package)
407 *package = pctx->package;
408 if (pctx->merge)
409 pctx->package = NULL;
410
411 pctx->name = NULL;
412 uci_switch_config(ctx);
413
414 /* no error happened, we can get rid of the parser context now */
415 uci_cleanup(ctx);
416
417 return 0;
418 }
419
420
421 static char *uci_config_path(struct uci_context *ctx, const char *name)
422 {
423 char *filename;
424
425 UCI_ASSERT(ctx, uci_validate_str(name, false));
426 filename = uci_malloc(ctx, strlen(name) + strlen(ctx->confdir) + 2);
427 sprintf(filename, "%s/%s", ctx->confdir, name);
428
429 return filename;
430 }
431
432 void uci_file_commit(struct uci_context *ctx, struct uci_package **package, bool overwrite)
433 {
434 struct uci_package *p = *package;
435 FILE *f = NULL;
436 char *name = NULL;
437 char *path = NULL;
438
439 if (!p->path) {
440 if (overwrite)
441 p->path = uci_config_path(ctx, p->e.name);
442 else
443 UCI_THROW(ctx, UCI_ERR_INVAL);
444 }
445
446 /* open the config file for writing now, so that it is locked */
447 f = uci_open_stream(ctx, p->path, SEEK_SET, true, true);
448
449 /* flush unsaved changes and reload from history file */
450 UCI_TRAP_SAVE(ctx, done);
451 if (p->has_history) {
452 if (!overwrite) {
453 name = uci_strdup(ctx, p->e.name);
454 path = uci_strdup(ctx, p->path);
455 /* dump our own changes to the history file */
456 if (!uci_list_empty(&p->history))
457 UCI_INTERNAL(uci_save, ctx, p);
458
459 /*
460 * other processes might have modified the config
461 * as well. dump and reload
462 */
463 uci_free_package(&p);
464 uci_cleanup(ctx);
465 UCI_INTERNAL(uci_import, ctx, f, name, &p, true);
466
467 p->path = path;
468 p->has_history = true;
469 *package = p;
470
471 /* freed together with the uci_package */
472 path = NULL;
473
474 /* check for updated history, flush */
475 if (!uci_load_history(ctx, p, true))
476 goto done;
477 } else {
478 /* flush history */
479 if (!uci_load_history(ctx, NULL, true))
480 goto done;
481 }
482 }
483
484 rewind(f);
485 ftruncate(fileno(f), 0);
486
487 uci_export(ctx, f, p, false);
488 UCI_TRAP_RESTORE(ctx);
489
490 done:
491 if (name)
492 free(name);
493 if (path)
494 free(path);
495 uci_close_stream(f);
496 if (ctx->err)
497 UCI_THROW(ctx, ctx->err);
498 }
499
500
501 /*
502 * This function returns the filename by returning the string
503 * after the last '/' character. By checking for a non-'\0'
504 * character afterwards, directories are ignored (glob marks
505 * those with a trailing '/'
506 */
507 static inline char *get_filename(char *path)
508 {
509 char *p;
510
511 p = strrchr(path, '/');
512 p++;
513 if (!*p)
514 return NULL;
515 return p;
516 }
517
518 static char **uci_list_config_files(struct uci_context *ctx)
519 {
520 char **configs;
521 glob_t globbuf;
522 int size, i;
523 char *buf;
524 char *dir;
525
526 dir = uci_malloc(ctx, strlen(ctx->confdir) + 1 + sizeof("/*"));
527 sprintf(dir, "%s/*", ctx->confdir);
528 if (glob(dir, GLOB_MARK, NULL, &globbuf) != 0)
529 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
530
531 size = sizeof(char *) * (globbuf.gl_pathc + 1);
532 for(i = 0; i < globbuf.gl_pathc; i++) {
533 char *p;
534
535 p = get_filename(globbuf.gl_pathv[i]);
536 if (!p)
537 continue;
538
539 size += strlen(p) + 1;
540 }
541
542 configs = uci_malloc(ctx, size);
543 buf = (char *) &configs[globbuf.gl_pathc + 1];
544 for(i = 0; i < globbuf.gl_pathc; i++) {
545 char *p;
546
547 p = get_filename(globbuf.gl_pathv[i]);
548 if (!p)
549 continue;
550
551 if (!uci_validate_name(p))
552 continue;
553
554 configs[i] = buf;
555 strcpy(buf, p);
556 buf += strlen(buf) + 1;
557 }
558 free(dir);
559 return configs;
560 }
561
562 static struct uci_package *uci_file_load(struct uci_context *ctx, const char *name)
563 {
564 struct uci_package *package = NULL;
565 char *filename;
566 bool confdir;
567 FILE *file = NULL;
568
569 switch (name[0]) {
570 case '.':
571 /* relative path outside of /etc/config */
572 if (name[1] != '/')
573 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
574 /* fall through */
575 case '/':
576 /* absolute path outside of /etc/config */
577 filename = uci_strdup(ctx, name);
578 name = strrchr(name, '/') + 1;
579 confdir = false;
580 break;
581 default:
582 /* config in /etc/config */
583 filename = uci_config_path(ctx, name);
584 confdir = true;
585 break;
586 }
587
588 file = uci_open_stream(ctx, filename, SEEK_SET, false, false);
589 ctx->err = 0;
590 UCI_TRAP_SAVE(ctx, done);
591 UCI_INTERNAL(uci_import, ctx, file, name, &package, true);
592 UCI_TRAP_RESTORE(ctx);
593
594 if (package) {
595 package->path = filename;
596 package->has_history = confdir;
597 uci_load_history(ctx, package, false);
598 }
599
600 done:
601 uci_close_stream(file);
602 if (ctx->err)
603 UCI_THROW(ctx, ctx->err);
604 return package;
605 }
606
607 static UCI_BACKEND(uci_file_backend, "file",
608 .load = uci_file_load,
609 .commit = uci_file_commit,
610 .list_configs = uci_list_config_files,
611 );