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