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;
51 static int uci_cmd(int argc, char **argv);
53 static void uci_usage(void)
56 "Usage: %s [<options>] <command> [<arguments>]\n\n"
59 "\texport [<config>]\n"
60 "\timport [<config>]\n"
61 "\tchanges [<config>]\n"
62 "\tcommit [<config>]\n"
63 "\tadd <config> <section-type>\n"
64 "\tshow [<config>[.<section>[.<option>]]]\n"
65 "\tget <config>.<section>[.<option>]\n"
66 "\tset <config>.<section>[.<option>]=<value>\n"
67 "\trename <config>.<section>[.<option>]=<name>\n"
68 "\trevert <config>[.<section>[.<option>]]\n"
71 "\t-f <file> use <file> as input instead of stdin\n"
72 "\t-m when importing, merge data into an existing package\n"
73 "\t-n name unnamed sections on export (default)\n"
74 "\t-N don't name unnamed sections\n"
75 "\t-p <path> add a search path for config change files\n"
76 "\t-P <path> add a search path for config change files and use as default\n"
77 "\t-q quiet mode (don't print error messages)\n"
78 "\t-s force strict mode (stop on parser errors, default)\n"
79 "\t-S disable strict mode\n"
85 static void cli_perror(void)
87 if (flags & CLI_FLAG_QUIET)
90 uci_perror(ctx, appname);
93 static void uci_show_section(struct uci_section *p)
95 struct uci_element *e;
96 const char *cname, *sname;
98 cname = p->package->e.name;
100 printf("%s.%s=%s\n", cname, sname, p->type);
101 uci_foreach_element(&p->options, e) {
102 printf("%s.%s.%s=%s\n", cname, sname, e->name, uci_to_option(e)->value);
106 static void uci_show_package(struct uci_package *p)
108 struct uci_element *e;
110 uci_foreach_element( &p->sections, e) {
111 uci_show_section(uci_to_section(e));
115 static void uci_show_changes(struct uci_package *p)
117 struct uci_element *e;
119 uci_foreach_element(&p->saved_history, e) {
120 struct uci_history *h = uci_to_history(e);
122 if (h->cmd == UCI_CMD_REMOVE)
124 printf("%s.%s", p->e.name, h->section);
126 printf(".%s", e->name);
127 if (h->cmd != UCI_CMD_REMOVE)
128 printf("=%s", h->value);
133 static int package_cmd(int cmd, char *package)
135 struct uci_package *p = NULL;
138 ret = uci_load(ctx, package, &p);
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;
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)
190 ret = uci_import(ctx, input, name, &package, (name != NULL));
193 ret = uci_save(ctx, package);
195 struct uci_element *e;
196 /* loop through all config sections and overwrite existing data */
197 uci_foreach_element(&ctx->root, e) {
198 struct uci_package *p = uci_to_package(e);
199 ret = uci_commit(ctx, &p, true);
212 static int uci_do_package_cmd(int cmd, int argc, char **argv)
214 char **configs = NULL;
221 return package_cmd(cmd, argv[1]);
223 if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
228 for (p = configs; *p; p++) {
229 package_cmd(cmd, *p);
235 static int uci_do_add(int argc, char **argv)
237 struct uci_package *p = NULL;
238 struct uci_section *s = NULL;
244 ret = uci_load(ctx, argv[1], &p);
248 ret = uci_add_section(ctx, p, argv[2], &s);
252 ret = uci_save(ctx, p);
258 fprintf(stdout, "%s\n", s->e.name);
263 static int uci_do_section_cmd(int cmd, int argc, char **argv)
265 struct uci_package *p = NULL;
266 struct uci_element *e = NULL;
267 char *package = NULL;
268 char *section = NULL;
285 if (uci_parse_tuple(ctx, argv[1], &package, §ion, &option, ptr) != UCI_OK)
287 if (section && !section[0])
290 if (uci_load(ctx, package, &p) != UCI_OK) {
299 if (uci_lookup(ctx, &e, p, section, option) != UCI_OK)
303 case UCI_TYPE_SECTION:
304 value = uci_to_section(e)->type;
306 case UCI_TYPE_OPTION:
307 value = uci_to_option(e)->value;
310 /* should not happen */
313 /* throw the value to stdout */
314 printf("%s\n", value);
317 ret = uci_rename(ctx, p, section, option, value);
320 ret = uci_revert(ctx, &p, section, option);
323 ret = uci_set(ctx, p, section, option, value, NULL);
326 ret = uci_delete(ctx, p, section, option);
330 /* no save necessary for get */
331 if ((cmd == CMD_GET) || (cmd == CMD_REVERT))
334 /* save changes, but don't commit them yet */
336 ret = uci_save(ctx, p);
346 static int uci_batch_cmd(void)
348 char *argv[MAX_ARGS];
353 for(i = 0; i <= MAX_ARGS; i++) {
355 fprintf(stderr, "Too many arguments\n");
359 if ((ret = uci_parse_argument(ctx, input, &str, &argv[i])) != UCI_OK) {
366 argv[i] = strdup(argv[i]);
375 if (!strcasecmp(argv[0], "exit"))
377 ret = uci_cmd(i, argv);
381 for (j = 0; j < i; j++) {
389 static int uci_batch(void)
393 while (!feof(input)) {
394 struct uci_element *e, *tmp;
396 ret = uci_batch_cmd();
400 fprintf(stderr, "Unknown command\n");
403 uci_foreach_element_safe(&ctx->root, tmp, e) {
404 uci_unload(ctx, uci_to_package(e));
410 static int uci_cmd(int argc, char **argv)
414 if (!strcasecmp(argv[0], "batch") && !(flags & CLI_FLAG_BATCH))
416 else if (!strcasecmp(argv[0], "show"))
418 else if (!strcasecmp(argv[0], "changes"))
420 else if (!strcasecmp(argv[0], "export"))
422 else if (!strcasecmp(argv[0], "commit"))
424 else if (!strcasecmp(argv[0], "get"))
426 else if (!strcasecmp(argv[0], "set"))
428 else if (!strcasecmp(argv[0], "ren") ||
429 !strcasecmp(argv[0], "rename"))
431 else if (!strcasecmp(argv[0], "revert"))
433 else if (!strcasecmp(argv[0], "del"))
435 else if (!strcasecmp(argv[0], "import"))
437 else if (!strcasecmp(argv[0], "help"))
439 else if (!strcasecmp(argv[0], "add"))
450 return uci_do_section_cmd(cmd, argc, argv);
455 return uci_do_package_cmd(cmd, argc, argv);
457 return uci_do_import(argc, argv);
459 return uci_do_add(argc, argv);
468 int main(int argc, char **argv)
475 ctx = uci_alloc_context();
477 fprintf(stderr, "Out of memory\n");
481 while((c = getopt(argc, argv, "f:mnNp:P:sSq")) != -1) {
484 input = fopen(optarg, "r");
491 flags |= CLI_FLAG_MERGE;
494 ctx->flags |= UCI_FLAG_STRICT;
497 ctx->flags &= ~UCI_FLAG_STRICT;
498 ctx->flags |= UCI_FLAG_PERROR;
501 ctx->flags |= UCI_FLAG_EXPORT_NAME;
504 ctx->flags &= ~UCI_FLAG_EXPORT_NAME;
507 uci_add_history_path(ctx, optarg);
510 uci_add_history_path(ctx, ctx->savedir);
511 uci_set_savedir(ctx, optarg);
512 flags |= CLI_FLAG_NOCOMMIT;
515 flags |= CLI_FLAG_QUIET;
523 argv[optind - 1] = argv[0];
531 ret = uci_cmd(argc - 1, argv + 1);
539 uci_free_context(ctx);