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>
27 #define LINEBUF_MAX 4096
30 * Fetch a new line from the input stream and resize buffer if necessary
32 static void uci_getln(struct uci_context
*ctx
, int offset
)
34 struct uci_parse_context
*pctx
= ctx
->pctx
;
38 if (pctx
->buf
== NULL
) {
39 pctx
->buf
= uci_malloc(ctx
, LINEBUF
);
40 pctx
->bufsz
= LINEBUF
;
48 p
= fgets(p
, pctx
->bufsz
- ofs
, pctx
->file
);
53 if (pctx
->buf
[ofs
- 1] == '\n') {
55 pctx
->buf
[ofs
- 1] = 0;
59 if (pctx
->bufsz
> LINEBUF_MAX
/2) {
60 pctx
->reason
= "line too long";
61 pctx
->byte
= LINEBUF_MAX
;
62 UCI_THROW(ctx
, UCI_ERR_PARSE
);
66 pctx
->buf
= uci_realloc(ctx
, pctx
->buf
, pctx
->bufsz
);
71 * Clean up all extra memory used by the parser and exporter
73 static void uci_file_cleanup(struct uci_context
*ctx
)
75 struct uci_parse_context
*pctx
;
89 uci_free_package(pctx
->package
);
100 * parse a character escaped by '\'
101 * returns true if the escaped character is to be parsed
102 * returns false if the escaped character is to be ignored
104 static inline bool parse_backslash(struct uci_context
*ctx
, char **str
)
109 /* undecoded backslash at the end of line, fetch the next line */
112 uci_getln(ctx
, *str
- ctx
->pctx
->buf
);
116 /* FIXME: decode escaped char, necessary? */
121 * move the string pointer forward until a non-whitespace character or
124 static void skip_whitespace(struct uci_context
*ctx
, char **str
)
127 while (**str
&& isspace(**str
))
131 if (!parse_backslash(ctx
, str
))
136 static inline void addc(char **dest
, char **src
)
144 * parse a double quoted string argument from the command line
146 static void parse_double_quote(struct uci_context
*ctx
, char **str
, char **target
)
150 /* skip quote character */
153 while ((c
= **str
)) {
160 if (!parse_backslash(ctx
, str
))
168 ctx
->pctx
->reason
= "unterminated \"";
169 ctx
->pctx
->byte
= *str
- ctx
->pctx
->buf
;
170 UCI_THROW(ctx
, UCI_ERR_PARSE
);
174 * parse a single quoted string argument from the command line
176 static void parse_single_quote(struct uci_context
*ctx
, char **str
, char **target
)
179 /* skip quote character */
182 while ((c
= **str
)) {
192 ctx
->pctx
->reason
= "unterminated '";
193 ctx
->pctx
->byte
= *str
- ctx
->pctx
->buf
;
194 UCI_THROW(ctx
, UCI_ERR_PARSE
);
198 * parse a string from the command line and detect the quoting style
200 static void parse_str(struct uci_context
*ctx
, char **str
, char **target
)
205 parse_single_quote(ctx
, str
, target
);
208 parse_double_quote(ctx
, str
, target
);
213 if (!parse_backslash(ctx
, str
))
220 } while (**str
&& !isspace(**str
));
224 * if the string was unquoted and we've stopped at a whitespace
225 * character, skip to the next one, because the whitespace will
226 * be overwritten by a null byte here
231 /* terminate the parsed string */
236 * extract the next argument from the command line
238 static char *next_arg(struct uci_context
*ctx
, char **str
, bool required
)
244 skip_whitespace(ctx
, str
);
245 parse_str(ctx
, str
, &ptr
);
246 if (required
&& !*val
) {
247 ctx
->pctx
->reason
= "insufficient arguments";
248 ctx
->pctx
->byte
= *str
- ctx
->pctx
->buf
;
249 UCI_THROW(ctx
, UCI_ERR_PARSE
);
256 * verify that the end of the line or command is reached.
257 * throw an error if extra arguments are given on the command line
259 static void assert_eol(struct uci_context
*ctx
, char **str
)
263 tmp
= next_arg(ctx
, str
, false);
265 ctx
->pctx
->reason
= "too many arguments";
266 ctx
->pctx
->byte
= tmp
- ctx
->pctx
->buf
;
267 UCI_THROW(ctx
, UCI_ERR_PARSE
);
272 * switch to a different config, either triggered by uci_load, or by a
273 * 'package <...>' statement in the import file
275 static void uci_switch_config(struct uci_context
*ctx
)
277 struct uci_parse_context
*pctx
;
283 /* add the last config to main config file list */
285 uci_list_add(&ctx
->root
, &pctx
->package
->e
.list
);
287 pctx
->package
= NULL
;
288 pctx
->section
= NULL
;
295 * if an older config under the same name exists, unload it
296 * ignore errors here, e.g. if the config was not found
298 UCI_TRAP_SAVE(ctx
, ignore
);
299 uci_unload(ctx
, name
);
300 UCI_TRAP_RESTORE(ctx
);
304 pctx
->package
= uci_alloc_package(ctx
, name
);
308 * parse the 'package' uci command (next config package)
310 static void uci_parse_package(struct uci_context
*ctx
, char **str
)
314 /* command string null-terminated by strtok */
315 *str
+= strlen(*str
) + 1;
317 name
= next_arg(ctx
, str
, true);
318 assert_eol(ctx
, str
);
319 ctx
->pctx
->name
= name
;
320 uci_switch_config(ctx
);
324 * parse the 'config' uci command (open a section)
326 static void uci_parse_config(struct uci_context
*ctx
, char **str
)
331 if (!ctx
->pctx
->package
) {
332 if (!ctx
->pctx
->name
) {
333 ctx
->pctx
->byte
= *str
- ctx
->pctx
->buf
;
334 ctx
->pctx
->reason
= "attempting to import a file without a package name";
335 UCI_THROW(ctx
, UCI_ERR_PARSE
);
337 uci_switch_config(ctx
);
340 /* command string null-terminated by strtok */
341 *str
+= strlen(*str
) + 1;
343 type
= next_arg(ctx
, str
, true);
344 name
= next_arg(ctx
, str
, false);
345 assert_eol(ctx
, str
);
346 ctx
->pctx
->section
= uci_alloc_section(ctx
->pctx
->package
, type
, name
);
350 * parse the 'option' uci command (open a value)
352 static void uci_parse_option(struct uci_context
*ctx
, char **str
)
357 if (!ctx
->pctx
->section
) {
358 ctx
->pctx
->byte
= *str
- ctx
->pctx
->buf
;
359 ctx
->pctx
->reason
= "option command found before the first section";
360 UCI_THROW(ctx
, UCI_ERR_PARSE
);
362 /* command string null-terminated by strtok */
363 *str
+= strlen(*str
) + 1;
365 name
= next_arg(ctx
, str
, true);
366 value
= next_arg(ctx
, str
, true);
367 assert_eol(ctx
, str
);
368 uci_alloc_option(ctx
->pctx
->section
, name
, value
);
373 * parse a complete input line, split up combined commands by ';'
375 static void uci_parse_line(struct uci_context
*ctx
)
377 struct uci_parse_context
*pctx
= ctx
->pctx
;
378 char *word
, *brk
= NULL
;
380 for (word
= strtok_r(pctx
->buf
, ";", &brk
);
382 word
= strtok_r(NULL
, ";", &brk
)) {
385 word
= strtok_r(word
, " \t", &pbrk
);
389 if ((word
[1] == 0) || !strcmp(word
+ 1, "ackage"))
390 uci_parse_package(ctx
, &word
);
393 if ((word
[1] == 0) || !strcmp(word
+ 1, "onfig"))
394 uci_parse_config(ctx
, &word
);
397 if ((word
[1] == 0) || !strcmp(word
+ 1, "ption"))
398 uci_parse_option(ctx
, &word
);
401 pctx
->reason
= "unterminated command";
402 pctx
->byte
= word
- pctx
->buf
;
403 UCI_THROW(ctx
, UCI_ERR_PARSE
);
409 /* max number of characters that escaping adds to the string */
410 #define UCI_QUOTE_ESCAPE "'\\'"
413 * escape an uci string for export
415 static char *uci_escape(struct uci_context
*ctx
, char *str
)
421 ctx
->bufsz
= LINEBUF
;
422 ctx
->buf
= malloc(LINEBUF
);
426 p
= strchr(str
, '\'');
433 if (p
+ sizeof(UCI_QUOTE_ESCAPE
) - str
>= ctx
->bufsz
) {
435 ctx
->buf
= realloc(ctx
->buf
, ctx
->bufsz
);
437 UCI_THROW(ctx
, UCI_ERR_MEM
);
439 memcpy(&ctx
->buf
[pos
], s
, len
);
442 strcpy(&ctx
->buf
[pos
], UCI_QUOTE_ESCAPE
);
443 pos
+= sizeof(UCI_QUOTE_ESCAPE
);
445 } while ((p
= strchr(s
, '\'')));
452 * export a single config package to a file stream
454 static void uci_export_package(struct uci_package
*p
, FILE *stream
)
456 struct uci_context
*ctx
= p
->ctx
;
457 struct uci_element
*s
, *o
;
459 fprintf(stream
, "package '%s'\n", uci_escape(ctx
, p
->e
.name
));
460 uci_foreach_element(&p
->sections
, s
) {
461 struct uci_section
*sec
= uci_to_section(s
);
462 fprintf(stream
, "\nconfig '%s'", uci_escape(ctx
, sec
->type
));
463 fprintf(stream
, " '%s'\n", uci_escape(ctx
, sec
->e
.name
));
464 uci_foreach_element(&sec
->options
, o
) {
465 struct uci_option
*opt
= uci_to_option(o
);
466 fprintf(stream
, "\toption '%s'", uci_escape(ctx
, opt
->e
.name
));
467 fprintf(stream
, " '%s'\n", uci_escape(ctx
, opt
->value
));
470 fprintf(stream
, "\n");
473 int uci_export(struct uci_context
*ctx
, FILE *stream
, struct uci_package
*package
)
475 struct uci_element
*e
;
478 UCI_ASSERT(ctx
, stream
!= NULL
);
481 uci_export_package(package
, stream
);
485 uci_foreach_element(&ctx
->root
, e
) {
486 uci_export_package(uci_to_package(e
), stream
);
492 int uci_import(struct uci_context
*ctx
, FILE *stream
, const char *name
, struct uci_package
**package
)
494 struct uci_parse_context
*pctx
;
496 /* make sure no memory from previous parse attempts is leaked */
497 uci_file_cleanup(ctx
);
499 pctx
= (struct uci_parse_context
*) uci_malloc(ctx
, sizeof(struct uci_parse_context
));
504 * If 'name' was supplied, assume that the supplied stream does not contain
505 * the appropriate 'package <name>' string to specify the config name
506 * NB: the config file can still override the package name
511 while (!feof(pctx
->file
)) {
518 *package
= pctx
->package
;
521 uci_switch_config(ctx
);
523 /* no error happened, we can get rid of the parser context now */
524 uci_file_cleanup(ctx
);
529 int uci_load(struct uci_context
*ctx
, const char *name
, struct uci_package
**package
)
538 UCI_ASSERT(ctx
, name
!= NULL
);
543 /* absolute/relative path outside of /etc/config */
544 filename
= (char *) name
;
548 filename
= uci_malloc(ctx
, strlen(name
) + sizeof(UCI_CONFDIR
) + 2);
549 sprintf(filename
, UCI_CONFDIR
"/%s", name
);
554 if ((stat(filename
, &statbuf
) < 0) ||
555 ((statbuf
.st_mode
& S_IFMT
) != S_IFREG
)) {
556 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
559 fd
= open(filename
, O_RDONLY
);
560 if (filename
!= name
)
564 file
= fdopen(fd
, "r");
566 UCI_THROW(ctx
, UCI_ERR_IO
);
569 UCI_TRAP_SAVE(ctx
, done
);
570 uci_import(ctx
, file
, name
, package
);
571 UCI_TRAP_RESTORE(ctx
);
580 * This function returns the filename by returning the string
581 * after the last '/' character. By checking for a non-'\0'
582 * character afterwards, directories are ignored (glob marks
583 * those with a trailing '/'
585 static inline char *get_filename(char *path
)
589 p
= strrchr(path
, '/');
596 char **uci_list_configs(struct uci_context
*ctx
)
603 if (glob(UCI_CONFDIR
"/*", GLOB_MARK
, NULL
, &globbuf
) != 0)
606 size
= sizeof(char *) * (globbuf
.gl_pathc
+ 1);
607 for(i
= 0; i
< globbuf
.gl_pathc
; i
++) {
610 p
= get_filename(globbuf
.gl_pathv
[i
]);
614 size
+= strlen(p
) + 1;
617 configs
= malloc(size
);
621 memset(configs
, 0, size
);
622 buf
= (char *) &configs
[globbuf
.gl_pathc
+ 1];
623 for(i
= 0; i
< globbuf
.gl_pathc
; i
++) {
626 p
= get_filename(globbuf
.gl_pathv
[i
]);
632 buf
+= strlen(buf
) + 1;