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;
83 static void skip_whitespace(char **str)
85 while (**str && isspace(**str))
89 static char *parse_double_quote(char **str)
97 /* skip escaped characters */
103 /* check for the end of the quoted string */
115 static char *parse_single_quote(char **str)
117 /* TODO: implement */
121 static char *parse_unquoted(char **str)
127 while (**str && !isspace(**str))
135 static char *next_arg(char **str)
137 skip_whitespace(str);
141 return parse_double_quote(str);
143 return parse_single_quote(str);
147 return parse_unquoted(str);
151 static void uci_parse_config(struct uci_context *ctx, char **str)
155 *str += strlen(*str) + 1;
158 ctx->pctx->byte = *str - ctx->pctx->buf;
159 UCI_THROW(ctx, UCI_ERR_PARSE);
162 type = next_arg(str);
164 ctx->pctx->byte = *str - ctx->pctx->buf;
165 UCI_THROW(ctx, UCI_ERR_PARSE);
168 name = next_arg(str);
169 DPRINTF("Section<%s>: %s\n", type, name);
172 static void uci_parse_option(struct uci_context *ctx, char **str)
176 *str += strlen(*str) + 1;
178 name = next_arg(str);
180 ctx->pctx->byte = *str - ctx->pctx->buf;
181 UCI_THROW(ctx, UCI_ERR_PARSE);
184 value = next_arg(str);
186 ctx->pctx->byte = *str - ctx->pctx->buf;
187 UCI_THROW(ctx, UCI_ERR_PARSE);
190 DPRINTF("\tOption: %s=\"%s\"\n", name, value);
193 static void uci_parse_line(struct uci_context *ctx)
195 struct uci_parse_context *pctx = ctx->pctx;
198 for (word = strtok_r(pctx->buf, ";", &brk);
200 word = strtok_r(NULL, ";", &brk)) {
203 word = strtok_r(word, " \t", &pbrk);
207 if ((word[1] == 0) || !strcmp(word + 1, "onfig"))
208 uci_parse_config(ctx, &word);
211 if ((word[1] == 0) || !strcmp(word + 1, "ption"))
212 uci_parse_option(ctx, &word);
215 pctx->byte = word - pctx->buf;
216 UCI_THROW(ctx, UCI_ERR_PARSE);
222 int uci_parse(struct uci_context *ctx, const char *name)
224 struct uci_parse_context *pctx;
227 UCI_ASSERT(ctx, name != NULL);
229 /* make sure no memory from previous parse attempts is leaked */
230 uci_parse_cleanup(ctx);
232 pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
235 /* TODO: use /etc/config/ */
236 pctx->file = fopen(name, "r");
238 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
240 while (!feof(pctx->file)) {
246 /* if no error happened, we can get rid of the parser context now */
247 uci_parse_cleanup(ctx);