optimization
[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) != 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         return 0;
112 }
113
114 static int uci_do_package_cmd(int cmd, int argc, char **argv)
115 {
116         char **configs = NULL;
117         char **p;
118
119         if (argc > 2)
120                 return 255;
121
122         if (argc == 2)
123                 return package_cmd(cmd, argv[1]);
124
125         if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
126                 uci_perror(ctx, appname);
127                 return 1;
128         }
129
130         for (p = configs; *p; p++) {
131                 package_cmd(cmd, *p);
132         }
133
134         return 0;
135 }
136
137
138 static int uci_do_section_cmd(int cmd, int argc, char **argv)
139 {
140         struct uci_package *p = NULL;
141         struct uci_element *e = NULL;
142         char *package = NULL;
143         char *section = NULL;
144         char *option = NULL;
145         char *value = NULL;
146         char **ptr = NULL;
147         int ret = UCI_OK;
148
149         if (argc != 2)
150                 return 255;
151
152         switch(cmd) {
153         case CMD_SET:
154         case CMD_RENAME:
155                 ptr = &value;
156                 break;
157         default:
158                 break;
159         }
160         if (uci_parse_tuple(ctx, argv[1], &package, &section, &option, ptr) != UCI_OK)
161                 return 1;
162
163         if (uci_load(ctx, package, &p) != UCI_OK) {
164                 uci_perror(ctx, appname);
165                 return 1;
166         }
167
168         switch(cmd) {
169         case CMD_GET:
170                 if (uci_lookup(ctx, &e, p, section, option) != UCI_OK)
171                         return 1;
172
173                 switch(e->type) {
174                 case UCI_TYPE_SECTION:
175                         value = uci_to_section(e)->type;
176                         break;
177                 case UCI_TYPE_OPTION:
178                         value = uci_to_option(e)->value;
179                         break;
180                 default:
181                         /* should not happen */
182                         return 1;
183                 }
184                 /* throw the value to stdout */
185                 printf("%s\n", value);
186                 break;
187         case CMD_RENAME:
188                 ret = uci_rename(ctx, p, section, option, value);
189                 break;
190         case CMD_SET:
191                 ret = uci_set(ctx, p, section, option, value);
192                 break;
193         case CMD_DEL:
194                 ret = uci_delete(ctx, p, section, option);
195                 break;
196         }
197
198         /* no save necessary for get */
199         if (cmd == CMD_GET)
200                 return 0;
201
202         /* save changes, but don't commit them yet */
203         if (ret == UCI_OK)
204                 ret = uci_save(ctx, p);
205
206         if (ret != UCI_OK) {
207                 uci_perror(ctx, appname);
208                 return 1;
209         }
210
211         return 0;
212 }
213
214 static int uci_cmd(int argc, char **argv)
215 {
216         int cmd = 0;
217
218         if (!strcasecmp(argv[0], "show"))
219                 cmd = CMD_SHOW;
220         else if (!strcasecmp(argv[0], "export"))
221                 cmd = CMD_EXPORT;
222         else if (!strcasecmp(argv[0], "commit"))
223                 cmd = CMD_COMMIT;
224         else if (!strcasecmp(argv[0], "get"))
225                 cmd = CMD_GET;
226         else if (!strcasecmp(argv[0], "set"))
227                 cmd = CMD_SET;
228         else if (!strcasecmp(argv[0], "ren") ||
229                  !strcasecmp(argv[0], "rename"))
230                 cmd = CMD_RENAME;
231         else if (!strcasecmp(argv[0], "del"))
232                 cmd = CMD_DEL;
233         else if (!strcasecmp(argv[0], "import"))
234                 cmd = CMD_IMPORT;
235         else
236                 cmd = -1;
237
238         switch(cmd) {
239                 case CMD_GET:
240                 case CMD_SET:
241                 case CMD_DEL:
242                 case CMD_RENAME:
243                         return uci_do_section_cmd(cmd, argc, argv);
244                 case CMD_SHOW:
245                 case CMD_EXPORT:
246                 case CMD_COMMIT:
247                         return uci_do_package_cmd(cmd, argc, argv);
248                 case CMD_IMPORT:
249                         return uci_do_import(argc, argv);
250                 default:
251                         return 255;
252         }
253 }
254
255 int main(int argc, char **argv)
256 {
257         int ret;
258         int c;
259
260         ctx = uci_alloc_context();
261         if (!ctx) {
262                 fprintf(stderr, "Out of memory\n");
263                 return 1;
264         }
265
266         while((c = getopt(argc, argv, "sS")) != -1) {
267                 switch(c) {
268                         case 'f':
269                                 input = fopen(optarg, "r");
270                                 if (!input) {
271                                         perror("uci");
272                                         return 1;
273                                 }
274                                 break;
275                         case 'm':
276                                 flags |= CLI_FLAG_MERGE;
277                                 break;
278                         case 's':
279                                 ctx->flags |= UCI_FLAG_STRICT;
280                                 break;
281                         case 'S':
282                                 ctx->flags &= ~UCI_FLAG_STRICT;
283                                 ctx->flags |= UCI_FLAG_PERROR;
284                                 break;
285                         default:
286                                 uci_usage(argc, argv);
287                                 break;
288                 }
289         }
290         if (optind > 1)
291                 argv[optind - 1] = argv[0];
292         argv += optind - 1;
293         argc -= optind - 1;
294
295         if (argc < 2)
296                 uci_usage(argc, argv);
297         ret = uci_cmd(argc - 1, argv + 1);
298         if (input != stdin)
299                 fclose(input);
300         if (ret == 255)
301                 uci_usage(argc, argv);
302
303         uci_free_context(ctx);
304
305         return ret;
306 }