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
;
18 static char *buf
= NULL
;
19 static int buflen
= 256;
21 static void uci_usage(int argc
, char **argv
)
24 "Usage: %s [<options>] <command> [<arguments>]\n\n"
26 "\tshow [<config>[.<section>[.<option>]]]\n"
27 "\texport [<config>]\n"
34 static void uci_show_section(struct uci_section
*p
)
37 const char *cname
, *sname
;
39 cname
= p
->package
->name
;
41 printf("%s.%s=%s\n", cname
, sname
, p
->type
);
42 uci_foreach_entry(option
, &p
->options
, o
) {
43 printf("%s.%s.%s=%s\n", cname
, sname
, o
->name
, o
->value
);
47 static int uci_show(int argc
, char **argv
)
49 char *section
= (argc
> 2 ? argv
[2] : NULL
);
50 struct uci_package
*package
;
51 struct uci_section
*s
;
55 configs
= uci_list_configs(ctx
);
59 for (p
= configs
; *p
; p
++) {
60 if ((argc
< 2) || !strcmp(argv
[1], *p
)) {
61 if (uci_load(ctx
, *p
, &package
) != UCI_OK
) {
62 uci_perror(ctx
, "uci_load");
65 uci_foreach_entry(section
, &package
->sections
, s
) {
66 if (!section
|| !strcmp(s
->name
, section
))
76 static int uci_do_export(int argc
, char **argv
)
78 char **configs
= uci_list_configs(ctx
);
84 for (p
= configs
; *p
; p
++) {
85 if ((argc
< 2) || !strcmp(argv
[1], *p
)) {
86 struct uci_package
*package
= NULL
;
89 ret
= uci_load(ctx
, *p
, &package
);
92 uci_export(ctx
, stdout
, package
);
99 static int uci_cmd(int argc
, char **argv
)
101 if (!strcasecmp(argv
[0], "show"))
102 return uci_show(argc
, argv
);
103 if (!strcasecmp(argv
[0], "export"))
104 return uci_do_export(argc
, argv
);
108 int main(int argc
, char **argv
)
114 uci_usage(argc
, argv
);
115 ret
= uci_cmd(argc
- 1, argv
+ 1);
117 uci_usage(argc
, argv
);