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 Lesser General Public License for more details.
16 * This file contains the code for parsing uci config files
20 #include <sys/types.h>
33 #include "uci_internal.h"
38 * Fetch a new line from the input stream and resize buffer if necessary
40 __private
void uci_getln(struct uci_context
*ctx
, int offset
)
42 struct uci_parse_context
*pctx
= ctx
->pctx
;
46 if (pctx
->buf
== NULL
) {
47 pctx
->buf
= uci_malloc(ctx
, LINEBUF
);
48 pctx
->bufsz
= LINEBUF
;
50 /* It takes 2 slots for fgets to read 1 char. */
51 if (offset
>= pctx
->bufsz
- 1) {
53 pctx
->buf
= uci_realloc(ctx
, pctx
->buf
, pctx
->bufsz
);
61 p
= fgets(p
, pctx
->bufsz
- ofs
, pctx
->file
);
66 if (pctx
->buf
[ofs
- 1] == '\n') {
72 pctx
->buf
= uci_realloc(ctx
, pctx
->buf
, pctx
->bufsz
);
77 * parse a character escaped by '\'
78 * returns true if the escaped character is to be parsed
79 * returns false if the escaped character is to be ignored
81 static bool parse_backslash(struct uci_context
*ctx
)
83 struct uci_parse_context
*pctx
= ctx
->pctx
;
88 /* undecoded backslash at the end of line, fetch the next line */
89 if (!pctx_cur_char(pctx
) ||
90 pctx_cur_char(pctx
) == '\n' ||
91 (pctx_cur_char(pctx
) == '\r' &&
92 pctx_char(pctx
, pctx_pos(pctx
) + 1) == '\n' &&
93 !pctx_char(pctx
, pctx_pos(pctx
) + 2))) {
94 uci_getln(ctx
, pctx
->pos
);
98 /* FIXME: decode escaped char, necessary? */
103 * move the string pointer forward until a non-whitespace character or
106 static void skip_whitespace(struct uci_context
*ctx
)
108 struct uci_parse_context
*pctx
= ctx
->pctx
;
110 while (pctx_cur_char(pctx
) && isspace(pctx_cur_char(pctx
)))
114 static inline void addc(struct uci_context
*ctx
, int *pos_dest
, int *pos_src
)
116 struct uci_parse_context
*pctx
= ctx
->pctx
;
118 pctx_char(pctx
, *pos_dest
) = pctx_char(pctx
, *pos_src
);
124 * parse a double quoted string argument from the command line
126 static void parse_double_quote(struct uci_context
*ctx
, int *target
)
128 struct uci_parse_context
*pctx
= ctx
->pctx
;
131 /* skip quote character */
135 c
= pctx_cur_char(pctx
);
141 /* Multi-line str value */
142 uci_getln(ctx
, pctx
->pos
);
143 if (!pctx_cur_char(pctx
)) {
144 uci_parse_error(ctx
, "EOF with unterminated \"");
148 if (!parse_backslash(ctx
))
152 addc(ctx
, target
, &pctx
->pos
);
159 * parse a single quoted string argument from the command line
161 static void parse_single_quote(struct uci_context
*ctx
, int *target
)
163 struct uci_parse_context
*pctx
= ctx
->pctx
;
165 /* skip quote character */
169 c
= pctx_cur_char(pctx
);
175 /* Multi-line str value */
176 uci_getln(ctx
, pctx
->pos
);
177 if (!pctx_cur_char(pctx
))
178 uci_parse_error(ctx
, "EOF with unterminated '");
182 addc(ctx
, target
, &pctx
->pos
);
188 * parse a string from the command line and detect the quoting style
190 static void parse_str(struct uci_context
*ctx
, int *target
)
192 struct uci_parse_context
*pctx
= ctx
->pctx
;
195 switch(pctx_cur_char(pctx
)) {
197 parse_single_quote(ctx
, target
);
200 parse_double_quote(ctx
, target
);
203 pctx_cur_char(pctx
) = 0;
211 if (!parse_backslash(ctx
))
215 addc(ctx
, target
, &pctx
->pos
);
218 } while (pctx_cur_char(pctx
) && !isspace(pctx_cur_char(pctx
)));
222 * if the string was unquoted and we've stopped at a whitespace
223 * character, skip to the next one, because the whitespace will
224 * be overwritten by a null byte here
226 if (pctx_cur_char(pctx
) && next
)
229 /* terminate the parsed string */
230 pctx_char(pctx
, *target
) = 0;
234 * extract the next argument from the command line
236 static int next_arg(struct uci_context
*ctx
, bool required
, bool name
, bool package
)
238 struct uci_parse_context
*pctx
= ctx
->pctx
;
241 skip_whitespace(ctx
);
242 val
= ptr
= pctx_pos(pctx
);
243 if (pctx_cur_char(pctx
) == ';') {
244 pctx_cur_char(pctx
) = 0;
247 parse_str(ctx
, &ptr
);
249 if (!pctx_char(pctx
, val
)) {
251 uci_parse_error(ctx
, "insufficient arguments");
255 if (name
&& !uci_validate_str(pctx_str(pctx
, val
), name
, package
))
256 uci_parse_error(ctx
, "invalid character in name field");
262 int uci_parse_argument(struct uci_context
*ctx
, FILE *stream
, char **str
, char **result
)
267 UCI_ASSERT(ctx
, str
!= NULL
);
268 UCI_ASSERT(ctx
, result
!= NULL
);
270 if (ctx
->pctx
&& (ctx
->pctx
->file
!= stream
))
274 uci_alloc_parse_context(ctx
);
276 ctx
->pctx
->file
= stream
;
282 ofs_result
= next_arg(ctx
, false, false, false);
283 *result
= pctx_str(ctx
->pctx
, ofs_result
);
284 *str
= pctx_cur_str(ctx
->pctx
);
290 uci_fill_ptr(struct uci_context
*ctx
, struct uci_ptr
*ptr
, struct uci_element
*e
)
292 UCI_ASSERT(ctx
, ptr
!= NULL
);
293 UCI_ASSERT(ctx
, e
!= NULL
);
295 memset(ptr
, 0, sizeof(struct uci_ptr
));
297 case UCI_TYPE_OPTION
:
298 ptr
->o
= uci_to_option(e
);
300 case UCI_TYPE_SECTION
:
301 ptr
->s
= uci_to_section(e
);
303 case UCI_TYPE_PACKAGE
:
304 ptr
->p
= uci_to_package(e
);
307 UCI_THROW(ctx
, UCI_ERR_INVAL
);
311 ptr
->option
= ptr
->o
->e
.name
;
312 ptr
->s
= ptr
->o
->section
;
314 ptr
->section
= ptr
->s
->e
.name
;
315 ptr
->p
= ptr
->s
->package
;
317 ptr
->package
= ptr
->p
->e
.name
;
319 ptr
->flags
|= UCI_LOOKUP_DONE
;
327 * verify that the end of the line or command is reached.
328 * throw an error if extra arguments are given on the command line
330 static void assert_eol(struct uci_context
*ctx
)
335 skip_whitespace(ctx
);
336 ofs_tmp
= next_arg(ctx
, false, false, false);
337 tmp
= pctx_str(ctx
->pctx
, ofs_tmp
);
338 if (*tmp
&& (ctx
->flags
& UCI_FLAG_STRICT
))
339 uci_parse_error(ctx
, "too many arguments");
343 * switch to a different config, either triggered by uci_load, or by a
344 * 'package <...>' statement in the import file
346 static void uci_switch_config(struct uci_context
*ctx
)
348 struct uci_parse_context
*pctx
;
349 struct uci_element
*e
;
355 /* add the last config to main config file list */
357 pctx
->package
->backend
= ctx
->backend
;
358 uci_list_add(&ctx
->root
, &pctx
->package
->e
.list
);
360 pctx
->package
= NULL
;
361 pctx
->section
= NULL
;
368 * if an older config under the same name exists, unload it
369 * ignore errors here, e.g. if the config was not found
371 e
= uci_lookup_list(&ctx
->root
, name
);
373 UCI_THROW(ctx
, UCI_ERR_DUPLICATE
);
374 pctx
->package
= uci_alloc_package(ctx
, name
);
378 * parse the 'package' uci command (next config package)
380 static void uci_parse_package(struct uci_context
*ctx
, bool single
)
382 struct uci_parse_context
*pctx
= ctx
->pctx
;
386 /* command string null-terminated by strtok */
387 pctx
->pos
+= strlen(pctx_cur_str(pctx
)) + 1;
389 ofs_name
= next_arg(ctx
, true, true, true);
390 name
= pctx_str(pctx
, ofs_name
);
395 ctx
->pctx
->name
= name
;
396 uci_switch_config(ctx
);
400 * parse the 'config' uci command (open a section)
402 static void uci_parse_config(struct uci_context
*ctx
)
404 struct uci_parse_context
*pctx
= ctx
->pctx
;
405 struct uci_element
*e
;
407 int ofs_name
, ofs_type
;
411 if (!ctx
->pctx
->package
) {
412 if (!ctx
->pctx
->name
)
413 uci_parse_error(ctx
, "attempting to import a file without a package name");
415 uci_switch_config(ctx
);
418 /* command string null-terminated by strtok */
419 pctx
->pos
+= strlen(pctx_cur_str(pctx
)) + 1;
421 ofs_type
= next_arg(ctx
, true, false, false);
422 type
= pctx_str(pctx
, ofs_type
);
423 if (!uci_validate_type(type
))
424 uci_parse_error(ctx
, "invalid character in type field");
426 ofs_name
= next_arg(ctx
, false, true, false);
427 type
= pctx_str(pctx
, ofs_type
);
428 name
= pctx_str(pctx
, ofs_name
);
431 if (!name
|| !name
[0]) {
432 ctx
->internal
= !pctx
->merge
;
433 UCI_NESTED(uci_add_section
, ctx
, pctx
->package
, type
, &pctx
->section
);
435 uci_fill_ptr(ctx
, &ptr
, &pctx
->package
->e
);
436 e
= uci_lookup_list(&pctx
->package
->sections
, name
);
438 ptr
.s
= uci_to_section(e
);
440 if ((ctx
->flags
& UCI_FLAG_STRICT
) && strcmp(ptr
.s
->type
, type
))
441 uci_parse_error(ctx
, "section of different type overwrites prior section with same name");
447 ctx
->internal
= !pctx
->merge
;
448 UCI_NESTED(uci_set
, ctx
, &ptr
);
449 pctx
->section
= uci_to_section(ptr
.last
);
454 * parse the 'option' uci command (open a value)
456 static void uci_parse_option(struct uci_context
*ctx
, bool list
)
458 struct uci_parse_context
*pctx
= ctx
->pctx
;
459 struct uci_element
*e
;
461 int ofs_name
, ofs_value
;
466 uci_parse_error(ctx
, "option/list command found before the first section");
468 /* command string null-terminated by strtok */
469 pctx
->pos
+= strlen(pctx_cur_str(pctx
)) + 1;
471 ofs_name
= next_arg(ctx
, true, true, false);
472 ofs_value
= next_arg(ctx
, false, false, false);
473 name
= pctx_str(pctx
, ofs_name
);
474 value
= pctx_str(pctx
, ofs_value
);
477 uci_fill_ptr(ctx
, &ptr
, &pctx
->section
->e
);
478 e
= uci_lookup_list(&pctx
->section
->options
, name
);
480 ptr
.o
= uci_to_option(e
);
484 ctx
->internal
= !pctx
->merge
;
486 UCI_NESTED(uci_add_list
, ctx
, &ptr
);
488 UCI_NESTED(uci_set
, ctx
, &ptr
);
492 * parse a complete input line, split up combined commands by ';'
494 static void uci_parse_line(struct uci_context
*ctx
, bool single
)
496 struct uci_parse_context
*pctx
= ctx
->pctx
;
499 /* Skip whitespace characters at the start of line */
500 skip_whitespace(ctx
);
502 word
= strtok(pctx_cur_str(pctx
), " \t");
511 if ((word
[1] == 0) || !strcmp(word
+ 1, "ackage"))
512 uci_parse_package(ctx
, single
);
517 if ((word
[1] == 0) || !strcmp(word
+ 1, "onfig"))
518 uci_parse_config(ctx
);
523 if ((word
[1] == 0) || !strcmp(word
+ 1, "ption"))
524 uci_parse_option(ctx
, false);
529 if ((word
[1] == 0) || !strcmp(word
+ 1, "ist"))
530 uci_parse_option(ctx
, true);
539 uci_parse_error(ctx
, "invalid command");
543 /* max number of characters that escaping adds to the string */
544 #define UCI_QUOTE_ESCAPE "'\\''"
547 * escape an uci string for export
549 static const char *uci_escape(struct uci_context
*ctx
, const char *str
)
555 ctx
->bufsz
= LINEBUF
;
556 ctx
->buf
= malloc(LINEBUF
);
565 end
= strchr(str
, '\'');
567 end
= str
+ strlen(str
);
570 /* make sure that we have enough room in the buffer */
571 while (ofs
+ len
+ sizeof(UCI_QUOTE_ESCAPE
) + 1 > ctx
->bufsz
) {
573 ctx
->buf
= uci_realloc(ctx
, ctx
->buf
, ctx
->bufsz
);
576 /* copy the string until the character before the quote */
577 memcpy(&ctx
->buf
[ofs
], str
, len
);
580 /* end of string? return the buffer */
584 memcpy(&ctx
->buf
[ofs
], UCI_QUOTE_ESCAPE
, sizeof(UCI_QUOTE_ESCAPE
));
585 ofs
+= strlen(&ctx
->buf
[ofs
]);
594 * export a single config package to a file stream
596 static void uci_export_package(struct uci_package
*p
, FILE *stream
, bool header
)
598 struct uci_context
*ctx
= p
->ctx
;
599 struct uci_element
*s
, *o
, *i
;
602 fprintf(stream
, "package %s\n", uci_escape(ctx
, p
->e
.name
));
603 uci_foreach_element(&p
->sections
, s
) {
604 struct uci_section
*sec
= uci_to_section(s
);
605 fprintf(stream
, "\nconfig %s", uci_escape(ctx
, sec
->type
));
606 if (!sec
->anonymous
|| (ctx
->flags
& UCI_FLAG_EXPORT_NAME
))
607 fprintf(stream
, " '%s'", uci_escape(ctx
, sec
->e
.name
));
608 fprintf(stream
, "\n");
609 uci_foreach_element(&sec
->options
, o
) {
610 struct uci_option
*opt
= uci_to_option(o
);
612 case UCI_TYPE_STRING
:
613 fprintf(stream
, "\toption %s", uci_escape(ctx
, opt
->e
.name
));
614 fprintf(stream
, " '%s'\n", uci_escape(ctx
, opt
->v
.string
));
617 uci_foreach_element(&opt
->v
.list
, i
) {
618 fprintf(stream
, "\tlist %s", uci_escape(ctx
, opt
->e
.name
));
619 fprintf(stream
, " '%s'\n", uci_escape(ctx
, i
->name
));
623 fprintf(stream
, "\t# unknown type for option '%s'\n", uci_escape(ctx
, opt
->e
.name
));
628 fprintf(stream
, "\n");
631 int uci_export(struct uci_context
*ctx
, FILE *stream
, struct uci_package
*package
, bool header
)
633 struct uci_element
*e
;
636 UCI_ASSERT(ctx
, stream
!= NULL
);
639 uci_export_package(package
, stream
, header
);
641 uci_foreach_element(&ctx
->root
, e
) {
642 uci_export_package(uci_to_package(e
), stream
, header
);
649 int uci_import(struct uci_context
*ctx
, FILE *stream
, const char *name
, struct uci_package
**package
, bool single
)
651 struct uci_parse_context
*pctx
;
654 /* make sure no memory from previous parse attempts is leaked */
657 uci_alloc_parse_context(ctx
);
660 if (package
&& *package
&& single
) {
661 pctx
->package
= *package
;
666 * If 'name' was supplied, assume that the supplied stream does not contain
667 * the appropriate 'package <name>' string to specify the config name
668 * NB: the config file can still override the package name
671 UCI_ASSERT(ctx
, uci_validate_package(name
));
675 while (!feof(pctx
->file
)) {
678 UCI_TRAP_SAVE(ctx
, error
);
680 uci_parse_line(ctx
, single
);
681 UCI_TRAP_RESTORE(ctx
);
684 if (ctx
->flags
& UCI_FLAG_PERROR
)
685 uci_perror(ctx
, NULL
);
686 if ((ctx
->err
!= UCI_ERR_PARSE
) ||
687 (ctx
->flags
& UCI_FLAG_STRICT
))
688 UCI_THROW(ctx
, ctx
->err
);
691 if (!pctx
->package
&& name
)
692 uci_switch_config(ctx
);
694 *package
= pctx
->package
;
696 pctx
->package
= NULL
;
699 uci_switch_config(ctx
);
701 /* no error happened, we can get rid of the parser context now */
708 static char *uci_config_path(struct uci_context
*ctx
, const char *name
)
712 UCI_ASSERT(ctx
, uci_validate_package(name
));
713 filename
= uci_malloc(ctx
, strlen(name
) + strlen(ctx
->confdir
) + 2);
714 sprintf(filename
, "%s/%s", ctx
->confdir
, name
);
719 static void uci_file_commit(struct uci_context
*ctx
, struct uci_package
**package
, bool overwrite
)
721 struct uci_package
*p
= *package
;
722 FILE *f1
, *f2
= NULL
;
725 char *filename
= NULL
;
727 bool do_rename
= false;
731 p
->path
= uci_config_path(ctx
, p
->e
.name
);
733 UCI_THROW(ctx
, UCI_ERR_INVAL
);
736 if ((asprintf(&filename
, "%s/.%s.uci-XXXXXX", ctx
->confdir
, p
->e
.name
) < 0) || !filename
)
737 UCI_THROW(ctx
, UCI_ERR_MEM
);
739 /* open the config file for writing now, so that it is locked */
740 f1
= uci_open_stream(ctx
, p
->path
, NULL
, SEEK_SET
, true, true);
742 /* flush unsaved changes and reload from delta file */
743 UCI_TRAP_SAVE(ctx
, done
);
746 name
= uci_strdup(ctx
, p
->e
.name
);
747 path
= uci_strdup(ctx
, p
->path
);
748 /* dump our own changes to the delta file */
749 if (!uci_list_empty(&p
->delta
))
750 UCI_INTERNAL(uci_save
, ctx
, p
);
753 * other processes might have modified the config
754 * as well. dump and reload
756 uci_free_package(&p
);
758 UCI_INTERNAL(uci_import
, ctx
, f1
, name
, &p
, true);
764 /* freed together with the uci_package */
769 if (!uci_load_delta(ctx
, p
, true))
773 if (!mktemp(filename
))
778 UCI_THROW(ctx
, UCI_ERR_IO
);
781 if ((stat(filename
, &statbuf
) == 0) && ((statbuf
.st_mode
& S_IFMT
) != S_IFREG
))
782 UCI_THROW(ctx
, UCI_ERR_IO
);
784 f2
= uci_open_stream(ctx
, filename
, p
->path
, SEEK_SET
, true, true);
785 uci_export(ctx
, f2
, p
, false);
789 uci_close_stream(f2
);
793 UCI_TRAP_RESTORE(ctx
);
798 uci_close_stream(f1
);
800 path
= realpath(p
->path
, NULL
);
801 if (!path
|| rename(filename
, path
)) {
803 UCI_THROW(ctx
, UCI_ERR_IO
);
809 UCI_THROW(ctx
, ctx
->err
);
814 * This function returns the filename by returning the string
815 * after the last '/' character. By checking for a non-'\0'
816 * character afterwards, directories are ignored (glob marks
817 * those with a trailing '/'
819 static inline char *get_filename(char *path
)
823 p
= strrchr(path
, '/');
830 static char **uci_list_config_files(struct uci_context
*ctx
)
834 int size
, i
, j
, skipped
;
838 dir
= uci_malloc(ctx
, strlen(ctx
->confdir
) + 1 + sizeof("/*"));
839 sprintf(dir
, "%s/*", ctx
->confdir
);
840 if (glob(dir
, GLOB_MARK
, NULL
, &globbuf
) != 0) {
842 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
845 size
= sizeof(char *) * (globbuf
.gl_pathc
+ 1);
847 for(i
= 0; i
< globbuf
.gl_pathc
; i
++) {
850 p
= get_filename(globbuf
.gl_pathv
[i
]);
856 size
+= strlen(p
) + 1;
859 configs
= uci_malloc(ctx
, size
- skipped
);
860 buf
= (char *) &configs
[globbuf
.gl_pathc
+ 1 - skipped
];
862 for(i
= 0; i
< globbuf
.gl_pathc
; i
++) {
865 p
= get_filename(globbuf
.gl_pathv
[i
]);
869 if (!uci_validate_package(p
))
874 buf
+= strlen(buf
) + 1;
881 static struct uci_package
*uci_file_load(struct uci_context
*ctx
, const char *name
)
883 struct uci_package
*package
= NULL
;
890 /* relative path outside of /etc/config */
892 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
895 /* absolute path outside of /etc/config */
896 filename
= uci_strdup(ctx
, name
);
897 name
= strrchr(name
, '/') + 1;
901 /* config in /etc/config */
902 filename
= uci_config_path(ctx
, name
);
907 UCI_TRAP_SAVE(ctx
, done
);
908 file
= uci_open_stream(ctx
, filename
, NULL
, SEEK_SET
, false, false);
910 UCI_INTERNAL(uci_import
, ctx
, file
, name
, &package
, true);
911 UCI_TRAP_RESTORE(ctx
);
914 package
->path
= filename
;
915 package
->has_delta
= confdir
;
916 uci_load_delta(ctx
, package
, false);
920 uci_close_stream(file
);
923 UCI_THROW(ctx
, ctx
->err
);
928 __private
UCI_BACKEND(uci_file_backend
, "file",
929 .load
= uci_file_load
,
930 .commit
= uci_file_commit
,
931 .list_configs
= uci_list_config_files
,