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 static struct uci_backend uci_file_backend
;
32 * verify that the end of the line or command is reached.
33 * throw an error if extra arguments are given on the command line
35 static void assert_eol(struct uci_context
*ctx
, char **str
)
39 skip_whitespace(ctx
, str
);
40 tmp
= next_arg(ctx
, str
, false, false);
41 if (*tmp
&& (ctx
->flags
& UCI_FLAG_STRICT
))
42 uci_parse_error(ctx
, *str
, "too many arguments");
46 * switch to a different config, either triggered by uci_load, or by a
47 * 'package <...>' statement in the import file
49 static void uci_switch_config(struct uci_context
*ctx
)
51 struct uci_parse_context
*pctx
;
52 struct uci_element
*e
;
58 /* add the last config to main config file list */
60 pctx
->package
->backend
= ctx
->backend
;
61 uci_list_add(&ctx
->root
, &pctx
->package
->e
.list
);
71 * if an older config under the same name exists, unload it
72 * ignore errors here, e.g. if the config was not found
74 e
= uci_lookup_list(&ctx
->root
, name
);
76 UCI_THROW(ctx
, UCI_ERR_DUPLICATE
);
77 pctx
->package
= uci_alloc_package(ctx
, name
);
81 * parse the 'package' uci command (next config package)
83 static void uci_parse_package(struct uci_context
*ctx
, char **str
, bool single
)
87 /* command string null-terminated by strtok */
88 *str
+= strlen(*str
) + 1;
90 name
= next_arg(ctx
, str
, true, true);
95 ctx
->pctx
->name
= name
;
96 uci_switch_config(ctx
);
100 * parse the 'config' uci command (open a section)
102 static void uci_parse_config(struct uci_context
*ctx
, char **str
)
104 struct uci_parse_context
*pctx
= ctx
->pctx
;
108 uci_fixup_section(ctx
, ctx
->pctx
->section
);
109 if (!ctx
->pctx
->package
) {
110 if (!ctx
->pctx
->name
)
111 uci_parse_error(ctx
, *str
, "attempting to import a file without a package name");
113 uci_switch_config(ctx
);
116 /* command string null-terminated by strtok */
117 *str
+= strlen(*str
) + 1;
119 type
= next_arg(ctx
, str
, true, false);
120 if (!uci_validate_str(type
, false))
121 uci_parse_error(ctx
, type
, "invalid character in field");
122 name
= next_arg(ctx
, str
, false, true);
123 assert_eol(ctx
, str
);
126 UCI_TRAP_SAVE(ctx
, error
);
127 if (uci_set(ctx
, pctx
->package
, name
, NULL
, type
, NULL
) != UCI_OK
)
129 UCI_TRAP_RESTORE(ctx
);
132 UCI_THROW(ctx
, ctx
->err
);
134 pctx
->section
= uci_alloc_section(pctx
->package
, type
, name
);
138 * parse the 'option' uci command (open a value)
140 static void uci_parse_option(struct uci_context
*ctx
, char **str
)
142 struct uci_parse_context
*pctx
= ctx
->pctx
;
147 uci_parse_error(ctx
, *str
, "option command found before the first section");
149 /* command string null-terminated by strtok */
150 *str
+= strlen(*str
) + 1;
152 name
= next_arg(ctx
, str
, true, true);
153 value
= next_arg(ctx
, str
, false, false);
154 assert_eol(ctx
, str
);
157 UCI_TRAP_SAVE(ctx
, error
);
158 uci_set(ctx
, pctx
->package
, pctx
->section
->e
.name
, name
, value
, NULL
);
159 UCI_TRAP_RESTORE(ctx
);
162 UCI_THROW(ctx
, ctx
->err
);
164 uci_alloc_option(pctx
->section
, name
, value
);
167 static void uci_parse_list(struct uci_context
*ctx
, char **str
)
169 struct uci_parse_context
*pctx
= ctx
->pctx
;
175 uci_parse_error(ctx
, *str
, "list command found before the first section");
177 /* command string null-terminated by strtok */
178 *str
+= strlen(*str
) + 1;
180 name
= next_arg(ctx
, str
, true, true);
181 value
= next_arg(ctx
, str
, false, false);
182 assert_eol(ctx
, str
);
184 UCI_NESTED(uci_fill_ptr
, ctx
, &ptr
, &pctx
->section
->e
, false);
188 UCI_INTERNAL(uci_lookup_ptr
, ctx
, &ptr
, NULL
, false);
190 ctx
->internal
= !pctx
->merge
;
191 UCI_NESTED(uci_add_list
, ctx
, &ptr
);
196 * parse a complete input line, split up combined commands by ';'
198 static void uci_parse_line(struct uci_context
*ctx
, bool single
)
200 struct uci_parse_context
*pctx
= ctx
->pctx
;
206 word
= strtok_r(word
, " \t", &brk
);
215 if ((word
[1] == 0) || !strcmp(word
+ 1, "ackage"))
216 uci_parse_package(ctx
, &word
, single
);
221 if ((word
[1] == 0) || !strcmp(word
+ 1, "onfig"))
222 uci_parse_config(ctx
, &word
);
227 if ((word
[1] == 0) || !strcmp(word
+ 1, "ption"))
228 uci_parse_option(ctx
, &word
);
233 if ((word
[1] == 0) || !strcmp(word
+ 1, "ist"))
234 uci_parse_list(ctx
, &word
);
243 uci_parse_error(ctx
, word
, "invalid command");
247 /* max number of characters that escaping adds to the string */
248 #define UCI_QUOTE_ESCAPE "'\\''"
251 * escape an uci string for export
253 static char *uci_escape(struct uci_context
*ctx
, const char *str
)
259 ctx
->bufsz
= LINEBUF
;
260 ctx
->buf
= malloc(LINEBUF
);
266 end
= strchr(str
, '\'');
268 end
= str
+ strlen(str
);
271 /* make sure that we have enough room in the buffer */
272 while (ofs
+ len
+ sizeof(UCI_QUOTE_ESCAPE
) + 1 > ctx
->bufsz
) {
274 ctx
->buf
= uci_realloc(ctx
, ctx
->buf
, ctx
->bufsz
);
277 /* copy the string until the character before the quote */
278 memcpy(&ctx
->buf
[ofs
], str
, len
);
281 /* end of string? return the buffer */
285 memcpy(&ctx
->buf
[ofs
], UCI_QUOTE_ESCAPE
, sizeof(UCI_QUOTE_ESCAPE
));
286 ofs
+= strlen(&ctx
->buf
[ofs
]);
295 * export a single config package to a file stream
297 static void uci_export_package(struct uci_package
*p
, FILE *stream
, bool header
)
299 struct uci_context
*ctx
= p
->ctx
;
300 struct uci_element
*s
, *o
, *i
;
303 fprintf(stream
, "package '%s'\n", uci_escape(ctx
, p
->e
.name
));
304 uci_foreach_element(&p
->sections
, s
) {
305 struct uci_section
*sec
= uci_to_section(s
);
306 fprintf(stream
, "\nconfig '%s'", uci_escape(ctx
, sec
->type
));
307 if (!sec
->anonymous
|| (ctx
->flags
& UCI_FLAG_EXPORT_NAME
))
308 fprintf(stream
, " '%s'", uci_escape(ctx
, sec
->e
.name
));
309 fprintf(stream
, "\n");
310 uci_foreach_element(&sec
->options
, o
) {
311 struct uci_option
*opt
= uci_to_option(o
);
313 case UCI_TYPE_STRING
:
314 fprintf(stream
, "\toption '%s'", uci_escape(ctx
, opt
->e
.name
));
315 fprintf(stream
, " '%s'\n", uci_escape(ctx
, opt
->v
.string
));
318 uci_foreach_element(&opt
->v
.list
, i
) {
319 fprintf(stream
, "\tlist '%s'", uci_escape(ctx
, opt
->e
.name
));
320 fprintf(stream
, " '%s'\n", uci_escape(ctx
, i
->name
));
324 fprintf(stream
, "\t# unknown type for option '%s'\n", uci_escape(ctx
, opt
->e
.name
));
329 fprintf(stream
, "\n");
332 int uci_export(struct uci_context
*ctx
, FILE *stream
, struct uci_package
*package
, bool header
)
334 struct uci_element
*e
;
337 UCI_ASSERT(ctx
, stream
!= NULL
);
340 uci_export_package(package
, stream
, header
);
342 uci_foreach_element(&ctx
->root
, e
) {
343 uci_export_package(uci_to_package(e
), stream
, header
);
350 int uci_import(struct uci_context
*ctx
, FILE *stream
, const char *name
, struct uci_package
**package
, bool single
)
352 struct uci_parse_context
*pctx
;
355 /* make sure no memory from previous parse attempts is leaked */
358 uci_alloc_parse_context(ctx
);
361 if (*package
&& single
) {
362 pctx
->package
= *package
;
367 * If 'name' was supplied, assume that the supplied stream does not contain
368 * the appropriate 'package <name>' string to specify the config name
369 * NB: the config file can still override the package name
372 UCI_ASSERT(ctx
, uci_validate_str(name
, false));
376 while (!feof(pctx
->file
)) {
378 UCI_TRAP_SAVE(ctx
, error
);
380 uci_parse_line(ctx
, single
);
381 UCI_TRAP_RESTORE(ctx
);
384 if (ctx
->flags
& UCI_FLAG_PERROR
)
385 uci_perror(ctx
, NULL
);
386 if ((ctx
->err
!= UCI_ERR_PARSE
) ||
387 (ctx
->flags
& UCI_FLAG_STRICT
))
388 UCI_THROW(ctx
, ctx
->err
);
391 uci_fixup_section(ctx
, ctx
->pctx
->section
);
392 if (!pctx
->package
&& name
)
393 uci_switch_config(ctx
);
395 *package
= pctx
->package
;
397 pctx
->package
= NULL
;
400 uci_switch_config(ctx
);
402 /* no error happened, we can get rid of the parser context now */
409 static char *uci_config_path(struct uci_context
*ctx
, const char *name
)
413 UCI_ASSERT(ctx
, uci_validate_str(name
, false));
414 filename
= uci_malloc(ctx
, strlen(name
) + strlen(ctx
->confdir
) + 2);
415 sprintf(filename
, "%s/%s", ctx
->confdir
, name
);
420 void uci_file_commit(struct uci_context
*ctx
, struct uci_package
**package
, bool overwrite
)
422 struct uci_package
*p
= *package
;
429 p
->path
= uci_config_path(ctx
, p
->e
.name
);
431 UCI_THROW(ctx
, UCI_ERR_INVAL
);
434 /* open the config file for writing now, so that it is locked */
435 f
= uci_open_stream(ctx
, p
->path
, SEEK_SET
, true, true);
437 /* flush unsaved changes and reload from history file */
438 UCI_TRAP_SAVE(ctx
, done
);
439 if (p
->has_history
) {
441 name
= uci_strdup(ctx
, p
->e
.name
);
442 path
= uci_strdup(ctx
, p
->path
);
443 /* dump our own changes to the history file */
444 if (!uci_list_empty(&p
->history
))
445 UCI_INTERNAL(uci_save
, ctx
, p
);
448 * other processes might have modified the config
449 * as well. dump and reload
451 uci_free_package(&p
);
453 UCI_INTERNAL(uci_import
, ctx
, f
, name
, &p
, true);
456 p
->has_history
= true;
459 /* freed together with the uci_package */
462 /* check for updated history, flush */
463 if (!uci_load_history(ctx
, p
, true))
467 if (!uci_load_history(ctx
, NULL
, true))
473 ftruncate(fileno(f
), 0);
475 uci_export(ctx
, f
, p
, false);
476 UCI_TRAP_RESTORE(ctx
);
485 UCI_THROW(ctx
, ctx
->err
);
490 * This function returns the filename by returning the string
491 * after the last '/' character. By checking for a non-'\0'
492 * character afterwards, directories are ignored (glob marks
493 * those with a trailing '/'
495 static inline char *get_filename(char *path
)
499 p
= strrchr(path
, '/');
506 static char **uci_list_config_files(struct uci_context
*ctx
)
514 dir
= uci_malloc(ctx
, strlen(ctx
->confdir
) + 1 + sizeof("/*"));
515 sprintf(dir
, "%s/*", ctx
->confdir
);
516 if (glob(dir
, GLOB_MARK
, NULL
, &globbuf
) != 0)
517 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
519 size
= sizeof(char *) * (globbuf
.gl_pathc
+ 1);
520 for(i
= 0; i
< globbuf
.gl_pathc
; i
++) {
523 p
= get_filename(globbuf
.gl_pathv
[i
]);
527 size
+= strlen(p
) + 1;
530 configs
= uci_malloc(ctx
, size
);
531 buf
= (char *) &configs
[globbuf
.gl_pathc
+ 1];
532 for(i
= 0; i
< globbuf
.gl_pathc
; i
++) {
535 p
= get_filename(globbuf
.gl_pathv
[i
]);
539 if (!uci_validate_name(p
))
544 buf
+= strlen(buf
) + 1;
550 static struct uci_package
*uci_file_load(struct uci_context
*ctx
, const char *name
)
552 struct uci_package
*package
= NULL
;
559 /* relative path outside of /etc/config */
561 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
564 /* absolute path outside of /etc/config */
565 filename
= uci_strdup(ctx
, name
);
566 name
= strrchr(name
, '/') + 1;
570 /* config in /etc/config */
571 filename
= uci_config_path(ctx
, name
);
576 file
= uci_open_stream(ctx
, filename
, SEEK_SET
, false, false);
578 UCI_TRAP_SAVE(ctx
, done
);
579 UCI_INTERNAL(uci_import
, ctx
, file
, name
, &package
, true);
580 UCI_TRAP_RESTORE(ctx
);
583 package
->path
= filename
;
584 package
->has_history
= confdir
;
585 uci_load_history(ctx
, package
, false);
589 uci_close_stream(file
);
591 UCI_THROW(ctx
, ctx
->err
);
595 static UCI_BACKEND(uci_file_backend
, "file",
596 .load
= uci_file_load
,
597 .commit
= uci_file_commit
,
598 .list_configs
= uci_list_config_files
,