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
;
84 * move the string pointer forward until a non-whitespace character or
87 static void skip_whitespace(char **str
)
89 while (**str
&& isspace(**str
))
94 * parse a double quoted string argument from the command line
96 static char *parse_double_quote(char **str
)
104 /* skip escaped characters */
110 /* check for the end of the quoted string */
123 * parse a single quoted string argument from the command line
125 static char *parse_single_quote(char **str
)
127 /* TODO: implement */
132 * extract the next word from the command line (unquoted argument)
134 static char *parse_unquoted(char **str
)
140 while (**str
&& !isspace(**str
))
152 * extract the next argument from the command line
154 static char *next_arg(struct uci_context
*ctx
, char **str
, bool required
)
157 skip_whitespace(str
);
161 val
= parse_double_quote(str
);
163 val
= parse_single_quote(str
);
167 val
= parse_unquoted(str
);
170 if (required
&& !val
) {
171 ctx
->pctx
->byte
= *str
- ctx
->pctx
->buf
;
172 UCI_THROW(ctx
, UCI_ERR_PARSE
);
179 * verify that the end of the line or command is reached.
180 * throw an error if extra arguments are given on the command line
182 static void assert_eol(struct uci_context
*ctx
, char **str
)
186 tmp
= next_arg(ctx
, str
, false);
188 ctx
->pctx
->byte
= tmp
- ctx
->pctx
->buf
;
189 UCI_THROW(ctx
, UCI_ERR_PARSE
);
194 * parse the 'config' uci command (open a section)
196 static void uci_parse_config(struct uci_context
*ctx
, char **str
)
200 *str
+= strlen(*str
) + 1;
203 ctx
->pctx
->byte
= *str
- ctx
->pctx
->buf
;
204 UCI_THROW(ctx
, UCI_ERR_PARSE
);
207 type
= next_arg(ctx
, str
, true);
208 name
= next_arg(ctx
, str
, false);
209 assert_eol(ctx
, str
);
211 DPRINTF("Section<%s>: %s\n", type
, name
);
215 * parse the 'option' uci command (open a value)
217 static void uci_parse_option(struct uci_context
*ctx
, char **str
)
221 *str
+= strlen(*str
) + 1;
223 name
= next_arg(ctx
, str
, true);
224 value
= next_arg(ctx
, str
, true);
225 assert_eol(ctx
, str
);
227 DPRINTF("\tOption: %s=\"%s\"\n", name
, value
);
231 * parse a complete input line, split up combined commands by ';'
233 static void uci_parse_line(struct uci_context
*ctx
)
235 struct uci_parse_context
*pctx
= ctx
->pctx
;
238 for (word
= strtok_r(pctx
->buf
, ";", &brk
);
240 word
= strtok_r(NULL
, ";", &brk
)) {
243 word
= strtok_r(word
, " \t", &pbrk
);
247 if ((word
[1] == 0) || !strcmp(word
+ 1, "onfig"))
248 uci_parse_config(ctx
, &word
);
251 if ((word
[1] == 0) || !strcmp(word
+ 1, "ption"))
252 uci_parse_option(ctx
, &word
);
255 pctx
->byte
= word
- pctx
->buf
;
256 UCI_THROW(ctx
, UCI_ERR_PARSE
);
262 int uci_parse(struct uci_context
*ctx
, const char *name
)
264 struct uci_parse_context
*pctx
;
267 UCI_ASSERT(ctx
, name
!= NULL
);
269 /* make sure no memory from previous parse attempts is leaked */
270 uci_parse_cleanup(ctx
);
272 pctx
= (struct uci_parse_context
*) uci_malloc(ctx
, sizeof(struct uci_parse_context
));
275 /* TODO: use /etc/config/ */
276 pctx
->file
= fopen(name
, "r");
278 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
280 while (!feof(pctx
->file
)) {
286 /* if no error happened, we can get rid of the parser context now */
287 uci_parse_cleanup(ctx
);