add list support
[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
159 if (h->cmd == UCI_CMD_REMOVE)
160 printf("-");
161 printf("%s.%s", p->e.name, h->section);
162 if (e->name)
163 printf(".%s", e->name);
164 if (h->cmd != UCI_CMD_REMOVE)
165 printf("=%s", h->value);
166 printf("\n");
167 }
168 }
169
170 static int package_cmd(int cmd, char *tuple)
171 {
172 struct uci_package *p;
173 struct uci_section *s;
174 struct uci_element *e = NULL;
175
176 if (uci_lookup_ext(ctx, &e, tuple) != UCI_OK) {
177 cli_perror();
178 return 1;
179 }
180 switch(e->type) {
181 case UCI_TYPE_PACKAGE:
182 p = uci_to_package(e);
183 break;
184 case UCI_TYPE_SECTION:
185 s = uci_to_section(e);
186 p = s->package;
187 break;
188 case UCI_TYPE_OPTION:
189 s = uci_to_option(e)->section;
190 p = s->package;
191 break;
192 default:
193 return 0;
194 }
195
196 switch(cmd) {
197 case CMD_CHANGES:
198 uci_show_changes(p);
199 break;
200 case CMD_COMMIT:
201 if (flags & CLI_FLAG_NOCOMMIT)
202 return 0;
203 if (uci_commit(ctx, &p, false) != UCI_OK)
204 cli_perror();
205 break;
206 case CMD_EXPORT:
207 uci_export(ctx, stdout, p, true);
208 break;
209 case CMD_SHOW:
210 switch(e->type) {
211 case UCI_TYPE_PACKAGE:
212 uci_show_package(p);
213 break;
214 case UCI_TYPE_SECTION:
215 uci_show_section(uci_to_section(e));
216 break;
217 case UCI_TYPE_OPTION:
218 uci_show_option(uci_to_option(e));
219 break;
220 default:
221 /* should not happen */
222 return 1;
223 }
224 break;
225 }
226
227 uci_unload(ctx, 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_package *p = NULL;
329 struct uci_section *s = NULL;
330 struct uci_element *e = NULL;
331 struct uci_option *o = NULL;
332 char *section = NULL;
333 char *option = NULL;
334 char *value = NULL;
335 int ret = UCI_OK;
336
337 if (argc != 2)
338 return 255;
339
340 value = strchr(argv[1], '=');
341 if (value) {
342 *value = 0;
343 value++;
344 if (!uci_validate_text(value))
345 return 1;
346 }
347
348 if (value && (cmd != CMD_SET) && (cmd != CMD_ADD_LIST) && (cmd != CMD_RENAME))
349 return 1;
350
351 if (uci_lookup_ext(ctx, &e, argv[1]) != UCI_OK) {
352 cli_perror();
353 return 1;
354 }
355
356 switch(e->type) {
357 case UCI_TYPE_PACKAGE:
358 p = uci_to_package(e);
359 break;
360 case UCI_TYPE_SECTION:
361 s = uci_to_section(e);
362 break;
363 case UCI_TYPE_OPTION:
364 option = e->name;
365 s = uci_to_option(e)->section;
366 break;
367 default:
368 return 1;
369 }
370 if (s) {
371 section = s->e.name;
372 p = s->package;
373 }
374
375 switch(cmd) {
376 case CMD_GET:
377 switch(e->type) {
378 case UCI_TYPE_SECTION:
379 printf("%s\n", s->type);
380 break;
381 case UCI_TYPE_OPTION:
382 o = uci_to_option(e);
383 uci_show_value(o);
384 break;
385 default:
386 break;
387 }
388 /* throw the value to stdout */
389 break;
390 case CMD_RENAME:
391 ret = uci_rename(ctx, p, section, option, value);
392 break;
393 case CMD_REVERT:
394 ret = uci_revert(ctx, &p, section, option);
395 break;
396 case CMD_SET:
397 ret = uci_set(ctx, p, section, option, value, NULL);
398 break;
399 case CMD_ADD_LIST:
400 ret = uci_add_list(ctx, p, section, option, value, NULL);
401 break;
402 case CMD_DEL:
403 ret = uci_delete(ctx, p, section, option);
404 break;
405 }
406
407 /* no save necessary for get */
408 if ((cmd == CMD_GET) || (cmd == CMD_REVERT))
409 return 0;
410
411 /* save changes, but don't commit them yet */
412 if (ret == UCI_OK)
413 ret = uci_save(ctx, p);
414
415 if (ret != UCI_OK) {
416 cli_perror();
417 return 1;
418 }
419
420 return 0;
421 }
422
423 static int uci_batch_cmd(void)
424 {
425 char *argv[MAX_ARGS];
426 char *str = NULL;
427 int ret = 0;
428 int i, j;
429
430 for(i = 0; i <= MAX_ARGS; i++) {
431 if (i == MAX_ARGS) {
432 fprintf(stderr, "Too many arguments\n");
433 return 1;
434 }
435 argv[i] = NULL;
436 if ((ret = uci_parse_argument(ctx, input, &str, &argv[i])) != UCI_OK) {
437 cli_perror();
438 i = 0;
439 break;
440 }
441 if (!argv[i][0])
442 break;
443 argv[i] = strdup(argv[i]);
444 if (!argv[i]) {
445 perror("uci");
446 return 1;
447 }
448 }
449 argv[i] = NULL;
450
451 if (i > 0) {
452 if (!strcasecmp(argv[0], "exit"))
453 return 254;
454 ret = uci_cmd(i, argv);
455 } else
456 return 0;
457
458 for (j = 0; j < i; j++) {
459 if (argv[j])
460 free(argv[j]);
461 }
462
463 return ret;
464 }
465
466 static int uci_batch(void)
467 {
468 int ret = 0;
469
470 while (!feof(input)) {
471 struct uci_element *e, *tmp;
472
473 ret = uci_batch_cmd();
474 if (ret == 254)
475 return 0;
476 else if (ret == 255)
477 fprintf(stderr, "Unknown command\n");
478
479 /* clean up */
480 uci_foreach_element_safe(&ctx->root, tmp, e) {
481 uci_unload(ctx, uci_to_package(e));
482 }
483 }
484 return 0;
485 }
486
487 static int uci_cmd(int argc, char **argv)
488 {
489 int cmd = 0;
490
491 if (!strcasecmp(argv[0], "batch") && !(flags & CLI_FLAG_BATCH))
492 return uci_batch();
493 else if (!strcasecmp(argv[0], "show"))
494 cmd = CMD_SHOW;
495 else if (!strcasecmp(argv[0], "changes"))
496 cmd = CMD_CHANGES;
497 else if (!strcasecmp(argv[0], "export"))
498 cmd = CMD_EXPORT;
499 else if (!strcasecmp(argv[0], "commit"))
500 cmd = CMD_COMMIT;
501 else if (!strcasecmp(argv[0], "get"))
502 cmd = CMD_GET;
503 else if (!strcasecmp(argv[0], "set"))
504 cmd = CMD_SET;
505 else if (!strcasecmp(argv[0], "ren") ||
506 !strcasecmp(argv[0], "rename"))
507 cmd = CMD_RENAME;
508 else if (!strcasecmp(argv[0], "revert"))
509 cmd = CMD_REVERT;
510 else if (!strcasecmp(argv[0], "del"))
511 cmd = CMD_DEL;
512 else if (!strcasecmp(argv[0], "import"))
513 cmd = CMD_IMPORT;
514 else if (!strcasecmp(argv[0], "help"))
515 cmd = CMD_HELP;
516 else if (!strcasecmp(argv[0], "add"))
517 cmd = CMD_ADD;
518 else if (!strcasecmp(argv[0], "add_list"))
519 cmd = CMD_ADD_LIST;
520 else
521 cmd = -1;
522
523 switch(cmd) {
524 case CMD_ADD_LIST:
525 case CMD_GET:
526 case CMD_SET:
527 case CMD_DEL:
528 case CMD_RENAME:
529 case CMD_REVERT:
530 return uci_do_section_cmd(cmd, argc, argv);
531 case CMD_SHOW:
532 case CMD_EXPORT:
533 case CMD_COMMIT:
534 case CMD_CHANGES:
535 return uci_do_package_cmd(cmd, argc, argv);
536 case CMD_IMPORT:
537 return uci_do_import(argc, argv);
538 case CMD_ADD:
539 return uci_do_add(argc, argv);
540 case CMD_HELP:
541 uci_usage();
542 return 0;
543 default:
544 return 255;
545 }
546 }
547
548 int main(int argc, char **argv)
549 {
550 int ret;
551 int c;
552
553 appname = argv[0];
554 input = stdin;
555 ctx = uci_alloc_context();
556 if (!ctx) {
557 fprintf(stderr, "Out of memory\n");
558 return 1;
559 }
560
561 while((c = getopt(argc, argv, "c:d:f:mnNp:P:sSq")) != -1) {
562 switch(c) {
563 case 'c':
564 uci_set_confdir(ctx, optarg);
565 break;
566 case 'd':
567 delimiter = optarg;
568 break;
569 case 'f':
570 input = fopen(optarg, "r");
571 if (!input) {
572 perror("uci");
573 return 1;
574 }
575 break;
576 case 'm':
577 flags |= CLI_FLAG_MERGE;
578 break;
579 case 's':
580 ctx->flags |= UCI_FLAG_STRICT;
581 break;
582 case 'S':
583 ctx->flags &= ~UCI_FLAG_STRICT;
584 ctx->flags |= UCI_FLAG_PERROR;
585 break;
586 case 'n':
587 ctx->flags |= UCI_FLAG_EXPORT_NAME;
588 break;
589 case 'N':
590 ctx->flags &= ~UCI_FLAG_EXPORT_NAME;
591 break;
592 case 'p':
593 uci_add_history_path(ctx, optarg);
594 break;
595 case 'P':
596 uci_add_history_path(ctx, ctx->savedir);
597 uci_set_savedir(ctx, optarg);
598 flags |= CLI_FLAG_NOCOMMIT;
599 break;
600 case 'q':
601 flags |= CLI_FLAG_QUIET;
602 break;
603 default:
604 uci_usage();
605 return 0;
606 }
607 }
608 if (optind > 1)
609 argv[optind - 1] = argv[0];
610 argv += optind - 1;
611 argc -= optind - 1;
612
613 if (argc < 2) {
614 uci_usage();
615 return 0;
616 }
617 ret = uci_cmd(argc - 1, argv + 1);
618 if (input != stdin)
619 fclose(input);
620 if (ret == 255) {
621 uci_usage();
622 return 0;
623 }
624
625 uci_free_context(ctx);
626
627 return ret;
628 }