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
20 #include <sys/types.h>
29 static struct uci_backend uci_file_backend;
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
35 static void assert_eol(struct uci_context *ctx, char **str)
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");
46 * switch to a different config, either triggered by uci_load, or by a
47 * 'package <...>' statement in the import file
49 static void uci_switch_config(struct uci_context *ctx)
51 struct uci_parse_context *pctx;
52 struct uci_element *e;
58 /* add the last config to main config file list */
60 pctx->package->backend = ctx->backend;
61 uci_list_add(&ctx->root, &pctx->package->e.list);
71 * if an older config under the same name exists, unload it
72 * ignore errors here, e.g. if the config was not found
74 e = uci_lookup_list(&ctx->root, name);
76 UCI_THROW(ctx, UCI_ERR_DUPLICATE);
77 pctx->package = uci_alloc_package(ctx, name);
81 * parse the 'package' uci command (next config package)
83 static void uci_parse_package(struct uci_context *ctx, char **str, bool single)
87 /* command string null-terminated by strtok */
88 *str += strlen(*str) + 1;
90 name = next_arg(ctx, str, true, true);
95 ctx->pctx->name = name;
96 uci_switch_config(ctx);
100 * parse the 'config' uci command (open a section)
102 static void uci_parse_config(struct uci_context *ctx, char **str)
104 struct uci_parse_context *pctx = ctx->pctx;
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");
113 uci_switch_config(ctx);
116 /* command string null-terminated by strtok */
117 *str += strlen(*str) + 1;
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);
126 UCI_NESTED(uci_set, ctx, pctx->package, name, NULL, type, NULL);
128 pctx->section = uci_alloc_section(pctx->package, type, name);
132 * parse the 'option' uci command (open a value)
134 static void uci_parse_option(struct uci_context *ctx, char **str)
136 struct uci_parse_context *pctx = ctx->pctx;
141 uci_parse_error(ctx, *str, "option command found before the first section");
143 /* command string null-terminated by strtok */
144 *str += strlen(*str) + 1;
146 name = next_arg(ctx, str, true, true);
147 value = next_arg(ctx, str, false, false);
148 assert_eol(ctx, str);
151 UCI_NESTED(uci_set, ctx, pctx->package, pctx->section->e.name, name, value, NULL);
153 uci_alloc_option(pctx->section, name, value);
156 static void uci_parse_list(struct uci_context *ctx, char **str)
158 struct uci_parse_context *pctx = ctx->pctx;
164 uci_parse_error(ctx, *str, "list command found before the first section");
166 /* command string null-terminated by strtok */
167 *str += strlen(*str) + 1;
169 name = next_arg(ctx, str, true, true);
170 value = next_arg(ctx, str, false, false);
171 assert_eol(ctx, str);
173 UCI_NESTED(uci_fill_ptr, ctx, &ptr, &pctx->section->e, false);
177 UCI_INTERNAL(uci_lookup_ptr, ctx, &ptr, NULL, false);
179 ctx->internal = !pctx->merge;
180 UCI_NESTED(uci_add_list, ctx, &ptr);
185 * parse a complete input line, split up combined commands by ';'
187 static void uci_parse_line(struct uci_context *ctx, bool single)
189 struct uci_parse_context *pctx = ctx->pctx;
195 word = strtok_r(word, " \t", &brk);
204 if ((word[1] == 0) || !strcmp(word + 1, "ackage"))
205 uci_parse_package(ctx, &word, single);
210 if ((word[1] == 0) || !strcmp(word + 1, "onfig"))
211 uci_parse_config(ctx, &word);
216 if ((word[1] == 0) || !strcmp(word + 1, "ption"))
217 uci_parse_option(ctx, &word);
222 if ((word[1] == 0) || !strcmp(word + 1, "ist"))
223 uci_parse_list(ctx, &word);
232 uci_parse_error(ctx, word, "invalid command");
236 /* max number of characters that escaping adds to the string */
237 #define UCI_QUOTE_ESCAPE "'\\''"
240 * escape an uci string for export
242 static char *uci_escape(struct uci_context *ctx, const char *str)
248 ctx->bufsz = LINEBUF;
249 ctx->buf = malloc(LINEBUF);
255 end = strchr(str, '\'');
257 end = str + strlen(str);
260 /* make sure that we have enough room in the buffer */
261 while (ofs + len + sizeof(UCI_QUOTE_ESCAPE) + 1 > ctx->bufsz) {
263 ctx->buf = uci_realloc(ctx, ctx->buf, ctx->bufsz);
266 /* copy the string until the character before the quote */
267 memcpy(&ctx->buf[ofs], str, len);
270 /* end of string? return the buffer */
274 memcpy(&ctx->buf[ofs], UCI_QUOTE_ESCAPE, sizeof(UCI_QUOTE_ESCAPE));
275 ofs += strlen(&ctx->buf[ofs]);
284 * export a single config package to a file stream
286 static void uci_export_package(struct uci_package *p, FILE *stream, bool header)
288 struct uci_context *ctx = p->ctx;
289 struct uci_element *s, *o, *i;
292 fprintf(stream, "package '%s'\n", uci_escape(ctx, p->e.name));
293 uci_foreach_element(&p->sections, s) {
294 struct uci_section *sec = uci_to_section(s);
295 fprintf(stream, "\nconfig '%s'", uci_escape(ctx, sec->type));
296 if (!sec->anonymous || (ctx->flags & UCI_FLAG_EXPORT_NAME))
297 fprintf(stream, " '%s'", uci_escape(ctx, sec->e.name));
298 fprintf(stream, "\n");
299 uci_foreach_element(&sec->options, o) {
300 struct uci_option *opt = uci_to_option(o);
302 case UCI_TYPE_STRING:
303 fprintf(stream, "\toption '%s'", uci_escape(ctx, opt->e.name));
304 fprintf(stream, " '%s'\n", uci_escape(ctx, opt->v.string));
307 uci_foreach_element(&opt->v.list, i) {
308 fprintf(stream, "\tlist '%s'", uci_escape(ctx, opt->e.name));
309 fprintf(stream, " '%s'\n", uci_escape(ctx, i->name));
313 fprintf(stream, "\t# unknown type for option '%s'\n", uci_escape(ctx, opt->e.name));
318 fprintf(stream, "\n");
321 int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package, bool header)
323 struct uci_element *e;
326 UCI_ASSERT(ctx, stream != NULL);
329 uci_export_package(package, stream, header);
331 uci_foreach_element(&ctx->root, e) {
332 uci_export_package(uci_to_package(e), stream, header);
339 int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package, bool single)
341 struct uci_parse_context *pctx;
344 /* make sure no memory from previous parse attempts is leaked */
347 uci_alloc_parse_context(ctx);
350 if (*package && single) {
351 pctx->package = *package;
356 * If 'name' was supplied, assume that the supplied stream does not contain
357 * the appropriate 'package <name>' string to specify the config name
358 * NB: the config file can still override the package name
361 UCI_ASSERT(ctx, uci_validate_str(name, false));
365 while (!feof(pctx->file)) {
367 UCI_TRAP_SAVE(ctx, error);
369 uci_parse_line(ctx, single);
370 UCI_TRAP_RESTORE(ctx);
373 if (ctx->flags & UCI_FLAG_PERROR)
374 uci_perror(ctx, NULL);
375 if ((ctx->err != UCI_ERR_PARSE) ||
376 (ctx->flags & UCI_FLAG_STRICT))
377 UCI_THROW(ctx, ctx->err);
380 uci_fixup_section(ctx, ctx->pctx->section);
381 if (!pctx->package && name)
382 uci_switch_config(ctx);
384 *package = pctx->package;
386 pctx->package = NULL;
389 uci_switch_config(ctx);
391 /* no error happened, we can get rid of the parser context now */
398 static char *uci_config_path(struct uci_context *ctx, const char *name)
402 UCI_ASSERT(ctx, uci_validate_str(name, false));
403 filename = uci_malloc(ctx, strlen(name) + strlen(ctx->confdir) + 2);
404 sprintf(filename, "%s/%s", ctx->confdir, name);
409 void uci_file_commit(struct uci_context *ctx, struct uci_package **package, bool overwrite)
411 struct uci_package *p = *package;
418 p->path = uci_config_path(ctx, p->e.name);
420 UCI_THROW(ctx, UCI_ERR_INVAL);
423 /* open the config file for writing now, so that it is locked */
424 f = uci_open_stream(ctx, p->path, SEEK_SET, true, true);
426 /* flush unsaved changes and reload from history file */
427 UCI_TRAP_SAVE(ctx, done);
428 if (p->has_history) {
430 name = uci_strdup(ctx, p->e.name);
431 path = uci_strdup(ctx, p->path);
432 /* dump our own changes to the history file */
433 if (!uci_list_empty(&p->history))
434 UCI_INTERNAL(uci_save, ctx, p);
437 * other processes might have modified the config
438 * as well. dump and reload
440 uci_free_package(&p);
442 UCI_INTERNAL(uci_import, ctx, f, name, &p, true);
445 p->has_history = true;
448 /* freed together with the uci_package */
451 /* check for updated history, flush */
452 if (!uci_load_history(ctx, p, true))
456 if (!uci_load_history(ctx, NULL, true))
462 ftruncate(fileno(f), 0);
464 uci_export(ctx, f, p, false);
465 UCI_TRAP_RESTORE(ctx);
474 UCI_THROW(ctx, ctx->err);
479 * This function returns the filename by returning the string
480 * after the last '/' character. By checking for a non-'\0'
481 * character afterwards, directories are ignored (glob marks
482 * those with a trailing '/'
484 static inline char *get_filename(char *path)
488 p = strrchr(path, '/');
495 static char **uci_list_config_files(struct uci_context *ctx)
503 dir = uci_malloc(ctx, strlen(ctx->confdir) + 1 + sizeof("/*"));
504 sprintf(dir, "%s/*", ctx->confdir);
505 if (glob(dir, GLOB_MARK, NULL, &globbuf) != 0)
506 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
508 size = sizeof(char *) * (globbuf.gl_pathc + 1);
509 for(i = 0; i < globbuf.gl_pathc; i++) {
512 p = get_filename(globbuf.gl_pathv[i]);
516 size += strlen(p) + 1;
519 configs = uci_malloc(ctx, size);
520 buf = (char *) &configs[globbuf.gl_pathc + 1];
521 for(i = 0; i < globbuf.gl_pathc; i++) {
524 p = get_filename(globbuf.gl_pathv[i]);
528 if (!uci_validate_name(p))
533 buf += strlen(buf) + 1;
539 static struct uci_package *uci_file_load(struct uci_context *ctx, const char *name)
541 struct uci_package *package = NULL;
548 /* relative path outside of /etc/config */
550 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
553 /* absolute path outside of /etc/config */
554 filename = uci_strdup(ctx, name);
555 name = strrchr(name, '/') + 1;
559 /* config in /etc/config */
560 filename = uci_config_path(ctx, name);
565 file = uci_open_stream(ctx, filename, SEEK_SET, false, false);
567 UCI_TRAP_SAVE(ctx, done);
568 UCI_INTERNAL(uci_import, ctx, file, name, &package, true);
569 UCI_TRAP_RESTORE(ctx);
572 package->path = filename;
573 package->has_history = confdir;
574 uci_load_history(ctx, package, false);
578 uci_close_stream(file);
580 UCI_THROW(ctx, ctx->err);
584 static UCI_BACKEND(uci_file_backend, "file",
585 .load = uci_file_load,
586 .commit = uci_file_commit,
587 .list_configs = uci_list_config_files,