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 tmp = next_arg(ctx, str, false, false);
39 if (*tmp && (ctx->flags & UCI_FLAG_STRICT))
40 uci_parse_error(ctx, *str, "too many arguments");
44 * switch to a different config, either triggered by uci_load, or by a
45 * 'package <...>' statement in the import file
47 static void uci_switch_config(struct uci_context *ctx)
49 struct uci_parse_context *pctx;
50 struct uci_element *e;
56 /* add the last config to main config file list */
58 pctx->package->backend = ctx->backend;
59 uci_list_add(&ctx->root, &pctx->package->e.list);
69 * if an older config under the same name exists, unload it
70 * ignore errors here, e.g. if the config was not found
72 e = uci_lookup_list(ctx, &ctx->root, name);
74 UCI_THROW(ctx, UCI_ERR_DUPLICATE);
75 pctx->package = uci_alloc_package(ctx, name);
79 * parse the 'package' uci command (next config package)
81 static void uci_parse_package(struct uci_context *ctx, char **str, bool single)
85 /* command string null-terminated by strtok */
86 *str += strlen(*str) + 1;
88 name = next_arg(ctx, str, true, true);
93 ctx->pctx->name = name;
94 uci_switch_config(ctx);
98 * parse the 'config' uci command (open a section)
100 static void uci_parse_config(struct uci_context *ctx, char **str)
102 struct uci_parse_context *pctx = ctx->pctx;
106 uci_fixup_section(ctx, ctx->pctx->section);
107 if (!ctx->pctx->package) {
108 if (!ctx->pctx->name)
109 uci_parse_error(ctx, *str, "attempting to import a file without a package name");
111 uci_switch_config(ctx);
114 /* command string null-terminated by strtok */
115 *str += strlen(*str) + 1;
117 type = next_arg(ctx, str, true, false);
118 if (!uci_validate_str(type, false))
119 uci_parse_error(ctx, type, "invalid character in field");
120 name = next_arg(ctx, str, false, true);
121 assert_eol(ctx, str);
124 UCI_TRAP_SAVE(ctx, error);
125 uci_set(ctx, pctx->package, name, NULL, type, NULL);
126 UCI_TRAP_RESTORE(ctx);
129 UCI_THROW(ctx, ctx->errno);
131 pctx->section = uci_alloc_section(pctx->package, type, name);
135 * parse the 'option' uci command (open a value)
137 static void uci_parse_option(struct uci_context *ctx, char **str)
139 struct uci_parse_context *pctx = ctx->pctx;
144 uci_parse_error(ctx, *str, "option command found before the first section");
146 /* command string null-terminated by strtok */
147 *str += strlen(*str) + 1;
149 name = next_arg(ctx, str, true, true);
150 value = next_arg(ctx, str, false, false);
151 assert_eol(ctx, str);
154 UCI_TRAP_SAVE(ctx, error);
155 uci_set(ctx, pctx->package, pctx->section->e.name, name, value, NULL);
156 UCI_TRAP_RESTORE(ctx);
159 UCI_THROW(ctx, ctx->errno);
161 uci_alloc_option(pctx->section, name, value);
166 * parse a complete input line, split up combined commands by ';'
168 static void uci_parse_line(struct uci_context *ctx, bool single)
170 struct uci_parse_context *pctx = ctx->pctx;
171 char *word, *brk = NULL;
173 for (word = strtok_r(pctx->buf, ";", &brk);
175 word = strtok_r(NULL, ";", &brk)) {
178 word = strtok_r(word, " \t", &pbrk);
187 if ((word[1] == 0) || !strcmp(word + 1, "ackage"))
188 uci_parse_package(ctx, &word, single);
191 if ((word[1] == 0) || !strcmp(word + 1, "onfig"))
192 uci_parse_config(ctx, &word);
195 if ((word[1] == 0) || !strcmp(word + 1, "ption"))
196 uci_parse_option(ctx, &word);
199 uci_parse_error(ctx, word, "unterminated command");
205 /* max number of characters that escaping adds to the string */
206 #define UCI_QUOTE_ESCAPE "'\\''"
209 * escape an uci string for export
211 static char *uci_escape(struct uci_context *ctx, char *str)
217 ctx->bufsz = LINEBUF;
218 ctx->buf = malloc(LINEBUF);
222 p = strchr(str, '\'');
229 if (p + sizeof(UCI_QUOTE_ESCAPE) - str >= ctx->bufsz) {
231 ctx->buf = realloc(ctx->buf, ctx->bufsz);
233 UCI_THROW(ctx, UCI_ERR_MEM);
235 memcpy(&ctx->buf[pos], s, len);
238 strcpy(&ctx->buf[pos], UCI_QUOTE_ESCAPE);
239 pos += sizeof(UCI_QUOTE_ESCAPE);
241 } while ((p = strchr(s, '\'')));
248 * export a single config package to a file stream
250 static void uci_export_package(struct uci_package *p, FILE *stream, bool header)
252 struct uci_context *ctx = p->ctx;
253 struct uci_element *s, *o;
256 fprintf(stream, "package '%s'\n", uci_escape(ctx, p->e.name));
257 uci_foreach_element(&p->sections, s) {
258 struct uci_section *sec = uci_to_section(s);
259 fprintf(stream, "\nconfig '%s'", uci_escape(ctx, sec->type));
260 if (!sec->anonymous || (ctx->flags & UCI_FLAG_EXPORT_NAME))
261 fprintf(stream, " '%s'", uci_escape(ctx, sec->e.name));
262 fprintf(stream, "\n");
263 uci_foreach_element(&sec->options, o) {
264 struct uci_option *opt = uci_to_option(o);
265 fprintf(stream, "\toption '%s'", uci_escape(ctx, opt->e.name));
266 fprintf(stream, " '%s'\n", uci_escape(ctx, opt->value));
269 fprintf(stream, "\n");
272 int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package, bool header)
274 struct uci_element *e;
277 UCI_ASSERT(ctx, stream != NULL);
280 uci_export_package(package, stream, header);
282 uci_foreach_element(&ctx->root, e) {
283 uci_export_package(uci_to_package(e), stream, header);
290 int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package, bool single)
292 struct uci_parse_context *pctx;
295 /* make sure no memory from previous parse attempts is leaked */
298 uci_alloc_parse_context(ctx);
301 if (*package && single) {
302 pctx->package = *package;
307 * If 'name' was supplied, assume that the supplied stream does not contain
308 * the appropriate 'package <name>' string to specify the config name
309 * NB: the config file can still override the package name
312 UCI_ASSERT(ctx, uci_validate_name(name));
316 while (!feof(pctx->file)) {
318 UCI_TRAP_SAVE(ctx, error);
320 uci_parse_line(ctx, single);
321 UCI_TRAP_RESTORE(ctx);
324 if (ctx->flags & UCI_FLAG_PERROR)
325 uci_perror(ctx, NULL);
326 if ((ctx->errno != UCI_ERR_PARSE) ||
327 (ctx->flags & UCI_FLAG_STRICT))
328 UCI_THROW(ctx, ctx->errno);
331 uci_fixup_section(ctx, ctx->pctx->section);
333 *package = pctx->package;
335 pctx->package = NULL;
338 uci_switch_config(ctx);
340 /* no error happened, we can get rid of the parser context now */
347 static char *uci_config_path(struct uci_context *ctx, const char *name)
351 UCI_ASSERT(ctx, uci_validate_name(name));
352 filename = uci_malloc(ctx, strlen(name) + strlen(ctx->confdir) + 2);
353 sprintf(filename, "%s/%s", ctx->confdir, name);
358 void uci_file_commit(struct uci_context *ctx, struct uci_package **package, bool overwrite)
360 struct uci_package *p = *package;
367 p->path = uci_config_path(ctx, p->e.name);
369 UCI_THROW(ctx, UCI_ERR_INVAL);
372 /* open the config file for writing now, so that it is locked */
373 f = uci_open_stream(ctx, p->path, SEEK_SET, true, true);
375 /* flush unsaved changes and reload from history file */
376 UCI_TRAP_SAVE(ctx, done);
377 if (p->has_history) {
379 name = uci_strdup(ctx, p->e.name);
380 path = uci_strdup(ctx, p->path);
381 /* dump our own changes to the history file */
382 if (!uci_list_empty(&p->history))
383 UCI_INTERNAL(uci_save, ctx, p);
386 * other processes might have modified the config
387 * as well. dump and reload
389 uci_free_package(&p);
391 UCI_INTERNAL(uci_import, ctx, f, name, &p, true);
394 p->has_history = true;
397 /* freed together with the uci_package */
400 /* check for updated history, flush */
401 if (!uci_load_history(ctx, p, true))
405 if (!uci_load_history(ctx, NULL, true))
411 ftruncate(fileno(f), 0);
413 uci_export(ctx, f, p, false);
414 UCI_TRAP_RESTORE(ctx);
423 UCI_THROW(ctx, ctx->errno);
428 * This function returns the filename by returning the string
429 * after the last '/' character. By checking for a non-'\0'
430 * character afterwards, directories are ignored (glob marks
431 * those with a trailing '/'
433 static inline char *get_filename(char *path)
437 p = strrchr(path, '/');
444 static char **uci_list_config_files(struct uci_context *ctx)
452 dir = uci_malloc(ctx, strlen(ctx->confdir) + 1 + sizeof("/*"));
453 sprintf(dir, "%s/*", ctx->confdir);
454 if (glob(dir, GLOB_MARK, NULL, &globbuf) != 0)
455 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
457 size = sizeof(char *) * (globbuf.gl_pathc + 1);
458 for(i = 0; i < globbuf.gl_pathc; i++) {
461 p = get_filename(globbuf.gl_pathv[i]);
465 size += strlen(p) + 1;
468 configs = uci_malloc(ctx, size);
469 buf = (char *) &configs[globbuf.gl_pathc + 1];
470 for(i = 0; i < globbuf.gl_pathc; i++) {
473 p = get_filename(globbuf.gl_pathv[i]);
479 buf += strlen(buf) + 1;
485 static struct uci_package *uci_file_load(struct uci_context *ctx, const char *name)
487 struct uci_package *package = NULL;
494 /* relative path outside of /etc/config */
496 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
499 /* absolute path outside of /etc/config */
500 filename = uci_strdup(ctx, name);
501 name = strrchr(name, '/') + 1;
505 /* config in /etc/config */
506 filename = uci_config_path(ctx, name);
511 file = uci_open_stream(ctx, filename, SEEK_SET, false, false);
513 UCI_TRAP_SAVE(ctx, done);
514 UCI_INTERNAL(uci_import, ctx, file, name, &package, true);
515 UCI_TRAP_RESTORE(ctx);
518 package->path = filename;
519 package->has_history = confdir;
520 uci_load_history(ctx, package, false);
524 uci_close_stream(file);
526 UCI_THROW(ctx, ctx->errno);
530 static struct uci_backend uci_file_backend = {
532 .load = uci_file_load,
533 .commit = uci_file_commit,
534 .list_configs = uci_list_config_files,