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
22 #define LINEBUF_MAX 4096
25 * Fetch a new line from the input stream and resize buffer if necessary
27 static void uci_getln(struct uci_context *ctx)
29 struct uci_parse_context *pctx = ctx->pctx;
33 if (pctx->buf == NULL) {
34 pctx->buf = uci_malloc(ctx, LINEBUF);
35 pctx->bufsz = LINEBUF;
43 p = fgets(p, pctx->bufsz - ofs, pctx->file);
48 if (pctx->buf[ofs - 1] == '\n') {
50 pctx->buf[ofs - 1] = 0;
54 if (pctx->bufsz > LINEBUF_MAX/2) {
55 pctx->byte = LINEBUF_MAX;
56 UCI_THROW(ctx, UCI_ERR_PARSE);
60 pctx->buf = uci_realloc(ctx, pctx->buf, pctx->bufsz);
65 * Clean up all extra memory used by the parser
67 static void uci_parse_cleanup(struct uci_context *ctx)
69 struct uci_parse_context *pctx;
76 uci_list_del(&pctx->cfg->list);
77 uci_drop_file(pctx->cfg);
88 * move the string pointer forward until a non-whitespace character or
91 static void skip_whitespace(char **str)
93 while (**str && isspace(**str))
98 * parse a double quoted string argument from the command line
100 static char *parse_double_quote(char **str)
108 /* skip escaped characters */
114 /* check for the end of the quoted string */
127 * parse a single quoted string argument from the command line
129 static char *parse_single_quote(char **str)
131 /* TODO: implement */
136 * extract the next word from the command line (unquoted argument)
138 static char *parse_unquoted(char **str)
144 while (**str && !isspace(**str))
156 * extract the next argument from the command line
158 static char *next_arg(struct uci_context *ctx, char **str, bool required)
162 skip_whitespace(str);
165 val = parse_double_quote(str);
167 val = parse_single_quote(str);
171 val = parse_unquoted(str);
174 if (required && !val) {
175 ctx->pctx->byte = *str - ctx->pctx->buf;
176 UCI_THROW(ctx, UCI_ERR_PARSE);
183 * verify that the end of the line or command is reached.
184 * throw an error if extra arguments are given on the command line
186 static void assert_eol(struct uci_context *ctx, char **str)
190 tmp = next_arg(ctx, str, false);
192 ctx->pctx->byte = tmp - ctx->pctx->buf;
193 UCI_THROW(ctx, UCI_ERR_PARSE);
198 * parse the 'config' uci command (open a section)
200 static void uci_parse_config(struct uci_context *ctx, char **str)
204 *str += strlen(*str) + 1;
207 ctx->pctx->byte = *str - ctx->pctx->buf;
208 UCI_THROW(ctx, UCI_ERR_PARSE);
211 type = next_arg(ctx, str, true);
212 name = next_arg(ctx, str, false);
213 assert_eol(ctx, str);
215 DPRINTF("Section<%s>: %s\n", type, name);
219 * parse the 'option' uci command (open a value)
221 static void uci_parse_option(struct uci_context *ctx, char **str)
225 *str += strlen(*str) + 1;
227 name = next_arg(ctx, str, true);
228 value = next_arg(ctx, str, true);
229 assert_eol(ctx, str);
231 DPRINTF("\tOption: %s=\"%s\"\n", name, value);
235 * parse a complete input line, split up combined commands by ';'
237 static void uci_parse_line(struct uci_context *ctx)
239 struct uci_parse_context *pctx = ctx->pctx;
242 for (word = strtok_r(pctx->buf, ";", &brk);
244 word = strtok_r(NULL, ";", &brk)) {
247 word = strtok_r(word, " \t", &pbrk);
251 if ((word[1] == 0) || !strcmp(word + 1, "onfig"))
252 uci_parse_config(ctx, &word);
255 if ((word[1] == 0) || !strcmp(word + 1, "ption"))
256 uci_parse_option(ctx, &word);
259 pctx->byte = word - pctx->buf;
260 UCI_THROW(ctx, UCI_ERR_PARSE);
266 int uci_parse(struct uci_context *ctx, const char *name)
268 struct uci_parse_context *pctx;
271 UCI_ASSERT(ctx, name != NULL);
273 /* make sure no memory from previous parse attempts is leaked */
274 uci_parse_cleanup(ctx);
276 pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
279 /* TODO: use /etc/config/ */
280 pctx->file = fopen(name, "r");
282 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
284 pctx->cfg = uci_alloc_file(ctx, name);
286 while (!feof(pctx->file)) {
292 /* add to main config file list */
293 uci_list_add(&ctx->root, &pctx->cfg->list);
296 /* if no error happened, we can get rid of the parser context now */
297 uci_parse_cleanup(ctx);