1c31083871a6a1009c129079be7abd552cc28ddf
[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
20 static struct uci_context *ctx;
21 enum {
22         CMD_GET,
23         CMD_SET,
24         CMD_DEL
25 };
26
27 static void uci_usage(int argc, char **argv)
28 {
29         fprintf(stderr,
30                 "Usage: %s [<options>] <command> [<arguments>]\n\n"
31                 "Commands:\n"
32                 "\texport   [<config>]\n"
33                 "\tshow     [<config>[.<section>[.<option>]]]\n"
34                 "\tget      <config>.<section>[.<option>]\n"
35                 "\tset      <config>.<section>[.<option>]=<value>\n"
36                 "\n"
37                 "Options:\n"
38                 "\t-s       force strict mode (stop on parser errors)\n"
39                 "\t-S       disable strict mode\n"
40                 "\n",
41                 argv[0]
42         );
43         exit(255);
44 }
45
46 static void uci_show_section(struct uci_section *p)
47 {
48         struct uci_element *e;
49         const char *cname, *sname;
50
51         cname = p->package->e.name;
52         sname = p->e.name;
53         printf("%s.%s=%s\n", cname, sname, p->type);
54         uci_foreach_element(&p->options, e) {
55                 printf("%s.%s.%s=%s\n", cname, sname, e->name, uci_to_option(e)->value);
56         }
57 }
58
59 static void uci_show_package(struct uci_package *p, char *section)
60 {
61         struct uci_element *e;
62
63         uci_foreach_element( &p->sections, e) {
64                 if (!section || !strcmp(e->name, section))
65                         uci_show_section(uci_to_section(e));
66         }
67 }
68
69 static int uci_show(int argc, char **argv)
70 {
71         char *section = (argc > 2 ? argv[2] : NULL);
72         struct uci_package *package;
73         char **configs = NULL;
74         char **p;
75
76         if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
77                 uci_perror(ctx, appname);
78                 return 1;
79         }
80
81         if (argc >= 2) {
82                 if (uci_load(ctx, argv[1], &package) != UCI_OK) {
83                         uci_perror(ctx, appname);
84                         return 1;
85                 }
86                 uci_show_package(package, section);
87                 uci_unload(ctx, package);
88                 return 0;
89         }
90
91         for (p = configs; *p; p++) {
92                 if ((argc < 2) || !strcmp(argv[1], *p)) {
93                         if (uci_load(ctx, *p, &package) != UCI_OK) {
94                                 uci_perror(ctx, appname);
95                                 return 1;
96                         }
97                         uci_show_package(package, section);
98                         uci_unload(ctx, package);
99                 }
100         }
101
102         return 0;
103 }
104
105 static int uci_do_export(int argc, char **argv)
106 {
107         char **configs = NULL;
108         char **p;
109
110         if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
111                 uci_perror(ctx, appname);
112                 return 1;
113         }
114
115         for (p = configs; *p; p++) {
116                 if ((argc < 2) || !strcmp(argv[1], *p)) {
117                         struct uci_package *package = NULL;
118                         int ret;
119
120                         ret = uci_load(ctx, *p, &package);
121                         if (ret)
122                                 continue;
123                         uci_export(ctx, stdout, package, true);
124                         uci_unload(ctx, package);
125                 }
126         }
127         return 0;
128 }
129
130 static int uci_do_commit(int argc, char **argv)
131 {
132         char **configs = NULL;
133         char **p;
134
135         if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
136                 uci_perror(ctx, appname);
137                 return 1;
138         }
139
140         for (p = configs; *p; p++) {
141                 if ((argc < 2) || !strcmp(argv[1], *p)) {
142                         struct uci_package *package = NULL;
143                         int ret;
144
145                         if (uci_load(ctx, *p, &package) != UCI_OK) {
146                                 uci_perror(ctx, appname);
147                                 continue;
148                         }
149                         if (uci_commit(ctx, &package) != UCI_OK)
150                                 uci_perror(ctx, appname);
151
152                         uci_unload(ctx, package);
153                 }
154         }
155         return 0;
156 }
157
158
159 static int uci_do_cmd(int cmd, int argc, char **argv)
160 {
161         char *package = NULL;
162         char *section = NULL;
163         char *option = NULL;
164         char *value = NULL;
165         struct uci_package *p = NULL;
166         struct uci_element *e = NULL;
167
168         if (argc != 2)
169                 return 255;
170
171         if (uci_parse_tuple(ctx, argv[1], &package, &section, &option, (cmd == CMD_SET ? &value : NULL)) != UCI_OK)
172                 return 1;
173
174         if (uci_load(ctx, package, &p) != UCI_OK) {
175                 uci_perror(ctx, appname);
176                 return 1;
177         }
178
179         switch(cmd) {
180         case CMD_GET:
181                 if (uci_lookup(ctx, &e, p, section, option) != UCI_OK)
182                         return 1;
183
184                 switch(e->type) {
185                 case UCI_TYPE_SECTION:
186                         value = uci_to_section(e)->type;
187                         break;
188                 case UCI_TYPE_OPTION:
189                         value = uci_to_option(e)->value;
190                         break;
191                 default:
192                         /* should not happen */
193                         return 1;
194                 }
195                 /* throw the value to stdout */
196                 printf("%s\n", value);
197                 break;
198         case CMD_SET:
199                 if (uci_set(ctx, p, section, option, value) != UCI_OK) {
200                         uci_perror(ctx, appname);
201                         return 1;
202                 }
203                 break;
204         case CMD_DEL:
205                 if (uci_del(ctx, p, section, option) != UCI_OK) {
206                         uci_perror(ctx, appname);
207                         return 1;
208                 }
209                 break;
210         }
211
212         /* no save necessary for get */
213         if (cmd == CMD_GET)
214                 return 0;
215
216         /* save changes, but don't commit them yet */
217         if (uci_save(ctx, p) != UCI_OK) {
218                 uci_perror(ctx, appname);
219                 return 1;
220         }
221
222         return 0;
223 }
224
225 static int uci_cmd(int argc, char **argv)
226 {
227         int cmd;
228
229         if (!strcasecmp(argv[0], "show"))
230                 return uci_show(argc, argv);
231         if (!strcasecmp(argv[0], "export"))
232                 return uci_do_export(argc, argv);
233         if (!strcasecmp(argv[0], "commit"))
234                 return uci_do_commit(argc, argv);
235
236         if (!strcasecmp(argv[0], "get"))
237                 cmd = CMD_GET;
238         else if (!strcasecmp(argv[0], "set"))
239                 cmd = CMD_SET;
240         else if (!strcasecmp(argv[0], "del"))
241                 cmd = CMD_DEL;
242         else
243                 return 255;
244         return uci_do_cmd(cmd, argc, argv);
245 }
246
247 int main(int argc, char **argv)
248 {
249         int ret;
250         int c;
251
252         ctx = uci_alloc_context();
253         if (!ctx) {
254                 fprintf(stderr, "Out of memory\n");
255                 return 1;
256         }
257
258         while((c = getopt(argc, argv, "sS")) != -1) {
259                 switch(c) {
260                         case 's':
261                                 ctx->flags |= UCI_FLAG_STRICT;
262                                 break;
263                         case 'S':
264                                 ctx->flags &= ~UCI_FLAG_STRICT;
265                                 ctx->flags |= UCI_FLAG_PERROR;
266                                 break;
267                         default:
268                                 uci_usage(argc, argv);
269                                 break;
270                 }
271         }
272         if (optind > 1)
273                 argv[optind - 1] = argv[0];
274         argv += optind - 1;
275         argc -= optind - 1;
276
277         if (argc < 2)
278                 uci_usage(argc, argv);
279         ret = uci_cmd(argc - 1, argv + 1);
280         if (ret == 255)
281                 uci_usage(argc, argv);
282
283         uci_free_context(ctx);
284
285         return ret;
286 }