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.
19 static const char *appname
= "uci";
21 CLI_FLAG_MERGE
= (1 << 0),
22 CLI_FLAG_QUIET
= (1 << 1),
23 CLI_FLAG_NOCOMMIT
= (1 << 2),
27 static struct uci_context
*ctx
;
42 static void uci_usage(int argc
, char **argv
)
45 "Usage: %s [<options>] <command> [<arguments>]\n\n"
47 "\texport [<config>]\n"
48 "\timport [<config>]\n"
49 "\tshow [<config>[.<section>[.<option>]]]\n"
50 "\tget <config>.<section>[.<option>]\n"
51 "\tset <config>.<section>[.<option>]=<value>\n"
52 "\trename <config>.<section>[.<option>]=<name>\n"
53 "\trevert <config>[.<section>[.<option>]]\n"
56 "\t-f <file> use <file> as input instead of stdin\n"
57 "\t-m when importing, merge data into an existing package\n"
58 "\t-n name unnamed sections on export (default)\n"
59 "\t-N don't name unnamed sections\n"
60 "\t-p <path> add a search path for config change files\n"
61 "\t-P <path> add a search path for config change files and use as default\n"
62 "\t-q quiet mode (don't print error messages)\n"
63 "\t-s force strict mode (stop on parser errors, default)\n"
64 "\t-S disable strict mode\n"
71 static void cli_perror(void)
73 if (flags
& CLI_FLAG_QUIET
)
76 uci_perror(ctx
, appname
);
79 static void uci_show_section(struct uci_section
*p
)
81 struct uci_element
*e
;
82 const char *cname
, *sname
;
84 cname
= p
->package
->e
.name
;
86 printf("%s.%s=%s\n", cname
, sname
, p
->type
);
87 uci_foreach_element(&p
->options
, e
) {
88 printf("%s.%s.%s=%s\n", cname
, sname
, e
->name
, uci_to_option(e
)->value
);
92 static void uci_show_package(struct uci_package
*p
)
94 struct uci_element
*e
;
96 uci_foreach_element( &p
->sections
, e
) {
97 uci_show_section(uci_to_section(e
));
102 static int package_cmd(int cmd
, char *package
)
104 struct uci_package
*p
= NULL
;
106 if (uci_load(ctx
, package
, &p
) != UCI_OK
) {
114 if (flags
& CLI_FLAG_NOCOMMIT
)
116 if (uci_commit(ctx
, &p
, false) != UCI_OK
)
120 uci_export(ctx
, stdout
, p
, true);
131 static int uci_do_import(int argc
, char **argv
)
133 struct uci_package
*package
= NULL
;
142 else if (flags
& CLI_FLAG_MERGE
)
143 /* need a package to merge */
146 if (flags
& CLI_FLAG_MERGE
) {
147 if (uci_load(ctx
, name
, &package
) != UCI_OK
)
150 ret
= uci_import(ctx
, input
, name
, &package
, (name
!= NULL
));
152 if (flags
& CLI_FLAG_MERGE
) {
153 ret
= uci_save(ctx
, package
);
155 struct uci_element
*e
;
156 /* loop through all config sections and overwrite existing data */
157 uci_foreach_element(&ctx
->root
, e
) {
158 struct uci_package
*p
= uci_to_package(e
);
159 ret
= uci_commit(ctx
, &p
, true);
172 static int uci_do_package_cmd(int cmd
, int argc
, char **argv
)
174 char **configs
= NULL
;
181 return package_cmd(cmd
, argv
[1]);
183 if ((uci_list_configs(ctx
, &configs
) != UCI_OK
) || !configs
) {
188 for (p
= configs
; *p
; p
++) {
189 package_cmd(cmd
, *p
);
196 static int uci_do_section_cmd(int cmd
, int argc
, char **argv
)
198 struct uci_package
*p
= NULL
;
199 struct uci_element
*e
= NULL
;
200 char *package
= NULL
;
201 char *section
= NULL
;
218 if (uci_parse_tuple(ctx
, argv
[1], &package
, §ion
, &option
, ptr
) != UCI_OK
)
221 if (uci_load(ctx
, package
, &p
) != UCI_OK
) {
230 if (uci_lookup(ctx
, &e
, p
, section
, option
) != UCI_OK
)
234 case UCI_TYPE_SECTION
:
235 value
= uci_to_section(e
)->type
;
237 case UCI_TYPE_OPTION
:
238 value
= uci_to_option(e
)->value
;
241 /* should not happen */
244 /* throw the value to stdout */
245 printf("%s\n", value
);
248 ret
= uci_rename(ctx
, p
, section
, option
, value
);
251 ret
= uci_revert(ctx
, &p
, section
, option
);
254 ret
= uci_set(ctx
, p
, section
, option
, value
);
257 ret
= uci_delete(ctx
, p
, section
, option
);
261 /* no save necessary for get */
262 if ((cmd
== CMD_GET
) || (cmd
== CMD_REVERT
))
265 /* save changes, but don't commit them yet */
267 ret
= uci_save(ctx
, p
);
277 static int uci_cmd(int argc
, char **argv
)
281 if (!strcasecmp(argv
[0], "show"))
283 else if (!strcasecmp(argv
[0], "export"))
285 else if (!strcasecmp(argv
[0], "commit"))
287 else if (!strcasecmp(argv
[0], "get"))
289 else if (!strcasecmp(argv
[0], "set"))
291 else if (!strcasecmp(argv
[0], "ren") ||
292 !strcasecmp(argv
[0], "rename"))
294 else if (!strcasecmp(argv
[0], "revert"))
296 else if (!strcasecmp(argv
[0], "del"))
298 else if (!strcasecmp(argv
[0], "import"))
309 return uci_do_section_cmd(cmd
, argc
, argv
);
313 return uci_do_package_cmd(cmd
, argc
, argv
);
315 return uci_do_import(argc
, argv
);
321 int main(int argc
, char **argv
)
327 ctx
= uci_alloc_context();
329 fprintf(stderr
, "Out of memory\n");
333 while((c
= getopt(argc
, argv
, "f:mnNp:P:sSq")) != -1) {
336 input
= fopen(optarg
, "r");
343 flags
|= CLI_FLAG_MERGE
;
346 ctx
->flags
|= UCI_FLAG_STRICT
;
349 ctx
->flags
&= ~UCI_FLAG_STRICT
;
350 ctx
->flags
|= UCI_FLAG_PERROR
;
353 ctx
->flags
|= UCI_FLAG_EXPORT_NAME
;
356 ctx
->flags
&= ~UCI_FLAG_EXPORT_NAME
;
359 uci_add_history_path(ctx
, optarg
);
362 uci_add_history_path(ctx
, ctx
->savedir
);
363 uci_set_savedir(ctx
, optarg
);
364 flags
|= CLI_FLAG_NOCOMMIT
;
367 flags
|= CLI_FLAG_QUIET
;
370 uci_usage(argc
, argv
);
375 argv
[optind
- 1] = argv
[0];
380 uci_usage(argc
, argv
);
381 ret
= uci_cmd(argc
- 1, argv
+ 1);
385 uci_usage(argc
, argv
);
387 uci_free_context(ctx
);