1fa1cb1095d747672654973728d2bc22972eb250
[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         if (argc >= 2) {
69                 if (uci_load(ctx, argv[1], &package) != UCI_OK) {
70                         uci_perror(ctx, NULL);
71                         return 1;
72                 }
73                 uci_show_package(package, section);
74                 uci_unload(ctx, package);
75                 return 0;
76         }
77
78         for (p = configs; *p; p++) {
79                 if ((argc < 2) || !strcmp(argv[1], *p)) {
80                         if (uci_load(ctx, *p, &package) != UCI_OK) {
81                                 uci_perror(ctx, NULL);
82                                 return 1;
83                         }
84                         uci_show_package(package, section);
85                         uci_unload(ctx, package);
86                 }
87         }
88
89         return 0;
90 }
91
92 static int uci_do_export(int argc, char **argv)
93 {
94         char **configs = uci_list_configs(ctx);
95         char **p;
96
97         if (!configs)
98                 return 0;
99
100         for (p = configs; *p; p++) {
101                 if ((argc < 2) || !strcmp(argv[1], *p)) {
102                         struct uci_package *package = NULL;
103                         int ret;
104
105                         ret = uci_load(ctx, *p, &package);
106                         if (ret)
107                                 continue;
108                         uci_export(ctx, stdout, package, true);
109                         uci_unload(ctx, package);
110                 }
111         }
112         return 0;
113 }
114
115 static void parse_tuple(char *str, char **package, char **section, char **option, char **value)
116 {
117         char *last = NULL;
118
119         *package = strtok(str, ".");
120         if (!*package)
121                 goto done;
122
123         last = *package;
124         *section = strtok(NULL, ".");
125         if (!*section)
126                 goto done;
127
128         last = *section;
129         *option = strtok(NULL, ".");
130         if (!*option)
131                 goto done;
132
133         last = *option;
134 done:
135         if (!value)
136                 return;
137
138         last = strtok(last, "=");
139         if (!last)
140                 return;
141
142         *value = last + strlen(last) + 1;
143 }
144
145
146 static int uci_do_get(int argc, char **argv)
147 {
148         char *package = NULL;
149         char *section = NULL;
150         char *option = NULL;
151         struct uci_package *p = NULL;
152         struct uci_element *e = NULL;
153         char *value = NULL;
154
155         if (argc != 2)
156                 return 255;
157
158         parse_tuple(argv[1], &package, &section, &option, NULL);
159         if (!package)
160                 return 1;
161
162         if (uci_load(ctx, package, &p) != UCI_OK) {
163                 uci_perror(ctx, "uci");
164                 return 1;
165         }
166
167         if (uci_lookup(ctx, &e, p, section, option) != UCI_OK)
168                 return 1;
169
170         switch(e->type) {
171         case UCI_TYPE_SECTION:
172                 value = uci_to_section(e)->type;
173                 break;
174         case UCI_TYPE_OPTION:
175                 value = uci_to_option(e)->value;
176                 break;
177         default:
178                 /* should not happen */
179                 return 1;
180         }
181
182         /* throw the value to stdout */
183         printf("%s\n", value);
184
185         return 0;
186 }
187
188 static int uci_do_set(int argc, char **argv)
189 {
190         struct uci_package *p;
191         char *package = NULL;
192         char *section = NULL;
193         char *option = NULL;
194         char *value = NULL;
195
196         if (argc != 2)
197                 return 255;
198
199         parse_tuple(argv[1], &package, &section, &option, &value);
200         if (!package)
201                 return 1;
202
203         if (uci_load(ctx, package, &p) != UCI_OK) {
204                 uci_perror(ctx, "uci");
205                 return 1;
206         }
207
208         if (uci_set(ctx, package, section, option, value) != UCI_OK) {
209                 uci_perror(ctx, "uci");
210                 return 1;
211         }
212         uci_commit(ctx, p);
213         return 0;
214 }
215
216 static int uci_cmd(int argc, char **argv)
217 {
218         if (!strcasecmp(argv[0], "show"))
219                 return uci_show(argc, argv);
220         if (!strcasecmp(argv[0], "export"))
221                 return uci_do_export(argc, argv);
222         if (!strcasecmp(argv[0], "get"))
223                 return uci_do_get(argc, argv);
224         if (!strcasecmp(argv[0], "set"))
225                 return uci_do_set(argc, argv);
226         return 255;
227 }
228
229 int main(int argc, char **argv)
230 {
231         int ret;
232
233         ctx = uci_alloc_context();
234         if (argc < 2)
235                 uci_usage(argc, argv);
236         ret = uci_cmd(argc - 1, argv + 1);
237         if (ret == 255)
238                 uci_usage(argc, argv);
239         uci_free_context(ctx);
240
241         return ret;
242 }