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
);
119 void *p
= realloc(typestr
, maxlen
);
129 sprintf(typestr
, "@%s[%d]", ti
->name
, ti
->idx
);
141 static void uci_usage(void)
144 "Usage: %s [<options>] <command> [<arguments>]\n\n"
147 "\texport [<config>]\n"
148 "\timport [<config>]\n"
149 "\tchanges [<config>]\n"
150 "\tcommit [<config>]\n"
151 "\tadd <config> <section-type>\n"
152 "\tadd_list <config>.<section>.<option>=<string>\n"
153 "\tdel_list <config>.<section>.<option>=<string>\n"
154 "\tshow [<config>[.<section>[.<option>]]]\n"
155 "\tget <config>.<section>[.<option>]\n"
156 "\tset <config>.<section>[.<option>]=<value>\n"
157 "\tdelete <config>[.<section>[[.<option>][=<id>]]]\n"
158 "\trename <config>.<section>[.<option>]=<name>\n"
159 "\trevert <config>[.<section>[.<option>]]\n"
160 "\treorder <config>.<section>=<position>\n"
163 "\t-c <path> set the search path for config files (default: /etc/config)\n"
164 "\t-d <str> set the delimiter for list values in uci show\n"
165 "\t-f <file> use <file> as input instead of stdin\n"
166 "\t-m when importing, merge data into an existing package\n"
167 "\t-n name unnamed sections on export (default)\n"
168 "\t-N don't name unnamed sections\n"
169 "\t-p <path> add a search path for config change files\n"
170 "\t-P <path> add a search path for config change files and use as default\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 static void cli_error(const char *fmt
, ...)
192 if (flags
& CLI_FLAG_QUIET
)
196 vfprintf(stderr
, fmt
, ap
);
200 static void uci_print_value(FILE *f
, const char *v
)
213 static void uci_show_value(struct uci_option
*o
, bool quote
)
215 struct uci_element
*e
;
220 case UCI_TYPE_STRING
:
222 uci_print_value(stdout
, o
->v
.string
);
224 printf("%s", o
->v
.string
);
228 uci_foreach_element(&o
->v
.list
, e
) {
229 printf("%s", (sep
? delimiter
: ""));
230 space
= strpbrk(e
->name
, " \t\r\n");
231 if (!space
&& !quote
)
232 printf("%s", e
->name
);
234 uci_print_value(stdout
, e
->name
);
240 printf("<unknown>\n");
245 static void uci_show_option(struct uci_option
*o
, bool quote
)
248 o
->section
->package
->e
.name
,
249 (cur_section_ref
? cur_section_ref
: o
->section
->e
.name
),
251 uci_show_value(o
, quote
);
254 static void uci_show_section(struct uci_section
*s
)
256 struct uci_element
*e
;
260 cname
= s
->package
->e
.name
;
261 sname
= (cur_section_ref
? cur_section_ref
: s
->e
.name
);
262 printf("%s.%s=%s\n", cname
, sname
, s
->type
);
263 uci_foreach_element(&s
->options
, e
) {
264 uci_show_option(uci_to_option(e
), true);
268 static void uci_show_package(struct uci_package
*p
)
270 struct uci_element
*e
;
272 uci_reset_typelist();
273 uci_foreach_element( &p
->sections
, e
) {
274 struct uci_section
*s
= uci_to_section(e
);
275 cur_section_ref
= uci_lookup_section_ref(s
);
278 uci_reset_typelist();
281 static void uci_show_changes(struct uci_package
*p
)
283 struct uci_element
*e
;
285 uci_foreach_element(&p
->saved_delta
, e
) {
286 struct uci_delta
*h
= uci_to_delta(e
);
294 case UCI_CMD_LIST_ADD
:
297 case UCI_CMD_LIST_DEL
:
303 printf("%s%s.%s", prefix
, p
->e
.name
, h
->section
);
305 printf(".%s", e
->name
);
306 if (h
->cmd
!= UCI_CMD_REMOVE
) {
308 uci_print_value(stdout
, h
->value
);
314 static int package_cmd(int cmd
, char *tuple
)
316 struct uci_element
*e
= NULL
;
320 if (uci_lookup_ptr(ctx
, &ptr
, tuple
, true) != UCI_OK
) {
328 uci_show_changes(ptr
.p
);
331 if (flags
& CLI_FLAG_NOCOMMIT
) {
335 if (uci_commit(ctx
, &ptr
.p
, false) != UCI_OK
) {
341 if (uci_export(ctx
, stdout
, ptr
.p
, true) != UCI_OK
) {
346 if (!(ptr
.flags
& UCI_LOOKUP_COMPLETE
)) {
347 ctx
->err
= UCI_ERR_NOTFOUND
;
352 case UCI_TYPE_PACKAGE
:
353 uci_show_package(ptr
.p
);
355 case UCI_TYPE_SECTION
:
356 uci_show_section(ptr
.s
);
358 case UCI_TYPE_OPTION
:
359 uci_show_option(ptr
.o
, true);
362 /* should not happen */
372 uci_unload(ctx
, ptr
.p
);
376 static int uci_do_import(int argc
, char **argv
)
378 struct uci_package
*package
= NULL
;
388 else if (flags
& CLI_FLAG_MERGE
)
389 /* need a package to merge */
392 if (flags
& CLI_FLAG_MERGE
) {
393 if (uci_load(ctx
, name
, &package
) != UCI_OK
)
398 ret
= uci_import(ctx
, input
, name
, &package
, (name
!= NULL
));
401 ret
= uci_save(ctx
, package
);
403 struct uci_element
*e
;
404 /* loop through all config sections and overwrite existing data */
405 uci_foreach_element(&ctx
->root
, e
) {
406 struct uci_package
*p
= uci_to_package(e
);
407 ret
= uci_commit(ctx
, &p
, true);
420 static int uci_do_package_cmd(int cmd
, int argc
, char **argv
)
422 char **configs
= NULL
;
430 return package_cmd(cmd
, argv
[1]);
432 if ((uci_list_configs(ctx
, &configs
) != UCI_OK
) || !configs
) {
437 for (p
= configs
; *p
; p
++) {
438 package_cmd(cmd
, *p
);
447 static int uci_do_add(int argc
, char **argv
)
449 struct uci_package
*p
= NULL
;
450 struct uci_section
*s
= NULL
;
456 ret
= uci_load(ctx
, argv
[1], &p
);
460 ret
= uci_add_section(ctx
, p
, argv
[2], &s
);
464 ret
= uci_save(ctx
, p
);
470 fprintf(stdout
, "%s\n", s
->e
.name
);
475 static int uci_do_section_cmd(int cmd
, int argc
, char **argv
)
477 struct uci_element
*e
;
485 if (uci_lookup_ptr(ctx
, &ptr
, argv
[1], true) != UCI_OK
) {
490 if (ptr
.value
&& (cmd
!= CMD_SET
) && (cmd
!= CMD_DEL
) &&
491 (cmd
!= CMD_ADD_LIST
) && (cmd
!= CMD_DEL_LIST
) &&
492 (cmd
!= CMD_RENAME
) && (cmd
!= CMD_REORDER
))
498 if (!(ptr
.flags
& UCI_LOOKUP_COMPLETE
)) {
499 ctx
->err
= UCI_ERR_NOTFOUND
;
504 case UCI_TYPE_SECTION
:
505 printf("%s\n", ptr
.s
->type
);
507 case UCI_TYPE_OPTION
:
508 uci_show_value(ptr
.o
, false);
513 /* throw the value to stdout */
516 ret
= uci_rename(ctx
, &ptr
);
519 ret
= uci_revert(ctx
, &ptr
);
522 ret
= uci_set(ctx
, &ptr
);
525 ret
= uci_add_list(ctx
, &ptr
);
528 ret
= uci_del_list(ctx
, &ptr
);
531 if (!ptr
.s
|| !ptr
.value
) {
532 ctx
->err
= UCI_ERR_NOTFOUND
;
536 ret
= uci_reorder_section(ctx
, ptr
.s
, strtoul(ptr
.value
, NULL
, 10));
539 if (ptr
.value
&& !sscanf(ptr
.value
, "%d", &dummy
))
541 ret
= uci_delete(ctx
, &ptr
);
545 /* no save necessary for get */
546 if ((cmd
== CMD_GET
) || (cmd
== CMD_REVERT
))
549 /* save changes, but don't commit them yet */
551 ret
= uci_save(ctx
, ptr
.p
);
561 static int uci_batch_cmd(void)
563 char *argv
[MAX_ARGS
+ 2];
568 for(i
= 0; i
<= MAX_ARGS
; i
++) {
570 cli_error("Too many arguments\n");
574 if ((ret
= uci_parse_argument(ctx
, input
, &str
, &argv
[i
])) != UCI_OK
) {
581 argv
[i
] = strdup(argv
[i
]);
583 cli_error("uci: %s", strerror(errno
));
590 if (!strcasecmp(argv
[0], "exit"))
592 ret
= uci_cmd(i
, argv
);
596 for (j
= 0; j
< i
; j
++) {
603 static int uci_batch(void)
607 flags
|= CLI_FLAG_BATCH
;
608 while (!feof(input
)) {
609 struct uci_element
*e
, *tmp
;
611 ret
= uci_batch_cmd();
615 cli_error("Unknown command\n");
618 uci_foreach_element_safe(&ctx
->root
, tmp
, e
) {
619 uci_unload(ctx
, uci_to_package(e
));
622 flags
&= ~CLI_FLAG_BATCH
;
627 static int uci_cmd(int argc
, char **argv
)
631 if (!strcasecmp(argv
[0], "batch") && !(flags
& CLI_FLAG_BATCH
))
633 else if (!strcasecmp(argv
[0], "show"))
635 else if (!strcasecmp(argv
[0], "changes"))
637 else if (!strcasecmp(argv
[0], "export"))
639 else if (!strcasecmp(argv
[0], "commit"))
641 else if (!strcasecmp(argv
[0], "get"))
643 else if (!strcasecmp(argv
[0], "set"))
645 else if (!strcasecmp(argv
[0], "ren") ||
646 !strcasecmp(argv
[0], "rename"))
648 else if (!strcasecmp(argv
[0], "revert"))
650 else if (!strcasecmp(argv
[0], "reorder"))
652 else if (!strcasecmp(argv
[0], "del") ||
653 !strcasecmp(argv
[0], "delete"))
655 else if (!strcasecmp(argv
[0], "import"))
657 else if (!strcasecmp(argv
[0], "help"))
659 else if (!strcasecmp(argv
[0], "add"))
661 else if (!strcasecmp(argv
[0], "add_list"))
663 else if (!strcasecmp(argv
[0], "del_list"))
677 return uci_do_section_cmd(cmd
, argc
, argv
);
682 return uci_do_package_cmd(cmd
, argc
, argv
);
684 return uci_do_import(argc
, argv
);
686 return uci_do_add(argc
, argv
);
695 int main(int argc
, char **argv
)
700 flags
= CLI_FLAG_SHOW_EXT
;
703 ctx
= uci_alloc_context();
705 cli_error("Out of memory\n");
709 while((c
= getopt(argc
, argv
, "c:d:f:LmnNp:P:sSqX")) != -1) {
712 uci_set_confdir(ctx
, optarg
);
718 if (input
!= stdin
) {
720 cli_error("Too many input files.\n");
724 input
= fopen(optarg
, "r");
726 cli_error("uci: %s", strerror(errno
));
731 flags
|= CLI_FLAG_MERGE
;
734 ctx
->flags
|= UCI_FLAG_STRICT
;
737 ctx
->flags
&= ~UCI_FLAG_STRICT
;
738 ctx
->flags
|= UCI_FLAG_PERROR
;
741 ctx
->flags
|= UCI_FLAG_EXPORT_NAME
;
744 ctx
->flags
&= ~UCI_FLAG_EXPORT_NAME
;
747 uci_add_delta_path(ctx
, optarg
);
750 uci_add_delta_path(ctx
, ctx
->savedir
);
751 uci_set_savedir(ctx
, optarg
);
752 flags
|= CLI_FLAG_NOCOMMIT
;
755 flags
|= CLI_FLAG_QUIET
;
758 flags
&= ~CLI_FLAG_SHOW_EXT
;
766 argv
[optind
- 1] = argv
[0];
775 ret
= uci_cmd(argc
- 1, argv
+ 1);
782 uci_free_context(ctx
);