31756d9a01ec5c27667b8169634390cb91694f8b
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
;
81 uci_list_del(&pctx
->cfg
->list
);
82 uci_drop_file(pctx
->cfg
);
93 * move the string pointer forward until a non-whitespace character or
96 static void skip_whitespace(char **str
)
98 while (**str
&& isspace(**str
))
102 static inline void addc(char **dest
, char **src
)
109 static inline void parse_backslash(char **str
, char **target
)
113 /* FIXME: decode escaped characters? */
118 * parse a double quoted string argument from the command line
120 static void parse_double_quote(struct uci_context
*ctx
, char **str
, char **target
)
124 /* skip quote character */
127 while ((c
= **str
)) {
130 parse_backslash(str
, target
);
141 ctx
->pctx
->reason
= "unterminated \"";
142 ctx
->pctx
->byte
= *str
- ctx
->pctx
->buf
;
143 UCI_THROW(ctx
, UCI_ERR_PARSE
);
147 * parse a single quoted string argument from the command line
149 static void parse_single_quote(struct uci_context
*ctx
, char **str
, char **target
)
152 /* skip quote character */
155 while ((c
= **str
)) {
165 ctx
->pctx
->reason
= "unterminated '";
166 ctx
->pctx
->byte
= *str
- ctx
->pctx
->buf
;
167 UCI_THROW(ctx
, UCI_ERR_PARSE
);
171 * parse a string from the command line and detect the quoting style
173 static void parse_str(struct uci_context
*ctx
, char **str
, char **target
)
178 parse_backslash(str
, target
);
181 parse_single_quote(ctx
, str
, target
);
184 parse_double_quote(ctx
, str
, target
);
192 } while (**str
&& !isspace(**str
));
196 * if the string was unquoted and we've stopped at a whitespace
197 * character, skip to the next one, because the whitespace will
198 * be overwritten by a null byte here
203 /* terminate the parsed string */
208 * extract the next argument from the command line
210 static char *next_arg(struct uci_context
*ctx
, char **str
, bool required
)
216 skip_whitespace(str
);
217 parse_str(ctx
, str
, &ptr
);
218 if (required
&& !*val
) {
219 ctx
->pctx
->reason
= "insufficient arguments";
220 ctx
->pctx
->byte
= *str
- ctx
->pctx
->buf
;
221 UCI_THROW(ctx
, UCI_ERR_PARSE
);
224 return uci_strdup(ctx
, val
);
228 * verify that the end of the line or command is reached.
229 * throw an error if extra arguments are given on the command line
231 static void assert_eol(struct uci_context
*ctx
, char **str
)
235 tmp
= next_arg(ctx
, str
, false);
237 ctx
->pctx
->reason
= "too many arguments";
238 ctx
->pctx
->byte
= tmp
- ctx
->pctx
->buf
;
239 UCI_THROW(ctx
, UCI_ERR_PARSE
);
244 * parse the 'config' uci command (open a section)
246 static void uci_parse_config(struct uci_context
*ctx
, char **str
)
250 /* command string null-terminated by strtok */
251 *str
+= strlen(*str
) + 1;
253 type
= next_arg(ctx
, str
, true);
254 name
= next_arg(ctx
, str
, false);
255 assert_eol(ctx
, str
);
257 DPRINTF("Section<%s>: %s\n", type
, name
);
261 * parse the 'option' uci command (open a value)
263 static void uci_parse_option(struct uci_context
*ctx
, char **str
)
267 /* command string null-terminated by strtok */
268 *str
+= strlen(*str
) + 1;
270 name
= next_arg(ctx
, str
, true);
271 value
= next_arg(ctx
, str
, true);
272 assert_eol(ctx
, str
);
274 DPRINTF("\tOption: %s=\"%s\"\n", name
, value
);
278 * parse a complete input line, split up combined commands by ';'
280 static void uci_parse_line(struct uci_context
*ctx
)
282 struct uci_parse_context
*pctx
= ctx
->pctx
;
285 for (word
= strtok_r(pctx
->buf
, ";", &brk
);
287 word
= strtok_r(NULL
, ";", &brk
)) {
290 word
= strtok_r(word
, " \t", &pbrk
);
294 if ((word
[1] == 0) || !strcmp(word
+ 1, "onfig"))
295 uci_parse_config(ctx
, &word
);
298 if ((word
[1] == 0) || !strcmp(word
+ 1, "ption"))
299 uci_parse_option(ctx
, &word
);
302 pctx
->reason
= "unterminated command";
303 pctx
->byte
= word
- pctx
->buf
;
304 UCI_THROW(ctx
, UCI_ERR_PARSE
);
310 int uci_load(struct uci_context
*ctx
, const char *name
)
312 struct uci_parse_context
*pctx
;
318 UCI_ASSERT(ctx
, name
!= NULL
);
320 /* make sure no memory from previous parse attempts is leaked */
321 uci_parse_cleanup(ctx
);
323 pctx
= (struct uci_parse_context
*) uci_malloc(ctx
, sizeof(struct uci_parse_context
));
329 /* absolute/relative path outside of /etc/config */
330 filename
= (char *) name
;
334 filename
= uci_malloc(ctx
, strlen(name
) + sizeof(UCI_CONFDIR
) + 2);
335 sprintf(filename
, UCI_CONFDIR
"/%s", name
);
340 if ((stat(filename
, &statbuf
) < 0) ||
341 ((statbuf
.st_mode
& S_IFMT
) != S_IFREG
))
342 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
344 pctx
->file
= fopen(filename
, "r");
345 if (filename
!= name
)
349 UCI_THROW(ctx
, UCI_ERR_IO
);
351 pctx
->cfg
= uci_alloc_file(ctx
, name
);
353 while (!feof(pctx
->file
)) {
359 /* add to main config file list */
360 uci_list_add(&ctx
->root
, &pctx
->cfg
->list
);
363 /* no error happened, we can get rid of the parser context now */
364 uci_parse_cleanup(ctx
);