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 CLI_FLAG_MERGE = (1 << 0),
22 static FILE *input = stdin;
24 static struct uci_context *ctx;
38 static void uci_usage(int argc, char **argv)
41 "Usage: %s [<options>] <command> [<arguments>]\n\n"
43 "\texport [<config>]\n"
44 "\timport [<config>]\n"
45 "\tshow [<config>[.<section>[.<option>]]]\n"
46 "\tget <config>.<section>[.<option>]\n"
47 "\tset <config>.<section>[.<option>]=<value>\n"
48 "\trename <config>.<section>[.<option>]=<name>\n"
51 "\t-f <file> use <file> as input instead of stdin\n"
52 "\t-m when importing, merge data into an existing package\n"
53 "\t-s force strict mode (stop on parser errors)\n"
54 "\t-S disable strict mode\n"
61 static void uci_show_section(struct uci_section *p)
63 struct uci_element *e;
64 const char *cname, *sname;
66 cname = p->package->e.name;
68 printf("%s.%s=%s\n", cname, sname, p->type);
69 uci_foreach_element(&p->options, e) {
70 printf("%s.%s.%s=%s\n", cname, sname, e->name, uci_to_option(e)->value);
74 static void uci_show_package(struct uci_package *p)
76 struct uci_element *e;
78 uci_foreach_element( &p->sections, e) {
79 uci_show_section(uci_to_section(e));
84 static int package_cmd(int cmd, char *package)
86 struct uci_package *p = NULL;
88 if (uci_load(ctx, package, &p) != UCI_OK) {
89 uci_perror(ctx, appname);
94 if (uci_commit(ctx, &p) != UCI_OK)
95 uci_perror(ctx, appname);
98 uci_export(ctx, stdout, p, true);
109 static int uci_do_import(int argc, char **argv)
114 static int uci_do_package_cmd(int cmd, int argc, char **argv)
116 char **configs = NULL;
123 return package_cmd(cmd, argv[1]);
125 if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
126 uci_perror(ctx, appname);
130 for (p = configs; *p; p++) {
131 package_cmd(cmd, *p);
138 static int uci_do_section_cmd(int cmd, int argc, char **argv)
140 struct uci_package *p = NULL;
141 struct uci_element *e = NULL;
142 char *package = NULL;
143 char *section = NULL;
160 if (uci_parse_tuple(ctx, argv[1], &package, §ion, &option, ptr) != UCI_OK)
163 if (uci_load(ctx, package, &p) != UCI_OK) {
164 uci_perror(ctx, appname);
170 if (uci_lookup(ctx, &e, p, section, option) != UCI_OK)
174 case UCI_TYPE_SECTION:
175 value = uci_to_section(e)->type;
177 case UCI_TYPE_OPTION:
178 value = uci_to_option(e)->value;
181 /* should not happen */
184 /* throw the value to stdout */
185 printf("%s\n", value);
188 ret = uci_rename(ctx, p, section, option, value);
191 ret = uci_set(ctx, p, section, option, value);
194 ret = uci_delete(ctx, p, section, option);
198 /* no save necessary for get */
202 /* save changes, but don't commit them yet */
204 ret = uci_save(ctx, p);
207 uci_perror(ctx, appname);
214 static int uci_cmd(int argc, char **argv)
218 if (!strcasecmp(argv[0], "show"))
220 else if (!strcasecmp(argv[0], "export"))
222 else if (!strcasecmp(argv[0], "commit"))
224 else if (!strcasecmp(argv[0], "get"))
226 else if (!strcasecmp(argv[0], "set"))
228 else if (!strcasecmp(argv[0], "ren") ||
229 !strcasecmp(argv[0], "rename"))
231 else if (!strcasecmp(argv[0], "del"))
233 else if (!strcasecmp(argv[0], "import"))
243 return uci_do_section_cmd(cmd, argc, argv);
247 return uci_do_package_cmd(cmd, argc, argv);
249 return uci_do_import(argc, argv);
255 int main(int argc, char **argv)
260 ctx = uci_alloc_context();
262 fprintf(stderr, "Out of memory\n");
266 while((c = getopt(argc, argv, "sS")) != -1) {
269 input = fopen(optarg, "r");
276 flags |= CLI_FLAG_MERGE;
279 ctx->flags |= UCI_FLAG_STRICT;
282 ctx->flags &= ~UCI_FLAG_STRICT;
283 ctx->flags |= UCI_FLAG_PERROR;
286 uci_usage(argc, argv);
291 argv[optind - 1] = argv[0];
296 uci_usage(argc, argv);
297 ret = uci_cmd(argc - 1, argv + 1);
301 uci_usage(argc, argv);
303 uci_free_context(ctx);