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-n name unnamed sections on export (default)\n"
160 "\t-N don't name unnamed sections\n"
161 "\t-p <path> add a search path for config change files\n"
162 "\t-P <path> add a search path for config change files and use as default\n"
163 "\t-q quiet mode (don't print error messages)\n"
164 "\t-s force strict mode (stop on parser errors, default)\n"
165 "\t-S disable strict mode\n"
166 "\t-X do not use extended syntax on 'show'\n"
172 static void cli_perror(void)
174 if (flags
& CLI_FLAG_QUIET
)
177 uci_perror(ctx
, appname
);
180 static void cli_error(const char *fmt
, ...)
184 if (flags
& CLI_FLAG_QUIET
)
188 vfprintf(stderr
, fmt
, ap
);
192 static void uci_print_value(FILE *f
, const char *v
)
205 static void uci_show_value(struct uci_option
*o
, bool quote
)
207 struct uci_element
*e
;
212 case UCI_TYPE_STRING
:
214 uci_print_value(stdout
, o
->v
.string
);
216 printf("%s", o
->v
.string
);
220 uci_foreach_element(&o
->v
.list
, e
) {
221 printf("%s", (sep
? delimiter
: ""));
222 space
= strpbrk(e
->name
, " \t\r\n");
223 if (!space
&& !quote
)
224 printf("%s", e
->name
);
226 uci_print_value(stdout
, e
->name
);
232 printf("<unknown>\n");
237 static void uci_show_option(struct uci_option
*o
, bool quote
)
240 o
->section
->package
->e
.name
,
241 (cur_section_ref
? cur_section_ref
: o
->section
->e
.name
),
243 uci_show_value(o
, quote
);
246 static void uci_show_section(struct uci_section
*s
)
248 struct uci_element
*e
;
252 cname
= s
->package
->e
.name
;
253 sname
= (cur_section_ref
? cur_section_ref
: s
->e
.name
);
254 printf("%s.%s=%s\n", cname
, sname
, s
->type
);
255 uci_foreach_element(&s
->options
, e
) {
256 uci_show_option(uci_to_option(e
), true);
260 static void uci_show_package(struct uci_package
*p
)
262 struct uci_element
*e
;
264 uci_reset_typelist();
265 uci_foreach_element( &p
->sections
, e
) {
266 struct uci_section
*s
= uci_to_section(e
);
267 cur_section_ref
= uci_lookup_section_ref(s
);
270 uci_reset_typelist();
273 static void uci_show_changes(struct uci_package
*p
)
275 struct uci_element
*e
;
277 uci_foreach_element(&p
->saved_delta
, e
) {
278 struct uci_delta
*h
= uci_to_delta(e
);
286 case UCI_CMD_LIST_ADD
:
289 case UCI_CMD_LIST_DEL
:
295 printf("%s%s.%s", prefix
, p
->e
.name
, h
->section
);
297 printf(".%s", e
->name
);
298 if (h
->cmd
!= UCI_CMD_REMOVE
) {
300 uci_print_value(stdout
, h
->value
);
306 static int package_cmd(int cmd
, char *tuple
)
308 struct uci_element
*e
= NULL
;
312 if (uci_lookup_ptr(ctx
, &ptr
, tuple
, true) != UCI_OK
) {
320 uci_show_changes(ptr
.p
);
323 if (flags
& CLI_FLAG_NOCOMMIT
) {
327 if (uci_commit(ctx
, &ptr
.p
, false) != UCI_OK
) {
333 if (uci_export(ctx
, stdout
, ptr
.p
, true) != UCI_OK
) {
338 if (!(ptr
.flags
& UCI_LOOKUP_COMPLETE
)) {
339 ctx
->err
= UCI_ERR_NOTFOUND
;
344 case UCI_TYPE_PACKAGE
:
345 uci_show_package(ptr
.p
);
347 case UCI_TYPE_SECTION
:
348 uci_show_section(ptr
.s
);
350 case UCI_TYPE_OPTION
:
351 uci_show_option(ptr
.o
, true);
354 /* should not happen */
364 uci_unload(ctx
, ptr
.p
);
368 static int uci_do_import(int argc
, char **argv
)
370 struct uci_package
*package
= NULL
;
380 else if (flags
& CLI_FLAG_MERGE
)
381 /* need a package to merge */
384 if (flags
& CLI_FLAG_MERGE
) {
385 if (uci_load(ctx
, name
, &package
) != UCI_OK
)
390 ret
= uci_import(ctx
, input
, name
, &package
, (name
!= NULL
));
393 ret
= uci_save(ctx
, package
);
395 struct uci_element
*e
;
396 /* loop through all config sections and overwrite existing data */
397 uci_foreach_element(&ctx
->root
, e
) {
398 struct uci_package
*p
= uci_to_package(e
);
399 ret
= uci_commit(ctx
, &p
, true);
412 static int uci_do_package_cmd(int cmd
, int argc
, char **argv
)
414 char **configs
= NULL
;
422 return package_cmd(cmd
, argv
[1]);
424 if ((uci_list_configs(ctx
, &configs
) != UCI_OK
) || !configs
) {
429 for (p
= configs
; *p
; p
++) {
430 package_cmd(cmd
, *p
);
439 static int uci_do_add(int argc
, char **argv
)
441 struct uci_package
*p
= NULL
;
442 struct uci_section
*s
= NULL
;
448 ret
= uci_load(ctx
, argv
[1], &p
);
452 ret
= uci_add_section(ctx
, p
, argv
[2], &s
);
456 ret
= uci_save(ctx
, p
);
462 fprintf(stdout
, "%s\n", s
->e
.name
);
467 static int uci_do_section_cmd(int cmd
, int argc
, char **argv
)
469 struct uci_element
*e
;
477 if (uci_lookup_ptr(ctx
, &ptr
, argv
[1], true) != UCI_OK
) {
482 if (ptr
.value
&& (cmd
!= CMD_SET
) && (cmd
!= CMD_DEL
) &&
483 (cmd
!= CMD_ADD_LIST
) && (cmd
!= CMD_DEL_LIST
) &&
484 (cmd
!= CMD_RENAME
) && (cmd
!= CMD_REORDER
))
490 if (!(ptr
.flags
& UCI_LOOKUP_COMPLETE
)) {
491 ctx
->err
= UCI_ERR_NOTFOUND
;
496 case UCI_TYPE_SECTION
:
497 printf("%s\n", ptr
.s
->type
);
499 case UCI_TYPE_OPTION
:
500 uci_show_value(ptr
.o
, false);
505 /* throw the value to stdout */
508 ret
= uci_rename(ctx
, &ptr
);
511 ret
= uci_revert(ctx
, &ptr
);
514 ret
= uci_set(ctx
, &ptr
);
517 ret
= uci_add_list(ctx
, &ptr
);
520 ret
= uci_del_list(ctx
, &ptr
);
523 if (!ptr
.s
|| !ptr
.value
) {
524 ctx
->err
= UCI_ERR_NOTFOUND
;
528 ret
= uci_reorder_section(ctx
, ptr
.s
, strtoul(ptr
.value
, NULL
, 10));
531 if (ptr
.value
&& !sscanf(ptr
.value
, "%d", &dummy
))
533 ret
= uci_delete(ctx
, &ptr
);
537 /* no save necessary for get */
538 if ((cmd
== CMD_GET
) || (cmd
== CMD_REVERT
))
541 /* save changes, but don't commit them yet */
543 ret
= uci_save(ctx
, ptr
.p
);
553 static int uci_batch_cmd(void)
555 char *argv
[MAX_ARGS
+ 2];
560 for(i
= 0; i
<= MAX_ARGS
; i
++) {
562 cli_error("Too many arguments\n");
566 if ((ret
= uci_parse_argument(ctx
, input
, &str
, &argv
[i
])) != UCI_OK
) {
573 argv
[i
] = strdup(argv
[i
]);
575 cli_error("uci: %s", strerror(errno
));
582 if (!strcasecmp(argv
[0], "exit"))
584 ret
= uci_cmd(i
, argv
);
588 for (j
= 0; j
< i
; j
++) {
595 static int uci_batch(void)
599 flags
|= CLI_FLAG_BATCH
;
600 while (!feof(input
)) {
601 struct uci_element
*e
, *tmp
;
603 ret
= uci_batch_cmd();
607 cli_error("Unknown command\n");
610 uci_foreach_element_safe(&ctx
->root
, tmp
, e
) {
611 uci_unload(ctx
, uci_to_package(e
));
614 flags
&= ~CLI_FLAG_BATCH
;
619 static int uci_cmd(int argc
, char **argv
)
623 if (!strcasecmp(argv
[0], "batch") && !(flags
& CLI_FLAG_BATCH
))
625 else if (!strcasecmp(argv
[0], "show"))
627 else if (!strcasecmp(argv
[0], "changes"))
629 else if (!strcasecmp(argv
[0], "export"))
631 else if (!strcasecmp(argv
[0], "commit"))
633 else if (!strcasecmp(argv
[0], "get"))
635 else if (!strcasecmp(argv
[0], "set"))
637 else if (!strcasecmp(argv
[0], "ren") ||
638 !strcasecmp(argv
[0], "rename"))
640 else if (!strcasecmp(argv
[0], "revert"))
642 else if (!strcasecmp(argv
[0], "reorder"))
644 else if (!strcasecmp(argv
[0], "del") ||
645 !strcasecmp(argv
[0], "delete"))
647 else if (!strcasecmp(argv
[0], "import"))
649 else if (!strcasecmp(argv
[0], "help"))
651 else if (!strcasecmp(argv
[0], "add"))
653 else if (!strcasecmp(argv
[0], "add_list"))
655 else if (!strcasecmp(argv
[0], "del_list"))
669 return uci_do_section_cmd(cmd
, argc
, argv
);
674 return uci_do_package_cmd(cmd
, argc
, argv
);
676 return uci_do_import(argc
, argv
);
678 return uci_do_add(argc
, argv
);
687 int main(int argc
, char **argv
)
692 flags
= CLI_FLAG_SHOW_EXT
;
695 ctx
= uci_alloc_context();
697 cli_error("Out of memory\n");
701 while((c
= getopt(argc
, argv
, "c:d:f:LmnNp:P:sSqX")) != -1) {
704 uci_set_confdir(ctx
, optarg
);
710 if (input
!= stdin
) {
712 cli_error("Too many input files.\n");
716 input
= fopen(optarg
, "r");
718 cli_error("uci: %s", strerror(errno
));
723 flags
|= CLI_FLAG_MERGE
;
726 ctx
->flags
|= UCI_FLAG_STRICT
;
729 ctx
->flags
&= ~UCI_FLAG_STRICT
;
730 ctx
->flags
|= UCI_FLAG_PERROR
;
733 ctx
->flags
|= UCI_FLAG_EXPORT_NAME
;
736 ctx
->flags
&= ~UCI_FLAG_EXPORT_NAME
;
739 uci_add_delta_path(ctx
, optarg
);
742 uci_add_delta_path(ctx
, ctx
->savedir
);
743 uci_set_savedir(ctx
, optarg
);
744 flags
|= CLI_FLAG_NOCOMMIT
;
747 flags
|= CLI_FLAG_QUIET
;
750 flags
&= ~CLI_FLAG_SHOW_EXT
;
758 argv
[optind
- 1] = argv
[0];
767 ret
= uci_cmd(argc
- 1, argv
+ 1);
774 uci_free_context(ctx
);