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
;
32 static void uci_usage(int argc
, char **argv
)
35 "Usage: %s [<options>] <command> [<arguments>]\n\n"
37 "\texport [<config>]\n"
38 "\tshow [<config>[.<section>[.<option>]]]\n"
39 "\tget <config>.<section>[.<option>]\n"
40 "\tset <config>.<section>[.<option>]=<value>\n"
43 "\t-s force strict mode (stop on parser errors)\n"
44 "\t-S disable strict mode\n"
51 static void uci_show_section(struct uci_section
*p
)
53 struct uci_element
*e
;
54 const char *cname
, *sname
;
56 cname
= p
->package
->e
.name
;
58 printf("%s.%s=%s\n", cname
, sname
, p
->type
);
59 uci_foreach_element(&p
->options
, e
) {
60 printf("%s.%s.%s=%s\n", cname
, sname
, e
->name
, uci_to_option(e
)->value
);
64 static void uci_show_package(struct uci_package
*p
)
66 struct uci_element
*e
;
68 uci_foreach_element( &p
->sections
, e
) {
69 uci_show_section(uci_to_section(e
));
74 static int package_cmd(int cmd
, char *package
)
76 struct uci_package
*p
= NULL
;
78 if (uci_load(ctx
, package
, &p
) != UCI_OK
) {
79 uci_perror(ctx
, appname
);
84 if (uci_commit(ctx
, &p
) != UCI_OK
)
85 uci_perror(ctx
, appname
);
88 uci_export(ctx
, stdout
, p
, true);
99 static int uci_do_package_cmd(int cmd
, int argc
, char **argv
)
101 char **configs
= NULL
;
108 return package_cmd(cmd
, argv
[1]);
110 if ((uci_list_configs(ctx
, &configs
) != UCI_OK
) || !configs
) {
111 uci_perror(ctx
, appname
);
115 for (p
= configs
; *p
; p
++) {
116 package_cmd(cmd
, *p
);
123 static int uci_do_section_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
);
145 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"))
195 else if (!strcasecmp(argv
[0], "export"))
197 else if (!strcasecmp(argv
[0], "commit"))
199 else if (!strcasecmp(argv
[0], "get"))
201 else if (!strcasecmp(argv
[0], "set"))
203 else if (!strcasecmp(argv
[0], "del"))
210 return uci_do_section_cmd(cmd
, argc
, argv
);
214 return uci_do_package_cmd(cmd
, argc
, argv
);
220 int main(int argc
, char **argv
)
225 ctx
= uci_alloc_context();
227 fprintf(stderr
, "Out of memory\n");
231 while((c
= getopt(argc
, argv
, "sS")) != -1) {
234 ctx
->flags
|= UCI_FLAG_STRICT
;
237 ctx
->flags
&= ~UCI_FLAG_STRICT
;
238 ctx
->flags
|= UCI_FLAG_PERROR
;
241 uci_usage(argc
, argv
);
246 argv
[optind
- 1] = argv
[0];
251 uci_usage(argc
, argv
);
252 ret
= uci_cmd(argc
- 1, argv
+ 1);
254 uci_usage(argc
, argv
);
256 uci_free_context(ctx
);