add support for merged importing
[project/uci.git] / cli.c
1 /*
2 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
3 *
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
7 *
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.
12 */
13 #include <strings.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include "uci.h"
17
18 static const char *appname = "uci";
19 static enum {
20 CLI_FLAG_MERGE = (1 << 0),
21 } flags;
22 static FILE *input = stdin;
23
24 static struct uci_context *ctx;
25 enum {
26 /* section cmds */
27 CMD_GET,
28 CMD_SET,
29 CMD_DEL,
30 CMD_RENAME,
31 /* package cmds */
32 CMD_SHOW,
33 CMD_IMPORT,
34 CMD_EXPORT,
35 CMD_COMMIT,
36 };
37
38 static void uci_usage(int argc, char **argv)
39 {
40 fprintf(stderr,
41 "Usage: %s [<options>] <command> [<arguments>]\n\n"
42 "Commands:\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"
49 "\n"
50 "Options:\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"
55 "\n",
56 argv[0]
57 );
58 exit(255);
59 }
60
61 static void uci_show_section(struct uci_section *p)
62 {
63 struct uci_element *e;
64 const char *cname, *sname;
65
66 cname = p->package->e.name;
67 sname = p->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);
71 }
72 }
73
74 static void uci_show_package(struct uci_package *p)
75 {
76 struct uci_element *e;
77
78 uci_foreach_element( &p->sections, e) {
79 uci_show_section(uci_to_section(e));
80 }
81 }
82
83
84 static int package_cmd(int cmd, char *package)
85 {
86 struct uci_package *p = NULL;
87
88 if (uci_load(ctx, package, &p) != UCI_OK) {
89 uci_perror(ctx, appname);
90 return 1;
91 }
92 switch(cmd) {
93 case CMD_COMMIT:
94 if (uci_commit(ctx, &p) != UCI_OK)
95 uci_perror(ctx, appname);
96 break;
97 case CMD_EXPORT:
98 uci_export(ctx, stdout, p, true);
99 break;
100 case CMD_SHOW:
101 uci_show_package(p);
102 break;
103 }
104
105 uci_unload(ctx, p);
106 return 0;
107 }
108
109 static int uci_do_import(int argc, char **argv)
110 {
111 struct uci_package *package = NULL;
112 char **configs = NULL;
113 char *name = NULL;
114 int ret = UCI_OK;
115 char **p;
116
117 if (argc > 2)
118 return 255;
119
120 if (argc == 2)
121 name = argv[1];
122 else if (flags & CLI_FLAG_MERGE)
123 /* need a package to merge */
124 return 255;
125
126 if (flags & CLI_FLAG_MERGE) {
127 if (uci_load(ctx, name, &package) != UCI_OK)
128 package = NULL;
129 }
130 ret = uci_import(ctx, input, name, &package, (name != NULL));
131 if ((ret == UCI_OK) && (flags & CLI_FLAG_MERGE)) {
132 ret = uci_save(ctx, package);
133 }
134
135 if (ret != UCI_OK) {
136 uci_perror(ctx, appname);
137 return 1;
138 }
139
140 return 0;
141 }
142
143 static int uci_do_package_cmd(int cmd, int argc, char **argv)
144 {
145 char **configs = NULL;
146 char **p;
147
148 if (argc > 2)
149 return 255;
150
151 if (argc == 2)
152 return package_cmd(cmd, argv[1]);
153
154 if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
155 uci_perror(ctx, appname);
156 return 1;
157 }
158
159 for (p = configs; *p; p++) {
160 package_cmd(cmd, *p);
161 }
162
163 return 0;
164 }
165
166
167 static int uci_do_section_cmd(int cmd, int argc, char **argv)
168 {
169 struct uci_package *p = NULL;
170 struct uci_element *e = NULL;
171 char *package = NULL;
172 char *section = NULL;
173 char *option = NULL;
174 char *value = NULL;
175 char **ptr = NULL;
176 int ret = UCI_OK;
177
178 if (argc != 2)
179 return 255;
180
181 switch(cmd) {
182 case CMD_SET:
183 case CMD_RENAME:
184 ptr = &value;
185 break;
186 default:
187 break;
188 }
189 if (uci_parse_tuple(ctx, argv[1], &package, &section, &option, ptr) != UCI_OK)
190 return 1;
191
192 if (uci_load(ctx, package, &p) != UCI_OK) {
193 uci_perror(ctx, appname);
194 return 1;
195 }
196
197 switch(cmd) {
198 case CMD_GET:
199 if (uci_lookup(ctx, &e, p, section, option) != UCI_OK)
200 return 1;
201
202 switch(e->type) {
203 case UCI_TYPE_SECTION:
204 value = uci_to_section(e)->type;
205 break;
206 case UCI_TYPE_OPTION:
207 value = uci_to_option(e)->value;
208 break;
209 default:
210 /* should not happen */
211 return 1;
212 }
213 /* throw the value to stdout */
214 printf("%s\n", value);
215 break;
216 case CMD_RENAME:
217 ret = uci_rename(ctx, p, section, option, value);
218 break;
219 case CMD_SET:
220 ret = uci_set(ctx, p, section, option, value);
221 break;
222 case CMD_DEL:
223 ret = uci_delete(ctx, p, section, option);
224 break;
225 }
226
227 /* no save necessary for get */
228 if (cmd == CMD_GET)
229 return 0;
230
231 /* save changes, but don't commit them yet */
232 if (ret == UCI_OK)
233 ret = uci_save(ctx, p);
234
235 if (ret != UCI_OK) {
236 uci_perror(ctx, appname);
237 return 1;
238 }
239
240 return 0;
241 }
242
243 static int uci_cmd(int argc, char **argv)
244 {
245 int cmd = 0;
246
247 if (!strcasecmp(argv[0], "show"))
248 cmd = CMD_SHOW;
249 else if (!strcasecmp(argv[0], "export"))
250 cmd = CMD_EXPORT;
251 else if (!strcasecmp(argv[0], "commit"))
252 cmd = CMD_COMMIT;
253 else if (!strcasecmp(argv[0], "get"))
254 cmd = CMD_GET;
255 else if (!strcasecmp(argv[0], "set"))
256 cmd = CMD_SET;
257 else if (!strcasecmp(argv[0], "ren") ||
258 !strcasecmp(argv[0], "rename"))
259 cmd = CMD_RENAME;
260 else if (!strcasecmp(argv[0], "del"))
261 cmd = CMD_DEL;
262 else if (!strcasecmp(argv[0], "import"))
263 cmd = CMD_IMPORT;
264 else
265 cmd = -1;
266
267 switch(cmd) {
268 case CMD_GET:
269 case CMD_SET:
270 case CMD_DEL:
271 case CMD_RENAME:
272 return uci_do_section_cmd(cmd, argc, argv);
273 case CMD_SHOW:
274 case CMD_EXPORT:
275 case CMD_COMMIT:
276 return uci_do_package_cmd(cmd, argc, argv);
277 case CMD_IMPORT:
278 return uci_do_import(argc, argv);
279 default:
280 return 255;
281 }
282 }
283
284 int main(int argc, char **argv)
285 {
286 int ret;
287 int c;
288
289 ctx = uci_alloc_context();
290 if (!ctx) {
291 fprintf(stderr, "Out of memory\n");
292 return 1;
293 }
294
295 while((c = getopt(argc, argv, "mfsS")) != -1) {
296 switch(c) {
297 case 'f':
298 input = fopen(optarg, "r");
299 if (!input) {
300 perror("uci");
301 return 1;
302 }
303 break;
304 case 'm':
305 flags |= CLI_FLAG_MERGE;
306 break;
307 case 's':
308 ctx->flags |= UCI_FLAG_STRICT;
309 break;
310 case 'S':
311 ctx->flags &= ~UCI_FLAG_STRICT;
312 ctx->flags |= UCI_FLAG_PERROR;
313 break;
314 default:
315 uci_usage(argc, argv);
316 break;
317 }
318 }
319 if (optind > 1)
320 argv[optind - 1] = argv[0];
321 argv += optind - 1;
322 argc -= optind - 1;
323
324 if (argc < 2)
325 uci_usage(argc, argv);
326 ret = uci_cmd(argc - 1, argv + 1);
327 if (input != stdin)
328 fclose(input);
329 if (ret == 255)
330 uci_usage(argc, argv);
331
332 uci_free_context(ctx);
333
334 return ret;
335 }