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
);
74 UCI_THROW(ctx
, UCI_ERR_MEM
);
79 * parse a character escaped by '\'
80 * returns true if the escaped character is to be parsed
81 * returns false if the escaped character is to be ignored
83 static bool parse_backslash(struct uci_context
*ctx
)
85 struct uci_parse_context
*pctx
= ctx
->pctx
;
90 /* undecoded backslash at the end of line, fetch the next line */
91 if (!pctx_cur_char(pctx
) ||
92 pctx_cur_char(pctx
) == '\n' ||
93 (pctx_cur_char(pctx
) == '\r' &&
94 pctx_char(pctx
, pctx_pos(pctx
) + 1) == '\n' &&
95 !pctx_char(pctx
, pctx_pos(pctx
) + 2))) {
96 uci_getln(ctx
, pctx
->pos
);
100 /* FIXME: decode escaped char, necessary? */
105 * move the string pointer forward until a non-whitespace character or
108 static void skip_whitespace(struct uci_context
*ctx
)
110 struct uci_parse_context
*pctx
= ctx
->pctx
;
112 while (pctx_cur_char(pctx
) && isspace(pctx_cur_char(pctx
)))
116 static inline void addc(struct uci_context
*ctx
, int *pos_dest
, int *pos_src
)
118 struct uci_parse_context
*pctx
= ctx
->pctx
;
120 pctx_char(pctx
, *pos_dest
) = pctx_char(pctx
, *pos_src
);
126 * parse a double quoted string argument from the command line
128 static void parse_double_quote(struct uci_context
*ctx
, int *target
)
130 struct uci_parse_context
*pctx
= ctx
->pctx
;
133 /* skip quote character */
137 c
= pctx_cur_char(pctx
);
143 /* Multi-line str value */
144 uci_getln(ctx
, pctx
->pos
);
145 if (!pctx_cur_char(pctx
)) {
146 uci_parse_error(ctx
, "EOF with unterminated \"");
150 if (!parse_backslash(ctx
))
154 addc(ctx
, target
, &pctx
->pos
);
161 * parse a single quoted string argument from the command line
163 static void parse_single_quote(struct uci_context
*ctx
, int *target
)
165 struct uci_parse_context
*pctx
= ctx
->pctx
;
167 /* skip quote character */
171 c
= pctx_cur_char(pctx
);
177 /* Multi-line str value */
178 uci_getln(ctx
, pctx
->pos
);
179 if (!pctx_cur_char(pctx
))
180 uci_parse_error(ctx
, "EOF with unterminated '");
184 addc(ctx
, target
, &pctx
->pos
);
190 * parse a string from the command line and detect the quoting style
192 static void parse_str(struct uci_context
*ctx
, int *target
)
194 struct uci_parse_context
*pctx
= ctx
->pctx
;
197 switch(pctx_cur_char(pctx
)) {
199 parse_single_quote(ctx
, target
);
202 parse_double_quote(ctx
, target
);
205 pctx_cur_char(pctx
) = 0;
213 if (!parse_backslash(ctx
))
217 addc(ctx
, target
, &pctx
->pos
);
220 } while (pctx_cur_char(pctx
) && !isspace(pctx_cur_char(pctx
)));
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
228 if (pctx_cur_char(pctx
) && next
)
231 /* terminate the parsed string */
232 pctx_char(pctx
, *target
) = 0;
236 * extract the next argument from the command line
238 static int next_arg(struct uci_context
*ctx
, bool required
, bool name
, bool package
)
240 struct uci_parse_context
*pctx
= ctx
->pctx
;
243 skip_whitespace(ctx
);
244 val
= ptr
= pctx_pos(pctx
);
245 if (pctx_cur_char(pctx
) == ';') {
246 pctx_cur_char(pctx
) = 0;
249 parse_str(ctx
, &ptr
);
251 if (!pctx_char(pctx
, val
)) {
253 uci_parse_error(ctx
, "insufficient arguments");
257 if (name
&& !uci_validate_str(pctx_str(pctx
, val
), name
, package
))
258 uci_parse_error(ctx
, "invalid character in name field");
264 int uci_parse_argument(struct uci_context
*ctx
, FILE *stream
, char **str
, char **result
)
269 UCI_ASSERT(ctx
, str
!= NULL
);
270 UCI_ASSERT(ctx
, result
!= NULL
);
272 if (ctx
->pctx
&& (ctx
->pctx
->file
!= stream
))
276 uci_alloc_parse_context(ctx
);
278 ctx
->pctx
->file
= stream
;
284 ofs_result
= next_arg(ctx
, false, false, false);
285 *result
= pctx_str(ctx
->pctx
, ofs_result
);
286 *str
= pctx_cur_str(ctx
->pctx
);
292 * Fixup sections functions does the fixup of all sections for given package.
293 * It is used as a preprocessing step for fixing up existing anonymous sections
294 * from configurations.
296 * It uses uci_fixup_section() from list.c and then adds delta changes.
299 uci_fixup_sections(struct uci_context
*ctx
, struct uci_package
*p
)
301 struct uci_element
*e
;
302 struct uci_section
*s
;
304 uci_foreach_element(&p
->sections
, e
) {
305 s
= uci_to_section(e
);
306 s
->package
->name_index
++;
307 uci_fixup_section(ctx
, s
);
308 s
->anonymous
= false;
313 uci_fill_ptr(struct uci_context
*ctx
, struct uci_ptr
*ptr
, struct uci_element
*e
)
315 UCI_ASSERT(ctx
, ptr
!= NULL
);
316 UCI_ASSERT(ctx
, e
!= NULL
);
318 memset(ptr
, 0, sizeof(struct uci_ptr
));
320 case UCI_TYPE_OPTION
:
321 ptr
->o
= uci_to_option(e
);
323 case UCI_TYPE_SECTION
:
324 ptr
->s
= uci_to_section(e
);
326 case UCI_TYPE_PACKAGE
:
327 ptr
->p
= uci_to_package(e
);
330 UCI_THROW(ctx
, UCI_ERR_INVAL
);
334 ptr
->option
= ptr
->o
->e
.name
;
335 ptr
->s
= ptr
->o
->section
;
337 ptr
->section
= ptr
->s
->e
.name
;
338 ptr
->p
= ptr
->s
->package
;
340 ptr
->package
= ptr
->p
->e
.name
;
342 ptr
->flags
|= UCI_LOOKUP_DONE
;
350 * verify that the end of the line or command is reached.
351 * throw an error if extra arguments are given on the command line
353 static void assert_eol(struct uci_context
*ctx
)
358 skip_whitespace(ctx
);
359 ofs_tmp
= next_arg(ctx
, false, false, false);
360 tmp
= pctx_str(ctx
->pctx
, ofs_tmp
);
361 if (*tmp
&& (ctx
->flags
& UCI_FLAG_STRICT
))
362 uci_parse_error(ctx
, "too many arguments");
366 * switch to a different config, either triggered by uci_load, or by a
367 * 'package <...>' statement in the import file
369 static void uci_switch_config(struct uci_context
*ctx
)
371 struct uci_parse_context
*pctx
;
372 struct uci_element
*e
;
378 /* add the last config to main config file list */
380 pctx
->package
->backend
= ctx
->backend
;
381 uci_list_add(&ctx
->root
, &pctx
->package
->e
.list
);
383 pctx
->package
= NULL
;
384 pctx
->section
= NULL
;
391 * if an older config under the same name exists, unload it
392 * ignore errors here, e.g. if the config was not found
394 e
= uci_lookup_list(&ctx
->root
, name
);
396 UCI_THROW(ctx
, UCI_ERR_DUPLICATE
);
397 pctx
->package
= uci_alloc_package(ctx
, name
);
401 * parse the 'package' uci command (next config package)
403 static void uci_parse_package(struct uci_context
*ctx
, bool single
)
405 struct uci_parse_context
*pctx
= ctx
->pctx
;
409 /* command string null-terminated by strtok */
410 pctx
->pos
+= strlen(pctx_cur_str(pctx
)) + 1;
412 ofs_name
= next_arg(ctx
, true, true, true);
413 name
= pctx_str(pctx
, ofs_name
);
418 ctx
->pctx
->name
= name
;
419 uci_switch_config(ctx
);
423 * parse the 'config' uci command (open a section)
425 static void uci_parse_config(struct uci_context
*ctx
)
427 struct uci_parse_context
*pctx
= ctx
->pctx
;
428 struct uci_element
*e
;
430 int ofs_name
, ofs_type
;
434 if (!ctx
->pctx
->package
) {
435 if (!ctx
->pctx
->name
)
436 uci_parse_error(ctx
, "attempting to import a file without a package name");
438 uci_switch_config(ctx
);
441 /* command string null-terminated by strtok */
442 pctx
->pos
+= strlen(pctx_cur_str(pctx
)) + 1;
444 ofs_type
= next_arg(ctx
, true, false, false);
445 type
= pctx_str(pctx
, ofs_type
);
446 if (!uci_validate_type(type
))
447 uci_parse_error(ctx
, "invalid character in type field");
449 ofs_name
= next_arg(ctx
, false, true, false);
450 type
= pctx_str(pctx
, ofs_type
);
451 name
= pctx_str(pctx
, ofs_name
);
454 if (!name
|| !name
[0]) {
455 ctx
->internal
= !pctx
->merge
;
456 UCI_NESTED(uci_add_section
, ctx
, pctx
->package
, type
, &pctx
->section
);
458 uci_fill_ptr(ctx
, &ptr
, &pctx
->package
->e
);
459 e
= uci_lookup_list(&pctx
->package
->sections
, name
);
461 ptr
.s
= uci_to_section(e
);
463 if ((ctx
->flags
& UCI_FLAG_STRICT
) && strcmp(ptr
.s
->type
, type
))
464 uci_parse_error(ctx
, "section of different type overwrites prior section with same name");
470 ctx
->internal
= !pctx
->merge
;
471 UCI_NESTED(uci_set
, ctx
, &ptr
);
472 pctx
->section
= uci_to_section(ptr
.last
);
477 * parse the 'option' uci command (open a value)
479 static void uci_parse_option(struct uci_context
*ctx
, bool list
)
481 struct uci_parse_context
*pctx
= ctx
->pctx
;
482 struct uci_element
*e
;
484 int ofs_name
, ofs_value
;
489 uci_parse_error(ctx
, "option/list command found before the first section");
491 /* command string null-terminated by strtok */
492 pctx
->pos
+= strlen(pctx_cur_str(pctx
)) + 1;
494 ofs_name
= next_arg(ctx
, true, true, false);
495 ofs_value
= next_arg(ctx
, false, false, false);
496 name
= pctx_str(pctx
, ofs_name
);
497 value
= pctx_str(pctx
, ofs_value
);
500 uci_fill_ptr(ctx
, &ptr
, &pctx
->section
->e
);
501 e
= uci_lookup_list(&pctx
->section
->options
, name
);
503 ptr
.o
= uci_to_option(e
);
507 ctx
->internal
= !pctx
->merge
;
509 UCI_NESTED(uci_add_list
, ctx
, &ptr
);
511 UCI_NESTED(uci_set
, ctx
, &ptr
);
515 * parse a complete input line, split up combined commands by ';'
517 static void uci_parse_line(struct uci_context
*ctx
, bool single
)
519 struct uci_parse_context
*pctx
= ctx
->pctx
;
522 /* Skip whitespace characters at the start of line */
523 skip_whitespace(ctx
);
525 word
= strtok(pctx_cur_str(pctx
), " \t");
534 if ((word
[1] == 0) || !strcmp(word
+ 1, "ackage"))
535 uci_parse_package(ctx
, single
);
540 if ((word
[1] == 0) || !strcmp(word
+ 1, "onfig"))
541 uci_parse_config(ctx
);
546 if ((word
[1] == 0) || !strcmp(word
+ 1, "ption"))
547 uci_parse_option(ctx
, false);
552 if ((word
[1] == 0) || !strcmp(word
+ 1, "ist"))
553 uci_parse_option(ctx
, true);
562 uci_parse_error(ctx
, "invalid command");
566 /* max number of characters that escaping adds to the string */
567 #define UCI_QUOTE_ESCAPE "'\\''"
570 * escape an uci string for export
572 static const char *uci_escape(struct uci_context
*ctx
, const char *str
)
578 ctx
->bufsz
= LINEBUF
;
579 ctx
->buf
= malloc(LINEBUF
);
588 end
= strchr(str
, '\'');
590 end
= str
+ strlen(str
);
593 /* make sure that we have enough room in the buffer */
594 while (ofs
+ len
+ sizeof(UCI_QUOTE_ESCAPE
) + 1 > ctx
->bufsz
) {
596 ctx
->buf
= uci_realloc(ctx
, ctx
->buf
, ctx
->bufsz
);
599 /* copy the string until the character before the quote */
600 memcpy(&ctx
->buf
[ofs
], str
, len
);
603 /* end of string? return the buffer */
607 memcpy(&ctx
->buf
[ofs
], UCI_QUOTE_ESCAPE
, sizeof(UCI_QUOTE_ESCAPE
));
608 ofs
+= strlen(&ctx
->buf
[ofs
]);
617 * export a single config package to a file stream
619 static void uci_export_package(struct uci_package
*p
, FILE *stream
, bool header
)
621 struct uci_context
*ctx
= p
->ctx
;
622 struct uci_element
*s
, *o
, *i
;
625 fprintf(stream
, "package %s\n", uci_escape(ctx
, p
->e
.name
));
626 uci_foreach_element(&p
->sections
, s
) {
627 struct uci_section
*sec
= uci_to_section(s
);
628 fprintf(stream
, "\nconfig %s", uci_escape(ctx
, sec
->type
));
629 fprintf(stream
, " '%s'", uci_escape(ctx
, sec
->e
.name
));
630 fprintf(stream
, "\n");
631 uci_foreach_element(&sec
->options
, o
) {
632 struct uci_option
*opt
= uci_to_option(o
);
634 case UCI_TYPE_STRING
:
635 fprintf(stream
, "\toption %s", uci_escape(ctx
, opt
->e
.name
));
636 fprintf(stream
, " '%s'\n", uci_escape(ctx
, opt
->v
.string
));
639 uci_foreach_element(&opt
->v
.list
, i
) {
640 fprintf(stream
, "\tlist %s", uci_escape(ctx
, opt
->e
.name
));
641 fprintf(stream
, " '%s'\n", uci_escape(ctx
, i
->name
));
645 fprintf(stream
, "\t# unknown type for option '%s'\n", uci_escape(ctx
, opt
->e
.name
));
650 fprintf(stream
, "\n");
653 int uci_export(struct uci_context
*ctx
, FILE *stream
, struct uci_package
*package
, bool header
)
655 struct uci_element
*e
;
658 UCI_ASSERT(ctx
, stream
!= NULL
);
661 uci_export_package(package
, stream
, header
);
663 uci_foreach_element(&ctx
->root
, e
) {
664 uci_export_package(uci_to_package(e
), stream
, header
);
671 int uci_import(struct uci_context
*ctx
, FILE *stream
, const char *name
, struct uci_package
**package
, bool single
)
673 struct uci_parse_context
*pctx
;
676 /* make sure no memory from previous parse attempts is leaked */
679 uci_alloc_parse_context(ctx
);
682 if (package
&& *package
&& single
) {
683 pctx
->package
= *package
;
688 * If 'name' was supplied, assume that the supplied stream does not contain
689 * the appropriate 'package <name>' string to specify the config name
690 * NB: the config file can still override the package name
693 UCI_ASSERT(ctx
, uci_validate_package(name
));
697 while (!feof(pctx
->file
)) {
700 UCI_TRAP_SAVE(ctx
, error
);
702 uci_parse_line(ctx
, single
);
703 UCI_TRAP_RESTORE(ctx
);
706 if (ctx
->flags
& UCI_FLAG_PERROR
)
707 uci_perror(ctx
, NULL
);
708 if ((ctx
->err
!= UCI_ERR_PARSE
) ||
709 (ctx
->flags
& UCI_FLAG_STRICT
))
710 UCI_THROW(ctx
, ctx
->err
);
713 if (!pctx
->package
&& name
)
714 uci_switch_config(ctx
);
716 *package
= pctx
->package
;
718 pctx
->package
= NULL
;
720 uci_fixup_sections(ctx
, *package
);
722 uci_switch_config(ctx
);
724 /* no error happened, we can get rid of the parser context now */
731 static char *uci_config_path(struct uci_context
*ctx
, const char *name
)
735 UCI_ASSERT(ctx
, uci_validate_package(name
));
736 filename
= uci_malloc(ctx
, strlen(name
) + strlen(ctx
->confdir
) + 2);
737 sprintf(filename
, "%s/%s", ctx
->confdir
, name
);
742 static void uci_file_commit(struct uci_context
*ctx
, struct uci_package
**package
, bool overwrite
)
744 struct uci_package
*p
= *package
;
745 FILE *f1
, *f2
= NULL
;
748 char *filename
= NULL
;
750 bool do_rename
= false;
754 p
->path
= uci_config_path(ctx
, p
->e
.name
);
756 UCI_THROW(ctx
, UCI_ERR_INVAL
);
759 if ((asprintf(&filename
, "%s/.%s.uci-XXXXXX", ctx
->confdir
, p
->e
.name
) < 0) || !filename
)
760 UCI_THROW(ctx
, UCI_ERR_MEM
);
762 /* open the config file for writing now, so that it is locked */
763 f1
= uci_open_stream(ctx
, p
->path
, NULL
, SEEK_SET
, true, true);
765 /* flush unsaved changes and reload from delta file */
766 UCI_TRAP_SAVE(ctx
, done
);
769 name
= uci_strdup(ctx
, p
->e
.name
);
770 path
= uci_strdup(ctx
, p
->path
);
771 /* dump our own changes to the delta file */
772 if (!uci_list_empty(&p
->delta
))
773 UCI_INTERNAL(uci_save
, ctx
, p
);
776 * other processes might have modified the config
777 * as well. dump and reload
779 uci_free_package(&p
);
781 UCI_INTERNAL(uci_import
, ctx
, f1
, name
, &p
, true);
787 /* freed together with the uci_package */
792 if (!uci_load_delta(ctx
, p
, true))
796 if (!mktemp(filename
))
801 UCI_THROW(ctx
, UCI_ERR_IO
);
804 if ((stat(filename
, &statbuf
) == 0) && ((statbuf
.st_mode
& S_IFMT
) != S_IFREG
))
805 UCI_THROW(ctx
, UCI_ERR_IO
);
807 f2
= uci_open_stream(ctx
, filename
, p
->path
, SEEK_SET
, true, true);
808 uci_export(ctx
, f2
, p
, false);
812 uci_close_stream(f2
);
816 UCI_TRAP_RESTORE(ctx
);
821 uci_close_stream(f1
);
823 path
= realpath(p
->path
, NULL
);
824 if (!path
|| rename(filename
, path
)) {
826 UCI_THROW(ctx
, UCI_ERR_IO
);
832 UCI_THROW(ctx
, ctx
->err
);
837 * This function returns the filename by returning the string
838 * after the last '/' character. By checking for a non-'\0'
839 * character afterwards, directories are ignored (glob marks
840 * those with a trailing '/'
842 static inline char *get_filename(char *path
)
846 p
= strrchr(path
, '/');
853 static char **uci_list_config_files(struct uci_context
*ctx
)
861 dir
= uci_malloc(ctx
, strlen(ctx
->confdir
) + 1 + sizeof("/*"));
862 sprintf(dir
, "%s/*", ctx
->confdir
);
863 if (glob(dir
, GLOB_MARK
, NULL
, &globbuf
) != 0) {
865 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
868 size
= sizeof(char *) * (globbuf
.gl_pathc
+ 1);
869 for(i
= 0; i
< globbuf
.gl_pathc
; i
++) {
872 p
= get_filename(globbuf
.gl_pathv
[i
]);
876 size
+= strlen(p
) + 1;
879 configs
= uci_malloc(ctx
, size
);
880 buf
= (char *) &configs
[globbuf
.gl_pathc
+ 1];
881 for(i
= 0; i
< globbuf
.gl_pathc
; i
++) {
884 p
= get_filename(globbuf
.gl_pathv
[i
]);
888 if (!uci_validate_package(p
))
893 buf
+= strlen(buf
) + 1;
900 static struct uci_package
*uci_file_load(struct uci_context
*ctx
, const char *name
)
902 struct uci_package
*package
= NULL
;
910 /* relative path outside of /etc/config */
912 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
915 /* absolute path outside of /etc/config */
916 filename
= uci_strdup(ctx
, name
);
917 name
= strrchr(name
, '/') + 1;
921 /* config in /etc/config */
922 filename
= uci_config_path(ctx
, name
);
927 UCI_TRAP_SAVE(ctx
, done
);
928 file
= uci_open_stream(ctx
, filename
, NULL
, SEEK_SET
, false, false);
930 UCI_INTERNAL(uci_import
, ctx
, file
, name
, &package
, true);
931 UCI_TRAP_RESTORE(ctx
);
934 package
->path
= filename
;
935 package
->has_delta
= confdir
;
936 n_change
= uci_load_delta(ctx
, package
, false);
937 package
->name_index
+= n_change
;
941 uci_close_stream(file
);
944 UCI_THROW(ctx
, ctx
->err
);
949 __private
UCI_BACKEND(uci_file_backend
, "file",
950 .load
= uci_file_load
,
951 .commit
= uci_file_commit
,
952 .list_configs
= uci_list_config_files
,