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>
29 * Clean up all extra memory used by the parser and exporter
31 static void uci_file_cleanup(struct uci_context
*ctx
)
33 struct uci_parse_context
*pctx
;
47 uci_free_package(&pctx
->package
);
57 * parse a character escaped by '\'
58 * returns true if the escaped character is to be parsed
59 * returns false if the escaped character is to be ignored
61 static inline bool parse_backslash(struct uci_context
*ctx
, char **str
)
66 /* undecoded backslash at the end of line, fetch the next line */
69 uci_getln(ctx
, *str
- ctx
->pctx
->buf
);
73 /* FIXME: decode escaped char, necessary? */
78 * move the string pointer forward until a non-whitespace character or
81 static void skip_whitespace(struct uci_context
*ctx
, char **str
)
84 while (**str
&& isspace(**str
))
88 if (!parse_backslash(ctx
, str
))
93 static inline void addc(char **dest
, char **src
)
101 * parse a double quoted string argument from the command line
103 static void parse_double_quote(struct uci_context
*ctx
, char **str
, char **target
)
107 /* skip quote character */
110 while ((c
= **str
)) {
117 if (!parse_backslash(ctx
, str
))
125 uci_parse_error(ctx
, *str
, "unterminated \"");
129 * parse a single quoted string argument from the command line
131 static void parse_single_quote(struct uci_context
*ctx
, char **str
, char **target
)
134 /* skip quote character */
137 while ((c
= **str
)) {
147 uci_parse_error(ctx
, *str
, "unterminated '");
151 * parse a string from the command line and detect the quoting style
153 static void parse_str(struct uci_context
*ctx
, char **str
, char **target
)
158 parse_single_quote(ctx
, str
, target
);
161 parse_double_quote(ctx
, str
, target
);
169 if (!parse_backslash(ctx
, str
))
176 } while (**str
&& !isspace(**str
));
180 * if the string was unquoted and we've stopped at a whitespace
181 * character, skip to the next one, because the whitespace will
182 * be overwritten by a null byte here
187 /* terminate the parsed string */
192 * extract the next argument from the command line
194 static char *next_arg(struct uci_context
*ctx
, char **str
, bool required
, bool name
)
200 skip_whitespace(ctx
, str
);
201 parse_str(ctx
, str
, &ptr
);
204 uci_parse_error(ctx
, *str
, "insufficient arguments");
208 if (name
&& !uci_validate_name(val
))
209 uci_parse_error(ctx
, val
, "invalid character in field");
216 * verify that the end of the line or command is reached.
217 * throw an error if extra arguments are given on the command line
219 static void assert_eol(struct uci_context
*ctx
, char **str
)
223 tmp
= next_arg(ctx
, str
, false, false);
224 if (tmp
&& *tmp
&& (ctx
->flags
& UCI_FLAG_STRICT
))
225 uci_parse_error(ctx
, *str
, "too many arguments");
229 * switch to a different config, either triggered by uci_load, or by a
230 * 'package <...>' statement in the import file
232 static void uci_switch_config(struct uci_context
*ctx
)
234 struct uci_parse_context
*pctx
;
235 struct uci_element
*e
;
241 /* add the last config to main config file list */
243 uci_list_add(&ctx
->root
, &pctx
->package
->e
.list
);
245 pctx
->package
= NULL
;
246 pctx
->section
= NULL
;
253 * if an older config under the same name exists, unload it
254 * ignore errors here, e.g. if the config was not found
256 e
= uci_lookup_list(ctx
, &ctx
->root
, name
);
258 UCI_THROW(ctx
, UCI_ERR_DUPLICATE
);
259 pctx
->package
= uci_alloc_package(ctx
, name
);
263 * parse the 'package' uci command (next config package)
265 static void uci_parse_package(struct uci_context
*ctx
, char **str
, bool single
)
269 /* command string null-terminated by strtok */
270 *str
+= strlen(*str
) + 1;
272 name
= next_arg(ctx
, str
, true, true);
273 assert_eol(ctx
, str
);
277 ctx
->pctx
->name
= name
;
278 uci_switch_config(ctx
);
281 /* Based on an efficient hash function published by D. J. Bernstein */
282 static unsigned int djbhash(unsigned int hash
, char *str
)
284 int len
= strlen(str
);
291 for(i
= 0; i
< len
; i
++) {
292 hash
= ((hash
<< 5) + hash
) + str
[i
];
294 return (hash
& 0x7FFFFFFF);
297 /* fix up an unnamed section */
298 static void uci_fixup_section(struct uci_context
*ctx
, struct uci_section
*s
)
300 unsigned int hash
= ~0;
301 struct uci_element
*e
;
308 * Generate a name for unnamed sections. This is used as reference
309 * when locating or updating the section from apps/scripts.
310 * To make multiple concurrent versions somewhat safe for updating,
311 * the name is generated from a hash of its type and name/value
312 * pairs of its option, and it is prefixed by a counter value.
313 * If the order of the unnamed sections changes for some reason,
314 * updates to them will be rejected.
316 hash
= djbhash(hash
, s
->type
);
317 uci_foreach_element(&s
->options
, e
) {
318 hash
= djbhash(hash
, e
->name
);
319 hash
= djbhash(hash
, uci_to_option(e
)->value
);
321 sprintf(buf
, "cfg%02x%04x", ++s
->package
->n_section
, hash
% (1 << 16));
322 s
->e
.name
= uci_strdup(ctx
, buf
);
326 * parse the 'config' uci command (open a section)
328 static void uci_parse_config(struct uci_context
*ctx
, char **str
)
330 struct uci_parse_context
*pctx
= ctx
->pctx
;
334 uci_fixup_section(ctx
, ctx
->pctx
->section
);
335 if (!ctx
->pctx
->package
) {
336 if (!ctx
->pctx
->name
)
337 uci_parse_error(ctx
, *str
, "attempting to import a file without a package name");
339 uci_switch_config(ctx
);
342 /* command string null-terminated by strtok */
343 *str
+= strlen(*str
) + 1;
345 type
= next_arg(ctx
, str
, true, true);
346 name
= next_arg(ctx
, str
, false, true);
347 assert_eol(ctx
, str
);
350 UCI_TRAP_SAVE(ctx
, error
);
351 uci_set(ctx
, pctx
->package
, name
, NULL
, type
);
352 UCI_TRAP_RESTORE(ctx
);
355 UCI_THROW(ctx
, ctx
->errno
);
357 pctx
->section
= uci_alloc_section(pctx
->package
, type
, name
);
361 * parse the 'option' uci command (open a value)
363 static void uci_parse_option(struct uci_context
*ctx
, char **str
)
365 struct uci_parse_context
*pctx
= ctx
->pctx
;
370 uci_parse_error(ctx
, *str
, "option command found before the first section");
372 /* command string null-terminated by strtok */
373 *str
+= strlen(*str
) + 1;
375 name
= next_arg(ctx
, str
, true, true);
376 value
= next_arg(ctx
, str
, false, false);
377 assert_eol(ctx
, str
);
380 UCI_TRAP_SAVE(ctx
, error
);
381 uci_set(ctx
, pctx
->package
, pctx
->section
->e
.name
, name
, value
);
382 UCI_TRAP_RESTORE(ctx
);
385 UCI_THROW(ctx
, ctx
->errno
);
387 uci_alloc_option(pctx
->section
, name
, value
);
392 * parse a complete input line, split up combined commands by ';'
394 static void uci_parse_line(struct uci_context
*ctx
, bool single
)
396 struct uci_parse_context
*pctx
= ctx
->pctx
;
397 char *word
, *brk
= NULL
;
399 for (word
= strtok_r(pctx
->buf
, ";", &brk
);
401 word
= strtok_r(NULL
, ";", &brk
)) {
404 word
= strtok_r(word
, " \t", &pbrk
);
413 if ((word
[1] == 0) || !strcmp(word
+ 1, "ackage"))
414 uci_parse_package(ctx
, &word
, single
);
417 if ((word
[1] == 0) || !strcmp(word
+ 1, "onfig"))
418 uci_parse_config(ctx
, &word
);
421 if ((word
[1] == 0) || !strcmp(word
+ 1, "ption"))
422 uci_parse_option(ctx
, &word
);
425 uci_parse_error(ctx
, word
, "unterminated command");
431 /* max number of characters that escaping adds to the string */
432 #define UCI_QUOTE_ESCAPE "'\\''"
435 * escape an uci string for export
437 static char *uci_escape(struct uci_context
*ctx
, char *str
)
443 ctx
->bufsz
= LINEBUF
;
444 ctx
->buf
= malloc(LINEBUF
);
448 p
= strchr(str
, '\'');
455 if (p
+ sizeof(UCI_QUOTE_ESCAPE
) - str
>= ctx
->bufsz
) {
457 ctx
->buf
= realloc(ctx
->buf
, ctx
->bufsz
);
459 UCI_THROW(ctx
, UCI_ERR_MEM
);
461 memcpy(&ctx
->buf
[pos
], s
, len
);
464 strcpy(&ctx
->buf
[pos
], UCI_QUOTE_ESCAPE
);
465 pos
+= sizeof(UCI_QUOTE_ESCAPE
);
467 } while ((p
= strchr(s
, '\'')));
474 * export a single config package to a file stream
476 static void uci_export_package(struct uci_package
*p
, FILE *stream
, bool header
)
478 struct uci_context
*ctx
= p
->ctx
;
479 struct uci_element
*s
, *o
;
482 fprintf(stream
, "package '%s'\n", uci_escape(ctx
, p
->e
.name
));
483 uci_foreach_element(&p
->sections
, s
) {
484 struct uci_section
*sec
= uci_to_section(s
);
485 fprintf(stream
, "\nconfig '%s'", uci_escape(ctx
, sec
->type
));
486 if (!sec
->anonymous
|| (ctx
->flags
& UCI_FLAG_EXPORT_NAME
))
487 fprintf(stream
, " '%s'", uci_escape(ctx
, sec
->e
.name
));
488 fprintf(stream
, "\n");
489 uci_foreach_element(&sec
->options
, o
) {
490 struct uci_option
*opt
= uci_to_option(o
);
491 fprintf(stream
, "\toption '%s'", uci_escape(ctx
, opt
->e
.name
));
492 fprintf(stream
, " '%s'\n", uci_escape(ctx
, opt
->value
));
495 fprintf(stream
, "\n");
498 int uci_export(struct uci_context
*ctx
, FILE *stream
, struct uci_package
*package
, bool header
)
500 struct uci_element
*e
;
503 UCI_ASSERT(ctx
, stream
!= NULL
);
506 uci_export_package(package
, stream
, header
);
508 uci_foreach_element(&ctx
->root
, e
) {
509 uci_export_package(uci_to_package(e
), stream
, header
);
516 int uci_import(struct uci_context
*ctx
, FILE *stream
, const char *name
, struct uci_package
**package
, bool single
)
518 struct uci_parse_context
*pctx
;
521 /* make sure no memory from previous parse attempts is leaked */
522 uci_file_cleanup(ctx
);
524 uci_alloc_parse_context(ctx
);
527 if (*package
&& single
) {
528 pctx
->package
= *package
;
533 * If 'name' was supplied, assume that the supplied stream does not contain
534 * the appropriate 'package <name>' string to specify the config name
535 * NB: the config file can still override the package name
538 UCI_ASSERT(ctx
, uci_validate_name(name
));
542 while (!feof(pctx
->file
)) {
544 UCI_TRAP_SAVE(ctx
, error
);
546 uci_parse_line(ctx
, single
);
547 UCI_TRAP_RESTORE(ctx
);
550 if (ctx
->flags
& UCI_FLAG_PERROR
)
551 uci_perror(ctx
, NULL
);
552 if ((ctx
->errno
!= UCI_ERR_PARSE
) ||
553 (ctx
->flags
& UCI_FLAG_STRICT
))
554 UCI_THROW(ctx
, ctx
->errno
);
557 uci_fixup_section(ctx
, ctx
->pctx
->section
);
559 *package
= pctx
->package
;
561 pctx
->package
= NULL
;
564 uci_switch_config(ctx
);
566 /* no error happened, we can get rid of the parser context now */
567 uci_file_cleanup(ctx
);
573 static char *uci_config_path(struct uci_context
*ctx
, const char *name
)
577 UCI_ASSERT(ctx
, uci_validate_name(name
));
578 filename
= uci_malloc(ctx
, strlen(name
) + strlen(ctx
->confdir
) + 2);
579 sprintf(filename
, "%s/%s", ctx
->confdir
, name
);
584 int uci_load(struct uci_context
*ctx
, const char *name
, struct uci_package
**package
)
594 /* relative path outside of /etc/config */
596 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
599 /* absolute path outside of /etc/config */
600 filename
= uci_strdup(ctx
, name
);
601 name
= strrchr(name
, '/') + 1;
605 /* config in /etc/config */
606 filename
= uci_config_path(ctx
, name
);
611 file
= uci_open_stream(ctx
, filename
, SEEK_SET
, false, false);
613 UCI_TRAP_SAVE(ctx
, done
);
614 UCI_INTERNAL(uci_import
, ctx
, file
, name
, package
, true);
615 UCI_TRAP_RESTORE(ctx
);
618 (*package
)->path
= filename
;
619 (*package
)->confdir
= confdir
;
620 uci_load_history(ctx
, *package
, false);
624 uci_close_stream(file
);
628 int uci_commit(struct uci_context
*ctx
, struct uci_package
**package
, bool overwrite
)
630 struct uci_package
*p
;
636 UCI_ASSERT(ctx
, package
!= NULL
);
639 UCI_ASSERT(ctx
, p
!= NULL
);
642 p
->path
= uci_config_path(ctx
, p
->e
.name
);
644 UCI_THROW(ctx
, UCI_ERR_INVAL
);
648 /* open the config file for writing now, so that it is locked */
649 f
= uci_open_stream(ctx
, p
->path
, SEEK_SET
, true, true);
651 /* flush unsaved changes and reload from history file */
652 UCI_TRAP_SAVE(ctx
, done
);
655 name
= uci_strdup(ctx
, p
->e
.name
);
656 path
= uci_strdup(ctx
, p
->path
);
657 /* dump our own changes to the history file */
658 if (!uci_list_empty(&p
->history
))
659 UCI_INTERNAL(uci_save
, ctx
, p
);
662 * other processes might have modified the config
663 * as well. dump and reload
665 uci_free_package(&p
);
666 uci_file_cleanup(ctx
);
667 UCI_INTERNAL(uci_import
, ctx
, f
, name
, &p
, true);
673 /* freed together with the uci_package */
676 /* check for updated history, flush */
677 uci_load_history(ctx
, p
, true);
680 uci_load_history(ctx
, NULL
, true);
685 ftruncate(fileno(f
), 0);
687 uci_export(ctx
, f
, p
, false);
688 UCI_TRAP_RESTORE(ctx
);
697 UCI_THROW(ctx
, ctx
->errno
);
704 * This function returns the filename by returning the string
705 * after the last '/' character. By checking for a non-'\0'
706 * character afterwards, directories are ignored (glob marks
707 * those with a trailing '/'
709 static inline char *get_filename(char *path
)
713 p
= strrchr(path
, '/');
720 int uci_list_configs(struct uci_context
*ctx
, char ***list
)
730 dir
= uci_malloc(ctx
, strlen(ctx
->confdir
) + 1 + sizeof("/*"));
731 sprintf(dir
, "%s/*", ctx
->confdir
);
732 if (glob(dir
, GLOB_MARK
, NULL
, &globbuf
) != 0)
733 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
735 size
= sizeof(char *) * (globbuf
.gl_pathc
+ 1);
736 for(i
= 0; i
< globbuf
.gl_pathc
; i
++) {
739 p
= get_filename(globbuf
.gl_pathv
[i
]);
743 size
+= strlen(p
) + 1;
746 configs
= uci_malloc(ctx
, size
);
747 buf
= (char *) &configs
[globbuf
.gl_pathc
+ 1];
748 for(i
= 0; i
< globbuf
.gl_pathc
; i
++) {
751 p
= get_filename(globbuf
.gl_pathv
[i
]);
757 buf
+= strlen(buf
) + 1;