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);
460 if ((word[1] == 0) || !strcmp(word + 1, "ackage"))
461 uci_parse_package(ctx, &word, single);
464 if ((word[1] == 0) || !strcmp(word + 1, "onfig"))
465 uci_parse_config(ctx, &word);
468 if ((word[1] == 0) || !strcmp(word + 1, "ption"))
469 uci_parse_option(ctx, &word);
472 uci_parse_error(ctx, word, "unterminated command");
478 /* max number of characters that escaping adds to the string */
479 #define UCI_QUOTE_ESCAPE "'\\''"
482 * escape an uci string for export
484 static char *uci_escape(struct uci_context *ctx, char *str)
490 ctx->bufsz = LINEBUF;
491 ctx->buf = malloc(LINEBUF);
495 p = strchr(str, '\'');
502 if (p + sizeof(UCI_QUOTE_ESCAPE) - str >= ctx->bufsz) {
504 ctx->buf = realloc(ctx->buf, ctx->bufsz);
506 UCI_THROW(ctx, UCI_ERR_MEM);
508 memcpy(&ctx->buf[pos], s, len);
511 strcpy(&ctx->buf[pos], UCI_QUOTE_ESCAPE);
512 pos += sizeof(UCI_QUOTE_ESCAPE);
514 } while ((p = strchr(s, '\'')));
521 * export a single config package to a file stream
523 static void uci_export_package(struct uci_package *p, FILE *stream, bool header)
525 struct uci_context *ctx = p->ctx;
526 struct uci_element *s, *o;
529 fprintf(stream, "package '%s'\n", uci_escape(ctx, p->e.name));
530 uci_foreach_element(&p->sections, s) {
531 struct uci_section *sec = uci_to_section(s);
532 fprintf(stream, "\nconfig '%s'", uci_escape(ctx, sec->type));
533 if (!sec->anonymous || (ctx->flags & UCI_FLAG_EXPORT_NAME))
534 fprintf(stream, " '%s'", uci_escape(ctx, sec->e.name));
535 fprintf(stream, "\n");
536 uci_foreach_element(&sec->options, o) {
537 struct uci_option *opt = uci_to_option(o);
538 fprintf(stream, "\toption '%s'", uci_escape(ctx, opt->e.name));
539 fprintf(stream, " '%s'\n", uci_escape(ctx, opt->value));
542 fprintf(stream, "\n");
545 int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package, bool header)
547 struct uci_element *e;
550 UCI_ASSERT(ctx, stream != NULL);
553 uci_export_package(package, stream, header);
555 uci_foreach_element(&ctx->root, e) {
556 uci_export_package(uci_to_package(e), stream, header);
563 int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package, bool single)
565 struct uci_parse_context *pctx;
568 /* make sure no memory from previous parse attempts is leaked */
569 uci_file_cleanup(ctx);
571 pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
574 if (*package && single) {
575 pctx->package = *package;
580 * If 'name' was supplied, assume that the supplied stream does not contain
581 * the appropriate 'package <name>' string to specify the config name
582 * NB: the config file can still override the package name
585 UCI_ASSERT(ctx, uci_validate_name(name));
589 while (!feof(pctx->file)) {
591 UCI_TRAP_SAVE(ctx, error);
593 uci_parse_line(ctx, single);
594 UCI_TRAP_RESTORE(ctx);
597 if (ctx->flags & UCI_FLAG_PERROR)
598 uci_perror(ctx, NULL);
599 if ((ctx->errno != UCI_ERR_PARSE) ||
600 (ctx->flags & UCI_FLAG_STRICT))
601 UCI_THROW(ctx, ctx->errno);
604 uci_fixup_section(ctx, ctx->pctx->section);
606 *package = pctx->package;
608 pctx->package = NULL;
611 uci_switch_config(ctx);
613 /* no error happened, we can get rid of the parser context now */
614 uci_file_cleanup(ctx);
620 * open a stream and go to the right position
622 * note: when opening for write and seeking to the beginning of
623 * the stream, truncate the file
625 static FILE *uci_open_stream(struct uci_context *ctx, const char *filename, int pos, bool write, bool create)
630 int mode = (write ? O_RDWR : O_RDONLY);
635 if (!write && ((stat(filename, &statbuf) < 0) ||
636 ((statbuf.st_mode & S_IFMT) != S_IFREG))) {
637 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
640 fd = open(filename, mode, UCI_FILEMODE);
644 if (flock(fd, (write ? LOCK_EX : LOCK_SH)) < 0)
647 ret = lseek(fd, 0, pos);
652 file = fdopen(fd, (write ? "w+" : "r"));
657 UCI_THROW(ctx, UCI_ERR_IO);
662 static void uci_close_stream(FILE *stream)
674 static void uci_parse_history_line(struct uci_context *ctx, struct uci_package *p, char *buf)
678 char *package = NULL;
679 char *section = NULL;
686 } else if (buf[0] == '@') {
691 UCI_INTERNAL(uci_parse_tuple, ctx, buf, &package, §ion, &option, &value);
692 if (!package || (strcmp(package, p->e.name) != 0))
694 if (!uci_validate_name(section))
696 if (option && !uci_validate_name(option))
698 if ((rename || !delete) && !uci_validate_name(value))
702 UCI_INTERNAL(uci_rename, ctx, p, section, option, value);
704 UCI_INTERNAL(uci_delete, ctx, p, section, option);
706 UCI_INTERNAL(uci_set, ctx, p, section, option, value);
710 UCI_THROW(ctx, UCI_ERR_PARSE);
713 static void uci_parse_history(struct uci_context *ctx, FILE *stream, struct uci_package *p)
715 struct uci_parse_context *pctx;
717 /* make sure no memory from previous parse attempts is leaked */
718 uci_file_cleanup(ctx);
720 pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
724 while (!feof(pctx->file)) {
730 * ignore parse errors in single lines, we want to preserve as much
731 * history as possible
733 UCI_TRAP_SAVE(ctx, error);
734 uci_parse_history_line(ctx, p, pctx->buf);
735 UCI_TRAP_RESTORE(ctx);
740 /* no error happened, we can get rid of the parser context now */
741 uci_file_cleanup(ctx);
744 static void uci_load_history(struct uci_context *ctx, struct uci_package *p, bool flush)
746 char *filename = NULL;
752 if ((asprintf(&filename, "%s/%s", UCI_SAVEDIR, p->e.name) < 0) || !filename)
753 UCI_THROW(ctx, UCI_ERR_MEM);
755 UCI_TRAP_SAVE(ctx, done);
756 f = uci_open_stream(ctx, filename, SEEK_SET, flush, false);
758 uci_parse_history(ctx, f, p);
759 UCI_TRAP_RESTORE(ctx);
764 ftruncate(fileno(f), 0);
773 static char *uci_config_path(struct uci_context *ctx, const char *name)
777 UCI_ASSERT(ctx, uci_validate_name(name));
778 filename = uci_malloc(ctx, strlen(name) + sizeof(UCI_CONFDIR) + 2);
779 sprintf(filename, UCI_CONFDIR "/%s", name);
784 int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package)
794 /* relative path outside of /etc/config */
796 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
799 /* absolute path outside of /etc/config */
800 filename = uci_strdup(ctx, name);
801 name = strrchr(name, '/') + 1;
805 /* config in /etc/config */
806 filename = uci_config_path(ctx, name);
811 file = uci_open_stream(ctx, filename, SEEK_SET, false, false);
813 UCI_TRAP_SAVE(ctx, done);
814 UCI_INTERNAL(uci_import, ctx, file, name, package, true);
815 UCI_TRAP_RESTORE(ctx);
818 (*package)->path = filename;
819 (*package)->confdir = confdir;
820 uci_load_history(ctx, *package, false);
824 uci_close_stream(file);
828 int uci_save(struct uci_context *ctx, struct uci_package *p)
831 char *filename = NULL;
832 struct uci_element *e, *tmp;
835 UCI_ASSERT(ctx, p != NULL);
838 * if the config file was outside of the /etc/config path,
839 * don't save the history to a file, update the real file
841 * does not modify the uci_package pointer
844 return uci_commit(ctx, &p, false);
846 if (uci_list_empty(&p->history))
849 if ((asprintf(&filename, "%s/%s", UCI_SAVEDIR, p->e.name) < 0) || !filename)
850 UCI_THROW(ctx, UCI_ERR_MEM);
853 UCI_TRAP_SAVE(ctx, done);
854 f = uci_open_stream(ctx, filename, SEEK_END, true, true);
855 UCI_TRAP_RESTORE(ctx);
857 uci_foreach_element_safe(&p->history, tmp, e) {
858 struct uci_history *h = uci_to_history(e);
860 if (h->cmd == UCI_CMD_REMOVE)
862 else if (h->cmd == UCI_CMD_RENAME)
865 fprintf(f, "%s.%s", p->e.name, h->section);
867 fprintf(f, ".%s", e->name);
869 if (h->cmd == UCI_CMD_REMOVE)
872 fprintf(f, "=%s\n", h->value);
881 UCI_THROW(ctx, ctx->errno);
886 int uci_commit(struct uci_context *ctx, struct uci_package **package, bool overwrite)
888 struct uci_package *p;
894 UCI_ASSERT(ctx, package != NULL);
897 UCI_ASSERT(ctx, p != NULL);
900 p->path = uci_config_path(ctx, p->e.name);
902 UCI_THROW(ctx, UCI_ERR_INVAL);
906 /* open the config file for writing now, so that it is locked */
907 f = uci_open_stream(ctx, p->path, SEEK_SET, true, true);
909 /* flush unsaved changes and reload from history file */
910 UCI_TRAP_SAVE(ctx, done);
913 name = uci_strdup(ctx, p->e.name);
914 path = uci_strdup(ctx, p->path);
915 /* dump our own changes to the history file */
916 if (!uci_list_empty(&p->history))
917 UCI_INTERNAL(uci_save, ctx, p);
920 * other processes might have modified the config
921 * as well. dump and reload
923 uci_free_package(&p);
924 uci_file_cleanup(ctx);
925 UCI_INTERNAL(uci_import, ctx, f, name, &p, true);
931 /* freed together with the uci_package */
934 /* check for updated history, just in case */
935 uci_load_history(ctx, p, true);
938 uci_load_history(ctx, NULL, true);
943 ftruncate(fileno(f), 0);
945 uci_export(ctx, f, p, false);
946 UCI_TRAP_RESTORE(ctx);
955 UCI_THROW(ctx, ctx->errno);
962 * This function returns the filename by returning the string
963 * after the last '/' character. By checking for a non-'\0'
964 * character afterwards, directories are ignored (glob marks
965 * those with a trailing '/'
967 static inline char *get_filename(char *path)
971 p = strrchr(path, '/');
978 int uci_list_configs(struct uci_context *ctx, char ***list)
987 if (glob(UCI_CONFDIR "/*", GLOB_MARK, NULL, &globbuf) != 0)
988 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
990 size = sizeof(char *) * (globbuf.gl_pathc + 1);
991 for(i = 0; i < globbuf.gl_pathc; i++) {
994 p = get_filename(globbuf.gl_pathv[i]);
998 size += strlen(p) + 1;
1001 configs = uci_malloc(ctx, size);
1002 buf = (char *) &configs[globbuf.gl_pathc + 1];
1003 for(i = 0; i < globbuf.gl_pathc; i++) {
1006 p = get_filename(globbuf.gl_pathv[i]);
1012 buf += strlen(buf) + 1;