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
19 #include <sys/types.h>
26 #define LINEBUF_MAX 4096
29 * Fetch a new line from the input stream and resize buffer if necessary
31 static void uci_getln(struct uci_context *ctx, int offset)
33 struct uci_parse_context *pctx = ctx->pctx;
37 if (pctx->buf == NULL) {
38 pctx->buf = uci_malloc(ctx, LINEBUF);
39 pctx->bufsz = LINEBUF;
47 p = fgets(p, pctx->bufsz - ofs, pctx->file);
52 if (pctx->buf[ofs - 1] == '\n') {
54 pctx->buf[ofs - 1] = 0;
58 if (pctx->bufsz > LINEBUF_MAX/2) {
59 pctx->reason = "line too long";
60 pctx->byte = LINEBUF_MAX;
61 UCI_THROW(ctx, UCI_ERR_PARSE);
65 pctx->buf = uci_realloc(ctx, pctx->buf, pctx->bufsz);
70 * Clean up all extra memory used by the parser and exporter
72 static void uci_file_cleanup(struct uci_context *ctx)
74 struct uci_parse_context *pctx;
88 uci_free_package(pctx->package);
99 * parse a character escaped by '\'
100 * returns true if the escaped character is to be parsed
101 * returns false if the escaped character is to be ignored
103 static inline bool parse_backslash(struct uci_context *ctx, char **str)
108 /* undecoded backslash at the end of line, fetch the next line */
111 uci_getln(ctx, *str - ctx->pctx->buf);
115 /* FIXME: decode escaped char, necessary? */
120 * move the string pointer forward until a non-whitespace character or
123 static void skip_whitespace(struct uci_context *ctx, char **str)
126 while (**str && isspace(**str))
130 if (!parse_backslash(ctx, str))
135 static inline void addc(char **dest, char **src)
143 * parse a double quoted string argument from the command line
145 static void parse_double_quote(struct uci_context *ctx, char **str, char **target)
149 /* skip quote character */
152 while ((c = **str)) {
159 if (!parse_backslash(ctx, str))
167 ctx->pctx->reason = "unterminated \"";
168 ctx->pctx->byte = *str - ctx->pctx->buf;
169 UCI_THROW(ctx, UCI_ERR_PARSE);
173 * parse a single quoted string argument from the command line
175 static void parse_single_quote(struct uci_context *ctx, char **str, char **target)
178 /* skip quote character */
181 while ((c = **str)) {
191 ctx->pctx->reason = "unterminated '";
192 ctx->pctx->byte = *str - ctx->pctx->buf;
193 UCI_THROW(ctx, UCI_ERR_PARSE);
197 * parse a string from the command line and detect the quoting style
199 static void parse_str(struct uci_context *ctx, char **str, char **target)
204 parse_single_quote(ctx, str, target);
207 parse_double_quote(ctx, str, target);
212 if (!parse_backslash(ctx, str))
219 } while (**str && !isspace(**str));
223 * if the string was unquoted and we've stopped at a whitespace
224 * character, skip to the next one, because the whitespace will
225 * be overwritten by a null byte here
230 /* terminate the parsed string */
235 * extract the next argument from the command line
237 static char *next_arg(struct uci_context *ctx, char **str, bool required)
243 skip_whitespace(ctx, str);
244 parse_str(ctx, str, &ptr);
245 if (required && !*val) {
246 ctx->pctx->reason = "insufficient arguments";
247 ctx->pctx->byte = *str - ctx->pctx->buf;
248 UCI_THROW(ctx, UCI_ERR_PARSE);
255 * verify that the end of the line or command is reached.
256 * throw an error if extra arguments are given on the command line
258 static void assert_eol(struct uci_context *ctx, char **str)
262 tmp = next_arg(ctx, str, false);
264 ctx->pctx->reason = "too many arguments";
265 ctx->pctx->byte = tmp - ctx->pctx->buf;
266 UCI_THROW(ctx, UCI_ERR_PARSE);
271 * switch to a different config, either triggered by uci_load, or by a
272 * 'package <...>' statement in the import file
274 static void uci_switch_config(struct uci_context *ctx)
276 struct uci_parse_context *pctx;
282 /* add the last config to main config file list */
284 uci_list_add(&ctx->root, &pctx->package->e.list);
286 pctx->package = NULL;
287 pctx->section = NULL;
294 * if an older config under the same name exists, unload it
295 * ignore errors here, e.g. if the config was not found
297 UCI_TRAP_SAVE(ctx, ignore);
298 uci_unload(ctx, name);
299 UCI_TRAP_RESTORE(ctx);
303 pctx->package = uci_alloc_package(ctx, name);
307 * parse the 'package' uci command (next config package)
309 static void uci_parse_package(struct uci_context *ctx, char **str)
313 /* command string null-terminated by strtok */
314 *str += strlen(*str) + 1;
316 name = next_arg(ctx, str, true);
317 assert_eol(ctx, str);
318 ctx->pctx->name = name;
319 uci_switch_config(ctx);
323 * parse the 'config' uci command (open a section)
325 static void uci_parse_config(struct uci_context *ctx, char **str)
330 if (!ctx->pctx->package) {
331 if (!ctx->pctx->name) {
332 ctx->pctx->byte = *str - ctx->pctx->buf;
333 ctx->pctx->reason = "attempting to import a file without a package name";
334 UCI_THROW(ctx, UCI_ERR_PARSE);
336 uci_switch_config(ctx);
339 /* command string null-terminated by strtok */
340 *str += strlen(*str) + 1;
342 type = next_arg(ctx, str, true);
343 name = next_arg(ctx, str, false);
344 assert_eol(ctx, str);
345 ctx->pctx->section = uci_alloc_section(ctx->pctx->package, type, name);
349 * parse the 'option' uci command (open a value)
351 static void uci_parse_option(struct uci_context *ctx, char **str)
356 if (!ctx->pctx->section) {
357 ctx->pctx->byte = *str - ctx->pctx->buf;
358 ctx->pctx->reason = "option command found before the first section";
359 UCI_THROW(ctx, UCI_ERR_PARSE);
361 /* command string null-terminated by strtok */
362 *str += strlen(*str) + 1;
364 name = next_arg(ctx, str, true);
365 value = next_arg(ctx, str, true);
366 assert_eol(ctx, str);
367 uci_alloc_option(ctx->pctx->section, name, value);
372 * parse a complete input line, split up combined commands by ';'
374 static void uci_parse_line(struct uci_context *ctx)
376 struct uci_parse_context *pctx = ctx->pctx;
377 char *word, *brk = NULL;
379 for (word = strtok_r(pctx->buf, ";", &brk);
381 word = strtok_r(NULL, ";", &brk)) {
384 word = strtok_r(word, " \t", &pbrk);
388 if ((word[1] == 0) || !strcmp(word + 1, "ackage"))
389 uci_parse_package(ctx, &word);
392 if ((word[1] == 0) || !strcmp(word + 1, "onfig"))
393 uci_parse_config(ctx, &word);
396 if ((word[1] == 0) || !strcmp(word + 1, "ption"))
397 uci_parse_option(ctx, &word);
400 pctx->reason = "unterminated command";
401 pctx->byte = word - pctx->buf;
402 UCI_THROW(ctx, UCI_ERR_PARSE);
408 /* max number of characters that escaping adds to the string */
409 #define UCI_QUOTE_ESCAPE "'\\'"
412 * escape an uci string for export
414 static char *uci_escape(struct uci_context *ctx, char *str)
420 ctx->bufsz = LINEBUF;
421 ctx->buf = malloc(LINEBUF);
425 p = strchr(str, '\'');
432 if (p + sizeof(UCI_QUOTE_ESCAPE) - str >= ctx->bufsz) {
434 ctx->buf = realloc(ctx->buf, ctx->bufsz);
436 UCI_THROW(ctx, UCI_ERR_MEM);
438 memcpy(&ctx->buf[pos], s, len);
441 strcpy(&ctx->buf[pos], UCI_QUOTE_ESCAPE);
442 pos += sizeof(UCI_QUOTE_ESCAPE);
444 } while ((p = strchr(s, '\'')));
451 * export a single config package to a file stream
453 static void uci_export_package(struct uci_package *p, FILE *stream)
455 struct uci_context *ctx = p->ctx;
456 struct uci_element *s, *o;
458 fprintf(stream, "package '%s'\n", uci_escape(ctx, p->e.name));
459 uci_foreach_element(&p->sections, s) {
460 struct uci_section *sec = uci_to_section(s);
461 fprintf(stream, "\nconfig '%s'", uci_escape(ctx, sec->type));
462 fprintf(stream, " '%s'\n", uci_escape(ctx, sec->e.name));
463 uci_foreach_element(&sec->options, o) {
464 struct uci_option *opt = uci_to_option(o);
465 fprintf(stream, "\toption '%s'", uci_escape(ctx, opt->e.name));
466 fprintf(stream, " '%s'\n", uci_escape(ctx, opt->value));
469 fprintf(stream, "\n");
472 int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package)
474 struct uci_element *e;
477 UCI_ASSERT(ctx, stream != NULL);
480 uci_export_package(package, stream);
484 uci_foreach_element(&ctx->root, e) {
485 uci_export_package(uci_to_package(e), stream);
491 int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package)
493 struct uci_parse_context *pctx;
495 /* make sure no memory from previous parse attempts is leaked */
496 uci_file_cleanup(ctx);
498 pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
503 * If 'name' was supplied, assume that the supplied stream does not contain
504 * the appropriate 'package <name>' string to specify the config name
505 * NB: the config file can still override the package name
510 while (!feof(pctx->file)) {
517 *package = pctx->package;
520 uci_switch_config(ctx);
522 /* no error happened, we can get rid of the parser context now */
523 uci_file_cleanup(ctx);
528 int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package)
536 UCI_ASSERT(ctx, name != NULL);
541 /* absolute/relative path outside of /etc/config */
542 filename = (char *) name;
546 filename = uci_malloc(ctx, strlen(name) + sizeof(UCI_CONFDIR) + 2);
547 sprintf(filename, UCI_CONFDIR "/%s", name);
552 if ((stat(filename, &statbuf) < 0) ||
553 ((statbuf.st_mode & S_IFMT) != S_IFREG)) {
554 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
557 file = fopen(filename, "r");
558 if (filename != name)
562 UCI_THROW(ctx, UCI_ERR_IO);
564 return uci_import(ctx, file, name, package);
568 * This function returns the filename by returning the string
569 * after the last '/' character. By checking for a non-'\0'
570 * character afterwards, directories are ignored (glob marks
571 * those with a trailing '/'
573 static inline char *get_filename(char *path)
577 p = strrchr(path, '/');
584 char **uci_list_configs(struct uci_context *ctx)
591 if (glob(UCI_CONFDIR "/*", GLOB_MARK, NULL, &globbuf) != 0)
594 size = sizeof(char *) * (globbuf.gl_pathc + 1);
595 for(i = 0; i < globbuf.gl_pathc; i++) {
598 p = get_filename(globbuf.gl_pathv[i]);
602 size += strlen(p) + 1;
605 configs = malloc(size);
609 memset(configs, 0, size);
610 buf = (char *) &configs[globbuf.gl_pathc + 1];
611 for(i = 0; i < globbuf.gl_pathc; i++) {
614 p = get_filename(globbuf.gl_pathv[i]);
620 buf += strlen(buf) + 1;