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