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 static int uci_cmd(int argc, char **argv);
55 static void uci_usage(void)
58 "Usage: %s [<options>] <command> [<arguments>]\n\n"
61 "\texport [<config>]\n"
62 "\timport [<config>]\n"
63 "\tchanges [<config>]\n"
64 "\tcommit [<config>]\n"
65 "\tadd <config> <section-type>\n"
66 "\tadd_list <config>.<section>.<option>=<string>\n"
67 "\tshow [<config>[.<section>[.<option>]]]\n"
68 "\tget <config>.<section>[.<option>]\n"
69 "\tset <config>.<section>[.<option>]=<value>\n"
70 "\tdelete <config>[.<section[.<option>]]\n"
71 "\trename <config>.<section>[.<option>]=<name>\n"
72 "\trevert <config>[.<section>[.<option>]]\n"
75 "\t-c <path> set the search path for config files (default: /etc/config)\n"
76 "\t-d <str> set the delimiter for list values in uci show\n"
77 "\t-f <file> use <file> as input instead of stdin\n"
78 "\t-m when importing, merge data into an existing package\n"
79 "\t-n name unnamed sections on export (default)\n"
80 "\t-N don't name unnamed sections\n"
81 "\t-p <path> add a search path for config change files\n"
82 "\t-P <path> add a search path for config change files and use as default\n"
83 "\t-q quiet mode (don't print error messages)\n"
84 "\t-s force strict mode (stop on parser errors, default)\n"
85 "\t-S disable strict mode\n"
91 static void cli_perror(void)
93 if (flags & CLI_FLAG_QUIET)
96 uci_perror(ctx, appname);
99 static void uci_show_value(struct uci_option *o)
101 struct uci_element *e;
105 case UCI_TYPE_STRING:
106 printf("%s\n", o->v.string);
109 uci_foreach_element(&o->v.list, e) {
110 printf("%s%s", (sep ? delimiter : ""), e->name);
116 printf("<unknown>\n");
121 static void uci_show_option(struct uci_option *o)
124 o->section->package->e.name,
130 static void uci_show_section(struct uci_section *p)
132 struct uci_element *e;
133 const char *cname, *sname;
135 cname = p->package->e.name;
137 printf("%s.%s=%s\n", cname, sname, p->type);
138 uci_foreach_element(&p->options, e) {
139 uci_show_option(uci_to_option(e));
143 static void uci_show_package(struct uci_package *p)
145 struct uci_element *e;
147 uci_foreach_element( &p->sections, e) {
148 uci_show_section(uci_to_section(e));
152 static void uci_show_changes(struct uci_package *p)
154 struct uci_element *e;
156 uci_foreach_element(&p->saved_history, e) {
157 struct uci_history *h = uci_to_history(e);
159 if (h->cmd == UCI_CMD_REMOVE)
161 printf("%s.%s", p->e.name, h->section);
163 printf(".%s", e->name);
164 if (h->cmd != UCI_CMD_REMOVE)
165 printf("=%s", h->value);
170 static int package_cmd(int cmd, char *tuple)
172 struct uci_package *p;
173 struct uci_section *s;
174 struct uci_element *e = NULL;
176 if (uci_lookup_ext(ctx, &e, tuple) != UCI_OK) {
181 case UCI_TYPE_PACKAGE:
182 p = uci_to_package(e);
184 case UCI_TYPE_SECTION:
185 s = uci_to_section(e);
188 case UCI_TYPE_OPTION:
189 s = uci_to_option(e)->section;
201 if (flags & CLI_FLAG_NOCOMMIT)
203 if (uci_commit(ctx, &p, false) != UCI_OK)
207 uci_export(ctx, stdout, p, true);
211 case UCI_TYPE_PACKAGE:
214 case UCI_TYPE_SECTION:
215 uci_show_section(uci_to_section(e));
217 case UCI_TYPE_OPTION:
218 uci_show_option(uci_to_option(e));
221 /* should not happen */
231 static int uci_do_import(int argc, char **argv)
233 struct uci_package *package = NULL;
243 else if (flags & CLI_FLAG_MERGE)
244 /* need a package to merge */
247 if (flags & CLI_FLAG_MERGE) {
248 if (uci_load(ctx, name, &package) != UCI_OK)
253 ret = uci_import(ctx, input, name, &package, (name != NULL));
256 ret = uci_save(ctx, package);
258 struct uci_element *e;
259 /* loop through all config sections and overwrite existing data */
260 uci_foreach_element(&ctx->root, e) {
261 struct uci_package *p = uci_to_package(e);
262 ret = uci_commit(ctx, &p, true);
275 static int uci_do_package_cmd(int cmd, int argc, char **argv)
277 char **configs = NULL;
284 return package_cmd(cmd, argv[1]);
286 if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
291 for (p = configs; *p; p++) {
292 package_cmd(cmd, *p);
298 static int uci_do_add(int argc, char **argv)
300 struct uci_package *p = NULL;
301 struct uci_section *s = NULL;
307 ret = uci_load(ctx, argv[1], &p);
311 ret = uci_add_section(ctx, p, argv[2], &s);
315 ret = uci_save(ctx, p);
321 fprintf(stdout, "%s\n", s->e.name);
326 static int uci_do_section_cmd(int cmd, int argc, char **argv)
328 struct uci_package *p = NULL;
329 struct uci_section *s = NULL;
330 struct uci_element *e = NULL;
331 struct uci_option *o = NULL;
332 char *package = NULL;
333 char *section = NULL;
342 value = strchr(argv[1], '=');
346 if (!uci_validate_text(value))
350 str = strdup(argv[1]);
354 if (value && (cmd != CMD_SET) && (cmd != CMD_ADD_LIST) && (cmd != CMD_RENAME))
357 if (uci_parse_tuple(ctx, str, &package, §ion, &option, NULL) != UCI_OK) {
361 sprintf(argv[1], "%s.%s", package, section);
364 if (uci_lookup_ext(ctx, &e, argv[1]) != UCI_OK) {
370 case UCI_TYPE_PACKAGE:
371 p = uci_to_package(e);
373 case UCI_TYPE_SECTION:
374 s = uci_to_section(e);
376 case UCI_TYPE_OPTION:
378 s = uci_to_option(e)->section;
391 case UCI_TYPE_SECTION:
392 printf("%s\n", s->type);
394 case UCI_TYPE_OPTION:
395 o = uci_to_option(e);
401 /* throw the value to stdout */
404 ret = uci_rename(ctx, p, section, option, value);
407 ret = uci_revert(ctx, &p, section, option);
410 ret = uci_set(ctx, p, section, option, value, NULL);
413 ret = uci_add_list(ctx, p, section, option, value, NULL);
416 ret = uci_delete(ctx, p, section, option);
420 /* no save necessary for get */
421 if ((cmd == CMD_GET) || (cmd == CMD_REVERT))
424 /* save changes, but don't commit them yet */
426 ret = uci_save(ctx, p);
436 static int uci_batch_cmd(void)
438 char *argv[MAX_ARGS];
443 for(i = 0; i <= MAX_ARGS; i++) {
445 fprintf(stderr, "Too many arguments\n");
449 if ((ret = uci_parse_argument(ctx, input, &str, &argv[i])) != UCI_OK) {
456 argv[i] = strdup(argv[i]);
465 if (!strcasecmp(argv[0], "exit"))
467 ret = uci_cmd(i, argv);
471 for (j = 0; j < i; j++) {
479 static int uci_batch(void)
483 while (!feof(input)) {
484 struct uci_element *e, *tmp;
486 ret = uci_batch_cmd();
490 fprintf(stderr, "Unknown command\n");
493 uci_foreach_element_safe(&ctx->root, tmp, e) {
494 uci_unload(ctx, uci_to_package(e));
500 static int uci_cmd(int argc, char **argv)
504 if (!strcasecmp(argv[0], "batch") && !(flags & CLI_FLAG_BATCH))
506 else if (!strcasecmp(argv[0], "show"))
508 else if (!strcasecmp(argv[0], "changes"))
510 else if (!strcasecmp(argv[0], "export"))
512 else if (!strcasecmp(argv[0], "commit"))
514 else if (!strcasecmp(argv[0], "get"))
516 else if (!strcasecmp(argv[0], "set"))
518 else if (!strcasecmp(argv[0], "ren") ||
519 !strcasecmp(argv[0], "rename"))
521 else if (!strcasecmp(argv[0], "revert"))
523 else if (!strcasecmp(argv[0], "del"))
525 else if (!strcasecmp(argv[0], "import"))
527 else if (!strcasecmp(argv[0], "help"))
529 else if (!strcasecmp(argv[0], "add"))
531 else if (!strcasecmp(argv[0], "add_list"))
543 return uci_do_section_cmd(cmd, argc, argv);
548 return uci_do_package_cmd(cmd, argc, argv);
550 return uci_do_import(argc, argv);
552 return uci_do_add(argc, argv);
561 int main(int argc, char **argv)
568 ctx = uci_alloc_context();
570 fprintf(stderr, "Out of memory\n");
574 while((c = getopt(argc, argv, "c:d:f:mnNp:P:sSq")) != -1) {
577 uci_set_confdir(ctx, optarg);
583 input = fopen(optarg, "r");
590 flags |= CLI_FLAG_MERGE;
593 ctx->flags |= UCI_FLAG_STRICT;
596 ctx->flags &= ~UCI_FLAG_STRICT;
597 ctx->flags |= UCI_FLAG_PERROR;
600 ctx->flags |= UCI_FLAG_EXPORT_NAME;
603 ctx->flags &= ~UCI_FLAG_EXPORT_NAME;
606 uci_add_history_path(ctx, optarg);
609 uci_add_history_path(ctx, ctx->savedir);
610 uci_set_savedir(ctx, optarg);
611 flags |= CLI_FLAG_NOCOMMIT;
614 flags |= CLI_FLAG_QUIET;
622 argv[optind - 1] = argv[0];
630 ret = uci_cmd(argc - 1, argv + 1);
638 uci_free_context(ctx);