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 struct uci_context *ctx;
19 static void uci_usage(int argc, char **argv)
22 "Usage: %s [<options>] <command> [<arguments>]\n\n"
24 "\tshow [<config>[.<section>[.<option>]]]\n"
25 "\texport [<config>]\n"
32 static void uci_show_section(struct uci_section *p)
34 struct uci_element *e;
35 const char *cname, *sname;
37 cname = p->package->e.name;
39 printf("%s.%s=%s\n", cname, sname, p->type);
40 uci_foreach_element(&p->options, e) {
41 printf("%s.%s.%s=%s\n", cname, sname, e->name, uci_to_option(e)->value);
45 static int uci_show(int argc, char **argv)
47 char *section = (argc > 2 ? argv[2] : NULL);
48 struct uci_package *package;
49 struct uci_element *e;
53 configs = uci_list_configs(ctx);
57 for (p = configs; *p; p++) {
58 if ((argc < 2) || !strcmp(argv[1], *p)) {
59 if (uci_load(ctx, *p, &package) != UCI_OK) {
60 uci_perror(ctx, "uci_load");
63 uci_foreach_element( &package->sections, e) {
64 if (!section || !strcmp(e->name, section))
65 uci_show_section(uci_to_section(e));
74 static int uci_do_export(int argc, char **argv)
76 char **configs = uci_list_configs(ctx);
82 for (p = configs; *p; p++) {
83 if ((argc < 2) || !strcmp(argv[1], *p)) {
84 struct uci_package *package = NULL;
87 ret = uci_load(ctx, *p, &package);
90 uci_export(ctx, stdout, package);
99 static int uci_do_get(int argc, char **argv)
101 char *package = NULL;
102 char *section = NULL;
104 struct uci_package *p = NULL;
105 struct uci_element *e = NULL;
108 package = strtok(argv[1], ".");
112 section = strtok(NULL, ".");
114 option = strtok(NULL, ".");
116 if (uci_load(ctx, package, &p) != UCI_OK) {
117 uci_perror(ctx, "uci");
120 if (uci_lookup(ctx, &e, package, section, option) != UCI_OK)
123 case UCI_TYPE_SECTION:
124 value = uci_to_section(e)->type;
126 case UCI_TYPE_OPTION:
127 value = uci_to_option(e)->value;
130 /* should not happen */
133 printf("%s\n", value);
137 static int uci_cmd(int argc, char **argv)
139 if (!strcasecmp(argv[0], "show"))
140 return uci_show(argc, argv);
141 if (!strcasecmp(argv[0], "export"))
142 return uci_do_export(argc, argv);
143 if (!strcasecmp(argv[0], "get"))
144 return uci_do_get(argc, argv);
148 int main(int argc, char **argv)
152 ctx = uci_alloc_context();
154 uci_usage(argc, argv);
155 ret = uci_cmd(argc - 1, argv + 1);
157 uci_usage(argc, argv);
158 uci_free_context(ctx);