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>
34 #include "uci_internal.h"
39 * Fetch a new line from the input stream and resize buffer if necessary
41 __private
void uci_getln(struct uci_context
*ctx
, size_t 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 pctx
->buf_filled
= ofs
;
68 if (pctx
->buf
[ofs
- 1] == '\n') {
74 pctx
->buf
= uci_realloc(ctx
, pctx
->buf
, pctx
->bufsz
);
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
, size_t *pos_dest
, size_t *pos_src
)
118 struct uci_parse_context
*pctx
= ctx
->pctx
;
120 pctx_char(pctx
, *pos_dest
) = pctx_char(pctx
, *pos_src
);
125 static int uci_increase_pos(struct uci_parse_context
*pctx
, size_t add
)
127 if (pctx
->pos
+ add
> pctx
->buf_filled
)
135 * parse a double quoted string argument from the command line
137 static void parse_double_quote(struct uci_context
*ctx
, size_t *target
)
139 struct uci_parse_context
*pctx
= ctx
->pctx
;
142 /* skip quote character */
146 c
= pctx_cur_char(pctx
);
152 /* Multi-line str value */
153 uci_getln(ctx
, pctx
->pos
);
154 if (!pctx_cur_char(pctx
)) {
155 uci_parse_error(ctx
, "EOF with unterminated \"");
159 if (!parse_backslash(ctx
))
163 addc(ctx
, target
, &pctx
->pos
);
170 * parse a single quoted string argument from the command line
172 static void parse_single_quote(struct uci_context
*ctx
, size_t *target
)
174 struct uci_parse_context
*pctx
= ctx
->pctx
;
176 /* skip quote character */
180 c
= pctx_cur_char(pctx
);
186 /* Multi-line str value */
187 uci_getln(ctx
, pctx
->pos
);
188 if (!pctx_cur_char(pctx
))
189 uci_parse_error(ctx
, "EOF with unterminated '");
193 addc(ctx
, target
, &pctx
->pos
);
199 * parse a string from the command line and detect the quoting style
201 static void parse_str(struct uci_context
*ctx
, size_t *target
)
203 struct uci_parse_context
*pctx
= ctx
->pctx
;
206 switch(pctx_cur_char(pctx
)) {
208 parse_single_quote(ctx
, target
);
211 parse_double_quote(ctx
, target
);
214 pctx_cur_char(pctx
) = 0;
222 if (!parse_backslash(ctx
))
226 addc(ctx
, target
, &pctx
->pos
);
229 } while (pctx_cur_char(pctx
) && !isspace(pctx_cur_char(pctx
)));
233 * if the string was unquoted and we've stopped at a whitespace
234 * character, skip to the next one, because the whitespace will
235 * be overwritten by a null byte here
237 if (pctx_cur_char(pctx
) && next
)
240 /* terminate the parsed string */
241 pctx_char(pctx
, *target
) = 0;
245 * extract the next argument from the command line
247 static int next_arg(struct uci_context
*ctx
, bool required
, bool name
, bool package
)
249 struct uci_parse_context
*pctx
= ctx
->pctx
;
252 skip_whitespace(ctx
);
253 val
= ptr
= pctx_pos(pctx
);
254 if (pctx_cur_char(pctx
) == ';') {
255 pctx_cur_char(pctx
) = 0;
258 parse_str(ctx
, &ptr
);
260 if (!pctx_char(pctx
, val
)) {
262 uci_parse_error(ctx
, "insufficient arguments");
266 if (name
&& !uci_validate_str(pctx_str(pctx
, val
), name
, package
))
267 uci_parse_error(ctx
, "invalid character in name field");
273 int uci_parse_argument(struct uci_context
*ctx
, FILE *stream
, char **str
, char **result
)
278 UCI_ASSERT(ctx
, str
!= NULL
);
279 UCI_ASSERT(ctx
, result
!= NULL
);
281 if (ctx
->pctx
&& (ctx
->pctx
->file
!= stream
))
285 uci_alloc_parse_context(ctx
);
287 ctx
->pctx
->file
= stream
;
293 ofs_result
= next_arg(ctx
, false, false, false);
294 *result
= pctx_str(ctx
->pctx
, ofs_result
);
295 *str
= pctx_cur_str(ctx
->pctx
);
301 uci_fill_ptr(struct uci_context
*ctx
, struct uci_ptr
*ptr
, struct uci_element
*e
)
303 UCI_ASSERT(ctx
, ptr
!= NULL
);
304 UCI_ASSERT(ctx
, e
!= NULL
);
306 memset(ptr
, 0, sizeof(struct uci_ptr
));
308 case UCI_TYPE_OPTION
:
309 ptr
->o
= uci_to_option(e
);
311 case UCI_TYPE_SECTION
:
312 ptr
->s
= uci_to_section(e
);
314 case UCI_TYPE_PACKAGE
:
315 ptr
->p
= uci_to_package(e
);
318 UCI_THROW(ctx
, UCI_ERR_INVAL
);
322 ptr
->option
= ptr
->o
->e
.name
;
323 ptr
->s
= ptr
->o
->section
;
325 ptr
->section
= ptr
->s
->e
.name
;
326 ptr
->p
= ptr
->s
->package
;
328 ptr
->package
= ptr
->p
->e
.name
;
330 ptr
->flags
|= UCI_LOOKUP_DONE
;
338 * verify that the end of the line or command is reached.
339 * throw an error if extra arguments are given on the command line
341 static void assert_eol(struct uci_context
*ctx
)
346 skip_whitespace(ctx
);
347 ofs_tmp
= next_arg(ctx
, false, false, false);
348 tmp
= pctx_str(ctx
->pctx
, ofs_tmp
);
349 if (*tmp
&& (ctx
->flags
& UCI_FLAG_STRICT
))
350 uci_parse_error(ctx
, "too many arguments");
354 * switch to a different config, either triggered by uci_load, or by a
355 * 'package <...>' statement in the import file
357 static void uci_switch_config(struct uci_context
*ctx
)
359 struct uci_parse_context
*pctx
;
360 struct uci_element
*e
;
366 /* add the last config to main config file list */
368 pctx
->package
->backend
= ctx
->backend
;
369 uci_list_add(&ctx
->root
, &pctx
->package
->e
.list
);
371 pctx
->package
= NULL
;
372 pctx
->section
= NULL
;
379 * if an older config under the same name exists, unload it
380 * ignore errors here, e.g. if the config was not found
382 e
= uci_lookup_list(&ctx
->root
, name
);
384 UCI_THROW(ctx
, UCI_ERR_DUPLICATE
);
385 pctx
->package
= uci_alloc_package(ctx
, name
);
389 * parse the 'package' uci command (next config package)
391 static void uci_parse_package(struct uci_context
*ctx
, bool single
)
393 struct uci_parse_context
*pctx
= ctx
->pctx
;
397 /* command string null-terminated by strtok */
398 if (uci_increase_pos(pctx
, strlen(pctx_cur_str(pctx
)) + 1))
399 uci_parse_error(ctx
, "package without name");
401 ofs_name
= next_arg(ctx
, true, true, true);
403 name
= pctx_str(pctx
, ofs_name
);
407 ctx
->pctx
->name
= name
;
408 uci_switch_config(ctx
);
412 * parse the 'config' uci command (open a section)
414 static void uci_parse_config(struct uci_context
*ctx
)
416 struct uci_parse_context
*pctx
= ctx
->pctx
;
417 struct uci_element
*e
;
419 int ofs_name
, ofs_type
;
423 if (!ctx
->pctx
->package
) {
424 if (!ctx
->pctx
->name
)
425 uci_parse_error(ctx
, "attempting to import a file without a package name");
427 uci_switch_config(ctx
);
430 /* command string null-terminated by strtok */
431 if (uci_increase_pos(pctx
, strlen(pctx_cur_str(pctx
)) + 1))
432 uci_parse_error(ctx
, "config without name");
434 ofs_type
= next_arg(ctx
, true, false, false);
435 type
= pctx_str(pctx
, ofs_type
);
436 if (!uci_validate_type(type
))
437 uci_parse_error(ctx
, "invalid character in type field");
439 ofs_name
= next_arg(ctx
, false, true, false);
441 type
= pctx_str(pctx
, ofs_type
);
442 name
= pctx_str(pctx
, ofs_name
);
444 if (!name
|| !name
[0]) {
445 ctx
->internal
= !pctx
->merge
;
446 UCI_NESTED(uci_add_section
, ctx
, pctx
->package
, type
, &pctx
->section
);
448 uci_fill_ptr(ctx
, &ptr
, &pctx
->package
->e
);
449 e
= uci_lookup_list(&pctx
->package
->sections
, name
);
451 ptr
.s
= uci_to_section(e
);
453 if ((ctx
->flags
& UCI_FLAG_STRICT
) && strcmp(ptr
.s
->type
, type
))
454 uci_parse_error(ctx
, "section of different type overwrites prior section with same name");
460 ctx
->internal
= !pctx
->merge
;
461 UCI_NESTED(uci_set
, ctx
, &ptr
);
462 pctx
->section
= uci_to_section(ptr
.last
);
467 * parse the 'option' uci command (open a value)
469 static void uci_parse_option(struct uci_context
*ctx
, bool list
)
471 struct uci_parse_context
*pctx
= ctx
->pctx
;
472 struct uci_element
*e
;
474 int ofs_name
, ofs_value
;
479 uci_parse_error(ctx
, "option/list command found before the first section");
481 /* command string null-terminated by strtok */
482 if (uci_increase_pos(pctx
, strlen(pctx_cur_str(pctx
)) + 1))
483 uci_parse_error(ctx
, "option without name");
485 ofs_name
= next_arg(ctx
, true, true, false);
486 ofs_value
= next_arg(ctx
, false, false, false);
488 name
= pctx_str(pctx
, ofs_name
);
489 value
= pctx_str(pctx
, ofs_value
);
491 uci_fill_ptr(ctx
, &ptr
, &pctx
->section
->e
);
492 e
= uci_lookup_list(&pctx
->section
->options
, name
);
494 ptr
.o
= uci_to_option(e
);
498 ctx
->internal
= !pctx
->merge
;
500 UCI_NESTED(uci_add_list
, ctx
, &ptr
);
502 UCI_NESTED(uci_set
, ctx
, &ptr
);
506 * parse a complete input line, split up combined commands by ';'
508 static void uci_parse_line(struct uci_context
*ctx
, bool single
)
510 struct uci_parse_context
*pctx
= ctx
->pctx
;
513 /* Skip whitespace characters at the start of line */
514 skip_whitespace(ctx
);
516 word
= strtok(pctx_cur_str(pctx
), " \t");
525 if ((word
[1] == 0) || !strcmp(word
+ 1, "ackage"))
526 uci_parse_package(ctx
, single
);
531 if ((word
[1] == 0) || !strcmp(word
+ 1, "onfig"))
532 uci_parse_config(ctx
);
537 if ((word
[1] == 0) || !strcmp(word
+ 1, "ption"))
538 uci_parse_option(ctx
, false);
543 if ((word
[1] == 0) || !strcmp(word
+ 1, "ist"))
544 uci_parse_option(ctx
, true);
553 uci_parse_error(ctx
, "invalid command");
557 /* max number of characters that escaping adds to the string */
558 #define UCI_QUOTE_ESCAPE "'\\''"
561 * escape an uci string for export
563 static const char *uci_escape(struct uci_context
*ctx
, const char *str
)
569 ctx
->bufsz
= LINEBUF
;
570 ctx
->buf
= malloc(LINEBUF
);
579 end
= strchr(str
, '\'');
581 end
= str
+ strlen(str
);
584 /* make sure that we have enough room in the buffer */
585 while (ofs
+ len
+ (int) sizeof(UCI_QUOTE_ESCAPE
) + 1 > ctx
->bufsz
) {
587 ctx
->buf
= uci_realloc(ctx
, ctx
->buf
, ctx
->bufsz
);
590 /* copy the string until the character before the quote */
591 memcpy(&ctx
->buf
[ofs
], str
, len
);
594 /* end of string? return the buffer */
598 memcpy(&ctx
->buf
[ofs
], UCI_QUOTE_ESCAPE
, sizeof(UCI_QUOTE_ESCAPE
));
599 ofs
+= strlen(&ctx
->buf
[ofs
]);
608 * export a single config package to a file stream
610 static void uci_export_package(struct uci_package
*p
, FILE *stream
, bool header
)
612 struct uci_context
*ctx
= p
->ctx
;
613 struct uci_element
*s
, *o
, *i
;
616 fprintf(stream
, "package %s\n", uci_escape(ctx
, p
->e
.name
));
617 uci_foreach_element(&p
->sections
, s
) {
618 struct uci_section
*sec
= uci_to_section(s
);
619 fprintf(stream
, "\nconfig %s", uci_escape(ctx
, sec
->type
));
620 if (!sec
->anonymous
|| (ctx
->flags
& UCI_FLAG_EXPORT_NAME
))
621 fprintf(stream
, " '%s'", uci_escape(ctx
, sec
->e
.name
));
622 fprintf(stream
, "\n");
623 uci_foreach_element(&sec
->options
, o
) {
624 struct uci_option
*opt
= uci_to_option(o
);
626 case UCI_TYPE_STRING
:
627 fprintf(stream
, "\toption %s", uci_escape(ctx
, opt
->e
.name
));
628 fprintf(stream
, " '%s'\n", uci_escape(ctx
, opt
->v
.string
));
631 uci_foreach_element(&opt
->v
.list
, i
) {
632 fprintf(stream
, "\tlist %s", uci_escape(ctx
, opt
->e
.name
));
633 fprintf(stream
, " '%s'\n", uci_escape(ctx
, i
->name
));
637 fprintf(stream
, "\t# unknown type for option '%s'\n", uci_escape(ctx
, opt
->e
.name
));
642 fprintf(stream
, "\n");
645 int uci_export(struct uci_context
*ctx
, FILE *stream
, struct uci_package
*package
, bool header
)
647 struct uci_element
*e
;
650 UCI_ASSERT(ctx
, stream
!= NULL
);
653 uci_export_package(package
, stream
, header
);
655 uci_foreach_element(&ctx
->root
, e
) {
656 uci_export_package(uci_to_package(e
), stream
, header
);
663 int uci_import(struct uci_context
*ctx
, FILE *stream
, const char *name
, struct uci_package
**package
, bool single
)
665 struct uci_parse_context
*pctx
;
668 /* make sure no memory from previous parse attempts is leaked */
671 uci_alloc_parse_context(ctx
);
674 if (package
&& *package
&& single
) {
675 pctx
->package
= *package
;
680 * If 'name' was supplied, assume that the supplied stream does not contain
681 * the appropriate 'package <name>' string to specify the config name
682 * NB: the config file can still override the package name
685 UCI_ASSERT(ctx
, uci_validate_package(name
));
689 while (!feof(pctx
->file
)) {
692 UCI_TRAP_SAVE(ctx
, error
);
694 uci_parse_line(ctx
, single
);
695 UCI_TRAP_RESTORE(ctx
);
698 if (ctx
->flags
& UCI_FLAG_PERROR
)
699 uci_perror(ctx
, NULL
);
700 if ((ctx
->err
!= UCI_ERR_PARSE
) ||
701 (ctx
->flags
& UCI_FLAG_STRICT
))
702 UCI_THROW(ctx
, ctx
->err
);
705 if (!pctx
->package
&& name
)
706 uci_switch_config(ctx
);
708 *package
= pctx
->package
;
710 pctx
->package
= NULL
;
713 uci_switch_config(ctx
);
715 /* no error happened, we can get rid of the parser context now */
722 static char *uci_config_path(struct uci_context
*ctx
, const char *name
)
726 UCI_ASSERT(ctx
, uci_validate_package(name
));
727 filename
= uci_malloc(ctx
, strlen(name
) + strlen(ctx
->confdir
) + 2);
728 sprintf(filename
, "%s/%s", ctx
->confdir
, name
);
733 static void uci_file_commit(struct uci_context
*ctx
, struct uci_package
**package
, bool overwrite
)
735 struct uci_package
*p
= *package
;
736 FILE *f1
, *f2
= NULL
;
737 char *volatile name
= NULL
;
738 char *volatile path
= NULL
;
739 char *filename
= NULL
;
741 volatile bool do_rename
= false;
746 p
->path
= uci_config_path(ctx
, p
->e
.name
);
748 UCI_THROW(ctx
, UCI_ERR_INVAL
);
751 sz
= snprintf(NULL
, 0, "%s/.%s.uci-XXXXXX", ctx
->confdir
, p
->e
.name
);
752 filename
= alloca(sz
+ 1);
753 snprintf(filename
, sz
+ 1, "%s/.%s.uci-XXXXXX", ctx
->confdir
, p
->e
.name
);
755 /* open the config file for writing now, so that it is locked */
756 f1
= uci_open_stream(ctx
, p
->path
, NULL
, SEEK_SET
, true, true);
758 /* flush unsaved changes and reload from delta file */
759 UCI_TRAP_SAVE(ctx
, done
);
762 name
= uci_strdup(ctx
, p
->e
.name
);
763 path
= uci_strdup(ctx
, p
->path
);
764 /* dump our own changes to the delta file */
765 if (!uci_list_empty(&p
->delta
))
766 UCI_INTERNAL(uci_save
, ctx
, p
);
769 * other processes might have modified the config
770 * as well. dump and reload
772 uci_free_package(&p
);
774 UCI_INTERNAL(uci_import
, ctx
, f1
, name
, &p
, true);
780 /* freed together with the uci_package */
785 if (!uci_load_delta(ctx
, p
, true))
789 fd
= mkstemp(filename
);
791 UCI_THROW(ctx
, UCI_ERR_IO
);
793 if ((flock(fd
, LOCK_EX
) < 0) && (errno
!= ENOSYS
))
794 UCI_THROW(ctx
, UCI_ERR_IO
);
796 if (lseek(fd
, 0, SEEK_SET
) < 0)
797 UCI_THROW(ctx
, UCI_ERR_IO
);
799 f2
= fdopen(fd
, "w+");
801 UCI_THROW(ctx
, UCI_ERR_IO
);
803 uci_export(ctx
, f2
, p
, false);
807 uci_close_stream(f2
);
811 UCI_TRAP_RESTORE(ctx
);
816 uci_close_stream(f1
);
818 path
= realpath(p
->path
, NULL
);
819 if (!path
|| stat(path
, &statbuf
) || chmod(filename
, statbuf
.st_mode
) || rename(filename
, path
)) {
821 UCI_THROW(ctx
, UCI_ERR_IO
);
826 UCI_THROW(ctx
, ctx
->err
);
831 * This function returns the filename by returning the string
832 * after the last '/' character. By checking for a non-'\0'
833 * character afterwards, directories are ignored (glob marks
834 * those with a trailing '/'
836 static inline char *get_filename(char *path
)
840 p
= strrchr(path
, '/');
847 static char **uci_list_config_files(struct uci_context
*ctx
)
851 int size
, j
, skipped
;
856 dir
= uci_malloc(ctx
, strlen(ctx
->confdir
) + 1 + sizeof("/*"));
857 sprintf(dir
, "%s/*", ctx
->confdir
);
858 if (glob(dir
, GLOB_MARK
, NULL
, &globbuf
) != 0) {
860 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
863 size
= sizeof(char *) * (globbuf
.gl_pathc
+ 1);
865 for(i
= 0; i
< globbuf
.gl_pathc
; i
++) {
868 p
= get_filename(globbuf
.gl_pathv
[i
]);
874 size
+= strlen(p
) + 1;
877 configs
= uci_malloc(ctx
, size
- skipped
);
878 buf
= (char *) &configs
[globbuf
.gl_pathc
+ 1 - skipped
];
880 for(i
= 0; i
< globbuf
.gl_pathc
; i
++) {
883 p
= get_filename(globbuf
.gl_pathv
[i
]);
887 if (!uci_validate_package(p
))
892 buf
+= strlen(buf
) + 1;
899 static struct uci_package
*uci_file_load(struct uci_context
*ctx
,
900 const char *volatile name
)
902 struct uci_package
*package
= NULL
;
905 FILE *volatile file
= NULL
;
909 /* relative path outside of /etc/config */
911 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
914 /* absolute path outside of /etc/config */
915 filename
= uci_strdup(ctx
, name
);
916 name
= strrchr(name
, '/') + 1;
920 /* config in /etc/config */
921 filename
= uci_config_path(ctx
, name
);
926 UCI_TRAP_SAVE(ctx
, done
);
927 file
= uci_open_stream(ctx
, filename
, NULL
, SEEK_SET
, false, false);
929 UCI_INTERNAL(uci_import
, ctx
, file
, name
, &package
, true);
930 UCI_TRAP_RESTORE(ctx
);
933 package
->path
= filename
;
934 package
->has_delta
= confdir
;
935 uci_load_delta(ctx
, package
, false);
939 uci_close_stream(file
);
942 UCI_THROW(ctx
, ctx
->err
);
947 __private
UCI_BACKEND(uci_file_backend
, "file",
948 .load
= uci_file_load
,
949 .commit
= uci_file_commit
,
950 .list_configs
= uci_list_config_files
,