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.
22 #define MAX_ARGS 4 /* max command line arguments for batch mode */
24 static const char *delimiter = " ";
25 static const char *appname;
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),
36 static struct uci_context *ctx;
58 struct uci_type_list {
61 struct uci_type_list *next;
64 static struct uci_type_list *type_list = NULL;
65 static char *typestr = NULL;
66 static const char *cur_section_ref = NULL;
68 static int uci_cmd(int argc, char **argv);
71 uci_reset_typelist(void)
73 struct uci_type_list *type;
74 while (type_list != NULL) {
76 type_list = type_list->next;
83 cur_section_ref = NULL;
87 uci_lookup_section_ref(struct uci_section *s)
89 struct uci_type_list *ti = type_list;
92 if (!s->anonymous || !(flags & CLI_FLAG_SHOW_EXT))
95 /* look up in section type list */
97 if (strcmp(ti->name, s->type) == 0)
102 ti = malloc(sizeof(struct uci_type_list));
105 memset(ti, 0, sizeof(struct uci_type_list));
106 ti->next = type_list;
111 maxlen = strlen(s->type) + 1 + 2 + 10;
113 typestr = malloc(maxlen);
115 typestr = realloc(typestr, maxlen);
119 sprintf(typestr, "@%s[%d]", ti->name, ti->idx);
126 static void uci_usage(void)
129 "Usage: %s [<options>] <command> [<arguments>]\n\n"
132 "\texport [<config>]\n"
133 "\timport [<config>]\n"
134 "\tchanges [<config>]\n"
135 "\tcommit [<config>]\n"
136 "\tadd <config> <section-type>\n"
137 "\tadd_list <config>.<section>.<option>=<string>\n"
138 "\tdel_list <config>.<section>.<option>=<string>\n"
139 "\tshow [<config>[.<section>[.<option>]]]\n"
140 "\tget <config>.<section>[.<option>]\n"
141 "\tset <config>.<section>[.<option>]=<value>\n"
142 "\tdelete <config>[.<section>[[.<option>][=<id>]]]\n"
143 "\trename <config>.<section>[.<option>]=<name>\n"
144 "\trevert <config>[.<section>[.<option>]]\n"
145 "\treorder <config>.<section>=<position>\n"
148 "\t-c <path> set the search path for config files (default: /etc/config)\n"
149 "\t-d <str> set the delimiter for list values in uci show\n"
150 "\t-f <file> use <file> as input instead of stdin\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 cli_error(const char *fmt, ...)
177 if (flags & CLI_FLAG_QUIET)
181 vfprintf(stderr, fmt, ap);
185 static void uci_print_value(FILE *f, const char *v)
198 static void uci_show_value(struct uci_option *o, bool quote)
200 struct uci_element *e;
205 case UCI_TYPE_STRING:
207 uci_print_value(stdout, o->v.string);
209 printf("%s", o->v.string);
213 uci_foreach_element(&o->v.list, e) {
214 printf("%s", (sep ? delimiter : ""));
215 space = strpbrk(e->name, " \t\r\n");
216 if (!space && !quote)
217 printf("%s", e->name);
219 uci_print_value(stdout, e->name);
225 printf("<unknown>\n");
230 static void uci_show_option(struct uci_option *o, bool quote)
233 o->section->package->e.name,
234 (cur_section_ref ? cur_section_ref : o->section->e.name),
236 uci_show_value(o, quote);
239 static void uci_show_section(struct uci_section *s)
241 struct uci_element *e;
245 cname = s->package->e.name;
246 sname = (cur_section_ref ? cur_section_ref : s->e.name);
247 printf("%s.%s=%s\n", cname, sname, s->type);
248 uci_foreach_element(&s->options, e) {
249 uci_show_option(uci_to_option(e), true);
253 static void uci_show_package(struct uci_package *p)
255 struct uci_element *e;
257 uci_reset_typelist();
258 uci_foreach_element( &p->sections, e) {
259 struct uci_section *s = uci_to_section(e);
260 cur_section_ref = uci_lookup_section_ref(s);
263 uci_reset_typelist();
266 static void uci_show_changes(struct uci_package *p)
268 struct uci_element *e;
270 uci_foreach_element(&p->saved_delta, e) {
271 struct uci_delta *h = uci_to_delta(e);
279 case UCI_CMD_LIST_ADD:
282 case UCI_CMD_LIST_DEL:
288 printf("%s%s.%s", prefix, p->e.name, h->section);
290 printf(".%s", e->name);
291 if (h->cmd != UCI_CMD_REMOVE) {
293 uci_print_value(stdout, h->value);
299 static int package_cmd(int cmd, char *tuple)
301 struct uci_element *e = NULL;
305 if (uci_lookup_ptr(ctx, &ptr, tuple, true) != UCI_OK) {
313 uci_show_changes(ptr.p);
316 if (flags & CLI_FLAG_NOCOMMIT) {
320 if (uci_commit(ctx, &ptr.p, false) != UCI_OK) {
326 if (uci_export(ctx, stdout, ptr.p, true) != UCI_OK) {
331 if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
332 ctx->err = UCI_ERR_NOTFOUND;
337 case UCI_TYPE_PACKAGE:
338 uci_show_package(ptr.p);
340 case UCI_TYPE_SECTION:
341 uci_show_section(ptr.s);
343 case UCI_TYPE_OPTION:
344 uci_show_option(ptr.o, true);
347 /* should not happen */
357 uci_unload(ctx, ptr.p);
361 static int uci_do_import(int argc, char **argv)
363 struct uci_package *package = NULL;
373 else if (flags & CLI_FLAG_MERGE)
374 /* need a package to merge */
377 if (flags & CLI_FLAG_MERGE) {
378 if (uci_load(ctx, name, &package) != UCI_OK)
383 ret = uci_import(ctx, input, name, &package, (name != NULL));
386 ret = uci_save(ctx, package);
388 struct uci_element *e;
389 /* loop through all config sections and overwrite existing data */
390 uci_foreach_element(&ctx->root, e) {
391 struct uci_package *p = uci_to_package(e);
392 ret = uci_commit(ctx, &p, true);
405 static int uci_do_package_cmd(int cmd, int argc, char **argv)
407 char **configs = NULL;
415 return package_cmd(cmd, argv[1]);
417 if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
422 for (p = configs; *p; p++) {
423 package_cmd(cmd, *p);
432 static int uci_do_add(int argc, char **argv)
434 struct uci_package *p = NULL;
435 struct uci_section *s = NULL;
441 ret = uci_load(ctx, argv[1], &p);
445 ret = uci_add_section(ctx, p, argv[2], &s);
449 ret = uci_save(ctx, p);
455 fprintf(stdout, "%s\n", s->e.name);
460 static int uci_do_section_cmd(int cmd, int argc, char **argv)
462 struct uci_element *e;
470 if (uci_lookup_ptr(ctx, &ptr, argv[1], true) != UCI_OK) {
475 if (ptr.value && (cmd != CMD_SET) && (cmd != CMD_DEL) &&
476 (cmd != CMD_ADD_LIST) && (cmd != CMD_DEL_LIST) &&
477 (cmd != CMD_RENAME) && (cmd != CMD_REORDER))
483 if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
484 ctx->err = UCI_ERR_NOTFOUND;
489 case UCI_TYPE_SECTION:
490 printf("%s\n", ptr.s->type);
492 case UCI_TYPE_OPTION:
493 uci_show_value(ptr.o, false);
498 /* throw the value to stdout */
501 ret = uci_rename(ctx, &ptr);
504 ret = uci_revert(ctx, &ptr);
507 ret = uci_set(ctx, &ptr);
510 ret = uci_add_list(ctx, &ptr);
513 ret = uci_del_list(ctx, &ptr);
516 if (!ptr.s || !ptr.value) {
517 ctx->err = UCI_ERR_NOTFOUND;
521 ret = uci_reorder_section(ctx, ptr.s, strtoul(ptr.value, NULL, 10));
524 if (ptr.value && !sscanf(ptr.value, "%d", &dummy))
526 ret = uci_delete(ctx, &ptr);
530 /* no save necessary for get */
531 if ((cmd == CMD_GET) || (cmd == CMD_REVERT))
534 /* save changes, but don't commit them yet */
536 ret = uci_save(ctx, ptr.p);
546 static int uci_batch_cmd(void)
548 char *argv[MAX_ARGS + 2];
553 for(i = 0; i <= MAX_ARGS; i++) {
555 cli_error("Too many arguments\n");
559 if ((ret = uci_parse_argument(ctx, input, &str, &argv[i])) != UCI_OK) {
566 argv[i] = strdup(argv[i]);
568 cli_error("uci: %s", strerror(errno));
575 if (!strcasecmp(argv[0], "exit"))
577 ret = uci_cmd(i, argv);
581 for (j = 0; j < i; j++) {
588 static int uci_batch(void)
592 flags |= CLI_FLAG_BATCH;
593 while (!feof(input)) {
594 struct uci_element *e, *tmp;
596 ret = uci_batch_cmd();
600 cli_error("Unknown command\n");
603 uci_foreach_element_safe(&ctx->root, tmp, e) {
604 uci_unload(ctx, uci_to_package(e));
607 flags &= ~CLI_FLAG_BATCH;
612 static int uci_cmd(int argc, char **argv)
616 if (!strcasecmp(argv[0], "batch") && !(flags & CLI_FLAG_BATCH))
618 else if (!strcasecmp(argv[0], "show"))
620 else if (!strcasecmp(argv[0], "changes"))
622 else if (!strcasecmp(argv[0], "export"))
624 else if (!strcasecmp(argv[0], "commit"))
626 else if (!strcasecmp(argv[0], "get"))
628 else if (!strcasecmp(argv[0], "set"))
630 else if (!strcasecmp(argv[0], "ren") ||
631 !strcasecmp(argv[0], "rename"))
633 else if (!strcasecmp(argv[0], "revert"))
635 else if (!strcasecmp(argv[0], "reorder"))
637 else if (!strcasecmp(argv[0], "del") ||
638 !strcasecmp(argv[0], "delete"))
640 else if (!strcasecmp(argv[0], "import"))
642 else if (!strcasecmp(argv[0], "help"))
644 else if (!strcasecmp(argv[0], "add"))
646 else if (!strcasecmp(argv[0], "add_list"))
648 else if (!strcasecmp(argv[0], "del_list"))
662 return uci_do_section_cmd(cmd, argc, argv);
667 return uci_do_package_cmd(cmd, argc, argv);
669 return uci_do_import(argc, argv);
671 return uci_do_add(argc, argv);
680 int main(int argc, char **argv)
685 flags = CLI_FLAG_SHOW_EXT;
688 ctx = uci_alloc_context();
690 cli_error("Out of memory\n");
694 while((c = getopt(argc, argv, "c:d:f:LmnNp:P:sSqX")) != -1) {
697 uci_set_confdir(ctx, optarg);
703 if (input != stdin) {
705 cli_error("Too many input files.\n");
709 input = fopen(optarg, "r");
711 cli_error("uci: %s", strerror(errno));
716 flags |= CLI_FLAG_MERGE;
719 ctx->flags |= UCI_FLAG_STRICT;
722 ctx->flags &= ~UCI_FLAG_STRICT;
723 ctx->flags |= UCI_FLAG_PERROR;
726 ctx->flags |= UCI_FLAG_EXPORT_NAME;
729 ctx->flags &= ~UCI_FLAG_EXPORT_NAME;
732 uci_add_delta_path(ctx, optarg);
735 uci_add_delta_path(ctx, ctx->savedir);
736 uci_set_savedir(ctx, optarg);
737 flags |= CLI_FLAG_NOCOMMIT;
740 flags |= CLI_FLAG_QUIET;
743 flags &= ~CLI_FLAG_SHOW_EXT;
751 argv[optind - 1] = argv[0];
760 ret = uci_cmd(argc - 1, argv + 1);
767 uci_free_context(ctx);