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 Lesser General Public License for more details.
16 * This file contains the code for parsing uci config files
20 #include <sys/types.h>
32 #include "uci_internal.h"
35 #define LINEBUF_MAX 4096
38 * Fetch a new line from the input stream and resize buffer if necessary
40 __private void uci_getln(struct uci_context *ctx, int offset)
42 struct uci_parse_context *pctx = ctx->pctx;
46 if (pctx->buf == NULL) {
47 pctx->buf = uci_malloc(ctx, LINEBUF);
48 pctx->bufsz = LINEBUF;
56 p = fgets(p, pctx->bufsz - ofs, pctx->file);
61 if (pctx->buf[ofs - 1] == '\n') {
63 if (ofs >= 2 && pctx->buf[ofs - 2] == '\r')
64 pctx->buf[ofs - 2] = 0;
66 pctx->buf[ofs - 1] = 0;
70 if (pctx->bufsz > LINEBUF_MAX/2)
71 uci_parse_error(ctx, p, "line too long");
74 pctx->buf = uci_realloc(ctx, pctx->buf, pctx->bufsz);
80 * parse a character escaped by '\'
81 * returns true if the escaped character is to be parsed
82 * returns false if the escaped character is to be ignored
84 static inline bool parse_backslash(struct uci_context *ctx, char **str)
89 /* undecoded backslash at the end of line, fetch the next line */
92 uci_getln(ctx, *str - ctx->pctx->buf);
96 /* FIXME: decode escaped char, necessary? */
101 * move the string pointer forward until a non-whitespace character or
104 static void skip_whitespace(struct uci_context *ctx, char **str)
107 while (**str && isspace(**str))
111 if (!parse_backslash(ctx, str))
116 static inline void addc(char **dest, char **src)
124 * parse a double quoted string argument from the command line
126 static void parse_double_quote(struct uci_context *ctx, char **str, char **target)
130 /* skip quote character */
133 while ((c = **str)) {
140 if (!parse_backslash(ctx, str))
148 uci_parse_error(ctx, *str, "unterminated \"");
152 * parse a single quoted string argument from the command line
154 static void parse_single_quote(struct uci_context *ctx, char **str, char **target)
157 /* skip quote character */
160 while ((c = **str)) {
170 uci_parse_error(ctx, *str, "unterminated '");
174 * parse a string from the command line and detect the quoting style
176 static void parse_str(struct uci_context *ctx, char **str, char **target)
182 parse_single_quote(ctx, str, target);
185 parse_double_quote(ctx, str, target);
196 if (!parse_backslash(ctx, str))
203 } while (**str && !isspace(**str));
207 * if the string was unquoted and we've stopped at a whitespace
208 * character, skip to the next one, because the whitespace will
209 * be overwritten by a null byte here
214 /* terminate the parsed string */
219 * extract the next argument from the command line
221 static char *next_arg(struct uci_context *ctx, char **str, bool required, bool name)
227 skip_whitespace(ctx, str);
232 parse_str(ctx, str, &ptr);
236 uci_parse_error(ctx, *str, "insufficient arguments");
240 if (name && !uci_validate_name(val))
241 uci_parse_error(ctx, val, "invalid character in field");
247 int uci_parse_argument(struct uci_context *ctx, FILE *stream, char **str, char **result)
250 UCI_ASSERT(ctx, str != NULL);
251 UCI_ASSERT(ctx, result != NULL);
253 if (ctx->pctx && (ctx->pctx->file != stream))
257 uci_alloc_parse_context(ctx);
259 ctx->pctx->file = stream;
263 *str = ctx->pctx->buf;
266 *result = next_arg(ctx, str, false, false);
272 uci_fill_ptr(struct uci_context *ctx, struct uci_ptr *ptr, struct uci_element *e)
274 UCI_ASSERT(ctx, ptr != NULL);
275 UCI_ASSERT(ctx, e != NULL);
277 memset(ptr, 0, sizeof(struct uci_ptr));
279 case UCI_TYPE_OPTION:
280 ptr->o = uci_to_option(e);
282 case UCI_TYPE_SECTION:
283 ptr->s = uci_to_section(e);
285 case UCI_TYPE_PACKAGE:
286 ptr->p = uci_to_package(e);
289 UCI_THROW(ctx, UCI_ERR_INVAL);
293 ptr->option = ptr->o->e.name;
294 ptr->s = ptr->o->section;
296 ptr->section = ptr->s->e.name;
297 ptr->p = ptr->s->package;
299 ptr->package = ptr->p->e.name;
301 ptr->flags |= UCI_LOOKUP_DONE;
309 * verify that the end of the line or command is reached.
310 * throw an error if extra arguments are given on the command line
312 static void assert_eol(struct uci_context *ctx, char **str)
316 skip_whitespace(ctx, str);
317 tmp = next_arg(ctx, str, false, false);
318 if (*tmp && (ctx->flags & UCI_FLAG_STRICT))
319 uci_parse_error(ctx, *str, "too many arguments");
323 * switch to a different config, either triggered by uci_load, or by a
324 * 'package <...>' statement in the import file
326 static void uci_switch_config(struct uci_context *ctx)
328 struct uci_parse_context *pctx;
329 struct uci_element *e;
335 /* add the last config to main config file list */
337 pctx->package->backend = ctx->backend;
338 uci_list_add(&ctx->root, &pctx->package->e.list);
340 pctx->package = NULL;
341 pctx->section = NULL;
348 * if an older config under the same name exists, unload it
349 * ignore errors here, e.g. if the config was not found
351 e = uci_lookup_list(&ctx->root, name);
353 UCI_THROW(ctx, UCI_ERR_DUPLICATE);
354 pctx->package = uci_alloc_package(ctx, name);
358 * parse the 'package' uci command (next config package)
360 static void uci_parse_package(struct uci_context *ctx, char **str, bool single)
364 /* command string null-terminated by strtok */
365 *str += strlen(*str) + 1;
367 name = next_arg(ctx, str, true, true);
368 assert_eol(ctx, str);
372 ctx->pctx->name = name;
373 uci_switch_config(ctx);
377 * parse the 'config' uci command (open a section)
379 static void uci_parse_config(struct uci_context *ctx, char **str)
381 struct uci_parse_context *pctx = ctx->pctx;
382 struct uci_element *e;
387 uci_fixup_section(ctx, ctx->pctx->section);
388 if (!ctx->pctx->package) {
389 if (!ctx->pctx->name)
390 uci_parse_error(ctx, *str, "attempting to import a file without a package name");
392 uci_switch_config(ctx);
395 /* command string null-terminated by strtok */
396 *str += strlen(*str) + 1;
398 type = next_arg(ctx, str, true, false);
399 if (!uci_validate_type(type))
400 uci_parse_error(ctx, type, "invalid character in field");
401 name = next_arg(ctx, str, false, true);
402 assert_eol(ctx, str);
404 if (!name || !name[0]) {
405 ctx->internal = !pctx->merge;
406 UCI_NESTED(uci_add_section, ctx, pctx->package, type, &pctx->section);
408 uci_fill_ptr(ctx, &ptr, &pctx->package->e);
409 e = uci_lookup_list(&pctx->package->sections, name);
411 ptr.s = uci_to_section(e);
415 ctx->internal = !pctx->merge;
416 UCI_NESTED(uci_set, ctx, &ptr);
417 pctx->section = uci_to_section(ptr.last);
422 * parse the 'option' uci command (open a value)
424 static void uci_parse_option(struct uci_context *ctx, char **str, bool list)
426 struct uci_parse_context *pctx = ctx->pctx;
427 struct uci_element *e;
433 uci_parse_error(ctx, *str, "option/list command found before the first section");
435 /* command string null-terminated by strtok */
436 *str += strlen(*str) + 1;
438 name = next_arg(ctx, str, true, true);
439 value = next_arg(ctx, str, false, false);
440 assert_eol(ctx, str);
442 uci_fill_ptr(ctx, &ptr, &pctx->section->e);
443 e = uci_lookup_list(&pctx->section->options, name);
445 ptr.o = uci_to_option(e);
449 ctx->internal = !pctx->merge;
451 UCI_NESTED(uci_add_list, ctx, &ptr);
453 UCI_NESTED(uci_set, ctx, &ptr);
457 * parse a complete input line, split up combined commands by ';'
459 static void uci_parse_line(struct uci_context *ctx, bool single)
461 struct uci_parse_context *pctx = ctx->pctx;
467 word = strtok_r(word, " \t", &brk);
476 if ((word[1] == 0) || !strcmp(word + 1, "ackage"))
477 uci_parse_package(ctx, &word, single);
482 if ((word[1] == 0) || !strcmp(word + 1, "onfig"))
483 uci_parse_config(ctx, &word);
488 if ((word[1] == 0) || !strcmp(word + 1, "ption"))
489 uci_parse_option(ctx, &word, false);
494 if ((word[1] == 0) || !strcmp(word + 1, "ist"))
495 uci_parse_option(ctx, &word, true);
504 uci_parse_error(ctx, word, "invalid command");
508 /* max number of characters that escaping adds to the string */
509 #define UCI_QUOTE_ESCAPE "'\\''"
512 * escape an uci string for export
514 static const char *uci_escape(struct uci_context *ctx, const char *str)
520 ctx->bufsz = LINEBUF;
521 ctx->buf = malloc(LINEBUF);
530 end = strchr(str, '\'');
532 end = str + strlen(str);
535 /* make sure that we have enough room in the buffer */
536 while (ofs + len + sizeof(UCI_QUOTE_ESCAPE) + 1 > ctx->bufsz) {
538 ctx->buf = uci_realloc(ctx, ctx->buf, ctx->bufsz);
541 /* copy the string until the character before the quote */
542 memcpy(&ctx->buf[ofs], str, len);
545 /* end of string? return the buffer */
549 memcpy(&ctx->buf[ofs], UCI_QUOTE_ESCAPE, sizeof(UCI_QUOTE_ESCAPE));
550 ofs += strlen(&ctx->buf[ofs]);
559 * export a single config package to a file stream
561 static void uci_export_package(struct uci_package *p, FILE *stream, bool header)
563 struct uci_context *ctx = p->ctx;
564 struct uci_element *s, *o, *i;
567 fprintf(stream, "package %s\n", uci_escape(ctx, p->e.name));
568 uci_foreach_element(&p->sections, s) {
569 struct uci_section *sec = uci_to_section(s);
570 fprintf(stream, "\nconfig %s", uci_escape(ctx, sec->type));
571 if (!sec->anonymous || (ctx->flags & UCI_FLAG_EXPORT_NAME))
572 fprintf(stream, " '%s'", uci_escape(ctx, sec->e.name));
573 fprintf(stream, "\n");
574 uci_foreach_element(&sec->options, o) {
575 struct uci_option *opt = uci_to_option(o);
577 case UCI_TYPE_STRING:
578 fprintf(stream, "\toption %s", uci_escape(ctx, opt->e.name));
579 fprintf(stream, " '%s'\n", uci_escape(ctx, opt->v.string));
582 uci_foreach_element(&opt->v.list, i) {
583 fprintf(stream, "\tlist %s", uci_escape(ctx, opt->e.name));
584 fprintf(stream, " '%s'\n", uci_escape(ctx, i->name));
588 fprintf(stream, "\t# unknown type for option '%s'\n", uci_escape(ctx, opt->e.name));
593 fprintf(stream, "\n");
596 int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package, bool header)
598 struct uci_element *e;
601 UCI_ASSERT(ctx, stream != NULL);
604 uci_export_package(package, stream, header);
606 uci_foreach_element(&ctx->root, e) {
607 uci_export_package(uci_to_package(e), stream, header);
614 int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package, bool single)
616 struct uci_parse_context *pctx;
619 /* make sure no memory from previous parse attempts is leaked */
622 uci_alloc_parse_context(ctx);
625 if (package && *package && single) {
626 pctx->package = *package;
631 * If 'name' was supplied, assume that the supplied stream does not contain
632 * the appropriate 'package <name>' string to specify the config name
633 * NB: the config file can still override the package name
636 UCI_ASSERT(ctx, uci_validate_package(name));
640 while (!feof(pctx->file)) {
642 UCI_TRAP_SAVE(ctx, error);
644 uci_parse_line(ctx, single);
645 UCI_TRAP_RESTORE(ctx);
648 if (ctx->flags & UCI_FLAG_PERROR)
649 uci_perror(ctx, NULL);
650 if ((ctx->err != UCI_ERR_PARSE) ||
651 (ctx->flags & UCI_FLAG_STRICT))
652 UCI_THROW(ctx, ctx->err);
655 uci_fixup_section(ctx, ctx->pctx->section);
656 if (!pctx->package && name)
657 uci_switch_config(ctx);
659 *package = pctx->package;
661 pctx->package = NULL;
664 uci_switch_config(ctx);
666 /* no error happened, we can get rid of the parser context now */
673 static char *uci_config_path(struct uci_context *ctx, const char *name)
677 UCI_ASSERT(ctx, uci_validate_package(name));
678 filename = uci_malloc(ctx, strlen(name) + strlen(ctx->confdir) + 2);
679 sprintf(filename, "%s/%s", ctx->confdir, name);
684 static void uci_file_commit(struct uci_context *ctx, struct uci_package **package, bool overwrite)
686 struct uci_package *p = *package;
693 p->path = uci_config_path(ctx, p->e.name);
695 UCI_THROW(ctx, UCI_ERR_INVAL);
698 /* open the config file for writing now, so that it is locked */
699 f = uci_open_stream(ctx, p->path, SEEK_SET, true, true);
701 /* flush unsaved changes and reload from delta file */
702 UCI_TRAP_SAVE(ctx, done);
705 name = uci_strdup(ctx, p->e.name);
706 path = uci_strdup(ctx, p->path);
707 /* dump our own changes to the delta file */
708 if (!uci_list_empty(&p->delta))
709 UCI_INTERNAL(uci_save, ctx, p);
712 * other processes might have modified the config
713 * as well. dump and reload
715 uci_free_package(&p);
717 UCI_INTERNAL(uci_import, ctx, f, name, &p, true);
723 /* freed together with the uci_package */
728 if (!uci_load_delta(ctx, p, true))
733 if (ftruncate(fileno(f), 0) < 0)
734 UCI_THROW(ctx, UCI_ERR_IO);
736 uci_export(ctx, f, p, false);
737 UCI_TRAP_RESTORE(ctx);
746 UCI_THROW(ctx, ctx->err);
751 * This function returns the filename by returning the string
752 * after the last '/' character. By checking for a non-'\0'
753 * character afterwards, directories are ignored (glob marks
754 * those with a trailing '/'
756 static inline char *get_filename(char *path)
760 p = strrchr(path, '/');
767 static char **uci_list_config_files(struct uci_context *ctx)
775 dir = uci_malloc(ctx, strlen(ctx->confdir) + 1 + sizeof("/*"));
776 sprintf(dir, "%s/*", ctx->confdir);
777 if (glob(dir, GLOB_MARK, NULL, &globbuf) != 0) {
779 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
782 size = sizeof(char *) * (globbuf.gl_pathc + 1);
783 for(i = 0; i < globbuf.gl_pathc; i++) {
786 p = get_filename(globbuf.gl_pathv[i]);
790 size += strlen(p) + 1;
793 configs = uci_malloc(ctx, size);
794 buf = (char *) &configs[globbuf.gl_pathc + 1];
795 for(i = 0; i < globbuf.gl_pathc; i++) {
798 p = get_filename(globbuf.gl_pathv[i]);
802 if (!uci_validate_package(p))
807 buf += strlen(buf) + 1;
814 static struct uci_package *uci_file_load(struct uci_context *ctx, const char *name)
816 struct uci_package *package = NULL;
823 /* relative path outside of /etc/config */
825 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
828 /* absolute path outside of /etc/config */
829 filename = uci_strdup(ctx, name);
830 name = strrchr(name, '/') + 1;
834 /* config in /etc/config */
835 filename = uci_config_path(ctx, name);
840 UCI_TRAP_SAVE(ctx, done);
841 file = uci_open_stream(ctx, filename, SEEK_SET, false, false);
843 UCI_INTERNAL(uci_import, ctx, file, name, &package, true);
844 UCI_TRAP_RESTORE(ctx);
847 package->path = filename;
848 package->has_delta = confdir;
849 uci_load_delta(ctx, package, false);
853 uci_close_stream(file);
856 UCI_THROW(ctx, ctx->err);
861 __private UCI_BACKEND(uci_file_backend, "file",
862 .load = uci_file_load,
863 .commit = uci_file_commit,
864 .list_configs = uci_list_config_files,