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