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),
34 static struct uci_context *ctx;
56 struct uci_type_list {
59 struct uci_type_list *next;
62 static struct uci_type_list *type_list = NULL;
63 static char *typestr = NULL;
64 static const char *cur_section_ref = NULL;
66 static int uci_cmd(int argc, char **argv);
69 uci_reset_typelist(void)
71 struct uci_type_list *type;
72 while (type_list != NULL) {
74 type_list = type_list->next;
81 cur_section_ref = NULL;
85 uci_lookup_section_ref(struct uci_section *s)
87 struct uci_type_list *ti = type_list;
90 if (!s->anonymous || !(flags & CLI_FLAG_SHOW_EXT))
93 /* look up in section type list */
95 if (strcmp(ti->name, s->type) == 0)
100 ti = malloc(sizeof(struct uci_type_list));
103 memset(ti, 0, sizeof(struct uci_type_list));
104 ti->next = type_list;
109 maxlen = strlen(s->type) + 1 + 2 + 10;
111 typestr = malloc(maxlen);
113 typestr = realloc(typestr, maxlen);
117 sprintf(typestr, "@%s[%d]", ti->name, ti->idx);
124 static void uci_usage(void)
127 "Usage: %s [<options>] <command> [<arguments>]\n\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"
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"
163 static void cli_perror(void)
165 if (flags & CLI_FLAG_QUIET)
168 uci_perror(ctx, appname);
171 static void uci_print_value(FILE *f, const char *v)
184 static void uci_show_value(struct uci_option *o, bool quote)
186 struct uci_element *e;
191 case UCI_TYPE_STRING:
193 uci_print_value(stdout, o->v.string);
195 printf("%s", o->v.string);
199 uci_foreach_element(&o->v.list, e) {
200 printf("%s", (sep ? delimiter : ""));
201 space = strpbrk(e->name, " \t\r\n");
202 if (!space && !quote)
203 printf("%s", e->name);
205 uci_print_value(stdout, e->name);
211 printf("<unknown>\n");
216 static void uci_show_option(struct uci_option *o, bool quote)
219 o->section->package->e.name,
220 (cur_section_ref ? cur_section_ref : o->section->e.name),
222 uci_show_value(o, quote);
225 static void uci_show_section(struct uci_section *s)
227 struct uci_element *e;
231 cname = s->package->e.name;
232 sname = (cur_section_ref ? cur_section_ref : s->e.name);
233 printf("%s.%s=%s\n", cname, sname, s->type);
234 uci_foreach_element(&s->options, e) {
235 uci_show_option(uci_to_option(e), true);
239 static void uci_show_package(struct uci_package *p)
241 struct uci_element *e;
243 uci_reset_typelist();
244 uci_foreach_element( &p->sections, e) {
245 struct uci_section *s = uci_to_section(e);
246 cur_section_ref = uci_lookup_section_ref(s);
249 uci_reset_typelist();
252 static void uci_show_changes(struct uci_package *p)
254 struct uci_element *e;
256 uci_foreach_element(&p->saved_delta, e) {
257 struct uci_delta *h = uci_to_delta(e);
265 case UCI_CMD_LIST_ADD:
268 case UCI_CMD_LIST_DEL:
274 printf("%s%s.%s", prefix, p->e.name, h->section);
276 printf(".%s", e->name);
277 if (h->cmd != UCI_CMD_REMOVE) {
279 uci_print_value(stdout, h->value);
285 static int package_cmd(int cmd, char *tuple)
287 struct uci_element *e = NULL;
291 if (uci_lookup_ptr(ctx, &ptr, tuple, true) != UCI_OK) {
299 uci_show_changes(ptr.p);
302 if (flags & CLI_FLAG_NOCOMMIT) {
306 if (uci_commit(ctx, &ptr.p, false) != UCI_OK) {
312 if (uci_export(ctx, stdout, ptr.p, true) != UCI_OK) {
317 if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
318 ctx->err = UCI_ERR_NOTFOUND;
323 case UCI_TYPE_PACKAGE:
324 uci_show_package(ptr.p);
326 case UCI_TYPE_SECTION:
327 uci_show_section(ptr.s);
329 case UCI_TYPE_OPTION:
330 uci_show_option(ptr.o, true);
333 /* should not happen */
343 uci_unload(ctx, ptr.p);
347 static int uci_do_import(int argc, char **argv)
349 struct uci_package *package = NULL;
359 else if (flags & CLI_FLAG_MERGE)
360 /* need a package to merge */
363 if (flags & CLI_FLAG_MERGE) {
364 if (uci_load(ctx, name, &package) != UCI_OK)
369 ret = uci_import(ctx, input, name, &package, (name != NULL));
372 ret = uci_save(ctx, package);
374 struct uci_element *e;
375 /* loop through all config sections and overwrite existing data */
376 uci_foreach_element(&ctx->root, e) {
377 struct uci_package *p = uci_to_package(e);
378 ret = uci_commit(ctx, &p, true);
391 static int uci_do_package_cmd(int cmd, int argc, char **argv)
393 char **configs = NULL;
401 return package_cmd(cmd, argv[1]);
403 if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
408 for (p = configs; *p; p++) {
409 package_cmd(cmd, *p);
418 static int uci_do_add(int argc, char **argv)
420 struct uci_package *p = NULL;
421 struct uci_section *s = NULL;
427 ret = uci_load(ctx, argv[1], &p);
431 ret = uci_add_section(ctx, p, argv[2], &s);
435 ret = uci_save(ctx, p);
441 fprintf(stdout, "%s\n", s->e.name);
446 static int uci_do_section_cmd(int cmd, int argc, char **argv)
448 struct uci_element *e;
456 if (uci_lookup_ptr(ctx, &ptr, argv[1], true) != UCI_OK) {
461 if (ptr.value && (cmd != CMD_SET) && (cmd != CMD_DEL) &&
462 (cmd != CMD_ADD_LIST) && (cmd != CMD_DEL_LIST) &&
463 (cmd != CMD_RENAME) && (cmd != CMD_REORDER))
469 if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
470 ctx->err = UCI_ERR_NOTFOUND;
475 case UCI_TYPE_SECTION:
476 printf("%s\n", ptr.s->type);
478 case UCI_TYPE_OPTION:
479 uci_show_value(ptr.o, false);
484 /* throw the value to stdout */
487 ret = uci_rename(ctx, &ptr);
490 ret = uci_revert(ctx, &ptr);
493 ret = uci_set(ctx, &ptr);
496 ret = uci_add_list(ctx, &ptr);
499 ret = uci_del_list(ctx, &ptr);
502 if (!ptr.s || !ptr.value) {
503 ctx->err = UCI_ERR_NOTFOUND;
507 ret = uci_reorder_section(ctx, ptr.s, strtoul(ptr.value, NULL, 10));
510 if (ptr.value && !sscanf(ptr.value, "%d", &dummy))
512 ret = uci_delete(ctx, &ptr);
516 /* no save necessary for get */
517 if ((cmd == CMD_GET) || (cmd == CMD_REVERT))
520 /* save changes, but don't commit them yet */
522 ret = uci_save(ctx, ptr.p);
532 static int uci_batch_cmd(void)
534 char *argv[MAX_ARGS + 2];
539 for(i = 0; i <= MAX_ARGS; i++) {
541 fprintf(stderr, "Too many arguments\n");
545 if ((ret = uci_parse_argument(ctx, input, &str, &argv[i])) != UCI_OK) {
552 argv[i] = strdup(argv[i]);
561 if (!strcasecmp(argv[0], "exit"))
563 ret = uci_cmd(i, argv);
567 for (j = 0; j < i; j++) {
574 static int uci_batch(void)
578 flags |= CLI_FLAG_BATCH;
579 while (!feof(input)) {
580 struct uci_element *e, *tmp;
582 ret = uci_batch_cmd();
586 fprintf(stderr, "Unknown command\n");
589 uci_foreach_element_safe(&ctx->root, tmp, e) {
590 uci_unload(ctx, uci_to_package(e));
593 flags &= ~CLI_FLAG_BATCH;
598 static int uci_cmd(int argc, char **argv)
602 if (!strcasecmp(argv[0], "batch") && !(flags & CLI_FLAG_BATCH))
604 else if (!strcasecmp(argv[0], "show"))
606 else if (!strcasecmp(argv[0], "changes"))
608 else if (!strcasecmp(argv[0], "export"))
610 else if (!strcasecmp(argv[0], "commit"))
612 else if (!strcasecmp(argv[0], "get"))
614 else if (!strcasecmp(argv[0], "set"))
616 else if (!strcasecmp(argv[0], "ren") ||
617 !strcasecmp(argv[0], "rename"))
619 else if (!strcasecmp(argv[0], "revert"))
621 else if (!strcasecmp(argv[0], "reorder"))
623 else if (!strcasecmp(argv[0], "del") ||
624 !strcasecmp(argv[0], "delete"))
626 else if (!strcasecmp(argv[0], "import"))
628 else if (!strcasecmp(argv[0], "help"))
630 else if (!strcasecmp(argv[0], "add"))
632 else if (!strcasecmp(argv[0], "add_list"))
634 else if (!strcasecmp(argv[0], "del_list"))
648 return uci_do_section_cmd(cmd, argc, argv);
653 return uci_do_package_cmd(cmd, argc, argv);
655 return uci_do_import(argc, argv);
657 return uci_do_add(argc, argv);
666 int main(int argc, char **argv)
671 flags = CLI_FLAG_SHOW_EXT;
674 ctx = uci_alloc_context();
676 fprintf(stderr, "Out of memory\n");
680 while((c = getopt(argc, argv, "c:d:f:LmnNp:P:sSqX")) != -1) {
683 uci_set_confdir(ctx, optarg);
689 if (input != stdin) {
695 input = fopen(optarg, "r");
702 flags |= CLI_FLAG_MERGE;
705 ctx->flags |= UCI_FLAG_STRICT;
708 ctx->flags &= ~UCI_FLAG_STRICT;
709 ctx->flags |= UCI_FLAG_PERROR;
712 ctx->flags |= UCI_FLAG_EXPORT_NAME;
715 ctx->flags &= ~UCI_FLAG_EXPORT_NAME;
718 uci_add_delta_path(ctx, optarg);
721 uci_add_delta_path(ctx, ctx->savedir);
722 uci_set_savedir(ctx, optarg);
723 flags |= CLI_FLAG_NOCOMMIT;
726 flags |= CLI_FLAG_QUIET;
729 flags &= ~CLI_FLAG_SHOW_EXT;
737 argv[optind - 1] = argv[0];
746 ret = uci_cmd(argc - 1, argv + 1);
753 uci_free_context(ctx);