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