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 *appname;
24 CLI_FLAG_MERGE = (1 << 0),
25 CLI_FLAG_QUIET = (1 << 1),
26 CLI_FLAG_NOCOMMIT = (1 << 2),
27 CLI_FLAG_BATCH = (1 << 3),
32 static struct uci_context *ctx;
50 static int uci_cmd(int argc, char **argv);
52 static void uci_usage(void)
55 "Usage: %s [<options>] <command> [<arguments>]\n\n"
58 "\texport [<config>]\n"
59 "\timport [<config>]\n"
60 "\tchanges [<config>]\n"
61 "\tshow [<config>[.<section>[.<option>]]]\n"
62 "\tget <config>.<section>[.<option>]\n"
63 "\tset <config>.<section>[.<option>]=<value>\n"
64 "\trename <config>.<section>[.<option>]=<name>\n"
65 "\trevert <config>[.<section>[.<option>]]\n"
68 "\t-f <file> use <file> as input instead of stdin\n"
69 "\t-m when importing, merge data into an existing package\n"
70 "\t-n name unnamed sections on export (default)\n"
71 "\t-N don't name unnamed sections\n"
72 "\t-p <path> add a search path for config change files\n"
73 "\t-P <path> add a search path for config change files and use as default\n"
74 "\t-q quiet mode (don't print error messages)\n"
75 "\t-s force strict mode (stop on parser errors, default)\n"
76 "\t-S disable strict mode\n"
82 static void cli_perror(void)
84 if (flags & CLI_FLAG_QUIET)
87 uci_perror(ctx, appname);
90 static void uci_show_section(struct uci_section *p)
92 struct uci_element *e;
93 const char *cname, *sname;
95 cname = p->package->e.name;
97 printf("%s.%s=%s\n", cname, sname, p->type);
98 uci_foreach_element(&p->options, e) {
99 printf("%s.%s.%s=%s\n", cname, sname, e->name, uci_to_option(e)->value);
103 static void uci_show_package(struct uci_package *p)
105 struct uci_element *e;
107 uci_foreach_element( &p->sections, e) {
108 uci_show_section(uci_to_section(e));
112 static void uci_show_changes(struct uci_package *p)
114 struct uci_element *e;
116 uci_foreach_element(&p->saved_history, e) {
117 struct uci_history *h = uci_to_history(e);
119 if (h->cmd == UCI_CMD_REMOVE)
121 printf("%s.%s", p->e.name, h->section);
123 printf(".%s", e->name);
124 if (h->cmd != UCI_CMD_REMOVE)
125 printf("=%s", h->value);
130 static int package_cmd(int cmd, char *package)
132 struct uci_package *p = NULL;
135 if (cmd == CMD_CHANGES)
136 ctx->flags |= UCI_FLAG_SAVED_HISTORY;
137 ret = uci_load(ctx, package, &p);
138 if (cmd == CMD_CHANGES)
139 ctx->flags &= ~UCI_FLAG_SAVED_HISTORY;
152 if (flags & CLI_FLAG_NOCOMMIT)
154 if (uci_commit(ctx, &p, false) != UCI_OK)
158 uci_export(ctx, stdout, p, true);
169 static int uci_do_import(int argc, char **argv)
171 struct uci_package *package = NULL;
180 else if (flags & CLI_FLAG_MERGE)
181 /* need a package to merge */
184 if (flags & CLI_FLAG_MERGE) {
185 if (uci_load(ctx, name, &package) != UCI_OK)
188 ret = uci_import(ctx, input, name, &package, (name != NULL));
190 if (flags & CLI_FLAG_MERGE) {
191 ret = uci_save(ctx, package);
193 struct uci_element *e;
194 /* loop through all config sections and overwrite existing data */
195 uci_foreach_element(&ctx->root, e) {
196 struct uci_package *p = uci_to_package(e);
197 ret = uci_commit(ctx, &p, true);
210 static int uci_do_package_cmd(int cmd, int argc, char **argv)
212 char **configs = NULL;
219 return package_cmd(cmd, argv[1]);
221 if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
226 for (p = configs; *p; p++) {
227 package_cmd(cmd, *p);
234 static int uci_do_section_cmd(int cmd, int argc, char **argv)
236 struct uci_package *p = NULL;
237 struct uci_element *e = NULL;
238 char *package = NULL;
239 char *section = NULL;
256 if (uci_parse_tuple(ctx, argv[1], &package, §ion, &option, ptr) != UCI_OK)
259 if (uci_load(ctx, package, &p) != UCI_OK) {
268 if (uci_lookup(ctx, &e, p, section, option) != UCI_OK)
272 case UCI_TYPE_SECTION:
273 value = uci_to_section(e)->type;
275 case UCI_TYPE_OPTION:
276 value = uci_to_option(e)->value;
279 /* should not happen */
282 /* throw the value to stdout */
283 printf("%s\n", value);
286 ret = uci_rename(ctx, p, section, option, value);
289 ret = uci_revert(ctx, &p, section, option);
292 ret = uci_set(ctx, p, section, option, value);
295 ret = uci_delete(ctx, p, section, option);
299 /* no save necessary for get */
300 if ((cmd == CMD_GET) || (cmd == CMD_REVERT))
303 /* save changes, but don't commit them yet */
305 ret = uci_save(ctx, p);
315 static int uci_batch_cmd(void)
317 char *argv[MAX_ARGS];
322 for(i = 0; i <= MAX_ARGS; i++) {
324 fprintf(stderr, "Too many arguments\n");
328 if ((ret = uci_parse_argument(ctx, input, &str, &argv[i])) != UCI_OK) {
335 argv[i] = strdup(argv[i]);
344 if (!strcasecmp(argv[0], "exit"))
346 ret = uci_cmd(i, argv);
350 for (j = 0; j < i; j++) {
358 static int uci_batch(void)
362 while (!feof(input)) {
363 struct uci_element *e, *tmp;
365 ret = uci_batch_cmd();
369 fprintf(stderr, "Unknown command\n");
373 uci_foreach_element_safe(&ctx->root, tmp, e) {
374 uci_unload(ctx, uci_to_package(e));
380 static int uci_cmd(int argc, char **argv)
384 if (!strcasecmp(argv[0], "batch") && !(flags & CLI_FLAG_BATCH))
386 else if (!strcasecmp(argv[0], "show"))
388 else if (!strcasecmp(argv[0], "changes"))
390 else if (!strcasecmp(argv[0], "export"))
392 else if (!strcasecmp(argv[0], "commit"))
394 else if (!strcasecmp(argv[0], "get"))
396 else if (!strcasecmp(argv[0], "set"))
398 else if (!strcasecmp(argv[0], "ren") ||
399 !strcasecmp(argv[0], "rename"))
401 else if (!strcasecmp(argv[0], "revert"))
403 else if (!strcasecmp(argv[0], "del"))
405 else if (!strcasecmp(argv[0], "import"))
407 else if (!strcasecmp(argv[0], "help"))
418 return uci_do_section_cmd(cmd, argc, argv);
423 return uci_do_package_cmd(cmd, argc, argv);
425 return uci_do_import(argc, argv);
434 int main(int argc, char **argv)
441 ctx = uci_alloc_context();
443 fprintf(stderr, "Out of memory\n");
447 while((c = getopt(argc, argv, "f:mnNp:P:sSq")) != -1) {
450 input = fopen(optarg, "r");
457 flags |= CLI_FLAG_MERGE;
460 ctx->flags |= UCI_FLAG_STRICT;
463 ctx->flags &= ~UCI_FLAG_STRICT;
464 ctx->flags |= UCI_FLAG_PERROR;
467 ctx->flags |= UCI_FLAG_EXPORT_NAME;
470 ctx->flags &= ~UCI_FLAG_EXPORT_NAME;
473 uci_add_history_path(ctx, optarg);
476 uci_add_history_path(ctx, ctx->savedir);
477 uci_set_savedir(ctx, optarg);
478 flags |= CLI_FLAG_NOCOMMIT;
481 flags |= CLI_FLAG_QUIET;
489 argv[optind - 1] = argv[0];
497 ret = uci_cmd(argc - 1, argv + 1);
505 uci_free_context(ctx);