b7ddd511676ed82fdfe9ce77c841bb6a8cc74ad0
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.
17 static struct uci_context
*ctx
;
19 static void uci_usage(int argc
, char **argv
)
22 "Usage: %s [<options>] <command> [<arguments>]\n\n"
24 "\tshow [<config>[.<section>[.<option>]]]\n"
25 "\texport [<config>]\n"
32 static void uci_show_section(struct uci_section
*p
)
34 struct uci_element
*e
;
35 const char *cname
, *sname
;
37 cname
= p
->package
->e
.name
;
39 printf("%s.%s=%s\n", cname
, sname
, p
->type
);
40 uci_foreach_element(&p
->options
, e
) {
41 printf("%s.%s.%s=%s\n", cname
, sname
, e
->name
, uci_to_option(e
)->value
);
45 static int uci_show(int argc
, char **argv
)
47 char *section
= (argc
> 2 ? argv
[2] : NULL
);
48 struct uci_package
*package
;
49 struct uci_element
*e
;
53 configs
= uci_list_configs(ctx
);
57 for (p
= configs
; *p
; p
++) {
58 if ((argc
< 2) || !strcmp(argv
[1], *p
)) {
59 if (uci_load(ctx
, *p
, &package
) != UCI_OK
) {
60 uci_perror(ctx
, "uci_load");
63 uci_foreach_element( &package
->sections
, e
) {
64 if (!section
|| !strcmp(e
->name
, section
))
65 uci_show_section(uci_to_section(e
));
74 static int uci_do_export(int argc
, char **argv
)
76 char **configs
= uci_list_configs(ctx
);
82 for (p
= configs
; *p
; p
++) {
83 if ((argc
< 2) || !strcmp(argv
[1], *p
)) {
84 struct uci_package
*package
= NULL
;
87 ret
= uci_load(ctx
, *p
, &package
);
90 uci_export(ctx
, stdout
, package
);
97 static int uci_cmd(int argc
, char **argv
)
99 if (!strcasecmp(argv
[0], "show"))
100 return uci_show(argc
, argv
);
101 if (!strcasecmp(argv
[0], "export"))
102 return uci_do_export(argc
, argv
);
106 int main(int argc
, char **argv
)
110 ctx
= uci_alloc_context();
112 uci_usage(argc
, argv
);
113 ret
= uci_cmd(argc
- 1, argv
+ 1);
115 uci_usage(argc
, argv
);
116 uci_free_context(ctx
);