some extra null pointer checks
[project/uci.git] / cli.c
1 /*
2  * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2
6  * as published by the Free Software Foundation
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13 #include <strings.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include "uci.h"
17
18 static const char *appname = "uci";
19 static enum {
20         CLI_FLAG_MERGE = (1 << 0),
21         CLI_FLAG_QUIET = (1 << 1)
22 } flags;
23 static FILE *input;
24
25 static struct uci_context *ctx;
26 enum {
27         /* section cmds */
28         CMD_GET,
29         CMD_SET,
30         CMD_DEL,
31         CMD_RENAME,
32         /* package cmds */
33         CMD_SHOW,
34         CMD_IMPORT,
35         CMD_EXPORT,
36         CMD_COMMIT,
37 };
38
39 static void uci_usage(int argc, char **argv)
40 {
41         fprintf(stderr,
42                 "Usage: %s [<options>] <command> [<arguments>]\n\n"
43                 "Commands:\n"
44                 "\texport     [<config>]\n"
45                 "\timport     [<config>]\n"
46                 "\tshow       [<config>[.<section>[.<option>]]]\n"
47                 "\tget        <config>.<section>[.<option>]\n"
48                 "\tset        <config>.<section>[.<option>]=<value>\n"
49                 "\trename     <config>.<section>[.<option>]=<name>\n"
50                 "\n"
51                 "Options:\n"
52                 "\t-f <file>  use <file> as input instead of stdin\n"
53                 "\t-m         when importing, merge data into an existing package\n"
54                 "\t-n         name unnamed sections on export (default)\n"
55                 "\t-N         don't name unnamed sections\n"
56                 "\t-q         quiet mode (don't print error messages)\n"
57                 "\t-s         force strict mode (stop on parser errors, default)\n"
58                 "\t-S         disable strict mode\n"
59                 "\n",
60                 argv[0]
61         );
62         exit(255);
63 }
64
65 static void cli_perror(void)
66 {
67         if (flags & CLI_FLAG_QUIET)
68                 return;
69
70         uci_perror(ctx, appname);
71 }
72
73 static void uci_show_section(struct uci_section *p)
74 {
75         struct uci_element *e;
76         const char *cname, *sname;
77
78         cname = p->package->e.name;
79         sname = p->e.name;
80         printf("%s.%s=%s\n", cname, sname, p->type);
81         uci_foreach_element(&p->options, e) {
82                 printf("%s.%s.%s=%s\n", cname, sname, e->name, uci_to_option(e)->value);
83         }
84 }
85
86 static void uci_show_package(struct uci_package *p)
87 {
88         struct uci_element *e;
89
90         uci_foreach_element( &p->sections, e) {
91                 uci_show_section(uci_to_section(e));
92         }
93 }
94
95
96 static int package_cmd(int cmd, char *package)
97 {
98         struct uci_package *p = NULL;
99
100         if (uci_load(ctx, package, &p) != UCI_OK) {
101                 cli_perror();
102                 return 1;
103         }
104         if (!p)
105                 return 0;
106         switch(cmd) {
107         case CMD_COMMIT:
108                 if (uci_commit(ctx, &p, false) != UCI_OK)
109                         cli_perror();
110                 break;
111         case CMD_EXPORT:
112                 uci_export(ctx, stdout, p, true);
113                 break;
114         case CMD_SHOW:
115                 uci_show_package(p);
116                 break;
117         }
118
119         uci_unload(ctx, p);
120         return 0;
121 }
122
123 static int uci_do_import(int argc, char **argv)
124 {
125         struct uci_package *package = NULL;
126         char *name = NULL;
127         int ret = UCI_OK;
128
129         if (argc > 2)
130                 return 255;
131
132         if (argc == 2)
133                 name = argv[1];
134         else if (flags & CLI_FLAG_MERGE)
135                 /* need a package to merge */
136                 return 255;
137
138         if (flags & CLI_FLAG_MERGE) {
139                 if (uci_load(ctx, name, &package) != UCI_OK)
140                         package = NULL;
141         }
142         ret = uci_import(ctx, input, name, &package, (name != NULL));
143         if (ret == UCI_OK) {
144                 if (flags & CLI_FLAG_MERGE) {
145                         ret = uci_save(ctx, package);
146                 } else {
147                         struct uci_element *e;
148                         /* loop through all config sections and overwrite existing data */
149                         uci_foreach_element(&ctx->root, e) {
150                                 struct uci_package *p = uci_to_package(e);
151                                 ret = uci_commit(ctx, &p, true);
152                         }
153                 }
154         }
155
156         if (ret != UCI_OK) {
157                 cli_perror();
158                 return 1;
159         }
160
161         return 0;
162 }
163
164 static int uci_do_package_cmd(int cmd, int argc, char **argv)
165 {
166         char **configs = NULL;
167         char **p;
168
169         if (argc > 2)
170                 return 255;
171
172         if (argc == 2)
173                 return package_cmd(cmd, argv[1]);
174
175         if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
176                 cli_perror();
177                 return 1;
178         }
179
180         for (p = configs; *p; p++) {
181                 package_cmd(cmd, *p);
182         }
183
184         return 0;
185 }
186
187
188 static int uci_do_section_cmd(int cmd, int argc, char **argv)
189 {
190         struct uci_package *p = NULL;
191         struct uci_element *e = NULL;
192         char *package = NULL;
193         char *section = NULL;
194         char *option = NULL;
195         char *value = NULL;
196         char **ptr = NULL;
197         int ret = UCI_OK;
198
199         if (argc != 2)
200                 return 255;
201
202         switch(cmd) {
203         case CMD_SET:
204         case CMD_RENAME:
205                 ptr = &value;
206                 break;
207         default:
208                 break;
209         }
210         if (uci_parse_tuple(ctx, argv[1], &package, &section, &option, ptr) != UCI_OK)
211                 return 1;
212
213         if (uci_load(ctx, package, &p) != UCI_OK) {
214                 cli_perror();
215                 return 1;
216         }
217         if (!p)
218                 return 0;
219
220         switch(cmd) {
221         case CMD_GET:
222                 if (uci_lookup(ctx, &e, p, section, option) != UCI_OK)
223                         return 1;
224
225                 switch(e->type) {
226                 case UCI_TYPE_SECTION:
227                         value = uci_to_section(e)->type;
228                         break;
229                 case UCI_TYPE_OPTION:
230                         value = uci_to_option(e)->value;
231                         break;
232                 default:
233                         /* should not happen */
234                         return 1;
235                 }
236                 /* throw the value to stdout */
237                 printf("%s\n", value);
238                 break;
239         case CMD_RENAME:
240                 ret = uci_rename(ctx, p, section, option, value);
241                 break;
242         case CMD_SET:
243                 ret = uci_set(ctx, p, section, option, value);
244                 break;
245         case CMD_DEL:
246                 ret = uci_delete(ctx, p, section, option);
247                 break;
248         }
249
250         /* no save necessary for get */
251         if (cmd == CMD_GET)
252                 return 0;
253
254         /* save changes, but don't commit them yet */
255         if (ret == UCI_OK)
256                 ret = uci_save(ctx, p);
257
258         if (ret != UCI_OK) {
259                 cli_perror();
260                 return 1;
261         }
262
263         return 0;
264 }
265
266 static int uci_cmd(int argc, char **argv)
267 {
268         int cmd = 0;
269
270         if (!strcasecmp(argv[0], "show"))
271                 cmd = CMD_SHOW;
272         else if (!strcasecmp(argv[0], "export"))
273                 cmd = CMD_EXPORT;
274         else if (!strcasecmp(argv[0], "commit"))
275                 cmd = CMD_COMMIT;
276         else if (!strcasecmp(argv[0], "get"))
277                 cmd = CMD_GET;
278         else if (!strcasecmp(argv[0], "set"))
279                 cmd = CMD_SET;
280         else if (!strcasecmp(argv[0], "ren") ||
281                  !strcasecmp(argv[0], "rename"))
282                 cmd = CMD_RENAME;
283         else if (!strcasecmp(argv[0], "del"))
284                 cmd = CMD_DEL;
285         else if (!strcasecmp(argv[0], "import"))
286                 cmd = CMD_IMPORT;
287         else
288                 cmd = -1;
289
290         switch(cmd) {
291                 case CMD_GET:
292                 case CMD_SET:
293                 case CMD_DEL:
294                 case CMD_RENAME:
295                         return uci_do_section_cmd(cmd, argc, argv);
296                 case CMD_SHOW:
297                 case CMD_EXPORT:
298                 case CMD_COMMIT:
299                         return uci_do_package_cmd(cmd, argc, argv);
300                 case CMD_IMPORT:
301                         return uci_do_import(argc, argv);
302                 default:
303                         return 255;
304         }
305 }
306
307 int main(int argc, char **argv)
308 {
309         int ret;
310         int c;
311
312         input = stdin;
313         ctx = uci_alloc_context();
314         if (!ctx) {
315                 fprintf(stderr, "Out of memory\n");
316                 return 1;
317         }
318
319         while((c = getopt(argc, argv, "mf:sSnNq")) != -1) {
320                 switch(c) {
321                         case 'f':
322                                 input = fopen(optarg, "r");
323                                 if (!input) {
324                                         perror("uci");
325                                         return 1;
326                                 }
327                                 break;
328                         case 'm':
329                                 flags |= CLI_FLAG_MERGE;
330                                 break;
331                         case 's':
332                                 ctx->flags |= UCI_FLAG_STRICT;
333                                 break;
334                         case 'S':
335                                 ctx->flags &= ~UCI_FLAG_STRICT;
336                                 ctx->flags |= UCI_FLAG_PERROR;
337                                 break;
338                         case 'n':
339                                 ctx->flags |= UCI_FLAG_EXPORT_NAME;
340                                 break;
341                         case 'N':
342                                 ctx->flags &= ~UCI_FLAG_EXPORT_NAME;
343                                 break;
344                         case 'q':
345                                 flags |= CLI_FLAG_QUIET;
346                                 break;
347                         default:
348                                 uci_usage(argc, argv);
349                                 break;
350                 }
351         }
352         if (optind > 1)
353                 argv[optind - 1] = argv[0];
354         argv += optind - 1;
355         argc -= optind - 1;
356
357         if (argc < 2)
358                 uci_usage(argc, argv);
359         ret = uci_cmd(argc - 1, argv + 1);
360         if (input != stdin)
361                 fclose(input);
362         if (ret == 255)
363                 uci_usage(argc, argv);
364
365         uci_free_context(ctx);
366
367         return ret;
368 }