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
;
33 static void uci_usage(int argc
, char **argv
)
36 "Usage: %s [<options>] <command> [<arguments>]\n\n"
38 "\texport [<config>]\n"
39 "\tshow [<config>[.<section>[.<option>]]]\n"
40 "\tget <config>.<section>[.<option>]\n"
41 "\tset <config>.<section>[.<option>]=<value>\n"
44 "\t-s force strict mode (stop on parser errors)\n"
45 "\t-S disable strict mode\n"
52 static void uci_show_section(struct uci_section
*p
)
54 struct uci_element
*e
;
55 const char *cname
, *sname
;
57 cname
= p
->package
->e
.name
;
59 printf("%s.%s=%s\n", cname
, sname
, p
->type
);
60 uci_foreach_element(&p
->options
, e
) {
61 printf("%s.%s.%s=%s\n", cname
, sname
, e
->name
, uci_to_option(e
)->value
);
65 static void uci_show_package(struct uci_package
*p
)
67 struct uci_element
*e
;
69 uci_foreach_element( &p
->sections
, e
) {
70 uci_show_section(uci_to_section(e
));
75 static int package_cmd(int cmd
, char *package
)
77 struct uci_package
*p
= NULL
;
79 if (uci_load(ctx
, package
, &p
) != UCI_OK
) {
80 uci_perror(ctx
, appname
);
85 if (uci_commit(ctx
, &p
) != UCI_OK
)
86 uci_perror(ctx
, appname
);
89 uci_export(ctx
, stdout
, p
, true);
100 static int uci_do_package_cmd(int cmd
, int argc
, char **argv
)
102 char **configs
= NULL
;
109 return package_cmd(cmd
, argv
[1]);
111 if ((uci_list_configs(ctx
, &configs
) != UCI_OK
) || !configs
) {
112 uci_perror(ctx
, appname
);
116 for (p
= configs
; *p
; p
++) {
117 package_cmd(cmd
, *p
);
124 static int uci_do_section_cmd(int cmd
, int argc
, char **argv
)
126 char *package
= NULL
;
127 char *section
= NULL
;
131 struct uci_package
*p
= NULL
;
132 struct uci_element
*e
= NULL
;
145 if (uci_parse_tuple(ctx
, argv
[1], &package
, §ion
, &option
, ptr
) != UCI_OK
)
148 if (uci_load(ctx
, package
, &p
) != UCI_OK
) {
149 uci_perror(ctx
, appname
);
155 if (uci_lookup(ctx
, &e
, p
, section
, option
) != UCI_OK
)
159 case UCI_TYPE_SECTION
:
160 value
= uci_to_section(e
)->type
;
162 case UCI_TYPE_OPTION
:
163 value
= uci_to_option(e
)->value
;
166 /* should not happen */
169 /* throw the value to stdout */
170 printf("%s\n", value
);
173 if (uci_rename(ctx
, p
, section
, option
, value
) != UCI_OK
) {
174 uci_perror(ctx
, appname
);
179 if (uci_set(ctx
, p
, section
, option
, value
) != UCI_OK
) {
180 uci_perror(ctx
, appname
);
185 if (uci_delete(ctx
, p
, section
, option
) != UCI_OK
) {
186 uci_perror(ctx
, appname
);
192 /* no save necessary for get */
196 /* save changes, but don't commit them yet */
197 if (uci_save(ctx
, p
) != UCI_OK
) {
198 uci_perror(ctx
, appname
);
205 static int uci_cmd(int argc
, char **argv
)
209 if (!strcasecmp(argv
[0], "show"))
211 else if (!strcasecmp(argv
[0], "export"))
213 else if (!strcasecmp(argv
[0], "commit"))
215 else if (!strcasecmp(argv
[0], "get"))
217 else if (!strcasecmp(argv
[0], "set"))
219 else if (!strcasecmp(argv
[0], "ren") ||
220 !strcasecmp(argv
[0], "rename"))
222 else if (!strcasecmp(argv
[0], "del"))
232 return uci_do_section_cmd(cmd
, argc
, argv
);
236 return uci_do_package_cmd(cmd
, argc
, argv
);
242 int main(int argc
, char **argv
)
247 ctx
= uci_alloc_context();
249 fprintf(stderr
, "Out of memory\n");
253 while((c
= getopt(argc
, argv
, "sS")) != -1) {
256 ctx
->flags
|= UCI_FLAG_STRICT
;
259 ctx
->flags
&= ~UCI_FLAG_STRICT
;
260 ctx
->flags
|= UCI_FLAG_PERROR
;
263 uci_usage(argc
, argv
);
268 argv
[optind
- 1] = argv
[0];
273 uci_usage(argc
, argv
);
274 ret
= uci_cmd(argc
- 1, argv
+ 1);
276 uci_usage(argc
, argv
);
278 uci_free_context(ctx
);