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),
29 CLI_FLAG_SHOW_EXT
= (1 << 4),
30 CLI_FLAG_NOPLUGINS
= (1 << 5),
35 static struct uci_context
*ctx
;
56 struct uci_type_list
{
59 struct uci_type_list
*next
;
62 static struct uci_type_list
*type_list
= NULL
;
63 static char *typestr
= NULL
;
64 static const char *cur_section_ref
= NULL
;
66 static int uci_cmd(int argc
, char **argv
);
69 uci_reset_typelist(void)
71 struct uci_type_list
*type
;
72 while (type_list
!= NULL
) {
74 type_list
= type_list
->next
;
81 cur_section_ref
= NULL
;
85 uci_lookup_section_ref(struct uci_section
*s
)
87 struct uci_type_list
*ti
= type_list
;
90 if (!s
->anonymous
|| !(flags
& CLI_FLAG_SHOW_EXT
))
93 /* look up in section type list */
95 if (strcmp(ti
->name
, s
->type
) == 0)
100 ti
= malloc(sizeof(struct uci_type_list
));
101 memset(ti
, 0, sizeof(struct uci_type_list
));
102 ti
->next
= type_list
;
107 maxlen
= strlen(s
->type
) + 1 + 2 + 10;
109 typestr
= malloc(maxlen
);
111 typestr
= realloc(typestr
, maxlen
);
113 sprintf(typestr
, "@%s[%d]", ti
->name
, ti
->idx
);
118 static void uci_usage(void)
121 "Usage: %s [<options>] <command> [<arguments>]\n\n"
124 "\texport [<config>]\n"
125 "\timport [<config>]\n"
126 "\tchanges [<config>]\n"
127 "\tcommit [<config>]\n"
128 "\tadd <config> <section-type>\n"
129 "\tadd_list <config>.<section>.<option>=<string>\n"
130 "\tshow [<config>[.<section>[.<option>]]]\n"
131 "\tget <config>.<section>[.<option>]\n"
132 "\tset <config>.<section>[.<option>]=<value>\n"
133 "\tdelete <config>[.<section[.<option>]]\n"
134 "\trename <config>.<section>[.<option>]=<name>\n"
135 "\trevert <config>[.<section>[.<option>]]\n"
136 "\treorder <config>.<section>=<position>\n"
139 "\t-c <path> set the search path for config files (default: /etc/config)\n"
140 "\t-d <str> set the delimiter for list values in uci show\n"
141 "\t-f <file> use <file> as input instead of stdin\n"
142 "\t-L do not load any plugins\n"
143 "\t-m when importing, merge data into an existing package\n"
144 "\t-n name unnamed sections on export (default)\n"
145 "\t-N don't name unnamed sections\n"
146 "\t-p <path> add a search path for config change files\n"
147 "\t-P <path> add a search path for config change files and use as default\n"
148 "\t-q quiet mode (don't print error messages)\n"
149 "\t-s force strict mode (stop on parser errors, default)\n"
150 "\t-S disable strict mode\n"
151 "\t-X do not use extended syntax on 'show'\n"
157 static void cli_perror(void)
159 if (flags
& CLI_FLAG_QUIET
)
162 uci_perror(ctx
, appname
);
165 static void uci_show_value(struct uci_option
*o
)
167 struct uci_element
*e
;
171 case UCI_TYPE_STRING
:
172 printf("%s\n", o
->v
.string
);
175 uci_foreach_element(&o
->v
.list
, e
) {
176 printf("%s%s", (sep
? delimiter
: ""), e
->name
);
182 printf("<unknown>\n");
187 static void uci_show_option(struct uci_option
*o
)
190 o
->section
->package
->e
.name
,
191 (cur_section_ref
? cur_section_ref
: o
->section
->e
.name
),
196 static void uci_show_section(struct uci_section
*s
)
198 struct uci_element
*e
;
202 cname
= s
->package
->e
.name
;
203 sname
= (cur_section_ref
? cur_section_ref
: s
->e
.name
);
204 printf("%s.%s=%s\n", cname
, sname
, s
->type
);
205 uci_foreach_element(&s
->options
, e
) {
206 uci_show_option(uci_to_option(e
));
210 static void uci_show_package(struct uci_package
*p
)
212 struct uci_element
*e
;
214 uci_reset_typelist();
215 uci_foreach_element( &p
->sections
, e
) {
216 struct uci_section
*s
= uci_to_section(e
);
217 cur_section_ref
= uci_lookup_section_ref(s
);
220 uci_reset_typelist();
223 static void uci_show_changes(struct uci_package
*p
)
225 struct uci_element
*e
;
227 uci_foreach_element(&p
->saved_history
, e
) {
228 struct uci_history
*h
= uci_to_history(e
);
236 case UCI_CMD_LIST_ADD
:
242 printf("%s%s.%s", prefix
, p
->e
.name
, h
->section
);
244 printf(".%s", e
->name
);
245 if (h
->cmd
!= UCI_CMD_REMOVE
)
246 printf("%s%s", op
, h
->value
);
251 static int package_cmd(int cmd
, char *tuple
)
253 struct uci_element
*e
= NULL
;
256 if (uci_lookup_ptr(ctx
, &ptr
, tuple
, true) != UCI_OK
) {
264 uci_show_changes(ptr
.p
);
267 if (flags
& CLI_FLAG_NOCOMMIT
)
269 if (uci_commit(ctx
, &ptr
.p
, false) != UCI_OK
)
273 uci_export(ctx
, stdout
, ptr
.p
, true);
276 if (!(ptr
.flags
& UCI_LOOKUP_COMPLETE
)) {
277 ctx
->err
= UCI_ERR_NOTFOUND
;
282 case UCI_TYPE_PACKAGE
:
283 uci_show_package(ptr
.p
);
285 case UCI_TYPE_SECTION
:
286 uci_show_section(ptr
.s
);
288 case UCI_TYPE_OPTION
:
289 uci_show_option(ptr
.o
);
292 /* should not happen */
298 uci_unload(ctx
, ptr
.p
);
302 static int uci_do_import(int argc
, char **argv
)
304 struct uci_package
*package
= NULL
;
314 else if (flags
& CLI_FLAG_MERGE
)
315 /* need a package to merge */
318 if (flags
& CLI_FLAG_MERGE
) {
319 if (uci_load(ctx
, name
, &package
) != UCI_OK
)
324 ret
= uci_import(ctx
, input
, name
, &package
, (name
!= NULL
));
327 ret
= uci_save(ctx
, package
);
329 struct uci_element
*e
;
330 /* loop through all config sections and overwrite existing data */
331 uci_foreach_element(&ctx
->root
, e
) {
332 struct uci_package
*p
= uci_to_package(e
);
333 ret
= uci_commit(ctx
, &p
, true);
346 static int uci_do_package_cmd(int cmd
, int argc
, char **argv
)
348 char **configs
= NULL
;
355 return package_cmd(cmd
, argv
[1]);
357 if ((uci_list_configs(ctx
, &configs
) != UCI_OK
) || !configs
) {
362 for (p
= configs
; *p
; p
++) {
363 package_cmd(cmd
, *p
);
369 static int uci_do_add(int argc
, char **argv
)
371 struct uci_package
*p
= NULL
;
372 struct uci_section
*s
= NULL
;
378 ret
= uci_load(ctx
, argv
[1], &p
);
382 ret
= uci_add_section(ctx
, p
, argv
[2], &s
);
386 ret
= uci_save(ctx
, p
);
392 fprintf(stdout
, "%s\n", s
->e
.name
);
397 static int uci_do_section_cmd(int cmd
, int argc
, char **argv
)
399 struct uci_element
*e
;
406 if (uci_lookup_ptr(ctx
, &ptr
, argv
[1], true) != UCI_OK
) {
411 if (ptr
.value
&& (cmd
!= CMD_SET
) && (cmd
!= CMD_ADD_LIST
) && (cmd
!= CMD_RENAME
) && (cmd
!= CMD_REORDER
))
417 if (!(ptr
.flags
& UCI_LOOKUP_COMPLETE
)) {
418 ctx
->err
= UCI_ERR_NOTFOUND
;
423 case UCI_TYPE_SECTION
:
424 printf("%s\n", ptr
.s
->type
);
426 case UCI_TYPE_OPTION
:
427 uci_show_value(ptr
.o
);
432 /* throw the value to stdout */
435 ret
= uci_rename(ctx
, &ptr
);
438 ret
= uci_revert(ctx
, &ptr
);
441 ret
= uci_set(ctx
, &ptr
);
444 ret
= uci_add_list(ctx
, &ptr
);
448 ctx
->err
= UCI_ERR_NOTFOUND
;
452 ret
= uci_reorder_section(ctx
, ptr
.s
, strtoul(ptr
.value
, NULL
, 10));
455 ret
= uci_delete(ctx
, &ptr
);
459 /* no save necessary for get */
460 if ((cmd
== CMD_GET
) || (cmd
== CMD_REVERT
))
463 /* save changes, but don't commit them yet */
465 ret
= uci_save(ctx
, ptr
.p
);
475 static int uci_batch_cmd(void)
477 char *argv
[MAX_ARGS
+ 2];
482 for(i
= 0; i
<= MAX_ARGS
; i
++) {
484 fprintf(stderr
, "Too many arguments\n");
488 if ((ret
= uci_parse_argument(ctx
, input
, &str
, &argv
[i
])) != UCI_OK
) {
495 argv
[i
] = strdup(argv
[i
]);
504 if (!strcasecmp(argv
[0], "exit"))
506 ret
= uci_cmd(i
, argv
);
510 for (j
= 0; j
< i
; j
++) {
518 static int uci_batch(void)
522 flags
|= CLI_FLAG_BATCH
;
523 while (!feof(input
)) {
524 struct uci_element
*e
, *tmp
;
526 ret
= uci_batch_cmd();
530 fprintf(stderr
, "Unknown command\n");
533 uci_foreach_element_safe(&ctx
->root
, tmp
, e
) {
534 uci_unload(ctx
, uci_to_package(e
));
537 flags
&= ~CLI_FLAG_BATCH
;
542 static int uci_cmd(int argc
, char **argv
)
546 if (!strcasecmp(argv
[0], "batch") && !(flags
& CLI_FLAG_BATCH
))
548 else if (!strcasecmp(argv
[0], "show"))
550 else if (!strcasecmp(argv
[0], "changes"))
552 else if (!strcasecmp(argv
[0], "export"))
554 else if (!strcasecmp(argv
[0], "commit"))
556 else if (!strcasecmp(argv
[0], "get"))
558 else if (!strcasecmp(argv
[0], "set"))
560 else if (!strcasecmp(argv
[0], "ren") ||
561 !strcasecmp(argv
[0], "rename"))
563 else if (!strcasecmp(argv
[0], "revert"))
565 else if (!strcasecmp(argv
[0], "reorder"))
567 else if (!strcasecmp(argv
[0], "del") ||
568 !strcasecmp(argv
[0], "delete"))
570 else if (!strcasecmp(argv
[0], "import"))
572 else if (!strcasecmp(argv
[0], "help"))
574 else if (!strcasecmp(argv
[0], "add"))
576 else if (!strcasecmp(argv
[0], "add_list"))
589 return uci_do_section_cmd(cmd
, argc
, argv
);
594 return uci_do_package_cmd(cmd
, argc
, argv
);
596 return uci_do_import(argc
, argv
);
598 return uci_do_add(argc
, argv
);
607 int main(int argc
, char **argv
)
612 flags
= CLI_FLAG_SHOW_EXT
;
615 ctx
= uci_alloc_context();
617 fprintf(stderr
, "Out of memory\n");
621 while((c
= getopt(argc
, argv
, "c:d:f:LmnNp:P:sSqX")) != -1) {
624 uci_set_confdir(ctx
, optarg
);
630 input
= fopen(optarg
, "r");
637 flags
|= CLI_FLAG_NOPLUGINS
;
640 flags
|= CLI_FLAG_MERGE
;
643 ctx
->flags
|= UCI_FLAG_STRICT
;
646 ctx
->flags
&= ~UCI_FLAG_STRICT
;
647 ctx
->flags
|= UCI_FLAG_PERROR
;
650 ctx
->flags
|= UCI_FLAG_EXPORT_NAME
;
653 ctx
->flags
&= ~UCI_FLAG_EXPORT_NAME
;
656 uci_add_history_path(ctx
, optarg
);
659 uci_add_history_path(ctx
, ctx
->savedir
);
660 uci_set_savedir(ctx
, optarg
);
661 flags
|= CLI_FLAG_NOCOMMIT
;
664 flags
|= CLI_FLAG_QUIET
;
667 flags
&= ~CLI_FLAG_SHOW_EXT
;
675 argv
[optind
- 1] = argv
[0];
684 if (!(flags
& CLI_FLAG_NOPLUGINS
))
685 uci_load_plugins(ctx
, NULL
);
687 ret
= uci_cmd(argc
- 1, argv
+ 1);
695 uci_free_context(ctx
);