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