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