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;
18 static char *buf = NULL;
19 static int buflen = 256;
21 static void uci_usage(int argc, char **argv)
24 "Usage: %s [<options>] <command> [<arguments>]\n\n"
26 "\tshow [<config>[.<section>[.<option>]]]\n"
27 "\texport [<config>]\n"
34 static void uci_show_section(struct uci_section *p)
37 const char *cname, *sname;
39 cname = p->config->name;
41 printf("%s.%s=%s\n", cname, sname, p->type);
42 uci_foreach_entry(option, &p->options, o) {
43 printf("%s.%s.%s=%s\n", cname, sname, o->name, o->value);
47 static void foreach_section(const char *configname, const char *section, void (*callback)(struct uci_section *))
49 struct uci_config *cfg;
50 struct uci_section *p;
52 if (uci_load(ctx, configname, &cfg) != UCI_OK) {
53 uci_perror(ctx, "uci_load");
57 uci_foreach_entry(section, &cfg->sections, p) {
58 if (!section || !strcmp(p->name, section))
61 uci_unload(ctx, configname);
64 static int uci_show(int argc, char **argv)
66 char **configs = uci_list_configs();
72 for (p = configs; *p; p++) {
73 if ((argc < 2) || !strcmp(argv[1], *p))
74 foreach_section(*p, (argc > 2 ? argv[2] : NULL), uci_show_section);
80 static int uci_do_export(int argc, char **argv)
82 char **configs = uci_list_configs();
88 for (p = configs; *p; p++) {
89 if ((argc < 2) || !strcmp(argv[1], *p)) {
90 struct uci_config *cfg = NULL;
93 ret = uci_load(ctx, *p, &cfg);
96 uci_export(ctx, stdout, cfg);
103 static int uci_cmd(int argc, char **argv)
105 if (!strcasecmp(argv[0], "show"))
106 return uci_show(argc, argv);
107 if (!strcasecmp(argv[0], "export"))
108 return uci_do_export(argc, argv);
112 int main(int argc, char **argv)
118 uci_usage(argc, argv);
119 ret = uci_cmd(argc - 1, argv + 1);
121 uci_usage(argc, argv);