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