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
)
37 * verify that the end of the line or command is reached.
38 * throw an error if extra arguments are given on the command line
40 static void assert_eol(struct uci_context
*ctx
, char **str
)
44 tmp
= next_arg(ctx
, str
, false, false);
45 if (*tmp
&& (ctx
->flags
& UCI_FLAG_STRICT
))
46 uci_parse_error(ctx
, *str
, "too many arguments");
50 * switch to a different config, either triggered by uci_load, or by a
51 * 'package <...>' statement in the import file
53 static void uci_switch_config(struct uci_context
*ctx
)
55 struct uci_parse_context
*pctx
;
56 struct uci_element
*e
;
62 /* add the last config to main config file list */
64 uci_list_add(&ctx
->root
, &pctx
->package
->e
.list
);
74 * if an older config under the same name exists, unload it
75 * ignore errors here, e.g. if the config was not found
77 e
= uci_lookup_list(ctx
, &ctx
->root
, name
);
79 UCI_THROW(ctx
, UCI_ERR_DUPLICATE
);
80 pctx
->package
= uci_alloc_package(ctx
, name
);
84 * parse the 'package' uci command (next config package)
86 static void uci_parse_package(struct uci_context
*ctx
, char **str
, bool single
)
90 /* command string null-terminated by strtok */
91 *str
+= strlen(*str
) + 1;
93 name
= next_arg(ctx
, str
, true, true);
98 ctx
->pctx
->name
= name
;
99 uci_switch_config(ctx
);
103 * parse the 'config' uci command (open a section)
105 static void uci_parse_config(struct uci_context
*ctx
, char **str
)
107 struct uci_parse_context
*pctx
= ctx
->pctx
;
111 uci_fixup_section(ctx
, ctx
->pctx
->section
);
112 if (!ctx
->pctx
->package
) {
113 if (!ctx
->pctx
->name
)
114 uci_parse_error(ctx
, *str
, "attempting to import a file without a package name");
116 uci_switch_config(ctx
);
119 /* command string null-terminated by strtok */
120 *str
+= strlen(*str
) + 1;
122 type
= next_arg(ctx
, str
, true, false);
123 if (!uci_validate_str(type
, false))
124 uci_parse_error(ctx
, type
, "invalid character in field");
125 name
= next_arg(ctx
, str
, false, true);
126 assert_eol(ctx
, str
);
129 UCI_TRAP_SAVE(ctx
, error
);
130 uci_set(ctx
, pctx
->package
, name
, NULL
, type
, NULL
);
131 UCI_TRAP_RESTORE(ctx
);
134 UCI_THROW(ctx
, ctx
->errno
);
136 pctx
->section
= uci_alloc_section(pctx
->package
, type
, name
);
140 * parse the 'option' uci command (open a value)
142 static void uci_parse_option(struct uci_context
*ctx
, char **str
)
144 struct uci_parse_context
*pctx
= ctx
->pctx
;
149 uci_parse_error(ctx
, *str
, "option command found before the first section");
151 /* command string null-terminated by strtok */
152 *str
+= strlen(*str
) + 1;
154 name
= next_arg(ctx
, str
, true, true);
155 value
= next_arg(ctx
, str
, false, false);
156 assert_eol(ctx
, str
);
159 UCI_TRAP_SAVE(ctx
, error
);
160 uci_set(ctx
, pctx
->package
, pctx
->section
->e
.name
, name
, value
, NULL
);
161 UCI_TRAP_RESTORE(ctx
);
164 UCI_THROW(ctx
, ctx
->errno
);
166 uci_alloc_option(pctx
->section
, name
, value
);
171 * parse a complete input line, split up combined commands by ';'
173 static void uci_parse_line(struct uci_context
*ctx
, bool single
)
175 struct uci_parse_context
*pctx
= ctx
->pctx
;
176 char *word
, *brk
= NULL
;
178 for (word
= strtok_r(pctx
->buf
, ";", &brk
);
180 word
= strtok_r(NULL
, ";", &brk
)) {
183 word
= strtok_r(word
, " \t", &pbrk
);
192 if ((word
[1] == 0) || !strcmp(word
+ 1, "ackage"))
193 uci_parse_package(ctx
, &word
, single
);
196 if ((word
[1] == 0) || !strcmp(word
+ 1, "onfig"))
197 uci_parse_config(ctx
, &word
);
200 if ((word
[1] == 0) || !strcmp(word
+ 1, "ption"))
201 uci_parse_option(ctx
, &word
);
204 uci_parse_error(ctx
, word
, "unterminated command");
210 /* max number of characters that escaping adds to the string */
211 #define UCI_QUOTE_ESCAPE "'\\''"
214 * escape an uci string for export
216 static char *uci_escape(struct uci_context
*ctx
, char *str
)
222 ctx
->bufsz
= LINEBUF
;
223 ctx
->buf
= malloc(LINEBUF
);
227 p
= strchr(str
, '\'');
234 if (p
+ sizeof(UCI_QUOTE_ESCAPE
) - str
>= ctx
->bufsz
) {
236 ctx
->buf
= realloc(ctx
->buf
, ctx
->bufsz
);
238 UCI_THROW(ctx
, UCI_ERR_MEM
);
240 memcpy(&ctx
->buf
[pos
], s
, len
);
243 strcpy(&ctx
->buf
[pos
], UCI_QUOTE_ESCAPE
);
244 pos
+= sizeof(UCI_QUOTE_ESCAPE
);
246 } while ((p
= strchr(s
, '\'')));
253 * export a single config package to a file stream
255 static void uci_export_package(struct uci_package
*p
, FILE *stream
, bool header
)
257 struct uci_context
*ctx
= p
->ctx
;
258 struct uci_element
*s
, *o
;
261 fprintf(stream
, "package '%s'\n", uci_escape(ctx
, p
->e
.name
));
262 uci_foreach_element(&p
->sections
, s
) {
263 struct uci_section
*sec
= uci_to_section(s
);
264 fprintf(stream
, "\nconfig '%s'", uci_escape(ctx
, sec
->type
));
265 if (!sec
->anonymous
|| (ctx
->flags
& UCI_FLAG_EXPORT_NAME
))
266 fprintf(stream
, " '%s'", uci_escape(ctx
, sec
->e
.name
));
267 fprintf(stream
, "\n");
268 uci_foreach_element(&sec
->options
, o
) {
269 struct uci_option
*opt
= uci_to_option(o
);
270 fprintf(stream
, "\toption '%s'", uci_escape(ctx
, opt
->e
.name
));
271 fprintf(stream
, " '%s'\n", uci_escape(ctx
, opt
->value
));
274 fprintf(stream
, "\n");
277 int uci_export(struct uci_context
*ctx
, FILE *stream
, struct uci_package
*package
, bool header
)
279 struct uci_element
*e
;
282 UCI_ASSERT(ctx
, stream
!= NULL
);
285 uci_export_package(package
, stream
, header
);
287 uci_foreach_element(&ctx
->root
, e
) {
288 uci_export_package(uci_to_package(e
), stream
, header
);
295 int uci_import(struct uci_context
*ctx
, FILE *stream
, const char *name
, struct uci_package
**package
, bool single
)
297 struct uci_parse_context
*pctx
;
300 /* make sure no memory from previous parse attempts is leaked */
301 uci_file_cleanup(ctx
);
303 uci_alloc_parse_context(ctx
);
306 if (*package
&& single
) {
307 pctx
->package
= *package
;
312 * If 'name' was supplied, assume that the supplied stream does not contain
313 * the appropriate 'package <name>' string to specify the config name
314 * NB: the config file can still override the package name
317 UCI_ASSERT(ctx
, uci_validate_name(name
));
321 while (!feof(pctx
->file
)) {
323 UCI_TRAP_SAVE(ctx
, error
);
325 uci_parse_line(ctx
, single
);
326 UCI_TRAP_RESTORE(ctx
);
329 if (ctx
->flags
& UCI_FLAG_PERROR
)
330 uci_perror(ctx
, NULL
);
331 if ((ctx
->errno
!= UCI_ERR_PARSE
) ||
332 (ctx
->flags
& UCI_FLAG_STRICT
))
333 UCI_THROW(ctx
, ctx
->errno
);
336 uci_fixup_section(ctx
, ctx
->pctx
->section
);
338 *package
= pctx
->package
;
340 pctx
->package
= NULL
;
343 uci_switch_config(ctx
);
345 /* no error happened, we can get rid of the parser context now */
346 uci_file_cleanup(ctx
);
352 static char *uci_config_path(struct uci_context
*ctx
, const char *name
)
356 UCI_ASSERT(ctx
, uci_validate_name(name
));
357 filename
= uci_malloc(ctx
, strlen(name
) + strlen(ctx
->confdir
) + 2);
358 sprintf(filename
, "%s/%s", ctx
->confdir
, name
);
363 int uci_load(struct uci_context
*ctx
, const char *name
, struct uci_package
**package
)
373 /* relative path outside of /etc/config */
375 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
378 /* absolute path outside of /etc/config */
379 filename
= uci_strdup(ctx
, name
);
380 name
= strrchr(name
, '/') + 1;
384 /* config in /etc/config */
385 filename
= uci_config_path(ctx
, name
);
390 file
= uci_open_stream(ctx
, filename
, SEEK_SET
, false, false);
392 UCI_TRAP_SAVE(ctx
, done
);
393 UCI_INTERNAL(uci_import
, ctx
, file
, name
, package
, true);
394 UCI_TRAP_RESTORE(ctx
);
397 (*package
)->path
= filename
;
398 (*package
)->confdir
= confdir
;
399 uci_load_history(ctx
, *package
, false);
403 uci_close_stream(file
);
407 int uci_commit(struct uci_context
*ctx
, struct uci_package
**package
, bool overwrite
)
409 struct uci_package
*p
;
415 UCI_ASSERT(ctx
, package
!= NULL
);
418 UCI_ASSERT(ctx
, p
!= NULL
);
421 p
->path
= uci_config_path(ctx
, p
->e
.name
);
423 UCI_THROW(ctx
, UCI_ERR_INVAL
);
427 /* open the config file for writing now, so that it is locked */
428 f
= uci_open_stream(ctx
, p
->path
, SEEK_SET
, true, true);
430 /* flush unsaved changes and reload from history file */
431 UCI_TRAP_SAVE(ctx
, done
);
434 name
= uci_strdup(ctx
, p
->e
.name
);
435 path
= uci_strdup(ctx
, p
->path
);
436 /* dump our own changes to the history file */
437 if (!uci_list_empty(&p
->history
))
438 UCI_INTERNAL(uci_save
, ctx
, p
);
441 * other processes might have modified the config
442 * as well. dump and reload
444 uci_free_package(&p
);
445 uci_file_cleanup(ctx
);
446 UCI_INTERNAL(uci_import
, ctx
, f
, name
, &p
, true);
452 /* freed together with the uci_package */
455 /* check for updated history, flush */
456 if (!uci_load_history(ctx
, p
, true))
460 if (!uci_load_history(ctx
, NULL
, true))
466 ftruncate(fileno(f
), 0);
468 uci_export(ctx
, f
, p
, false);
469 UCI_TRAP_RESTORE(ctx
);
478 UCI_THROW(ctx
, ctx
->errno
);
485 * This function returns the filename by returning the string
486 * after the last '/' character. By checking for a non-'\0'
487 * character afterwards, directories are ignored (glob marks
488 * those with a trailing '/'
490 static inline char *get_filename(char *path
)
494 p
= strrchr(path
, '/');
501 int uci_list_configs(struct uci_context
*ctx
, char ***list
)
511 dir
= uci_malloc(ctx
, strlen(ctx
->confdir
) + 1 + sizeof("/*"));
512 sprintf(dir
, "%s/*", ctx
->confdir
);
513 if (glob(dir
, GLOB_MARK
, NULL
, &globbuf
) != 0)
514 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
516 size
= sizeof(char *) * (globbuf
.gl_pathc
+ 1);
517 for(i
= 0; i
< globbuf
.gl_pathc
; i
++) {
520 p
= get_filename(globbuf
.gl_pathv
[i
]);
524 size
+= strlen(p
) + 1;
527 configs
= uci_malloc(ctx
, size
);
528 buf
= (char *) &configs
[globbuf
.gl_pathc
+ 1];
529 for(i
= 0; i
< globbuf
.gl_pathc
; i
++) {
532 p
= get_filename(globbuf
.gl_pathv
[i
]);
538 buf
+= strlen(buf
) + 1;