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 "\texport [<config>]\n"
25 "\tshow [<config>[.<section>[.<option>]]]\n"
26 "\tget <config>.<section>[.<option>]\n"
27 "\tset <config>.<section>[.<option>]=<value>\n"
34 static void uci_show_section(struct uci_section
*p
)
36 struct uci_element
*e
;
37 const char *cname
, *sname
;
39 cname
= p
->package
->e
.name
;
41 printf("%s.%s=%s\n", cname
, sname
, p
->type
);
42 uci_foreach_element(&p
->options
, e
) {
43 printf("%s.%s.%s=%s\n", cname
, sname
, e
->name
, uci_to_option(e
)->value
);
47 static void uci_show_package(struct uci_package
*p
, char *section
)
49 struct uci_element
*e
;
51 uci_foreach_element( &p
->sections
, e
) {
52 if (!section
|| !strcmp(e
->name
, section
))
53 uci_show_section(uci_to_section(e
));
57 static int uci_show(int argc
, char **argv
)
59 char *section
= (argc
> 2 ? argv
[2] : NULL
);
60 struct uci_package
*package
;
64 configs
= uci_list_configs(ctx
);
69 if (uci_load(ctx
, argv
[1], &package
) != UCI_OK
) {
70 uci_perror(ctx
, NULL
);
73 uci_show_package(package
, section
);
74 uci_unload(ctx
, package
);
78 for (p
= configs
; *p
; p
++) {
79 if ((argc
< 2) || !strcmp(argv
[1], *p
)) {
80 if (uci_load(ctx
, *p
, &package
) != UCI_OK
) {
81 uci_perror(ctx
, NULL
);
84 uci_show_package(package
, section
);
85 uci_unload(ctx
, package
);
92 static int uci_do_export(int argc
, char **argv
)
94 char **configs
= uci_list_configs(ctx
);
100 for (p
= configs
; *p
; p
++) {
101 if ((argc
< 2) || !strcmp(argv
[1], *p
)) {
102 struct uci_package
*package
= NULL
;
105 ret
= uci_load(ctx
, *p
, &package
);
108 uci_export(ctx
, stdout
, package
, true);
109 uci_unload(ctx
, package
);
115 static int uci_do_get(int argc
, char **argv
)
117 char *package
= NULL
;
118 char *section
= NULL
;
120 struct uci_package
*p
= NULL
;
121 struct uci_element
*e
= NULL
;
127 if (uci_parse_tuple(ctx
, argv
[1], &package
, §ion
, &option
, NULL
) != UCI_OK
)
130 if (uci_load(ctx
, package
, &p
) != UCI_OK
) {
131 uci_perror(ctx
, "uci");
135 if (uci_lookup(ctx
, &e
, p
, section
, option
) != UCI_OK
)
139 case UCI_TYPE_SECTION
:
140 value
= uci_to_section(e
)->type
;
142 case UCI_TYPE_OPTION
:
143 value
= uci_to_option(e
)->value
;
146 /* should not happen */
150 /* throw the value to stdout */
151 printf("%s\n", value
);
156 static int uci_do_set(int argc
, char **argv
)
158 struct uci_package
*p
;
159 char *package
= NULL
;
160 char *section
= NULL
;
167 if (uci_parse_tuple(ctx
, argv
[1], &package
, §ion
, &option
, &value
) != UCI_OK
)
170 if (uci_load(ctx
, package
, &p
) != UCI_OK
) {
171 uci_perror(ctx
, "uci");
175 if (uci_set(ctx
, package
, section
, option
, value
) != UCI_OK
) {
176 uci_perror(ctx
, "uci");
183 static int uci_cmd(int argc
, char **argv
)
185 if (!strcasecmp(argv
[0], "show"))
186 return uci_show(argc
, argv
);
187 if (!strcasecmp(argv
[0], "export"))
188 return uci_do_export(argc
, argv
);
189 if (!strcasecmp(argv
[0], "get"))
190 return uci_do_get(argc
, argv
);
191 if (!strcasecmp(argv
[0], "set"))
192 return uci_do_set(argc
, argv
);
196 int main(int argc
, char **argv
)
200 ctx
= uci_alloc_context();
202 uci_usage(argc
, argv
);
203 ret
= uci_cmd(argc
- 1, argv
+ 1);
205 uci_usage(argc
, argv
);
206 uci_free_context(ctx
);