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>
28 static struct uci_backend uci_file_backend;
31 * verify that the end of the line or command is reached.
32 * throw an error if extra arguments are given on the command line
34 static void assert_eol(struct uci_context *ctx, char **str)
38 skip_whitespace(ctx, str);
39 tmp = next_arg(ctx, str, false, false);
40 if (*tmp && (ctx->flags & UCI_FLAG_STRICT))
41 uci_parse_error(ctx, *str, "too many arguments");
45 * switch to a different config, either triggered by uci_load, or by a
46 * 'package <...>' statement in the import file
48 static void uci_switch_config(struct uci_context *ctx)
50 struct uci_parse_context *pctx;
51 struct uci_element *e;
57 /* add the last config to main config file list */
59 pctx->package->backend = ctx->backend;
60 uci_list_add(&ctx->root, &pctx->package->e.list);
70 * if an older config under the same name exists, unload it
71 * ignore errors here, e.g. if the config was not found
73 e = uci_lookup_list(&ctx->root, name);
75 UCI_THROW(ctx, UCI_ERR_DUPLICATE);
76 pctx->package = uci_alloc_package(ctx, name);
80 * parse the 'package' uci command (next config package)
82 static void uci_parse_package(struct uci_context *ctx, char **str, bool single)
86 /* command string null-terminated by strtok */
87 *str += strlen(*str) + 1;
89 name = next_arg(ctx, str, true, true);
94 ctx->pctx->name = name;
95 uci_switch_config(ctx);
99 * parse the 'config' uci command (open a section)
101 static void uci_parse_config(struct uci_context *ctx, char **str)
103 struct uci_parse_context *pctx = ctx->pctx;
107 uci_fixup_section(ctx, ctx->pctx->section);
108 if (!ctx->pctx->package) {
109 if (!ctx->pctx->name)
110 uci_parse_error(ctx, *str, "attempting to import a file without a package name");
112 uci_switch_config(ctx);
115 /* command string null-terminated by strtok */
116 *str += strlen(*str) + 1;
118 type = next_arg(ctx, str, true, false);
119 if (!uci_validate_str(type, false))
120 uci_parse_error(ctx, type, "invalid character in field");
121 name = next_arg(ctx, str, false, true);
122 assert_eol(ctx, str);
125 UCI_TRAP_SAVE(ctx, error);
126 if (uci_set(ctx, pctx->package, name, NULL, type, NULL) != UCI_OK)
128 UCI_TRAP_RESTORE(ctx);
131 UCI_THROW(ctx, ctx->err);
133 pctx->section = uci_alloc_section(pctx->package, type, name);
137 * parse the 'option' uci command (open a value)
139 static void uci_parse_option(struct uci_context *ctx, char **str)
141 struct uci_parse_context *pctx = ctx->pctx;
146 uci_parse_error(ctx, *str, "option command found before the first section");
148 /* command string null-terminated by strtok */
149 *str += strlen(*str) + 1;
151 name = next_arg(ctx, str, true, true);
152 value = next_arg(ctx, str, false, false);
153 assert_eol(ctx, str);
156 UCI_TRAP_SAVE(ctx, error);
157 uci_set(ctx, pctx->package, pctx->section->e.name, name, value, NULL);
158 UCI_TRAP_RESTORE(ctx);
161 UCI_THROW(ctx, ctx->err);
163 uci_alloc_option(pctx->section, name, value);
168 * parse a complete input line, split up combined commands by ';'
170 static void uci_parse_line(struct uci_context *ctx, bool single)
172 struct uci_parse_context *pctx = ctx->pctx;
178 word = strtok_r(word, " \t", &brk);
187 if ((word[1] == 0) || !strcmp(word + 1, "ackage"))
188 uci_parse_package(ctx, &word, single);
193 if ((word[1] == 0) || !strcmp(word + 1, "onfig"))
194 uci_parse_config(ctx, &word);
199 if ((word[1] == 0) || !strcmp(word + 1, "ption"))
200 uci_parse_option(ctx, &word);
209 uci_parse_error(ctx, word, "invalid command");
213 /* max number of characters that escaping adds to the string */
214 #define UCI_QUOTE_ESCAPE "'\\''"
217 * escape an uci string for export
219 static char *uci_escape(struct uci_context *ctx, const char *str)
225 ctx->bufsz = LINEBUF;
226 ctx->buf = malloc(LINEBUF);
232 end = strchr(str, '\'');
234 end = str + strlen(str);
237 /* make sure that we have enough room in the buffer */
238 while (ofs + len + sizeof(UCI_QUOTE_ESCAPE) + 1 > ctx->bufsz) {
240 ctx->buf = uci_realloc(ctx, ctx->buf, ctx->bufsz);
243 /* copy the string until the character before the quote */
244 memcpy(&ctx->buf[ofs], str, len);
247 /* end of string? return the buffer */
251 memcpy(&ctx->buf[ofs], UCI_QUOTE_ESCAPE, sizeof(UCI_QUOTE_ESCAPE));
252 ofs += strlen(&ctx->buf[ofs]);
261 * export a single config package to a file stream
263 static void uci_export_package(struct uci_package *p, FILE *stream, bool header)
265 struct uci_context *ctx = p->ctx;
266 struct uci_element *s, *o;
269 fprintf(stream, "package '%s'\n", uci_escape(ctx, p->e.name));
270 uci_foreach_element(&p->sections, s) {
271 struct uci_section *sec = uci_to_section(s);
272 fprintf(stream, "\nconfig '%s'", uci_escape(ctx, sec->type));
273 if (!sec->anonymous || (ctx->flags & UCI_FLAG_EXPORT_NAME))
274 fprintf(stream, " '%s'", uci_escape(ctx, sec->e.name));
275 fprintf(stream, "\n");
276 uci_foreach_element(&sec->options, o) {
277 struct uci_option *opt = uci_to_option(o);
278 fprintf(stream, "\toption '%s'", uci_escape(ctx, opt->e.name));
279 fprintf(stream, " '%s'\n", uci_escape(ctx, opt->value));
282 fprintf(stream, "\n");
285 int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package, bool header)
287 struct uci_element *e;
290 UCI_ASSERT(ctx, stream != NULL);
293 uci_export_package(package, stream, header);
295 uci_foreach_element(&ctx->root, e) {
296 uci_export_package(uci_to_package(e), stream, header);
303 int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package, bool single)
305 struct uci_parse_context *pctx;
308 /* make sure no memory from previous parse attempts is leaked */
311 uci_alloc_parse_context(ctx);
314 if (*package && single) {
315 pctx->package = *package;
320 * If 'name' was supplied, assume that the supplied stream does not contain
321 * the appropriate 'package <name>' string to specify the config name
322 * NB: the config file can still override the package name
325 UCI_ASSERT(ctx, uci_validate_str(name, false));
329 while (!feof(pctx->file)) {
331 UCI_TRAP_SAVE(ctx, error);
333 uci_parse_line(ctx, single);
334 UCI_TRAP_RESTORE(ctx);
337 if (ctx->flags & UCI_FLAG_PERROR)
338 uci_perror(ctx, NULL);
339 if ((ctx->err != UCI_ERR_PARSE) ||
340 (ctx->flags & UCI_FLAG_STRICT))
341 UCI_THROW(ctx, ctx->err);
344 uci_fixup_section(ctx, ctx->pctx->section);
345 if (!pctx->package && name)
346 uci_switch_config(ctx);
348 *package = pctx->package;
350 pctx->package = NULL;
353 uci_switch_config(ctx);
355 /* no error happened, we can get rid of the parser context now */
362 static char *uci_config_path(struct uci_context *ctx, const char *name)
366 UCI_ASSERT(ctx, uci_validate_str(name, false));
367 filename = uci_malloc(ctx, strlen(name) + strlen(ctx->confdir) + 2);
368 sprintf(filename, "%s/%s", ctx->confdir, name);
373 void uci_file_commit(struct uci_context *ctx, struct uci_package **package, bool overwrite)
375 struct uci_package *p = *package;
382 p->path = uci_config_path(ctx, p->e.name);
384 UCI_THROW(ctx, UCI_ERR_INVAL);
387 /* open the config file for writing now, so that it is locked */
388 f = uci_open_stream(ctx, p->path, SEEK_SET, true, true);
390 /* flush unsaved changes and reload from history file */
391 UCI_TRAP_SAVE(ctx, done);
392 if (p->has_history) {
394 name = uci_strdup(ctx, p->e.name);
395 path = uci_strdup(ctx, p->path);
396 /* dump our own changes to the history file */
397 if (!uci_list_empty(&p->history))
398 UCI_INTERNAL(uci_save, ctx, p);
401 * other processes might have modified the config
402 * as well. dump and reload
404 uci_free_package(&p);
406 UCI_INTERNAL(uci_import, ctx, f, name, &p, true);
409 p->has_history = true;
412 /* freed together with the uci_package */
415 /* check for updated history, flush */
416 if (!uci_load_history(ctx, p, true))
420 if (!uci_load_history(ctx, NULL, true))
426 ftruncate(fileno(f), 0);
428 uci_export(ctx, f, p, false);
429 UCI_TRAP_RESTORE(ctx);
438 UCI_THROW(ctx, ctx->err);
443 * This function returns the filename by returning the string
444 * after the last '/' character. By checking for a non-'\0'
445 * character afterwards, directories are ignored (glob marks
446 * those with a trailing '/'
448 static inline char *get_filename(char *path)
452 p = strrchr(path, '/');
459 static char **uci_list_config_files(struct uci_context *ctx)
467 dir = uci_malloc(ctx, strlen(ctx->confdir) + 1 + sizeof("/*"));
468 sprintf(dir, "%s/*", ctx->confdir);
469 if (glob(dir, GLOB_MARK, NULL, &globbuf) != 0)
470 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
472 size = sizeof(char *) * (globbuf.gl_pathc + 1);
473 for(i = 0; i < globbuf.gl_pathc; i++) {
476 p = get_filename(globbuf.gl_pathv[i]);
480 size += strlen(p) + 1;
483 configs = uci_malloc(ctx, size);
484 buf = (char *) &configs[globbuf.gl_pathc + 1];
485 for(i = 0; i < globbuf.gl_pathc; i++) {
488 p = get_filename(globbuf.gl_pathv[i]);
492 if (!uci_validate_name(p))
497 buf += strlen(buf) + 1;
503 static struct uci_package *uci_file_load(struct uci_context *ctx, const char *name)
505 struct uci_package *package = NULL;
512 /* relative path outside of /etc/config */
514 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
517 /* absolute path outside of /etc/config */
518 filename = uci_strdup(ctx, name);
519 name = strrchr(name, '/') + 1;
523 /* config in /etc/config */
524 filename = uci_config_path(ctx, name);
529 file = uci_open_stream(ctx, filename, SEEK_SET, false, false);
531 UCI_TRAP_SAVE(ctx, done);
532 UCI_INTERNAL(uci_import, ctx, file, name, &package, true);
533 UCI_TRAP_RESTORE(ctx);
536 package->path = filename;
537 package->has_history = confdir;
538 uci_load_history(ctx, package, false);
542 uci_close_stream(file);
544 UCI_THROW(ctx, ctx->err);
548 static UCI_BACKEND(uci_file_backend, "file",
549 .load = uci_file_load,
550 .commit = uci_file_commit,
551 .list_configs = uci_list_config_files,