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.
22 #define MAX_ARGS 4 /* max command line arguments for batch mode */
24 static const char *delimiter
= " ";
25 static const char *appname
;
27 CLI_FLAG_MERGE
= (1 << 0),
28 CLI_FLAG_QUIET
= (1 << 1),
29 CLI_FLAG_NOCOMMIT
= (1 << 2),
30 CLI_FLAG_BATCH
= (1 << 3),
31 CLI_FLAG_SHOW_EXT
= (1 << 4),
36 static struct uci_context
*ctx
;
58 struct uci_type_list
{
61 struct uci_type_list
*next
;
64 static struct uci_type_list
*type_list
= NULL
;
65 static char *typestr
= NULL
;
66 static const char *cur_section_ref
= NULL
;
68 static int uci_cmd(int argc
, char **argv
);
71 uci_reset_typelist(void)
73 struct uci_type_list
*type
;
74 while (type_list
!= NULL
) {
76 type_list
= type_list
->next
;
83 cur_section_ref
= NULL
;
87 uci_lookup_section_ref(struct uci_section
*s
)
89 struct uci_type_list
*ti
= type_list
;
93 if (!(flags
& CLI_FLAG_SHOW_EXT
))
96 /* look up in section type list */
98 if (strcmp(ti
->name
, s
->type
) == 0)
103 ti
= malloc(sizeof(struct uci_type_list
));
106 memset(ti
, 0, sizeof(struct uci_type_list
));
107 ti
->next
= type_list
;
113 maxlen
= strlen(s
->type
) + 1 + 2 + 10;
115 typestr
= malloc(maxlen
);
117 typestr
= realloc(typestr
, maxlen
);
121 sprintf(typestr
, "@%s[%d]", ti
->name
, ti
->idx
);
133 static void uci_usage(void)
136 "Usage: %s [<options>] <command> [<arguments>]\n\n"
139 "\texport [<config>]\n"
140 "\timport [<config>]\n"
141 "\tchanges [<config>]\n"
142 "\tcommit [<config>]\n"
143 "\tadd <config> <section-type>\n"
144 "\tadd_list <config>.<section>.<option>=<string>\n"
145 "\tdel_list <config>.<section>.<option>=<string>\n"
146 "\tshow [<config>[.<section>[.<option>]]]\n"
147 "\tget <config>.<section>[.<option>]\n"
148 "\tset <config>.<section>[.<option>]=<value>\n"
149 "\tdelete <config>[.<section>[[.<option>][=<id>]]]\n"
150 "\trename <config>.<section>[.<option>]=<name>\n"
151 "\trevert <config>[.<section>[.<option>]]\n"
152 "\treorder <config>.<section>=<position>\n"
155 "\t-c <path> set the search path for config files (default: /etc/config)\n"
156 "\t-d <str> set the delimiter for list values in uci show\n"
157 "\t-f <file> use <file> as input instead of stdin\n"
158 "\t-m when importing, merge data into an existing package\n"
159 "\t-p <path> add a search path for config change files\n"
160 "\t-P <path> add a search path for config change files and use as default\n"
161 "\t-q quiet mode (don't print error messages)\n"
162 "\t-s force strict mode (stop on parser errors, default)\n"
163 "\t-S disable strict mode\n"
164 "\t-X do not use extended syntax on 'show'\n"
170 static void cli_perror(void)
172 if (flags
& CLI_FLAG_QUIET
)
175 uci_perror(ctx
, appname
);
178 static void cli_error(const char *fmt
, ...)
182 if (flags
& CLI_FLAG_QUIET
)
186 vfprintf(stderr
, fmt
, ap
);
190 static void uci_print_value(FILE *f
, const char *v
)
203 static void uci_show_value(struct uci_option
*o
, bool quote
)
205 struct uci_element
*e
;
210 case UCI_TYPE_STRING
:
212 uci_print_value(stdout
, o
->v
.string
);
214 printf("%s", o
->v
.string
);
218 uci_foreach_element(&o
->v
.list
, e
) {
219 printf("%s", (sep
? delimiter
: ""));
220 space
= strpbrk(e
->name
, " \t\r\n");
221 if (!space
&& !quote
)
222 printf("%s", e
->name
);
224 uci_print_value(stdout
, e
->name
);
230 printf("<unknown>\n");
235 static void uci_show_option(struct uci_option
*o
, bool quote
)
238 o
->section
->package
->e
.name
,
239 (cur_section_ref
? cur_section_ref
: o
->section
->e
.name
),
241 uci_show_value(o
, quote
);
244 static void uci_show_section(struct uci_section
*s
)
246 struct uci_element
*e
;
250 cname
= s
->package
->e
.name
;
251 sname
= (cur_section_ref
? cur_section_ref
: s
->e
.name
);
252 printf("%s.%s=%s\n", cname
, sname
, s
->type
);
253 uci_foreach_element(&s
->options
, e
) {
254 uci_show_option(uci_to_option(e
), true);
258 static void uci_show_package(struct uci_package
*p
)
260 struct uci_element
*e
;
262 uci_reset_typelist();
263 uci_foreach_element( &p
->sections
, e
) {
264 struct uci_section
*s
= uci_to_section(e
);
265 cur_section_ref
= uci_lookup_section_ref(s
);
268 uci_reset_typelist();
271 static void uci_show_changes(struct uci_package
*p
)
273 struct uci_element
*e
;
275 uci_foreach_element(&p
->saved_delta
, e
) {
276 struct uci_delta
*h
= uci_to_delta(e
);
284 case UCI_CMD_LIST_ADD
:
287 case UCI_CMD_LIST_DEL
:
293 printf("%s%s.%s", prefix
, p
->e
.name
, h
->section
);
295 printf(".%s", e
->name
);
296 if (h
->cmd
!= UCI_CMD_REMOVE
) {
298 uci_print_value(stdout
, h
->value
);
304 static int package_cmd(int cmd
, char *tuple
)
306 struct uci_element
*e
= NULL
;
310 if (uci_lookup_ptr(ctx
, &ptr
, tuple
, true) != UCI_OK
) {
318 uci_show_changes(ptr
.p
);
321 if (flags
& CLI_FLAG_NOCOMMIT
) {
325 if (uci_commit(ctx
, &ptr
.p
, false) != UCI_OK
) {
331 if (uci_export(ctx
, stdout
, ptr
.p
, true) != UCI_OK
) {
336 if (!(ptr
.flags
& UCI_LOOKUP_COMPLETE
)) {
337 ctx
->err
= UCI_ERR_NOTFOUND
;
342 case UCI_TYPE_PACKAGE
:
343 uci_show_package(ptr
.p
);
345 case UCI_TYPE_SECTION
:
346 uci_show_section(ptr
.s
);
348 case UCI_TYPE_OPTION
:
349 uci_show_option(ptr
.o
, true);
352 /* should not happen */
362 uci_unload(ctx
, ptr
.p
);
366 static int uci_do_import(int argc
, char **argv
)
368 struct uci_package
*package
= NULL
;
378 else if (flags
& CLI_FLAG_MERGE
)
379 /* need a package to merge */
382 if (flags
& CLI_FLAG_MERGE
) {
383 if (uci_load(ctx
, name
, &package
) != UCI_OK
)
388 ret
= uci_import(ctx
, input
, name
, &package
, (name
!= NULL
));
391 ret
= uci_save(ctx
, package
);
393 struct uci_element
*e
;
394 /* loop through all config sections and overwrite existing data */
395 uci_foreach_element(&ctx
->root
, e
) {
396 struct uci_package
*p
= uci_to_package(e
);
397 ret
= uci_commit(ctx
, &p
, true);
410 static int uci_do_package_cmd(int cmd
, int argc
, char **argv
)
412 char **configs
= NULL
;
420 return package_cmd(cmd
, argv
[1]);
422 if ((uci_list_configs(ctx
, &configs
) != UCI_OK
) || !configs
) {
427 for (p
= configs
; *p
; p
++) {
428 package_cmd(cmd
, *p
);
437 static int uci_do_add(int argc
, char **argv
)
439 struct uci_package
*p
= NULL
;
440 struct uci_section
*s
= NULL
;
446 ret
= uci_load(ctx
, argv
[1], &p
);
450 ret
= uci_add_section(ctx
, p
, argv
[2], &s
);
454 ret
= uci_save(ctx
, p
);
460 fprintf(stdout
, "%s\n", s
->e
.name
);
465 static int uci_do_section_cmd(int cmd
, int argc
, char **argv
)
467 struct uci_element
*e
;
475 if (uci_lookup_ptr(ctx
, &ptr
, argv
[1], true) != UCI_OK
) {
480 if (ptr
.value
&& (cmd
!= CMD_SET
) && (cmd
!= CMD_DEL
) &&
481 (cmd
!= CMD_ADD_LIST
) && (cmd
!= CMD_DEL_LIST
) &&
482 (cmd
!= CMD_RENAME
) && (cmd
!= CMD_REORDER
))
488 if (!(ptr
.flags
& UCI_LOOKUP_COMPLETE
)) {
489 ctx
->err
= UCI_ERR_NOTFOUND
;
494 case UCI_TYPE_SECTION
:
495 printf("%s\n", ptr
.s
->type
);
497 case UCI_TYPE_OPTION
:
498 uci_show_value(ptr
.o
, false);
503 /* throw the value to stdout */
506 ret
= uci_rename(ctx
, &ptr
);
509 ret
= uci_revert(ctx
, &ptr
);
512 ret
= uci_set(ctx
, &ptr
);
515 ret
= uci_add_list(ctx
, &ptr
);
518 ret
= uci_del_list(ctx
, &ptr
);
521 if (!ptr
.s
|| !ptr
.value
) {
522 ctx
->err
= UCI_ERR_NOTFOUND
;
526 ret
= uci_reorder_section(ctx
, ptr
.s
, strtoul(ptr
.value
, NULL
, 10));
529 if (ptr
.value
&& !sscanf(ptr
.value
, "%d", &dummy
))
531 ret
= uci_delete(ctx
, &ptr
);
535 /* no save necessary for get */
536 if ((cmd
== CMD_GET
) || (cmd
== CMD_REVERT
))
539 /* save changes, but don't commit them yet */
541 ret
= uci_save(ctx
, ptr
.p
);
551 static int uci_batch_cmd(void)
553 char *argv
[MAX_ARGS
+ 2];
558 for(i
= 0; i
<= MAX_ARGS
; i
++) {
560 cli_error("Too many arguments\n");
564 if ((ret
= uci_parse_argument(ctx
, input
, &str
, &argv
[i
])) != UCI_OK
) {
571 argv
[i
] = strdup(argv
[i
]);
573 cli_error("uci: %s", strerror(errno
));
580 if (!strcasecmp(argv
[0], "exit"))
582 ret
= uci_cmd(i
, argv
);
586 for (j
= 0; j
< i
; j
++) {
593 static int uci_batch(void)
597 flags
|= CLI_FLAG_BATCH
;
598 while (!feof(input
)) {
599 struct uci_element
*e
, *tmp
;
601 ret
= uci_batch_cmd();
605 cli_error("Unknown command\n");
608 uci_foreach_element_safe(&ctx
->root
, tmp
, e
) {
609 uci_unload(ctx
, uci_to_package(e
));
612 flags
&= ~CLI_FLAG_BATCH
;
617 static int uci_cmd(int argc
, char **argv
)
621 if (!strcasecmp(argv
[0], "batch") && !(flags
& CLI_FLAG_BATCH
))
623 else if (!strcasecmp(argv
[0], "show"))
625 else if (!strcasecmp(argv
[0], "changes"))
627 else if (!strcasecmp(argv
[0], "export"))
629 else if (!strcasecmp(argv
[0], "commit"))
631 else if (!strcasecmp(argv
[0], "get"))
633 else if (!strcasecmp(argv
[0], "set"))
635 else if (!strcasecmp(argv
[0], "ren") ||
636 !strcasecmp(argv
[0], "rename"))
638 else if (!strcasecmp(argv
[0], "revert"))
640 else if (!strcasecmp(argv
[0], "reorder"))
642 else if (!strcasecmp(argv
[0], "del") ||
643 !strcasecmp(argv
[0], "delete"))
645 else if (!strcasecmp(argv
[0], "import"))
647 else if (!strcasecmp(argv
[0], "help"))
649 else if (!strcasecmp(argv
[0], "add"))
651 else if (!strcasecmp(argv
[0], "add_list"))
653 else if (!strcasecmp(argv
[0], "del_list"))
667 return uci_do_section_cmd(cmd
, argc
, argv
);
672 return uci_do_package_cmd(cmd
, argc
, argv
);
674 return uci_do_import(argc
, argv
);
676 return uci_do_add(argc
, argv
);
685 int main(int argc
, char **argv
)
690 flags
= CLI_FLAG_SHOW_EXT
;
693 ctx
= uci_alloc_context();
695 cli_error("Out of memory\n");
699 while((c
= getopt(argc
, argv
, "c:d:f:LmnNp:P:sSqX")) != -1) {
702 uci_set_confdir(ctx
, optarg
);
708 if (input
!= stdin
) {
710 cli_error("Too many input files.\n");
714 input
= fopen(optarg
, "r");
716 cli_error("uci: %s", strerror(errno
));
721 flags
|= CLI_FLAG_MERGE
;
724 ctx
->flags
|= UCI_FLAG_STRICT
;
727 ctx
->flags
&= ~UCI_FLAG_STRICT
;
728 ctx
->flags
|= UCI_FLAG_PERROR
;
731 uci_add_delta_path(ctx
, optarg
);
734 uci_add_delta_path(ctx
, ctx
->savedir
);
735 uci_set_savedir(ctx
, optarg
);
736 flags
|= CLI_FLAG_NOCOMMIT
;
739 flags
|= CLI_FLAG_QUIET
;
742 flags
&= ~CLI_FLAG_SHOW_EXT
;
754 argv
[optind
- 1] = argv
[0];
763 ret
= uci_cmd(argc
- 1, argv
+ 1);
770 uci_free_context(ctx
);