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
= calloc(1, sizeof(struct uci_type_list
));
106 ti
->next
= type_list
;
112 maxlen
= strlen(s
->type
) + 1 + 2 + 10;
114 typestr
= malloc(maxlen
);
118 void *p
= realloc(typestr
, maxlen
);
128 sprintf(typestr
, "@%s[%d]", ti
->name
, ti
->idx
);
140 static void uci_usage(void)
143 "Usage: %s [<options>] <command> [<arguments>]\n\n"
146 "\texport [<config>]\n"
147 "\timport [<config>]\n"
148 "\tchanges [<config>]\n"
149 "\tcommit [<config>]\n"
150 "\tadd <config> <section-type>\n"
151 "\tadd_list <config>.<section>.<option>=<string>\n"
152 "\tdel_list <config>.<section>.<option>=<string>\n"
153 "\tshow [<config>[.<section>[.<option>]]]\n"
154 "\tget <config>.<section>[.<option>]\n"
155 "\tset <config>.<section>[.<option>]=<value>\n"
156 "\tdelete <config>[.<section>[[.<option>][=<id>]]]\n"
157 "\trename <config>.<section>[.<option>]=<name>\n"
158 "\trevert <config>[.<section>[.<option>]]\n"
159 "\treorder <config>.<section>=<position>\n"
162 "\t-c <path> set the search path for config files (default: /etc/config)\n"
163 "\t-d <str> set the delimiter for list values in uci show\n"
164 "\t-f <file> use <file> as input instead of stdin\n"
165 "\t-m when importing, merge data into an existing package\n"
166 "\t-n name unnamed sections on export (default)\n"
167 "\t-N don't name unnamed sections\n"
168 "\t-p <path> add a search path for config change files\n"
169 "\t-P <path> add a search path for config change files and use as default\n"
170 "\t-t <path> set save path for config change files\n"
171 "\t-q quiet mode (don't print error messages)\n"
172 "\t-s force strict mode (stop on parser errors, default)\n"
173 "\t-S disable strict mode\n"
174 "\t-X do not use extended syntax on 'show'\n"
180 static void cli_perror(void)
182 if (flags
& CLI_FLAG_QUIET
)
185 uci_perror(ctx
, appname
);
188 __attribute__((format(printf
, 1, 2)))
189 static void cli_error(const char *fmt
, ...)
193 if (flags
& CLI_FLAG_QUIET
)
197 vfprintf(stderr
, fmt
, ap
);
201 static void uci_print_value(FILE *f
, const char *v
)
214 static void uci_show_value(struct uci_option
*o
, bool quote
)
216 struct uci_element
*e
;
221 case UCI_TYPE_STRING
:
223 uci_print_value(stdout
, o
->v
.string
);
225 printf("%s", o
->v
.string
);
229 uci_foreach_element(&o
->v
.list
, e
) {
230 printf("%s", (sep
? delimiter
: ""));
231 space
= strpbrk(e
->name
, " \t\r\n");
232 if (!space
&& !quote
)
233 printf("%s", e
->name
);
235 uci_print_value(stdout
, e
->name
);
241 printf("<unknown>\n");
246 static void uci_show_option(struct uci_option
*o
, bool quote
)
249 o
->section
->package
->e
.name
,
250 (cur_section_ref
? cur_section_ref
: o
->section
->e
.name
),
252 uci_show_value(o
, quote
);
255 static void uci_show_section(struct uci_section
*s
)
257 struct uci_element
*e
;
261 cname
= s
->package
->e
.name
;
262 sname
= (cur_section_ref
? cur_section_ref
: s
->e
.name
);
263 printf("%s.%s=%s\n", cname
, sname
, s
->type
);
264 uci_foreach_element(&s
->options
, e
) {
265 uci_show_option(uci_to_option(e
), true);
269 static void uci_show_package(struct uci_package
*p
)
271 struct uci_element
*e
;
273 uci_reset_typelist();
274 uci_foreach_element( &p
->sections
, e
) {
275 struct uci_section
*s
= uci_to_section(e
);
276 cur_section_ref
= uci_lookup_section_ref(s
);
279 uci_reset_typelist();
282 static void uci_show_changes(struct uci_package
*p
)
284 struct uci_element
*e
;
286 uci_foreach_element(&p
->saved_delta
, e
) {
287 struct uci_delta
*h
= uci_to_delta(e
);
295 case UCI_CMD_LIST_ADD
:
298 case UCI_CMD_LIST_DEL
:
304 printf("%s%s.%s", prefix
, p
->e
.name
, h
->section
);
306 printf(".%s", e
->name
);
307 if (h
->cmd
!= UCI_CMD_REMOVE
) {
309 uci_print_value(stdout
, h
->value
);
315 static int package_cmd(int cmd
, char *tuple
)
317 struct uci_element
*e
= NULL
;
321 if (uci_lookup_ptr(ctx
, &ptr
, tuple
, true) != UCI_OK
) {
329 uci_show_changes(ptr
.p
);
332 if (flags
& CLI_FLAG_NOCOMMIT
) {
336 if (uci_commit(ctx
, &ptr
.p
, false) != UCI_OK
) {
342 if (uci_export(ctx
, stdout
, ptr
.p
, true) != UCI_OK
) {
347 if (!(ptr
.flags
& UCI_LOOKUP_COMPLETE
)) {
348 ctx
->err
= UCI_ERR_NOTFOUND
;
353 case UCI_TYPE_PACKAGE
:
354 uci_show_package(ptr
.p
);
356 case UCI_TYPE_SECTION
:
357 uci_show_section(ptr
.s
);
359 case UCI_TYPE_OPTION
:
360 uci_show_option(ptr
.o
, true);
363 /* should not happen */
373 uci_unload(ctx
, ptr
.p
);
377 static int uci_do_import(int argc
, char **argv
)
379 struct uci_package
*package
= NULL
;
389 else if (flags
& CLI_FLAG_MERGE
)
390 /* need a package to merge */
393 if (flags
& CLI_FLAG_MERGE
) {
394 if (uci_load(ctx
, name
, &package
) != UCI_OK
)
399 ret
= uci_import(ctx
, input
, name
, &package
, (name
!= NULL
));
402 ret
= uci_save(ctx
, package
);
404 struct uci_element
*e
;
405 /* loop through all config sections and overwrite existing data */
406 uci_foreach_element(&ctx
->root
, e
) {
407 struct uci_package
*p
= uci_to_package(e
);
408 ret
= uci_commit(ctx
, &p
, true);
421 static int uci_do_package_cmd(int cmd
, int argc
, char **argv
)
423 char **configs
= NULL
;
431 return package_cmd(cmd
, argv
[1]);
433 if ((uci_list_configs(ctx
, &configs
) != UCI_OK
) || !configs
) {
438 for (p
= configs
; *p
; p
++) {
439 package_cmd(cmd
, *p
);
448 static int uci_do_add(int argc
, char **argv
)
450 struct uci_package
*p
= NULL
;
451 struct uci_section
*s
= NULL
;
457 ret
= uci_load(ctx
, argv
[1], &p
);
461 ret
= uci_add_section(ctx
, p
, argv
[2], &s
);
465 ret
= uci_save(ctx
, p
);
471 fprintf(stdout
, "%s\n", s
->e
.name
);
476 static int uci_do_section_cmd(int cmd
, int argc
, char **argv
)
478 struct uci_element
*e
;
486 if (uci_lookup_ptr(ctx
, &ptr
, argv
[1], true) != UCI_OK
) {
491 if (ptr
.value
&& (cmd
!= CMD_SET
) && (cmd
!= CMD_DEL
) &&
492 (cmd
!= CMD_ADD_LIST
) && (cmd
!= CMD_DEL_LIST
) &&
493 (cmd
!= CMD_RENAME
) && (cmd
!= CMD_REORDER
))
499 if (!(ptr
.flags
& UCI_LOOKUP_COMPLETE
)) {
500 ctx
->err
= UCI_ERR_NOTFOUND
;
505 case UCI_TYPE_SECTION
:
506 printf("%s\n", ptr
.s
->type
);
508 case UCI_TYPE_OPTION
:
509 uci_show_value(ptr
.o
, false);
514 /* throw the value to stdout */
517 ret
= uci_rename(ctx
, &ptr
);
520 ret
= uci_revert(ctx
, &ptr
);
523 ret
= uci_set(ctx
, &ptr
);
526 ret
= uci_add_list(ctx
, &ptr
);
529 ret
= uci_del_list(ctx
, &ptr
);
532 if (!ptr
.s
|| !ptr
.value
) {
533 ctx
->err
= UCI_ERR_NOTFOUND
;
537 ret
= uci_reorder_section(ctx
, ptr
.s
, strtoul(ptr
.value
, NULL
, 10));
540 if (ptr
.value
&& !sscanf(ptr
.value
, "%d", &dummy
))
542 ret
= uci_delete(ctx
, &ptr
);
546 /* no save necessary for get */
547 if ((cmd
== CMD_GET
) || (cmd
== CMD_REVERT
))
550 /* save changes, but don't commit them yet */
552 ret
= uci_save(ctx
, ptr
.p
);
562 static int uci_batch_cmd(void)
564 char *argv
[MAX_ARGS
+ 2];
569 for(i
= 0; i
<= MAX_ARGS
; i
++) {
571 cli_error("Too many arguments\n");
575 if (uci_parse_argument(ctx
, input
, &str
, &argv
[i
]) != UCI_OK
) {
582 argv
[i
] = strdup(argv
[i
]);
584 cli_error("uci: %s", strerror(errno
));
591 if (!strcasecmp(argv
[0], "exit"))
593 ret
= uci_cmd(i
, argv
);
597 for (j
= 0; j
< i
; j
++) {
604 static int uci_batch(void)
608 flags
|= CLI_FLAG_BATCH
;
609 while (!feof(input
)) {
610 struct uci_element
*e
, *tmp
;
612 ret
= uci_batch_cmd();
616 cli_error("Unknown command\n");
619 uci_foreach_element_safe(&ctx
->root
, tmp
, e
) {
620 uci_unload(ctx
, uci_to_package(e
));
623 flags
&= ~CLI_FLAG_BATCH
;
628 static int uci_cmd(int argc
, char **argv
)
632 if (!strcasecmp(argv
[0], "batch") && !(flags
& CLI_FLAG_BATCH
))
634 else if (!strcasecmp(argv
[0], "show"))
636 else if (!strcasecmp(argv
[0], "changes"))
638 else if (!strcasecmp(argv
[0], "export"))
640 else if (!strcasecmp(argv
[0], "commit"))
642 else if (!strcasecmp(argv
[0], "get"))
644 else if (!strcasecmp(argv
[0], "set"))
646 else if (!strcasecmp(argv
[0], "ren") ||
647 !strcasecmp(argv
[0], "rename"))
649 else if (!strcasecmp(argv
[0], "revert"))
651 else if (!strcasecmp(argv
[0], "reorder"))
653 else if (!strcasecmp(argv
[0], "del") ||
654 !strcasecmp(argv
[0], "delete"))
656 else if (!strcasecmp(argv
[0], "import"))
658 else if (!strcasecmp(argv
[0], "help"))
660 else if (!strcasecmp(argv
[0], "add"))
662 else if (!strcasecmp(argv
[0], "add_list"))
664 else if (!strcasecmp(argv
[0], "del_list"))
678 return uci_do_section_cmd(cmd
, argc
, argv
);
683 return uci_do_package_cmd(cmd
, argc
, argv
);
685 return uci_do_import(argc
, argv
);
687 return uci_do_add(argc
, argv
);
696 int main(int argc
, char **argv
)
701 flags
= CLI_FLAG_SHOW_EXT
;
704 ctx
= uci_alloc_context();
706 cli_error("Out of memory\n");
710 while((c
= getopt(argc
, argv
, "c:d:f:LmnNp:P:qsSt:X")) != -1) {
713 uci_set_confdir(ctx
, optarg
);
719 if (input
!= stdin
) {
721 cli_error("Too many input files.\n");
725 input
= fopen(optarg
, "r");
727 cli_error("uci: %s", strerror(errno
));
732 flags
|= CLI_FLAG_MERGE
;
735 ctx
->flags
|= UCI_FLAG_STRICT
;
738 ctx
->flags
&= ~UCI_FLAG_STRICT
;
739 ctx
->flags
|= UCI_FLAG_PERROR
;
742 ctx
->flags
|= UCI_FLAG_EXPORT_NAME
;
745 ctx
->flags
&= ~UCI_FLAG_EXPORT_NAME
;
748 uci_add_delta_path(ctx
, optarg
);
751 uci_set_savedir(ctx
, optarg
);
752 flags
|= CLI_FLAG_NOCOMMIT
;
755 flags
|= CLI_FLAG_QUIET
;
758 uci_set_savedir(ctx
, optarg
);
761 flags
&= ~CLI_FLAG_SHOW_EXT
;
769 argv
[optind
- 1] = argv
[0];
778 ret
= uci_cmd(argc
- 1, argv
+ 1);
785 uci_free_context(ctx
);