add list support
[project/uci.git] / file.c
1 /*
2 * libuci - Library for the Unified Configuration Interface
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
4 *
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
8 *
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.
13 */
14
15 /*
16 * This file contains the code for parsing uci config files
17 */
18
19 #define _GNU_SOURCE
20 #include <sys/types.h>
21 #include <sys/file.h>
22 #include <stdbool.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <ctype.h>
27 #include <glob.h>
28
29 static struct uci_backend uci_file_backend;
30
31 /*
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
34 */
35 static void assert_eol(struct uci_context *ctx, char **str)
36 {
37 char *tmp;
38
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");
43 }
44
45 /*
46 * switch to a different config, either triggered by uci_load, or by a
47 * 'package <...>' statement in the import file
48 */
49 static void uci_switch_config(struct uci_context *ctx)
50 {
51 struct uci_parse_context *pctx;
52 struct uci_element *e;
53 const char *name;
54
55 pctx = ctx->pctx;
56 name = pctx->name;
57
58 /* add the last config to main config file list */
59 if (pctx->package) {
60 pctx->package->backend = ctx->backend;
61 uci_list_add(&ctx->root, &pctx->package->e.list);
62
63 pctx->package = NULL;
64 pctx->section = NULL;
65 }
66
67 if (!name)
68 return;
69
70 /*
71 * if an older config under the same name exists, unload it
72 * ignore errors here, e.g. if the config was not found
73 */
74 e = uci_lookup_list(&ctx->root, name);
75 if (e)
76 UCI_THROW(ctx, UCI_ERR_DUPLICATE);
77 pctx->package = uci_alloc_package(ctx, name);
78 }
79
80 /*
81 * parse the 'package' uci command (next config package)
82 */
83 static void uci_parse_package(struct uci_context *ctx, char **str, bool single)
84 {
85 char *name = NULL;
86
87 /* command string null-terminated by strtok */
88 *str += strlen(*str) + 1;
89
90 name = next_arg(ctx, str, true, true);
91 assert_eol(ctx, str);
92 if (single)
93 return;
94
95 ctx->pctx->name = name;
96 uci_switch_config(ctx);
97 }
98
99 /*
100 * parse the 'config' uci command (open a section)
101 */
102 static void uci_parse_config(struct uci_context *ctx, char **str)
103 {
104 struct uci_parse_context *pctx = ctx->pctx;
105 char *name = NULL;
106 char *type = NULL;
107
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");
112
113 uci_switch_config(ctx);
114 }
115
116 /* command string null-terminated by strtok */
117 *str += strlen(*str) + 1;
118
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);
124
125 if (pctx->merge) {
126 UCI_TRAP_SAVE(ctx, error);
127 if (uci_set(ctx, pctx->package, name, NULL, type, NULL) != UCI_OK)
128 goto error;
129 UCI_TRAP_RESTORE(ctx);
130 return;
131 error:
132 UCI_THROW(ctx, ctx->err);
133 } else
134 pctx->section = uci_alloc_section(pctx->package, type, name);
135 }
136
137 /*
138 * parse the 'option' uci command (open a value)
139 */
140 static void uci_parse_option(struct uci_context *ctx, char **str)
141 {
142 struct uci_parse_context *pctx = ctx->pctx;
143 char *name = NULL;
144 char *value = NULL;
145
146 if (!pctx->section)
147 uci_parse_error(ctx, *str, "option command found before the first section");
148
149 /* command string null-terminated by strtok */
150 *str += strlen(*str) + 1;
151
152 name = next_arg(ctx, str, true, true);
153 value = next_arg(ctx, str, false, false);
154 assert_eol(ctx, str);
155
156 if (pctx->merge) {
157 UCI_TRAP_SAVE(ctx, error);
158 uci_set(ctx, pctx->package, pctx->section->e.name, name, value, NULL);
159 UCI_TRAP_RESTORE(ctx);
160 return;
161 error:
162 UCI_THROW(ctx, ctx->err);
163 } else
164 uci_alloc_option(pctx->section, name, value);
165 }
166
167 static void uci_parse_list(struct uci_context *ctx, char **str)
168 {
169 struct uci_parse_context *pctx = ctx->pctx;
170 char *name = NULL;
171 char *value = NULL;
172
173 if (!pctx->section)
174 uci_parse_error(ctx, *str, "list command found before the first section");
175
176 /* command string null-terminated by strtok */
177 *str += strlen(*str) + 1;
178
179 name = next_arg(ctx, str, true, true);
180 value = next_arg(ctx, str, false, false);
181 assert_eol(ctx, str);
182
183 if (pctx->merge) {
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);
187 return;
188 error:
189 UCI_THROW(ctx, ctx->err);
190 } else
191 UCI_INTERNAL(uci_add_list, ctx, pctx->package, pctx->section->e.name, name, value, NULL);
192 }
193
194
195 /*
196 * parse a complete input line, split up combined commands by ';'
197 */
198 static void uci_parse_line(struct uci_context *ctx, bool single)
199 {
200 struct uci_parse_context *pctx = ctx->pctx;
201 char *word, *brk;
202
203 word = pctx->buf;
204 do {
205 brk = NULL;
206 word = strtok_r(word, " \t", &brk);
207 if (!word)
208 return;
209
210 switch(word[0]) {
211 case 0:
212 case '#':
213 return;
214 case 'p':
215 if ((word[1] == 0) || !strcmp(word + 1, "ackage"))
216 uci_parse_package(ctx, &word, single);
217 else
218 goto invalid;
219 break;
220 case 'c':
221 if ((word[1] == 0) || !strcmp(word + 1, "onfig"))
222 uci_parse_config(ctx, &word);
223 else
224 goto invalid;
225 break;
226 case 'o':
227 if ((word[1] == 0) || !strcmp(word + 1, "ption"))
228 uci_parse_option(ctx, &word);
229 else
230 goto invalid;
231 break;
232 case 'l':
233 if ((word[1] == 0) || !strcmp(word + 1, "ist"))
234 uci_parse_list(ctx, &word);
235 else
236 goto invalid;
237 break;
238 default:
239 goto invalid;
240 }
241 continue;
242 invalid:
243 uci_parse_error(ctx, word, "invalid command");
244 } while (1);
245 }
246
247 /* max number of characters that escaping adds to the string */
248 #define UCI_QUOTE_ESCAPE "'\\''"
249
250 /*
251 * escape an uci string for export
252 */
253 static char *uci_escape(struct uci_context *ctx, const char *str)
254 {
255 const char *end;
256 int ofs = 0;
257
258 if (!ctx->buf) {
259 ctx->bufsz = LINEBUF;
260 ctx->buf = malloc(LINEBUF);
261 }
262
263 while (1) {
264 int len;
265
266 end = strchr(str, '\'');
267 if (!end)
268 end = str + strlen(str);
269 len = end - str;
270
271 /* make sure that we have enough room in the buffer */
272 while (ofs + len + sizeof(UCI_QUOTE_ESCAPE) + 1 > ctx->bufsz) {
273 ctx->bufsz *= 2;
274 ctx->buf = uci_realloc(ctx, ctx->buf, ctx->bufsz);
275 }
276
277 /* copy the string until the character before the quote */
278 memcpy(&ctx->buf[ofs], str, len);
279 ofs += len;
280
281 /* end of string? return the buffer */
282 if (*end == 0)
283 break;
284
285 memcpy(&ctx->buf[ofs], UCI_QUOTE_ESCAPE, sizeof(UCI_QUOTE_ESCAPE));
286 ofs += strlen(&ctx->buf[ofs]);
287 str = end + 1;
288 }
289
290 ctx->buf[ofs] = 0;
291 return ctx->buf;
292 }
293
294 /*
295 * export a single config package to a file stream
296 */
297 static void uci_export_package(struct uci_package *p, FILE *stream, bool header)
298 {
299 struct uci_context *ctx = p->ctx;
300 struct uci_element *s, *o, *i;
301
302 if (header)
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);
312 switch(opt->type) {
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));
316 break;
317 case UCI_TYPE_LIST:
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));
321 }
322 break;
323 default:
324 fprintf(stream, "\t# unknown type for option '%s'\n", uci_escape(ctx, opt->e.name));
325 break;
326 }
327 }
328 }
329 fprintf(stream, "\n");
330 }
331
332 int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package, bool header)
333 {
334 struct uci_element *e;
335
336 UCI_HANDLE_ERR(ctx);
337 UCI_ASSERT(ctx, stream != NULL);
338
339 if (package)
340 uci_export_package(package, stream, header);
341 else {
342 uci_foreach_element(&ctx->root, e) {
343 uci_export_package(uci_to_package(e), stream, header);
344 }
345 }
346
347 return 0;
348 }
349
350 int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package, bool single)
351 {
352 struct uci_parse_context *pctx;
353 UCI_HANDLE_ERR(ctx);
354
355 /* make sure no memory from previous parse attempts is leaked */
356 uci_cleanup(ctx);
357
358 uci_alloc_parse_context(ctx);
359 pctx = ctx->pctx;
360 pctx->file = stream;
361 if (*package && single) {
362 pctx->package = *package;
363 pctx->merge = true;
364 }
365
366 /*
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
370 */
371 if (name) {
372 UCI_ASSERT(ctx, uci_validate_str(name, false));
373 pctx->name = name;
374 }
375
376 while (!feof(pctx->file)) {
377 uci_getln(ctx, 0);
378 UCI_TRAP_SAVE(ctx, error);
379 if (pctx->buf[0])
380 uci_parse_line(ctx, single);
381 UCI_TRAP_RESTORE(ctx);
382 continue;
383 error:
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);
389 }
390
391 uci_fixup_section(ctx, ctx->pctx->section);
392 if (!pctx->package && name)
393 uci_switch_config(ctx);
394 if (package)
395 *package = pctx->package;
396 if (pctx->merge)
397 pctx->package = NULL;
398
399 pctx->name = NULL;
400 uci_switch_config(ctx);
401
402 /* no error happened, we can get rid of the parser context now */
403 uci_cleanup(ctx);
404
405 return 0;
406 }
407
408
409 static char *uci_config_path(struct uci_context *ctx, const char *name)
410 {
411 char *filename;
412
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);
416
417 return filename;
418 }
419
420 void uci_file_commit(struct uci_context *ctx, struct uci_package **package, bool overwrite)
421 {
422 struct uci_package *p = *package;
423 FILE *f = NULL;
424 char *name = NULL;
425 char *path = NULL;
426
427 if (!p->path) {
428 if (overwrite)
429 p->path = uci_config_path(ctx, p->e.name);
430 else
431 UCI_THROW(ctx, UCI_ERR_INVAL);
432 }
433
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);
436
437 /* flush unsaved changes and reload from history file */
438 UCI_TRAP_SAVE(ctx, done);
439 if (p->has_history) {
440 if (!overwrite) {
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);
446
447 /*
448 * other processes might have modified the config
449 * as well. dump and reload
450 */
451 uci_free_package(&p);
452 uci_cleanup(ctx);
453 UCI_INTERNAL(uci_import, ctx, f, name, &p, true);
454
455 p->path = path;
456 p->has_history = true;
457 *package = p;
458
459 /* freed together with the uci_package */
460 path = NULL;
461
462 /* check for updated history, flush */
463 if (!uci_load_history(ctx, p, true))
464 goto done;
465 } else {
466 /* flush history */
467 if (!uci_load_history(ctx, NULL, true))
468 goto done;
469 }
470 }
471
472 rewind(f);
473 ftruncate(fileno(f), 0);
474
475 uci_export(ctx, f, p, false);
476 UCI_TRAP_RESTORE(ctx);
477
478 done:
479 if (name)
480 free(name);
481 if (path)
482 free(path);
483 uci_close_stream(f);
484 if (ctx->err)
485 UCI_THROW(ctx, ctx->err);
486 }
487
488
489 /*
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 '/'
494 */
495 static inline char *get_filename(char *path)
496 {
497 char *p;
498
499 p = strrchr(path, '/');
500 p++;
501 if (!*p)
502 return NULL;
503 return p;
504 }
505
506 static char **uci_list_config_files(struct uci_context *ctx)
507 {
508 char **configs;
509 glob_t globbuf;
510 int size, i;
511 char *buf;
512 char *dir;
513
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);
518
519 size = sizeof(char *) * (globbuf.gl_pathc + 1);
520 for(i = 0; i < globbuf.gl_pathc; i++) {
521 char *p;
522
523 p = get_filename(globbuf.gl_pathv[i]);
524 if (!p)
525 continue;
526
527 size += strlen(p) + 1;
528 }
529
530 configs = uci_malloc(ctx, size);
531 buf = (char *) &configs[globbuf.gl_pathc + 1];
532 for(i = 0; i < globbuf.gl_pathc; i++) {
533 char *p;
534
535 p = get_filename(globbuf.gl_pathv[i]);
536 if (!p)
537 continue;
538
539 if (!uci_validate_name(p))
540 continue;
541
542 configs[i] = buf;
543 strcpy(buf, p);
544 buf += strlen(buf) + 1;
545 }
546 free(dir);
547 return configs;
548 }
549
550 static struct uci_package *uci_file_load(struct uci_context *ctx, const char *name)
551 {
552 struct uci_package *package = NULL;
553 char *filename;
554 bool confdir;
555 FILE *file = NULL;
556
557 switch (name[0]) {
558 case '.':
559 /* relative path outside of /etc/config */
560 if (name[1] != '/')
561 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
562 /* fall through */
563 case '/':
564 /* absolute path outside of /etc/config */
565 filename = uci_strdup(ctx, name);
566 name = strrchr(name, '/') + 1;
567 confdir = false;
568 break;
569 default:
570 /* config in /etc/config */
571 filename = uci_config_path(ctx, name);
572 confdir = true;
573 break;
574 }
575
576 file = uci_open_stream(ctx, filename, SEEK_SET, false, false);
577 ctx->err = 0;
578 UCI_TRAP_SAVE(ctx, done);
579 UCI_INTERNAL(uci_import, ctx, file, name, &package, true);
580 UCI_TRAP_RESTORE(ctx);
581
582 if (package) {
583 package->path = filename;
584 package->has_history = confdir;
585 uci_load_history(ctx, package, false);
586 }
587
588 done:
589 uci_close_stream(file);
590 if (ctx->err)
591 UCI_THROW(ctx, ctx->err);
592 return package;
593 }
594
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,
599 );