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