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
;
105 struct uci_element
*e
;
110 uci_fixup_section(ctx
, ctx
->pctx
->section
);
111 if (!ctx
->pctx
->package
) {
112 if (!ctx
->pctx
->name
)
113 uci_parse_error(ctx
, *str
, "attempting to import a file without a package name");
115 uci_switch_config(ctx
);
118 /* command string null-terminated by strtok */
119 *str
+= strlen(*str
) + 1;
121 type
= next_arg(ctx
, str
, true, false);
122 if (!uci_validate_type(type
))
123 uci_parse_error(ctx
, type
, "invalid character in field");
124 name
= next_arg(ctx
, str
, false, true);
125 assert_eol(ctx
, str
);
128 ctx
->internal
= !pctx
->merge
;
129 UCI_NESTED(uci_add_section
, ctx
, pctx
->package
, type
, &pctx
->section
);
131 UCI_NESTED(uci_fill_ptr
, ctx
, &ptr
, &pctx
->package
->e
, false);
132 e
= uci_lookup_list(&pctx
->package
->sections
, name
);
134 ptr
.s
= uci_to_section(e
);
138 ctx
->internal
= !pctx
->merge
;
139 UCI_NESTED(uci_set
, ctx
, &ptr
);
140 pctx
->section
= uci_to_section(ptr
.last
);
145 * parse the 'option' uci command (open a value)
147 static void uci_parse_option(struct uci_context
*ctx
, char **str
, bool list
)
149 struct uci_parse_context
*pctx
= ctx
->pctx
;
150 struct uci_element
*e
;
156 uci_parse_error(ctx
, *str
, "option/list command found before the first section");
158 /* command string null-terminated by strtok */
159 *str
+= strlen(*str
) + 1;
161 name
= next_arg(ctx
, str
, true, true);
162 value
= next_arg(ctx
, str
, false, false);
163 assert_eol(ctx
, str
);
165 UCI_NESTED(uci_fill_ptr
, ctx
, &ptr
, &pctx
->section
->e
, false);
166 e
= uci_lookup_list(&pctx
->section
->options
, name
);
168 ptr
.o
= uci_to_option(e
);
172 ctx
->internal
= !pctx
->merge
;
174 UCI_NESTED(uci_add_list
, ctx
, &ptr
);
176 UCI_NESTED(uci_set
, ctx
, &ptr
);
180 * parse a complete input line, split up combined commands by ';'
182 static void uci_parse_line(struct uci_context
*ctx
, bool single
)
184 struct uci_parse_context
*pctx
= ctx
->pctx
;
190 word
= strtok_r(word
, " \t", &brk
);
199 if ((word
[1] == 0) || !strcmp(word
+ 1, "ackage"))
200 uci_parse_package(ctx
, &word
, single
);
205 if ((word
[1] == 0) || !strcmp(word
+ 1, "onfig"))
206 uci_parse_config(ctx
, &word
);
211 if ((word
[1] == 0) || !strcmp(word
+ 1, "ption"))
212 uci_parse_option(ctx
, &word
, false);
217 if ((word
[1] == 0) || !strcmp(word
+ 1, "ist"))
218 uci_parse_option(ctx
, &word
, true);
227 uci_parse_error(ctx
, word
, "invalid command");
231 /* max number of characters that escaping adds to the string */
232 #define UCI_QUOTE_ESCAPE "'\\''"
235 * escape an uci string for export
237 static char *uci_escape(struct uci_context
*ctx
, const char *str
)
243 ctx
->bufsz
= LINEBUF
;
244 ctx
->buf
= malloc(LINEBUF
);
250 end
= strchr(str
, '\'');
252 end
= str
+ strlen(str
);
255 /* make sure that we have enough room in the buffer */
256 while (ofs
+ len
+ sizeof(UCI_QUOTE_ESCAPE
) + 1 > ctx
->bufsz
) {
258 ctx
->buf
= uci_realloc(ctx
, ctx
->buf
, ctx
->bufsz
);
261 /* copy the string until the character before the quote */
262 memcpy(&ctx
->buf
[ofs
], str
, len
);
265 /* end of string? return the buffer */
269 memcpy(&ctx
->buf
[ofs
], UCI_QUOTE_ESCAPE
, sizeof(UCI_QUOTE_ESCAPE
));
270 ofs
+= strlen(&ctx
->buf
[ofs
]);
279 * export a single config package to a file stream
281 static void uci_export_package(struct uci_package
*p
, FILE *stream
, bool header
)
283 struct uci_context
*ctx
= p
->ctx
;
284 struct uci_element
*s
, *o
, *i
;
287 fprintf(stream
, "package '%s'\n", uci_escape(ctx
, p
->e
.name
));
288 uci_foreach_element(&p
->sections
, s
) {
289 struct uci_section
*sec
= uci_to_section(s
);
290 fprintf(stream
, "\nconfig '%s'", uci_escape(ctx
, sec
->type
));
291 if (!sec
->anonymous
|| (ctx
->flags
& UCI_FLAG_EXPORT_NAME
))
292 fprintf(stream
, " '%s'", uci_escape(ctx
, sec
->e
.name
));
293 fprintf(stream
, "\n");
294 uci_foreach_element(&sec
->options
, o
) {
295 struct uci_option
*opt
= uci_to_option(o
);
297 case UCI_TYPE_STRING
:
298 fprintf(stream
, "\toption '%s'", uci_escape(ctx
, opt
->e
.name
));
299 fprintf(stream
, " '%s'\n", uci_escape(ctx
, opt
->v
.string
));
302 uci_foreach_element(&opt
->v
.list
, i
) {
303 fprintf(stream
, "\tlist '%s'", uci_escape(ctx
, opt
->e
.name
));
304 fprintf(stream
, " '%s'\n", uci_escape(ctx
, i
->name
));
308 fprintf(stream
, "\t# unknown type for option '%s'\n", uci_escape(ctx
, opt
->e
.name
));
313 fprintf(stream
, "\n");
316 int uci_export(struct uci_context
*ctx
, FILE *stream
, struct uci_package
*package
, bool header
)
318 struct uci_element
*e
;
321 UCI_ASSERT(ctx
, stream
!= NULL
);
324 uci_export_package(package
, stream
, header
);
326 uci_foreach_element(&ctx
->root
, e
) {
327 uci_export_package(uci_to_package(e
), stream
, header
);
334 int uci_import(struct uci_context
*ctx
, FILE *stream
, const char *name
, struct uci_package
**package
, bool single
)
336 struct uci_parse_context
*pctx
;
339 /* make sure no memory from previous parse attempts is leaked */
342 uci_alloc_parse_context(ctx
);
345 if (*package
&& single
) {
346 pctx
->package
= *package
;
351 * If 'name' was supplied, assume that the supplied stream does not contain
352 * the appropriate 'package <name>' string to specify the config name
353 * NB: the config file can still override the package name
356 UCI_ASSERT(ctx
, uci_validate_package(name
));
360 while (!feof(pctx
->file
)) {
362 UCI_TRAP_SAVE(ctx
, error
);
364 uci_parse_line(ctx
, single
);
365 UCI_TRAP_RESTORE(ctx
);
368 if (ctx
->flags
& UCI_FLAG_PERROR
)
369 uci_perror(ctx
, NULL
);
370 if ((ctx
->err
!= UCI_ERR_PARSE
) ||
371 (ctx
->flags
& UCI_FLAG_STRICT
))
372 UCI_THROW(ctx
, ctx
->err
);
375 uci_fixup_section(ctx
, ctx
->pctx
->section
);
376 if (!pctx
->package
&& name
)
377 uci_switch_config(ctx
);
379 *package
= pctx
->package
;
381 pctx
->package
= NULL
;
384 uci_switch_config(ctx
);
386 /* no error happened, we can get rid of the parser context now */
393 static char *uci_config_path(struct uci_context
*ctx
, const char *name
)
397 UCI_ASSERT(ctx
, uci_validate_package(name
));
398 filename
= uci_malloc(ctx
, strlen(name
) + strlen(ctx
->confdir
) + 2);
399 sprintf(filename
, "%s/%s", ctx
->confdir
, name
);
404 void uci_file_commit(struct uci_context
*ctx
, struct uci_package
**package
, bool overwrite
)
406 struct uci_package
*p
= *package
;
413 p
->path
= uci_config_path(ctx
, p
->e
.name
);
415 UCI_THROW(ctx
, UCI_ERR_INVAL
);
418 /* open the config file for writing now, so that it is locked */
419 f
= uci_open_stream(ctx
, p
->path
, SEEK_SET
, true, true);
421 /* flush unsaved changes and reload from history file */
422 UCI_TRAP_SAVE(ctx
, done
);
423 if (p
->has_history
) {
425 name
= uci_strdup(ctx
, p
->e
.name
);
426 path
= uci_strdup(ctx
, p
->path
);
427 /* dump our own changes to the history file */
428 if (!uci_list_empty(&p
->history
))
429 UCI_INTERNAL(uci_save
, ctx
, p
);
432 * other processes might have modified the config
433 * as well. dump and reload
435 uci_free_package(&p
);
437 UCI_INTERNAL(uci_import
, ctx
, f
, name
, &p
, true);
440 p
->has_history
= true;
443 /* freed together with the uci_package */
446 /* check for updated history, flush */
447 if (!uci_load_history(ctx
, p
, true))
451 if (!uci_load_history(ctx
, NULL
, true))
457 ftruncate(fileno(f
), 0);
459 uci_export(ctx
, f
, p
, false);
460 UCI_TRAP_RESTORE(ctx
);
469 UCI_THROW(ctx
, ctx
->err
);
474 * This function returns the filename by returning the string
475 * after the last '/' character. By checking for a non-'\0'
476 * character afterwards, directories are ignored (glob marks
477 * those with a trailing '/'
479 static inline char *get_filename(char *path
)
483 p
= strrchr(path
, '/');
490 static char **uci_list_config_files(struct uci_context
*ctx
)
498 dir
= uci_malloc(ctx
, strlen(ctx
->confdir
) + 1 + sizeof("/*"));
499 sprintf(dir
, "%s/*", ctx
->confdir
);
500 if (glob(dir
, GLOB_MARK
, NULL
, &globbuf
) != 0)
501 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
503 size
= sizeof(char *) * (globbuf
.gl_pathc
+ 1);
504 for(i
= 0; i
< globbuf
.gl_pathc
; i
++) {
507 p
= get_filename(globbuf
.gl_pathv
[i
]);
511 size
+= strlen(p
) + 1;
514 configs
= uci_malloc(ctx
, size
);
515 buf
= (char *) &configs
[globbuf
.gl_pathc
+ 1];
516 for(i
= 0; i
< globbuf
.gl_pathc
; i
++) {
519 p
= get_filename(globbuf
.gl_pathv
[i
]);
523 if (!uci_validate_package(p
))
528 buf
+= strlen(buf
) + 1;
535 static struct uci_package
*uci_file_load(struct uci_context
*ctx
, const char *name
)
537 struct uci_package
*package
= NULL
;
544 /* relative path outside of /etc/config */
546 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
549 /* absolute path outside of /etc/config */
550 filename
= uci_strdup(ctx
, name
);
551 name
= strrchr(name
, '/') + 1;
555 /* config in /etc/config */
556 filename
= uci_config_path(ctx
, name
);
561 file
= uci_open_stream(ctx
, filename
, SEEK_SET
, false, false);
563 UCI_TRAP_SAVE(ctx
, done
);
564 UCI_INTERNAL(uci_import
, ctx
, file
, name
, &package
, true);
565 UCI_TRAP_RESTORE(ctx
);
568 package
->path
= filename
;
569 package
->has_history
= confdir
;
570 uci_load_history(ctx
, package
, false);
574 uci_close_stream(file
);
576 UCI_THROW(ctx
, ctx
->err
);
580 static UCI_BACKEND(uci_file_backend
, "file",
581 .load
= uci_file_load
,
582 .commit
= uci_file_commit
,
583 .list_configs
= uci_list_config_files
,