f169e429478b12a56333aa0494cffe64ea616318
[project/uci.git] / cli.c
1 /*
2 * cli - Command Line Interface 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 General Public License version 2
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 #include <strings.h>
15 #include <string.h>
16 #include <stdlib.h>
17 #include <stdarg.h>
18 #include <errno.h>
19 #include <unistd.h>
20 #include "uci.h"
21
22 #define MAX_ARGS 4 /* max command line arguments for batch mode */
23
24 static const char *delimiter = " ";
25 static const char *appname;
26 static enum {
27 CLI_FLAG_MERGE = (1 << 0),
28 CLI_FLAG_QUIET = (1 << 1),
29 CLI_FLAG_NOCOMMIT = (1 << 2),
30 CLI_FLAG_BATCH = (1 << 3),
31 CLI_FLAG_SHOW_EXT = (1 << 4),
32 } flags;
33
34 static FILE *input;
35
36 static struct uci_context *ctx;
37 enum {
38 /* section cmds */
39 CMD_GET,
40 CMD_SET,
41 CMD_ADD_LIST,
42 CMD_DEL_LIST,
43 CMD_DEL,
44 CMD_RENAME,
45 CMD_REVERT,
46 CMD_REORDER,
47 /* package cmds */
48 CMD_SHOW,
49 CMD_CHANGES,
50 CMD_EXPORT,
51 CMD_COMMIT,
52 /* other cmds */
53 CMD_ADD,
54 CMD_IMPORT,
55 CMD_HELP,
56 };
57
58 struct uci_type_list {
59 unsigned int idx;
60 const char *name;
61 struct uci_type_list *next;
62 };
63
64 static struct uci_type_list *type_list = NULL;
65 static char *typestr = NULL;
66 static const char *cur_section_ref = NULL;
67
68 static int uci_cmd(int argc, char **argv);
69
70 static void
71 uci_reset_typelist(void)
72 {
73 struct uci_type_list *type;
74 while (type_list != NULL) {
75 type = type_list;
76 type_list = type_list->next;
77 free(type);
78 }
79 if (typestr) {
80 free(typestr);
81 typestr = NULL;
82 }
83 cur_section_ref = NULL;
84 }
85
86 static char *
87 uci_lookup_section_ref(struct uci_section *s)
88 {
89 struct uci_type_list *ti = type_list;
90 char *ret;
91 int maxlen;
92
93 if (!(flags & CLI_FLAG_SHOW_EXT))
94 return s->e.name;
95
96 /* look up in section type list */
97 while (ti) {
98 if (strcmp(ti->name, s->type) == 0)
99 break;
100 ti = ti->next;
101 }
102 if (!ti) {
103 ti = calloc(1, sizeof(struct uci_type_list));
104 if (!ti)
105 return NULL;
106 ti->next = type_list;
107 type_list = ti;
108 ti->name = s->type;
109 }
110
111 if (s->anonymous) {
112 maxlen = strlen(s->type) + 1 + 2 + 10;
113 if (!typestr) {
114 typestr = malloc(maxlen);
115 if (!typestr)
116 return NULL;
117 } else {
118 void *p = realloc(typestr, maxlen);
119 if (!p) {
120 free(typestr);
121 return NULL;
122 }
123
124 typestr = p;
125 }
126
127 if (typestr)
128 sprintf(typestr, "@%s[%d]", ti->name, ti->idx);
129
130 ret = typestr;
131 } else {
132 ret = s->e.name;
133 }
134
135 ti->idx++;
136
137 return ret;
138 }
139
140 static void uci_usage(void)
141 {
142 fprintf(stderr,
143 "Usage: %s [<options>] <command> [<arguments>]\n\n"
144 "Commands:\n"
145 "\tbatch\n"
146 "\texport [<config>]\n"
147 "\timport [<config>]\n"
148 "\tchanges [<config>]\n"
149 "\tcommit [<config>]\n"
150 "\tadd <config> <section-type>\n"
151 "\tadd_list <config>.<section>.<option>=<string>\n"
152 "\tdel_list <config>.<section>.<option>=<string>\n"
153 "\tshow [<config>[.<section>[.<option>]]]\n"
154 "\tget <config>.<section>[.<option>]\n"
155 "\tset <config>.<section>[.<option>]=<value>\n"
156 "\tdelete <config>[.<section>[[.<option>][=<id>]]]\n"
157 "\trename <config>.<section>[.<option>]=<name>\n"
158 "\trevert <config>[.<section>[.<option>]]\n"
159 "\treorder <config>.<section>=<position>\n"
160 "\n"
161 "Options:\n"
162 "\t-c <path> set the search path for config files (default: /etc/config)\n"
163 "\t-d <str> set the delimiter for list values in uci show\n"
164 "\t-f <file> use <file> as input instead of stdin\n"
165 "\t-m when importing, merge data into an existing package\n"
166 "\t-n name unnamed sections on export (default)\n"
167 "\t-N don't name unnamed sections\n"
168 "\t-p <path> add a search path for config change files\n"
169 "\t-P <path> add a search path for config change files and use as default\n"
170 "\t-t <path> set save path for config change files\n"
171 "\t-q quiet mode (don't print error messages)\n"
172 "\t-s force strict mode (stop on parser errors, default)\n"
173 "\t-S disable strict mode\n"
174 "\t-X do not use extended syntax on 'show'\n"
175 "\n",
176 appname
177 );
178 }
179
180 static void cli_perror(void)
181 {
182 if (flags & CLI_FLAG_QUIET)
183 return;
184
185 uci_perror(ctx, appname);
186 }
187
188 __attribute__((format(printf, 1, 2)))
189 static void cli_error(const char *fmt, ...)
190 {
191 va_list ap;
192
193 if (flags & CLI_FLAG_QUIET)
194 return;
195
196 va_start(ap, fmt);
197 vfprintf(stderr, fmt, ap);
198 va_end(ap);
199 }
200
201 static void uci_print_value(FILE *f, const char *v)
202 {
203 fprintf(f, "'");
204 while (*v) {
205 if (*v != '\'')
206 fputc(*v, f);
207 else
208 fprintf(f, "'\\''");
209 v++;
210 }
211 fprintf(f, "'");
212 }
213
214 static void uci_show_value(struct uci_option *o, bool quote)
215 {
216 struct uci_element *e;
217 bool sep = false;
218 char *space;
219
220 switch(o->type) {
221 case UCI_TYPE_STRING:
222 if (quote)
223 uci_print_value(stdout, o->v.string);
224 else
225 printf("%s", o->v.string);
226 printf("\n");
227 break;
228 case UCI_TYPE_LIST:
229 uci_foreach_element(&o->v.list, e) {
230 printf("%s", (sep ? delimiter : ""));
231 space = strpbrk(e->name, " \t\r\n");
232 if (!space && !quote)
233 printf("%s", e->name);
234 else
235 uci_print_value(stdout, e->name);
236 sep = true;
237 }
238 printf("\n");
239 break;
240 default:
241 printf("<unknown>\n");
242 break;
243 }
244 }
245
246 static void uci_show_option(struct uci_option *o, bool quote)
247 {
248 printf("%s.%s.%s=",
249 o->section->package->e.name,
250 (cur_section_ref ? cur_section_ref : o->section->e.name),
251 o->e.name);
252 uci_show_value(o, quote);
253 }
254
255 static void uci_show_section(struct uci_section *s)
256 {
257 struct uci_element *e;
258 const char *cname;
259 const char *sname;
260
261 cname = s->package->e.name;
262 sname = (cur_section_ref ? cur_section_ref : s->e.name);
263 printf("%s.%s=%s\n", cname, sname, s->type);
264 uci_foreach_element(&s->options, e) {
265 uci_show_option(uci_to_option(e), true);
266 }
267 }
268
269 static void uci_show_package(struct uci_package *p)
270 {
271 struct uci_element *e;
272
273 uci_reset_typelist();
274 uci_foreach_element( &p->sections, e) {
275 struct uci_section *s = uci_to_section(e);
276 cur_section_ref = uci_lookup_section_ref(s);
277 uci_show_section(s);
278 }
279 uci_reset_typelist();
280 }
281
282 static void uci_show_changes(struct uci_package *p)
283 {
284 struct uci_element *e;
285
286 uci_foreach_element(&p->saved_delta, e) {
287 struct uci_delta *h = uci_to_delta(e);
288 char *prefix = "";
289 char *op = "=";
290
291 switch(h->cmd) {
292 case UCI_CMD_REMOVE:
293 prefix = "-";
294 break;
295 case UCI_CMD_LIST_ADD:
296 op = "+=";
297 break;
298 case UCI_CMD_LIST_DEL:
299 op = "-=";
300 break;
301 default:
302 break;
303 }
304 printf("%s%s.%s", prefix, p->e.name, h->section);
305 if (e->name)
306 printf(".%s", e->name);
307 if (h->cmd != UCI_CMD_REMOVE) {
308 printf("%s", op);
309 uci_print_value(stdout, h->value);
310 }
311 printf("\n");
312 }
313 }
314
315 static int package_cmd(int cmd, char *tuple)
316 {
317 struct uci_ptr ptr;
318 int ret = 1;
319
320 if (uci_lookup_ptr(ctx, &ptr, tuple, true) != UCI_OK) {
321 cli_perror();
322 return 1;
323 }
324
325 switch(cmd) {
326 case CMD_CHANGES:
327 uci_show_changes(ptr.p);
328 break;
329 case CMD_COMMIT:
330 if (flags & CLI_FLAG_NOCOMMIT) {
331 ret = 0;
332 goto out;
333 }
334 if (uci_commit(ctx, &ptr.p, false) != UCI_OK) {
335 cli_perror();
336 goto out;
337 }
338 break;
339 case CMD_EXPORT:
340 if (uci_export(ctx, stdout, ptr.p, true) != UCI_OK) {
341 goto out;
342 }
343 break;
344 case CMD_SHOW:
345 if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
346 ctx->err = UCI_ERR_NOTFOUND;
347 cli_perror();
348 goto out;
349 }
350 if (ptr.o)
351 uci_show_option(ptr.o, true);
352 else if (ptr.s)
353 uci_show_section(ptr.s);
354 else if (ptr.p)
355 uci_show_package(ptr.p);
356 else
357 goto out; /* should not happen */
358 break;
359 }
360
361 ret = 0;
362
363 out:
364 if (ptr.p)
365 uci_unload(ctx, ptr.p);
366 return ret;
367 }
368
369 static int uci_do_import(int argc, char **argv)
370 {
371 struct uci_package *package = NULL;
372 char *name = NULL;
373 int ret = UCI_OK;
374 bool merge = false;
375
376 if (argc > 2)
377 return 255;
378
379 if (argc == 2)
380 name = argv[1];
381 else if (flags & CLI_FLAG_MERGE)
382 /* need a package to merge */
383 return 255;
384
385 if (flags & CLI_FLAG_MERGE) {
386 if (uci_load(ctx, name, &package) != UCI_OK)
387 package = NULL;
388 else
389 merge = true;
390 }
391 ret = uci_import(ctx, input, name, &package, (name != NULL));
392 if (ret == UCI_OK) {
393 if (merge) {
394 ret = uci_save(ctx, package);
395 } else {
396 struct uci_element *e;
397 /* loop through all config sections and overwrite existing data */
398 uci_foreach_element(&ctx->root, e) {
399 struct uci_package *p = uci_to_package(e);
400 ret = uci_commit(ctx, &p, true);
401 }
402 }
403 }
404
405 if (ret != UCI_OK) {
406 cli_perror();
407 return 1;
408 }
409
410 return 0;
411 }
412
413 static int uci_do_package_cmd(int cmd, int argc, char **argv)
414 {
415 char **configs = NULL;
416 char **p;
417 int ret = 1;
418
419 if (argc > 2)
420 return 255;
421
422 if (argc == 2)
423 return package_cmd(cmd, argv[1]);
424
425 if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
426 cli_perror();
427 goto out;
428 }
429
430 for (p = configs; *p; p++) {
431 package_cmd(cmd, *p);
432 }
433
434 ret = 0;
435 out:
436 free(configs);
437 return ret;
438 }
439
440 static int uci_do_add(int argc, char **argv)
441 {
442 struct uci_package *p = NULL;
443 struct uci_section *s = NULL;
444 int ret;
445
446 if (argc != 3)
447 return 255;
448
449 ret = uci_load(ctx, argv[1], &p);
450 if (ret != UCI_OK)
451 goto done;
452
453 ret = uci_add_section(ctx, p, argv[2], &s);
454 if (ret != UCI_OK)
455 goto done;
456
457 ret = uci_save(ctx, p);
458
459 done:
460 if (ret != UCI_OK)
461 cli_perror();
462 else if (s)
463 fprintf(stdout, "%s\n", s->e.name);
464
465 return ret;
466 }
467
468 static int uci_do_section_cmd(int cmd, int argc, char **argv)
469 {
470 struct uci_ptr ptr;
471 int ret = UCI_OK;
472 int dummy;
473
474 if (argc != 2)
475 return 255;
476
477 if (uci_lookup_ptr(ctx, &ptr, argv[1], true) != UCI_OK) {
478 cli_perror();
479 return 1;
480 }
481
482 if (ptr.value && (cmd != CMD_SET) && (cmd != CMD_DEL) &&
483 (cmd != CMD_ADD_LIST) && (cmd != CMD_DEL_LIST) &&
484 (cmd != CMD_RENAME) && (cmd != CMD_REORDER))
485 return 1;
486
487 switch(cmd) {
488 case CMD_GET:
489 if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
490 ctx->err = UCI_ERR_NOTFOUND;
491 cli_perror();
492 return 1;
493 }
494 if (ptr.o)
495 uci_show_value(ptr.o, false);
496 else if (ptr.s)
497 printf("%s\n", ptr.s->type);
498 break;
499 case CMD_RENAME:
500 ret = uci_rename(ctx, &ptr);
501 break;
502 case CMD_REVERT:
503 ret = uci_revert(ctx, &ptr);
504 break;
505 case CMD_SET:
506 ret = uci_set(ctx, &ptr);
507 break;
508 case CMD_ADD_LIST:
509 ret = uci_add_list(ctx, &ptr);
510 break;
511 case CMD_DEL_LIST:
512 ret = uci_del_list(ctx, &ptr);
513 break;
514 case CMD_REORDER:
515 if (!ptr.s || !ptr.value) {
516 ctx->err = UCI_ERR_NOTFOUND;
517 cli_perror();
518 return 1;
519 }
520 ret = uci_reorder_section(ctx, ptr.s, strtoul(ptr.value, NULL, 10));
521 break;
522 case CMD_DEL:
523 if (ptr.value && !sscanf(ptr.value, "%d", &dummy))
524 return 1;
525 ret = uci_delete(ctx, &ptr);
526 break;
527 }
528
529 /* no save necessary for get */
530 if ((cmd == CMD_GET) || (cmd == CMD_REVERT))
531 return 0;
532
533 /* save changes, but don't commit them yet */
534 if (ret == UCI_OK)
535 ret = uci_save(ctx, ptr.p);
536
537 if (ret != UCI_OK) {
538 cli_perror();
539 return 1;
540 }
541
542 return 0;
543 }
544
545 static int uci_batch_cmd(void)
546 {
547 char *argv[MAX_ARGS + 2];
548 char *str = NULL;
549 int ret = 0;
550 int i, j;
551
552 for(i = 0; i <= MAX_ARGS; i++) {
553 if (i == MAX_ARGS) {
554 cli_error("Too many arguments\n");
555 return 1;
556 }
557 argv[i] = NULL;
558 if (uci_parse_argument(ctx, input, &str, &argv[i]) != UCI_OK) {
559 cli_perror();
560 i = 0;
561 break;
562 }
563 if (!argv[i][0])
564 break;
565 argv[i] = strdup(argv[i]);
566 if (!argv[i]) {
567 cli_error("uci: %s", strerror(errno));
568 return 1;
569 }
570 }
571 argv[i] = NULL;
572
573 if (i > 0) {
574 if (!strcasecmp(argv[0], "exit"))
575 return 254;
576 ret = uci_cmd(i, argv);
577 } else
578 return 0;
579
580 for (j = 0; j < i; j++) {
581 free(argv[j]);
582 }
583
584 return ret;
585 }
586
587 static int uci_batch(void)
588 {
589 int ret = 0;
590
591 flags |= CLI_FLAG_BATCH;
592 while (!feof(input)) {
593 struct uci_element *e, *tmp;
594
595 ret = uci_batch_cmd();
596 if (ret == 254)
597 return 0;
598 else if (ret == 255)
599 cli_error("Unknown command\n");
600
601 /* clean up */
602 uci_foreach_element_safe(&ctx->root, tmp, e) {
603 uci_unload(ctx, uci_to_package(e));
604 }
605 }
606 flags &= ~CLI_FLAG_BATCH;
607
608 return 0;
609 }
610
611 static int uci_cmd(int argc, char **argv)
612 {
613 int cmd = 0;
614
615 if (!strcasecmp(argv[0], "batch") && !(flags & CLI_FLAG_BATCH))
616 return uci_batch();
617 else if (!strcasecmp(argv[0], "show"))
618 cmd = CMD_SHOW;
619 else if (!strcasecmp(argv[0], "changes"))
620 cmd = CMD_CHANGES;
621 else if (!strcasecmp(argv[0], "export"))
622 cmd = CMD_EXPORT;
623 else if (!strcasecmp(argv[0], "commit"))
624 cmd = CMD_COMMIT;
625 else if (!strcasecmp(argv[0], "get"))
626 cmd = CMD_GET;
627 else if (!strcasecmp(argv[0], "set"))
628 cmd = CMD_SET;
629 else if (!strcasecmp(argv[0], "ren") ||
630 !strcasecmp(argv[0], "rename"))
631 cmd = CMD_RENAME;
632 else if (!strcasecmp(argv[0], "revert"))
633 cmd = CMD_REVERT;
634 else if (!strcasecmp(argv[0], "reorder"))
635 cmd = CMD_REORDER;
636 else if (!strcasecmp(argv[0], "del") ||
637 !strcasecmp(argv[0], "delete"))
638 cmd = CMD_DEL;
639 else if (!strcasecmp(argv[0], "import"))
640 cmd = CMD_IMPORT;
641 else if (!strcasecmp(argv[0], "help"))
642 cmd = CMD_HELP;
643 else if (!strcasecmp(argv[0], "add"))
644 cmd = CMD_ADD;
645 else if (!strcasecmp(argv[0], "add_list"))
646 cmd = CMD_ADD_LIST;
647 else if (!strcasecmp(argv[0], "del_list"))
648 cmd = CMD_DEL_LIST;
649 else
650 cmd = -1;
651
652 switch(cmd) {
653 case CMD_ADD_LIST:
654 case CMD_DEL_LIST:
655 case CMD_GET:
656 case CMD_SET:
657 case CMD_DEL:
658 case CMD_RENAME:
659 case CMD_REVERT:
660 case CMD_REORDER:
661 return uci_do_section_cmd(cmd, argc, argv);
662 case CMD_SHOW:
663 case CMD_EXPORT:
664 case CMD_COMMIT:
665 case CMD_CHANGES:
666 return uci_do_package_cmd(cmd, argc, argv);
667 case CMD_IMPORT:
668 return uci_do_import(argc, argv);
669 case CMD_ADD:
670 return uci_do_add(argc, argv);
671 case CMD_HELP:
672 uci_usage();
673 return 0;
674 default:
675 return 255;
676 }
677 }
678
679 int main(int argc, char **argv)
680 {
681 int ret;
682 int c;
683
684 flags = CLI_FLAG_SHOW_EXT;
685 appname = argv[0];
686 input = stdin;
687 ctx = uci_alloc_context();
688 if (!ctx) {
689 cli_error("Out of memory\n");
690 return 1;
691 }
692
693 while((c = getopt(argc, argv, "c:d:f:LmnNp:P:qsSt:X")) != -1) {
694 switch(c) {
695 case 'c':
696 uci_set_confdir(ctx, optarg);
697 break;
698 case 'd':
699 delimiter = optarg;
700 break;
701 case 'f':
702 if (input != stdin) {
703 fclose(input);
704 cli_error("Too many input files.\n");
705 return 1;
706 }
707
708 input = fopen(optarg, "r");
709 if (!input) {
710 cli_error("uci: %s", strerror(errno));
711 return 1;
712 }
713 break;
714 case 'm':
715 flags |= CLI_FLAG_MERGE;
716 break;
717 case 's':
718 ctx->flags |= UCI_FLAG_STRICT;
719 break;
720 case 'S':
721 ctx->flags &= ~UCI_FLAG_STRICT;
722 ctx->flags |= UCI_FLAG_PERROR;
723 break;
724 case 'n':
725 ctx->flags |= UCI_FLAG_EXPORT_NAME;
726 break;
727 case 'N':
728 ctx->flags &= ~UCI_FLAG_EXPORT_NAME;
729 break;
730 case 'p':
731 uci_add_delta_path(ctx, optarg);
732 break;
733 case 'P':
734 uci_set_savedir(ctx, optarg);
735 flags |= CLI_FLAG_NOCOMMIT;
736 break;
737 case 'q':
738 flags |= CLI_FLAG_QUIET;
739 break;
740 case 't':
741 uci_set_savedir(ctx, optarg);
742 break;
743 case 'X':
744 flags &= ~CLI_FLAG_SHOW_EXT;
745 break;
746 default:
747 uci_usage();
748 return 0;
749 }
750 }
751 if (optind > 1)
752 argv[optind - 1] = argv[0];
753 argv += optind - 1;
754 argc -= optind - 1;
755
756 if (argc < 2) {
757 uci_usage();
758 return 0;
759 }
760
761 ret = uci_cmd(argc - 1, argv + 1);
762 if (input != stdin)
763 fclose(input);
764
765 if (ret == 255)
766 uci_usage();
767
768 uci_free_context(ctx);
769
770 return ret;
771 }