f5dfce05187c9a506f91367cab740ab9522946e3
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
;
57 struct uci_type_list
{
60 struct uci_type_list
*next
;
63 static struct uci_type_list
*type_list
= NULL
;
64 static char *typestr
= NULL
;
65 static const char *cur_section_ref
= NULL
;
67 static int uci_cmd(int argc
, char **argv
);
70 uci_reset_typelist(void)
72 struct uci_type_list
*type
;
73 while (type_list
!= NULL
) {
75 type_list
= type_list
->next
;
82 cur_section_ref
= NULL
;
86 uci_lookup_section_ref(struct uci_section
*s
)
88 struct uci_type_list
*ti
= type_list
;
91 if (!s
->anonymous
|| !(flags
& CLI_FLAG_SHOW_EXT
))
94 /* look up in section type list */
96 if (strcmp(ti
->name
, s
->type
) == 0)
101 ti
= malloc(sizeof(struct uci_type_list
));
104 memset(ti
, 0, sizeof(struct uci_type_list
));
105 ti
->next
= type_list
;
110 maxlen
= strlen(s
->type
) + 1 + 2 + 10;
112 typestr
= malloc(maxlen
);
114 typestr
= realloc(typestr
, maxlen
);
118 sprintf(typestr
, "@%s[%d]", ti
->name
, ti
->idx
);
125 static void uci_usage(void)
128 "Usage: %s [<options>] <command> [<arguments>]\n\n"
131 "\texport [<config>]\n"
132 "\timport [<config>]\n"
133 "\tchanges [<config>]\n"
134 "\tcommit [<config>]\n"
135 "\tadd <config> <section-type>\n"
136 "\tadd_list <config>.<section>.<option>=<string>\n"
137 "\tdel_list <config>.<section>.<option>=<string>\n"
138 "\tshow [<config>[.<section>[.<option>]]]\n"
139 "\tget <config>.<section>[.<option>]\n"
140 "\tset <config>.<section>[.<option>]=<value>\n"
141 "\tdelete <config>[.<section[.<option>]]\n"
142 "\trename <config>.<section>[.<option>]=<name>\n"
143 "\trevert <config>[.<section>[.<option>]]\n"
144 "\treorder <config>.<section>=<position>\n"
147 "\t-c <path> set the search path for config files (default: /etc/config)\n"
148 "\t-d <str> set the delimiter for list values in uci show\n"
149 "\t-f <file> use <file> as input instead of stdin\n"
150 "\t-L do not load any plugins\n"
151 "\t-m when importing, merge data into an existing package\n"
152 "\t-n name unnamed sections on export (default)\n"
153 "\t-N don't name unnamed sections\n"
154 "\t-p <path> add a search path for config change files\n"
155 "\t-P <path> add a search path for config change files and use as default\n"
156 "\t-q quiet mode (don't print error messages)\n"
157 "\t-s force strict mode (stop on parser errors, default)\n"
158 "\t-S disable strict mode\n"
159 "\t-X do not use extended syntax on 'show'\n"
165 static void cli_perror(void)
167 if (flags
& CLI_FLAG_QUIET
)
170 uci_perror(ctx
, appname
);
173 static void uci_show_value(struct uci_option
*o
)
175 struct uci_element
*e
;
179 case UCI_TYPE_STRING
:
180 printf("%s\n", o
->v
.string
);
183 uci_foreach_element(&o
->v
.list
, e
) {
184 printf("%s%s", (sep
? delimiter
: ""), e
->name
);
190 printf("<unknown>\n");
195 static void uci_show_option(struct uci_option
*o
)
198 o
->section
->package
->e
.name
,
199 (cur_section_ref
? cur_section_ref
: o
->section
->e
.name
),
204 static void uci_show_section(struct uci_section
*s
)
206 struct uci_element
*e
;
210 cname
= s
->package
->e
.name
;
211 sname
= (cur_section_ref
? cur_section_ref
: s
->e
.name
);
212 printf("%s.%s=%s\n", cname
, sname
, s
->type
);
213 uci_foreach_element(&s
->options
, e
) {
214 uci_show_option(uci_to_option(e
));
218 static void uci_show_package(struct uci_package
*p
)
220 struct uci_element
*e
;
222 uci_reset_typelist();
223 uci_foreach_element( &p
->sections
, e
) {
224 struct uci_section
*s
= uci_to_section(e
);
225 cur_section_ref
= uci_lookup_section_ref(s
);
228 uci_reset_typelist();
231 static void uci_show_changes(struct uci_package
*p
)
233 struct uci_element
*e
;
235 uci_foreach_element(&p
->saved_delta
, e
) {
236 struct uci_delta
*h
= uci_to_delta(e
);
244 case UCI_CMD_LIST_ADD
:
247 case UCI_CMD_LIST_DEL
:
253 printf("%s%s.%s", prefix
, p
->e
.name
, h
->section
);
255 printf(".%s", e
->name
);
256 if (h
->cmd
!= UCI_CMD_REMOVE
)
257 printf("%s%s", op
, h
->value
);
262 static int package_cmd(int cmd
, char *tuple
)
264 struct uci_element
*e
= NULL
;
268 if (uci_lookup_ptr(ctx
, &ptr
, tuple
, true) != UCI_OK
) {
276 uci_show_changes(ptr
.p
);
279 if (flags
& CLI_FLAG_NOCOMMIT
)
281 if (uci_commit(ctx
, &ptr
.p
, false) != UCI_OK
) {
287 uci_export(ctx
, stdout
, ptr
.p
, true);
290 if (!(ptr
.flags
& UCI_LOOKUP_COMPLETE
)) {
291 ctx
->err
= UCI_ERR_NOTFOUND
;
296 case UCI_TYPE_PACKAGE
:
297 uci_show_package(ptr
.p
);
299 case UCI_TYPE_SECTION
:
300 uci_show_section(ptr
.s
);
302 case UCI_TYPE_OPTION
:
303 uci_show_option(ptr
.o
);
306 /* should not happen */
313 uci_unload(ctx
, ptr
.p
);
317 static int uci_do_import(int argc
, char **argv
)
319 struct uci_package
*package
= NULL
;
329 else if (flags
& CLI_FLAG_MERGE
)
330 /* need a package to merge */
333 if (flags
& CLI_FLAG_MERGE
) {
334 if (uci_load(ctx
, name
, &package
) != UCI_OK
)
339 ret
= uci_import(ctx
, input
, name
, &package
, (name
!= NULL
));
342 ret
= uci_save(ctx
, package
);
344 struct uci_element
*e
;
345 /* loop through all config sections and overwrite existing data */
346 uci_foreach_element(&ctx
->root
, e
) {
347 struct uci_package
*p
= uci_to_package(e
);
348 ret
= uci_commit(ctx
, &p
, true);
361 static int uci_do_package_cmd(int cmd
, int argc
, char **argv
)
363 char **configs
= NULL
;
370 return package_cmd(cmd
, argv
[1]);
372 if ((uci_list_configs(ctx
, &configs
) != UCI_OK
) || !configs
) {
377 for (p
= configs
; *p
; p
++) {
378 package_cmd(cmd
, *p
);
384 static int uci_do_add(int argc
, char **argv
)
386 struct uci_package
*p
= NULL
;
387 struct uci_section
*s
= NULL
;
393 ret
= uci_load(ctx
, argv
[1], &p
);
397 ret
= uci_add_section(ctx
, p
, argv
[2], &s
);
401 ret
= uci_save(ctx
, p
);
407 fprintf(stdout
, "%s\n", s
->e
.name
);
412 static int uci_do_section_cmd(int cmd
, int argc
, char **argv
)
414 struct uci_element
*e
;
421 if (uci_lookup_ptr(ctx
, &ptr
, argv
[1], true) != UCI_OK
) {
426 if (ptr
.value
&& (cmd
!= CMD_SET
) &&
427 (cmd
!= CMD_ADD_LIST
) && (cmd
!= CMD_DEL_LIST
) &&
428 (cmd
!= CMD_RENAME
) && (cmd
!= CMD_REORDER
))
434 if (!(ptr
.flags
& UCI_LOOKUP_COMPLETE
)) {
435 ctx
->err
= UCI_ERR_NOTFOUND
;
440 case UCI_TYPE_SECTION
:
441 printf("%s\n", ptr
.s
->type
);
443 case UCI_TYPE_OPTION
:
444 uci_show_value(ptr
.o
);
449 /* throw the value to stdout */
452 ret
= uci_rename(ctx
, &ptr
);
455 ret
= uci_revert(ctx
, &ptr
);
458 ret
= uci_set(ctx
, &ptr
);
461 ret
= uci_add_list(ctx
, &ptr
);
464 ret
= uci_del_list(ctx
, &ptr
);
467 if (!ptr
.s
|| !ptr
.value
) {
468 ctx
->err
= UCI_ERR_NOTFOUND
;
472 ret
= uci_reorder_section(ctx
, ptr
.s
, strtoul(ptr
.value
, NULL
, 10));
475 ret
= uci_delete(ctx
, &ptr
);
479 /* no save necessary for get */
480 if ((cmd
== CMD_GET
) || (cmd
== CMD_REVERT
))
483 /* save changes, but don't commit them yet */
485 ret
= uci_save(ctx
, ptr
.p
);
495 static int uci_batch_cmd(void)
497 char *argv
[MAX_ARGS
+ 2];
502 for(i
= 0; i
<= MAX_ARGS
; i
++) {
504 fprintf(stderr
, "Too many arguments\n");
508 if ((ret
= uci_parse_argument(ctx
, input
, &str
, &argv
[i
])) != UCI_OK
) {
515 argv
[i
] = strdup(argv
[i
]);
524 if (!strcasecmp(argv
[0], "exit"))
526 ret
= uci_cmd(i
, argv
);
530 for (j
= 0; j
< i
; j
++) {
538 static int uci_batch(void)
542 flags
|= CLI_FLAG_BATCH
;
543 while (!feof(input
)) {
544 struct uci_element
*e
, *tmp
;
546 ret
= uci_batch_cmd();
550 fprintf(stderr
, "Unknown command\n");
553 uci_foreach_element_safe(&ctx
->root
, tmp
, e
) {
554 uci_unload(ctx
, uci_to_package(e
));
557 flags
&= ~CLI_FLAG_BATCH
;
562 static int uci_cmd(int argc
, char **argv
)
566 if (!strcasecmp(argv
[0], "batch") && !(flags
& CLI_FLAG_BATCH
))
568 else if (!strcasecmp(argv
[0], "show"))
570 else if (!strcasecmp(argv
[0], "changes"))
572 else if (!strcasecmp(argv
[0], "export"))
574 else if (!strcasecmp(argv
[0], "commit"))
576 else if (!strcasecmp(argv
[0], "get"))
578 else if (!strcasecmp(argv
[0], "set"))
580 else if (!strcasecmp(argv
[0], "ren") ||
581 !strcasecmp(argv
[0], "rename"))
583 else if (!strcasecmp(argv
[0], "revert"))
585 else if (!strcasecmp(argv
[0], "reorder"))
587 else if (!strcasecmp(argv
[0], "del") ||
588 !strcasecmp(argv
[0], "delete"))
590 else if (!strcasecmp(argv
[0], "import"))
592 else if (!strcasecmp(argv
[0], "help"))
594 else if (!strcasecmp(argv
[0], "add"))
596 else if (!strcasecmp(argv
[0], "add_list"))
598 else if (!strcasecmp(argv
[0], "del_list"))
612 return uci_do_section_cmd(cmd
, argc
, argv
);
617 return uci_do_package_cmd(cmd
, argc
, argv
);
619 return uci_do_import(argc
, argv
);
621 return uci_do_add(argc
, argv
);
630 int main(int argc
, char **argv
)
635 flags
= CLI_FLAG_SHOW_EXT
;
638 ctx
= uci_alloc_context();
640 fprintf(stderr
, "Out of memory\n");
644 while((c
= getopt(argc
, argv
, "c:d:f:LmnNp:P:sSqX")) != -1) {
647 uci_set_confdir(ctx
, optarg
);
653 if (input
!= stdin
) {
658 input
= fopen(optarg
, "r");
665 flags
|= CLI_FLAG_NOPLUGINS
;
668 flags
|= CLI_FLAG_MERGE
;
671 ctx
->flags
|= UCI_FLAG_STRICT
;
674 ctx
->flags
&= ~UCI_FLAG_STRICT
;
675 ctx
->flags
|= UCI_FLAG_PERROR
;
678 ctx
->flags
|= UCI_FLAG_EXPORT_NAME
;
681 ctx
->flags
&= ~UCI_FLAG_EXPORT_NAME
;
684 uci_add_delta_path(ctx
, optarg
);
687 uci_add_delta_path(ctx
, ctx
->savedir
);
688 uci_set_savedir(ctx
, optarg
);
689 flags
|= CLI_FLAG_NOCOMMIT
;
692 flags
|= CLI_FLAG_QUIET
;
695 flags
&= ~CLI_FLAG_SHOW_EXT
;
703 argv
[optind
- 1] = argv
[0];
712 if (!(flags
& CLI_FLAG_NOPLUGINS
))
713 uci_load_plugins(ctx
, NULL
);
715 ret
= uci_cmd(argc
- 1, argv
+ 1);
722 uci_free_context(ctx
);