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),
33 static struct uci_context *ctx;
53 struct uci_type_list {
56 struct uci_type_list *next;
59 static struct uci_type_list *type_list = NULL;
60 static char *typestr = NULL;
61 static const char *cur_section_ref = NULL;
63 static int uci_cmd(int argc, char **argv);
66 uci_reset_typelist(void)
68 struct uci_type_list *type;
69 while (type_list != NULL) {
71 type_list = type_list->next;
78 cur_section_ref = NULL;
82 uci_lookup_section_ref(struct uci_section *s)
84 struct uci_type_list *ti = type_list;
90 /* look up in section type list */
92 if (strcmp(ti->name, s->type) == 0)
97 ti = malloc(sizeof(struct uci_type_list));
98 memset(ti, 0, sizeof(struct uci_type_list));
104 maxlen = strlen(s->type) + 1 + 2 + 10;
106 typestr = malloc(maxlen);
108 typestr = realloc(typestr, maxlen);
110 sprintf(typestr, "@%s[%d]", ti->name, ti->idx);
115 static void uci_usage(void)
118 "Usage: %s [<options>] <command> [<arguments>]\n\n"
121 "\texport [<config>]\n"
122 "\timport [<config>]\n"
123 "\tchanges [<config>]\n"
124 "\tcommit [<config>]\n"
125 "\tadd <config> <section-type>\n"
126 "\tadd_list <config>.<section>.<option>=<string>\n"
127 "\tshow [<config>[.<section>[.<option>]]]\n"
128 "\tget <config>.<section>[.<option>]\n"
129 "\tset <config>.<section>[.<option>]=<value>\n"
130 "\tdelete <config>[.<section[.<option>]]\n"
131 "\trename <config>.<section>[.<option>]=<name>\n"
132 "\trevert <config>[.<section>[.<option>]]\n"
135 "\t-c <path> set the search path for config files (default: /etc/config)\n"
136 "\t-d <str> set the delimiter for list values in uci show\n"
137 "\t-f <file> use <file> as input instead of stdin\n"
138 "\t-m when importing, merge data into an existing package\n"
139 "\t-n name unnamed sections on export (default)\n"
140 "\t-N don't name unnamed sections\n"
141 "\t-p <path> add a search path for config change files\n"
142 "\t-P <path> add a search path for config change files and use as default\n"
143 "\t-q quiet mode (don't print error messages)\n"
144 "\t-s force strict mode (stop on parser errors, default)\n"
145 "\t-S disable strict mode\n"
151 static void cli_perror(void)
153 if (flags & CLI_FLAG_QUIET)
156 uci_perror(ctx, appname);
159 static void uci_show_value(struct uci_option *o)
161 struct uci_element *e;
165 case UCI_TYPE_STRING:
166 printf("%s\n", o->v.string);
169 uci_foreach_element(&o->v.list, e) {
170 printf("%s%s", (sep ? delimiter : ""), e->name);
176 printf("<unknown>\n");
181 static void uci_show_option(struct uci_option *o)
184 o->section->package->e.name,
185 (cur_section_ref ? cur_section_ref : o->section->e.name),
190 static void uci_show_section(struct uci_section *s)
192 struct uci_element *e;
196 cname = s->package->e.name;
197 sname = (cur_section_ref ? cur_section_ref : s->e.name);
198 printf("%s.%s=%s\n", cname, sname, s->type);
199 uci_foreach_element(&s->options, e) {
200 uci_show_option(uci_to_option(e));
204 static void uci_show_package(struct uci_package *p)
206 struct uci_element *e;
208 uci_reset_typelist();
209 uci_foreach_element( &p->sections, e) {
210 struct uci_section *s = uci_to_section(e);
211 cur_section_ref = uci_lookup_section_ref(s);
214 uci_reset_typelist();
217 static void uci_show_changes(struct uci_package *p)
219 struct uci_element *e;
221 uci_foreach_element(&p->saved_history, e) {
222 struct uci_history *h = uci_to_history(e);
230 case UCI_CMD_LIST_ADD:
236 printf("%s%s.%s", prefix, p->e.name, h->section);
238 printf(".%s", e->name);
239 if (h->cmd != UCI_CMD_REMOVE)
240 printf("%s%s", op, h->value);
245 static int package_cmd(int cmd, char *tuple)
247 struct uci_element *e = NULL;
250 if (uci_lookup_ptr(ctx, &ptr, tuple, true) != UCI_OK) {
258 uci_show_changes(ptr.p);
261 if (flags & CLI_FLAG_NOCOMMIT)
263 if (uci_commit(ctx, &ptr.p, false) != UCI_OK)
267 uci_export(ctx, stdout, ptr.p, true);
270 if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
271 ctx->err = UCI_ERR_NOTFOUND;
276 case UCI_TYPE_PACKAGE:
277 uci_show_package(ptr.p);
279 case UCI_TYPE_SECTION:
280 uci_show_section(ptr.s);
282 case UCI_TYPE_OPTION:
283 uci_show_option(ptr.o);
286 /* should not happen */
292 uci_unload(ctx, ptr.p);
296 static int uci_do_import(int argc, char **argv)
298 struct uci_package *package = NULL;
308 else if (flags & CLI_FLAG_MERGE)
309 /* need a package to merge */
312 if (flags & CLI_FLAG_MERGE) {
313 if (uci_load(ctx, name, &package) != UCI_OK)
318 ret = uci_import(ctx, input, name, &package, (name != NULL));
321 ret = uci_save(ctx, package);
323 struct uci_element *e;
324 /* loop through all config sections and overwrite existing data */
325 uci_foreach_element(&ctx->root, e) {
326 struct uci_package *p = uci_to_package(e);
327 ret = uci_commit(ctx, &p, true);
340 static int uci_do_package_cmd(int cmd, int argc, char **argv)
342 char **configs = NULL;
349 return package_cmd(cmd, argv[1]);
351 if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
356 for (p = configs; *p; p++) {
357 package_cmd(cmd, *p);
363 static int uci_do_add(int argc, char **argv)
365 struct uci_package *p = NULL;
366 struct uci_section *s = NULL;
372 ret = uci_load(ctx, argv[1], &p);
376 ret = uci_add_section(ctx, p, argv[2], &s);
380 ret = uci_save(ctx, p);
386 fprintf(stdout, "%s\n", s->e.name);
391 static int uci_do_section_cmd(int cmd, int argc, char **argv)
393 struct uci_element *e;
400 if (uci_lookup_ptr(ctx, &ptr, argv[1], true) != UCI_OK) {
405 if (ptr.value && (cmd != CMD_SET) && (cmd != CMD_ADD_LIST) && (cmd != CMD_RENAME))
411 if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
412 ctx->err = UCI_ERR_NOTFOUND;
417 case UCI_TYPE_SECTION:
418 printf("%s\n", ptr.s->type);
420 case UCI_TYPE_OPTION:
421 uci_show_value(ptr.o);
426 /* throw the value to stdout */
429 ret = uci_rename(ctx, &ptr);
432 ret = uci_revert(ctx, &ptr);
435 ret = uci_set(ctx, &ptr);
438 ret = uci_add_list(ctx, &ptr);
441 ret = uci_delete(ctx, &ptr);
445 /* no save necessary for get */
446 if ((cmd == CMD_GET) || (cmd == CMD_REVERT))
449 /* save changes, but don't commit them yet */
451 ret = uci_save(ctx, ptr.p);
461 static int uci_batch_cmd(void)
463 char *argv[MAX_ARGS];
468 for(i = 0; i <= MAX_ARGS; i++) {
470 fprintf(stderr, "Too many arguments\n");
474 if ((ret = uci_parse_argument(ctx, input, &str, &argv[i])) != UCI_OK) {
481 argv[i] = strdup(argv[i]);
490 if (!strcasecmp(argv[0], "exit"))
492 ret = uci_cmd(i, argv);
496 for (j = 0; j < i; j++) {
504 static int uci_batch(void)
508 flags |= CLI_FLAG_BATCH;
509 while (!feof(input)) {
510 struct uci_element *e, *tmp;
512 ret = uci_batch_cmd();
516 fprintf(stderr, "Unknown command\n");
519 uci_foreach_element_safe(&ctx->root, tmp, e) {
520 uci_unload(ctx, uci_to_package(e));
523 flags &= ~CLI_FLAG_BATCH;
528 static int uci_cmd(int argc, char **argv)
532 if (!strcasecmp(argv[0], "batch") && !(flags & CLI_FLAG_BATCH))
534 else if (!strcasecmp(argv[0], "show"))
536 else if (!strcasecmp(argv[0], "changes"))
538 else if (!strcasecmp(argv[0], "export"))
540 else if (!strcasecmp(argv[0], "commit"))
542 else if (!strcasecmp(argv[0], "get"))
544 else if (!strcasecmp(argv[0], "set"))
546 else if (!strcasecmp(argv[0], "ren") ||
547 !strcasecmp(argv[0], "rename"))
549 else if (!strcasecmp(argv[0], "revert"))
551 else if (!strcasecmp(argv[0], "del") ||
552 !strcasecmp(argv[0], "delete"))
554 else if (!strcasecmp(argv[0], "import"))
556 else if (!strcasecmp(argv[0], "help"))
558 else if (!strcasecmp(argv[0], "add"))
560 else if (!strcasecmp(argv[0], "add_list"))
572 return uci_do_section_cmd(cmd, argc, argv);
577 return uci_do_package_cmd(cmd, argc, argv);
579 return uci_do_import(argc, argv);
581 return uci_do_add(argc, argv);
590 int main(int argc, char **argv)
597 ctx = uci_alloc_context();
599 fprintf(stderr, "Out of memory\n");
603 while((c = getopt(argc, argv, "c:d:f:mnNp:P:sSq")) != -1) {
606 uci_set_confdir(ctx, optarg);
612 input = fopen(optarg, "r");
619 flags |= CLI_FLAG_MERGE;
622 ctx->flags |= UCI_FLAG_STRICT;
625 ctx->flags &= ~UCI_FLAG_STRICT;
626 ctx->flags |= UCI_FLAG_PERROR;
629 ctx->flags |= UCI_FLAG_EXPORT_NAME;
632 ctx->flags &= ~UCI_FLAG_EXPORT_NAME;
635 uci_add_history_path(ctx, optarg);
638 uci_add_history_path(ctx, ctx->savedir);
639 uci_set_savedir(ctx, optarg);
640 flags |= CLI_FLAG_NOCOMMIT;
643 flags |= CLI_FLAG_QUIET;
651 argv[optind - 1] = argv[0];
659 ret = uci_cmd(argc - 1, argv + 1);
667 uci_free_context(ctx);