2 * libuci - Library for the Unified Configuration Interface
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
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
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.
16 * This file contains the code for parsing uci config files
19 #include <sys/types.h>
28 #define LINEBUF_MAX 4096
30 static void uci_parse_error(struct uci_context *ctx, char *pos, char *reason)
32 struct uci_parse_context *pctx = ctx->pctx;
34 pctx->reason = reason;
35 pctx->byte = pos - pctx->buf;
36 UCI_THROW(ctx, UCI_ERR_PARSE);
40 * Fetch a new line from the input stream and resize buffer if necessary
42 static void uci_getln(struct uci_context *ctx, int offset)
44 struct uci_parse_context *pctx = ctx->pctx;
48 if (pctx->buf == NULL) {
49 pctx->buf = uci_malloc(ctx, LINEBUF);
50 pctx->bufsz = LINEBUF;
58 p = fgets(p, pctx->bufsz - ofs, pctx->file);
63 if (pctx->buf[ofs - 1] == '\n') {
65 pctx->buf[ofs - 1] = 0;
69 if (pctx->bufsz > LINEBUF_MAX/2)
70 uci_parse_error(ctx, p, "line too long");
73 pctx->buf = uci_realloc(ctx, pctx->buf, pctx->bufsz);
78 * Clean up all extra memory used by the parser and exporter
80 static void uci_file_cleanup(struct uci_context *ctx)
82 struct uci_parse_context *pctx;
96 uci_free_package(pctx->package);
107 * parse a character escaped by '\'
108 * returns true if the escaped character is to be parsed
109 * returns false if the escaped character is to be ignored
111 static inline bool parse_backslash(struct uci_context *ctx, char **str)
116 /* undecoded backslash at the end of line, fetch the next line */
119 uci_getln(ctx, *str - ctx->pctx->buf);
123 /* FIXME: decode escaped char, necessary? */
128 * move the string pointer forward until a non-whitespace character or
131 static void skip_whitespace(struct uci_context *ctx, char **str)
134 while (**str && isspace(**str))
138 if (!parse_backslash(ctx, str))
143 static inline void addc(char **dest, char **src)
151 * parse a double quoted string argument from the command line
153 static void parse_double_quote(struct uci_context *ctx, char **str, char **target)
157 /* skip quote character */
160 while ((c = **str)) {
167 if (!parse_backslash(ctx, str))
175 uci_parse_error(ctx, *str, "unterminated \"");
179 * parse a single quoted string argument from the command line
181 static void parse_single_quote(struct uci_context *ctx, char **str, char **target)
184 /* skip quote character */
187 while ((c = **str)) {
197 uci_parse_error(ctx, *str, "unterminated '");
201 * parse a string from the command line and detect the quoting style
203 static void parse_str(struct uci_context *ctx, char **str, char **target)
208 parse_single_quote(ctx, str, target);
211 parse_double_quote(ctx, str, target);
216 if (!parse_backslash(ctx, str))
223 } while (**str && !isspace(**str));
227 * if the string was unquoted and we've stopped at a whitespace
228 * character, skip to the next one, because the whitespace will
229 * be overwritten by a null byte here
234 /* terminate the parsed string */
239 * extract the next argument from the command line
241 static char *next_arg(struct uci_context *ctx, char **str, bool required)
247 skip_whitespace(ctx, str);
248 parse_str(ctx, str, &ptr);
249 if (required && !*val)
250 uci_parse_error(ctx, *str, "insufficient arguments");
256 * verify that the end of the line or command is reached.
257 * throw an error if extra arguments are given on the command line
259 static void assert_eol(struct uci_context *ctx, char **str)
263 tmp = next_arg(ctx, str, false);
265 uci_parse_error(ctx, *str, "too many arguments");
269 * switch to a different config, either triggered by uci_load, or by a
270 * 'package <...>' statement in the import file
272 static void uci_switch_config(struct uci_context *ctx)
274 struct uci_parse_context *pctx;
275 struct uci_element *e;
281 /* add the last config to main config file list */
283 uci_list_add(&ctx->root, &pctx->package->e.list);
285 pctx->package = NULL;
286 pctx->section = NULL;
293 * if an older config under the same name exists, unload it
294 * ignore errors here, e.g. if the config was not found
296 UCI_TRAP_SAVE(ctx, ignore);
297 e = uci_lookup_list(ctx, &ctx->root, name);
299 uci_unload(ctx, uci_to_package(e));
300 UCI_TRAP_RESTORE(ctx);
304 pctx->package = uci_alloc_package(ctx, name);
308 * parse the 'package' uci command (next config package)
310 static void uci_parse_package(struct uci_context *ctx, char **str, bool single)
314 /* command string null-terminated by strtok */
315 *str += strlen(*str) + 1;
317 name = next_arg(ctx, str, true);
318 assert_eol(ctx, str);
322 ctx->pctx->name = name;
323 uci_switch_config(ctx);
327 * parse the 'config' uci command (open a section)
329 static void uci_parse_config(struct uci_context *ctx, char **str)
334 if (!ctx->pctx->package) {
335 if (!ctx->pctx->name)
336 uci_parse_error(ctx, *str, "attempting to import a file without a package name");
338 uci_switch_config(ctx);
341 /* command string null-terminated by strtok */
342 *str += strlen(*str) + 1;
344 type = next_arg(ctx, str, true);
345 name = next_arg(ctx, str, false);
346 assert_eol(ctx, str);
347 ctx->pctx->section = uci_alloc_section(ctx->pctx->package, type, name);
351 * parse the 'option' uci command (open a value)
353 static void uci_parse_option(struct uci_context *ctx, char **str)
358 if (!ctx->pctx->section)
359 uci_parse_error(ctx, *str, "option command found before the first section");
361 /* command string null-terminated by strtok */
362 *str += strlen(*str) + 1;
364 name = next_arg(ctx, str, true);
365 value = next_arg(ctx, str, true);
366 assert_eol(ctx, str);
367 uci_alloc_option(ctx->pctx->section, name, value);
372 * parse a complete input line, split up combined commands by ';'
374 static void uci_parse_line(struct uci_context *ctx, bool single)
376 struct uci_parse_context *pctx = ctx->pctx;
377 char *word, *brk = NULL;
379 for (word = strtok_r(pctx->buf, ";", &brk);
381 word = strtok_r(NULL, ";", &brk)) {
384 word = strtok_r(word, " \t", &pbrk);
388 if ((word[1] == 0) || !strcmp(word + 1, "ackage"))
389 uci_parse_package(ctx, &word, single);
392 if ((word[1] == 0) || !strcmp(word + 1, "onfig"))
393 uci_parse_config(ctx, &word);
396 if ((word[1] == 0) || !strcmp(word + 1, "ption"))
397 uci_parse_option(ctx, &word);
400 uci_parse_error(ctx, word, "unterminated command");
406 /* max number of characters that escaping adds to the string */
407 #define UCI_QUOTE_ESCAPE "'\\''"
410 * escape an uci string for export
412 static char *uci_escape(struct uci_context *ctx, char *str)
418 ctx->bufsz = LINEBUF;
419 ctx->buf = malloc(LINEBUF);
423 p = strchr(str, '\'');
430 if (p + sizeof(UCI_QUOTE_ESCAPE) - str >= ctx->bufsz) {
432 ctx->buf = realloc(ctx->buf, ctx->bufsz);
434 UCI_THROW(ctx, UCI_ERR_MEM);
436 memcpy(&ctx->buf[pos], s, len);
439 strcpy(&ctx->buf[pos], UCI_QUOTE_ESCAPE);
440 pos += sizeof(UCI_QUOTE_ESCAPE);
442 } while ((p = strchr(s, '\'')));
449 * export a single config package to a file stream
451 static void uci_export_package(struct uci_package *p, FILE *stream, bool header)
453 struct uci_context *ctx = p->ctx;
454 struct uci_element *s, *o;
457 fprintf(stream, "package '%s'\n", uci_escape(ctx, p->e.name));
458 uci_foreach_element(&p->sections, s) {
459 struct uci_section *sec = uci_to_section(s);
460 fprintf(stream, "\nconfig '%s'", uci_escape(ctx, sec->type));
461 fprintf(stream, " '%s'\n", uci_escape(ctx, sec->e.name));
462 uci_foreach_element(&sec->options, o) {
463 struct uci_option *opt = uci_to_option(o);
464 fprintf(stream, "\toption '%s'", uci_escape(ctx, opt->e.name));
465 fprintf(stream, " '%s'\n", uci_escape(ctx, opt->value));
468 fprintf(stream, "\n");
471 int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package, bool header)
473 struct uci_element *e;
476 UCI_ASSERT(ctx, stream != NULL);
479 uci_export_package(package, stream, header);
483 uci_foreach_element(&ctx->root, e) {
484 uci_export_package(uci_to_package(e), stream, header);
490 int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package, bool single)
492 struct uci_parse_context *pctx;
494 /* make sure no memory from previous parse attempts is leaked */
495 uci_file_cleanup(ctx);
497 pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
502 * If 'name' was supplied, assume that the supplied stream does not contain
503 * the appropriate 'package <name>' string to specify the config name
504 * NB: the config file can still override the package name
509 while (!feof(pctx->file)) {
512 uci_parse_line(ctx, single);
516 *package = pctx->package;
519 uci_switch_config(ctx);
521 /* no error happened, we can get rid of the parser context now */
522 uci_file_cleanup(ctx);
527 int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package)
536 UCI_ASSERT(ctx, name != NULL);
541 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
544 /* absolute/relative path outside of /etc/config */
545 filename = uci_strdup(ctx, name);
546 name = strrchr(name, '/') + 1;
550 filename = uci_malloc(ctx, strlen(name) + sizeof(UCI_CONFDIR) + 2);
551 sprintf(filename, UCI_CONFDIR "/%s", name);
556 if ((stat(filename, &statbuf) < 0) ||
557 ((statbuf.st_mode & S_IFMT) != S_IFREG)) {
558 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
561 fd = open(filename, O_RDONLY);
563 UCI_THROW(ctx, UCI_ERR_IO);
565 if (flock(fd, LOCK_SH) < 0)
566 UCI_THROW(ctx, UCI_ERR_IO);
568 file = fdopen(fd, "r");
570 UCI_THROW(ctx, UCI_ERR_IO);
573 UCI_TRAP_SAVE(ctx, done);
574 uci_import(ctx, file, name, package, true);
575 UCI_TRAP_RESTORE(ctx);
578 (*package)->path = filename;
579 (*package)->confdir = confdir;
588 int uci_commit(struct uci_context *ctx, struct uci_package *p)
592 int err = UCI_ERR_IO;
595 UCI_ASSERT(ctx, p != NULL);
596 UCI_ASSERT(ctx, p->path != NULL);
598 fd = open(p->path, O_RDWR);
602 if (flock(fd, LOCK_EX) < 0)
610 UCI_TRAP_SAVE(ctx, done);
611 uci_export(ctx, f, p, false);
612 UCI_TRAP_RESTORE(ctx);
621 UCI_THROW(ctx, ctx->errno);
623 UCI_THROW(ctx, UCI_ERR_IO);
629 * This function returns the filename by returning the string
630 * after the last '/' character. By checking for a non-'\0'
631 * character afterwards, directories are ignored (glob marks
632 * those with a trailing '/'
634 static inline char *get_filename(char *path)
638 p = strrchr(path, '/');
645 char **uci_list_configs(struct uci_context *ctx)
652 if (glob(UCI_CONFDIR "/*", GLOB_MARK, NULL, &globbuf) != 0)
655 size = sizeof(char *) * (globbuf.gl_pathc + 1);
656 for(i = 0; i < globbuf.gl_pathc; i++) {
659 p = get_filename(globbuf.gl_pathv[i]);
663 size += strlen(p) + 1;
666 configs = malloc(size);
670 memset(configs, 0, size);
671 buf = (char *) &configs[globbuf.gl_pathc + 1];
672 for(i = 0; i < globbuf.gl_pathc; i++) {
675 p = get_filename(globbuf.gl_pathv[i]);
681 buf += strlen(buf) + 1;