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 static struct uci_context
*ctx
;
27 static void uci_usage(int argc
, char **argv
)
30 "Usage: %s [<options>] <command> [<arguments>]\n\n"
32 "\texport [<config>]\n"
33 "\tshow [<config>[.<section>[.<option>]]]\n"
34 "\tget <config>.<section>[.<option>]\n"
35 "\tset <config>.<section>[.<option>]=<value>\n"
42 static void uci_show_section(struct uci_section
*p
)
44 struct uci_element
*e
;
45 const char *cname
, *sname
;
47 cname
= p
->package
->e
.name
;
49 printf("%s.%s=%s\n", cname
, sname
, p
->type
);
50 uci_foreach_element(&p
->options
, e
) {
51 printf("%s.%s.%s=%s\n", cname
, sname
, e
->name
, uci_to_option(e
)->value
);
55 static void uci_show_package(struct uci_package
*p
, char *section
)
57 struct uci_element
*e
;
59 uci_foreach_element( &p
->sections
, e
) {
60 if (!section
|| !strcmp(e
->name
, section
))
61 uci_show_section(uci_to_section(e
));
65 static int uci_show(int argc
, char **argv
)
67 char *section
= (argc
> 2 ? argv
[2] : NULL
);
68 struct uci_package
*package
;
72 configs
= uci_list_configs(ctx
);
77 if (uci_load(ctx
, argv
[1], &package
) != UCI_OK
) {
78 uci_perror(ctx
, NULL
);
81 uci_show_package(package
, section
);
82 uci_unload(ctx
, package
);
86 for (p
= configs
; *p
; p
++) {
87 if ((argc
< 2) || !strcmp(argv
[1], *p
)) {
88 if (uci_load(ctx
, *p
, &package
) != UCI_OK
) {
89 uci_perror(ctx
, NULL
);
92 uci_show_package(package
, section
);
93 uci_unload(ctx
, package
);
100 static int uci_do_export(int argc
, char **argv
)
102 char **configs
= uci_list_configs(ctx
);
108 for (p
= configs
; *p
; p
++) {
109 if ((argc
< 2) || !strcmp(argv
[1], *p
)) {
110 struct uci_package
*package
= NULL
;
113 ret
= uci_load(ctx
, *p
, &package
);
116 uci_export(ctx
, stdout
, package
, true);
117 uci_unload(ctx
, package
);
123 static int uci_do_cmd(int cmd
, int argc
, char **argv
)
125 char *package
= NULL
;
126 char *section
= NULL
;
129 struct uci_package
*p
= NULL
;
130 struct uci_element
*e
= NULL
;
135 if (uci_parse_tuple(ctx
, argv
[1], &package
, §ion
, &option
, (cmd
== CMD_SET
? &value
: NULL
)) != UCI_OK
)
138 if (uci_load(ctx
, package
, &p
) != UCI_OK
) {
139 uci_perror(ctx
, appname
);
143 if (uci_lookup(ctx
, &e
, p
, section
, option
) != UCI_OK
)
149 case UCI_TYPE_SECTION
:
150 value
= uci_to_section(e
)->type
;
152 case UCI_TYPE_OPTION
:
153 value
= uci_to_option(e
)->value
;
156 /* should not happen */
159 /* throw the value to stdout */
160 printf("%s\n", value
);
163 if (uci_set(ctx
, p
, section
, option
, value
) != UCI_OK
) {
164 uci_perror(ctx
, appname
);
169 if (uci_del(ctx
, p
, section
, option
) != UCI_OK
) {
170 uci_perror(ctx
, appname
);
176 /* no save necessary for get */
180 /* save changes, but don't commit them yet */
181 if (uci_save(ctx
, p
) != UCI_OK
) {
182 uci_perror(ctx
, appname
);
189 static int uci_cmd(int argc
, char **argv
)
193 if (!strcasecmp(argv
[0], "show"))
194 return uci_show(argc
, argv
);
195 if (!strcasecmp(argv
[0], "export"))
196 return uci_do_export(argc
, argv
);
198 if (!strcasecmp(argv
[0], "get"))
200 else if (!strcasecmp(argv
[0], "set"))
202 else if (!strcasecmp(argv
[0], "del"))
206 return uci_do_cmd(cmd
, argc
, argv
);
209 int main(int argc
, char **argv
)
214 ctx
= uci_alloc_context();
216 fprintf(stderr
, "Out of memory\n");
220 while((c
= getopt(argc
, argv
, "sS")) != -1) {
223 ctx
->flags
|= UCI_FLAG_STRICT
;
226 ctx
->flags
&= ~UCI_FLAG_STRICT
;
227 ctx
->flags
|= UCI_FLAG_PERROR
;
230 uci_usage(argc
, argv
);
235 argv
[optind
- 1] = argv
[0];
240 uci_usage(argc
, argv
);
241 ret
= uci_cmd(argc
- 1, argv
+ 1);
243 uci_usage(argc
, argv
);
245 uci_free_context(ctx
);