some backend 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                 "\tadd        <config> <section-type>\n"
63                 "\tshow       [<config>[.<section>[.<option>]]]\n"
64                 "\tget        <config>.<section>[.<option>]\n"
65                 "\tset        <config>.<section>[.<option>]=<value>\n"
66                 "\trename     <config>.<section>[.<option>]=<name>\n"
67                 "\trevert     <config>[.<section>[.<option>]]\n"
68                 "\n"
69                 "Options:\n"
70                 "\t-f <file>  use <file> as input instead of stdin\n"
71                 "\t-m         when importing, merge data into an existing package\n"
72                 "\t-n         name unnamed sections on export (default)\n"
73                 "\t-N         don't name unnamed sections\n"
74                 "\t-p <path>  add a search path for config change files\n"
75                 "\t-P <path>  add a search path for config change files and use as default\n"
76                 "\t-q         quiet mode (don't print error messages)\n"
77                 "\t-s         force strict mode (stop on parser errors, default)\n"
78                 "\t-S         disable strict mode\n"
79                 "\n",
80                 appname
81         );
82 }
83
84 static void cli_perror(void)
85 {
86         if (flags & CLI_FLAG_QUIET)
87                 return;
88
89         uci_perror(ctx, appname);
90 }
91
92 static void uci_show_section(struct uci_section *p)
93 {
94         struct uci_element *e;
95         const char *cname, *sname;
96
97         cname = p->package->e.name;
98         sname = p->e.name;
99         printf("%s.%s=%s\n", cname, sname, p->type);
100         uci_foreach_element(&p->options, e) {
101                 printf("%s.%s.%s=%s\n", cname, sname, e->name, uci_to_option(e)->value);
102         }
103 }
104
105 static void uci_show_package(struct uci_package *p)
106 {
107         struct uci_element *e;
108
109         uci_foreach_element( &p->sections, e) {
110                 uci_show_section(uci_to_section(e));
111         }
112 }
113
114 static void uci_show_changes(struct uci_package *p)
115 {
116         struct uci_element *e;
117
118         uci_foreach_element(&p->saved_history, e) {
119                 struct uci_history *h = uci_to_history(e);
120
121                 if (h->cmd == UCI_CMD_REMOVE)
122                         printf("-");
123                 printf("%s.%s", p->e.name, h->section);
124                 if (e->name)
125                         printf(".%s", e->name);
126                 if (h->cmd != UCI_CMD_REMOVE)
127                         printf("=%s", h->value);
128                 printf("\n");
129         }
130 }
131
132 static int package_cmd(int cmd, char *package)
133 {
134         struct uci_package *p = NULL;
135         int ret;
136
137         if (cmd == CMD_CHANGES)
138                 ctx->flags |= UCI_FLAG_SAVED_HISTORY;
139         ret = uci_load(ctx, package, &p);
140         if (cmd == CMD_CHANGES)
141                 ctx->flags &= ~UCI_FLAG_SAVED_HISTORY;
142
143         if (ret != UCI_OK) {
144                 cli_perror();
145                 return 1;
146         }
147         if (!p)
148                 return 0;
149         switch(cmd) {
150         case CMD_CHANGES:
151                 uci_show_changes(p);
152                 break;
153         case CMD_COMMIT:
154                 if (flags & CLI_FLAG_NOCOMMIT)
155                         return 0;
156                 if (uci_commit(ctx, &p, false) != UCI_OK)
157                         cli_perror();
158                 break;
159         case CMD_EXPORT:
160                 uci_export(ctx, stdout, p, true);
161                 break;
162         case CMD_SHOW:
163                 uci_show_package(p);
164                 break;
165         }
166
167         uci_unload(ctx, p);
168         return 0;
169 }
170
171 static int uci_do_import(int argc, char **argv)
172 {
173         struct uci_package *package = NULL;
174         char *name = NULL;
175         int ret = UCI_OK;
176         bool merge = false;
177
178         if (argc > 2)
179                 return 255;
180
181         if (argc == 2)
182                 name = argv[1];
183         else if (flags & CLI_FLAG_MERGE)
184                 /* need a package to merge */
185                 return 255;
186
187         if (flags & CLI_FLAG_MERGE) {
188                 if (uci_load(ctx, name, &package) != UCI_OK)
189                         package = NULL;
190                 else
191                         merge = true;
192         }
193         ret = uci_import(ctx, input, name, &package, (name != NULL));
194         if (ret == UCI_OK) {
195                 if (merge) {
196                         ret = uci_save(ctx, package);
197                 } else {
198                         struct uci_element *e;
199                         /* loop through all config sections and overwrite existing data */
200                         uci_foreach_element(&ctx->root, e) {
201                                 struct uci_package *p = uci_to_package(e);
202                                 ret = uci_commit(ctx, &p, true);
203                         }
204                 }
205         }
206
207         if (ret != UCI_OK) {
208                 cli_perror();
209                 return 1;
210         }
211
212         return 0;
213 }
214
215 static int uci_do_package_cmd(int cmd, int argc, char **argv)
216 {
217         char **configs = NULL;
218         char **p;
219
220         if (argc > 2)
221                 return 255;
222
223         if (argc == 2)
224                 return package_cmd(cmd, argv[1]);
225
226         if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
227                 cli_perror();
228                 return 1;
229         }
230
231         for (p = configs; *p; p++) {
232                 package_cmd(cmd, *p);
233         }
234
235         return 0;
236 }
237
238 static int uci_do_add(int argc, char **argv)
239 {
240         struct uci_package *p = NULL;
241         struct uci_section *s = NULL;
242         int ret;
243
244         if (argc != 3)
245                 return 255;
246
247         ret = uci_load(ctx, argv[1], &p);
248         if (ret != UCI_OK)
249                 goto done;
250
251         ret = uci_add_section(ctx, p, argv[2], &s);
252         if (ret != UCI_OK)
253                 goto done;
254
255         ret = uci_save(ctx, p);
256
257 done:
258         if (ret != UCI_OK)
259                 cli_perror();
260         else if (s)
261                 fprintf(stdout, "%s\n", s->e.name);
262
263         return ret;
264 }
265
266 static int uci_do_section_cmd(int cmd, int argc, char **argv)
267 {
268         struct uci_package *p = NULL;
269         struct uci_element *e = NULL;
270         char *package = NULL;
271         char *section = NULL;
272         char *option = NULL;
273         char *value = NULL;
274         char **ptr = NULL;
275         int ret = UCI_OK;
276
277         if (argc != 2)
278                 return 255;
279
280         switch(cmd) {
281         case CMD_SET:
282         case CMD_RENAME:
283                 ptr = &value;
284                 break;
285         default:
286                 break;
287         }
288         if (uci_parse_tuple(ctx, argv[1], &package, &section, &option, ptr) != UCI_OK)
289                 return 1;
290         if (section && !section[0])
291                 return 1;
292
293         if (uci_load(ctx, package, &p) != UCI_OK) {
294                 cli_perror();
295                 return 1;
296         }
297         if (!p)
298                 return 0;
299
300         switch(cmd) {
301         case CMD_GET:
302                 if (uci_lookup(ctx, &e, p, section, option) != UCI_OK)
303                         return 1;
304
305                 switch(e->type) {
306                 case UCI_TYPE_SECTION:
307                         value = uci_to_section(e)->type;
308                         break;
309                 case UCI_TYPE_OPTION:
310                         value = uci_to_option(e)->value;
311                         break;
312                 default:
313                         /* should not happen */
314                         return 1;
315                 }
316                 /* throw the value to stdout */
317                 printf("%s\n", value);
318                 break;
319         case CMD_RENAME:
320                 ret = uci_rename(ctx, p, section, option, value);
321                 break;
322         case CMD_REVERT:
323                 ret = uci_revert(ctx, &p, section, option);
324                 break;
325         case CMD_SET:
326                 ret = uci_set(ctx, p, section, option, value, NULL);
327                 break;
328         case CMD_DEL:
329                 ret = uci_delete(ctx, p, section, option);
330                 break;
331         }
332
333         /* no save necessary for get */
334         if ((cmd == CMD_GET) || (cmd == CMD_REVERT))
335                 return 0;
336
337         /* save changes, but don't commit them yet */
338         if (ret == UCI_OK)
339                 ret = uci_save(ctx, p);
340
341         if (ret != UCI_OK) {
342                 cli_perror();
343                 return 1;
344         }
345
346         return 0;
347 }
348
349 static int uci_batch_cmd(void)
350 {
351         char *argv[MAX_ARGS];
352         char *str = NULL;
353         int ret = 0;
354         int i, j;
355
356         for(i = 0; i <= MAX_ARGS; i++) {
357                 if (i == MAX_ARGS) {
358                         fprintf(stderr, "Too many arguments\n");
359                         return 1;
360                 }
361                 argv[i] = NULL;
362                 if ((ret = uci_parse_argument(ctx, input, &str, &argv[i])) != UCI_OK) {
363                         cli_perror();
364                         i = 0;
365                         break;
366                 }
367                 if (!argv[i][0])
368                         break;
369                 argv[i] = strdup(argv[i]);
370                 if (!argv[i]) {
371                         perror("uci");
372                         return 1;
373                 }
374         }
375         argv[i] = NULL;
376
377         if (i > 0) {
378                 if (!strcasecmp(argv[0], "exit"))
379                         return 254;
380                 ret = uci_cmd(i, argv);
381         } else
382                 return 0;
383
384         for (j = 0; j < i; j++) {
385                 if (argv[j])
386                         free(argv[j]);
387         }
388
389         return ret;
390 }
391
392 static int uci_batch(void)
393 {
394         int ret = 0;
395
396         while (!feof(input)) {
397                 struct uci_element *e, *tmp;
398
399                 ret = uci_batch_cmd();
400                 if (ret == 254)
401                         return 0;
402                 else if (ret == 255)
403                         fprintf(stderr, "Unknown command\n");
404
405                 /* clean up */
406                 uci_foreach_element_safe(&ctx->root, tmp, e) {
407                         uci_unload(ctx, uci_to_package(e));
408                 }
409         }
410         return 0;
411 }
412
413 static int uci_cmd(int argc, char **argv)
414 {
415         int cmd = 0;
416
417         if (!strcasecmp(argv[0], "batch") && !(flags & CLI_FLAG_BATCH))
418                 return uci_batch();
419         else if (!strcasecmp(argv[0], "show"))
420                 cmd = CMD_SHOW;
421         else if (!strcasecmp(argv[0], "changes"))
422                 cmd = CMD_CHANGES;
423         else if (!strcasecmp(argv[0], "export"))
424                 cmd = CMD_EXPORT;
425         else if (!strcasecmp(argv[0], "commit"))
426                 cmd = CMD_COMMIT;
427         else if (!strcasecmp(argv[0], "get"))
428                 cmd = CMD_GET;
429         else if (!strcasecmp(argv[0], "set"))
430                 cmd = CMD_SET;
431         else if (!strcasecmp(argv[0], "ren") ||
432                  !strcasecmp(argv[0], "rename"))
433                 cmd = CMD_RENAME;
434         else if (!strcasecmp(argv[0], "revert"))
435                 cmd = CMD_REVERT;
436         else if (!strcasecmp(argv[0], "del"))
437                 cmd = CMD_DEL;
438         else if (!strcasecmp(argv[0], "import"))
439                 cmd = CMD_IMPORT;
440         else if (!strcasecmp(argv[0], "help"))
441                 cmd = CMD_HELP;
442         else if (!strcasecmp(argv[0], "add"))
443                 cmd = CMD_ADD;
444         else
445                 cmd = -1;
446
447         switch(cmd) {
448                 case CMD_GET:
449                 case CMD_SET:
450                 case CMD_DEL:
451                 case CMD_RENAME:
452                 case CMD_REVERT:
453                         return uci_do_section_cmd(cmd, argc, argv);
454                 case CMD_SHOW:
455                 case CMD_EXPORT:
456                 case CMD_COMMIT:
457                 case CMD_CHANGES:
458                         return uci_do_package_cmd(cmd, argc, argv);
459                 case CMD_IMPORT:
460                         return uci_do_import(argc, argv);
461                 case CMD_ADD:
462                         return uci_do_add(argc, argv);
463                 case CMD_HELP:
464                         uci_usage();
465                         return 0;
466                 default:
467                         return 255;
468         }
469 }
470
471 int main(int argc, char **argv)
472 {
473         int ret;
474         int c;
475
476         appname = argv[0];
477         input = stdin;
478         ctx = uci_alloc_context();
479         if (!ctx) {
480                 fprintf(stderr, "Out of memory\n");
481                 return 1;
482         }
483
484         while((c = getopt(argc, argv, "f:mnNp:P:sSq")) != -1) {
485                 switch(c) {
486                         case 'f':
487                                 input = fopen(optarg, "r");
488                                 if (!input) {
489                                         perror("uci");
490                                         return 1;
491                                 }
492                                 break;
493                         case 'm':
494                                 flags |= CLI_FLAG_MERGE;
495                                 break;
496                         case 's':
497                                 ctx->flags |= UCI_FLAG_STRICT;
498                                 break;
499                         case 'S':
500                                 ctx->flags &= ~UCI_FLAG_STRICT;
501                                 ctx->flags |= UCI_FLAG_PERROR;
502                                 break;
503                         case 'n':
504                                 ctx->flags |= UCI_FLAG_EXPORT_NAME;
505                                 break;
506                         case 'N':
507                                 ctx->flags &= ~UCI_FLAG_EXPORT_NAME;
508                                 break;
509                         case 'p':
510                                 uci_add_history_path(ctx, optarg);
511                                 break;
512                         case 'P':
513                                 uci_add_history_path(ctx, ctx->savedir);
514                                 uci_set_savedir(ctx, optarg);
515                                 flags |= CLI_FLAG_NOCOMMIT;
516                                 break;
517                         case 'q':
518                                 flags |= CLI_FLAG_QUIET;
519                                 break;
520                         default:
521                                 uci_usage();
522                                 return 0;
523                 }
524         }
525         if (optind > 1)
526                 argv[optind - 1] = argv[0];
527         argv += optind - 1;
528         argc -= optind - 1;
529
530         if (argc < 2) {
531                 uci_usage();
532                 return 0;
533         }
534         ret = uci_cmd(argc - 1, argv + 1);
535         if (input != stdin)
536                 fclose(input);
537         if (ret == 255) {
538                 uci_usage();
539                 return 0;
540         }
541
542         uci_free_context(ctx);
543
544         return ret;
545 }