2 * cli - Command Line Interface for the Unified Configuration Interface
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
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
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.
20 #define MAX_ARGS 4 /* max command line arguments for batch mode */
22 static const char *delimiter = " ";
23 static const char *appname;
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),
35 static struct uci_context *ctx;
57 struct uci_type_list {
60 struct uci_type_list *next;
63 static struct uci_type_list *type_list = NULL;
64 static char *typestr = NULL;
65 static const char *cur_section_ref = NULL;
67 static int uci_cmd(int argc, char **argv);
70 uci_reset_typelist(void)
72 struct uci_type_list *type;
73 while (type_list != NULL) {
75 type_list = type_list->next;
82 cur_section_ref = NULL;
86 uci_lookup_section_ref(struct uci_section *s)
88 struct uci_type_list *ti = type_list;
91 if (!s->anonymous || !(flags & CLI_FLAG_SHOW_EXT))
94 /* look up in section type list */
96 if (strcmp(ti->name, s->type) == 0)
101 ti = malloc(sizeof(struct uci_type_list));
104 memset(ti, 0, sizeof(struct uci_type_list));
105 ti->next = type_list;
110 maxlen = strlen(s->type) + 1 + 2 + 10;
112 typestr = malloc(maxlen);
114 typestr = realloc(typestr, maxlen);
118 sprintf(typestr, "@%s[%d]", ti->name, ti->idx);
125 static void uci_usage(void)
128 "Usage: %s [<options>] <command> [<arguments>]\n\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"
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"
165 static void cli_perror(void)
167 if (flags & CLI_FLAG_QUIET)
170 uci_perror(ctx, appname);
173 static void uci_show_value(struct uci_option *o)
175 struct uci_element *e;
179 case UCI_TYPE_STRING:
180 printf("%s\n", o->v.string);
183 uci_foreach_element(&o->v.list, e) {
184 printf("%s%s", (sep ? delimiter : ""), e->name);
190 printf("<unknown>\n");
195 static void uci_show_option(struct uci_option *o)
198 o->section->package->e.name,
199 (cur_section_ref ? cur_section_ref : o->section->e.name),
204 static void uci_show_section(struct uci_section *s)
206 struct uci_element *e;
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));
218 static void uci_show_package(struct uci_package *p)
220 struct uci_element *e;
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);
228 uci_reset_typelist();
231 static void uci_show_changes(struct uci_package *p)
233 struct uci_element *e;
235 uci_foreach_element(&p->saved_delta, e) {
236 struct uci_delta *h = uci_to_delta(e);
244 case UCI_CMD_LIST_ADD:
247 case UCI_CMD_LIST_DEL:
253 printf("%s%s.%s", prefix, p->e.name, h->section);
255 printf(".%s", e->name);
256 if (h->cmd != UCI_CMD_REMOVE)
257 printf("%s%s", op, h->value);
262 static int package_cmd(int cmd, char *tuple)
264 struct uci_element *e = NULL;
268 if (uci_lookup_ptr(ctx, &ptr, tuple, true) != UCI_OK) {
276 uci_show_changes(ptr.p);
279 if (flags & CLI_FLAG_NOCOMMIT)
281 if (uci_commit(ctx, &ptr.p, false) != UCI_OK) {
287 uci_export(ctx, stdout, ptr.p, true);
290 if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
291 ctx->err = UCI_ERR_NOTFOUND;
296 case UCI_TYPE_PACKAGE:
297 uci_show_package(ptr.p);
299 case UCI_TYPE_SECTION:
300 uci_show_section(ptr.s);
302 case UCI_TYPE_OPTION:
303 uci_show_option(ptr.o);
306 /* should not happen */
313 uci_unload(ctx, ptr.p);
317 static int uci_do_import(int argc, char **argv)
319 struct uci_package *package = NULL;
329 else if (flags & CLI_FLAG_MERGE)
330 /* need a package to merge */
333 if (flags & CLI_FLAG_MERGE) {
334 if (uci_load(ctx, name, &package) != UCI_OK)
339 ret = uci_import(ctx, input, name, &package, (name != NULL));
342 ret = uci_save(ctx, package);
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);
361 static int uci_do_package_cmd(int cmd, int argc, char **argv)
363 char **configs = NULL;
370 return package_cmd(cmd, argv[1]);
372 if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
377 for (p = configs; *p; p++) {
378 package_cmd(cmd, *p);
384 static int uci_do_add(int argc, char **argv)
386 struct uci_package *p = NULL;
387 struct uci_section *s = NULL;
393 ret = uci_load(ctx, argv[1], &p);
397 ret = uci_add_section(ctx, p, argv[2], &s);
401 ret = uci_save(ctx, p);
407 fprintf(stdout, "%s\n", s->e.name);
412 static int uci_do_section_cmd(int cmd, int argc, char **argv)
414 struct uci_element *e;
422 if (uci_lookup_ptr(ctx, &ptr, argv[1], true) != UCI_OK) {
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))
435 if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
436 ctx->err = UCI_ERR_NOTFOUND;
441 case UCI_TYPE_SECTION:
442 printf("%s\n", ptr.s->type);
444 case UCI_TYPE_OPTION:
445 uci_show_value(ptr.o);
450 /* throw the value to stdout */
453 ret = uci_rename(ctx, &ptr);
456 ret = uci_revert(ctx, &ptr);
459 ret = uci_set(ctx, &ptr);
462 ret = uci_add_list(ctx, &ptr);
465 ret = uci_del_list(ctx, &ptr);
468 if (!ptr.s || !ptr.value) {
469 ctx->err = UCI_ERR_NOTFOUND;
473 ret = uci_reorder_section(ctx, ptr.s, strtoul(ptr.value, NULL, 10));
476 if (ptr.value && !sscanf(ptr.value, "%d", &dummy))
478 ret = uci_delete(ctx, &ptr);
482 /* no save necessary for get */
483 if ((cmd == CMD_GET) || (cmd == CMD_REVERT))
486 /* save changes, but don't commit them yet */
488 ret = uci_save(ctx, ptr.p);
498 static int uci_batch_cmd(void)
500 char *argv[MAX_ARGS + 2];
505 for(i = 0; i <= MAX_ARGS; i++) {
507 fprintf(stderr, "Too many arguments\n");
511 if ((ret = uci_parse_argument(ctx, input, &str, &argv[i])) != UCI_OK) {
518 argv[i] = strdup(argv[i]);
527 if (!strcasecmp(argv[0], "exit"))
529 ret = uci_cmd(i, argv);
533 for (j = 0; j < i; j++) {
541 static int uci_batch(void)
545 flags |= CLI_FLAG_BATCH;
546 while (!feof(input)) {
547 struct uci_element *e, *tmp;
549 ret = uci_batch_cmd();
553 fprintf(stderr, "Unknown command\n");
556 uci_foreach_element_safe(&ctx->root, tmp, e) {
557 uci_unload(ctx, uci_to_package(e));
560 flags &= ~CLI_FLAG_BATCH;
565 static int uci_cmd(int argc, char **argv)
569 if (!strcasecmp(argv[0], "batch") && !(flags & CLI_FLAG_BATCH))
571 else if (!strcasecmp(argv[0], "show"))
573 else if (!strcasecmp(argv[0], "changes"))
575 else if (!strcasecmp(argv[0], "export"))
577 else if (!strcasecmp(argv[0], "commit"))
579 else if (!strcasecmp(argv[0], "get"))
581 else if (!strcasecmp(argv[0], "set"))
583 else if (!strcasecmp(argv[0], "ren") ||
584 !strcasecmp(argv[0], "rename"))
586 else if (!strcasecmp(argv[0], "revert"))
588 else if (!strcasecmp(argv[0], "reorder"))
590 else if (!strcasecmp(argv[0], "del") ||
591 !strcasecmp(argv[0], "delete"))
593 else if (!strcasecmp(argv[0], "import"))
595 else if (!strcasecmp(argv[0], "help"))
597 else if (!strcasecmp(argv[0], "add"))
599 else if (!strcasecmp(argv[0], "add_list"))
601 else if (!strcasecmp(argv[0], "del_list"))
615 return uci_do_section_cmd(cmd, argc, argv);
620 return uci_do_package_cmd(cmd, argc, argv);
622 return uci_do_import(argc, argv);
624 return uci_do_add(argc, argv);
633 int main(int argc, char **argv)
638 flags = CLI_FLAG_SHOW_EXT;
641 ctx = uci_alloc_context();
643 fprintf(stderr, "Out of memory\n");
647 while((c = getopt(argc, argv, "c:d:f:LmnNp:P:sSqX")) != -1) {
650 uci_set_confdir(ctx, optarg);
656 if (input != stdin) {
661 input = fopen(optarg, "r");
668 flags |= CLI_FLAG_NOPLUGINS;
671 flags |= CLI_FLAG_MERGE;
674 ctx->flags |= UCI_FLAG_STRICT;
677 ctx->flags &= ~UCI_FLAG_STRICT;
678 ctx->flags |= UCI_FLAG_PERROR;
681 ctx->flags |= UCI_FLAG_EXPORT_NAME;
684 ctx->flags &= ~UCI_FLAG_EXPORT_NAME;
687 uci_add_delta_path(ctx, optarg);
690 uci_add_delta_path(ctx, ctx->savedir);
691 uci_set_savedir(ctx, optarg);
692 flags |= CLI_FLAG_NOCOMMIT;
695 flags |= CLI_FLAG_QUIET;
698 flags &= ~CLI_FLAG_SHOW_EXT;
706 argv[optind - 1] = argv[0];
715 if (!(flags & CLI_FLAG_NOPLUGINS))
716 uci_load_plugins(ctx, NULL);
718 ret = uci_cmd(argc - 1, argv + 1);
725 uci_free_context(ctx);