add strict mode to the cli usage
[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;
74 char **p;
75
76 configs = uci_list_configs(ctx);
77 if (!configs)
78 return 0;
79
80 if (argc >= 2) {
81 if (uci_load(ctx, argv[1], &package) != UCI_OK) {
82 uci_perror(ctx, NULL);
83 return 1;
84 }
85 uci_show_package(package, section);
86 uci_unload(ctx, package);
87 return 0;
88 }
89
90 for (p = configs; *p; p++) {
91 if ((argc < 2) || !strcmp(argv[1], *p)) {
92 if (uci_load(ctx, *p, &package) != UCI_OK) {
93 uci_perror(ctx, NULL);
94 return 1;
95 }
96 uci_show_package(package, section);
97 uci_unload(ctx, package);
98 }
99 }
100
101 return 0;
102 }
103
104 static int uci_do_export(int argc, char **argv)
105 {
106 char **configs = uci_list_configs(ctx);
107 char **p;
108
109 if (!configs)
110 return 0;
111
112 for (p = configs; *p; p++) {
113 if ((argc < 2) || !strcmp(argv[1], *p)) {
114 struct uci_package *package = NULL;
115 int ret;
116
117 ret = uci_load(ctx, *p, &package);
118 if (ret)
119 continue;
120 uci_export(ctx, stdout, package, true);
121 uci_unload(ctx, package);
122 }
123 }
124 return 0;
125 }
126
127 static int uci_do_cmd(int cmd, int argc, char **argv)
128 {
129 char *package = NULL;
130 char *section = NULL;
131 char *option = NULL;
132 char *value = NULL;
133 struct uci_package *p = NULL;
134 struct uci_element *e = NULL;
135
136 if (argc != 2)
137 return 255;
138
139 if (uci_parse_tuple(ctx, argv[1], &package, &section, &option, (cmd == CMD_SET ? &value : NULL)) != UCI_OK)
140 return 1;
141
142 if (uci_load(ctx, package, &p) != UCI_OK) {
143 uci_perror(ctx, appname);
144 return 1;
145 }
146
147 if (uci_lookup(ctx, &e, p, section, option) != UCI_OK)
148 return 1;
149
150 switch(cmd) {
151 case CMD_GET:
152 switch(e->type) {
153 case UCI_TYPE_SECTION:
154 value = uci_to_section(e)->type;
155 break;
156 case UCI_TYPE_OPTION:
157 value = uci_to_option(e)->value;
158 break;
159 default:
160 /* should not happen */
161 return 1;
162 }
163 /* throw the value to stdout */
164 printf("%s\n", value);
165 break;
166 case CMD_SET:
167 if (uci_set(ctx, p, section, option, value) != UCI_OK) {
168 uci_perror(ctx, appname);
169 return 1;
170 }
171 break;
172 case CMD_DEL:
173 if (uci_del(ctx, p, section, option) != UCI_OK) {
174 uci_perror(ctx, appname);
175 return 1;
176 }
177 break;
178 }
179
180 /* no save necessary for get */
181 if (cmd == CMD_GET)
182 return 0;
183
184 /* save changes, but don't commit them yet */
185 if (uci_save(ctx, p) != UCI_OK) {
186 uci_perror(ctx, appname);
187 return 1;
188 }
189
190 return 0;
191 }
192
193 static int uci_cmd(int argc, char **argv)
194 {
195 int cmd;
196
197 if (!strcasecmp(argv[0], "show"))
198 return uci_show(argc, argv);
199 if (!strcasecmp(argv[0], "export"))
200 return uci_do_export(argc, argv);
201
202 if (!strcasecmp(argv[0], "get"))
203 cmd = CMD_GET;
204 else if (!strcasecmp(argv[0], "set"))
205 cmd = CMD_SET;
206 else if (!strcasecmp(argv[0], "del"))
207 cmd = CMD_DEL;
208 else
209 return 255;
210 return uci_do_cmd(cmd, argc, argv);
211 }
212
213 int main(int argc, char **argv)
214 {
215 int ret;
216 int c;
217
218 ctx = uci_alloc_context();
219 if (!ctx) {
220 fprintf(stderr, "Out of memory\n");
221 return 1;
222 }
223
224 while((c = getopt(argc, argv, "sS")) != -1) {
225 switch(c) {
226 case 's':
227 ctx->flags |= UCI_FLAG_STRICT;
228 break;
229 case 'S':
230 ctx->flags &= ~UCI_FLAG_STRICT;
231 ctx->flags |= UCI_FLAG_PERROR;
232 break;
233 default:
234 uci_usage(argc, argv);
235 break;
236 }
237 }
238 if (optind > 1)
239 argv[optind - 1] = argv[0];
240 argv += optind - 1;
241 argc -= optind - 1;
242
243 if (argc < 2)
244 uci_usage(argc, argv);
245 ret = uci_cmd(argc - 1, argv + 1);
246 if (ret == 255)
247 uci_usage(argc, argv);
248
249 uci_free_context(ctx);
250
251 return ret;
252 }