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