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
;
174 uci_parse_error(ctx
, *str
, "list command found before the first section");
176 /* command string null-terminated by strtok */
177 *str
+= strlen(*str
) + 1;
179 name
= next_arg(ctx
, str
, true, true);
180 value
= next_arg(ctx
, str
, false, false);
181 assert_eol(ctx
, str
);
184 UCI_TRAP_SAVE(ctx
, error
);
185 uci_add_list(ctx
, pctx
->package
, pctx
->section
->e
.name
, name
, value
, NULL
);
186 UCI_TRAP_RESTORE(ctx
);
189 UCI_THROW(ctx
, ctx
->err
);
191 struct uci_option
*o
;
192 struct uci_element
*e
;
194 e
= uci_lookup_list(&pctx
->section
->options
, name
);
195 o
= uci_to_option(e
);
197 o
= uci_alloc_list(pctx
->section
, name
);
199 if (o
->type
!= UCI_TYPE_LIST
)
200 uci_parse_error(ctx
, *str
, "conflicting list/option keywords");
202 UCI_INTERNAL(uci_add_element_list
, ctx
, o
, value
);
208 * parse a complete input line, split up combined commands by ';'
210 static void uci_parse_line(struct uci_context
*ctx
, bool single
)
212 struct uci_parse_context
*pctx
= ctx
->pctx
;
218 word
= strtok_r(word
, " \t", &brk
);
227 if ((word
[1] == 0) || !strcmp(word
+ 1, "ackage"))
228 uci_parse_package(ctx
, &word
, single
);
233 if ((word
[1] == 0) || !strcmp(word
+ 1, "onfig"))
234 uci_parse_config(ctx
, &word
);
239 if ((word
[1] == 0) || !strcmp(word
+ 1, "ption"))
240 uci_parse_option(ctx
, &word
);
245 if ((word
[1] == 0) || !strcmp(word
+ 1, "ist"))
246 uci_parse_list(ctx
, &word
);
255 uci_parse_error(ctx
, word
, "invalid command");
259 /* max number of characters that escaping adds to the string */
260 #define UCI_QUOTE_ESCAPE "'\\''"
263 * escape an uci string for export
265 static char *uci_escape(struct uci_context
*ctx
, const char *str
)
271 ctx
->bufsz
= LINEBUF
;
272 ctx
->buf
= malloc(LINEBUF
);
278 end
= strchr(str
, '\'');
280 end
= str
+ strlen(str
);
283 /* make sure that we have enough room in the buffer */
284 while (ofs
+ len
+ sizeof(UCI_QUOTE_ESCAPE
) + 1 > ctx
->bufsz
) {
286 ctx
->buf
= uci_realloc(ctx
, ctx
->buf
, ctx
->bufsz
);
289 /* copy the string until the character before the quote */
290 memcpy(&ctx
->buf
[ofs
], str
, len
);
293 /* end of string? return the buffer */
297 memcpy(&ctx
->buf
[ofs
], UCI_QUOTE_ESCAPE
, sizeof(UCI_QUOTE_ESCAPE
));
298 ofs
+= strlen(&ctx
->buf
[ofs
]);
307 * export a single config package to a file stream
309 static void uci_export_package(struct uci_package
*p
, FILE *stream
, bool header
)
311 struct uci_context
*ctx
= p
->ctx
;
312 struct uci_element
*s
, *o
, *i
;
315 fprintf(stream
, "package '%s'\n", uci_escape(ctx
, p
->e
.name
));
316 uci_foreach_element(&p
->sections
, s
) {
317 struct uci_section
*sec
= uci_to_section(s
);
318 fprintf(stream
, "\nconfig '%s'", uci_escape(ctx
, sec
->type
));
319 if (!sec
->anonymous
|| (ctx
->flags
& UCI_FLAG_EXPORT_NAME
))
320 fprintf(stream
, " '%s'", uci_escape(ctx
, sec
->e
.name
));
321 fprintf(stream
, "\n");
322 uci_foreach_element(&sec
->options
, o
) {
323 struct uci_option
*opt
= uci_to_option(o
);
325 case UCI_TYPE_STRING
:
326 fprintf(stream
, "\toption '%s'", uci_escape(ctx
, opt
->e
.name
));
327 fprintf(stream
, " '%s'\n", uci_escape(ctx
, opt
->v
.string
));
330 uci_foreach_element(&opt
->v
.list
, i
) {
331 fprintf(stream
, "\tlist '%s'", uci_escape(ctx
, opt
->e
.name
));
332 fprintf(stream
, " '%s'\n", uci_escape(ctx
, i
->name
));
336 fprintf(stream
, "\t# unknown type for option '%s'\n", uci_escape(ctx
, opt
->e
.name
));
341 fprintf(stream
, "\n");
344 int uci_export(struct uci_context
*ctx
, FILE *stream
, struct uci_package
*package
, bool header
)
346 struct uci_element
*e
;
349 UCI_ASSERT(ctx
, stream
!= NULL
);
352 uci_export_package(package
, stream
, header
);
354 uci_foreach_element(&ctx
->root
, e
) {
355 uci_export_package(uci_to_package(e
), stream
, header
);
362 int uci_import(struct uci_context
*ctx
, FILE *stream
, const char *name
, struct uci_package
**package
, bool single
)
364 struct uci_parse_context
*pctx
;
367 /* make sure no memory from previous parse attempts is leaked */
370 uci_alloc_parse_context(ctx
);
373 if (*package
&& single
) {
374 pctx
->package
= *package
;
379 * If 'name' was supplied, assume that the supplied stream does not contain
380 * the appropriate 'package <name>' string to specify the config name
381 * NB: the config file can still override the package name
384 UCI_ASSERT(ctx
, uci_validate_str(name
, false));
388 while (!feof(pctx
->file
)) {
390 UCI_TRAP_SAVE(ctx
, error
);
392 uci_parse_line(ctx
, single
);
393 UCI_TRAP_RESTORE(ctx
);
396 if (ctx
->flags
& UCI_FLAG_PERROR
)
397 uci_perror(ctx
, NULL
);
398 if ((ctx
->err
!= UCI_ERR_PARSE
) ||
399 (ctx
->flags
& UCI_FLAG_STRICT
))
400 UCI_THROW(ctx
, ctx
->err
);
403 uci_fixup_section(ctx
, ctx
->pctx
->section
);
404 if (!pctx
->package
&& name
)
405 uci_switch_config(ctx
);
407 *package
= pctx
->package
;
409 pctx
->package
= NULL
;
412 uci_switch_config(ctx
);
414 /* no error happened, we can get rid of the parser context now */
421 static char *uci_config_path(struct uci_context
*ctx
, const char *name
)
425 UCI_ASSERT(ctx
, uci_validate_str(name
, false));
426 filename
= uci_malloc(ctx
, strlen(name
) + strlen(ctx
->confdir
) + 2);
427 sprintf(filename
, "%s/%s", ctx
->confdir
, name
);
432 void uci_file_commit(struct uci_context
*ctx
, struct uci_package
**package
, bool overwrite
)
434 struct uci_package
*p
= *package
;
441 p
->path
= uci_config_path(ctx
, p
->e
.name
);
443 UCI_THROW(ctx
, UCI_ERR_INVAL
);
446 /* open the config file for writing now, so that it is locked */
447 f
= uci_open_stream(ctx
, p
->path
, SEEK_SET
, true, true);
449 /* flush unsaved changes and reload from history file */
450 UCI_TRAP_SAVE(ctx
, done
);
451 if (p
->has_history
) {
453 name
= uci_strdup(ctx
, p
->e
.name
);
454 path
= uci_strdup(ctx
, p
->path
);
455 /* dump our own changes to the history file */
456 if (!uci_list_empty(&p
->history
))
457 UCI_INTERNAL(uci_save
, ctx
, p
);
460 * other processes might have modified the config
461 * as well. dump and reload
463 uci_free_package(&p
);
465 UCI_INTERNAL(uci_import
, ctx
, f
, name
, &p
, true);
468 p
->has_history
= true;
471 /* freed together with the uci_package */
474 /* check for updated history, flush */
475 if (!uci_load_history(ctx
, p
, true))
479 if (!uci_load_history(ctx
, NULL
, true))
485 ftruncate(fileno(f
), 0);
487 uci_export(ctx
, f
, p
, false);
488 UCI_TRAP_RESTORE(ctx
);
497 UCI_THROW(ctx
, ctx
->err
);
502 * This function returns the filename by returning the string
503 * after the last '/' character. By checking for a non-'\0'
504 * character afterwards, directories are ignored (glob marks
505 * those with a trailing '/'
507 static inline char *get_filename(char *path
)
511 p
= strrchr(path
, '/');
518 static char **uci_list_config_files(struct uci_context
*ctx
)
526 dir
= uci_malloc(ctx
, strlen(ctx
->confdir
) + 1 + sizeof("/*"));
527 sprintf(dir
, "%s/*", ctx
->confdir
);
528 if (glob(dir
, GLOB_MARK
, NULL
, &globbuf
) != 0)
529 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
531 size
= sizeof(char *) * (globbuf
.gl_pathc
+ 1);
532 for(i
= 0; i
< globbuf
.gl_pathc
; i
++) {
535 p
= get_filename(globbuf
.gl_pathv
[i
]);
539 size
+= strlen(p
) + 1;
542 configs
= uci_malloc(ctx
, size
);
543 buf
= (char *) &configs
[globbuf
.gl_pathc
+ 1];
544 for(i
= 0; i
< globbuf
.gl_pathc
; i
++) {
547 p
= get_filename(globbuf
.gl_pathv
[i
]);
551 if (!uci_validate_name(p
))
556 buf
+= strlen(buf
) + 1;
562 static struct uci_package
*uci_file_load(struct uci_context
*ctx
, const char *name
)
564 struct uci_package
*package
= NULL
;
571 /* relative path outside of /etc/config */
573 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
576 /* absolute path outside of /etc/config */
577 filename
= uci_strdup(ctx
, name
);
578 name
= strrchr(name
, '/') + 1;
582 /* config in /etc/config */
583 filename
= uci_config_path(ctx
, name
);
588 file
= uci_open_stream(ctx
, filename
, SEEK_SET
, false, false);
590 UCI_TRAP_SAVE(ctx
, done
);
591 UCI_INTERNAL(uci_import
, ctx
, file
, name
, &package
, true);
592 UCI_TRAP_RESTORE(ctx
);
595 package
->path
= filename
;
596 package
->has_history
= confdir
;
597 uci_load_history(ctx
, package
, false);
601 uci_close_stream(file
);
603 UCI_THROW(ctx
, ctx
->err
);
607 static UCI_BACKEND(uci_file_backend
, "file",
608 .load
= uci_file_load
,
609 .commit
= uci_file_commit
,
610 .list_configs
= uci_list_config_files
,