84284627d26e31d70f2a04bbf189ccc2a2602406
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
, int offset
)
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 * parse a character escaped by '\'
95 * returns true if the escaped character is to be parsed
96 * returns false if the escaped character is to be ignored
98 static inline bool parse_backslash(struct uci_context
*ctx
, char **str
)
103 /* undecoded backslash at the end of line, fetch the next line */
106 uci_getln(ctx
, *str
- ctx
->pctx
->buf
);
110 /* FIXME: decode escaped char, necessary? */
115 * move the string pointer forward until a non-whitespace character or
118 static void skip_whitespace(struct uci_context
*ctx
, char **str
)
121 while (**str
&& isspace(**str
))
125 if (!parse_backslash(ctx
, str
))
130 static inline void addc(char **dest
, char **src
)
138 * parse a double quoted string argument from the command line
140 static void parse_double_quote(struct uci_context
*ctx
, char **str
, char **target
)
144 /* skip quote character */
147 while ((c
= **str
)) {
154 if (!parse_backslash(ctx
, str
))
162 ctx
->pctx
->reason
= "unterminated \"";
163 ctx
->pctx
->byte
= *str
- ctx
->pctx
->buf
;
164 UCI_THROW(ctx
, UCI_ERR_PARSE
);
168 * parse a single quoted string argument from the command line
170 static void parse_single_quote(struct uci_context
*ctx
, char **str
, char **target
)
173 /* skip quote character */
176 while ((c
= **str
)) {
186 ctx
->pctx
->reason
= "unterminated '";
187 ctx
->pctx
->byte
= *str
- ctx
->pctx
->buf
;
188 UCI_THROW(ctx
, UCI_ERR_PARSE
);
192 * parse a string from the command line and detect the quoting style
194 static void parse_str(struct uci_context
*ctx
, char **str
, char **target
)
199 parse_single_quote(ctx
, str
, target
);
202 parse_double_quote(ctx
, str
, target
);
207 if (!parse_backslash(ctx
, str
))
214 } while (**str
&& !isspace(**str
));
218 * if the string was unquoted and we've stopped at a whitespace
219 * character, skip to the next one, because the whitespace will
220 * be overwritten by a null byte here
225 /* terminate the parsed string */
230 * extract the next argument from the command line
232 static char *next_arg(struct uci_context
*ctx
, char **str
, bool required
)
238 skip_whitespace(ctx
, str
);
239 parse_str(ctx
, str
, &ptr
);
240 if (required
&& !*val
) {
241 ctx
->pctx
->reason
= "insufficient arguments";
242 ctx
->pctx
->byte
= *str
- ctx
->pctx
->buf
;
243 UCI_THROW(ctx
, UCI_ERR_PARSE
);
246 return uci_strdup(ctx
, val
);
250 * verify that the end of the line or command is reached.
251 * throw an error if extra arguments are given on the command line
253 static void assert_eol(struct uci_context
*ctx
, char **str
)
257 tmp
= next_arg(ctx
, str
, false);
259 ctx
->pctx
->reason
= "too many arguments";
260 ctx
->pctx
->byte
= tmp
- ctx
->pctx
->buf
;
261 UCI_THROW(ctx
, UCI_ERR_PARSE
);
266 * switch to a different config, either triggered by uci_load, or by a
267 * 'package <...>' statement in the import file
269 static void uci_switch_config(struct uci_context
*ctx
)
271 struct uci_parse_context
*pctx
;
277 /* add the last config to main config file list */
279 uci_list_add(&ctx
->root
, &pctx
->cfg
->list
);
282 pctx
->section
= NULL
;
289 * if an older config under the same name exists, unload it
290 * ignore errors here, e.g. if the config was not found
292 UCI_TRAP_SAVE(ctx
, ignore
);
293 uci_unload(ctx
, name
);
294 UCI_TRAP_RESTORE(ctx
);
298 pctx
->cfg
= uci_alloc_config(ctx
, name
);
302 * parse the 'package' uci command (next config package)
304 static void uci_parse_package(struct uci_context
*ctx
, char **str
)
308 /* command string null-terminated by strtok */
309 *str
+= strlen(*str
) + 1;
311 UCI_TRAP_SAVE(ctx
, error
);
312 name
= next_arg(ctx
, str
, true);
313 assert_eol(ctx
, str
);
314 ctx
->pctx
->name
= name
;
315 uci_switch_config(ctx
);
316 UCI_TRAP_RESTORE(ctx
);
322 UCI_THROW(ctx
, ctx
->errno
);
326 * parse the 'config' uci command (open a section)
328 static void uci_parse_config(struct uci_context
*ctx
, char **str
)
333 if (!ctx
->pctx
->cfg
) {
334 if (!ctx
->pctx
->name
) {
335 ctx
->pctx
->byte
= *str
- ctx
->pctx
->buf
;
336 ctx
->pctx
->reason
= "attempting to import a file without a package name";
337 UCI_THROW(ctx
, UCI_ERR_PARSE
);
339 uci_switch_config(ctx
);
342 /* command string null-terminated by strtok */
343 *str
+= strlen(*str
) + 1;
345 UCI_TRAP_SAVE(ctx
, error
);
346 type
= next_arg(ctx
, str
, true);
347 name
= next_arg(ctx
, str
, false);
348 assert_eol(ctx
, str
);
349 ctx
->pctx
->section
= uci_add_section(ctx
->pctx
->cfg
, type
, name
);
350 UCI_TRAP_RESTORE(ctx
);
358 UCI_THROW(ctx
, ctx
->errno
);
362 * parse the 'option' uci command (open a value)
364 static void uci_parse_option(struct uci_context
*ctx
, char **str
)
369 if (!ctx
->pctx
->section
) {
370 ctx
->pctx
->byte
= *str
- ctx
->pctx
->buf
;
371 ctx
->pctx
->reason
= "option command found before the first section";
372 UCI_THROW(ctx
, UCI_ERR_PARSE
);
374 /* command string null-terminated by strtok */
375 *str
+= strlen(*str
) + 1;
377 UCI_TRAP_SAVE(ctx
, error
);
378 name
= next_arg(ctx
, str
, true);
379 value
= next_arg(ctx
, str
, true);
380 assert_eol(ctx
, str
);
381 uci_add_option(ctx
->pctx
->section
, name
, value
);
382 UCI_TRAP_RESTORE(ctx
);
390 UCI_THROW(ctx
, ctx
->errno
);
395 * parse a complete input line, split up combined commands by ';'
397 static void uci_parse_line(struct uci_context
*ctx
)
399 struct uci_parse_context
*pctx
= ctx
->pctx
;
402 for (word
= strtok_r(pctx
->buf
, ";", &brk
);
404 word
= strtok_r(NULL
, ";", &brk
)) {
407 word
= strtok_r(word
, " \t", &pbrk
);
411 if ((word
[1] == 0) || !strcmp(word
+ 1, "ackage"))
412 uci_parse_package(ctx
, &word
);
415 if ((word
[1] == 0) || !strcmp(word
+ 1, "onfig"))
416 uci_parse_config(ctx
, &word
);
419 if ((word
[1] == 0) || !strcmp(word
+ 1, "ption"))
420 uci_parse_option(ctx
, &word
);
423 pctx
->reason
= "unterminated command";
424 pctx
->byte
= word
- pctx
->buf
;
425 UCI_THROW(ctx
, UCI_ERR_PARSE
);
431 int uci_import(struct uci_context
*ctx
, FILE *stream
, const char *name
, struct uci_config
**cfg
)
433 struct uci_parse_context
*pctx
;
435 /* make sure no memory from previous parse attempts is leaked */
436 uci_parse_cleanup(ctx
);
438 pctx
= (struct uci_parse_context
*) uci_malloc(ctx
, sizeof(struct uci_parse_context
));
443 * If 'name' was supplied, assume that the supplied stream does not contain
444 * the appropriate 'package <name>' string to specify the config name
445 * NB: the config file can still override the package name
450 while (!feof(pctx
->file
)) {
460 uci_switch_config(ctx
);
462 /* no error happened, we can get rid of the parser context now */
463 uci_parse_cleanup(ctx
);
468 int uci_load(struct uci_context
*ctx
, const char *name
, struct uci_config
**cfg
)
476 UCI_ASSERT(ctx
, name
!= NULL
);
484 /* absolute/relative path outside of /etc/config */
485 filename
= (char *) name
;
489 filename
= uci_malloc(ctx
, strlen(name
) + sizeof(UCI_CONFDIR
) + 2);
490 sprintf(filename
, UCI_CONFDIR
"/%s", name
);
495 if ((stat(filename
, &statbuf
) < 0) ||
496 ((statbuf
.st_mode
& S_IFMT
) != S_IFREG
)) {
497 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
500 file
= fopen(filename
, "r");
501 if (filename
!= name
)
505 UCI_THROW(ctx
, UCI_ERR_IO
);
507 return uci_import(ctx
, file
, name
, cfg
);