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;
54 struct uci_type_list {
57 struct uci_type_list *next;
60 static struct uci_type_list *type_list = NULL;
61 static char *typestr = NULL;
62 static const char *cur_section_ref = NULL;
64 static int uci_cmd(int argc, char **argv);
67 uci_reset_typelist(void)
69 struct uci_type_list *type;
70 while (type_list != NULL) {
72 type_list = type_list->next;
79 cur_section_ref = NULL;
83 uci_lookup_section_ref(struct uci_section *s)
85 struct uci_type_list *ti = type_list;
88 if (!s->anonymous || !(flags & CLI_FLAG_SHOW_EXT))
91 /* look up in section type list */
93 if (strcmp(ti->name, s->type) == 0)
98 ti = malloc(sizeof(struct uci_type_list));
99 memset(ti, 0, sizeof(struct uci_type_list));
100 ti->next = type_list;
105 maxlen = strlen(s->type) + 1 + 2 + 10;
107 typestr = malloc(maxlen);
109 typestr = realloc(typestr, maxlen);
111 sprintf(typestr, "@%s[%d]", ti->name, ti->idx);
116 static void uci_usage(void)
119 "Usage: %s [<options>] <command> [<arguments>]\n\n"
122 "\texport [<config>]\n"
123 "\timport [<config>]\n"
124 "\tchanges [<config>]\n"
125 "\tcommit [<config>]\n"
126 "\tadd <config> <section-type>\n"
127 "\tadd_list <config>.<section>.<option>=<string>\n"
128 "\tshow [<config>[.<section>[.<option>]]]\n"
129 "\tget <config>.<section>[.<option>]\n"
130 "\tset <config>.<section>[.<option>]=<value>\n"
131 "\tdelete <config>[.<section[.<option>]]\n"
132 "\trename <config>.<section>[.<option>]=<name>\n"
133 "\trevert <config>[.<section>[.<option>]]\n"
136 "\t-c <path> set the search path for config files (default: /etc/config)\n"
137 "\t-d <str> set the delimiter for list values in uci show\n"
138 "\t-f <file> use <file> as input instead of stdin\n"
139 "\t-m when importing, merge data into an existing package\n"
140 "\t-n name unnamed sections on export (default)\n"
141 "\t-N don't name unnamed sections\n"
142 "\t-p <path> add a search path for config change files\n"
143 "\t-P <path> add a search path for config change files and use as default\n"
144 "\t-q quiet mode (don't print error messages)\n"
145 "\t-s force strict mode (stop on parser errors, default)\n"
146 "\t-S disable strict mode\n"
147 "\t-X do not use extended syntax on 'show'\n"
153 static void cli_perror(void)
155 if (flags & CLI_FLAG_QUIET)
158 uci_perror(ctx, appname);
161 static void uci_show_value(struct uci_option *o)
163 struct uci_element *e;
167 case UCI_TYPE_STRING:
168 printf("%s\n", o->v.string);
171 uci_foreach_element(&o->v.list, e) {
172 printf("%s%s", (sep ? delimiter : ""), e->name);
178 printf("<unknown>\n");
183 static void uci_show_option(struct uci_option *o)
186 o->section->package->e.name,
187 (cur_section_ref ? cur_section_ref : o->section->e.name),
192 static void uci_show_section(struct uci_section *s)
194 struct uci_element *e;
198 cname = s->package->e.name;
199 sname = (cur_section_ref ? cur_section_ref : s->e.name);
200 printf("%s.%s=%s\n", cname, sname, s->type);
201 uci_foreach_element(&s->options, e) {
202 uci_show_option(uci_to_option(e));
206 static void uci_show_package(struct uci_package *p)
208 struct uci_element *e;
210 uci_reset_typelist();
211 uci_foreach_element( &p->sections, e) {
212 struct uci_section *s = uci_to_section(e);
213 cur_section_ref = uci_lookup_section_ref(s);
216 uci_reset_typelist();
219 static void uci_show_changes(struct uci_package *p)
221 struct uci_element *e;
223 uci_foreach_element(&p->saved_history, e) {
224 struct uci_history *h = uci_to_history(e);
232 case UCI_CMD_LIST_ADD:
238 printf("%s%s.%s", prefix, p->e.name, h->section);
240 printf(".%s", e->name);
241 if (h->cmd != UCI_CMD_REMOVE)
242 printf("%s%s", op, h->value);
247 static int package_cmd(int cmd, char *tuple)
249 struct uci_element *e = NULL;
252 if (uci_lookup_ptr(ctx, &ptr, tuple, true) != UCI_OK) {
260 uci_show_changes(ptr.p);
263 if (flags & CLI_FLAG_NOCOMMIT)
265 if (uci_commit(ctx, &ptr.p, false) != UCI_OK)
269 uci_export(ctx, stdout, ptr.p, true);
272 if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
273 ctx->err = UCI_ERR_NOTFOUND;
278 case UCI_TYPE_PACKAGE:
279 uci_show_package(ptr.p);
281 case UCI_TYPE_SECTION:
282 uci_show_section(ptr.s);
284 case UCI_TYPE_OPTION:
285 uci_show_option(ptr.o);
288 /* should not happen */
294 uci_unload(ctx, ptr.p);
298 static int uci_do_import(int argc, char **argv)
300 struct uci_package *package = NULL;
310 else if (flags & CLI_FLAG_MERGE)
311 /* need a package to merge */
314 if (flags & CLI_FLAG_MERGE) {
315 if (uci_load(ctx, name, &package) != UCI_OK)
320 ret = uci_import(ctx, input, name, &package, (name != NULL));
323 ret = uci_save(ctx, package);
325 struct uci_element *e;
326 /* loop through all config sections and overwrite existing data */
327 uci_foreach_element(&ctx->root, e) {
328 struct uci_package *p = uci_to_package(e);
329 ret = uci_commit(ctx, &p, true);
342 static int uci_do_package_cmd(int cmd, int argc, char **argv)
344 char **configs = NULL;
351 return package_cmd(cmd, argv[1]);
353 if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
358 for (p = configs; *p; p++) {
359 package_cmd(cmd, *p);
365 static int uci_do_add(int argc, char **argv)
367 struct uci_package *p = NULL;
368 struct uci_section *s = NULL;
374 ret = uci_load(ctx, argv[1], &p);
378 ret = uci_add_section(ctx, p, argv[2], &s);
382 ret = uci_save(ctx, p);
388 fprintf(stdout, "%s\n", s->e.name);
393 static int uci_do_section_cmd(int cmd, int argc, char **argv)
395 struct uci_element *e;
402 if (uci_lookup_ptr(ctx, &ptr, argv[1], true) != UCI_OK) {
407 if (ptr.value && (cmd != CMD_SET) && (cmd != CMD_ADD_LIST) && (cmd != CMD_RENAME))
413 if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
414 ctx->err = UCI_ERR_NOTFOUND;
419 case UCI_TYPE_SECTION:
420 printf("%s\n", ptr.s->type);
422 case UCI_TYPE_OPTION:
423 uci_show_value(ptr.o);
428 /* throw the value to stdout */
431 ret = uci_rename(ctx, &ptr);
434 ret = uci_revert(ctx, &ptr);
437 ret = uci_set(ctx, &ptr);
440 ret = uci_add_list(ctx, &ptr);
443 ret = uci_delete(ctx, &ptr);
447 /* no save necessary for get */
448 if ((cmd == CMD_GET) || (cmd == CMD_REVERT))
451 /* save changes, but don't commit them yet */
453 ret = uci_save(ctx, ptr.p);
463 static int uci_batch_cmd(void)
465 char *argv[MAX_ARGS];
470 for(i = 0; i <= MAX_ARGS; i++) {
472 fprintf(stderr, "Too many arguments\n");
476 if ((ret = uci_parse_argument(ctx, input, &str, &argv[i])) != UCI_OK) {
483 argv[i] = strdup(argv[i]);
492 if (!strcasecmp(argv[0], "exit"))
494 ret = uci_cmd(i, argv);
498 for (j = 0; j < i; j++) {
506 static int uci_batch(void)
510 flags |= CLI_FLAG_BATCH;
511 while (!feof(input)) {
512 struct uci_element *e, *tmp;
514 ret = uci_batch_cmd();
518 fprintf(stderr, "Unknown command\n");
521 uci_foreach_element_safe(&ctx->root, tmp, e) {
522 uci_unload(ctx, uci_to_package(e));
525 flags &= ~CLI_FLAG_BATCH;
530 static int uci_cmd(int argc, char **argv)
534 if (!strcasecmp(argv[0], "batch") && !(flags & CLI_FLAG_BATCH))
536 else if (!strcasecmp(argv[0], "show"))
538 else if (!strcasecmp(argv[0], "changes"))
540 else if (!strcasecmp(argv[0], "export"))
542 else if (!strcasecmp(argv[0], "commit"))
544 else if (!strcasecmp(argv[0], "get"))
546 else if (!strcasecmp(argv[0], "set"))
548 else if (!strcasecmp(argv[0], "ren") ||
549 !strcasecmp(argv[0], "rename"))
551 else if (!strcasecmp(argv[0], "revert"))
553 else if (!strcasecmp(argv[0], "del") ||
554 !strcasecmp(argv[0], "delete"))
556 else if (!strcasecmp(argv[0], "import"))
558 else if (!strcasecmp(argv[0], "help"))
560 else if (!strcasecmp(argv[0], "add"))
562 else if (!strcasecmp(argv[0], "add_list"))
574 return uci_do_section_cmd(cmd, argc, argv);
579 return uci_do_package_cmd(cmd, argc, argv);
581 return uci_do_import(argc, argv);
583 return uci_do_add(argc, argv);
592 int main(int argc, char **argv)
597 flags = CLI_FLAG_SHOW_EXT;
600 ctx = uci_alloc_context();
602 fprintf(stderr, "Out of memory\n");
606 while((c = getopt(argc, argv, "c:d:f:mnNp:P:sSqX")) != -1) {
609 uci_set_confdir(ctx, optarg);
615 input = fopen(optarg, "r");
622 flags |= CLI_FLAG_MERGE;
625 ctx->flags |= UCI_FLAG_STRICT;
628 ctx->flags &= ~UCI_FLAG_STRICT;
629 ctx->flags |= UCI_FLAG_PERROR;
632 ctx->flags |= UCI_FLAG_EXPORT_NAME;
635 ctx->flags &= ~UCI_FLAG_EXPORT_NAME;
638 uci_add_history_path(ctx, optarg);
641 uci_add_history_path(ctx, ctx->savedir);
642 uci_set_savedir(ctx, optarg);
643 flags |= CLI_FLAG_NOCOMMIT;
646 flags |= CLI_FLAG_QUIET;
649 flags &= ~CLI_FLAG_SHOW_EXT;
657 argv[optind - 1] = argv[0];
665 ret = uci_cmd(argc - 1, argv + 1);
673 uci_free_context(ctx);