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