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),
33 static struct uci_context
*ctx
;
53 static int uci_cmd(int argc
, char **argv
);
55 static void uci_usage(void)
58 "Usage: %s [<options>] <command> [<arguments>]\n\n"
61 "\texport [<config>]\n"
62 "\timport [<config>]\n"
63 "\tchanges [<config>]\n"
64 "\tcommit [<config>]\n"
65 "\tadd <config> <section-type>\n"
66 "\tadd_list <config>.<section>.<option>=<string>\n"
67 "\tshow [<config>[.<section>[.<option>]]]\n"
68 "\tget <config>.<section>[.<option>]\n"
69 "\tset <config>.<section>[.<option>]=<value>\n"
70 "\tdelete <config>[.<section[.<option>]]\n"
71 "\trename <config>.<section>[.<option>]=<name>\n"
72 "\trevert <config>[.<section>[.<option>]]\n"
75 "\t-c <path> set the search path for config files (default: /etc/config)\n"
76 "\t-d <str> set the delimiter for list values in uci show\n"
77 "\t-f <file> use <file> as input instead of stdin\n"
78 "\t-m when importing, merge data into an existing package\n"
79 "\t-n name unnamed sections on export (default)\n"
80 "\t-N don't name unnamed sections\n"
81 "\t-p <path> add a search path for config change files\n"
82 "\t-P <path> add a search path for config change files and use as default\n"
83 "\t-q quiet mode (don't print error messages)\n"
84 "\t-s force strict mode (stop on parser errors, default)\n"
85 "\t-S disable strict mode\n"
91 static void cli_perror(void)
93 if (flags
& CLI_FLAG_QUIET
)
96 uci_perror(ctx
, appname
);
99 static void uci_show_value(struct uci_option
*o
)
101 struct uci_element
*e
;
105 case UCI_TYPE_STRING
:
106 printf("%s\n", o
->v
.string
);
109 uci_foreach_element(&o
->v
.list
, e
) {
110 printf("%s%s", (sep
? delimiter
: ""), e
->name
);
116 printf("<unknown>\n");
121 static void uci_show_option(struct uci_option
*o
)
124 o
->section
->package
->e
.name
,
130 static void uci_show_section(struct uci_section
*p
)
132 struct uci_element
*e
;
133 const char *cname
, *sname
;
135 cname
= p
->package
->e
.name
;
137 printf("%s.%s=%s\n", cname
, sname
, p
->type
);
138 uci_foreach_element(&p
->options
, e
) {
139 uci_show_option(uci_to_option(e
));
143 static void uci_show_package(struct uci_package
*p
)
145 struct uci_element
*e
;
147 uci_foreach_element( &p
->sections
, e
) {
148 uci_show_section(uci_to_section(e
));
152 static void uci_show_changes(struct uci_package
*p
)
154 struct uci_element
*e
;
156 uci_foreach_element(&p
->saved_history
, e
) {
157 struct uci_history
*h
= uci_to_history(e
);
165 case UCI_CMD_LIST_ADD
:
171 printf("%s%s.%s", prefix
, p
->e
.name
, h
->section
);
173 printf(".%s", e
->name
);
174 if (h
->cmd
!= UCI_CMD_REMOVE
)
175 printf("%s%s", op
, h
->value
);
180 static int package_cmd(int cmd
, char *tuple
)
182 struct uci_element
*e
= NULL
;
185 if (uci_lookup_ptr(ctx
, &ptr
, tuple
, true) != UCI_OK
) {
199 uci_show_changes(ptr
.p
);
202 if (flags
& CLI_FLAG_NOCOMMIT
)
204 if (uci_commit(ctx
, &ptr
.p
, false) != UCI_OK
)
208 uci_export(ctx
, stdout
, ptr
.p
, true);
212 case UCI_TYPE_PACKAGE
:
213 uci_show_package(ptr
.p
);
215 case UCI_TYPE_SECTION
:
216 uci_show_section(ptr
.s
);
218 case UCI_TYPE_OPTION
:
219 uci_show_option(ptr
.o
);
222 /* should not happen */
228 uci_unload(ctx
, ptr
.p
);
232 static int uci_do_import(int argc
, char **argv
)
234 struct uci_package
*package
= NULL
;
244 else if (flags
& CLI_FLAG_MERGE
)
245 /* need a package to merge */
248 if (flags
& CLI_FLAG_MERGE
) {
249 if (uci_load(ctx
, name
, &package
) != UCI_OK
)
254 ret
= uci_import(ctx
, input
, name
, &package
, (name
!= NULL
));
257 ret
= uci_save(ctx
, package
);
259 struct uci_element
*e
;
260 /* loop through all config sections and overwrite existing data */
261 uci_foreach_element(&ctx
->root
, e
) {
262 struct uci_package
*p
= uci_to_package(e
);
263 ret
= uci_commit(ctx
, &p
, true);
276 static int uci_do_package_cmd(int cmd
, int argc
, char **argv
)
278 char **configs
= NULL
;
285 return package_cmd(cmd
, argv
[1]);
287 if ((uci_list_configs(ctx
, &configs
) != UCI_OK
) || !configs
) {
292 for (p
= configs
; *p
; p
++) {
293 package_cmd(cmd
, *p
);
299 static int uci_do_add(int argc
, char **argv
)
301 struct uci_package
*p
= NULL
;
302 struct uci_section
*s
= NULL
;
308 ret
= uci_load(ctx
, argv
[1], &p
);
312 ret
= uci_add_section(ctx
, p
, argv
[2], &s
);
316 ret
= uci_save(ctx
, p
);
322 fprintf(stdout
, "%s\n", s
->e
.name
);
327 static int uci_do_section_cmd(int cmd
, int argc
, char **argv
)
329 struct uci_element
*e
;
336 if (uci_lookup_ptr(ctx
, &ptr
, argv
[1], true) != UCI_OK
) {
341 if (ptr
.value
&& (cmd
!= CMD_SET
) && (cmd
!= CMD_ADD_LIST
) && (cmd
!= CMD_RENAME
))
354 case UCI_TYPE_SECTION
:
355 printf("%s\n", ptr
.s
->type
);
357 case UCI_TYPE_OPTION
:
358 uci_show_value(ptr
.o
);
363 /* throw the value to stdout */
366 ret
= uci_rename(ctx
, &ptr
);
369 ret
= uci_revert(ctx
, &ptr
.p
, ptr
.section
, ptr
.option
);
372 ret
= uci_set(ctx
, ptr
.p
, ptr
.section
, ptr
.option
, ptr
.value
, NULL
);
375 ret
= uci_add_list(ctx
, &ptr
);
378 ret
= uci_delete(ctx
, &ptr
);
382 /* no save necessary for get */
383 if ((cmd
== CMD_GET
) || (cmd
== CMD_REVERT
))
386 /* save changes, but don't commit them yet */
388 ret
= uci_save(ctx
, ptr
.p
);
398 static int uci_batch_cmd(void)
400 char *argv
[MAX_ARGS
];
405 for(i
= 0; i
<= MAX_ARGS
; i
++) {
407 fprintf(stderr
, "Too many arguments\n");
411 if ((ret
= uci_parse_argument(ctx
, input
, &str
, &argv
[i
])) != UCI_OK
) {
418 argv
[i
] = strdup(argv
[i
]);
427 if (!strcasecmp(argv
[0], "exit"))
429 ret
= uci_cmd(i
, argv
);
433 for (j
= 0; j
< i
; j
++) {
441 static int uci_batch(void)
445 while (!feof(input
)) {
446 struct uci_element
*e
, *tmp
;
448 ret
= uci_batch_cmd();
452 fprintf(stderr
, "Unknown command\n");
455 uci_foreach_element_safe(&ctx
->root
, tmp
, e
) {
456 uci_unload(ctx
, uci_to_package(e
));
462 static int uci_cmd(int argc
, char **argv
)
466 if (!strcasecmp(argv
[0], "batch") && !(flags
& CLI_FLAG_BATCH
))
468 else if (!strcasecmp(argv
[0], "show"))
470 else if (!strcasecmp(argv
[0], "changes"))
472 else if (!strcasecmp(argv
[0], "export"))
474 else if (!strcasecmp(argv
[0], "commit"))
476 else if (!strcasecmp(argv
[0], "get"))
478 else if (!strcasecmp(argv
[0], "set"))
480 else if (!strcasecmp(argv
[0], "ren") ||
481 !strcasecmp(argv
[0], "rename"))
483 else if (!strcasecmp(argv
[0], "revert"))
485 else if (!strcasecmp(argv
[0], "del"))
487 else if (!strcasecmp(argv
[0], "import"))
489 else if (!strcasecmp(argv
[0], "help"))
491 else if (!strcasecmp(argv
[0], "add"))
493 else if (!strcasecmp(argv
[0], "add_list"))
505 return uci_do_section_cmd(cmd
, argc
, argv
);
510 return uci_do_package_cmd(cmd
, argc
, argv
);
512 return uci_do_import(argc
, argv
);
514 return uci_do_add(argc
, argv
);
523 int main(int argc
, char **argv
)
530 ctx
= uci_alloc_context();
532 fprintf(stderr
, "Out of memory\n");
536 while((c
= getopt(argc
, argv
, "c:d:f:mnNp:P:sSq")) != -1) {
539 uci_set_confdir(ctx
, optarg
);
545 input
= fopen(optarg
, "r");
552 flags
|= CLI_FLAG_MERGE
;
555 ctx
->flags
|= UCI_FLAG_STRICT
;
558 ctx
->flags
&= ~UCI_FLAG_STRICT
;
559 ctx
->flags
|= UCI_FLAG_PERROR
;
562 ctx
->flags
|= UCI_FLAG_EXPORT_NAME
;
565 ctx
->flags
&= ~UCI_FLAG_EXPORT_NAME
;
568 uci_add_history_path(ctx
, optarg
);
571 uci_add_history_path(ctx
, ctx
->savedir
);
572 uci_set_savedir(ctx
, optarg
);
573 flags
|= CLI_FLAG_NOCOMMIT
;
576 flags
|= CLI_FLAG_QUIET
;
584 argv
[optind
- 1] = argv
[0];
592 ret
= uci_cmd(argc
- 1, argv
+ 1);
600 uci_free_context(ctx
);