add quiet mode flag for cli
[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         switch(cmd) {
105         case CMD_COMMIT:
106                 if (uci_commit(ctx, &p, false) != UCI_OK)
107                         cli_perror();
108                 break;
109         case CMD_EXPORT:
110                 uci_export(ctx, stdout, p, true);
111                 break;
112         case CMD_SHOW:
113                 uci_show_package(p);
114                 break;
115         }
116
117         uci_unload(ctx, p);
118         return 0;
119 }
120
121 static int uci_do_import(int argc, char **argv)
122 {
123         struct uci_package *package = NULL;
124         char *name = NULL;
125         int ret = UCI_OK;
126
127         if (argc > 2)
128                 return 255;
129
130         if (argc == 2)
131                 name = argv[1];
132         else if (flags & CLI_FLAG_MERGE)
133                 /* need a package to merge */
134                 return 255;
135
136         if (flags & CLI_FLAG_MERGE) {
137                 if (uci_load(ctx, name, &package) != UCI_OK)
138                         package = NULL;
139         }
140         ret = uci_import(ctx, input, name, &package, (name != NULL));
141         if (ret == UCI_OK) {
142                 if (flags & CLI_FLAG_MERGE) {
143                         ret = uci_save(ctx, package);
144                 } else {
145                         struct uci_element *e;
146                         /* loop through all config sections and overwrite existing data */
147                         uci_foreach_element(&ctx->root, e) {
148                                 struct uci_package *p = uci_to_package(e);
149                                 ret = uci_commit(ctx, &p, true);
150                         }
151                 }
152         }
153
154         if (ret != UCI_OK) {
155                 cli_perror();
156                 return 1;
157         }
158
159         return 0;
160 }
161
162 static int uci_do_package_cmd(int cmd, int argc, char **argv)
163 {
164         char **configs = NULL;
165         char **p;
166
167         if (argc > 2)
168                 return 255;
169
170         if (argc == 2)
171                 return package_cmd(cmd, argv[1]);
172
173         if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
174                 cli_perror();
175                 return 1;
176         }
177
178         for (p = configs; *p; p++) {
179                 package_cmd(cmd, *p);
180         }
181
182         return 0;
183 }
184
185
186 static int uci_do_section_cmd(int cmd, int argc, char **argv)
187 {
188         struct uci_package *p = NULL;
189         struct uci_element *e = NULL;
190         char *package = NULL;
191         char *section = NULL;
192         char *option = NULL;
193         char *value = NULL;
194         char **ptr = NULL;
195         int ret = UCI_OK;
196
197         if (argc != 2)
198                 return 255;
199
200         switch(cmd) {
201         case CMD_SET:
202         case CMD_RENAME:
203                 ptr = &value;
204                 break;
205         default:
206                 break;
207         }
208         if (uci_parse_tuple(ctx, argv[1], &package, &section, &option, ptr) != UCI_OK)
209                 return 1;
210
211         if (uci_load(ctx, package, &p) != UCI_OK) {
212                 cli_perror();
213                 return 1;
214         }
215
216         switch(cmd) {
217         case CMD_GET:
218                 if (uci_lookup(ctx, &e, p, section, option) != UCI_OK)
219                         return 1;
220
221                 switch(e->type) {
222                 case UCI_TYPE_SECTION:
223                         value = uci_to_section(e)->type;
224                         break;
225                 case UCI_TYPE_OPTION:
226                         value = uci_to_option(e)->value;
227                         break;
228                 default:
229                         /* should not happen */
230                         return 1;
231                 }
232                 /* throw the value to stdout */
233                 printf("%s\n", value);
234                 break;
235         case CMD_RENAME:
236                 ret = uci_rename(ctx, p, section, option, value);
237                 break;
238         case CMD_SET:
239                 ret = uci_set(ctx, p, section, option, value);
240                 break;
241         case CMD_DEL:
242                 ret = uci_delete(ctx, p, section, option);
243                 break;
244         }
245
246         /* no save necessary for get */
247         if (cmd == CMD_GET)
248                 return 0;
249
250         /* save changes, but don't commit them yet */
251         if (ret == UCI_OK)
252                 ret = uci_save(ctx, p);
253
254         if (ret != UCI_OK) {
255                 cli_perror();
256                 return 1;
257         }
258
259         return 0;
260 }
261
262 static int uci_cmd(int argc, char **argv)
263 {
264         int cmd = 0;
265
266         if (!strcasecmp(argv[0], "show"))
267                 cmd = CMD_SHOW;
268         else if (!strcasecmp(argv[0], "export"))
269                 cmd = CMD_EXPORT;
270         else if (!strcasecmp(argv[0], "commit"))
271                 cmd = CMD_COMMIT;
272         else if (!strcasecmp(argv[0], "get"))
273                 cmd = CMD_GET;
274         else if (!strcasecmp(argv[0], "set"))
275                 cmd = CMD_SET;
276         else if (!strcasecmp(argv[0], "ren") ||
277                  !strcasecmp(argv[0], "rename"))
278                 cmd = CMD_RENAME;
279         else if (!strcasecmp(argv[0], "del"))
280                 cmd = CMD_DEL;
281         else if (!strcasecmp(argv[0], "import"))
282                 cmd = CMD_IMPORT;
283         else
284                 cmd = -1;
285
286         switch(cmd) {
287                 case CMD_GET:
288                 case CMD_SET:
289                 case CMD_DEL:
290                 case CMD_RENAME:
291                         return uci_do_section_cmd(cmd, argc, argv);
292                 case CMD_SHOW:
293                 case CMD_EXPORT:
294                 case CMD_COMMIT:
295                         return uci_do_package_cmd(cmd, argc, argv);
296                 case CMD_IMPORT:
297                         return uci_do_import(argc, argv);
298                 default:
299                         return 255;
300         }
301 }
302
303 int main(int argc, char **argv)
304 {
305         int ret;
306         int c;
307
308         input = stdin;
309         ctx = uci_alloc_context();
310         if (!ctx) {
311                 fprintf(stderr, "Out of memory\n");
312                 return 1;
313         }
314
315         while((c = getopt(argc, argv, "mf:sSnNq")) != -1) {
316                 switch(c) {
317                         case 'f':
318                                 input = fopen(optarg, "r");
319                                 if (!input) {
320                                         perror("uci");
321                                         return 1;
322                                 }
323                                 break;
324                         case 'm':
325                                 flags |= CLI_FLAG_MERGE;
326                                 break;
327                         case 's':
328                                 ctx->flags |= UCI_FLAG_STRICT;
329                                 break;
330                         case 'S':
331                                 ctx->flags &= ~UCI_FLAG_STRICT;
332                                 ctx->flags |= UCI_FLAG_PERROR;
333                                 break;
334                         case 'n':
335                                 ctx->flags |= UCI_FLAG_EXPORT_NAME;
336                                 break;
337                         case 'N':
338                                 ctx->flags &= ~UCI_FLAG_EXPORT_NAME;
339                                 break;
340                         case 'q':
341                                 flags |= CLI_FLAG_QUIET;
342                                 break;
343                         default:
344                                 uci_usage(argc, argv);
345                                 break;
346                 }
347         }
348         if (optind > 1)
349                 argv[optind - 1] = argv[0];
350         argv += optind - 1;
351         argc -= optind - 1;
352
353         if (argc < 2)
354                 uci_usage(argc, argv);
355         ret = uci_cmd(argc - 1, argv + 1);
356         if (input != stdin)
357                 fclose(input);
358         if (ret == 255)
359                 uci_usage(argc, argv);
360
361         uci_free_context(ctx);
362
363         return ret;
364 }