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)
33 struct uci_parse_context *pctx;
47 uci_free_package(&pctx->package);
57 * verify that the end of the line or command is reached.
58 * throw an error if extra arguments are given on the command line
60 static void assert_eol(struct uci_context *ctx, char **str)
64 tmp = next_arg(ctx, str, false, false);
65 if (*tmp && (ctx->flags & UCI_FLAG_STRICT))
66 uci_parse_error(ctx, *str, "too many arguments");
70 * switch to a different config, either triggered by uci_load, or by a
71 * 'package <...>' statement in the import file
73 static void uci_switch_config(struct uci_context *ctx)
75 struct uci_parse_context *pctx;
76 struct uci_element *e;
82 /* add the last config to main config file list */
84 uci_list_add(&ctx->root, &pctx->package->e.list);
94 * if an older config under the same name exists, unload it
95 * ignore errors here, e.g. if the config was not found
97 e = uci_lookup_list(ctx, &ctx->root, name);
99 UCI_THROW(ctx, UCI_ERR_DUPLICATE);
100 pctx->package = uci_alloc_package(ctx, name);
104 * parse the 'package' uci command (next config package)
106 static void uci_parse_package(struct uci_context *ctx, char **str, bool single)
110 /* command string null-terminated by strtok */
111 *str += strlen(*str) + 1;
113 name = next_arg(ctx, str, true, true);
114 assert_eol(ctx, str);
118 ctx->pctx->name = name;
119 uci_switch_config(ctx);
122 /* Based on an efficient hash function published by D. J. Bernstein */
123 static unsigned int djbhash(unsigned int hash, char *str)
125 int len = strlen(str);
132 for(i = 0; i < len; i++) {
133 hash = ((hash << 5) + hash) + str[i];
135 return (hash & 0x7FFFFFFF);
138 /* fix up an unnamed section */
139 static void uci_fixup_section(struct uci_context *ctx, struct uci_section *s)
141 unsigned int hash = ~0;
142 struct uci_element *e;
149 * Generate a name for unnamed sections. This is used as reference
150 * when locating or updating the section from apps/scripts.
151 * To make multiple concurrent versions somewhat safe for updating,
152 * the name is generated from a hash of its type and name/value
153 * pairs of its option, and it is prefixed by a counter value.
154 * If the order of the unnamed sections changes for some reason,
155 * updates to them will be rejected.
157 hash = djbhash(hash, s->type);
158 uci_foreach_element(&s->options, e) {
159 hash = djbhash(hash, e->name);
160 hash = djbhash(hash, uci_to_option(e)->value);
162 sprintf(buf, "cfg%02x%04x", ++s->package->n_section, hash % (1 << 16));
163 s->e.name = uci_strdup(ctx, buf);
167 * parse the 'config' uci command (open a section)
169 static void uci_parse_config(struct uci_context *ctx, char **str)
171 struct uci_parse_context *pctx = ctx->pctx;
175 uci_fixup_section(ctx, ctx->pctx->section);
176 if (!ctx->pctx->package) {
177 if (!ctx->pctx->name)
178 uci_parse_error(ctx, *str, "attempting to import a file without a package name");
180 uci_switch_config(ctx);
183 /* command string null-terminated by strtok */
184 *str += strlen(*str) + 1;
186 type = next_arg(ctx, str, true, false);
187 if (!uci_validate_str(type, false))
188 uci_parse_error(ctx, type, "invalid character in field");
189 name = next_arg(ctx, str, false, true);
190 assert_eol(ctx, str);
193 UCI_TRAP_SAVE(ctx, error);
194 uci_set(ctx, pctx->package, name, NULL, type);
195 UCI_TRAP_RESTORE(ctx);
198 UCI_THROW(ctx, ctx->errno);
200 pctx->section = uci_alloc_section(pctx->package, type, name);
204 * parse the 'option' uci command (open a value)
206 static void uci_parse_option(struct uci_context *ctx, char **str)
208 struct uci_parse_context *pctx = ctx->pctx;
213 uci_parse_error(ctx, *str, "option command found before the first section");
215 /* command string null-terminated by strtok */
216 *str += strlen(*str) + 1;
218 name = next_arg(ctx, str, true, true);
219 value = next_arg(ctx, str, false, false);
220 assert_eol(ctx, str);
223 UCI_TRAP_SAVE(ctx, error);
224 uci_set(ctx, pctx->package, pctx->section->e.name, name, value);
225 UCI_TRAP_RESTORE(ctx);
228 UCI_THROW(ctx, ctx->errno);
230 uci_alloc_option(pctx->section, name, value);
235 * parse a complete input line, split up combined commands by ';'
237 static void uci_parse_line(struct uci_context *ctx, bool single)
239 struct uci_parse_context *pctx = ctx->pctx;
240 char *word, *brk = NULL;
242 for (word = strtok_r(pctx->buf, ";", &brk);
244 word = strtok_r(NULL, ";", &brk)) {
247 word = strtok_r(word, " \t", &pbrk);
256 if ((word[1] == 0) || !strcmp(word + 1, "ackage"))
257 uci_parse_package(ctx, &word, single);
260 if ((word[1] == 0) || !strcmp(word + 1, "onfig"))
261 uci_parse_config(ctx, &word);
264 if ((word[1] == 0) || !strcmp(word + 1, "ption"))
265 uci_parse_option(ctx, &word);
268 uci_parse_error(ctx, word, "unterminated command");
274 /* max number of characters that escaping adds to the string */
275 #define UCI_QUOTE_ESCAPE "'\\''"
278 * escape an uci string for export
280 static char *uci_escape(struct uci_context *ctx, char *str)
286 ctx->bufsz = LINEBUF;
287 ctx->buf = malloc(LINEBUF);
291 p = strchr(str, '\'');
298 if (p + sizeof(UCI_QUOTE_ESCAPE) - str >= ctx->bufsz) {
300 ctx->buf = realloc(ctx->buf, ctx->bufsz);
302 UCI_THROW(ctx, UCI_ERR_MEM);
304 memcpy(&ctx->buf[pos], s, len);
307 strcpy(&ctx->buf[pos], UCI_QUOTE_ESCAPE);
308 pos += sizeof(UCI_QUOTE_ESCAPE);
310 } while ((p = strchr(s, '\'')));
317 * export a single config package to a file stream
319 static void uci_export_package(struct uci_package *p, FILE *stream, bool header)
321 struct uci_context *ctx = p->ctx;
322 struct uci_element *s, *o;
325 fprintf(stream, "package '%s'\n", uci_escape(ctx, p->e.name));
326 uci_foreach_element(&p->sections, s) {
327 struct uci_section *sec = uci_to_section(s);
328 fprintf(stream, "\nconfig '%s'", uci_escape(ctx, sec->type));
329 if (!sec->anonymous || (ctx->flags & UCI_FLAG_EXPORT_NAME))
330 fprintf(stream, " '%s'", uci_escape(ctx, sec->e.name));
331 fprintf(stream, "\n");
332 uci_foreach_element(&sec->options, o) {
333 struct uci_option *opt = uci_to_option(o);
334 fprintf(stream, "\toption '%s'", uci_escape(ctx, opt->e.name));
335 fprintf(stream, " '%s'\n", uci_escape(ctx, opt->value));
338 fprintf(stream, "\n");
341 int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package, bool header)
343 struct uci_element *e;
346 UCI_ASSERT(ctx, stream != NULL);
349 uci_export_package(package, stream, header);
351 uci_foreach_element(&ctx->root, e) {
352 uci_export_package(uci_to_package(e), stream, header);
359 int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package, bool single)
361 struct uci_parse_context *pctx;
364 /* make sure no memory from previous parse attempts is leaked */
365 uci_file_cleanup(ctx);
367 uci_alloc_parse_context(ctx);
370 if (*package && single) {
371 pctx->package = *package;
376 * If 'name' was supplied, assume that the supplied stream does not contain
377 * the appropriate 'package <name>' string to specify the config name
378 * NB: the config file can still override the package name
381 UCI_ASSERT(ctx, uci_validate_name(name));
385 while (!feof(pctx->file)) {
387 UCI_TRAP_SAVE(ctx, error);
389 uci_parse_line(ctx, single);
390 UCI_TRAP_RESTORE(ctx);
393 if (ctx->flags & UCI_FLAG_PERROR)
394 uci_perror(ctx, NULL);
395 if ((ctx->errno != UCI_ERR_PARSE) ||
396 (ctx->flags & UCI_FLAG_STRICT))
397 UCI_THROW(ctx, ctx->errno);
400 uci_fixup_section(ctx, ctx->pctx->section);
402 *package = pctx->package;
404 pctx->package = NULL;
407 uci_switch_config(ctx);
409 /* no error happened, we can get rid of the parser context now */
410 uci_file_cleanup(ctx);
416 static char *uci_config_path(struct uci_context *ctx, const char *name)
420 UCI_ASSERT(ctx, uci_validate_name(name));
421 filename = uci_malloc(ctx, strlen(name) + strlen(ctx->confdir) + 2);
422 sprintf(filename, "%s/%s", ctx->confdir, name);
427 int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package)
437 /* relative path outside of /etc/config */
439 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
442 /* absolute path outside of /etc/config */
443 filename = uci_strdup(ctx, name);
444 name = strrchr(name, '/') + 1;
448 /* config in /etc/config */
449 filename = uci_config_path(ctx, name);
454 file = uci_open_stream(ctx, filename, SEEK_SET, false, false);
456 UCI_TRAP_SAVE(ctx, done);
457 UCI_INTERNAL(uci_import, ctx, file, name, package, true);
458 UCI_TRAP_RESTORE(ctx);
461 (*package)->path = filename;
462 (*package)->confdir = confdir;
463 uci_load_history(ctx, *package, false);
467 uci_close_stream(file);
471 int uci_commit(struct uci_context *ctx, struct uci_package **package, bool overwrite)
473 struct uci_package *p;
479 UCI_ASSERT(ctx, package != NULL);
482 UCI_ASSERT(ctx, p != NULL);
485 p->path = uci_config_path(ctx, p->e.name);
487 UCI_THROW(ctx, UCI_ERR_INVAL);
491 /* open the config file for writing now, so that it is locked */
492 f = uci_open_stream(ctx, p->path, SEEK_SET, true, true);
494 /* flush unsaved changes and reload from history file */
495 UCI_TRAP_SAVE(ctx, done);
498 name = uci_strdup(ctx, p->e.name);
499 path = uci_strdup(ctx, p->path);
500 /* dump our own changes to the history file */
501 if (!uci_list_empty(&p->history))
502 UCI_INTERNAL(uci_save, ctx, p);
505 * other processes might have modified the config
506 * as well. dump and reload
508 uci_free_package(&p);
509 uci_file_cleanup(ctx);
510 UCI_INTERNAL(uci_import, ctx, f, name, &p, true);
516 /* freed together with the uci_package */
519 /* check for updated history, flush */
520 if (!uci_load_history(ctx, p, true))
524 if (!uci_load_history(ctx, NULL, true))
530 ftruncate(fileno(f), 0);
532 uci_export(ctx, f, p, false);
533 UCI_TRAP_RESTORE(ctx);
542 UCI_THROW(ctx, ctx->errno);
549 * This function returns the filename by returning the string
550 * after the last '/' character. By checking for a non-'\0'
551 * character afterwards, directories are ignored (glob marks
552 * those with a trailing '/'
554 static inline char *get_filename(char *path)
558 p = strrchr(path, '/');
565 int uci_list_configs(struct uci_context *ctx, char ***list)
575 dir = uci_malloc(ctx, strlen(ctx->confdir) + 1 + sizeof("/*"));
576 sprintf(dir, "%s/*", ctx->confdir);
577 if (glob(dir, GLOB_MARK, NULL, &globbuf) != 0)
578 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
580 size = sizeof(char *) * (globbuf.gl_pathc + 1);
581 for(i = 0; i < globbuf.gl_pathc; i++) {
584 p = get_filename(globbuf.gl_pathv[i]);
588 size += strlen(p) + 1;
591 configs = uci_malloc(ctx, size);
592 buf = (char *) &configs[globbuf.gl_pathc + 1];
593 for(i = 0; i < globbuf.gl_pathc; i++) {
596 p = get_filename(globbuf.gl_pathv[i]);
602 buf += strlen(buf) + 1;