allow the cli to override the confdir
[project/uci.git] / cli.c
1 /*
2 * cli - Command Line Interface for the Unified Configuration Interface
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
4 *
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
8 *
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.
13 */
14 #include <strings.h>
15 #include <string.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include "uci.h"
19
20 #define MAX_ARGS 4 /* max command line arguments for batch mode */
21
22 static const char *appname;
23 static enum {
24 CLI_FLAG_MERGE = (1 << 0),
25 CLI_FLAG_QUIET = (1 << 1),
26 CLI_FLAG_NOCOMMIT = (1 << 2),
27 CLI_FLAG_BATCH = (1 << 3),
28 } flags;
29
30 static FILE *input;
31
32 static struct uci_context *ctx;
33 enum {
34 /* section cmds */
35 CMD_GET,
36 CMD_SET,
37 CMD_DEL,
38 CMD_RENAME,
39 CMD_REVERT,
40 /* package cmds */
41 CMD_SHOW,
42 CMD_CHANGES,
43 CMD_EXPORT,
44 CMD_COMMIT,
45 /* other cmds */
46 CMD_ADD,
47 CMD_IMPORT,
48 CMD_HELP,
49 };
50
51 static int uci_cmd(int argc, char **argv);
52
53 static void uci_usage(void)
54 {
55 fprintf(stderr,
56 "Usage: %s [<options>] <command> [<arguments>]\n\n"
57 "Commands:\n"
58 "\tbatch\n"
59 "\texport [<config>]\n"
60 "\timport [<config>]\n"
61 "\tchanges [<config>]\n"
62 "\tcommit [<config>]\n"
63 "\tadd <config> <section-type>\n"
64 "\tshow [<config>[.<section>[.<option>]]]\n"
65 "\tget <config>.<section>[.<option>]\n"
66 "\tset <config>.<section>[.<option>]=<value>\n"
67 "\trename <config>.<section>[.<option>]=<name>\n"
68 "\trevert <config>[.<section>[.<option>]]\n"
69 "\n"
70 "Options:\n"
71 "\t-c <path> set the search path for config files (default: /etc/config)\n"
72 "\t-f <file> use <file> as input instead of stdin\n"
73 "\t-m when importing, merge data into an existing package\n"
74 "\t-n name unnamed sections on export (default)\n"
75 "\t-N don't name unnamed sections\n"
76 "\t-p <path> add a search path for config change files\n"
77 "\t-P <path> add a search path for config change files and use as default\n"
78 "\t-q quiet mode (don't print error messages)\n"
79 "\t-s force strict mode (stop on parser errors, default)\n"
80 "\t-S disable strict mode\n"
81 "\n",
82 appname
83 );
84 }
85
86 static void cli_perror(void)
87 {
88 if (flags & CLI_FLAG_QUIET)
89 return;
90
91 uci_perror(ctx, appname);
92 }
93
94 static void uci_show_section(struct uci_section *p)
95 {
96 struct uci_element *e;
97 const char *cname, *sname;
98
99 cname = p->package->e.name;
100 sname = p->e.name;
101 printf("%s.%s=%s\n", cname, sname, p->type);
102 uci_foreach_element(&p->options, e) {
103 printf("%s.%s.%s=%s\n", cname, sname, e->name, uci_to_option(e)->value);
104 }
105 }
106
107 static void uci_show_package(struct uci_package *p)
108 {
109 struct uci_element *e;
110
111 uci_foreach_element( &p->sections, e) {
112 uci_show_section(uci_to_section(e));
113 }
114 }
115
116 static void uci_show_changes(struct uci_package *p)
117 {
118 struct uci_element *e;
119
120 uci_foreach_element(&p->saved_history, e) {
121 struct uci_history *h = uci_to_history(e);
122
123 if (h->cmd == UCI_CMD_REMOVE)
124 printf("-");
125 printf("%s.%s", p->e.name, h->section);
126 if (e->name)
127 printf(".%s", e->name);
128 if (h->cmd != UCI_CMD_REMOVE)
129 printf("=%s", h->value);
130 printf("\n");
131 }
132 }
133
134 static int package_cmd(int cmd, char *package)
135 {
136 struct uci_package *p = NULL;
137 int ret;
138
139 ret = uci_load(ctx, package, &p);
140
141 if (ret != UCI_OK) {
142 cli_perror();
143 return 1;
144 }
145 if (!p)
146 return 0;
147 switch(cmd) {
148 case CMD_CHANGES:
149 uci_show_changes(p);
150 break;
151 case CMD_COMMIT:
152 if (flags & CLI_FLAG_NOCOMMIT)
153 return 0;
154 if (uci_commit(ctx, &p, false) != UCI_OK)
155 cli_perror();
156 break;
157 case CMD_EXPORT:
158 uci_export(ctx, stdout, p, true);
159 break;
160 case CMD_SHOW:
161 uci_show_package(p);
162 break;
163 }
164
165 uci_unload(ctx, p);
166 return 0;
167 }
168
169 static int uci_do_import(int argc, char **argv)
170 {
171 struct uci_package *package = NULL;
172 char *name = NULL;
173 int ret = UCI_OK;
174 bool merge = false;
175
176 if (argc > 2)
177 return 255;
178
179 if (argc == 2)
180 name = argv[1];
181 else if (flags & CLI_FLAG_MERGE)
182 /* need a package to merge */
183 return 255;
184
185 if (flags & CLI_FLAG_MERGE) {
186 if (uci_load(ctx, name, &package) != UCI_OK)
187 package = NULL;
188 else
189 merge = true;
190 }
191 ret = uci_import(ctx, input, name, &package, (name != NULL));
192 if (ret == UCI_OK) {
193 if (merge) {
194 ret = uci_save(ctx, package);
195 } else {
196 struct uci_element *e;
197 /* loop through all config sections and overwrite existing data */
198 uci_foreach_element(&ctx->root, e) {
199 struct uci_package *p = uci_to_package(e);
200 ret = uci_commit(ctx, &p, true);
201 }
202 }
203 }
204
205 if (ret != UCI_OK) {
206 cli_perror();
207 return 1;
208 }
209
210 return 0;
211 }
212
213 static int uci_do_package_cmd(int cmd, int argc, char **argv)
214 {
215 char **configs = NULL;
216 char **p;
217
218 if (argc > 2)
219 return 255;
220
221 if (argc == 2)
222 return package_cmd(cmd, argv[1]);
223
224 if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
225 cli_perror();
226 return 1;
227 }
228
229 for (p = configs; *p; p++) {
230 package_cmd(cmd, *p);
231 }
232
233 return 0;
234 }
235
236 static int uci_do_add(int argc, char **argv)
237 {
238 struct uci_package *p = NULL;
239 struct uci_section *s = NULL;
240 int ret;
241
242 if (argc != 3)
243 return 255;
244
245 ret = uci_load(ctx, argv[1], &p);
246 if (ret != UCI_OK)
247 goto done;
248
249 ret = uci_add_section(ctx, p, argv[2], &s);
250 if (ret != UCI_OK)
251 goto done;
252
253 ret = uci_save(ctx, p);
254
255 done:
256 if (ret != UCI_OK)
257 cli_perror();
258 else if (s)
259 fprintf(stdout, "%s\n", s->e.name);
260
261 return ret;
262 }
263
264 static int uci_do_section_cmd(int cmd, int argc, char **argv)
265 {
266 struct uci_package *p = NULL;
267 struct uci_element *e = NULL;
268 char *package = NULL;
269 char *section = NULL;
270 char *option = NULL;
271 char *value = NULL;
272 char **ptr = NULL;
273 int ret = UCI_OK;
274
275 if (argc != 2)
276 return 255;
277
278 switch(cmd) {
279 case CMD_SET:
280 case CMD_RENAME:
281 ptr = &value;
282 break;
283 default:
284 break;
285 }
286 if (uci_parse_tuple(ctx, argv[1], &package, &section, &option, ptr) != UCI_OK)
287 return 1;
288 if (section && !section[0])
289 return 1;
290
291 if (uci_load(ctx, package, &p) != UCI_OK) {
292 cli_perror();
293 return 1;
294 }
295 if (!p)
296 return 0;
297
298 switch(cmd) {
299 case CMD_GET:
300 if (uci_lookup(ctx, &e, p, section, option) != UCI_OK)
301 return 1;
302
303 switch(e->type) {
304 case UCI_TYPE_SECTION:
305 value = uci_to_section(e)->type;
306 break;
307 case UCI_TYPE_OPTION:
308 value = uci_to_option(e)->value;
309 break;
310 default:
311 /* should not happen */
312 return 1;
313 }
314 /* throw the value to stdout */
315 printf("%s\n", value);
316 break;
317 case CMD_RENAME:
318 ret = uci_rename(ctx, p, section, option, value);
319 break;
320 case CMD_REVERT:
321 ret = uci_revert(ctx, &p, section, option);
322 break;
323 case CMD_SET:
324 ret = uci_set(ctx, p, section, option, value, NULL);
325 break;
326 case CMD_DEL:
327 ret = uci_delete(ctx, p, section, option);
328 break;
329 }
330
331 /* no save necessary for get */
332 if ((cmd == CMD_GET) || (cmd == CMD_REVERT))
333 return 0;
334
335 /* save changes, but don't commit them yet */
336 if (ret == UCI_OK)
337 ret = uci_save(ctx, p);
338
339 if (ret != UCI_OK) {
340 cli_perror();
341 return 1;
342 }
343
344 return 0;
345 }
346
347 static int uci_batch_cmd(void)
348 {
349 char *argv[MAX_ARGS];
350 char *str = NULL;
351 int ret = 0;
352 int i, j;
353
354 for(i = 0; i <= MAX_ARGS; i++) {
355 if (i == MAX_ARGS) {
356 fprintf(stderr, "Too many arguments\n");
357 return 1;
358 }
359 argv[i] = NULL;
360 if ((ret = uci_parse_argument(ctx, input, &str, &argv[i])) != UCI_OK) {
361 cli_perror();
362 i = 0;
363 break;
364 }
365 if (!argv[i][0])
366 break;
367 argv[i] = strdup(argv[i]);
368 if (!argv[i]) {
369 perror("uci");
370 return 1;
371 }
372 }
373 argv[i] = NULL;
374
375 if (i > 0) {
376 if (!strcasecmp(argv[0], "exit"))
377 return 254;
378 ret = uci_cmd(i, argv);
379 } else
380 return 0;
381
382 for (j = 0; j < i; j++) {
383 if (argv[j])
384 free(argv[j]);
385 }
386
387 return ret;
388 }
389
390 static int uci_batch(void)
391 {
392 int ret = 0;
393
394 while (!feof(input)) {
395 struct uci_element *e, *tmp;
396
397 ret = uci_batch_cmd();
398 if (ret == 254)
399 return 0;
400 else if (ret == 255)
401 fprintf(stderr, "Unknown command\n");
402
403 /* clean up */
404 uci_foreach_element_safe(&ctx->root, tmp, e) {
405 uci_unload(ctx, uci_to_package(e));
406 }
407 }
408 return 0;
409 }
410
411 static int uci_cmd(int argc, char **argv)
412 {
413 int cmd = 0;
414
415 if (!strcasecmp(argv[0], "batch") && !(flags & CLI_FLAG_BATCH))
416 return uci_batch();
417 else if (!strcasecmp(argv[0], "show"))
418 cmd = CMD_SHOW;
419 else if (!strcasecmp(argv[0], "changes"))
420 cmd = CMD_CHANGES;
421 else if (!strcasecmp(argv[0], "export"))
422 cmd = CMD_EXPORT;
423 else if (!strcasecmp(argv[0], "commit"))
424 cmd = CMD_COMMIT;
425 else if (!strcasecmp(argv[0], "get"))
426 cmd = CMD_GET;
427 else if (!strcasecmp(argv[0], "set"))
428 cmd = CMD_SET;
429 else if (!strcasecmp(argv[0], "ren") ||
430 !strcasecmp(argv[0], "rename"))
431 cmd = CMD_RENAME;
432 else if (!strcasecmp(argv[0], "revert"))
433 cmd = CMD_REVERT;
434 else if (!strcasecmp(argv[0], "del"))
435 cmd = CMD_DEL;
436 else if (!strcasecmp(argv[0], "import"))
437 cmd = CMD_IMPORT;
438 else if (!strcasecmp(argv[0], "help"))
439 cmd = CMD_HELP;
440 else if (!strcasecmp(argv[0], "add"))
441 cmd = CMD_ADD;
442 else
443 cmd = -1;
444
445 switch(cmd) {
446 case CMD_GET:
447 case CMD_SET:
448 case CMD_DEL:
449 case CMD_RENAME:
450 case CMD_REVERT:
451 return uci_do_section_cmd(cmd, argc, argv);
452 case CMD_SHOW:
453 case CMD_EXPORT:
454 case CMD_COMMIT:
455 case CMD_CHANGES:
456 return uci_do_package_cmd(cmd, argc, argv);
457 case CMD_IMPORT:
458 return uci_do_import(argc, argv);
459 case CMD_ADD:
460 return uci_do_add(argc, argv);
461 case CMD_HELP:
462 uci_usage();
463 return 0;
464 default:
465 return 255;
466 }
467 }
468
469 int main(int argc, char **argv)
470 {
471 int ret;
472 int c;
473
474 appname = argv[0];
475 input = stdin;
476 ctx = uci_alloc_context();
477 if (!ctx) {
478 fprintf(stderr, "Out of memory\n");
479 return 1;
480 }
481
482 while((c = getopt(argc, argv, "c:f:mnNp:P:sSq")) != -1) {
483 switch(c) {
484 case 'c':
485 uci_set_confdir(ctx, optarg);
486 break;
487 case 'f':
488 input = fopen(optarg, "r");
489 if (!input) {
490 perror("uci");
491 return 1;
492 }
493 break;
494 case 'm':
495 flags |= CLI_FLAG_MERGE;
496 break;
497 case 's':
498 ctx->flags |= UCI_FLAG_STRICT;
499 break;
500 case 'S':
501 ctx->flags &= ~UCI_FLAG_STRICT;
502 ctx->flags |= UCI_FLAG_PERROR;
503 break;
504 case 'n':
505 ctx->flags |= UCI_FLAG_EXPORT_NAME;
506 break;
507 case 'N':
508 ctx->flags &= ~UCI_FLAG_EXPORT_NAME;
509 break;
510 case 'p':
511 uci_add_history_path(ctx, optarg);
512 break;
513 case 'P':
514 uci_add_history_path(ctx, ctx->savedir);
515 uci_set_savedir(ctx, optarg);
516 flags |= CLI_FLAG_NOCOMMIT;
517 break;
518 case 'q':
519 flags |= CLI_FLAG_QUIET;
520 break;
521 default:
522 uci_usage();
523 return 0;
524 }
525 }
526 if (optind > 1)
527 argv[optind - 1] = argv[0];
528 argv += optind - 1;
529 argc -= optind - 1;
530
531 if (argc < 2) {
532 uci_usage();
533 return 0;
534 }
535 ret = uci_cmd(argc - 1, argv + 1);
536 if (input != stdin)
537 fclose(input);
538 if (ret == 255) {
539 uci_usage();
540 return 0;
541 }
542
543 uci_free_context(ctx);
544
545 return ret;
546 }