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 "\tshow [<config>[.<section>[.<option>]]]\n"
61 "\tget <config>.<section>[.<option>]\n"
62 "\tset <config>.<section>[.<option>]=<value>\n"
63 "\trename <config>.<section>[.<option>]=<name>\n"
64 "\trevert <config>[.<section>[.<option>]]\n"
67 "\t-f <file> use <file> as input instead of stdin\n"
68 "\t-m when importing, merge data into an existing package\n"
69 "\t-n name unnamed sections on export (default)\n"
70 "\t-N don't name unnamed sections\n"
71 "\t-p <path> add a search path for config change files\n"
72 "\t-P <path> add a search path for config change files and use as default\n"
73 "\t-q quiet mode (don't print error messages)\n"
74 "\t-s force strict mode (stop on parser errors, default)\n"
75 "\t-S disable strict mode\n"
81 static void cli_perror(void)
83 if (flags & CLI_FLAG_QUIET)
86 uci_perror(ctx, appname);
89 static void uci_show_section(struct uci_section *p)
91 struct uci_element *e;
92 const char *cname, *sname;
94 cname = p->package->e.name;
96 printf("%s.%s=%s\n", cname, sname, p->type);
97 uci_foreach_element(&p->options, e) {
98 printf("%s.%s.%s=%s\n", cname, sname, e->name, uci_to_option(e)->value);
102 static void uci_show_package(struct uci_package *p)
104 struct uci_element *e;
106 uci_foreach_element( &p->sections, e) {
107 uci_show_section(uci_to_section(e));
111 static void uci_show_changes(struct uci_package *p)
113 struct uci_element *e;
115 uci_foreach_element(&p->saved_history, e) {
116 struct uci_history *h = uci_to_history(e);
118 if (h->cmd == UCI_CMD_REMOVE)
120 printf("%s.%s", p->e.name, h->section);
122 printf(".%s", e->name);
123 if (h->cmd != UCI_CMD_REMOVE)
124 printf("=%s", h->value);
129 static int package_cmd(int cmd, char *package)
131 struct uci_package *p = NULL;
134 if (cmd == CMD_CHANGES)
135 ctx->flags |= UCI_FLAG_SAVED_HISTORY;
136 ret = uci_load(ctx, package, &p);
137 if (cmd == CMD_CHANGES)
138 ctx->flags &= ~UCI_FLAG_SAVED_HISTORY;
151 if (flags & CLI_FLAG_NOCOMMIT)
153 if (uci_commit(ctx, &p, false) != UCI_OK)
157 uci_export(ctx, stdout, p, true);
168 static int uci_do_import(int argc, char **argv)
170 struct uci_package *package = NULL;
179 else if (flags & CLI_FLAG_MERGE)
180 /* need a package to merge */
183 if (flags & CLI_FLAG_MERGE) {
184 if (uci_load(ctx, name, &package) != UCI_OK)
187 ret = uci_import(ctx, input, name, &package, (name != NULL));
189 if (flags & CLI_FLAG_MERGE) {
190 ret = uci_save(ctx, package);
192 struct uci_element *e;
193 /* loop through all config sections and overwrite existing data */
194 uci_foreach_element(&ctx->root, e) {
195 struct uci_package *p = uci_to_package(e);
196 ret = uci_commit(ctx, &p, true);
209 static int uci_do_package_cmd(int cmd, int argc, char **argv)
211 char **configs = NULL;
218 return package_cmd(cmd, argv[1]);
220 if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
225 for (p = configs; *p; p++) {
226 package_cmd(cmd, *p);
233 static int uci_do_section_cmd(int cmd, int argc, char **argv)
235 struct uci_package *p = NULL;
236 struct uci_element *e = NULL;
237 char *package = NULL;
238 char *section = NULL;
255 if (uci_parse_tuple(ctx, argv[1], &package, §ion, &option, ptr) != UCI_OK)
258 if (uci_load(ctx, package, &p) != UCI_OK) {
267 if (uci_lookup(ctx, &e, p, section, option) != UCI_OK)
271 case UCI_TYPE_SECTION:
272 value = uci_to_section(e)->type;
274 case UCI_TYPE_OPTION:
275 value = uci_to_option(e)->value;
278 /* should not happen */
281 /* throw the value to stdout */
282 printf("%s\n", value);
285 ret = uci_rename(ctx, p, section, option, value);
288 ret = uci_revert(ctx, &p, section, option);
291 ret = uci_set(ctx, p, section, option, value);
294 ret = uci_delete(ctx, p, section, option);
298 /* no save necessary for get */
299 if ((cmd == CMD_GET) || (cmd == CMD_REVERT))
302 /* save changes, but don't commit them yet */
304 ret = uci_save(ctx, p);
314 static int uci_batch_cmd(void)
316 char *argv[MAX_ARGS];
321 for(i = 0; i <= MAX_ARGS; i++) {
323 fprintf(stderr, "Too many arguments\n");
327 if ((ret = uci_parse_argument(ctx, input, &str, &argv[i])) != UCI_OK) {
334 argv[i] = strdup(argv[i]);
343 if (!strcasecmp(argv[0], "exit"))
345 ret = uci_cmd(i, argv);
349 for (j = 0; j < i; j++) {
357 static int uci_batch(void)
361 while (!feof(input)) {
362 struct uci_element *e, *tmp;
364 ret = uci_batch_cmd();
368 fprintf(stderr, "Unknown command\n");
372 uci_foreach_element_safe(&ctx->root, tmp, e) {
373 uci_unload(ctx, uci_to_package(e));
379 static int uci_cmd(int argc, char **argv)
383 if (!strcasecmp(argv[0], "batch") && !(flags & CLI_FLAG_BATCH))
385 else if (!strcasecmp(argv[0], "show"))
387 else if (!strcasecmp(argv[0], "changes"))
389 else if (!strcasecmp(argv[0], "export"))
391 else if (!strcasecmp(argv[0], "commit"))
393 else if (!strcasecmp(argv[0], "get"))
395 else if (!strcasecmp(argv[0], "set"))
397 else if (!strcasecmp(argv[0], "ren") ||
398 !strcasecmp(argv[0], "rename"))
400 else if (!strcasecmp(argv[0], "revert"))
402 else if (!strcasecmp(argv[0], "del"))
404 else if (!strcasecmp(argv[0], "import"))
406 else if (!strcasecmp(argv[0], "help"))
417 return uci_do_section_cmd(cmd, argc, argv);
422 return uci_do_package_cmd(cmd, argc, argv);
424 return uci_do_import(argc, argv);
433 int main(int argc, char **argv)
440 ctx = uci_alloc_context();
442 fprintf(stderr, "Out of memory\n");
446 while((c = getopt(argc, argv, "f:mnNp:P:sSq")) != -1) {
449 input = fopen(optarg, "r");
456 flags |= CLI_FLAG_MERGE;
459 ctx->flags |= UCI_FLAG_STRICT;
462 ctx->flags &= ~UCI_FLAG_STRICT;
463 ctx->flags |= UCI_FLAG_PERROR;
466 ctx->flags |= UCI_FLAG_EXPORT_NAME;
469 ctx->flags &= ~UCI_FLAG_EXPORT_NAME;
472 uci_add_history_path(ctx, optarg);
475 uci_add_history_path(ctx, ctx->savedir);
476 uci_set_savedir(ctx, optarg);
477 flags |= CLI_FLAG_NOCOMMIT;
480 flags |= CLI_FLAG_QUIET;
488 argv[optind - 1] = argv[0];
496 ret = uci_cmd(argc - 1, argv + 1);
504 uci_free_context(ctx);