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 General Public License for more details.
16 * This file contains the code for parsing uci config files
20 #include <sys/types.h>
30 #define LINEBUF_MAX 4096
32 static void uci_parse_error(struct uci_context
*ctx
, char *pos
, char *reason
)
34 struct uci_parse_context
*pctx
= ctx
->pctx
;
36 pctx
->reason
= reason
;
37 pctx
->byte
= pos
- pctx
->buf
;
38 UCI_THROW(ctx
, UCI_ERR_PARSE
);
42 * Fetch a new line from the input stream and resize buffer if necessary
44 static void uci_getln(struct uci_context
*ctx
, int offset
)
46 struct uci_parse_context
*pctx
= ctx
->pctx
;
50 if (pctx
->buf
== NULL
) {
51 pctx
->buf
= uci_malloc(ctx
, LINEBUF
);
52 pctx
->bufsz
= LINEBUF
;
60 p
= fgets(p
, pctx
->bufsz
- ofs
, pctx
->file
);
65 if (pctx
->buf
[ofs
- 1] == '\n') {
67 pctx
->buf
[ofs
- 1] = 0;
71 if (pctx
->bufsz
> LINEBUF_MAX
/2)
72 uci_parse_error(ctx
, p
, "line too long");
75 pctx
->buf
= uci_realloc(ctx
, pctx
->buf
, pctx
->bufsz
);
80 * Clean up all extra memory used by the parser and exporter
82 static void uci_file_cleanup(struct uci_context
*ctx
)
84 struct uci_parse_context
*pctx
;
98 uci_free_package(&pctx
->package
);
107 * parse a character escaped by '\'
108 * returns true if the escaped character is to be parsed
109 * returns false if the escaped character is to be ignored
111 static inline bool parse_backslash(struct uci_context
*ctx
, char **str
)
116 /* undecoded backslash at the end of line, fetch the next line */
119 uci_getln(ctx
, *str
- ctx
->pctx
->buf
);
123 /* FIXME: decode escaped char, necessary? */
128 * move the string pointer forward until a non-whitespace character or
131 static void skip_whitespace(struct uci_context
*ctx
, char **str
)
134 while (**str
&& isspace(**str
))
138 if (!parse_backslash(ctx
, str
))
143 static inline void addc(char **dest
, char **src
)
151 * parse a double quoted string argument from the command line
153 static void parse_double_quote(struct uci_context
*ctx
, char **str
, char **target
)
157 /* skip quote character */
160 while ((c
= **str
)) {
167 if (!parse_backslash(ctx
, str
))
175 uci_parse_error(ctx
, *str
, "unterminated \"");
179 * parse a single quoted string argument from the command line
181 static void parse_single_quote(struct uci_context
*ctx
, char **str
, char **target
)
184 /* skip quote character */
187 while ((c
= **str
)) {
197 uci_parse_error(ctx
, *str
, "unterminated '");
201 * parse a string from the command line and detect the quoting style
203 static void parse_str(struct uci_context
*ctx
, char **str
, char **target
)
208 parse_single_quote(ctx
, str
, target
);
211 parse_double_quote(ctx
, str
, target
);
219 if (!parse_backslash(ctx
, str
))
226 } while (**str
&& !isspace(**str
));
230 * if the string was unquoted and we've stopped at a whitespace
231 * character, skip to the next one, because the whitespace will
232 * be overwritten by a null byte here
237 /* terminate the parsed string */
242 * extract the next argument from the command line
244 static char *next_arg(struct uci_context
*ctx
, char **str
, bool required
, bool name
)
250 skip_whitespace(ctx
, str
);
251 parse_str(ctx
, str
, &ptr
);
254 uci_parse_error(ctx
, *str
, "insufficient arguments");
258 if (name
&& !uci_validate_name(val
))
259 uci_parse_error(ctx
, val
, "invalid character in field");
266 * verify that the end of the line or command is reached.
267 * throw an error if extra arguments are given on the command line
269 static void assert_eol(struct uci_context
*ctx
, char **str
)
273 tmp
= next_arg(ctx
, str
, false, false);
274 if (tmp
&& *tmp
&& (ctx
->flags
& UCI_FLAG_STRICT
))
275 uci_parse_error(ctx
, *str
, "too many arguments");
279 * switch to a different config, either triggered by uci_load, or by a
280 * 'package <...>' statement in the import file
282 static void uci_switch_config(struct uci_context
*ctx
)
284 struct uci_parse_context
*pctx
;
285 struct uci_element
*e
;
291 /* add the last config to main config file list */
293 uci_list_add(&ctx
->root
, &pctx
->package
->e
.list
);
295 pctx
->package
= NULL
;
296 pctx
->section
= NULL
;
303 * if an older config under the same name exists, unload it
304 * ignore errors here, e.g. if the config was not found
306 e
= uci_lookup_list(ctx
, &ctx
->root
, name
);
308 UCI_THROW(ctx
, UCI_ERR_DUPLICATE
);
309 pctx
->package
= uci_alloc_package(ctx
, name
);
313 * parse the 'package' uci command (next config package)
315 static void uci_parse_package(struct uci_context
*ctx
, char **str
, bool single
)
319 /* command string null-terminated by strtok */
320 *str
+= strlen(*str
) + 1;
322 name
= next_arg(ctx
, str
, true, true);
323 assert_eol(ctx
, str
);
327 ctx
->pctx
->name
= name
;
328 uci_switch_config(ctx
);
331 /* Based on an efficient hash function published by D. J. Bernstein */
332 static unsigned int djbhash(unsigned int hash
, char *str
)
334 int len
= strlen(str
);
341 for(i
= 0; i
< len
; i
++) {
342 hash
= ((hash
<< 5) + hash
) + str
[i
];
344 return (hash
& 0x7FFFFFFF);
347 /* fix up an unnamed section */
348 static void uci_fixup_section(struct uci_context
*ctx
, struct uci_section
*s
)
350 unsigned int hash
= ~0;
351 struct uci_element
*e
;
358 * Generate a name for unnamed sections. This is used as reference
359 * when locating or updating the section from apps/scripts.
360 * To make multiple concurrent versions somewhat safe for updating,
361 * the name is generated from a hash of its type and name/value
362 * pairs of its option, and it is prefixed by a counter value.
363 * If the order of the unnamed sections changes for some reason,
364 * updates to them will be rejected.
366 hash
= djbhash(hash
, s
->type
);
367 uci_foreach_element(&s
->options
, e
) {
368 hash
= djbhash(hash
, e
->name
);
369 hash
= djbhash(hash
, uci_to_option(e
)->value
);
371 sprintf(buf
, "cfg%02x%04x", ++s
->package
->n_section
, hash
% (1 << 16));
372 s
->e
.name
= uci_strdup(ctx
, buf
);
376 * parse the 'config' uci command (open a section)
378 static void uci_parse_config(struct uci_context
*ctx
, char **str
)
380 struct uci_parse_context
*pctx
= ctx
->pctx
;
384 uci_fixup_section(ctx
, ctx
->pctx
->section
);
385 if (!ctx
->pctx
->package
) {
386 if (!ctx
->pctx
->name
)
387 uci_parse_error(ctx
, *str
, "attempting to import a file without a package name");
389 uci_switch_config(ctx
);
392 /* command string null-terminated by strtok */
393 *str
+= strlen(*str
) + 1;
395 type
= next_arg(ctx
, str
, true, true);
396 name
= next_arg(ctx
, str
, false, true);
397 assert_eol(ctx
, str
);
400 UCI_TRAP_SAVE(ctx
, error
);
401 uci_set(ctx
, pctx
->package
, name
, NULL
, type
);
402 UCI_TRAP_RESTORE(ctx
);
405 UCI_THROW(ctx
, ctx
->errno
);
407 pctx
->section
= uci_alloc_section(pctx
->package
, type
, name
);
411 * parse the 'option' uci command (open a value)
413 static void uci_parse_option(struct uci_context
*ctx
, char **str
)
415 struct uci_parse_context
*pctx
= ctx
->pctx
;
420 uci_parse_error(ctx
, *str
, "option command found before the first section");
422 /* command string null-terminated by strtok */
423 *str
+= strlen(*str
) + 1;
425 name
= next_arg(ctx
, str
, true, true);
426 value
= next_arg(ctx
, str
, true, false);
427 assert_eol(ctx
, str
);
430 UCI_TRAP_SAVE(ctx
, error
);
431 uci_set(ctx
, pctx
->package
, pctx
->section
->e
.name
, name
, value
);
432 UCI_TRAP_RESTORE(ctx
);
435 UCI_THROW(ctx
, ctx
->errno
);
437 uci_alloc_option(pctx
->section
, name
, value
);
442 * parse a complete input line, split up combined commands by ';'
444 static void uci_parse_line(struct uci_context
*ctx
, bool single
)
446 struct uci_parse_context
*pctx
= ctx
->pctx
;
447 char *word
, *brk
= NULL
;
449 for (word
= strtok_r(pctx
->buf
, ";", &brk
);
451 word
= strtok_r(NULL
, ";", &brk
)) {
454 word
= strtok_r(word
, " \t", &pbrk
);
463 if ((word
[1] == 0) || !strcmp(word
+ 1, "ackage"))
464 uci_parse_package(ctx
, &word
, single
);
467 if ((word
[1] == 0) || !strcmp(word
+ 1, "onfig"))
468 uci_parse_config(ctx
, &word
);
471 if ((word
[1] == 0) || !strcmp(word
+ 1, "ption"))
472 uci_parse_option(ctx
, &word
);
475 uci_parse_error(ctx
, word
, "unterminated command");
481 /* max number of characters that escaping adds to the string */
482 #define UCI_QUOTE_ESCAPE "'\\''"
485 * escape an uci string for export
487 static char *uci_escape(struct uci_context
*ctx
, char *str
)
493 ctx
->bufsz
= LINEBUF
;
494 ctx
->buf
= malloc(LINEBUF
);
498 p
= strchr(str
, '\'');
505 if (p
+ sizeof(UCI_QUOTE_ESCAPE
) - str
>= ctx
->bufsz
) {
507 ctx
->buf
= realloc(ctx
->buf
, ctx
->bufsz
);
509 UCI_THROW(ctx
, UCI_ERR_MEM
);
511 memcpy(&ctx
->buf
[pos
], s
, len
);
514 strcpy(&ctx
->buf
[pos
], UCI_QUOTE_ESCAPE
);
515 pos
+= sizeof(UCI_QUOTE_ESCAPE
);
517 } while ((p
= strchr(s
, '\'')));
524 * export a single config package to a file stream
526 static void uci_export_package(struct uci_package
*p
, FILE *stream
, bool header
)
528 struct uci_context
*ctx
= p
->ctx
;
529 struct uci_element
*s
, *o
;
532 fprintf(stream
, "package '%s'\n", uci_escape(ctx
, p
->e
.name
));
533 uci_foreach_element(&p
->sections
, s
) {
534 struct uci_section
*sec
= uci_to_section(s
);
535 fprintf(stream
, "\nconfig '%s'", uci_escape(ctx
, sec
->type
));
536 if (!sec
->anonymous
|| (ctx
->flags
& UCI_FLAG_EXPORT_NAME
))
537 fprintf(stream
, " '%s'", uci_escape(ctx
, sec
->e
.name
));
538 fprintf(stream
, "\n");
539 uci_foreach_element(&sec
->options
, o
) {
540 struct uci_option
*opt
= uci_to_option(o
);
541 fprintf(stream
, "\toption '%s'", uci_escape(ctx
, opt
->e
.name
));
542 fprintf(stream
, " '%s'\n", uci_escape(ctx
, opt
->value
));
545 fprintf(stream
, "\n");
548 int uci_export(struct uci_context
*ctx
, FILE *stream
, struct uci_package
*package
, bool header
)
550 struct uci_element
*e
;
553 UCI_ASSERT(ctx
, stream
!= NULL
);
556 uci_export_package(package
, stream
, header
);
558 uci_foreach_element(&ctx
->root
, e
) {
559 uci_export_package(uci_to_package(e
), stream
, header
);
566 int uci_import(struct uci_context
*ctx
, FILE *stream
, const char *name
, struct uci_package
**package
, bool single
)
568 struct uci_parse_context
*pctx
;
571 /* make sure no memory from previous parse attempts is leaked */
572 uci_file_cleanup(ctx
);
574 pctx
= (struct uci_parse_context
*) uci_malloc(ctx
, sizeof(struct uci_parse_context
));
577 if (*package
&& single
) {
578 pctx
->package
= *package
;
583 * If 'name' was supplied, assume that the supplied stream does not contain
584 * the appropriate 'package <name>' string to specify the config name
585 * NB: the config file can still override the package name
588 UCI_ASSERT(ctx
, uci_validate_name(name
));
592 while (!feof(pctx
->file
)) {
594 UCI_TRAP_SAVE(ctx
, error
);
596 uci_parse_line(ctx
, single
);
597 UCI_TRAP_RESTORE(ctx
);
600 if (ctx
->flags
& UCI_FLAG_PERROR
)
601 uci_perror(ctx
, NULL
);
602 if ((ctx
->errno
!= UCI_ERR_PARSE
) ||
603 (ctx
->flags
& UCI_FLAG_STRICT
))
604 UCI_THROW(ctx
, ctx
->errno
);
607 uci_fixup_section(ctx
, ctx
->pctx
->section
);
609 *package
= pctx
->package
;
611 pctx
->package
= NULL
;
614 uci_switch_config(ctx
);
616 /* no error happened, we can get rid of the parser context now */
617 uci_file_cleanup(ctx
);
623 * open a stream and go to the right position
625 * note: when opening for write and seeking to the beginning of
626 * the stream, truncate the file
628 static FILE *uci_open_stream(struct uci_context
*ctx
, const char *filename
, int pos
, bool write
, bool create
)
633 int mode
= (write
? O_RDWR
: O_RDONLY
);
638 if (!write
&& ((stat(filename
, &statbuf
) < 0) ||
639 ((statbuf
.st_mode
& S_IFMT
) != S_IFREG
))) {
640 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
643 fd
= open(filename
, mode
, UCI_FILEMODE
);
647 if (flock(fd
, (write
? LOCK_EX
: LOCK_SH
)) < 0)
650 ret
= lseek(fd
, 0, pos
);
655 file
= fdopen(fd
, (write
? "w+" : "r"));
660 UCI_THROW(ctx
, UCI_ERR_IO
);
665 static void uci_close_stream(FILE *stream
)
677 static void uci_parse_history_line(struct uci_context
*ctx
, struct uci_package
*p
, char *buf
)
681 char *package
= NULL
;
682 char *section
= NULL
;
689 } else if (buf
[0] == '@') {
694 UCI_INTERNAL(uci_parse_tuple
, ctx
, buf
, &package
, §ion
, &option
, &value
);
695 if (!package
|| (strcmp(package
, p
->e
.name
) != 0))
697 if (!uci_validate_name(section
))
699 if (option
&& !uci_validate_name(option
))
701 if ((rename
|| !delete) && !uci_validate_name(value
))
705 UCI_INTERNAL(uci_rename
, ctx
, p
, section
, option
, value
);
707 UCI_INTERNAL(uci_delete
, ctx
, p
, section
, option
);
709 UCI_INTERNAL(uci_set
, ctx
, p
, section
, option
, value
);
713 UCI_THROW(ctx
, UCI_ERR_PARSE
);
716 static void uci_parse_history(struct uci_context
*ctx
, FILE *stream
, struct uci_package
*p
)
718 struct uci_parse_context
*pctx
;
720 /* make sure no memory from previous parse attempts is leaked */
721 uci_file_cleanup(ctx
);
723 pctx
= (struct uci_parse_context
*) uci_malloc(ctx
, sizeof(struct uci_parse_context
));
727 while (!feof(pctx
->file
)) {
733 * ignore parse errors in single lines, we want to preserve as much
734 * history as possible
736 UCI_TRAP_SAVE(ctx
, error
);
737 uci_parse_history_line(ctx
, p
, pctx
->buf
);
738 UCI_TRAP_RESTORE(ctx
);
743 /* no error happened, we can get rid of the parser context now */
744 uci_file_cleanup(ctx
);
747 static void uci_load_history_file(struct uci_context
*ctx
, struct uci_package
*p
, char *filename
, FILE **f
, bool flush
)
751 UCI_TRAP_SAVE(ctx
, done
);
752 stream
= uci_open_stream(ctx
, filename
, SEEK_SET
, flush
, false);
754 uci_parse_history(ctx
, stream
, p
);
755 UCI_TRAP_RESTORE(ctx
);
760 uci_close_stream(stream
);
763 static void uci_load_history(struct uci_context
*ctx
, struct uci_package
*p
, bool flush
)
765 struct uci_element
*e
;
766 char *filename
= NULL
;
772 uci_foreach_element(&ctx
->history_path
, e
) {
773 if ((asprintf(&filename
, "%s/%s", e
->name
, p
->e
.name
) < 0) || !filename
)
774 UCI_THROW(ctx
, UCI_ERR_MEM
);
776 uci_load_history_file(ctx
, p
, filename
, NULL
, false);
780 if ((asprintf(&filename
, "%s/%s", ctx
->savedir
, p
->e
.name
) < 0) || !filename
)
781 UCI_THROW(ctx
, UCI_ERR_MEM
);
783 uci_load_history_file(ctx
, p
, filename
, &f
, flush
);
786 ftruncate(fileno(f
), 0);
795 static char *uci_config_path(struct uci_context
*ctx
, const char *name
)
799 UCI_ASSERT(ctx
, uci_validate_name(name
));
800 filename
= uci_malloc(ctx
, strlen(name
) + strlen(ctx
->confdir
) + 2);
801 sprintf(filename
, "%s/%s", ctx
->confdir
, name
);
806 int uci_load(struct uci_context
*ctx
, const char *name
, struct uci_package
**package
)
816 /* relative path outside of /etc/config */
818 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
821 /* absolute path outside of /etc/config */
822 filename
= uci_strdup(ctx
, name
);
823 name
= strrchr(name
, '/') + 1;
827 /* config in /etc/config */
828 filename
= uci_config_path(ctx
, name
);
833 file
= uci_open_stream(ctx
, filename
, SEEK_SET
, false, false);
835 UCI_TRAP_SAVE(ctx
, done
);
836 UCI_INTERNAL(uci_import
, ctx
, file
, name
, package
, true);
837 UCI_TRAP_RESTORE(ctx
);
840 (*package
)->path
= filename
;
841 (*package
)->confdir
= confdir
;
842 uci_load_history(ctx
, *package
, false);
846 uci_close_stream(file
);
850 int uci_save(struct uci_context
*ctx
, struct uci_package
*p
)
853 char *filename
= NULL
;
854 struct uci_element
*e
, *tmp
;
857 UCI_ASSERT(ctx
, p
!= NULL
);
860 * if the config file was outside of the /etc/config path,
861 * don't save the history to a file, update the real file
863 * does not modify the uci_package pointer
866 return uci_commit(ctx
, &p
, false);
868 if (uci_list_empty(&p
->history
))
871 if ((asprintf(&filename
, "%s/%s", ctx
->savedir
, p
->e
.name
) < 0) || !filename
)
872 UCI_THROW(ctx
, UCI_ERR_MEM
);
875 UCI_TRAP_SAVE(ctx
, done
);
876 f
= uci_open_stream(ctx
, filename
, SEEK_END
, true, true);
877 UCI_TRAP_RESTORE(ctx
);
879 uci_foreach_element_safe(&p
->history
, tmp
, e
) {
880 struct uci_history
*h
= uci_to_history(e
);
882 if (h
->cmd
== UCI_CMD_REMOVE
)
884 else if (h
->cmd
== UCI_CMD_RENAME
)
887 fprintf(f
, "%s.%s", p
->e
.name
, h
->section
);
889 fprintf(f
, ".%s", e
->name
);
891 if (h
->cmd
== UCI_CMD_REMOVE
)
894 fprintf(f
, "=%s\n", h
->value
);
903 UCI_THROW(ctx
, ctx
->errno
);
908 int uci_commit(struct uci_context
*ctx
, struct uci_package
**package
, bool overwrite
)
910 struct uci_package
*p
;
916 UCI_ASSERT(ctx
, package
!= NULL
);
919 UCI_ASSERT(ctx
, p
!= NULL
);
922 p
->path
= uci_config_path(ctx
, p
->e
.name
);
924 UCI_THROW(ctx
, UCI_ERR_INVAL
);
928 /* open the config file for writing now, so that it is locked */
929 f
= uci_open_stream(ctx
, p
->path
, SEEK_SET
, true, true);
931 /* flush unsaved changes and reload from history file */
932 UCI_TRAP_SAVE(ctx
, done
);
935 name
= uci_strdup(ctx
, p
->e
.name
);
936 path
= uci_strdup(ctx
, p
->path
);
937 /* dump our own changes to the history file */
938 if (!uci_list_empty(&p
->history
))
939 UCI_INTERNAL(uci_save
, ctx
, p
);
942 * other processes might have modified the config
943 * as well. dump and reload
945 uci_free_package(&p
);
946 uci_file_cleanup(ctx
);
947 UCI_INTERNAL(uci_import
, ctx
, f
, name
, &p
, true);
953 /* freed together with the uci_package */
956 /* check for updated history, just in case */
957 uci_load_history(ctx
, p
, true);
960 uci_load_history(ctx
, NULL
, true);
965 ftruncate(fileno(f
), 0);
967 uci_export(ctx
, f
, p
, false);
968 UCI_TRAP_RESTORE(ctx
);
977 UCI_THROW(ctx
, ctx
->errno
);
984 * This function returns the filename by returning the string
985 * after the last '/' character. By checking for a non-'\0'
986 * character afterwards, directories are ignored (glob marks
987 * those with a trailing '/'
989 static inline char *get_filename(char *path
)
993 p
= strrchr(path
, '/');
1000 int uci_list_configs(struct uci_context
*ctx
, char ***list
)
1008 UCI_HANDLE_ERR(ctx
);
1010 dir
= uci_malloc(ctx
, strlen(ctx
->confdir
) + 1 + sizeof("/*"));
1011 sprintf(dir
, "%s/*", ctx
->confdir
);
1012 if (glob(dir
, GLOB_MARK
, NULL
, &globbuf
) != 0)
1013 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
1015 size
= sizeof(char *) * (globbuf
.gl_pathc
+ 1);
1016 for(i
= 0; i
< globbuf
.gl_pathc
; i
++) {
1019 p
= get_filename(globbuf
.gl_pathv
[i
]);
1023 size
+= strlen(p
) + 1;
1026 configs
= uci_malloc(ctx
, size
);
1027 buf
= (char *) &configs
[globbuf
.gl_pathc
+ 1];
1028 for(i
= 0; i
< globbuf
.gl_pathc
; i
++) {
1031 p
= get_filename(globbuf
.gl_pathv
[i
]);
1037 buf
+= strlen(buf
) + 1;