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-p <path> add a search path for config change files\n"
57 "\t-P <path> add a search path for config change files and use as default\n"
58 "\t-q quiet mode (don't print error messages)\n"
59 "\t-s force strict mode (stop on parser errors, default)\n"
60 "\t-S disable strict mode\n"
67 static void cli_perror(void)
69 if (flags & CLI_FLAG_QUIET)
72 uci_perror(ctx, appname);
75 static void uci_show_section(struct uci_section *p)
77 struct uci_element *e;
78 const char *cname, *sname;
80 cname = p->package->e.name;
82 printf("%s.%s=%s\n", cname, sname, p->type);
83 uci_foreach_element(&p->options, e) {
84 printf("%s.%s.%s=%s\n", cname, sname, e->name, uci_to_option(e)->value);
88 static void uci_show_package(struct uci_package *p)
90 struct uci_element *e;
92 uci_foreach_element( &p->sections, e) {
93 uci_show_section(uci_to_section(e));
98 static int package_cmd(int cmd, char *package)
100 struct uci_package *p = NULL;
102 if (uci_load(ctx, package, &p) != UCI_OK) {
110 if (uci_commit(ctx, &p, false) != UCI_OK)
114 uci_export(ctx, stdout, p, true);
125 static int uci_do_import(int argc, char **argv)
127 struct uci_package *package = NULL;
136 else if (flags & CLI_FLAG_MERGE)
137 /* need a package to merge */
140 if (flags & CLI_FLAG_MERGE) {
141 if (uci_load(ctx, name, &package) != UCI_OK)
144 ret = uci_import(ctx, input, name, &package, (name != NULL));
146 if (flags & CLI_FLAG_MERGE) {
147 ret = uci_save(ctx, package);
149 struct uci_element *e;
150 /* loop through all config sections and overwrite existing data */
151 uci_foreach_element(&ctx->root, e) {
152 struct uci_package *p = uci_to_package(e);
153 ret = uci_commit(ctx, &p, true);
166 static int uci_do_package_cmd(int cmd, int argc, char **argv)
168 char **configs = NULL;
175 return package_cmd(cmd, argv[1]);
177 if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
182 for (p = configs; *p; p++) {
183 package_cmd(cmd, *p);
190 static int uci_do_section_cmd(int cmd, int argc, char **argv)
192 struct uci_package *p = NULL;
193 struct uci_element *e = NULL;
194 char *package = NULL;
195 char *section = NULL;
212 if (uci_parse_tuple(ctx, argv[1], &package, §ion, &option, ptr) != UCI_OK)
215 if (uci_load(ctx, package, &p) != UCI_OK) {
224 if (uci_lookup(ctx, &e, p, section, option) != UCI_OK)
228 case UCI_TYPE_SECTION:
229 value = uci_to_section(e)->type;
231 case UCI_TYPE_OPTION:
232 value = uci_to_option(e)->value;
235 /* should not happen */
238 /* throw the value to stdout */
239 printf("%s\n", value);
242 ret = uci_rename(ctx, p, section, option, value);
245 ret = uci_set(ctx, p, section, option, value);
248 ret = uci_delete(ctx, p, section, option);
252 /* no save necessary for get */
256 /* save changes, but don't commit them yet */
258 ret = uci_save(ctx, p);
268 static int uci_cmd(int argc, char **argv)
272 if (!strcasecmp(argv[0], "show"))
274 else if (!strcasecmp(argv[0], "export"))
276 else if (!strcasecmp(argv[0], "commit"))
278 else if (!strcasecmp(argv[0], "get"))
280 else if (!strcasecmp(argv[0], "set"))
282 else if (!strcasecmp(argv[0], "ren") ||
283 !strcasecmp(argv[0], "rename"))
285 else if (!strcasecmp(argv[0], "del"))
287 else if (!strcasecmp(argv[0], "import"))
297 return uci_do_section_cmd(cmd, argc, argv);
301 return uci_do_package_cmd(cmd, argc, argv);
303 return uci_do_import(argc, argv);
309 int main(int argc, char **argv)
315 ctx = uci_alloc_context();
317 fprintf(stderr, "Out of memory\n");
321 while((c = getopt(argc, argv, "f:mnNp:P:sSq")) != -1) {
324 input = fopen(optarg, "r");
331 flags |= CLI_FLAG_MERGE;
334 ctx->flags |= UCI_FLAG_STRICT;
337 ctx->flags &= ~UCI_FLAG_STRICT;
338 ctx->flags |= UCI_FLAG_PERROR;
341 ctx->flags |= UCI_FLAG_EXPORT_NAME;
344 ctx->flags &= ~UCI_FLAG_EXPORT_NAME;
347 uci_add_history_path(ctx, optarg);
350 uci_add_history_path(ctx, ctx->savedir);
351 uci_set_savedir(ctx, optarg);
354 flags |= CLI_FLAG_QUIET;
357 uci_usage(argc, argv);
362 argv[optind - 1] = argv[0];
367 uci_usage(argc, argv);
368 ret = uci_cmd(argc - 1, argv + 1);
372 uci_usage(argc, argv);
374 uci_free_context(ctx);