772cf255119ebf9156a34458dcafc2540c06f85d
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>
28 #define LINEBUF_MAX 4096
31 * Fetch a new line from the input stream and resize buffer if necessary
33 static void uci_getln(struct uci_context
*ctx
, int offset
)
35 struct uci_parse_context
*pctx
= ctx
->pctx
;
39 if (pctx
->buf
== NULL
) {
40 pctx
->buf
= uci_malloc(ctx
, LINEBUF
);
41 pctx
->bufsz
= LINEBUF
;
49 p
= fgets(p
, pctx
->bufsz
- ofs
, pctx
->file
);
54 if (pctx
->buf
[ofs
- 1] == '\n') {
56 pctx
->buf
[ofs
- 1] = 0;
60 if (pctx
->bufsz
> LINEBUF_MAX
/2) {
61 pctx
->reason
= "line too long";
62 pctx
->byte
= LINEBUF_MAX
;
63 UCI_THROW(ctx
, UCI_ERR_PARSE
);
67 pctx
->buf
= uci_realloc(ctx
, pctx
->buf
, pctx
->bufsz
);
72 * Clean up all extra memory used by the parser and exporter
74 static void uci_file_cleanup(struct uci_context
*ctx
)
76 struct uci_parse_context
*pctx
;
90 uci_free_package(pctx
->package
);
101 * parse a character escaped by '\'
102 * returns true if the escaped character is to be parsed
103 * returns false if the escaped character is to be ignored
105 static inline bool parse_backslash(struct uci_context
*ctx
, char **str
)
110 /* undecoded backslash at the end of line, fetch the next line */
113 uci_getln(ctx
, *str
- ctx
->pctx
->buf
);
117 /* FIXME: decode escaped char, necessary? */
122 * move the string pointer forward until a non-whitespace character or
125 static void skip_whitespace(struct uci_context
*ctx
, char **str
)
128 while (**str
&& isspace(**str
))
132 if (!parse_backslash(ctx
, str
))
137 static inline void addc(char **dest
, char **src
)
145 * parse a double quoted string argument from the command line
147 static void parse_double_quote(struct uci_context
*ctx
, char **str
, char **target
)
151 /* skip quote character */
154 while ((c
= **str
)) {
161 if (!parse_backslash(ctx
, str
))
169 ctx
->pctx
->reason
= "unterminated \"";
170 ctx
->pctx
->byte
= *str
- ctx
->pctx
->buf
;
171 UCI_THROW(ctx
, UCI_ERR_PARSE
);
175 * parse a single quoted string argument from the command line
177 static void parse_single_quote(struct uci_context
*ctx
, char **str
, char **target
)
180 /* skip quote character */
183 while ((c
= **str
)) {
193 ctx
->pctx
->reason
= "unterminated '";
194 ctx
->pctx
->byte
= *str
- ctx
->pctx
->buf
;
195 UCI_THROW(ctx
, UCI_ERR_PARSE
);
199 * parse a string from the command line and detect the quoting style
201 static void parse_str(struct uci_context
*ctx
, char **str
, char **target
)
206 parse_single_quote(ctx
, str
, target
);
209 parse_double_quote(ctx
, str
, target
);
214 if (!parse_backslash(ctx
, str
))
221 } while (**str
&& !isspace(**str
));
225 * if the string was unquoted and we've stopped at a whitespace
226 * character, skip to the next one, because the whitespace will
227 * be overwritten by a null byte here
232 /* terminate the parsed string */
237 * extract the next argument from the command line
239 static char *next_arg(struct uci_context
*ctx
, char **str
, bool required
)
245 skip_whitespace(ctx
, str
);
246 parse_str(ctx
, str
, &ptr
);
247 if (required
&& !*val
) {
248 ctx
->pctx
->reason
= "insufficient arguments";
249 ctx
->pctx
->byte
= *str
- ctx
->pctx
->buf
;
250 UCI_THROW(ctx
, UCI_ERR_PARSE
);
257 * verify that the end of the line or command is reached.
258 * throw an error if extra arguments are given on the command line
260 static void assert_eol(struct uci_context
*ctx
, char **str
)
264 tmp
= next_arg(ctx
, str
, false);
266 ctx
->pctx
->reason
= "too many arguments";
267 ctx
->pctx
->byte
= tmp
- ctx
->pctx
->buf
;
268 UCI_THROW(ctx
, UCI_ERR_PARSE
);
273 * switch to a different config, either triggered by uci_load, or by a
274 * 'package <...>' statement in the import file
276 static void uci_switch_config(struct uci_context
*ctx
)
278 struct uci_parse_context
*pctx
;
279 struct uci_element
*e
;
285 /* add the last config to main config file list */
287 uci_list_add(&ctx
->root
, &pctx
->package
->e
.list
);
289 pctx
->package
= NULL
;
290 pctx
->section
= NULL
;
297 * if an older config under the same name exists, unload it
298 * ignore errors here, e.g. if the config was not found
300 UCI_TRAP_SAVE(ctx
, ignore
);
301 e
= uci_lookup_list(ctx
, &ctx
->root
, name
);
303 uci_unload(ctx
, uci_to_package(e
));
304 UCI_TRAP_RESTORE(ctx
);
308 pctx
->package
= uci_alloc_package(ctx
, name
);
312 * parse the 'package' uci command (next config package)
314 static void uci_parse_package(struct uci_context
*ctx
, char **str
, bool single
)
318 /* command string null-terminated by strtok */
319 *str
+= strlen(*str
) + 1;
321 name
= next_arg(ctx
, str
, true);
322 assert_eol(ctx
, str
);
326 ctx
->pctx
->name
= name
;
327 uci_switch_config(ctx
);
331 * parse the 'config' uci command (open a section)
333 static void uci_parse_config(struct uci_context
*ctx
, char **str
)
338 if (!ctx
->pctx
->package
) {
339 if (!ctx
->pctx
->name
) {
340 ctx
->pctx
->byte
= *str
- ctx
->pctx
->buf
;
341 ctx
->pctx
->reason
= "attempting to import a file without a package name";
342 UCI_THROW(ctx
, UCI_ERR_PARSE
);
344 uci_switch_config(ctx
);
347 /* command string null-terminated by strtok */
348 *str
+= strlen(*str
) + 1;
350 type
= next_arg(ctx
, str
, true);
351 name
= next_arg(ctx
, str
, false);
352 assert_eol(ctx
, str
);
353 ctx
->pctx
->section
= uci_alloc_section(ctx
->pctx
->package
, type
, name
);
357 * parse the 'option' uci command (open a value)
359 static void uci_parse_option(struct uci_context
*ctx
, char **str
)
364 if (!ctx
->pctx
->section
) {
365 ctx
->pctx
->byte
= *str
- ctx
->pctx
->buf
;
366 ctx
->pctx
->reason
= "option command found before the first section";
367 UCI_THROW(ctx
, UCI_ERR_PARSE
);
369 /* command string null-terminated by strtok */
370 *str
+= strlen(*str
) + 1;
372 name
= next_arg(ctx
, str
, true);
373 value
= next_arg(ctx
, str
, true);
374 assert_eol(ctx
, str
);
375 uci_alloc_option(ctx
->pctx
->section
, name
, value
);
380 * parse a complete input line, split up combined commands by ';'
382 static void uci_parse_line(struct uci_context
*ctx
, bool single
)
384 struct uci_parse_context
*pctx
= ctx
->pctx
;
385 char *word
, *brk
= NULL
;
387 for (word
= strtok_r(pctx
->buf
, ";", &brk
);
389 word
= strtok_r(NULL
, ";", &brk
)) {
392 word
= strtok_r(word
, " \t", &pbrk
);
396 if ((word
[1] == 0) || !strcmp(word
+ 1, "ackage"))
397 uci_parse_package(ctx
, &word
, single
);
400 if ((word
[1] == 0) || !strcmp(word
+ 1, "onfig"))
401 uci_parse_config(ctx
, &word
);
404 if ((word
[1] == 0) || !strcmp(word
+ 1, "ption"))
405 uci_parse_option(ctx
, &word
);
408 pctx
->reason
= "unterminated command";
409 pctx
->byte
= word
- pctx
->buf
;
410 UCI_THROW(ctx
, UCI_ERR_PARSE
);
416 /* max number of characters that escaping adds to the string */
417 #define UCI_QUOTE_ESCAPE "'\\''"
420 * escape an uci string for export
422 static char *uci_escape(struct uci_context
*ctx
, char *str
)
428 ctx
->bufsz
= LINEBUF
;
429 ctx
->buf
= malloc(LINEBUF
);
433 p
= strchr(str
, '\'');
440 if (p
+ sizeof(UCI_QUOTE_ESCAPE
) - str
>= ctx
->bufsz
) {
442 ctx
->buf
= realloc(ctx
->buf
, ctx
->bufsz
);
444 UCI_THROW(ctx
, UCI_ERR_MEM
);
446 memcpy(&ctx
->buf
[pos
], s
, len
);
449 strcpy(&ctx
->buf
[pos
], UCI_QUOTE_ESCAPE
);
450 pos
+= sizeof(UCI_QUOTE_ESCAPE
);
452 } while ((p
= strchr(s
, '\'')));
459 * export a single config package to a file stream
461 static void uci_export_package(struct uci_package
*p
, FILE *stream
, bool header
)
463 struct uci_context
*ctx
= p
->ctx
;
464 struct uci_element
*s
, *o
;
467 fprintf(stream
, "package '%s'\n", uci_escape(ctx
, p
->e
.name
));
468 uci_foreach_element(&p
->sections
, s
) {
469 struct uci_section
*sec
= uci_to_section(s
);
470 fprintf(stream
, "\nconfig '%s'", uci_escape(ctx
, sec
->type
));
471 fprintf(stream
, " '%s'\n", uci_escape(ctx
, sec
->e
.name
));
472 uci_foreach_element(&sec
->options
, o
) {
473 struct uci_option
*opt
= uci_to_option(o
);
474 fprintf(stream
, "\toption '%s'", uci_escape(ctx
, opt
->e
.name
));
475 fprintf(stream
, " '%s'\n", uci_escape(ctx
, opt
->value
));
478 fprintf(stream
, "\n");
481 int uci_export(struct uci_context
*ctx
, FILE *stream
, struct uci_package
*package
, bool header
)
483 struct uci_element
*e
;
486 UCI_ASSERT(ctx
, stream
!= NULL
);
489 uci_export_package(package
, stream
, header
);
493 uci_foreach_element(&ctx
->root
, e
) {
494 uci_export_package(uci_to_package(e
), stream
, header
);
500 int uci_import(struct uci_context
*ctx
, FILE *stream
, const char *name
, struct uci_package
**package
, bool single
)
502 struct uci_parse_context
*pctx
;
504 /* make sure no memory from previous parse attempts is leaked */
505 uci_file_cleanup(ctx
);
507 pctx
= (struct uci_parse_context
*) uci_malloc(ctx
, sizeof(struct uci_parse_context
));
512 * If 'name' was supplied, assume that the supplied stream does not contain
513 * the appropriate 'package <name>' string to specify the config name
514 * NB: the config file can still override the package name
519 while (!feof(pctx
->file
)) {
522 uci_parse_line(ctx
, single
);
526 *package
= pctx
->package
;
529 uci_switch_config(ctx
);
531 /* no error happened, we can get rid of the parser context now */
532 uci_file_cleanup(ctx
);
537 int uci_load(struct uci_context
*ctx
, const char *name
, struct uci_package
**package
)
546 UCI_ASSERT(ctx
, name
!= NULL
);
551 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
554 /* absolute/relative path outside of /etc/config */
555 filename
= uci_strdup(ctx
, name
);
556 name
= strrchr(name
, '/') + 1;
560 filename
= uci_malloc(ctx
, strlen(name
) + sizeof(UCI_CONFDIR
) + 2);
561 sprintf(filename
, UCI_CONFDIR
"/%s", name
);
566 if ((stat(filename
, &statbuf
) < 0) ||
567 ((statbuf
.st_mode
& S_IFMT
) != S_IFREG
)) {
568 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
571 fd
= open(filename
, O_RDONLY
);
573 UCI_THROW(ctx
, UCI_ERR_IO
);
575 if (flock(fd
, LOCK_SH
) < 0)
576 UCI_THROW(ctx
, UCI_ERR_IO
);
578 file
= fdopen(fd
, "r");
580 UCI_THROW(ctx
, UCI_ERR_IO
);
583 UCI_TRAP_SAVE(ctx
, done
);
584 uci_import(ctx
, file
, name
, package
, true);
585 UCI_TRAP_RESTORE(ctx
);
588 (*package
)->path
= filename
;
589 (*package
)->confdir
= confdir
;
598 int uci_commit(struct uci_context
*ctx
, struct uci_package
*p
)
602 int err
= UCI_ERR_IO
;
605 UCI_ASSERT(ctx
, p
!= NULL
);
606 UCI_ASSERT(ctx
, p
->path
!= NULL
);
608 fd
= open(p
->path
, O_RDWR
);
612 if (flock(fd
, LOCK_EX
) < 0)
620 UCI_TRAP_SAVE(ctx
, done
);
621 uci_export(ctx
, f
, p
, false);
622 UCI_TRAP_RESTORE(ctx
);
631 UCI_THROW(ctx
, ctx
->errno
);
633 UCI_THROW(ctx
, UCI_ERR_IO
);
639 * This function returns the filename by returning the string
640 * after the last '/' character. By checking for a non-'\0'
641 * character afterwards, directories are ignored (glob marks
642 * those with a trailing '/'
644 static inline char *get_filename(char *path
)
648 p
= strrchr(path
, '/');
655 char **uci_list_configs(struct uci_context
*ctx
)
662 if (glob(UCI_CONFDIR
"/*", GLOB_MARK
, NULL
, &globbuf
) != 0)
665 size
= sizeof(char *) * (globbuf
.gl_pathc
+ 1);
666 for(i
= 0; i
< globbuf
.gl_pathc
; i
++) {
669 p
= get_filename(globbuf
.gl_pathv
[i
]);
673 size
+= strlen(p
) + 1;
676 configs
= malloc(size
);
680 memset(configs
, 0, size
);
681 buf
= (char *) &configs
[globbuf
.gl_pathc
+ 1];
682 for(i
= 0; i
< globbuf
.gl_pathc
; i
++) {
685 p
= get_filename(globbuf
.gl_pathv
[i
]);
691 buf
+= strlen(buf
) + 1;