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"
38 "\t-s force strict mode (stop on parser errors)\n"
39 "\t-S disable strict mode\n"
46 static void uci_show_section(struct uci_section *p)
48 struct uci_element *e;
49 const char *cname, *sname;
51 cname = p->package->e.name;
53 printf("%s.%s=%s\n", cname, sname, p->type);
54 uci_foreach_element(&p->options, e) {
55 printf("%s.%s.%s=%s\n", cname, sname, e->name, uci_to_option(e)->value);
59 static void uci_show_package(struct uci_package *p, char *section)
61 struct uci_element *e;
63 uci_foreach_element( &p->sections, e) {
64 if (!section || !strcmp(e->name, section))
65 uci_show_section(uci_to_section(e));
69 static int uci_show(int argc, char **argv)
71 char *section = (argc > 2 ? argv[2] : NULL);
72 struct uci_package *package;
73 char **configs = NULL;
76 if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
77 uci_perror(ctx, appname);
82 if (uci_load(ctx, argv[1], &package) != UCI_OK) {
83 uci_perror(ctx, appname);
86 uci_show_package(package, section);
87 uci_unload(ctx, package);
91 for (p = configs; *p; p++) {
92 if ((argc < 2) || !strcmp(argv[1], *p)) {
93 if (uci_load(ctx, *p, &package) != UCI_OK) {
94 uci_perror(ctx, appname);
97 uci_show_package(package, section);
98 uci_unload(ctx, package);
105 static int uci_do_export(int argc, char **argv)
107 char **configs = NULL;
110 if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
111 uci_perror(ctx, appname);
115 for (p = configs; *p; p++) {
116 if ((argc < 2) || !strcmp(argv[1], *p)) {
117 struct uci_package *package = NULL;
120 ret = uci_load(ctx, *p, &package);
123 uci_export(ctx, stdout, package, true);
124 uci_unload(ctx, package);
130 static int uci_do_cmd(int cmd, int argc, char **argv)
132 char *package = NULL;
133 char *section = NULL;
136 struct uci_package *p = NULL;
137 struct uci_element *e = NULL;
142 if (uci_parse_tuple(ctx, argv[1], &package, §ion, &option, (cmd == CMD_SET ? &value : NULL)) != UCI_OK)
145 if (uci_load(ctx, package, &p) != UCI_OK) {
146 uci_perror(ctx, appname);
150 if (uci_lookup(ctx, &e, p, section, option) != UCI_OK)
156 case UCI_TYPE_SECTION:
157 value = uci_to_section(e)->type;
159 case UCI_TYPE_OPTION:
160 value = uci_to_option(e)->value;
163 /* should not happen */
166 /* throw the value to stdout */
167 printf("%s\n", value);
170 if (uci_set(ctx, p, section, option, value) != UCI_OK) {
171 uci_perror(ctx, appname);
176 if (uci_del(ctx, p, section, option) != UCI_OK) {
177 uci_perror(ctx, appname);
183 /* no save necessary for get */
187 /* save changes, but don't commit them yet */
188 if (uci_save(ctx, p) != UCI_OK) {
189 uci_perror(ctx, appname);
196 static int uci_cmd(int argc, char **argv)
200 if (!strcasecmp(argv[0], "show"))
201 return uci_show(argc, argv);
202 if (!strcasecmp(argv[0], "export"))
203 return uci_do_export(argc, argv);
205 if (!strcasecmp(argv[0], "get"))
207 else if (!strcasecmp(argv[0], "set"))
209 else if (!strcasecmp(argv[0], "del"))
213 return uci_do_cmd(cmd, argc, argv);
216 int main(int argc, char **argv)
221 ctx = uci_alloc_context();
223 fprintf(stderr, "Out of memory\n");
227 while((c = getopt(argc, argv, "sS")) != -1) {
230 ctx->flags |= UCI_FLAG_STRICT;
233 ctx->flags &= ~UCI_FLAG_STRICT;
234 ctx->flags |= UCI_FLAG_PERROR;
237 uci_usage(argc, argv);
242 argv[optind - 1] = argv[0];
247 uci_usage(argc, argv);
248 ret = uci_cmd(argc - 1, argv + 1);
250 uci_usage(argc, argv);
252 uci_free_context(ctx);