add a stub for testing uci_set()
[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 "uci.h"
16
17 static struct uci_context *ctx;
18
19 static void uci_usage(int argc, char **argv)
20 {
21 fprintf(stderr,
22 "Usage: %s [<options>] <command> [<arguments>]\n\n"
23 "Commands:\n"
24 "\texport [<config>]\n"
25 "\tshow [<config>[.<section>[.<option>]]]\n"
26 "\tget <config>.<section>[.<option>]\n"
27 "\tset <config>.<section>[.<option>]=<value>\n"
28 "\n",
29 argv[0]
30 );
31 exit(255);
32 }
33
34 static void uci_show_section(struct uci_section *p)
35 {
36 struct uci_element *e;
37 const char *cname, *sname;
38
39 cname = p->package->e.name;
40 sname = p->e.name;
41 printf("%s.%s=%s\n", cname, sname, p->type);
42 uci_foreach_element(&p->options, e) {
43 printf("%s.%s.%s=%s\n", cname, sname, e->name, uci_to_option(e)->value);
44 }
45 }
46
47 static void uci_show_package(struct uci_package *p, char *section)
48 {
49 struct uci_element *e;
50
51 uci_foreach_element( &p->sections, e) {
52 if (!section || !strcmp(e->name, section))
53 uci_show_section(uci_to_section(e));
54 }
55 }
56
57 static int uci_show(int argc, char **argv)
58 {
59 char *section = (argc > 2 ? argv[2] : NULL);
60 struct uci_package *package;
61 char **configs;
62 char **p;
63
64 configs = uci_list_configs(ctx);
65 if (!configs)
66 return 0;
67
68 for (p = configs; *p; p++) {
69 if ((argc < 2) || !strcmp(argv[1], *p)) {
70 if (uci_load(ctx, *p, &package) != UCI_OK) {
71 uci_perror(ctx, "uci_load");
72 return 255;
73 }
74 uci_show_package(package, section);
75 uci_unload(ctx, *p);
76 }
77 }
78
79 return 0;
80 }
81
82 static int uci_do_export(int argc, char **argv)
83 {
84 char **configs = uci_list_configs(ctx);
85 char **p;
86
87 if (!configs)
88 return 0;
89
90 for (p = configs; *p; p++) {
91 if ((argc < 2) || !strcmp(argv[1], *p)) {
92 struct uci_package *package = NULL;
93 int ret;
94
95 ret = uci_load(ctx, *p, &package);
96 if (ret)
97 continue;
98 uci_export(ctx, stdout, package);
99 uci_unload(ctx, *p);
100 }
101 }
102 return 0;
103 }
104
105 static void parse_tuple(char *str, char **package, char **section, char **option, char **value)
106 {
107 char *last = NULL;
108
109 *package = strtok(str, ".");
110 if (!*package)
111 goto done;
112
113 last = *package;
114 *section = strtok(NULL, ".");
115 if (!*section)
116 goto done;
117
118 last = *section;
119 *option = strtok(NULL, ".");
120 if (!*option)
121 goto done;
122
123 last = *option;
124 done:
125 if (!value)
126 return;
127
128 last = strtok(last, "=");
129 if (!last)
130 return;
131
132 *value = last + strlen(last) + 1;
133 }
134
135
136 static int uci_do_get(int argc, char **argv)
137 {
138 char *package = NULL;
139 char *section = NULL;
140 char *option = NULL;
141 struct uci_package *p = NULL;
142 struct uci_element *e = NULL;
143 char *value = NULL;
144
145 if (argc != 2)
146 return 255;
147
148 parse_tuple(argv[1], &package, &section, &option, NULL);
149 if (!package)
150 return 1;
151
152 if (uci_load(ctx, package, &p) != UCI_OK) {
153 uci_perror(ctx, "uci");
154 return 1;
155 }
156
157 if (uci_lookup(ctx, &e, package, section, option) != UCI_OK)
158 return 1;
159
160 switch(e->type) {
161 case UCI_TYPE_SECTION:
162 value = uci_to_section(e)->type;
163 break;
164 case UCI_TYPE_OPTION:
165 value = uci_to_option(e)->value;
166 break;
167 default:
168 /* should not happen */
169 return 1;
170 }
171
172 /* throw the value to stdout */
173 printf("%s\n", value);
174
175 return 0;
176 }
177
178 static int uci_do_set(int argc, char **argv)
179 {
180 struct uci_package *p;
181 char *package = NULL;
182 char *section = NULL;
183 char *option = NULL;
184 char *value = NULL;
185
186 if (argc != 2)
187 return 255;
188
189 parse_tuple(argv[1], &package, &section, &option, &value);
190 if (!package)
191 return 1;
192
193 if (uci_load(ctx, package, &p) != UCI_OK) {
194 uci_perror(ctx, "uci");
195 return 1;
196 }
197
198 if (uci_set(ctx, package, section, option, value) != UCI_OK) {
199 uci_perror(ctx, "uci");
200 return 1;
201 }
202 uci_show_package(p, NULL);
203 return 0;
204 }
205
206 static int uci_cmd(int argc, char **argv)
207 {
208 if (!strcasecmp(argv[0], "show"))
209 return uci_show(argc, argv);
210 if (!strcasecmp(argv[0], "export"))
211 return uci_do_export(argc, argv);
212 if (!strcasecmp(argv[0], "get"))
213 return uci_do_get(argc, argv);
214 if (!strcasecmp(argv[0], "set"))
215 return uci_do_set(argc, argv);
216 return 255;
217 }
218
219 int main(int argc, char **argv)
220 {
221 int ret;
222
223 ctx = uci_alloc_context();
224 if (argc < 2)
225 uci_usage(argc, argv);
226 ret = uci_cmd(argc - 1, argv + 1);
227 if (ret == 255)
228 uci_usage(argc, argv);
229 uci_free_context(ctx);
230
231 return ret;
232 }