turn ucimap-example.c into a test case
[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 struct uci_element *e;
106 struct uci_ptr ptr;
107 char *name = NULL;
108 char *type = NULL;
109
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");
114
115 uci_switch_config(ctx);
116 }
117
118 /* command string null-terminated by strtok */
119 *str += strlen(*str) + 1;
120
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);
126
127 if (!name) {
128 ctx->internal = !pctx->merge;
129 UCI_NESTED(uci_add_section, ctx, pctx->package, type, &pctx->section);
130 } else {
131 UCI_NESTED(uci_fill_ptr, ctx, &ptr, &pctx->package->e, false);
132 e = uci_lookup_list(&pctx->package->sections, name);
133 if (e)
134 ptr.s = uci_to_section(e);
135 ptr.section = name;
136 ptr.value = type;
137
138 ctx->internal = !pctx->merge;
139 UCI_NESTED(uci_set, ctx, &ptr);
140 pctx->section = uci_to_section(ptr.last);
141 }
142 }
143
144 /*
145 * parse the 'option' uci command (open a value)
146 */
147 static void uci_parse_option(struct uci_context *ctx, char **str, bool list)
148 {
149 struct uci_parse_context *pctx = ctx->pctx;
150 struct uci_element *e;
151 struct uci_ptr ptr;
152 char *name = NULL;
153 char *value = NULL;
154
155 if (!pctx->section)
156 uci_parse_error(ctx, *str, "option/list command found before the first section");
157
158 /* command string null-terminated by strtok */
159 *str += strlen(*str) + 1;
160
161 name = next_arg(ctx, str, true, true);
162 value = next_arg(ctx, str, false, false);
163 assert_eol(ctx, str);
164
165 UCI_NESTED(uci_fill_ptr, ctx, &ptr, &pctx->section->e, false);
166 e = uci_lookup_list(&pctx->section->options, name);
167 if (e)
168 ptr.o = uci_to_option(e);
169 ptr.option = name;
170 ptr.value = value;
171
172 ctx->internal = !pctx->merge;
173 if (list)
174 UCI_NESTED(uci_add_list, ctx, &ptr);
175 else
176 UCI_NESTED(uci_set, ctx, &ptr);
177 }
178
179 /*
180 * parse a complete input line, split up combined commands by ';'
181 */
182 static void uci_parse_line(struct uci_context *ctx, bool single)
183 {
184 struct uci_parse_context *pctx = ctx->pctx;
185 char *word, *brk;
186
187 word = pctx->buf;
188 do {
189 brk = NULL;
190 word = strtok_r(word, " \t", &brk);
191 if (!word)
192 return;
193
194 switch(word[0]) {
195 case 0:
196 case '#':
197 return;
198 case 'p':
199 if ((word[1] == 0) || !strcmp(word + 1, "ackage"))
200 uci_parse_package(ctx, &word, single);
201 else
202 goto invalid;
203 break;
204 case 'c':
205 if ((word[1] == 0) || !strcmp(word + 1, "onfig"))
206 uci_parse_config(ctx, &word);
207 else
208 goto invalid;
209 break;
210 case 'o':
211 if ((word[1] == 0) || !strcmp(word + 1, "ption"))
212 uci_parse_option(ctx, &word, false);
213 else
214 goto invalid;
215 break;
216 case 'l':
217 if ((word[1] == 0) || !strcmp(word + 1, "ist"))
218 uci_parse_option(ctx, &word, true);
219 else
220 goto invalid;
221 break;
222 default:
223 goto invalid;
224 }
225 continue;
226 invalid:
227 uci_parse_error(ctx, word, "invalid command");
228 } while (1);
229 }
230
231 /* max number of characters that escaping adds to the string */
232 #define UCI_QUOTE_ESCAPE "'\\''"
233
234 /*
235 * escape an uci string for export
236 */
237 static char *uci_escape(struct uci_context *ctx, const char *str)
238 {
239 const char *end;
240 int ofs = 0;
241
242 if (!ctx->buf) {
243 ctx->bufsz = LINEBUF;
244 ctx->buf = malloc(LINEBUF);
245 }
246
247 while (1) {
248 int len;
249
250 end = strchr(str, '\'');
251 if (!end)
252 end = str + strlen(str);
253 len = end - str;
254
255 /* make sure that we have enough room in the buffer */
256 while (ofs + len + sizeof(UCI_QUOTE_ESCAPE) + 1 > ctx->bufsz) {
257 ctx->bufsz *= 2;
258 ctx->buf = uci_realloc(ctx, ctx->buf, ctx->bufsz);
259 }
260
261 /* copy the string until the character before the quote */
262 memcpy(&ctx->buf[ofs], str, len);
263 ofs += len;
264
265 /* end of string? return the buffer */
266 if (*end == 0)
267 break;
268
269 memcpy(&ctx->buf[ofs], UCI_QUOTE_ESCAPE, sizeof(UCI_QUOTE_ESCAPE));
270 ofs += strlen(&ctx->buf[ofs]);
271 str = end + 1;
272 }
273
274 ctx->buf[ofs] = 0;
275 return ctx->buf;
276 }
277
278 /*
279 * export a single config package to a file stream
280 */
281 static void uci_export_package(struct uci_package *p, FILE *stream, bool header)
282 {
283 struct uci_context *ctx = p->ctx;
284 struct uci_element *s, *o, *i;
285
286 if (header)
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);
296 switch(opt->type) {
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));
300 break;
301 case UCI_TYPE_LIST:
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));
305 }
306 break;
307 default:
308 fprintf(stream, "\t# unknown type for option '%s'\n", uci_escape(ctx, opt->e.name));
309 break;
310 }
311 }
312 }
313 fprintf(stream, "\n");
314 }
315
316 int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package, bool header)
317 {
318 struct uci_element *e;
319
320 UCI_HANDLE_ERR(ctx);
321 UCI_ASSERT(ctx, stream != NULL);
322
323 if (package)
324 uci_export_package(package, stream, header);
325 else {
326 uci_foreach_element(&ctx->root, e) {
327 uci_export_package(uci_to_package(e), stream, header);
328 }
329 }
330
331 return 0;
332 }
333
334 int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package, bool single)
335 {
336 struct uci_parse_context *pctx;
337 UCI_HANDLE_ERR(ctx);
338
339 /* make sure no memory from previous parse attempts is leaked */
340 uci_cleanup(ctx);
341
342 uci_alloc_parse_context(ctx);
343 pctx = ctx->pctx;
344 pctx->file = stream;
345 if (*package && single) {
346 pctx->package = *package;
347 pctx->merge = true;
348 }
349
350 /*
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
354 */
355 if (name) {
356 UCI_ASSERT(ctx, uci_validate_package(name));
357 pctx->name = name;
358 }
359
360 while (!feof(pctx->file)) {
361 uci_getln(ctx, 0);
362 UCI_TRAP_SAVE(ctx, error);
363 if (pctx->buf[0])
364 uci_parse_line(ctx, single);
365 UCI_TRAP_RESTORE(ctx);
366 continue;
367 error:
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);
373 }
374
375 uci_fixup_section(ctx, ctx->pctx->section);
376 if (!pctx->package && name)
377 uci_switch_config(ctx);
378 if (package)
379 *package = pctx->package;
380 if (pctx->merge)
381 pctx->package = NULL;
382
383 pctx->name = NULL;
384 uci_switch_config(ctx);
385
386 /* no error happened, we can get rid of the parser context now */
387 uci_cleanup(ctx);
388
389 return 0;
390 }
391
392
393 static char *uci_config_path(struct uci_context *ctx, const char *name)
394 {
395 char *filename;
396
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);
400
401 return filename;
402 }
403
404 void uci_file_commit(struct uci_context *ctx, struct uci_package **package, bool overwrite)
405 {
406 struct uci_package *p = *package;
407 FILE *f = NULL;
408 char *name = NULL;
409 char *path = NULL;
410
411 if (!p->path) {
412 if (overwrite)
413 p->path = uci_config_path(ctx, p->e.name);
414 else
415 UCI_THROW(ctx, UCI_ERR_INVAL);
416 }
417
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);
420
421 /* flush unsaved changes and reload from history file */
422 UCI_TRAP_SAVE(ctx, done);
423 if (p->has_history) {
424 if (!overwrite) {
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);
430
431 /*
432 * other processes might have modified the config
433 * as well. dump and reload
434 */
435 uci_free_package(&p);
436 uci_cleanup(ctx);
437 UCI_INTERNAL(uci_import, ctx, f, name, &p, true);
438
439 p->path = path;
440 p->has_history = true;
441 *package = p;
442
443 /* freed together with the uci_package */
444 path = NULL;
445
446 /* check for updated history, flush */
447 if (!uci_load_history(ctx, p, true))
448 goto done;
449 } else {
450 /* flush history */
451 if (!uci_load_history(ctx, NULL, true))
452 goto done;
453 }
454 }
455
456 rewind(f);
457 if (ftruncate(fileno(f), 0) < 0)
458 UCI_THROW(ctx, UCI_ERR_IO);
459
460 uci_export(ctx, f, p, false);
461 UCI_TRAP_RESTORE(ctx);
462
463 done:
464 if (name)
465 free(name);
466 if (path)
467 free(path);
468 uci_close_stream(f);
469 if (ctx->err)
470 UCI_THROW(ctx, ctx->err);
471 }
472
473
474 /*
475 * This function returns the filename by returning the string
476 * after the last '/' character. By checking for a non-'\0'
477 * character afterwards, directories are ignored (glob marks
478 * those with a trailing '/'
479 */
480 static inline char *get_filename(char *path)
481 {
482 char *p;
483
484 p = strrchr(path, '/');
485 p++;
486 if (!*p)
487 return NULL;
488 return p;
489 }
490
491 static char **uci_list_config_files(struct uci_context *ctx)
492 {
493 char **configs;
494 glob_t globbuf;
495 int size, i;
496 char *buf;
497 char *dir;
498
499 dir = uci_malloc(ctx, strlen(ctx->confdir) + 1 + sizeof("/*"));
500 sprintf(dir, "%s/*", ctx->confdir);
501 if (glob(dir, GLOB_MARK, NULL, &globbuf) != 0)
502 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
503
504 size = sizeof(char *) * (globbuf.gl_pathc + 1);
505 for(i = 0; i < globbuf.gl_pathc; i++) {
506 char *p;
507
508 p = get_filename(globbuf.gl_pathv[i]);
509 if (!p)
510 continue;
511
512 size += strlen(p) + 1;
513 }
514
515 configs = uci_malloc(ctx, size);
516 buf = (char *) &configs[globbuf.gl_pathc + 1];
517 for(i = 0; i < globbuf.gl_pathc; i++) {
518 char *p;
519
520 p = get_filename(globbuf.gl_pathv[i]);
521 if (!p)
522 continue;
523
524 if (!uci_validate_package(p))
525 continue;
526
527 configs[i] = buf;
528 strcpy(buf, p);
529 buf += strlen(buf) + 1;
530 }
531 free(dir);
532 globfree(&globbuf);
533 return configs;
534 }
535
536 static struct uci_package *uci_file_load(struct uci_context *ctx, const char *name)
537 {
538 struct uci_package *package = NULL;
539 char *filename;
540 bool confdir;
541 FILE *file = NULL;
542
543 switch (name[0]) {
544 case '.':
545 /* relative path outside of /etc/config */
546 if (name[1] != '/')
547 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
548 /* fall through */
549 case '/':
550 /* absolute path outside of /etc/config */
551 filename = uci_strdup(ctx, name);
552 name = strrchr(name, '/') + 1;
553 confdir = false;
554 break;
555 default:
556 /* config in /etc/config */
557 filename = uci_config_path(ctx, name);
558 confdir = true;
559 break;
560 }
561
562 file = uci_open_stream(ctx, filename, SEEK_SET, false, false);
563 ctx->err = 0;
564 UCI_TRAP_SAVE(ctx, done);
565 UCI_INTERNAL(uci_import, ctx, file, name, &package, true);
566 UCI_TRAP_RESTORE(ctx);
567
568 if (package) {
569 package->path = filename;
570 package->has_history = confdir;
571 uci_load_history(ctx, package, false);
572 }
573
574 done:
575 uci_close_stream(file);
576 if (ctx->err)
577 UCI_THROW(ctx, ctx->err);
578 return package;
579 }
580
581 static UCI_BACKEND(uci_file_backend, "file",
582 .load = uci_file_load,
583 .commit = uci_file_commit,
584 .list_configs = uci_list_config_files,
585 );