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, default)\n"
54 "\t-S disable strict mode\n"
55 "\t-n name unnamed sections on export (default)\n"
56 "\t-N don't name unnamed sections\n"
63 static void uci_show_section(struct uci_section *p)
65 struct uci_element *e;
66 const char *cname, *sname;
68 cname = p->package->e.name;
70 printf("%s.%s=%s\n", cname, sname, p->type);
71 uci_foreach_element(&p->options, e) {
72 printf("%s.%s.%s=%s\n", cname, sname, e->name, uci_to_option(e)->value);
76 static void uci_show_package(struct uci_package *p)
78 struct uci_element *e;
80 uci_foreach_element( &p->sections, e) {
81 uci_show_section(uci_to_section(e));
86 static int package_cmd(int cmd, char *package)
88 struct uci_package *p = NULL;
90 if (uci_load(ctx, package, &p) != UCI_OK) {
91 uci_perror(ctx, appname);
96 if (uci_commit(ctx, &p, false) != UCI_OK)
97 uci_perror(ctx, appname);
100 uci_export(ctx, stdout, p, true);
111 static int uci_do_import(int argc, char **argv)
113 struct uci_package *package = NULL;
122 else if (flags & CLI_FLAG_MERGE)
123 /* need a package to merge */
126 if (flags & CLI_FLAG_MERGE) {
127 if (uci_load(ctx, name, &package) != UCI_OK)
130 ret = uci_import(ctx, input, name, &package, (name != NULL));
132 if (flags & CLI_FLAG_MERGE) {
133 ret = uci_save(ctx, package);
135 struct uci_element *e;
136 /* loop through all config sections and overwrite existing data */
137 uci_foreach_element(&ctx->root, e) {
138 struct uci_package *p = uci_to_package(e);
139 ret = uci_commit(ctx, &p, true);
145 uci_perror(ctx, appname);
152 static int uci_do_package_cmd(int cmd, int argc, char **argv)
154 char **configs = NULL;
161 return package_cmd(cmd, argv[1]);
163 if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
164 uci_perror(ctx, appname);
168 for (p = configs; *p; p++) {
169 package_cmd(cmd, *p);
176 static int uci_do_section_cmd(int cmd, int argc, char **argv)
178 struct uci_package *p = NULL;
179 struct uci_element *e = NULL;
180 char *package = NULL;
181 char *section = NULL;
198 if (uci_parse_tuple(ctx, argv[1], &package, §ion, &option, ptr) != UCI_OK)
201 if (uci_load(ctx, package, &p) != UCI_OK) {
202 uci_perror(ctx, appname);
208 if (uci_lookup(ctx, &e, p, section, option) != UCI_OK)
212 case UCI_TYPE_SECTION:
213 value = uci_to_section(e)->type;
215 case UCI_TYPE_OPTION:
216 value = uci_to_option(e)->value;
219 /* should not happen */
222 /* throw the value to stdout */
223 printf("%s\n", value);
226 ret = uci_rename(ctx, p, section, option, value);
229 ret = uci_set(ctx, p, section, option, value);
232 ret = uci_delete(ctx, p, section, option);
236 /* no save necessary for get */
240 /* save changes, but don't commit them yet */
242 ret = uci_save(ctx, p);
245 uci_perror(ctx, appname);
252 static int uci_cmd(int argc, char **argv)
256 if (!strcasecmp(argv[0], "show"))
258 else if (!strcasecmp(argv[0], "export"))
260 else if (!strcasecmp(argv[0], "commit"))
262 else if (!strcasecmp(argv[0], "get"))
264 else if (!strcasecmp(argv[0], "set"))
266 else if (!strcasecmp(argv[0], "ren") ||
267 !strcasecmp(argv[0], "rename"))
269 else if (!strcasecmp(argv[0], "del"))
271 else if (!strcasecmp(argv[0], "import"))
281 return uci_do_section_cmd(cmd, argc, argv);
285 return uci_do_package_cmd(cmd, argc, argv);
287 return uci_do_import(argc, argv);
293 int main(int argc, char **argv)
298 ctx = uci_alloc_context();
300 fprintf(stderr, "Out of memory\n");
304 while((c = getopt(argc, argv, "mf:sSnN")) != -1) {
307 input = fopen(optarg, "r");
314 flags |= CLI_FLAG_MERGE;
317 ctx->flags |= UCI_FLAG_STRICT;
320 ctx->flags &= ~UCI_FLAG_STRICT;
321 ctx->flags |= UCI_FLAG_PERROR;
324 ctx->flags |= UCI_FLAG_EXPORT_NAME;
327 ctx->flags &= ~UCI_FLAG_EXPORT_NAME;
330 uci_usage(argc, argv);
335 argv[optind - 1] = argv[0];
340 uci_usage(argc, argv);
341 ret = uci_cmd(argc - 1, argv + 1);
345 uci_usage(argc, argv);
347 uci_free_context(ctx);