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),
21 CLI_FLAG_QUIET = (1 << 1)
25 static struct uci_context *ctx;
39 static void uci_usage(int argc, char **argv)
42 "Usage: %s [<options>] <command> [<arguments>]\n\n"
44 "\texport [<config>]\n"
45 "\timport [<config>]\n"
46 "\tshow [<config>[.<section>[.<option>]]]\n"
47 "\tget <config>.<section>[.<option>]\n"
48 "\tset <config>.<section>[.<option>]=<value>\n"
49 "\trename <config>.<section>[.<option>]=<name>\n"
52 "\t-f <file> use <file> as input instead of stdin\n"
53 "\t-m when importing, merge data into an existing package\n"
54 "\t-n name unnamed sections on export (default)\n"
55 "\t-N don't name unnamed sections\n"
56 "\t-q quiet mode (don't print error messages)\n"
57 "\t-s force strict mode (stop on parser errors, default)\n"
58 "\t-S disable strict mode\n"
65 static void cli_perror(void)
67 if (flags & CLI_FLAG_QUIET)
70 uci_perror(ctx, appname);
73 static void uci_show_section(struct uci_section *p)
75 struct uci_element *e;
76 const char *cname, *sname;
78 cname = p->package->e.name;
80 printf("%s.%s=%s\n", cname, sname, p->type);
81 uci_foreach_element(&p->options, e) {
82 printf("%s.%s.%s=%s\n", cname, sname, e->name, uci_to_option(e)->value);
86 static void uci_show_package(struct uci_package *p)
88 struct uci_element *e;
90 uci_foreach_element( &p->sections, e) {
91 uci_show_section(uci_to_section(e));
96 static int package_cmd(int cmd, char *package)
98 struct uci_package *p = NULL;
100 if (uci_load(ctx, package, &p) != UCI_OK) {
108 if (uci_commit(ctx, &p, false) != UCI_OK)
112 uci_export(ctx, stdout, p, true);
123 static int uci_do_import(int argc, char **argv)
125 struct uci_package *package = NULL;
134 else if (flags & CLI_FLAG_MERGE)
135 /* need a package to merge */
138 if (flags & CLI_FLAG_MERGE) {
139 if (uci_load(ctx, name, &package) != UCI_OK)
142 ret = uci_import(ctx, input, name, &package, (name != NULL));
144 if (flags & CLI_FLAG_MERGE) {
145 ret = uci_save(ctx, package);
147 struct uci_element *e;
148 /* loop through all config sections and overwrite existing data */
149 uci_foreach_element(&ctx->root, e) {
150 struct uci_package *p = uci_to_package(e);
151 ret = uci_commit(ctx, &p, true);
164 static int uci_do_package_cmd(int cmd, int argc, char **argv)
166 char **configs = NULL;
173 return package_cmd(cmd, argv[1]);
175 if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
180 for (p = configs; *p; p++) {
181 package_cmd(cmd, *p);
188 static int uci_do_section_cmd(int cmd, int argc, char **argv)
190 struct uci_package *p = NULL;
191 struct uci_element *e = NULL;
192 char *package = NULL;
193 char *section = NULL;
210 if (uci_parse_tuple(ctx, argv[1], &package, §ion, &option, ptr) != UCI_OK)
213 if (uci_load(ctx, package, &p) != UCI_OK) {
222 if (uci_lookup(ctx, &e, p, section, option) != UCI_OK)
226 case UCI_TYPE_SECTION:
227 value = uci_to_section(e)->type;
229 case UCI_TYPE_OPTION:
230 value = uci_to_option(e)->value;
233 /* should not happen */
236 /* throw the value to stdout */
237 printf("%s\n", value);
240 ret = uci_rename(ctx, p, section, option, value);
243 ret = uci_set(ctx, p, section, option, value);
246 ret = uci_delete(ctx, p, section, option);
250 /* no save necessary for get */
254 /* save changes, but don't commit them yet */
256 ret = uci_save(ctx, p);
266 static int uci_cmd(int argc, char **argv)
270 if (!strcasecmp(argv[0], "show"))
272 else if (!strcasecmp(argv[0], "export"))
274 else if (!strcasecmp(argv[0], "commit"))
276 else if (!strcasecmp(argv[0], "get"))
278 else if (!strcasecmp(argv[0], "set"))
280 else if (!strcasecmp(argv[0], "ren") ||
281 !strcasecmp(argv[0], "rename"))
283 else if (!strcasecmp(argv[0], "del"))
285 else if (!strcasecmp(argv[0], "import"))
295 return uci_do_section_cmd(cmd, argc, argv);
299 return uci_do_package_cmd(cmd, argc, argv);
301 return uci_do_import(argc, argv);
307 int main(int argc, char **argv)
313 ctx = uci_alloc_context();
315 fprintf(stderr, "Out of memory\n");
319 while((c = getopt(argc, argv, "mf:sSnNq")) != -1) {
322 input = fopen(optarg, "r");
329 flags |= CLI_FLAG_MERGE;
332 ctx->flags |= UCI_FLAG_STRICT;
335 ctx->flags &= ~UCI_FLAG_STRICT;
336 ctx->flags |= UCI_FLAG_PERROR;
339 ctx->flags |= UCI_FLAG_EXPORT_NAME;
342 ctx->flags &= ~UCI_FLAG_EXPORT_NAME;
345 flags |= CLI_FLAG_QUIET;
348 uci_usage(argc, argv);
353 argv[optind - 1] = argv[0];
358 uci_usage(argc, argv);
359 ret = uci_cmd(argc - 1, argv + 1);
363 uci_usage(argc, argv);
365 uci_free_context(ctx);