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 * Clean up all extra memory used by the parser and exporter
31 static void uci_file_cleanup(struct uci_context *ctx)
33 struct uci_parse_context *pctx;
47 uci_free_package(&pctx->package);
57 * verify that the end of the line or command is reached.
58 * throw an error if extra arguments are given on the command line
60 static void assert_eol(struct uci_context *ctx, char **str)
64 tmp = next_arg(ctx, str, false, false);
65 if (*tmp && (ctx->flags & UCI_FLAG_STRICT))
66 uci_parse_error(ctx, *str, "too many arguments");
70 * switch to a different config, either triggered by uci_load, or by a
71 * 'package <...>' statement in the import file
73 static void uci_switch_config(struct uci_context *ctx)
75 struct uci_parse_context *pctx;
76 struct uci_element *e;
82 /* add the last config to main config file list */
84 uci_list_add(&ctx->root, &pctx->package->e.list);
94 * if an older config under the same name exists, unload it
95 * ignore errors here, e.g. if the config was not found
97 e = uci_lookup_list(ctx, &ctx->root, name);
99 UCI_THROW(ctx, UCI_ERR_DUPLICATE);
100 pctx->package = uci_alloc_package(ctx, name);
104 * parse the 'package' uci command (next config package)
106 static void uci_parse_package(struct uci_context *ctx, char **str, bool single)
110 /* command string null-terminated by strtok */
111 *str += strlen(*str) + 1;
113 name = next_arg(ctx, str, true, true);
114 assert_eol(ctx, str);
118 ctx->pctx->name = name;
119 uci_switch_config(ctx);
123 * parse the 'config' uci command (open a section)
125 static void uci_parse_config(struct uci_context *ctx, char **str)
127 struct uci_parse_context *pctx = ctx->pctx;
131 uci_fixup_section(ctx, ctx->pctx->section);
132 if (!ctx->pctx->package) {
133 if (!ctx->pctx->name)
134 uci_parse_error(ctx, *str, "attempting to import a file without a package name");
136 uci_switch_config(ctx);
139 /* command string null-terminated by strtok */
140 *str += strlen(*str) + 1;
142 type = next_arg(ctx, str, true, false);
143 if (!uci_validate_str(type, false))
144 uci_parse_error(ctx, type, "invalid character in field");
145 name = next_arg(ctx, str, false, true);
146 assert_eol(ctx, str);
149 UCI_TRAP_SAVE(ctx, error);
150 uci_set(ctx, pctx->package, name, NULL, type, NULL);
151 UCI_TRAP_RESTORE(ctx);
154 UCI_THROW(ctx, ctx->errno);
156 pctx->section = uci_alloc_section(pctx->package, type, name);
160 * parse the 'option' uci command (open a value)
162 static void uci_parse_option(struct uci_context *ctx, char **str)
164 struct uci_parse_context *pctx = ctx->pctx;
169 uci_parse_error(ctx, *str, "option command found before the first section");
171 /* command string null-terminated by strtok */
172 *str += strlen(*str) + 1;
174 name = next_arg(ctx, str, true, true);
175 value = next_arg(ctx, str, false, false);
176 assert_eol(ctx, str);
179 UCI_TRAP_SAVE(ctx, error);
180 uci_set(ctx, pctx->package, pctx->section->e.name, name, value, NULL);
181 UCI_TRAP_RESTORE(ctx);
184 UCI_THROW(ctx, ctx->errno);
186 uci_alloc_option(pctx->section, name, value);
191 * parse a complete input line, split up combined commands by ';'
193 static void uci_parse_line(struct uci_context *ctx, bool single)
195 struct uci_parse_context *pctx = ctx->pctx;
196 char *word, *brk = NULL;
198 for (word = strtok_r(pctx->buf, ";", &brk);
200 word = strtok_r(NULL, ";", &brk)) {
203 word = strtok_r(word, " \t", &pbrk);
212 if ((word[1] == 0) || !strcmp(word + 1, "ackage"))
213 uci_parse_package(ctx, &word, single);
216 if ((word[1] == 0) || !strcmp(word + 1, "onfig"))
217 uci_parse_config(ctx, &word);
220 if ((word[1] == 0) || !strcmp(word + 1, "ption"))
221 uci_parse_option(ctx, &word);
224 uci_parse_error(ctx, word, "unterminated command");
230 /* max number of characters that escaping adds to the string */
231 #define UCI_QUOTE_ESCAPE "'\\''"
234 * escape an uci string for export
236 static char *uci_escape(struct uci_context *ctx, char *str)
242 ctx->bufsz = LINEBUF;
243 ctx->buf = malloc(LINEBUF);
247 p = strchr(str, '\'');
254 if (p + sizeof(UCI_QUOTE_ESCAPE) - str >= ctx->bufsz) {
256 ctx->buf = realloc(ctx->buf, ctx->bufsz);
258 UCI_THROW(ctx, UCI_ERR_MEM);
260 memcpy(&ctx->buf[pos], s, len);
263 strcpy(&ctx->buf[pos], UCI_QUOTE_ESCAPE);
264 pos += sizeof(UCI_QUOTE_ESCAPE);
266 } while ((p = strchr(s, '\'')));
273 * export a single config package to a file stream
275 static void uci_export_package(struct uci_package *p, FILE *stream, bool header)
277 struct uci_context *ctx = p->ctx;
278 struct uci_element *s, *o;
281 fprintf(stream, "package '%s'\n", uci_escape(ctx, p->e.name));
282 uci_foreach_element(&p->sections, s) {
283 struct uci_section *sec = uci_to_section(s);
284 fprintf(stream, "\nconfig '%s'", uci_escape(ctx, sec->type));
285 if (!sec->anonymous || (ctx->flags & UCI_FLAG_EXPORT_NAME))
286 fprintf(stream, " '%s'", uci_escape(ctx, sec->e.name));
287 fprintf(stream, "\n");
288 uci_foreach_element(&sec->options, o) {
289 struct uci_option *opt = uci_to_option(o);
290 fprintf(stream, "\toption '%s'", uci_escape(ctx, opt->e.name));
291 fprintf(stream, " '%s'\n", uci_escape(ctx, opt->value));
294 fprintf(stream, "\n");
297 int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package, bool header)
299 struct uci_element *e;
302 UCI_ASSERT(ctx, stream != NULL);
305 uci_export_package(package, stream, header);
307 uci_foreach_element(&ctx->root, e) {
308 uci_export_package(uci_to_package(e), stream, header);
315 int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package, bool single)
317 struct uci_parse_context *pctx;
320 /* make sure no memory from previous parse attempts is leaked */
321 uci_file_cleanup(ctx);
323 uci_alloc_parse_context(ctx);
326 if (*package && single) {
327 pctx->package = *package;
332 * If 'name' was supplied, assume that the supplied stream does not contain
333 * the appropriate 'package <name>' string to specify the config name
334 * NB: the config file can still override the package name
337 UCI_ASSERT(ctx, uci_validate_name(name));
341 while (!feof(pctx->file)) {
343 UCI_TRAP_SAVE(ctx, error);
345 uci_parse_line(ctx, single);
346 UCI_TRAP_RESTORE(ctx);
349 if (ctx->flags & UCI_FLAG_PERROR)
350 uci_perror(ctx, NULL);
351 if ((ctx->errno != UCI_ERR_PARSE) ||
352 (ctx->flags & UCI_FLAG_STRICT))
353 UCI_THROW(ctx, ctx->errno);
356 uci_fixup_section(ctx, ctx->pctx->section);
358 *package = pctx->package;
360 pctx->package = NULL;
363 uci_switch_config(ctx);
365 /* no error happened, we can get rid of the parser context now */
366 uci_file_cleanup(ctx);
372 static char *uci_config_path(struct uci_context *ctx, const char *name)
376 UCI_ASSERT(ctx, uci_validate_name(name));
377 filename = uci_malloc(ctx, strlen(name) + strlen(ctx->confdir) + 2);
378 sprintf(filename, "%s/%s", ctx->confdir, name);
383 int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package)
393 /* relative path outside of /etc/config */
395 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
398 /* absolute path outside of /etc/config */
399 filename = uci_strdup(ctx, name);
400 name = strrchr(name, '/') + 1;
404 /* config in /etc/config */
405 filename = uci_config_path(ctx, name);
410 file = uci_open_stream(ctx, filename, SEEK_SET, false, false);
412 UCI_TRAP_SAVE(ctx, done);
413 UCI_INTERNAL(uci_import, ctx, file, name, package, true);
414 UCI_TRAP_RESTORE(ctx);
417 (*package)->path = filename;
418 (*package)->confdir = confdir;
419 uci_load_history(ctx, *package, false);
423 uci_close_stream(file);
427 int uci_commit(struct uci_context *ctx, struct uci_package **package, bool overwrite)
429 struct uci_package *p;
435 UCI_ASSERT(ctx, package != NULL);
438 UCI_ASSERT(ctx, p != NULL);
441 p->path = uci_config_path(ctx, p->e.name);
443 UCI_THROW(ctx, UCI_ERR_INVAL);
447 /* open the config file for writing now, so that it is locked */
448 f = uci_open_stream(ctx, p->path, SEEK_SET, true, true);
450 /* flush unsaved changes and reload from history file */
451 UCI_TRAP_SAVE(ctx, done);
454 name = uci_strdup(ctx, p->e.name);
455 path = uci_strdup(ctx, p->path);
456 /* dump our own changes to the history file */
457 if (!uci_list_empty(&p->history))
458 UCI_INTERNAL(uci_save, ctx, p);
461 * other processes might have modified the config
462 * as well. dump and reload
464 uci_free_package(&p);
465 uci_file_cleanup(ctx);
466 UCI_INTERNAL(uci_import, ctx, f, name, &p, true);
472 /* freed together with the uci_package */
475 /* check for updated history, flush */
476 if (!uci_load_history(ctx, p, true))
480 if (!uci_load_history(ctx, NULL, true))
486 ftruncate(fileno(f), 0);
488 uci_export(ctx, f, p, false);
489 UCI_TRAP_RESTORE(ctx);
498 UCI_THROW(ctx, ctx->errno);
505 * This function returns the filename by returning the string
506 * after the last '/' character. By checking for a non-'\0'
507 * character afterwards, directories are ignored (glob marks
508 * those with a trailing '/'
510 static inline char *get_filename(char *path)
514 p = strrchr(path, '/');
521 int uci_list_configs(struct uci_context *ctx, char ***list)
531 dir = uci_malloc(ctx, strlen(ctx->confdir) + 1 + sizeof("/*"));
532 sprintf(dir, "%s/*", ctx->confdir);
533 if (glob(dir, GLOB_MARK, NULL, &globbuf) != 0)
534 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
536 size = sizeof(char *) * (globbuf.gl_pathc + 1);
537 for(i = 0; i < globbuf.gl_pathc; i++) {
540 p = get_filename(globbuf.gl_pathv[i]);
544 size += strlen(p) + 1;
547 configs = uci_malloc(ctx, size);
548 buf = (char *) &configs[globbuf.gl_pathc + 1];
549 for(i = 0; i < globbuf.gl_pathc; i++) {
552 p = get_filename(globbuf.gl_pathv[i]);
558 buf += strlen(buf) + 1;