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