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),
29 CLI_FLAG_SHOW_EXT = (1 << 4),
34 static struct uci_context *ctx;
55 struct uci_type_list {
58 struct uci_type_list *next;
61 static struct uci_type_list *type_list = NULL;
62 static char *typestr = NULL;
63 static const char *cur_section_ref = NULL;
65 static int uci_cmd(int argc, char **argv);
68 uci_reset_typelist(void)
70 struct uci_type_list *type;
71 while (type_list != NULL) {
73 type_list = type_list->next;
80 cur_section_ref = NULL;
84 uci_lookup_section_ref(struct uci_section *s)
86 struct uci_type_list *ti = type_list;
89 if (!s->anonymous || !(flags & CLI_FLAG_SHOW_EXT))
92 /* look up in section type list */
94 if (strcmp(ti->name, s->type) == 0)
99 ti = malloc(sizeof(struct uci_type_list));
100 memset(ti, 0, sizeof(struct uci_type_list));
101 ti->next = type_list;
106 maxlen = strlen(s->type) + 1 + 2 + 10;
108 typestr = malloc(maxlen);
110 typestr = realloc(typestr, maxlen);
112 sprintf(typestr, "@%s[%d]", ti->name, ti->idx);
117 static void uci_usage(void)
120 "Usage: %s [<options>] <command> [<arguments>]\n\n"
123 "\texport [<config>]\n"
124 "\timport [<config>]\n"
125 "\tchanges [<config>]\n"
126 "\tcommit [<config>]\n"
127 "\tadd <config> <section-type>\n"
128 "\tadd_list <config>.<section>.<option>=<string>\n"
129 "\tshow [<config>[.<section>[.<option>]]]\n"
130 "\tget <config>.<section>[.<option>]\n"
131 "\tset <config>.<section>[.<option>]=<value>\n"
132 "\tdelete <config>[.<section[.<option>]]\n"
133 "\trename <config>.<section>[.<option>]=<name>\n"
134 "\trevert <config>[.<section>[.<option>]]\n"
135 "\treorder <config>.<section>=<position>\n"
138 "\t-c <path> set the search path for config files (default: /etc/config)\n"
139 "\t-d <str> set the delimiter for list values in uci show\n"
140 "\t-f <file> use <file> as input instead of stdin\n"
141 "\t-m when importing, merge data into an existing package\n"
142 "\t-n name unnamed sections on export (default)\n"
143 "\t-N don't name unnamed sections\n"
144 "\t-p <path> add a search path for config change files\n"
145 "\t-P <path> add a search path for config change files and use as default\n"
146 "\t-q quiet mode (don't print error messages)\n"
147 "\t-s force strict mode (stop on parser errors, default)\n"
148 "\t-S disable strict mode\n"
149 "\t-X do not use extended syntax on 'show'\n"
155 static void cli_perror(void)
157 if (flags & CLI_FLAG_QUIET)
160 uci_perror(ctx, appname);
163 static void uci_show_value(struct uci_option *o)
165 struct uci_element *e;
169 case UCI_TYPE_STRING:
170 printf("%s\n", o->v.string);
173 uci_foreach_element(&o->v.list, e) {
174 printf("%s%s", (sep ? delimiter : ""), e->name);
180 printf("<unknown>\n");
185 static void uci_show_option(struct uci_option *o)
188 o->section->package->e.name,
189 (cur_section_ref ? cur_section_ref : o->section->e.name),
194 static void uci_show_section(struct uci_section *s)
196 struct uci_element *e;
200 cname = s->package->e.name;
201 sname = (cur_section_ref ? cur_section_ref : s->e.name);
202 printf("%s.%s=%s\n", cname, sname, s->type);
203 uci_foreach_element(&s->options, e) {
204 uci_show_option(uci_to_option(e));
208 static void uci_show_package(struct uci_package *p)
210 struct uci_element *e;
212 uci_reset_typelist();
213 uci_foreach_element( &p->sections, e) {
214 struct uci_section *s = uci_to_section(e);
215 cur_section_ref = uci_lookup_section_ref(s);
218 uci_reset_typelist();
221 static void uci_show_changes(struct uci_package *p)
223 struct uci_element *e;
225 uci_foreach_element(&p->saved_history, e) {
226 struct uci_history *h = uci_to_history(e);
234 case UCI_CMD_LIST_ADD:
240 printf("%s%s.%s", prefix, p->e.name, h->section);
242 printf(".%s", e->name);
243 if (h->cmd != UCI_CMD_REMOVE)
244 printf("%s%s", op, h->value);
249 static int package_cmd(int cmd, char *tuple)
251 struct uci_element *e = NULL;
254 if (uci_lookup_ptr(ctx, &ptr, tuple, true) != UCI_OK) {
262 uci_show_changes(ptr.p);
265 if (flags & CLI_FLAG_NOCOMMIT)
267 if (uci_commit(ctx, &ptr.p, false) != UCI_OK)
271 uci_export(ctx, stdout, ptr.p, true);
274 if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
275 ctx->err = UCI_ERR_NOTFOUND;
280 case UCI_TYPE_PACKAGE:
281 uci_show_package(ptr.p);
283 case UCI_TYPE_SECTION:
284 uci_show_section(ptr.s);
286 case UCI_TYPE_OPTION:
287 uci_show_option(ptr.o);
290 /* should not happen */
296 uci_unload(ctx, ptr.p);
300 static int uci_do_import(int argc, char **argv)
302 struct uci_package *package = NULL;
312 else if (flags & CLI_FLAG_MERGE)
313 /* need a package to merge */
316 if (flags & CLI_FLAG_MERGE) {
317 if (uci_load(ctx, name, &package) != UCI_OK)
322 ret = uci_import(ctx, input, name, &package, (name != NULL));
325 ret = uci_save(ctx, package);
327 struct uci_element *e;
328 /* loop through all config sections and overwrite existing data */
329 uci_foreach_element(&ctx->root, e) {
330 struct uci_package *p = uci_to_package(e);
331 ret = uci_commit(ctx, &p, true);
344 static int uci_do_package_cmd(int cmd, int argc, char **argv)
346 char **configs = NULL;
353 return package_cmd(cmd, argv[1]);
355 if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
360 for (p = configs; *p; p++) {
361 package_cmd(cmd, *p);
367 static int uci_do_add(int argc, char **argv)
369 struct uci_package *p = NULL;
370 struct uci_section *s = NULL;
376 ret = uci_load(ctx, argv[1], &p);
380 ret = uci_add_section(ctx, p, argv[2], &s);
384 ret = uci_save(ctx, p);
390 fprintf(stdout, "%s\n", s->e.name);
395 static int uci_do_section_cmd(int cmd, int argc, char **argv)
397 struct uci_element *e;
404 if (uci_lookup_ptr(ctx, &ptr, argv[1], true) != UCI_OK) {
409 if (ptr.value && (cmd != CMD_SET) && (cmd != CMD_ADD_LIST) && (cmd != CMD_RENAME) && (cmd != CMD_REORDER))
415 if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
416 ctx->err = UCI_ERR_NOTFOUND;
421 case UCI_TYPE_SECTION:
422 printf("%s\n", ptr.s->type);
424 case UCI_TYPE_OPTION:
425 uci_show_value(ptr.o);
430 /* throw the value to stdout */
433 ret = uci_rename(ctx, &ptr);
436 ret = uci_revert(ctx, &ptr);
439 ret = uci_set(ctx, &ptr);
442 ret = uci_add_list(ctx, &ptr);
446 ctx->err = UCI_ERR_NOTFOUND;
450 ret = uci_reorder_section(ctx, ptr.s, strtoul(ptr.value, NULL, 10));
453 ret = uci_delete(ctx, &ptr);
457 /* no save necessary for get */
458 if ((cmd == CMD_GET) || (cmd == CMD_REVERT))
461 /* save changes, but don't commit them yet */
463 ret = uci_save(ctx, ptr.p);
473 static int uci_batch_cmd(void)
475 char *argv[MAX_ARGS + 2];
480 for(i = 0; i <= MAX_ARGS; i++) {
482 fprintf(stderr, "Too many arguments\n");
486 if ((ret = uci_parse_argument(ctx, input, &str, &argv[i])) != UCI_OK) {
493 argv[i] = strdup(argv[i]);
502 if (!strcasecmp(argv[0], "exit"))
504 ret = uci_cmd(i, argv);
508 for (j = 0; j < i; j++) {
516 static int uci_batch(void)
520 flags |= CLI_FLAG_BATCH;
521 while (!feof(input)) {
522 struct uci_element *e, *tmp;
524 ret = uci_batch_cmd();
528 fprintf(stderr, "Unknown command\n");
531 uci_foreach_element_safe(&ctx->root, tmp, e) {
532 uci_unload(ctx, uci_to_package(e));
535 flags &= ~CLI_FLAG_BATCH;
540 static int uci_cmd(int argc, char **argv)
544 if (!strcasecmp(argv[0], "batch") && !(flags & CLI_FLAG_BATCH))
546 else if (!strcasecmp(argv[0], "show"))
548 else if (!strcasecmp(argv[0], "changes"))
550 else if (!strcasecmp(argv[0], "export"))
552 else if (!strcasecmp(argv[0], "commit"))
554 else if (!strcasecmp(argv[0], "get"))
556 else if (!strcasecmp(argv[0], "set"))
558 else if (!strcasecmp(argv[0], "ren") ||
559 !strcasecmp(argv[0], "rename"))
561 else if (!strcasecmp(argv[0], "revert"))
563 else if (!strcasecmp(argv[0], "reorder"))
565 else if (!strcasecmp(argv[0], "del") ||
566 !strcasecmp(argv[0], "delete"))
568 else if (!strcasecmp(argv[0], "import"))
570 else if (!strcasecmp(argv[0], "help"))
572 else if (!strcasecmp(argv[0], "add"))
574 else if (!strcasecmp(argv[0], "add_list"))
587 return uci_do_section_cmd(cmd, argc, argv);
592 return uci_do_package_cmd(cmd, argc, argv);
594 return uci_do_import(argc, argv);
596 return uci_do_add(argc, argv);
605 int main(int argc, char **argv)
610 flags = CLI_FLAG_SHOW_EXT;
613 ctx = uci_alloc_context();
615 fprintf(stderr, "Out of memory\n");
619 while((c = getopt(argc, argv, "c:d:f:mnNp:P:sSqX")) != -1) {
622 uci_set_confdir(ctx, optarg);
628 input = fopen(optarg, "r");
635 flags |= CLI_FLAG_MERGE;
638 ctx->flags |= UCI_FLAG_STRICT;
641 ctx->flags &= ~UCI_FLAG_STRICT;
642 ctx->flags |= UCI_FLAG_PERROR;
645 ctx->flags |= UCI_FLAG_EXPORT_NAME;
648 ctx->flags &= ~UCI_FLAG_EXPORT_NAME;
651 uci_add_history_path(ctx, optarg);
654 uci_add_history_path(ctx, ctx->savedir);
655 uci_set_savedir(ctx, optarg);
656 flags |= CLI_FLAG_NOCOMMIT;
659 flags |= CLI_FLAG_QUIET;
662 flags &= ~CLI_FLAG_SHOW_EXT;
670 argv[optind - 1] = argv[0];
678 ret = uci_cmd(argc - 1, argv + 1);
686 uci_free_context(ctx);