2 * cli - Command Line Interface for the Unified Configuration Interface
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
19 static const char *appname = "uci";
21 CLI_FLAG_MERGE = (1 << 0),
22 CLI_FLAG_QUIET = (1 << 1),
23 CLI_FLAG_NOCOMMIT = (1 << 2),
27 static struct uci_context *ctx;
41 static void uci_usage(int argc, char **argv)
44 "Usage: %s [<options>] <command> [<arguments>]\n\n"
46 "\texport [<config>]\n"
47 "\timport [<config>]\n"
48 "\tshow [<config>[.<section>[.<option>]]]\n"
49 "\tget <config>.<section>[.<option>]\n"
50 "\tset <config>.<section>[.<option>]=<value>\n"
51 "\trename <config>.<section>[.<option>]=<name>\n"
54 "\t-f <file> use <file> as input instead of stdin\n"
55 "\t-m when importing, merge data into an existing package\n"
56 "\t-n name unnamed sections on export (default)\n"
57 "\t-N don't name unnamed sections\n"
58 "\t-p <path> add a search path for config change files\n"
59 "\t-P <path> add a search path for config change files and use as default\n"
60 "\t-q quiet mode (don't print error messages)\n"
61 "\t-s force strict mode (stop on parser errors, default)\n"
62 "\t-S disable strict mode\n"
69 static void cli_perror(void)
71 if (flags & CLI_FLAG_QUIET)
74 uci_perror(ctx, appname);
77 static void uci_show_section(struct uci_section *p)
79 struct uci_element *e;
80 const char *cname, *sname;
82 cname = p->package->e.name;
84 printf("%s.%s=%s\n", cname, sname, p->type);
85 uci_foreach_element(&p->options, e) {
86 printf("%s.%s.%s=%s\n", cname, sname, e->name, uci_to_option(e)->value);
90 static void uci_show_package(struct uci_package *p)
92 struct uci_element *e;
94 uci_foreach_element( &p->sections, e) {
95 uci_show_section(uci_to_section(e));
100 static int package_cmd(int cmd, char *package)
102 struct uci_package *p = NULL;
104 if (uci_load(ctx, package, &p) != UCI_OK) {
112 if (flags & CLI_FLAG_NOCOMMIT)
114 if (uci_commit(ctx, &p, false) != UCI_OK)
118 uci_export(ctx, stdout, p, true);
129 static int uci_do_import(int argc, char **argv)
131 struct uci_package *package = NULL;
140 else if (flags & CLI_FLAG_MERGE)
141 /* need a package to merge */
144 if (flags & CLI_FLAG_MERGE) {
145 if (uci_load(ctx, name, &package) != UCI_OK)
148 ret = uci_import(ctx, input, name, &package, (name != NULL));
150 if (flags & CLI_FLAG_MERGE) {
151 ret = uci_save(ctx, package);
153 struct uci_element *e;
154 /* loop through all config sections and overwrite existing data */
155 uci_foreach_element(&ctx->root, e) {
156 struct uci_package *p = uci_to_package(e);
157 ret = uci_commit(ctx, &p, true);
170 static int uci_do_package_cmd(int cmd, int argc, char **argv)
172 char **configs = NULL;
179 return package_cmd(cmd, argv[1]);
181 if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
186 for (p = configs; *p; p++) {
187 package_cmd(cmd, *p);
194 static int uci_do_section_cmd(int cmd, int argc, char **argv)
196 struct uci_package *p = NULL;
197 struct uci_element *e = NULL;
198 char *package = NULL;
199 char *section = NULL;
216 if (uci_parse_tuple(ctx, argv[1], &package, §ion, &option, ptr) != UCI_OK)
219 if (uci_load(ctx, package, &p) != UCI_OK) {
228 if (uci_lookup(ctx, &e, p, section, option) != UCI_OK)
232 case UCI_TYPE_SECTION:
233 value = uci_to_section(e)->type;
235 case UCI_TYPE_OPTION:
236 value = uci_to_option(e)->value;
239 /* should not happen */
242 /* throw the value to stdout */
243 printf("%s\n", value);
246 ret = uci_rename(ctx, p, section, option, value);
249 ret = uci_set(ctx, p, section, option, value);
252 ret = uci_delete(ctx, p, section, option);
256 /* no save necessary for get */
260 /* save changes, but don't commit them yet */
262 ret = uci_save(ctx, p);
272 static int uci_cmd(int argc, char **argv)
276 if (!strcasecmp(argv[0], "show"))
278 else if (!strcasecmp(argv[0], "export"))
280 else if (!strcasecmp(argv[0], "commit"))
282 else if (!strcasecmp(argv[0], "get"))
284 else if (!strcasecmp(argv[0], "set"))
286 else if (!strcasecmp(argv[0], "ren") ||
287 !strcasecmp(argv[0], "rename"))
289 else if (!strcasecmp(argv[0], "del"))
291 else if (!strcasecmp(argv[0], "import"))
301 return uci_do_section_cmd(cmd, argc, argv);
305 return uci_do_package_cmd(cmd, argc, argv);
307 return uci_do_import(argc, argv);
313 int main(int argc, char **argv)
319 ctx = uci_alloc_context();
321 fprintf(stderr, "Out of memory\n");
325 while((c = getopt(argc, argv, "f:mnNp:P:sSq")) != -1) {
328 input = fopen(optarg, "r");
335 flags |= CLI_FLAG_MERGE;
338 ctx->flags |= UCI_FLAG_STRICT;
341 ctx->flags &= ~UCI_FLAG_STRICT;
342 ctx->flags |= UCI_FLAG_PERROR;
345 ctx->flags |= UCI_FLAG_EXPORT_NAME;
348 ctx->flags &= ~UCI_FLAG_EXPORT_NAME;
351 uci_add_history_path(ctx, optarg);
354 uci_add_history_path(ctx, ctx->savedir);
355 uci_set_savedir(ctx, optarg);
356 flags |= CLI_FLAG_NOCOMMIT;
359 flags |= CLI_FLAG_QUIET;
362 uci_usage(argc, argv);
367 argv[optind - 1] = argv[0];
372 uci_usage(argc, argv);
373 ret = uci_cmd(argc - 1, argv + 1);
377 uci_usage(argc, argv);
379 uci_free_context(ctx);