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