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 /* `offset' may off by one */
52 if (offset
>= pctx
->bufsz
) {
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 char *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");
265 return pctx_str(pctx
, val
);
268 int uci_parse_argument(struct uci_context
*ctx
, FILE *stream
, char **str
, char **result
)
271 UCI_ASSERT(ctx
, str
!= NULL
);
272 UCI_ASSERT(ctx
, result
!= NULL
);
274 if (ctx
->pctx
&& (ctx
->pctx
->file
!= stream
))
278 uci_alloc_parse_context(ctx
);
280 ctx
->pctx
->file
= stream
;
284 *str
= ctx
->pctx
->buf
;
286 UCI_ASSERT(ctx
, ctx
->pctx
->pos
== *str
- ctx
->pctx
->buf
);
289 *result
= next_arg(ctx
, false, false);
295 uci_fill_ptr(struct uci_context
*ctx
, struct uci_ptr
*ptr
, struct uci_element
*e
)
297 UCI_ASSERT(ctx
, ptr
!= NULL
);
298 UCI_ASSERT(ctx
, e
!= NULL
);
300 memset(ptr
, 0, sizeof(struct uci_ptr
));
302 case UCI_TYPE_OPTION
:
303 ptr
->o
= uci_to_option(e
);
305 case UCI_TYPE_SECTION
:
306 ptr
->s
= uci_to_section(e
);
308 case UCI_TYPE_PACKAGE
:
309 ptr
->p
= uci_to_package(e
);
312 UCI_THROW(ctx
, UCI_ERR_INVAL
);
316 ptr
->option
= ptr
->o
->e
.name
;
317 ptr
->s
= ptr
->o
->section
;
319 ptr
->section
= ptr
->s
->e
.name
;
320 ptr
->p
= ptr
->s
->package
;
322 ptr
->package
= ptr
->p
->e
.name
;
324 ptr
->flags
|= UCI_LOOKUP_DONE
;
332 * verify that the end of the line or command is reached.
333 * throw an error if extra arguments are given on the command line
335 static void assert_eol(struct uci_context
*ctx
)
339 skip_whitespace(ctx
);
340 tmp
= next_arg(ctx
, false, false);
341 if (*tmp
&& (ctx
->flags
& UCI_FLAG_STRICT
))
342 uci_parse_error(ctx
, "too many arguments");
346 * switch to a different config, either triggered by uci_load, or by a
347 * 'package <...>' statement in the import file
349 static void uci_switch_config(struct uci_context
*ctx
)
351 struct uci_parse_context
*pctx
;
352 struct uci_element
*e
;
358 /* add the last config to main config file list */
360 pctx
->package
->backend
= ctx
->backend
;
361 uci_list_add(&ctx
->root
, &pctx
->package
->e
.list
);
363 pctx
->package
= NULL
;
364 pctx
->section
= NULL
;
371 * if an older config under the same name exists, unload it
372 * ignore errors here, e.g. if the config was not found
374 e
= uci_lookup_list(&ctx
->root
, name
);
376 UCI_THROW(ctx
, UCI_ERR_DUPLICATE
);
377 pctx
->package
= uci_alloc_package(ctx
, name
);
381 * parse the 'package' uci command (next config package)
383 static void uci_parse_package(struct uci_context
*ctx
, bool single
)
385 struct uci_parse_context
*pctx
= ctx
->pctx
;
388 /* command string null-terminated by strtok */
389 pctx
->pos
+= strlen(pctx_cur_str(pctx
)) + 1;
391 name
= next_arg(ctx
, true, true);
396 ctx
->pctx
->name
= name
;
397 uci_switch_config(ctx
);
401 * parse the 'config' uci command (open a section)
403 static void uci_parse_config(struct uci_context
*ctx
)
405 struct uci_parse_context
*pctx
= ctx
->pctx
;
406 struct uci_element
*e
;
411 uci_fixup_section(ctx
, ctx
->pctx
->section
);
412 if (!ctx
->pctx
->package
) {
413 if (!ctx
->pctx
->name
)
414 uci_parse_error(ctx
, "attempting to import a file without a package name");
416 uci_switch_config(ctx
);
419 /* command string null-terminated by strtok */
420 pctx
->pos
+= strlen(pctx_cur_str(pctx
)) + 1;
422 type
= next_arg(ctx
, true, false);
423 if (!uci_validate_type(type
))
424 uci_parse_error(ctx
, "invalid character in type field");
425 name
= next_arg(ctx
, false, true);
428 if (!name
|| !name
[0]) {
429 ctx
->internal
= !pctx
->merge
;
430 UCI_NESTED(uci_add_section
, ctx
, pctx
->package
, type
, &pctx
->section
);
432 uci_fill_ptr(ctx
, &ptr
, &pctx
->package
->e
);
433 e
= uci_lookup_list(&pctx
->package
->sections
, name
);
435 ptr
.s
= uci_to_section(e
);
439 ctx
->internal
= !pctx
->merge
;
440 UCI_NESTED(uci_set
, ctx
, &ptr
);
441 pctx
->section
= uci_to_section(ptr
.last
);
446 * parse the 'option' uci command (open a value)
448 static void uci_parse_option(struct uci_context
*ctx
, bool list
)
450 struct uci_parse_context
*pctx
= ctx
->pctx
;
451 struct uci_element
*e
;
457 uci_parse_error(ctx
, "option/list command found before the first section");
459 /* command string null-terminated by strtok */
460 pctx
->pos
+= strlen(pctx_cur_str(pctx
)) + 1;
462 name
= next_arg(ctx
, true, true);
463 value
= next_arg(ctx
, false, false);
466 uci_fill_ptr(ctx
, &ptr
, &pctx
->section
->e
);
467 e
= uci_lookup_list(&pctx
->section
->options
, name
);
469 ptr
.o
= uci_to_option(e
);
473 ctx
->internal
= !pctx
->merge
;
475 UCI_NESTED(uci_add_list
, ctx
, &ptr
);
477 UCI_NESTED(uci_set
, ctx
, &ptr
);
481 * parse a complete input line, split up combined commands by ';'
483 static void uci_parse_line(struct uci_context
*ctx
, bool single
)
485 struct uci_parse_context
*pctx
= ctx
->pctx
;
488 /* Skip whitespace characters at the start of line */
489 skip_whitespace(ctx
);
491 word
= strtok(pctx_cur_str(pctx
), " \t");
500 if ((word
[1] == 0) || !strcmp(word
+ 1, "ackage"))
501 uci_parse_package(ctx
, single
);
506 if ((word
[1] == 0) || !strcmp(word
+ 1, "onfig"))
507 uci_parse_config(ctx
);
512 if ((word
[1] == 0) || !strcmp(word
+ 1, "ption"))
513 uci_parse_option(ctx
, false);
518 if ((word
[1] == 0) || !strcmp(word
+ 1, "ist"))
519 uci_parse_option(ctx
, true);
528 uci_parse_error(ctx
, "invalid command");
532 /* max number of characters that escaping adds to the string */
533 #define UCI_QUOTE_ESCAPE "'\\''"
536 * escape an uci string for export
538 static const char *uci_escape(struct uci_context
*ctx
, const char *str
)
544 ctx
->bufsz
= LINEBUF
;
545 ctx
->buf
= malloc(LINEBUF
);
554 end
= strchr(str
, '\'');
556 end
= str
+ strlen(str
);
559 /* make sure that we have enough room in the buffer */
560 while (ofs
+ len
+ sizeof(UCI_QUOTE_ESCAPE
) + 1 > ctx
->bufsz
) {
562 ctx
->buf
= uci_realloc(ctx
, ctx
->buf
, ctx
->bufsz
);
565 /* copy the string until the character before the quote */
566 memcpy(&ctx
->buf
[ofs
], str
, len
);
569 /* end of string? return the buffer */
573 memcpy(&ctx
->buf
[ofs
], UCI_QUOTE_ESCAPE
, sizeof(UCI_QUOTE_ESCAPE
));
574 ofs
+= strlen(&ctx
->buf
[ofs
]);
583 * export a single config package to a file stream
585 static void uci_export_package(struct uci_package
*p
, FILE *stream
, bool header
)
587 struct uci_context
*ctx
= p
->ctx
;
588 struct uci_element
*s
, *o
, *i
;
591 fprintf(stream
, "package %s\n", uci_escape(ctx
, p
->e
.name
));
592 uci_foreach_element(&p
->sections
, s
) {
593 struct uci_section
*sec
= uci_to_section(s
);
594 fprintf(stream
, "\nconfig %s", uci_escape(ctx
, sec
->type
));
595 if (!sec
->anonymous
|| (ctx
->flags
& UCI_FLAG_EXPORT_NAME
))
596 fprintf(stream
, " '%s'", uci_escape(ctx
, sec
->e
.name
));
597 fprintf(stream
, "\n");
598 uci_foreach_element(&sec
->options
, o
) {
599 struct uci_option
*opt
= uci_to_option(o
);
601 case UCI_TYPE_STRING
:
602 fprintf(stream
, "\toption %s", uci_escape(ctx
, opt
->e
.name
));
603 fprintf(stream
, " '%s'\n", uci_escape(ctx
, opt
->v
.string
));
606 uci_foreach_element(&opt
->v
.list
, i
) {
607 fprintf(stream
, "\tlist %s", uci_escape(ctx
, opt
->e
.name
));
608 fprintf(stream
, " '%s'\n", uci_escape(ctx
, i
->name
));
612 fprintf(stream
, "\t# unknown type for option '%s'\n", uci_escape(ctx
, opt
->e
.name
));
617 fprintf(stream
, "\n");
620 int uci_export(struct uci_context
*ctx
, FILE *stream
, struct uci_package
*package
, bool header
)
622 struct uci_element
*e
;
625 UCI_ASSERT(ctx
, stream
!= NULL
);
628 uci_export_package(package
, stream
, header
);
630 uci_foreach_element(&ctx
->root
, e
) {
631 uci_export_package(uci_to_package(e
), stream
, header
);
638 int uci_import(struct uci_context
*ctx
, FILE *stream
, const char *name
, struct uci_package
**package
, bool single
)
640 struct uci_parse_context
*pctx
;
643 /* make sure no memory from previous parse attempts is leaked */
646 uci_alloc_parse_context(ctx
);
649 if (package
&& *package
&& single
) {
650 pctx
->package
= *package
;
655 * If 'name' was supplied, assume that the supplied stream does not contain
656 * the appropriate 'package <name>' string to specify the config name
657 * NB: the config file can still override the package name
660 UCI_ASSERT(ctx
, uci_validate_package(name
));
664 while (!feof(pctx
->file
)) {
667 UCI_TRAP_SAVE(ctx
, error
);
669 uci_parse_line(ctx
, single
);
670 UCI_TRAP_RESTORE(ctx
);
673 if (ctx
->flags
& UCI_FLAG_PERROR
)
674 uci_perror(ctx
, NULL
);
675 if ((ctx
->err
!= UCI_ERR_PARSE
) ||
676 (ctx
->flags
& UCI_FLAG_STRICT
))
677 UCI_THROW(ctx
, ctx
->err
);
680 uci_fixup_section(ctx
, ctx
->pctx
->section
);
681 if (!pctx
->package
&& name
)
682 uci_switch_config(ctx
);
684 *package
= pctx
->package
;
686 pctx
->package
= NULL
;
689 uci_switch_config(ctx
);
691 /* no error happened, we can get rid of the parser context now */
698 static char *uci_config_path(struct uci_context
*ctx
, const char *name
)
702 UCI_ASSERT(ctx
, uci_validate_package(name
));
703 filename
= uci_malloc(ctx
, strlen(name
) + strlen(ctx
->confdir
) + 2);
704 sprintf(filename
, "%s/%s", ctx
->confdir
, name
);
709 static void uci_file_commit(struct uci_context
*ctx
, struct uci_package
**package
, bool overwrite
)
711 struct uci_package
*p
= *package
;
712 FILE *f1
, *f2
= NULL
;
715 char *filename
= NULL
;
717 bool do_rename
= false;
721 p
->path
= uci_config_path(ctx
, p
->e
.name
);
723 UCI_THROW(ctx
, UCI_ERR_INVAL
);
726 if ((asprintf(&filename
, "%s/.%s.uci-XXXXXX", ctx
->confdir
, p
->e
.name
) < 0) || !filename
)
727 UCI_THROW(ctx
, UCI_ERR_MEM
);
729 if (!mktemp(filename
))
734 UCI_THROW(ctx
, UCI_ERR_IO
);
737 if ((stat(filename
, &statbuf
) == 0) && ((statbuf
.st_mode
& S_IFMT
) != S_IFREG
))
738 UCI_THROW(ctx
, UCI_ERR_IO
);
740 /* open the config file for writing now, so that it is locked */
741 f1
= uci_open_stream(ctx
, p
->path
, NULL
, SEEK_SET
, true, true);
743 /* flush unsaved changes and reload from delta file */
744 UCI_TRAP_SAVE(ctx
, done
);
747 name
= uci_strdup(ctx
, p
->e
.name
);
748 path
= uci_strdup(ctx
, p
->path
);
749 /* dump our own changes to the delta file */
750 if (!uci_list_empty(&p
->delta
))
751 UCI_INTERNAL(uci_save
, ctx
, p
);
754 * other processes might have modified the config
755 * as well. dump and reload
757 uci_free_package(&p
);
759 UCI_INTERNAL(uci_import
, ctx
, f1
, name
, &p
, true);
765 /* freed together with the uci_package */
770 if (!uci_load_delta(ctx
, p
, true))
774 f2
= uci_open_stream(ctx
, filename
, p
->path
, SEEK_SET
, true, true);
775 uci_export(ctx
, f2
, p
, false);
779 uci_close_stream(f2
);
783 UCI_TRAP_RESTORE(ctx
);
788 uci_close_stream(f1
);
789 if (do_rename
&& rename(filename
, p
->path
)) {
791 UCI_THROW(ctx
, UCI_ERR_IO
);
796 UCI_THROW(ctx
, ctx
->err
);
801 * This function returns the filename by returning the string
802 * after the last '/' character. By checking for a non-'\0'
803 * character afterwards, directories are ignored (glob marks
804 * those with a trailing '/'
806 static inline char *get_filename(char *path
)
810 p
= strrchr(path
, '/');
817 static char **uci_list_config_files(struct uci_context
*ctx
)
825 dir
= uci_malloc(ctx
, strlen(ctx
->confdir
) + 1 + sizeof("/*"));
826 sprintf(dir
, "%s/*", ctx
->confdir
);
827 if (glob(dir
, GLOB_MARK
, NULL
, &globbuf
) != 0) {
829 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
832 size
= sizeof(char *) * (globbuf
.gl_pathc
+ 1);
833 for(i
= 0; i
< globbuf
.gl_pathc
; i
++) {
836 p
= get_filename(globbuf
.gl_pathv
[i
]);
840 size
+= strlen(p
) + 1;
843 configs
= uci_malloc(ctx
, size
);
844 buf
= (char *) &configs
[globbuf
.gl_pathc
+ 1];
845 for(i
= 0; i
< globbuf
.gl_pathc
; i
++) {
848 p
= get_filename(globbuf
.gl_pathv
[i
]);
852 if (!uci_validate_package(p
))
857 buf
+= strlen(buf
) + 1;
864 static struct uci_package
*uci_file_load(struct uci_context
*ctx
, const char *name
)
866 struct uci_package
*package
= NULL
;
873 /* relative path outside of /etc/config */
875 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
878 /* absolute path outside of /etc/config */
879 filename
= uci_strdup(ctx
, name
);
880 name
= strrchr(name
, '/') + 1;
884 /* config in /etc/config */
885 filename
= uci_config_path(ctx
, name
);
890 UCI_TRAP_SAVE(ctx
, done
);
891 file
= uci_open_stream(ctx
, filename
, NULL
, SEEK_SET
, false, false);
893 UCI_INTERNAL(uci_import
, ctx
, file
, name
, &package
, true);
894 UCI_TRAP_RESTORE(ctx
);
897 package
->path
= filename
;
898 package
->has_delta
= confdir
;
899 uci_load_delta(ctx
, package
, false);
903 uci_close_stream(file
);
906 UCI_THROW(ctx
, ctx
->err
);
911 __private
UCI_BACKEND(uci_file_backend
, "file",
912 .load
= uci_file_load
,
913 .commit
= uci_file_commit
,
914 .list_configs
= uci_list_config_files
,