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)
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
72 static void uci_parse_cleanup(struct uci_context *ctx)
74 struct uci_parse_context *pctx;
82 uci_list_del(&pctx->cfg->list);
83 uci_drop_config(pctx->cfg);
94 * move the string pointer forward until a non-whitespace character or
97 static void skip_whitespace(char **str)
99 while (**str && isspace(**str))
103 static inline void addc(char **dest, char **src)
110 static inline void parse_backslash(char **str, char **target)
114 /* FIXME: decode escaped characters? */
119 * parse a double quoted string argument from the command line
121 static void parse_double_quote(struct uci_context *ctx, char **str, char **target)
125 /* skip quote character */
128 while ((c = **str)) {
131 parse_backslash(str, target);
142 ctx->pctx->reason = "unterminated \"";
143 ctx->pctx->byte = *str - ctx->pctx->buf;
144 UCI_THROW(ctx, UCI_ERR_PARSE);
148 * parse a single quoted string argument from the command line
150 static void parse_single_quote(struct uci_context *ctx, char **str, char **target)
153 /* skip quote character */
156 while ((c = **str)) {
166 ctx->pctx->reason = "unterminated '";
167 ctx->pctx->byte = *str - ctx->pctx->buf;
168 UCI_THROW(ctx, UCI_ERR_PARSE);
172 * parse a string from the command line and detect the quoting style
174 static void parse_str(struct uci_context *ctx, char **str, char **target)
179 parse_backslash(str, target);
182 parse_single_quote(ctx, str, target);
185 parse_double_quote(ctx, str, target);
193 } while (**str && !isspace(**str));
197 * if the string was unquoted and we've stopped at a whitespace
198 * character, skip to the next one, because the whitespace will
199 * be overwritten by a null byte here
204 /* terminate the parsed string */
209 * extract the next argument from the command line
211 static char *next_arg(struct uci_context *ctx, char **str, bool required)
217 skip_whitespace(str);
218 parse_str(ctx, str, &ptr);
219 if (required && !*val) {
220 ctx->pctx->reason = "insufficient arguments";
221 ctx->pctx->byte = *str - ctx->pctx->buf;
222 UCI_THROW(ctx, UCI_ERR_PARSE);
225 return uci_strdup(ctx, val);
229 * verify that the end of the line or command is reached.
230 * throw an error if extra arguments are given on the command line
232 static void assert_eol(struct uci_context *ctx, char **str)
236 tmp = next_arg(ctx, str, false);
238 ctx->pctx->reason = "too many arguments";
239 ctx->pctx->byte = tmp - ctx->pctx->buf;
240 UCI_THROW(ctx, UCI_ERR_PARSE);
245 * switch to a different config, either triggered by uci_load, or by a
246 * 'package <...>' statement in the import file
248 static void uci_switch_config(struct uci_context *ctx)
250 struct uci_parse_context *pctx;
256 /* add the last config to main config file list */
258 uci_list_add(&ctx->root, &pctx->cfg->list);
261 pctx->section = NULL;
268 * if an older config under the same name exists, unload it
269 * ignore errors here, e.g. if the config was not found
271 UCI_TRAP_SAVE(ctx, ignore);
272 uci_unload(ctx, name);
273 UCI_TRAP_RESTORE(ctx);
277 pctx->cfg = uci_alloc_config(ctx, name);
281 * parse the 'package' uci command (next config package)
283 static void uci_parse_package(struct uci_context *ctx, char **str)
287 /* command string null-terminated by strtok */
288 *str += strlen(*str) + 1;
290 UCI_TRAP_SAVE(ctx, error);
291 name = next_arg(ctx, str, true);
292 assert_eol(ctx, str);
293 ctx->pctx->name = name;
294 uci_switch_config(ctx);
295 UCI_TRAP_RESTORE(ctx);
301 UCI_THROW(ctx, ctx->errno);
305 * parse the 'config' uci command (open a section)
307 static void uci_parse_config(struct uci_context *ctx, char **str)
312 if (!ctx->pctx->cfg) {
313 if (!ctx->pctx->name) {
314 ctx->pctx->byte = *str - ctx->pctx->buf;
315 ctx->pctx->reason = "attempting to import a file without a package name";
316 UCI_THROW(ctx, UCI_ERR_PARSE);
318 uci_switch_config(ctx);
321 /* command string null-terminated by strtok */
322 *str += strlen(*str) + 1;
324 UCI_TRAP_SAVE(ctx, error);
325 type = next_arg(ctx, str, true);
326 name = next_arg(ctx, str, false);
327 assert_eol(ctx, str);
328 ctx->pctx->section = uci_add_section(ctx->pctx->cfg, type, name);
329 UCI_TRAP_RESTORE(ctx);
337 UCI_THROW(ctx, ctx->errno);
341 * parse the 'option' uci command (open a value)
343 static void uci_parse_option(struct uci_context *ctx, char **str)
348 if (!ctx->pctx->section) {
349 ctx->pctx->byte = *str - ctx->pctx->buf;
350 ctx->pctx->reason = "option command found before the first section";
351 UCI_THROW(ctx, UCI_ERR_PARSE);
353 /* command string null-terminated by strtok */
354 *str += strlen(*str) + 1;
356 UCI_TRAP_SAVE(ctx, error);
357 name = next_arg(ctx, str, true);
358 value = next_arg(ctx, str, true);
359 assert_eol(ctx, str);
360 uci_add_option(ctx->pctx->section, name, value);
361 UCI_TRAP_RESTORE(ctx);
369 UCI_THROW(ctx, ctx->errno);
374 * parse a complete input line, split up combined commands by ';'
376 static void uci_parse_line(struct uci_context *ctx)
378 struct uci_parse_context *pctx = ctx->pctx;
381 for (word = strtok_r(pctx->buf, ";", &brk);
383 word = strtok_r(NULL, ";", &brk)) {
386 word = strtok_r(word, " \t", &pbrk);
390 if ((word[1] == 0) || !strcmp(word + 1, "ackage"))
391 uci_parse_package(ctx, &word);
394 if ((word[1] == 0) || !strcmp(word + 1, "onfig"))
395 uci_parse_config(ctx, &word);
398 if ((word[1] == 0) || !strcmp(word + 1, "ption"))
399 uci_parse_option(ctx, &word);
402 pctx->reason = "unterminated command";
403 pctx->byte = word - pctx->buf;
404 UCI_THROW(ctx, UCI_ERR_PARSE);
410 int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_config **cfg)
412 struct uci_parse_context *pctx;
414 /* make sure no memory from previous parse attempts is leaked */
415 uci_parse_cleanup(ctx);
417 pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
422 * If 'name' was supplied, assume that the supplied stream does not contain
423 * the appropriate 'package <name>' string to specify the config name
424 * NB: the config file can still override the package name
429 while (!feof(pctx->file)) {
439 uci_switch_config(ctx);
441 /* no error happened, we can get rid of the parser context now */
442 uci_parse_cleanup(ctx);
447 int uci_load(struct uci_context *ctx, const char *name, struct uci_config **cfg)
455 UCI_ASSERT(ctx, name != NULL);
463 /* absolute/relative path outside of /etc/config */
464 filename = (char *) name;
468 filename = uci_malloc(ctx, strlen(name) + sizeof(UCI_CONFDIR) + 2);
469 sprintf(filename, UCI_CONFDIR "/%s", name);
474 if ((stat(filename, &statbuf) < 0) ||
475 ((statbuf.st_mode & S_IFMT) != S_IFREG)) {
476 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
479 file = fopen(filename, "r");
480 if (filename != name)
484 UCI_THROW(ctx, UCI_ERR_IO);
486 return uci_import(ctx, file, name, cfg);