preserve section list order
[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         CLI_FLAG_SHOW_EXT = (1 << 4),
30         CLI_FLAG_NOPLUGINS= (1 << 5),
31 } flags;
32
33 static FILE *input;
34
35 static struct uci_context *ctx;
36 enum {
37         /* section cmds */
38         CMD_GET,
39         CMD_SET,
40         CMD_ADD_LIST,
41         CMD_DEL,
42         CMD_RENAME,
43         CMD_REVERT,
44         CMD_REORDER,
45         /* package cmds */
46         CMD_SHOW,
47         CMD_CHANGES,
48         CMD_EXPORT,
49         CMD_COMMIT,
50         /* other cmds */
51         CMD_ADD,
52         CMD_IMPORT,
53         CMD_HELP,
54 };
55
56 struct uci_type_list {
57         unsigned int idx;
58         const char *name;
59         struct uci_type_list *next;
60 };
61
62 static struct uci_type_list *type_list = NULL;
63 static char *typestr = NULL;
64 static const char *cur_section_ref = NULL;
65
66 static int uci_cmd(int argc, char **argv);
67
68 static void
69 uci_reset_typelist(void)
70 {
71         struct uci_type_list *type;
72         while (type_list != NULL) {
73                         type = type_list;
74                         type_list = type_list->next;
75                         free(type);
76         }
77         if (typestr) {
78                 free(typestr);
79                 typestr = NULL;
80         }
81         cur_section_ref = NULL;
82 }
83
84 static char *
85 uci_lookup_section_ref(struct uci_section *s)
86 {
87         struct uci_type_list *ti = type_list;
88         int maxlen;
89
90         if (!s->anonymous || !(flags & CLI_FLAG_SHOW_EXT))
91                 return s->e.name;
92
93         /* look up in section type list */
94         while (ti) {
95                 if (strcmp(ti->name, s->type) == 0)
96                         break;
97                 ti = ti->next;
98         }
99         if (!ti) {
100                 ti = malloc(sizeof(struct uci_type_list));
101                 memset(ti, 0, sizeof(struct uci_type_list));
102                 ti->next = type_list;
103                 type_list = ti;
104                 ti->name = s->type;
105         }
106
107         maxlen = strlen(s->type) + 1 + 2 + 10;
108         if (!typestr) {
109                 typestr = malloc(maxlen);
110         } else {
111                 typestr = realloc(typestr, maxlen);
112         }
113         sprintf(typestr, "@%s[%d]", ti->name, ti->idx);
114         ti->idx++;
115         return typestr;
116 }
117
118 static void uci_usage(void)
119 {
120         fprintf(stderr,
121                 "Usage: %s [<options>] <command> [<arguments>]\n\n"
122                 "Commands:\n"
123                 "\tbatch\n"
124                 "\texport     [<config>]\n"
125                 "\timport     [<config>]\n"
126                 "\tchanges    [<config>]\n"
127                 "\tcommit     [<config>]\n"
128                 "\tadd        <config> <section-type>\n"
129                 "\tadd_list   <config>.<section>.<option>=<string>\n"
130                 "\tshow       [<config>[.<section>[.<option>]]]\n"
131                 "\tget        <config>.<section>[.<option>]\n"
132                 "\tset        <config>.<section>[.<option>]=<value>\n"
133                 "\tdelete     <config>[.<section[.<option>]]\n"
134                 "\trename     <config>.<section>[.<option>]=<name>\n"
135                 "\trevert     <config>[.<section>[.<option>]]\n"
136                 "\treorder    <config>.<section>=<position>\n"
137                 "\n"
138                 "Options:\n"
139                 "\t-c <path>  set the search path for config files (default: /etc/config)\n"
140                 "\t-d <str>   set the delimiter for list values in uci show\n"
141                 "\t-f <file>  use <file> as input instead of stdin\n"
142                 "\t-L         do not load any plugins\n"
143                 "\t-m         when importing, merge data into an existing package\n"
144                 "\t-n         name unnamed sections on export (default)\n"
145                 "\t-N         don't name unnamed sections\n"
146                 "\t-p <path>  add a search path for config change files\n"
147                 "\t-P <path>  add a search path for config change files and use as default\n"
148                 "\t-q         quiet mode (don't print error messages)\n"
149                 "\t-s         force strict mode (stop on parser errors, default)\n"
150                 "\t-S         disable strict mode\n"
151                 "\t-X         do not use extended syntax on 'show'\n"
152                 "\n",
153                 appname
154         );
155 }
156
157 static void cli_perror(void)
158 {
159         if (flags & CLI_FLAG_QUIET)
160                 return;
161
162         uci_perror(ctx, appname);
163 }
164
165 static void uci_show_value(struct uci_option *o)
166 {
167         struct uci_element *e;
168         bool sep = false;
169
170         switch(o->type) {
171         case UCI_TYPE_STRING:
172                 printf("%s\n", o->v.string);
173                 break;
174         case UCI_TYPE_LIST:
175                 uci_foreach_element(&o->v.list, e) {
176                         printf("%s%s", (sep ? delimiter : ""), e->name);
177                         sep = true;
178                 }
179                 printf("\n");
180                 break;
181         default:
182                 printf("<unknown>\n");
183                 break;
184         }
185 }
186
187 static void uci_show_option(struct uci_option *o)
188 {
189         printf("%s.%s.%s=",
190                 o->section->package->e.name,
191                 (cur_section_ref ? cur_section_ref : o->section->e.name),
192                 o->e.name);
193         uci_show_value(o);
194 }
195
196 static void uci_show_section(struct uci_section *s)
197 {
198         struct uci_element *e;
199         const char *cname;
200         const char *sname;
201
202         cname = s->package->e.name;
203         sname = (cur_section_ref ? cur_section_ref : s->e.name);
204         printf("%s.%s=%s\n", cname, sname, s->type);
205         uci_foreach_element(&s->options, e) {
206                 uci_show_option(uci_to_option(e));
207         }
208 }
209
210 static void uci_show_package(struct uci_package *p)
211 {
212         struct uci_element *e;
213
214         uci_reset_typelist();
215         uci_foreach_element( &p->sections, e) {
216                 struct uci_section *s = uci_to_section(e);
217                 cur_section_ref = uci_lookup_section_ref(s);
218                 uci_show_section(s);
219         }
220         uci_reset_typelist();
221 }
222
223 static void uci_show_changes(struct uci_package *p)
224 {
225         struct uci_element *e;
226
227         uci_foreach_element(&p->saved_history, e) {
228                 struct uci_history *h = uci_to_history(e);
229                 char *prefix = "";
230                 char *op = "=";
231
232                 switch(h->cmd) {
233                 case UCI_CMD_REMOVE:
234                         prefix = "-";
235                         break;
236                 case UCI_CMD_LIST_ADD:
237                         op = "+=";
238                         break;
239                 default:
240                         break;
241                 }
242                 printf("%s%s.%s", prefix, p->e.name, h->section);
243                 if (e->name)
244                         printf(".%s", e->name);
245                 if (h->cmd != UCI_CMD_REMOVE)
246                         printf("%s%s", op, h->value);
247                 printf("\n");
248         }
249 }
250
251 static int package_cmd(int cmd, char *tuple)
252 {
253         struct uci_element *e = NULL;
254         struct uci_ptr ptr;
255
256         if (uci_lookup_ptr(ctx, &ptr, tuple, true) != UCI_OK) {
257                 cli_perror();
258                 return 1;
259         }
260
261         e = ptr.last;
262         switch(cmd) {
263         case CMD_CHANGES:
264                 uci_show_changes(ptr.p);
265                 break;
266         case CMD_COMMIT:
267                 if (flags & CLI_FLAG_NOCOMMIT)
268                         return 0;
269                 if (uci_commit(ctx, &ptr.p, false) != UCI_OK)
270                         cli_perror();
271                 break;
272         case CMD_EXPORT:
273                 uci_export(ctx, stdout, ptr.p, true);
274                 break;
275         case CMD_SHOW:
276                 if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
277                         ctx->err = UCI_ERR_NOTFOUND;
278                         cli_perror();
279                         return 1;
280                 }
281                 switch(e->type) {
282                         case UCI_TYPE_PACKAGE:
283                                 uci_show_package(ptr.p);
284                                 break;
285                         case UCI_TYPE_SECTION:
286                                 uci_show_section(ptr.s);
287                                 break;
288                         case UCI_TYPE_OPTION:
289                                 uci_show_option(ptr.o);
290                                 break;
291                         default:
292                                 /* should not happen */
293                                 return 1;
294                 }
295                 break;
296         }
297
298         uci_unload(ctx, ptr.p);
299         return 0;
300 }
301
302 static int uci_do_import(int argc, char **argv)
303 {
304         struct uci_package *package = NULL;
305         char *name = NULL;
306         int ret = UCI_OK;
307         bool merge = false;
308
309         if (argc > 2)
310                 return 255;
311
312         if (argc == 2)
313                 name = argv[1];
314         else if (flags & CLI_FLAG_MERGE)
315                 /* need a package to merge */
316                 return 255;
317
318         if (flags & CLI_FLAG_MERGE) {
319                 if (uci_load(ctx, name, &package) != UCI_OK)
320                         package = NULL;
321                 else
322                         merge = true;
323         }
324         ret = uci_import(ctx, input, name, &package, (name != NULL));
325         if (ret == UCI_OK) {
326                 if (merge) {
327                         ret = uci_save(ctx, package);
328                 } else {
329                         struct uci_element *e;
330                         /* loop through all config sections and overwrite existing data */
331                         uci_foreach_element(&ctx->root, e) {
332                                 struct uci_package *p = uci_to_package(e);
333                                 ret = uci_commit(ctx, &p, true);
334                         }
335                 }
336         }
337
338         if (ret != UCI_OK) {
339                 cli_perror();
340                 return 1;
341         }
342
343         return 0;
344 }
345
346 static int uci_do_package_cmd(int cmd, int argc, char **argv)
347 {
348         char **configs = NULL;
349         char **p;
350
351         if (argc > 2)
352                 return 255;
353
354         if (argc == 2)
355                 return package_cmd(cmd, argv[1]);
356
357         if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
358                 cli_perror();
359                 return 1;
360         }
361
362         for (p = configs; *p; p++) {
363                 package_cmd(cmd, *p);
364         }
365
366         return 0;
367 }
368
369 static int uci_do_add(int argc, char **argv)
370 {
371         struct uci_package *p = NULL;
372         struct uci_section *s = NULL;
373         int ret;
374
375         if (argc != 3)
376                 return 255;
377
378         ret = uci_load(ctx, argv[1], &p);
379         if (ret != UCI_OK)
380                 goto done;
381
382         ret = uci_add_section(ctx, p, argv[2], &s);
383         if (ret != UCI_OK)
384                 goto done;
385
386         ret = uci_save(ctx, p);
387
388 done:
389         if (ret != UCI_OK)
390                 cli_perror();
391         else if (s)
392                 fprintf(stdout, "%s\n", s->e.name);
393
394         return ret;
395 }
396
397 static int uci_do_section_cmd(int cmd, int argc, char **argv)
398 {
399         struct uci_element *e;
400         struct uci_ptr ptr;
401         int ret = UCI_OK;
402
403         if (argc != 2)
404                 return 255;
405
406         if (uci_lookup_ptr(ctx, &ptr, argv[1], true) != UCI_OK) {
407                 cli_perror();
408                 return 1;
409         }
410
411         if (ptr.value && (cmd != CMD_SET) && (cmd != CMD_ADD_LIST) && (cmd != CMD_RENAME) && (cmd != CMD_REORDER))
412                 return 1;
413
414         e = ptr.last;
415         switch(cmd) {
416         case CMD_GET:
417                 if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
418                         ctx->err = UCI_ERR_NOTFOUND;
419                         cli_perror();
420                         return 1;
421                 }
422                 switch(e->type) {
423                 case UCI_TYPE_SECTION:
424                         printf("%s\n", ptr.s->type);
425                         break;
426                 case UCI_TYPE_OPTION:
427                         uci_show_value(ptr.o);
428                         break;
429                 default:
430                         break;
431                 }
432                 /* throw the value to stdout */
433                 break;
434         case CMD_RENAME:
435                 ret = uci_rename(ctx, &ptr);
436                 break;
437         case CMD_REVERT:
438                 ret = uci_revert(ctx, &ptr);
439                 break;
440         case CMD_SET:
441                 ret = uci_set(ctx, &ptr);
442                 break;
443         case CMD_ADD_LIST:
444                 ret = uci_add_list(ctx, &ptr);
445                 break;
446         case CMD_REORDER:
447                 if (!ptr.s) {
448                         ctx->err = UCI_ERR_NOTFOUND;
449                         cli_perror();
450                         return 1;
451                 }
452                 ret = uci_reorder_section(ctx, ptr.s, strtoul(ptr.value, NULL, 10));
453                 break;
454         case CMD_DEL:
455                 ret = uci_delete(ctx, &ptr);
456                 break;
457         }
458
459         /* no save necessary for get */
460         if ((cmd == CMD_GET) || (cmd == CMD_REVERT))
461                 return 0;
462
463         /* save changes, but don't commit them yet */
464         if (ret == UCI_OK)
465                 ret = uci_save(ctx, ptr.p);
466
467         if (ret != UCI_OK) {
468                 cli_perror();
469                 return 1;
470         }
471
472         return 0;
473 }
474
475 static int uci_batch_cmd(void)
476 {
477         char *argv[MAX_ARGS + 2];
478         char *str = NULL;
479         int ret = 0;
480         int i, j;
481
482         for(i = 0; i <= MAX_ARGS; i++) {
483                 if (i == MAX_ARGS) {
484                         fprintf(stderr, "Too many arguments\n");
485                         return 1;
486                 }
487                 argv[i] = NULL;
488                 if ((ret = uci_parse_argument(ctx, input, &str, &argv[i])) != UCI_OK) {
489                         cli_perror();
490                         i = 0;
491                         break;
492                 }
493                 if (!argv[i][0])
494                         break;
495                 argv[i] = strdup(argv[i]);
496                 if (!argv[i]) {
497                         perror("uci");
498                         return 1;
499                 }
500         }
501         argv[i] = NULL;
502
503         if (i > 0) {
504                 if (!strcasecmp(argv[0], "exit"))
505                         return 254;
506                 ret = uci_cmd(i, argv);
507         } else
508                 return 0;
509
510         for (j = 0; j < i; j++) {
511                 if (argv[j])
512                         free(argv[j]);
513         }
514
515         return ret;
516 }
517
518 static int uci_batch(void)
519 {
520         int ret = 0;
521
522         flags |= CLI_FLAG_BATCH;
523         while (!feof(input)) {
524                 struct uci_element *e, *tmp;
525
526                 ret = uci_batch_cmd();
527                 if (ret == 254)
528                         return 0;
529                 else if (ret == 255)
530                         fprintf(stderr, "Unknown command\n");
531
532                 /* clean up */
533                 uci_foreach_element_safe(&ctx->root, tmp, e) {
534                         uci_unload(ctx, uci_to_package(e));
535                 }
536         }
537         flags &= ~CLI_FLAG_BATCH;
538
539         return 0;
540 }
541
542 static int uci_cmd(int argc, char **argv)
543 {
544         int cmd = 0;
545
546         if (!strcasecmp(argv[0], "batch") && !(flags & CLI_FLAG_BATCH))
547                 return uci_batch();
548         else if (!strcasecmp(argv[0], "show"))
549                 cmd = CMD_SHOW;
550         else if (!strcasecmp(argv[0], "changes"))
551                 cmd = CMD_CHANGES;
552         else if (!strcasecmp(argv[0], "export"))
553                 cmd = CMD_EXPORT;
554         else if (!strcasecmp(argv[0], "commit"))
555                 cmd = CMD_COMMIT;
556         else if (!strcasecmp(argv[0], "get"))
557                 cmd = CMD_GET;
558         else if (!strcasecmp(argv[0], "set"))
559                 cmd = CMD_SET;
560         else if (!strcasecmp(argv[0], "ren") ||
561                  !strcasecmp(argv[0], "rename"))
562                 cmd = CMD_RENAME;
563         else if (!strcasecmp(argv[0], "revert"))
564                 cmd = CMD_REVERT;
565         else if (!strcasecmp(argv[0], "reorder"))
566                 cmd = CMD_REORDER;
567         else if (!strcasecmp(argv[0], "del") ||
568                  !strcasecmp(argv[0], "delete"))
569                 cmd = CMD_DEL;
570         else if (!strcasecmp(argv[0], "import"))
571                 cmd = CMD_IMPORT;
572         else if (!strcasecmp(argv[0], "help"))
573                 cmd = CMD_HELP;
574         else if (!strcasecmp(argv[0], "add"))
575                 cmd = CMD_ADD;
576         else if (!strcasecmp(argv[0], "add_list"))
577                 cmd = CMD_ADD_LIST;
578         else
579                 cmd = -1;
580
581         switch(cmd) {
582                 case CMD_ADD_LIST:
583                 case CMD_GET:
584                 case CMD_SET:
585                 case CMD_DEL:
586                 case CMD_RENAME:
587                 case CMD_REVERT:
588                 case CMD_REORDER:
589                         return uci_do_section_cmd(cmd, argc, argv);
590                 case CMD_SHOW:
591                 case CMD_EXPORT:
592                 case CMD_COMMIT:
593                 case CMD_CHANGES:
594                         return uci_do_package_cmd(cmd, argc, argv);
595                 case CMD_IMPORT:
596                         return uci_do_import(argc, argv);
597                 case CMD_ADD:
598                         return uci_do_add(argc, argv);
599                 case CMD_HELP:
600                         uci_usage();
601                         return 0;
602                 default:
603                         return 255;
604         }
605 }
606
607 int main(int argc, char **argv)
608 {
609         int ret;
610         int c;
611
612         flags = CLI_FLAG_SHOW_EXT;
613         appname = argv[0];
614         input = stdin;
615         ctx = uci_alloc_context();
616         if (!ctx) {
617                 fprintf(stderr, "Out of memory\n");
618                 return 1;
619         }
620
621         while((c = getopt(argc, argv, "c:d:f:LmnNp:P:sSqX")) != -1) {
622                 switch(c) {
623                         case 'c':
624                                 uci_set_confdir(ctx, optarg);
625                                 break;
626                         case 'd':
627                                 delimiter = optarg;
628                                 break;
629                         case 'f':
630                                 input = fopen(optarg, "r");
631                                 if (!input) {
632                                         perror("uci");
633                                         return 1;
634                                 }
635                                 break;
636                         case 'L':
637                                 flags |= CLI_FLAG_NOPLUGINS;
638                                 break;
639                         case 'm':
640                                 flags |= CLI_FLAG_MERGE;
641                                 break;
642                         case 's':
643                                 ctx->flags |= UCI_FLAG_STRICT;
644                                 break;
645                         case 'S':
646                                 ctx->flags &= ~UCI_FLAG_STRICT;
647                                 ctx->flags |= UCI_FLAG_PERROR;
648                                 break;
649                         case 'n':
650                                 ctx->flags |= UCI_FLAG_EXPORT_NAME;
651                                 break;
652                         case 'N':
653                                 ctx->flags &= ~UCI_FLAG_EXPORT_NAME;
654                                 break;
655                         case 'p':
656                                 uci_add_history_path(ctx, optarg);
657                                 break;
658                         case 'P':
659                                 uci_add_history_path(ctx, ctx->savedir);
660                                 uci_set_savedir(ctx, optarg);
661                                 flags |= CLI_FLAG_NOCOMMIT;
662                                 break;
663                         case 'q':
664                                 flags |= CLI_FLAG_QUIET;
665                                 break;
666                         case 'X':
667                                 flags &= ~CLI_FLAG_SHOW_EXT;
668                                 break;
669                         default:
670                                 uci_usage();
671                                 return 0;
672                 }
673         }
674         if (optind > 1)
675                 argv[optind - 1] = argv[0];
676         argv += optind - 1;
677         argc -= optind - 1;
678
679         if (argc < 2) {
680                 uci_usage();
681                 return 0;
682         }
683
684         if (!(flags & CLI_FLAG_NOPLUGINS))
685                 uci_load_plugins(ctx, NULL);
686
687         ret = uci_cmd(argc - 1, argv + 1);
688         if (input != stdin)
689                 fclose(input);
690         if (ret == 255) {
691                 uci_usage();
692                 return 0;
693         }
694
695         uci_free_context(ctx);
696
697         return ret;
698 }