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