bfbb92c468b760203f8f83f990ed5f53d037c943
2 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
18 static const char *appname
= "uci";
20 CLI_FLAG_MERGE
= (1 << 0),
21 CLI_FLAG_QUIET
= (1 << 1),
22 CLI_FLAG_NOCOMMIT
= (1 << 2),
26 static struct uci_context
*ctx
;
40 static void uci_usage(int argc
, char **argv
)
43 "Usage: %s [<options>] <command> [<arguments>]\n\n"
45 "\texport [<config>]\n"
46 "\timport [<config>]\n"
47 "\tshow [<config>[.<section>[.<option>]]]\n"
48 "\tget <config>.<section>[.<option>]\n"
49 "\tset <config>.<section>[.<option>]=<value>\n"
50 "\trename <config>.<section>[.<option>]=<name>\n"
53 "\t-f <file> use <file> as input instead of stdin\n"
54 "\t-m when importing, merge data into an existing package\n"
55 "\t-n name unnamed sections on export (default)\n"
56 "\t-N don't name unnamed sections\n"
57 "\t-p <path> add a search path for config change files\n"
58 "\t-P <path> add a search path for config change files and use as default\n"
59 "\t-q quiet mode (don't print error messages)\n"
60 "\t-s force strict mode (stop on parser errors, default)\n"
61 "\t-S disable strict mode\n"
68 static void cli_perror(void)
70 if (flags
& CLI_FLAG_QUIET
)
73 uci_perror(ctx
, appname
);
76 static void uci_show_section(struct uci_section
*p
)
78 struct uci_element
*e
;
79 const char *cname
, *sname
;
81 cname
= p
->package
->e
.name
;
83 printf("%s.%s=%s\n", cname
, sname
, p
->type
);
84 uci_foreach_element(&p
->options
, e
) {
85 printf("%s.%s.%s=%s\n", cname
, sname
, e
->name
, uci_to_option(e
)->value
);
89 static void uci_show_package(struct uci_package
*p
)
91 struct uci_element
*e
;
93 uci_foreach_element( &p
->sections
, e
) {
94 uci_show_section(uci_to_section(e
));
99 static int package_cmd(int cmd
, char *package
)
101 struct uci_package
*p
= NULL
;
103 if (uci_load(ctx
, package
, &p
) != UCI_OK
) {
111 if (flags
& CLI_FLAG_NOCOMMIT
)
113 if (uci_commit(ctx
, &p
, false) != UCI_OK
)
117 uci_export(ctx
, stdout
, p
, true);
128 static int uci_do_import(int argc
, char **argv
)
130 struct uci_package
*package
= NULL
;
139 else if (flags
& CLI_FLAG_MERGE
)
140 /* need a package to merge */
143 if (flags
& CLI_FLAG_MERGE
) {
144 if (uci_load(ctx
, name
, &package
) != UCI_OK
)
147 ret
= uci_import(ctx
, input
, name
, &package
, (name
!= NULL
));
149 if (flags
& CLI_FLAG_MERGE
) {
150 ret
= uci_save(ctx
, package
);
152 struct uci_element
*e
;
153 /* loop through all config sections and overwrite existing data */
154 uci_foreach_element(&ctx
->root
, e
) {
155 struct uci_package
*p
= uci_to_package(e
);
156 ret
= uci_commit(ctx
, &p
, true);
169 static int uci_do_package_cmd(int cmd
, int argc
, char **argv
)
171 char **configs
= NULL
;
178 return package_cmd(cmd
, argv
[1]);
180 if ((uci_list_configs(ctx
, &configs
) != UCI_OK
) || !configs
) {
185 for (p
= configs
; *p
; p
++) {
186 package_cmd(cmd
, *p
);
193 static int uci_do_section_cmd(int cmd
, int argc
, char **argv
)
195 struct uci_package
*p
= NULL
;
196 struct uci_element
*e
= NULL
;
197 char *package
= NULL
;
198 char *section
= NULL
;
215 if (uci_parse_tuple(ctx
, argv
[1], &package
, §ion
, &option
, ptr
) != UCI_OK
)
218 if (uci_load(ctx
, package
, &p
) != UCI_OK
) {
227 if (uci_lookup(ctx
, &e
, p
, section
, option
) != UCI_OK
)
231 case UCI_TYPE_SECTION
:
232 value
= uci_to_section(e
)->type
;
234 case UCI_TYPE_OPTION
:
235 value
= uci_to_option(e
)->value
;
238 /* should not happen */
241 /* throw the value to stdout */
242 printf("%s\n", value
);
245 ret
= uci_rename(ctx
, p
, section
, option
, value
);
248 ret
= uci_set(ctx
, p
, section
, option
, value
);
251 ret
= uci_delete(ctx
, p
, section
, option
);
255 /* no save necessary for get */
259 /* save changes, but don't commit them yet */
261 ret
= uci_save(ctx
, p
);
271 static int uci_cmd(int argc
, char **argv
)
275 if (!strcasecmp(argv
[0], "show"))
277 else if (!strcasecmp(argv
[0], "export"))
279 else if (!strcasecmp(argv
[0], "commit"))
281 else if (!strcasecmp(argv
[0], "get"))
283 else if (!strcasecmp(argv
[0], "set"))
285 else if (!strcasecmp(argv
[0], "ren") ||
286 !strcasecmp(argv
[0], "rename"))
288 else if (!strcasecmp(argv
[0], "del"))
290 else if (!strcasecmp(argv
[0], "import"))
300 return uci_do_section_cmd(cmd
, argc
, argv
);
304 return uci_do_package_cmd(cmd
, argc
, argv
);
306 return uci_do_import(argc
, argv
);
312 int main(int argc
, char **argv
)
318 ctx
= uci_alloc_context();
320 fprintf(stderr
, "Out of memory\n");
324 while((c
= getopt(argc
, argv
, "f:mnNp:P:sSq")) != -1) {
327 input
= fopen(optarg
, "r");
334 flags
|= CLI_FLAG_MERGE
;
337 ctx
->flags
|= UCI_FLAG_STRICT
;
340 ctx
->flags
&= ~UCI_FLAG_STRICT
;
341 ctx
->flags
|= UCI_FLAG_PERROR
;
344 ctx
->flags
|= UCI_FLAG_EXPORT_NAME
;
347 ctx
->flags
&= ~UCI_FLAG_EXPORT_NAME
;
350 uci_add_history_path(ctx
, optarg
);
353 uci_add_history_path(ctx
, ctx
->savedir
);
354 uci_set_savedir(ctx
, optarg
);
355 flags
|= CLI_FLAG_NOCOMMIT
;
358 flags
|= CLI_FLAG_QUIET
;
361 uci_usage(argc
, argv
);
366 argv
[optind
- 1] = argv
[0];
371 uci_usage(argc
, argv
);
372 ret
= uci_cmd(argc
- 1, argv
+ 1);
376 uci_usage(argc
, argv
);
378 uci_free_context(ctx
);