3f02c119905febdc52508f6cc79cb5182ea75263
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"
36 #define LINEBUF_MAX 4096
39 * Fetch a new line from the input stream and resize buffer if necessary
41 __private
void uci_getln(struct uci_context
*ctx
, int offset
)
43 struct uci_parse_context
*pctx
= ctx
->pctx
;
47 if (pctx
->buf
== NULL
) {
48 pctx
->buf
= uci_malloc(ctx
, LINEBUF
);
49 pctx
->bufsz
= LINEBUF
;
51 /* It takes 2 slots for fgets to read 1 char. */
52 if (offset
>= pctx
->bufsz
- 1) {
54 pctx
->buf
= uci_realloc(ctx
, pctx
->buf
, pctx
->bufsz
);
62 p
= fgets(p
, pctx
->bufsz
- ofs
, pctx
->file
);
67 if (pctx
->buf
[ofs
- 1] == '\n') {
72 if (pctx
->bufsz
> LINEBUF_MAX
/2)
73 uci_parse_error(ctx
, "line too long");
76 pctx
->buf
= uci_realloc(ctx
, pctx
->buf
, pctx
->bufsz
);
81 * parse a character escaped by '\'
82 * returns true if the escaped character is to be parsed
83 * returns false if the escaped character is to be ignored
85 static bool parse_backslash(struct uci_context
*ctx
)
87 struct uci_parse_context
*pctx
= ctx
->pctx
;
92 /* undecoded backslash at the end of line, fetch the next line */
93 if (!pctx_cur_char(pctx
) ||
94 pctx_cur_char(pctx
) == '\n' ||
95 (pctx_cur_char(pctx
) == '\r' &&
96 pctx_char(pctx
, pctx_pos(pctx
) + 1) == '\n' &&
97 !pctx_char(pctx
, pctx_pos(pctx
) + 2))) {
98 uci_getln(ctx
, pctx
->pos
);
102 /* FIXME: decode escaped char, necessary? */
107 * move the string pointer forward until a non-whitespace character or
110 static void skip_whitespace(struct uci_context
*ctx
)
112 struct uci_parse_context
*pctx
= ctx
->pctx
;
114 while (pctx_cur_char(pctx
) && isspace(pctx_cur_char(pctx
)))
118 static inline void addc(struct uci_context
*ctx
, int *pos_dest
, int *pos_src
)
120 struct uci_parse_context
*pctx
= ctx
->pctx
;
122 pctx_char(pctx
, *pos_dest
) = pctx_char(pctx
, *pos_src
);
128 * parse a double quoted string argument from the command line
130 static void parse_double_quote(struct uci_context
*ctx
, int *target
)
132 struct uci_parse_context
*pctx
= ctx
->pctx
;
135 /* skip quote character */
139 c
= pctx_cur_char(pctx
);
145 /* Multi-line str value */
146 uci_getln(ctx
, pctx
->pos
);
147 if (!pctx_cur_char(pctx
)) {
148 uci_parse_error(ctx
, "EOF with unterminated \"");
152 if (!parse_backslash(ctx
))
156 addc(ctx
, target
, &pctx
->pos
);
160 uci_parse_error(ctx
, "unterminated \"");
164 * parse a single quoted string argument from the command line
166 static void parse_single_quote(struct uci_context
*ctx
, int *target
)
168 struct uci_parse_context
*pctx
= ctx
->pctx
;
170 /* skip quote character */
174 c
= pctx_cur_char(pctx
);
180 /* Multi-line str value */
181 uci_getln(ctx
, pctx
->pos
);
182 if (!pctx_cur_char(pctx
))
183 uci_parse_error(ctx
, "EOF with unterminated \"");
187 addc(ctx
, target
, &pctx
->pos
);
190 uci_parse_error(ctx
, "unterminated '");
194 * parse a string from the command line and detect the quoting style
196 static void parse_str(struct uci_context
*ctx
, int *target
)
198 struct uci_parse_context
*pctx
= ctx
->pctx
;
201 switch(pctx_cur_char(pctx
)) {
203 parse_single_quote(ctx
, target
);
206 parse_double_quote(ctx
, target
);
209 pctx_cur_char(pctx
) = 0;
217 if (!parse_backslash(ctx
))
221 addc(ctx
, target
, &pctx
->pos
);
224 } while (pctx_cur_char(pctx
) && !isspace(pctx_cur_char(pctx
)));
228 * if the string was unquoted and we've stopped at a whitespace
229 * character, skip to the next one, because the whitespace will
230 * be overwritten by a null byte here
232 if (pctx_cur_char(pctx
) && next
)
235 /* terminate the parsed string */
236 pctx_char(pctx
, *target
) = 0;
240 * extract the next argument from the command line
242 static int next_arg(struct uci_context
*ctx
, bool required
, bool name
)
244 struct uci_parse_context
*pctx
= ctx
->pctx
;
247 skip_whitespace(ctx
);
248 val
= ptr
= pctx_pos(pctx
);
249 if (pctx_cur_char(pctx
) == ';') {
250 pctx_cur_char(pctx
) = 0;
253 parse_str(ctx
, &ptr
);
255 if (!pctx_char(pctx
, val
)) {
257 uci_parse_error(ctx
, "insufficient arguments");
261 if (name
&& !uci_validate_name(pctx_str(pctx
, val
)))
262 uci_parse_error(ctx
, "invalid character in name field");
268 int uci_parse_argument(struct uci_context
*ctx
, FILE *stream
, char **str
, char **result
)
273 UCI_ASSERT(ctx
, str
!= NULL
);
274 UCI_ASSERT(ctx
, result
!= NULL
);
276 if (ctx
->pctx
&& (ctx
->pctx
->file
!= stream
))
280 uci_alloc_parse_context(ctx
);
282 ctx
->pctx
->file
= stream
;
286 *str
= ctx
->pctx
->buf
;
288 UCI_ASSERT(ctx
, ctx
->pctx
->pos
== *str
- ctx
->pctx
->buf
);
291 /*FIXME do we need to skip empty lines? */
292 ofs_result
= next_arg(ctx
, false, false);
293 *result
= pctx_str(ctx
->pctx
, ofs_result
);
294 *str
= pctx_cur_str(ctx
->pctx
);
300 uci_fill_ptr(struct uci_context
*ctx
, struct uci_ptr
*ptr
, struct uci_element
*e
)
302 UCI_ASSERT(ctx
, ptr
!= NULL
);
303 UCI_ASSERT(ctx
, e
!= NULL
);
305 memset(ptr
, 0, sizeof(struct uci_ptr
));
307 case UCI_TYPE_OPTION
:
308 ptr
->o
= uci_to_option(e
);
310 case UCI_TYPE_SECTION
:
311 ptr
->s
= uci_to_section(e
);
313 case UCI_TYPE_PACKAGE
:
314 ptr
->p
= uci_to_package(e
);
317 UCI_THROW(ctx
, UCI_ERR_INVAL
);
321 ptr
->option
= ptr
->o
->e
.name
;
322 ptr
->s
= ptr
->o
->section
;
324 ptr
->section
= ptr
->s
->e
.name
;
325 ptr
->p
= ptr
->s
->package
;
327 ptr
->package
= ptr
->p
->e
.name
;
329 ptr
->flags
|= UCI_LOOKUP_DONE
;
337 * verify that the end of the line or command is reached.
338 * throw an error if extra arguments are given on the command line
340 static void assert_eol(struct uci_context
*ctx
)
345 skip_whitespace(ctx
);
346 ofs_tmp
= next_arg(ctx
, false, false);
347 tmp
= pctx_str(ctx
->pctx
, ofs_tmp
);
348 if (*tmp
&& (ctx
->flags
& UCI_FLAG_STRICT
))
349 uci_parse_error(ctx
, "too many arguments");
353 * switch to a different config, either triggered by uci_load, or by a
354 * 'package <...>' statement in the import file
356 static void uci_switch_config(struct uci_context
*ctx
)
358 struct uci_parse_context
*pctx
;
359 struct uci_element
*e
;
365 /* add the last config to main config file list */
367 pctx
->package
->backend
= ctx
->backend
;
368 uci_list_add(&ctx
->root
, &pctx
->package
->e
.list
);
370 pctx
->package
= NULL
;
371 pctx
->section
= NULL
;
378 * if an older config under the same name exists, unload it
379 * ignore errors here, e.g. if the config was not found
381 e
= uci_lookup_list(&ctx
->root
, name
);
383 UCI_THROW(ctx
, UCI_ERR_DUPLICATE
);
384 pctx
->package
= uci_alloc_package(ctx
, name
);
388 * parse the 'package' uci command (next config package)
390 static void uci_parse_package(struct uci_context
*ctx
, bool single
)
392 struct uci_parse_context
*pctx
= ctx
->pctx
;
396 /* command string null-terminated by strtok */
397 pctx
->pos
+= strlen(pctx_cur_str(pctx
)) + 1;
399 ofs_name
= next_arg(ctx
, true, true);
400 name
= pctx_str(pctx
, ofs_name
);
405 ctx
->pctx
->name
= name
;
406 uci_switch_config(ctx
);
410 * parse the 'config' uci command (open a section)
412 static void uci_parse_config(struct uci_context
*ctx
)
414 struct uci_parse_context
*pctx
= ctx
->pctx
;
415 struct uci_element
*e
;
417 int ofs_name
, ofs_type
;
421 uci_fixup_section(ctx
, ctx
->pctx
->section
);
422 if (!ctx
->pctx
->package
) {
423 if (!ctx
->pctx
->name
)
424 uci_parse_error(ctx
, "attempting to import a file without a package name");
426 uci_switch_config(ctx
);
429 /* command string null-terminated by strtok */
430 pctx
->pos
+= strlen(pctx_cur_str(pctx
)) + 1;
432 ofs_type
= next_arg(ctx
, true, false);
433 type
= pctx_str(pctx
, ofs_type
);
434 if (!uci_validate_type(type
))
435 uci_parse_error(ctx
, "invalid character in type field");
437 ofs_name
= next_arg(ctx
, false, true);
438 type
= pctx_str(pctx
, ofs_type
);
439 name
= pctx_str(pctx
, ofs_name
);
442 if (!name
|| !name
[0]) {
443 ctx
->internal
= !pctx
->merge
;
444 UCI_NESTED(uci_add_section
, ctx
, pctx
->package
, type
, &pctx
->section
);
446 uci_fill_ptr(ctx
, &ptr
, &pctx
->package
->e
);
447 e
= uci_lookup_list(&pctx
->package
->sections
, name
);
449 ptr
.s
= uci_to_section(e
);
453 ctx
->internal
= !pctx
->merge
;
454 UCI_NESTED(uci_set
, ctx
, &ptr
);
455 pctx
->section
= uci_to_section(ptr
.last
);
460 * parse the 'option' uci command (open a value)
462 static void uci_parse_option(struct uci_context
*ctx
, bool list
)
464 struct uci_parse_context
*pctx
= ctx
->pctx
;
465 struct uci_element
*e
;
467 int ofs_name
, ofs_value
;
472 uci_parse_error(ctx
, "option/list command found before the first section");
474 /* command string null-terminated by strtok */
475 pctx
->pos
+= strlen(pctx_cur_str(pctx
)) + 1;
477 ofs_name
= next_arg(ctx
, true, true);
478 ofs_value
= next_arg(ctx
, false, false);
479 name
= pctx_str(pctx
, ofs_name
);
480 value
= pctx_str(pctx
, ofs_value
);
483 uci_fill_ptr(ctx
, &ptr
, &pctx
->section
->e
);
484 e
= uci_lookup_list(&pctx
->section
->options
, name
);
486 ptr
.o
= uci_to_option(e
);
490 ctx
->internal
= !pctx
->merge
;
492 UCI_NESTED(uci_add_list
, ctx
, &ptr
);
494 UCI_NESTED(uci_set
, ctx
, &ptr
);
498 * parse a complete input line, split up combined commands by ';'
500 static void uci_parse_line(struct uci_context
*ctx
, bool single
)
502 struct uci_parse_context
*pctx
= ctx
->pctx
;
505 /* Skip whitespace characters at the start of line */
506 skip_whitespace(ctx
);
508 word
= strtok(pctx_cur_str(pctx
), " \t");
517 if ((word
[1] == 0) || !strcmp(word
+ 1, "ackage"))
518 uci_parse_package(ctx
, single
);
523 if ((word
[1] == 0) || !strcmp(word
+ 1, "onfig"))
524 uci_parse_config(ctx
);
529 if ((word
[1] == 0) || !strcmp(word
+ 1, "ption"))
530 uci_parse_option(ctx
, false);
535 if ((word
[1] == 0) || !strcmp(word
+ 1, "ist"))
536 uci_parse_option(ctx
, true);
545 uci_parse_error(ctx
, "invalid command");
549 /* max number of characters that escaping adds to the string */
550 #define UCI_QUOTE_ESCAPE "'\\''"
553 * escape an uci string for export
555 static const char *uci_escape(struct uci_context
*ctx
, const char *str
)
561 ctx
->bufsz
= LINEBUF
;
562 ctx
->buf
= malloc(LINEBUF
);
571 end
= strchr(str
, '\'');
573 end
= str
+ strlen(str
);
576 /* make sure that we have enough room in the buffer */
577 while (ofs
+ len
+ sizeof(UCI_QUOTE_ESCAPE
) + 1 > ctx
->bufsz
) {
579 ctx
->buf
= uci_realloc(ctx
, ctx
->buf
, ctx
->bufsz
);
582 /* copy the string until the character before the quote */
583 memcpy(&ctx
->buf
[ofs
], str
, len
);
586 /* end of string? return the buffer */
590 memcpy(&ctx
->buf
[ofs
], UCI_QUOTE_ESCAPE
, sizeof(UCI_QUOTE_ESCAPE
));
591 ofs
+= strlen(&ctx
->buf
[ofs
]);
600 * export a single config package to a file stream
602 static void uci_export_package(struct uci_package
*p
, FILE *stream
, bool header
)
604 struct uci_context
*ctx
= p
->ctx
;
605 struct uci_element
*s
, *o
, *i
;
608 fprintf(stream
, "package %s\n", uci_escape(ctx
, p
->e
.name
));
609 uci_foreach_element(&p
->sections
, s
) {
610 struct uci_section
*sec
= uci_to_section(s
);
611 fprintf(stream
, "\nconfig %s", uci_escape(ctx
, sec
->type
));
612 if (!sec
->anonymous
|| (ctx
->flags
& UCI_FLAG_EXPORT_NAME
))
613 fprintf(stream
, " '%s'", uci_escape(ctx
, sec
->e
.name
));
614 fprintf(stream
, "\n");
615 uci_foreach_element(&sec
->options
, o
) {
616 struct uci_option
*opt
= uci_to_option(o
);
618 case UCI_TYPE_STRING
:
619 fprintf(stream
, "\toption %s", uci_escape(ctx
, opt
->e
.name
));
620 fprintf(stream
, " '%s'\n", uci_escape(ctx
, opt
->v
.string
));
623 uci_foreach_element(&opt
->v
.list
, i
) {
624 fprintf(stream
, "\tlist %s", uci_escape(ctx
, opt
->e
.name
));
625 fprintf(stream
, " '%s'\n", uci_escape(ctx
, i
->name
));
629 fprintf(stream
, "\t# unknown type for option '%s'\n", uci_escape(ctx
, opt
->e
.name
));
634 fprintf(stream
, "\n");
637 int uci_export(struct uci_context
*ctx
, FILE *stream
, struct uci_package
*package
, bool header
)
639 struct uci_element
*e
;
642 UCI_ASSERT(ctx
, stream
!= NULL
);
645 uci_export_package(package
, stream
, header
);
647 uci_foreach_element(&ctx
->root
, e
) {
648 uci_export_package(uci_to_package(e
), stream
, header
);
655 int uci_import(struct uci_context
*ctx
, FILE *stream
, const char *name
, struct uci_package
**package
, bool single
)
657 struct uci_parse_context
*pctx
;
660 /* make sure no memory from previous parse attempts is leaked */
663 uci_alloc_parse_context(ctx
);
666 if (package
&& *package
&& single
) {
667 pctx
->package
= *package
;
672 * If 'name' was supplied, assume that the supplied stream does not contain
673 * the appropriate 'package <name>' string to specify the config name
674 * NB: the config file can still override the package name
677 UCI_ASSERT(ctx
, uci_validate_package(name
));
681 while (!feof(pctx
->file
)) {
684 UCI_TRAP_SAVE(ctx
, error
);
686 uci_parse_line(ctx
, single
);
687 UCI_TRAP_RESTORE(ctx
);
690 if (ctx
->flags
& UCI_FLAG_PERROR
)
691 uci_perror(ctx
, NULL
);
692 if ((ctx
->err
!= UCI_ERR_PARSE
) ||
693 (ctx
->flags
& UCI_FLAG_STRICT
))
694 UCI_THROW(ctx
, ctx
->err
);
697 uci_fixup_section(ctx
, ctx
->pctx
->section
);
698 if (!pctx
->package
&& name
)
699 uci_switch_config(ctx
);
701 *package
= pctx
->package
;
703 pctx
->package
= NULL
;
706 uci_switch_config(ctx
);
708 /* no error happened, we can get rid of the parser context now */
715 static char *uci_config_path(struct uci_context
*ctx
, const char *name
)
719 UCI_ASSERT(ctx
, uci_validate_package(name
));
720 filename
= uci_malloc(ctx
, strlen(name
) + strlen(ctx
->confdir
) + 2);
721 sprintf(filename
, "%s/%s", ctx
->confdir
, name
);
726 static void uci_file_commit(struct uci_context
*ctx
, struct uci_package
**package
, bool overwrite
)
728 struct uci_package
*p
= *package
;
729 FILE *f1
, *f2
= NULL
;
732 char *filename
= NULL
;
734 bool do_rename
= false;
738 p
->path
= uci_config_path(ctx
, p
->e
.name
);
740 UCI_THROW(ctx
, UCI_ERR_INVAL
);
743 if ((asprintf(&filename
, "%s/.%s.uci-XXXXXX", ctx
->confdir
, p
->e
.name
) < 0) || !filename
)
744 UCI_THROW(ctx
, UCI_ERR_MEM
);
746 if (!mktemp(filename
))
751 UCI_THROW(ctx
, UCI_ERR_IO
);
754 if ((stat(filename
, &statbuf
) == 0) && ((statbuf
.st_mode
& S_IFMT
) != S_IFREG
))
755 UCI_THROW(ctx
, UCI_ERR_IO
);
757 /* open the config file for writing now, so that it is locked */
758 f1
= uci_open_stream(ctx
, p
->path
, NULL
, SEEK_SET
, true, true);
760 /* flush unsaved changes and reload from delta file */
761 UCI_TRAP_SAVE(ctx
, done
);
764 name
= uci_strdup(ctx
, p
->e
.name
);
765 path
= uci_strdup(ctx
, p
->path
);
766 /* dump our own changes to the delta file */
767 if (!uci_list_empty(&p
->delta
))
768 UCI_INTERNAL(uci_save
, ctx
, p
);
771 * other processes might have modified the config
772 * as well. dump and reload
774 uci_free_package(&p
);
776 UCI_INTERNAL(uci_import
, ctx
, f1
, name
, &p
, true);
782 /* freed together with the uci_package */
787 if (!uci_load_delta(ctx
, p
, true))
791 f2
= uci_open_stream(ctx
, filename
, p
->path
, SEEK_SET
, true, true);
792 uci_export(ctx
, f2
, p
, false);
796 uci_close_stream(f2
);
800 UCI_TRAP_RESTORE(ctx
);
805 uci_close_stream(f1
);
806 if (do_rename
&& rename(filename
, p
->path
)) {
808 UCI_THROW(ctx
, UCI_ERR_IO
);
813 UCI_THROW(ctx
, ctx
->err
);
818 * This function returns the filename by returning the string
819 * after the last '/' character. By checking for a non-'\0'
820 * character afterwards, directories are ignored (glob marks
821 * those with a trailing '/'
823 static inline char *get_filename(char *path
)
827 p
= strrchr(path
, '/');
834 static char **uci_list_config_files(struct uci_context
*ctx
)
842 dir
= uci_malloc(ctx
, strlen(ctx
->confdir
) + 1 + sizeof("/*"));
843 sprintf(dir
, "%s/*", ctx
->confdir
);
844 if (glob(dir
, GLOB_MARK
, NULL
, &globbuf
) != 0) {
846 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
849 size
= sizeof(char *) * (globbuf
.gl_pathc
+ 1);
850 for(i
= 0; i
< globbuf
.gl_pathc
; i
++) {
853 p
= get_filename(globbuf
.gl_pathv
[i
]);
857 size
+= strlen(p
) + 1;
860 configs
= uci_malloc(ctx
, size
);
861 buf
= (char *) &configs
[globbuf
.gl_pathc
+ 1];
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
,