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 *appname
;
24 CLI_FLAG_MERGE
= (1 << 0),
25 CLI_FLAG_QUIET
= (1 << 1),
26 CLI_FLAG_NOCOMMIT
= (1 << 2),
27 CLI_FLAG_BATCH
= (1 << 3),
32 static struct uci_context
*ctx
;
51 static int uci_cmd(int argc
, char **argv
);
53 static void uci_usage(void)
56 "Usage: %s [<options>] <command> [<arguments>]\n\n"
59 "\texport [<config>]\n"
60 "\timport [<config>]\n"
61 "\tchanges [<config>]\n"
62 "\tcommit [<config>]\n"
63 "\tadd <config> <section-type>\n"
64 "\tshow [<config>[.<section>[.<option>]]]\n"
65 "\tget <config>.<section>[.<option>]\n"
66 "\tset <config>.<section>[.<option>]=<value>\n"
67 "\trename <config>.<section>[.<option>]=<name>\n"
68 "\trevert <config>[.<section>[.<option>]]\n"
71 "\t-c <path> set the search path for config files (default: /etc/config)\n"
72 "\t-f <file> use <file> as input instead of stdin\n"
73 "\t-m when importing, merge data into an existing package\n"
74 "\t-n name unnamed sections on export (default)\n"
75 "\t-N don't name unnamed sections\n"
76 "\t-p <path> add a search path for config change files\n"
77 "\t-P <path> add a search path for config change files and use as default\n"
78 "\t-q quiet mode (don't print error messages)\n"
79 "\t-s force strict mode (stop on parser errors, default)\n"
80 "\t-S disable strict mode\n"
86 static void cli_perror(void)
88 if (flags
& CLI_FLAG_QUIET
)
91 uci_perror(ctx
, appname
);
94 static void uci_show_option(struct uci_option
*o
)
96 printf("%s.%s.%s=%s\n",
97 o
->section
->package
->e
.name
,
103 static void uci_show_section(struct uci_section
*p
)
105 struct uci_element
*e
;
106 const char *cname
, *sname
;
108 cname
= p
->package
->e
.name
;
110 printf("%s.%s=%s\n", cname
, sname
, p
->type
);
111 uci_foreach_element(&p
->options
, e
) {
112 uci_show_option(uci_to_option(e
));
116 static void uci_show_package(struct uci_package
*p
)
118 struct uci_element
*e
;
120 uci_foreach_element( &p
->sections
, e
) {
121 uci_show_section(uci_to_section(e
));
125 static void uci_show_changes(struct uci_package
*p
)
127 struct uci_element
*e
;
129 uci_foreach_element(&p
->saved_history
, e
) {
130 struct uci_history
*h
= uci_to_history(e
);
132 if (h
->cmd
== UCI_CMD_REMOVE
)
134 printf("%s.%s", p
->e
.name
, h
->section
);
136 printf(".%s", e
->name
);
137 if (h
->cmd
!= UCI_CMD_REMOVE
)
138 printf("=%s", h
->value
);
143 static int package_cmd(int cmd
, char *tuple
)
145 struct uci_package
*p
;
146 struct uci_section
*s
;
147 struct uci_element
*e
= NULL
;
149 if (uci_lookup_ext(ctx
, &e
, tuple
) != UCI_OK
) {
154 case UCI_TYPE_PACKAGE
:
155 p
= uci_to_package(e
);
157 case UCI_TYPE_SECTION
:
158 s
= uci_to_section(e
);
161 case UCI_TYPE_OPTION
:
162 s
= uci_to_option(e
)->section
;
174 if (flags
& CLI_FLAG_NOCOMMIT
)
176 if (uci_commit(ctx
, &p
, false) != UCI_OK
)
180 uci_export(ctx
, stdout
, p
, true);
184 case UCI_TYPE_PACKAGE
:
187 case UCI_TYPE_SECTION
:
188 uci_show_section(uci_to_section(e
));
190 case UCI_TYPE_OPTION
:
191 uci_show_option(uci_to_option(e
));
194 /* should not happen */
204 static int uci_do_import(int argc
, char **argv
)
206 struct uci_package
*package
= NULL
;
216 else if (flags
& CLI_FLAG_MERGE
)
217 /* need a package to merge */
220 if (flags
& CLI_FLAG_MERGE
) {
221 if (uci_load(ctx
, name
, &package
) != UCI_OK
)
226 ret
= uci_import(ctx
, input
, name
, &package
, (name
!= NULL
));
229 ret
= uci_save(ctx
, package
);
231 struct uci_element
*e
;
232 /* loop through all config sections and overwrite existing data */
233 uci_foreach_element(&ctx
->root
, e
) {
234 struct uci_package
*p
= uci_to_package(e
);
235 ret
= uci_commit(ctx
, &p
, true);
248 static int uci_do_package_cmd(int cmd
, int argc
, char **argv
)
250 char **configs
= NULL
;
257 return package_cmd(cmd
, argv
[1]);
259 if ((uci_list_configs(ctx
, &configs
) != UCI_OK
) || !configs
) {
264 for (p
= configs
; *p
; p
++) {
265 package_cmd(cmd
, *p
);
271 static int uci_do_add(int argc
, char **argv
)
273 struct uci_package
*p
= NULL
;
274 struct uci_section
*s
= NULL
;
280 ret
= uci_load(ctx
, argv
[1], &p
);
284 ret
= uci_add_section(ctx
, p
, argv
[2], &s
);
288 ret
= uci_save(ctx
, p
);
294 fprintf(stdout
, "%s\n", s
->e
.name
);
299 static int uci_do_section_cmd(int cmd
, int argc
, char **argv
)
301 struct uci_package
*p
= NULL
;
302 struct uci_section
*s
= NULL
;
303 struct uci_element
*e
= NULL
;
304 char *section
= NULL
;
312 value
= strchr(argv
[1], '=');
316 if (!uci_validate_text(value
))
320 if (value
&& (cmd
!= CMD_SET
) && (cmd
!= CMD_RENAME
))
323 if (uci_lookup_ext(ctx
, &e
, argv
[1]) != UCI_OK
) {
329 case UCI_TYPE_SECTION
:
330 s
= uci_to_section(e
);
332 case UCI_TYPE_OPTION
:
334 s
= uci_to_option(e
)->section
;
345 case UCI_TYPE_SECTION
:
348 case UCI_TYPE_OPTION
:
349 value
= uci_to_option(e
)->value
;
354 /* throw the value to stdout */
355 printf("%s\n", value
);
358 ret
= uci_rename(ctx
, p
, section
, option
, value
);
361 ret
= uci_revert(ctx
, &p
, section
, option
);
364 ret
= uci_set(ctx
, p
, section
, option
, value
, NULL
);
367 ret
= uci_delete(ctx
, p
, section
, option
);
371 /* no save necessary for get */
372 if ((cmd
== CMD_GET
) || (cmd
== CMD_REVERT
))
375 /* save changes, but don't commit them yet */
377 ret
= uci_save(ctx
, p
);
387 static int uci_batch_cmd(void)
389 char *argv
[MAX_ARGS
];
394 for(i
= 0; i
<= MAX_ARGS
; i
++) {
396 fprintf(stderr
, "Too many arguments\n");
400 if ((ret
= uci_parse_argument(ctx
, input
, &str
, &argv
[i
])) != UCI_OK
) {
407 argv
[i
] = strdup(argv
[i
]);
416 if (!strcasecmp(argv
[0], "exit"))
418 ret
= uci_cmd(i
, argv
);
422 for (j
= 0; j
< i
; j
++) {
430 static int uci_batch(void)
434 while (!feof(input
)) {
435 struct uci_element
*e
, *tmp
;
437 ret
= uci_batch_cmd();
441 fprintf(stderr
, "Unknown command\n");
444 uci_foreach_element_safe(&ctx
->root
, tmp
, e
) {
445 uci_unload(ctx
, uci_to_package(e
));
451 static int uci_cmd(int argc
, char **argv
)
455 if (!strcasecmp(argv
[0], "batch") && !(flags
& CLI_FLAG_BATCH
))
457 else if (!strcasecmp(argv
[0], "show"))
459 else if (!strcasecmp(argv
[0], "changes"))
461 else if (!strcasecmp(argv
[0], "export"))
463 else if (!strcasecmp(argv
[0], "commit"))
465 else if (!strcasecmp(argv
[0], "get"))
467 else if (!strcasecmp(argv
[0], "set"))
469 else if (!strcasecmp(argv
[0], "ren") ||
470 !strcasecmp(argv
[0], "rename"))
472 else if (!strcasecmp(argv
[0], "revert"))
474 else if (!strcasecmp(argv
[0], "del"))
476 else if (!strcasecmp(argv
[0], "import"))
478 else if (!strcasecmp(argv
[0], "help"))
480 else if (!strcasecmp(argv
[0], "add"))
491 return uci_do_section_cmd(cmd
, argc
, argv
);
496 return uci_do_package_cmd(cmd
, argc
, argv
);
498 return uci_do_import(argc
, argv
);
500 return uci_do_add(argc
, argv
);
509 int main(int argc
, char **argv
)
516 ctx
= uci_alloc_context();
518 fprintf(stderr
, "Out of memory\n");
522 while((c
= getopt(argc
, argv
, "c:f:mnNp:P:sSq")) != -1) {
525 uci_set_confdir(ctx
, optarg
);
528 input
= fopen(optarg
, "r");
535 flags
|= CLI_FLAG_MERGE
;
538 ctx
->flags
|= UCI_FLAG_STRICT
;
541 ctx
->flags
&= ~UCI_FLAG_STRICT
;
542 ctx
->flags
|= UCI_FLAG_PERROR
;
545 ctx
->flags
|= UCI_FLAG_EXPORT_NAME
;
548 ctx
->flags
&= ~UCI_FLAG_EXPORT_NAME
;
551 uci_add_history_path(ctx
, optarg
);
554 uci_add_history_path(ctx
, ctx
->savedir
);
555 uci_set_savedir(ctx
, optarg
);
556 flags
|= CLI_FLAG_NOCOMMIT
;
559 flags
|= CLI_FLAG_QUIET
;
567 argv
[optind
- 1] = argv
[0];
575 ret
= uci_cmd(argc
- 1, argv
+ 1);
583 uci_free_context(ctx
);