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