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