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