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 const char *appname = "uci";
19 static struct uci_context *ctx;
26 static void uci_usage(int argc, char **argv)
29 "Usage: %s [<options>] <command> [<arguments>]\n\n"
31 "\texport [<config>]\n"
32 "\tshow [<config>[.<section>[.<option>]]]\n"
33 "\tget <config>.<section>[.<option>]\n"
34 "\tset <config>.<section>[.<option>]=<value>\n"
41 static void uci_show_section(struct uci_section *p)
43 struct uci_element *e;
44 const char *cname, *sname;
46 cname = p->package->e.name;
48 printf("%s.%s=%s\n", cname, sname, p->type);
49 uci_foreach_element(&p->options, e) {
50 printf("%s.%s.%s=%s\n", cname, sname, e->name, uci_to_option(e)->value);
54 static void uci_show_package(struct uci_package *p, char *section)
56 struct uci_element *e;
58 uci_foreach_element( &p->sections, e) {
59 if (!section || !strcmp(e->name, section))
60 uci_show_section(uci_to_section(e));
64 static int uci_show(int argc, char **argv)
66 char *section = (argc > 2 ? argv[2] : NULL);
67 struct uci_package *package;
71 configs = uci_list_configs(ctx);
76 if (uci_load(ctx, argv[1], &package) != UCI_OK) {
77 uci_perror(ctx, NULL);
80 uci_show_package(package, section);
81 uci_unload(ctx, package);
85 for (p = configs; *p; p++) {
86 if ((argc < 2) || !strcmp(argv[1], *p)) {
87 if (uci_load(ctx, *p, &package) != UCI_OK) {
88 uci_perror(ctx, NULL);
91 uci_show_package(package, section);
92 uci_unload(ctx, package);
99 static int uci_do_export(int argc, char **argv)
101 char **configs = uci_list_configs(ctx);
107 for (p = configs; *p; p++) {
108 if ((argc < 2) || !strcmp(argv[1], *p)) {
109 struct uci_package *package = NULL;
112 ret = uci_load(ctx, *p, &package);
115 uci_export(ctx, stdout, package, true);
116 uci_unload(ctx, package);
122 static int uci_do_cmd(int cmd, int argc, char **argv)
124 char *package = NULL;
125 char *section = NULL;
128 struct uci_package *p = NULL;
129 struct uci_element *e = NULL;
134 if (uci_parse_tuple(ctx, argv[1], &package, §ion, &option, (cmd == CMD_SET ? &value : NULL)) != UCI_OK)
137 if (uci_load(ctx, package, &p) != UCI_OK) {
138 uci_perror(ctx, appname);
142 if (uci_lookup(ctx, &e, p, section, option) != UCI_OK)
148 case UCI_TYPE_SECTION:
149 value = uci_to_section(e)->type;
151 case UCI_TYPE_OPTION:
152 value = uci_to_option(e)->value;
155 /* should not happen */
158 /* throw the value to stdout */
159 printf("%s\n", value);
162 if (uci_set(ctx, p, section, option, value) != UCI_OK) {
163 uci_perror(ctx, appname);
168 if (uci_del(ctx, p, section, option) != UCI_OK) {
169 uci_perror(ctx, appname);
175 /* no save necessary for get */
179 /* save changes, but don't commit them yet */
180 if (uci_save(ctx, p) != UCI_OK) {
181 uci_perror(ctx, appname);
188 static int uci_cmd(int argc, char **argv)
192 if (!strcasecmp(argv[0], "show"))
193 return uci_show(argc, argv);
194 if (!strcasecmp(argv[0], "export"))
195 return uci_do_export(argc, argv);
197 if (!strcasecmp(argv[0], "get"))
199 else if (!strcasecmp(argv[0], "set"))
201 else if (!strcasecmp(argv[0], "del"))
205 return uci_do_cmd(cmd, argc, argv);
208 int main(int argc, char **argv)
212 ctx = uci_alloc_context();
214 uci_usage(argc, argv);
215 ret = uci_cmd(argc - 1, argv + 1);
217 uci_usage(argc, argv);
218 uci_free_context(ctx);