extend delete command so it can delete list elemets using index
[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>][=<id>]]]\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 int dummy;
418
419 if (argc != 2)
420 return 255;
421
422 if (uci_lookup_ptr(ctx, &ptr, argv[1], true) != UCI_OK) {
423 cli_perror();
424 return 1;
425 }
426
427 if (ptr.value && (cmd != CMD_SET) && (cmd != CMD_DEL) &&
428 (cmd != CMD_ADD_LIST) && (cmd != CMD_DEL_LIST) &&
429 (cmd != CMD_RENAME) && (cmd != CMD_REORDER))
430 return 1;
431
432 e = ptr.last;
433 switch(cmd) {
434 case CMD_GET:
435 if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
436 ctx->err = UCI_ERR_NOTFOUND;
437 cli_perror();
438 return 1;
439 }
440 switch(e->type) {
441 case UCI_TYPE_SECTION:
442 printf("%s\n", ptr.s->type);
443 break;
444 case UCI_TYPE_OPTION:
445 uci_show_value(ptr.o);
446 break;
447 default:
448 break;
449 }
450 /* throw the value to stdout */
451 break;
452 case CMD_RENAME:
453 ret = uci_rename(ctx, &ptr);
454 break;
455 case CMD_REVERT:
456 ret = uci_revert(ctx, &ptr);
457 break;
458 case CMD_SET:
459 ret = uci_set(ctx, &ptr);
460 break;
461 case CMD_ADD_LIST:
462 ret = uci_add_list(ctx, &ptr);
463 break;
464 case CMD_DEL_LIST:
465 ret = uci_del_list(ctx, &ptr);
466 break;
467 case CMD_REORDER:
468 if (!ptr.s || !ptr.value) {
469 ctx->err = UCI_ERR_NOTFOUND;
470 cli_perror();
471 return 1;
472 }
473 ret = uci_reorder_section(ctx, ptr.s, strtoul(ptr.value, NULL, 10));
474 break;
475 case CMD_DEL:
476 if (ptr.value && !sscanf(ptr.value, "%d", &dummy))
477 return 1;
478 ret = uci_delete(ctx, &ptr);
479 break;
480 }
481
482 /* no save necessary for get */
483 if ((cmd == CMD_GET) || (cmd == CMD_REVERT))
484 return 0;
485
486 /* save changes, but don't commit them yet */
487 if (ret == UCI_OK)
488 ret = uci_save(ctx, ptr.p);
489
490 if (ret != UCI_OK) {
491 cli_perror();
492 return 1;
493 }
494
495 return 0;
496 }
497
498 static int uci_batch_cmd(void)
499 {
500 char *argv[MAX_ARGS + 2];
501 char *str = NULL;
502 int ret = 0;
503 int i, j;
504
505 for(i = 0; i <= MAX_ARGS; i++) {
506 if (i == MAX_ARGS) {
507 fprintf(stderr, "Too many arguments\n");
508 return 1;
509 }
510 argv[i] = NULL;
511 if ((ret = uci_parse_argument(ctx, input, &str, &argv[i])) != UCI_OK) {
512 cli_perror();
513 i = 0;
514 break;
515 }
516 if (!argv[i][0])
517 break;
518 argv[i] = strdup(argv[i]);
519 if (!argv[i]) {
520 perror("uci");
521 return 1;
522 }
523 }
524 argv[i] = NULL;
525
526 if (i > 0) {
527 if (!strcasecmp(argv[0], "exit"))
528 return 254;
529 ret = uci_cmd(i, argv);
530 } else
531 return 0;
532
533 for (j = 0; j < i; j++) {
534 if (argv[j])
535 free(argv[j]);
536 }
537
538 return ret;
539 }
540
541 static int uci_batch(void)
542 {
543 int ret = 0;
544
545 flags |= CLI_FLAG_BATCH;
546 while (!feof(input)) {
547 struct uci_element *e, *tmp;
548
549 ret = uci_batch_cmd();
550 if (ret == 254)
551 return 0;
552 else if (ret == 255)
553 fprintf(stderr, "Unknown command\n");
554
555 /* clean up */
556 uci_foreach_element_safe(&ctx->root, tmp, e) {
557 uci_unload(ctx, uci_to_package(e));
558 }
559 }
560 flags &= ~CLI_FLAG_BATCH;
561
562 return 0;
563 }
564
565 static int uci_cmd(int argc, char **argv)
566 {
567 int cmd = 0;
568
569 if (!strcasecmp(argv[0], "batch") && !(flags & CLI_FLAG_BATCH))
570 return uci_batch();
571 else if (!strcasecmp(argv[0], "show"))
572 cmd = CMD_SHOW;
573 else if (!strcasecmp(argv[0], "changes"))
574 cmd = CMD_CHANGES;
575 else if (!strcasecmp(argv[0], "export"))
576 cmd = CMD_EXPORT;
577 else if (!strcasecmp(argv[0], "commit"))
578 cmd = CMD_COMMIT;
579 else if (!strcasecmp(argv[0], "get"))
580 cmd = CMD_GET;
581 else if (!strcasecmp(argv[0], "set"))
582 cmd = CMD_SET;
583 else if (!strcasecmp(argv[0], "ren") ||
584 !strcasecmp(argv[0], "rename"))
585 cmd = CMD_RENAME;
586 else if (!strcasecmp(argv[0], "revert"))
587 cmd = CMD_REVERT;
588 else if (!strcasecmp(argv[0], "reorder"))
589 cmd = CMD_REORDER;
590 else if (!strcasecmp(argv[0], "del") ||
591 !strcasecmp(argv[0], "delete"))
592 cmd = CMD_DEL;
593 else if (!strcasecmp(argv[0], "import"))
594 cmd = CMD_IMPORT;
595 else if (!strcasecmp(argv[0], "help"))
596 cmd = CMD_HELP;
597 else if (!strcasecmp(argv[0], "add"))
598 cmd = CMD_ADD;
599 else if (!strcasecmp(argv[0], "add_list"))
600 cmd = CMD_ADD_LIST;
601 else if (!strcasecmp(argv[0], "del_list"))
602 cmd = CMD_DEL_LIST;
603 else
604 cmd = -1;
605
606 switch(cmd) {
607 case CMD_ADD_LIST:
608 case CMD_DEL_LIST:
609 case CMD_GET:
610 case CMD_SET:
611 case CMD_DEL:
612 case CMD_RENAME:
613 case CMD_REVERT:
614 case CMD_REORDER:
615 return uci_do_section_cmd(cmd, argc, argv);
616 case CMD_SHOW:
617 case CMD_EXPORT:
618 case CMD_COMMIT:
619 case CMD_CHANGES:
620 return uci_do_package_cmd(cmd, argc, argv);
621 case CMD_IMPORT:
622 return uci_do_import(argc, argv);
623 case CMD_ADD:
624 return uci_do_add(argc, argv);
625 case CMD_HELP:
626 uci_usage();
627 return 0;
628 default:
629 return 255;
630 }
631 }
632
633 int main(int argc, char **argv)
634 {
635 int ret;
636 int c;
637
638 flags = CLI_FLAG_SHOW_EXT;
639 appname = argv[0];
640 input = stdin;
641 ctx = uci_alloc_context();
642 if (!ctx) {
643 fprintf(stderr, "Out of memory\n");
644 return 1;
645 }
646
647 while((c = getopt(argc, argv, "c:d:f:LmnNp:P:sSqX")) != -1) {
648 switch(c) {
649 case 'c':
650 uci_set_confdir(ctx, optarg);
651 break;
652 case 'd':
653 delimiter = optarg;
654 break;
655 case 'f':
656 if (input != stdin) {
657 perror("uci");
658 return 1;
659 }
660
661 input = fopen(optarg, "r");
662 if (!input) {
663 perror("uci");
664 return 1;
665 }
666 break;
667 case 'L':
668 flags |= CLI_FLAG_NOPLUGINS;
669 break;
670 case 'm':
671 flags |= CLI_FLAG_MERGE;
672 break;
673 case 's':
674 ctx->flags |= UCI_FLAG_STRICT;
675 break;
676 case 'S':
677 ctx->flags &= ~UCI_FLAG_STRICT;
678 ctx->flags |= UCI_FLAG_PERROR;
679 break;
680 case 'n':
681 ctx->flags |= UCI_FLAG_EXPORT_NAME;
682 break;
683 case 'N':
684 ctx->flags &= ~UCI_FLAG_EXPORT_NAME;
685 break;
686 case 'p':
687 uci_add_delta_path(ctx, optarg);
688 break;
689 case 'P':
690 uci_add_delta_path(ctx, ctx->savedir);
691 uci_set_savedir(ctx, optarg);
692 flags |= CLI_FLAG_NOCOMMIT;
693 break;
694 case 'q':
695 flags |= CLI_FLAG_QUIET;
696 break;
697 case 'X':
698 flags &= ~CLI_FLAG_SHOW_EXT;
699 break;
700 default:
701 uci_usage();
702 return 0;
703 }
704 }
705 if (optind > 1)
706 argv[optind - 1] = argv[0];
707 argv += optind - 1;
708 argc -= optind - 1;
709
710 if (argc < 2) {
711 uci_usage();
712 return 0;
713 }
714
715 if (!(flags & CLI_FLAG_NOPLUGINS))
716 uci_load_plugins(ctx, NULL);
717
718 ret = uci_cmd(argc - 1, argv + 1);
719 if (input != stdin)
720 fclose(input);
721
722 if (ret == 255)
723 uci_usage();
724
725 uci_free_context(ctx);
726
727 return ret;
728 }