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.
19 #define MAX_ARGS 4 /* max command line arguments for batch mode */
21 static const char *appname;
23 CLI_FLAG_MERGE = (1 << 0),
24 CLI_FLAG_QUIET = (1 << 1),
25 CLI_FLAG_NOCOMMIT = (1 << 2),
26 CLI_FLAG_BATCH = (1 << 3),
31 static struct uci_context *ctx;
48 static int uci_cmd(int argc, char **argv);
50 static void uci_usage(void)
53 "Usage: %s [<options>] <command> [<arguments>]\n\n"
56 "\texport [<config>]\n"
57 "\timport [<config>]\n"
58 "\tshow [<config>[.<section>[.<option>]]]\n"
59 "\tget <config>.<section>[.<option>]\n"
60 "\tset <config>.<section>[.<option>]=<value>\n"
61 "\trename <config>.<section>[.<option>]=<name>\n"
62 "\trevert <config>[.<section>[.<option>]]\n"
65 "\t-f <file> use <file> as input instead of stdin\n"
66 "\t-m when importing, merge data into an existing package\n"
67 "\t-n name unnamed sections on export (default)\n"
68 "\t-N don't name unnamed sections\n"
69 "\t-p <path> add a search path for config change files\n"
70 "\t-P <path> add a search path for config change files and use as default\n"
71 "\t-q quiet mode (don't print error messages)\n"
72 "\t-s force strict mode (stop on parser errors, default)\n"
73 "\t-S disable strict mode\n"
79 static void cli_perror(void)
81 if (flags & CLI_FLAG_QUIET)
84 uci_perror(ctx, appname);
87 static void uci_show_section(struct uci_section *p)
89 struct uci_element *e;
90 const char *cname, *sname;
92 cname = p->package->e.name;
94 printf("%s.%s=%s\n", cname, sname, p->type);
95 uci_foreach_element(&p->options, e) {
96 printf("%s.%s.%s=%s\n", cname, sname, e->name, uci_to_option(e)->value);
100 static void uci_show_package(struct uci_package *p)
102 struct uci_element *e;
104 uci_foreach_element( &p->sections, e) {
105 uci_show_section(uci_to_section(e));
110 static int package_cmd(int cmd, char *package)
112 struct uci_package *p = NULL;
114 if (uci_load(ctx, package, &p) != UCI_OK) {
122 if (flags & CLI_FLAG_NOCOMMIT)
124 if (uci_commit(ctx, &p, false) != UCI_OK)
128 uci_export(ctx, stdout, p, true);
139 static int uci_do_import(int argc, char **argv)
141 struct uci_package *package = NULL;
150 else if (flags & CLI_FLAG_MERGE)
151 /* need a package to merge */
154 if (flags & CLI_FLAG_MERGE) {
155 if (uci_load(ctx, name, &package) != UCI_OK)
158 ret = uci_import(ctx, input, name, &package, (name != NULL));
160 if (flags & CLI_FLAG_MERGE) {
161 ret = uci_save(ctx, package);
163 struct uci_element *e;
164 /* loop through all config sections and overwrite existing data */
165 uci_foreach_element(&ctx->root, e) {
166 struct uci_package *p = uci_to_package(e);
167 ret = uci_commit(ctx, &p, true);
180 static int uci_do_package_cmd(int cmd, int argc, char **argv)
182 char **configs = NULL;
189 return package_cmd(cmd, argv[1]);
191 if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
196 for (p = configs; *p; p++) {
197 package_cmd(cmd, *p);
204 static int uci_do_section_cmd(int cmd, int argc, char **argv)
206 struct uci_package *p = NULL;
207 struct uci_element *e = NULL;
208 char *package = NULL;
209 char *section = NULL;
226 if (uci_parse_tuple(ctx, argv[1], &package, §ion, &option, ptr) != UCI_OK)
229 if (uci_load(ctx, package, &p) != UCI_OK) {
238 if (uci_lookup(ctx, &e, p, section, option) != UCI_OK)
242 case UCI_TYPE_SECTION:
243 value = uci_to_section(e)->type;
245 case UCI_TYPE_OPTION:
246 value = uci_to_option(e)->value;
249 /* should not happen */
252 /* throw the value to stdout */
253 printf("%s\n", value);
256 ret = uci_rename(ctx, p, section, option, value);
259 ret = uci_revert(ctx, &p, section, option);
262 ret = uci_set(ctx, p, section, option, value);
265 ret = uci_delete(ctx, p, section, option);
269 /* no save necessary for get */
270 if ((cmd == CMD_GET) || (cmd == CMD_REVERT))
273 /* save changes, but don't commit them yet */
275 ret = uci_save(ctx, p);
285 static int uci_batch_cmd(void)
287 char *argv[MAX_ARGS];
292 for(i = 0; i <= MAX_ARGS; i++) {
294 fprintf(stderr, "Too many arguments\n");
298 if ((ret = uci_parse_argument(ctx, input, &str, &argv[i])) != UCI_OK) {
305 argv[i] = strdup(argv[i]);
314 if (!strcasecmp(argv[0], "exit"))
316 ret = uci_cmd(i, argv);
320 for (j = 0; j < i; j++) {
328 static int uci_batch(void)
332 while (!feof(input)) {
333 struct uci_element *e, *tmp;
335 ret = uci_batch_cmd();
339 fprintf(stderr, "Unknown command\n");
343 uci_foreach_element_safe(&ctx->root, tmp, e) {
344 uci_unload(ctx, uci_to_package(e));
350 static int uci_cmd(int argc, char **argv)
354 if (!strcasecmp(argv[0], "batch") && !(flags & CLI_FLAG_BATCH))
356 else if (!strcasecmp(argv[0], "show"))
358 else if (!strcasecmp(argv[0], "export"))
360 else if (!strcasecmp(argv[0], "commit"))
362 else if (!strcasecmp(argv[0], "get"))
364 else if (!strcasecmp(argv[0], "set"))
366 else if (!strcasecmp(argv[0], "ren") ||
367 !strcasecmp(argv[0], "rename"))
369 else if (!strcasecmp(argv[0], "revert"))
371 else if (!strcasecmp(argv[0], "del"))
373 else if (!strcasecmp(argv[0], "import"))
375 else if (!strcasecmp(argv[0], "help"))
386 return uci_do_section_cmd(cmd, argc, argv);
390 return uci_do_package_cmd(cmd, argc, argv);
392 return uci_do_import(argc, argv);
401 int main(int argc, char **argv)
408 ctx = uci_alloc_context();
410 fprintf(stderr, "Out of memory\n");
414 while((c = getopt(argc, argv, "f:mnNp:P:sSq")) != -1) {
417 input = fopen(optarg, "r");
424 flags |= CLI_FLAG_MERGE;
427 ctx->flags |= UCI_FLAG_STRICT;
430 ctx->flags &= ~UCI_FLAG_STRICT;
431 ctx->flags |= UCI_FLAG_PERROR;
434 ctx->flags |= UCI_FLAG_EXPORT_NAME;
437 ctx->flags &= ~UCI_FLAG_EXPORT_NAME;
440 uci_add_history_path(ctx, optarg);
443 uci_add_history_path(ctx, ctx->savedir);
444 uci_set_savedir(ctx, optarg);
445 flags |= CLI_FLAG_NOCOMMIT;
448 flags |= CLI_FLAG_QUIET;
456 argv[optind - 1] = argv[0];
464 ret = uci_cmd(argc - 1, argv + 1);
472 uci_free_context(ctx);