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;
87 uci_list_del(&pctx->cfg->list);
88 uci_drop_config(pctx->cfg);
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);
251 return uci_strdup(ctx, val);
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->cfg->list);
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->cfg = uci_alloc_config(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 UCI_TRAP_SAVE(ctx, error);
317 name = next_arg(ctx, str, true);
318 assert_eol(ctx, str);
319 ctx->pctx->name = name;
320 uci_switch_config(ctx);
321 UCI_TRAP_RESTORE(ctx);
327 UCI_THROW(ctx, ctx->errno);
331 * parse the 'config' uci command (open a section)
333 static void uci_parse_config(struct uci_context *ctx, char **str)
338 if (!ctx->pctx->cfg) {
339 if (!ctx->pctx->name) {
340 ctx->pctx->byte = *str - ctx->pctx->buf;
341 ctx->pctx->reason = "attempting to import a file without a package name";
342 UCI_THROW(ctx, UCI_ERR_PARSE);
344 uci_switch_config(ctx);
347 /* command string null-terminated by strtok */
348 *str += strlen(*str) + 1;
350 UCI_TRAP_SAVE(ctx, error);
351 type = next_arg(ctx, str, true);
352 name = next_arg(ctx, str, false);
353 assert_eol(ctx, str);
354 ctx->pctx->section = uci_add_section(ctx->pctx->cfg, type, name);
355 UCI_TRAP_RESTORE(ctx);
363 UCI_THROW(ctx, ctx->errno);
367 * parse the 'option' uci command (open a value)
369 static void uci_parse_option(struct uci_context *ctx, char **str)
374 if (!ctx->pctx->section) {
375 ctx->pctx->byte = *str - ctx->pctx->buf;
376 ctx->pctx->reason = "option command found before the first section";
377 UCI_THROW(ctx, UCI_ERR_PARSE);
379 /* command string null-terminated by strtok */
380 *str += strlen(*str) + 1;
382 UCI_TRAP_SAVE(ctx, error);
383 name = next_arg(ctx, str, true);
384 value = next_arg(ctx, str, true);
385 assert_eol(ctx, str);
386 uci_add_option(ctx->pctx->section, name, value);
387 UCI_TRAP_RESTORE(ctx);
395 UCI_THROW(ctx, ctx->errno);
400 * parse a complete input line, split up combined commands by ';'
402 static void uci_parse_line(struct uci_context *ctx)
404 struct uci_parse_context *pctx = ctx->pctx;
407 for (word = strtok_r(pctx->buf, ";", &brk);
409 word = strtok_r(NULL, ";", &brk)) {
412 word = strtok_r(word, " \t", &pbrk);
416 if ((word[1] == 0) || !strcmp(word + 1, "ackage"))
417 uci_parse_package(ctx, &word);
420 if ((word[1] == 0) || !strcmp(word + 1, "onfig"))
421 uci_parse_config(ctx, &word);
424 if ((word[1] == 0) || !strcmp(word + 1, "ption"))
425 uci_parse_option(ctx, &word);
428 pctx->reason = "unterminated command";
429 pctx->byte = word - pctx->buf;
430 UCI_THROW(ctx, UCI_ERR_PARSE);
436 /* max number of characters that escaping adds to the string */
437 #define UCI_QUOTE_ESCAPE "'\\'"
440 * escape an uci string for export
442 static char *uci_escape(struct uci_context *ctx, char *str)
448 ctx->bufsz = LINEBUF;
449 ctx->buf = malloc(LINEBUF);
453 p = strchr(str, '\'');
460 if (p + sizeof(UCI_QUOTE_ESCAPE) - str >= ctx->bufsz) {
462 ctx->buf = realloc(ctx->buf, ctx->bufsz);
464 UCI_THROW(ctx, UCI_ERR_MEM);
466 memcpy(&ctx->buf[pos], s, len);
469 strcpy(&ctx->buf[pos], UCI_QUOTE_ESCAPE);
470 pos += sizeof(UCI_QUOTE_ESCAPE);
472 } while ((p = strchr(s, '\'')));
479 * export a single config package to a file stream
481 static void uci_export_config(struct uci_config *cfg, FILE *stream)
483 struct uci_context *ctx = cfg->ctx;
484 struct uci_section *s;
485 struct uci_option *o;
487 fprintf(stream, "package '%s'\n", uci_escape(ctx, cfg->name));
488 uci_foreach_entry(section, &cfg->sections, s) {
489 fprintf(stream, "\nconfig '%s'", uci_escape(ctx, s->type));
490 fprintf(stream, " '%s'\n", uci_escape(ctx, s->name));
491 uci_foreach_entry(option, &s->options, o) {
492 fprintf(stream, "\toption '%s'", uci_escape(ctx, o->name));
493 fprintf(stream, " '%s'\n", uci_escape(ctx, o->value));
496 fprintf(stream, "\n");
499 int uci_export(struct uci_context *ctx, FILE *stream, struct uci_config *cfg)
502 UCI_ASSERT(ctx, stream != NULL);
505 uci_export_config(cfg, stream);
509 uci_foreach_entry(config, &ctx->root, cfg) {
510 uci_export_config(cfg, stream);
516 int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_config **cfg)
518 struct uci_parse_context *pctx;
520 /* make sure no memory from previous parse attempts is leaked */
521 uci_file_cleanup(ctx);
523 pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
528 * If 'name' was supplied, assume that the supplied stream does not contain
529 * the appropriate 'package <name>' string to specify the config name
530 * NB: the config file can still override the package name
535 while (!feof(pctx->file)) {
545 uci_switch_config(ctx);
547 /* no error happened, we can get rid of the parser context now */
548 uci_file_cleanup(ctx);
553 int uci_load(struct uci_context *ctx, const char *name, struct uci_config **cfg)
561 UCI_ASSERT(ctx, name != NULL);
569 /* absolute/relative path outside of /etc/config */
570 filename = (char *) name;
574 filename = uci_malloc(ctx, strlen(name) + sizeof(UCI_CONFDIR) + 2);
575 sprintf(filename, UCI_CONFDIR "/%s", name);
580 if ((stat(filename, &statbuf) < 0) ||
581 ((statbuf.st_mode & S_IFMT) != S_IFREG)) {
582 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
585 file = fopen(filename, "r");
586 if (filename != name)
590 UCI_THROW(ctx, UCI_ERR_IO);
592 return uci_import(ctx, file, name, cfg);