cdd1838e1b2a17f15ed9a480c403661b32e3e65c
[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         /* section cmds */
23         CMD_GET = 1,
24         CMD_SET = 2,
25         CMD_DEL = 3,
26         /* package cmds */
27         CMD_SHOW = 4,
28         CMD_EXPORT = 5,
29         CMD_COMMIT = 6,
30 };
31
32 static void uci_usage(int argc, char **argv)
33 {
34         fprintf(stderr,
35                 "Usage: %s [<options>] <command> [<arguments>]\n\n"
36                 "Commands:\n"
37                 "\texport   [<config>]\n"
38                 "\tshow     [<config>[.<section>[.<option>]]]\n"
39                 "\tget      <config>.<section>[.<option>]\n"
40                 "\tset      <config>.<section>[.<option>]=<value>\n"
41                 "\n"
42                 "Options:\n"
43                 "\t-s       force strict mode (stop on parser errors)\n"
44                 "\t-S       disable strict mode\n"
45                 "\n",
46                 argv[0]
47         );
48         exit(255);
49 }
50
51 static void uci_show_section(struct uci_section *p)
52 {
53         struct uci_element *e;
54         const char *cname, *sname;
55
56         cname = p->package->e.name;
57         sname = p->e.name;
58         printf("%s.%s=%s\n", cname, sname, p->type);
59         uci_foreach_element(&p->options, e) {
60                 printf("%s.%s.%s=%s\n", cname, sname, e->name, uci_to_option(e)->value);
61         }
62 }
63
64 static void uci_show_package(struct uci_package *p)
65 {
66         struct uci_element *e;
67
68         uci_foreach_element( &p->sections, e) {
69                 uci_show_section(uci_to_section(e));
70         }
71 }
72
73
74 static int package_cmd(int cmd, char *package)
75 {
76         struct uci_package *p = NULL;
77
78         if (uci_load(ctx, package, &p) != UCI_OK) {
79                 uci_perror(ctx, appname);
80                 return 1;
81         }
82         switch(cmd) {
83         case CMD_COMMIT:
84                 if (uci_commit(ctx, &p) != UCI_OK)
85                         uci_perror(ctx, appname);
86                 break;
87         case CMD_EXPORT:
88                 uci_export(ctx, stdout, p, true);
89                 break;
90         case CMD_SHOW:
91                 uci_show_package(p);
92                 break;
93         }
94
95         uci_unload(ctx, p);
96         return 0;
97 }
98
99 static int uci_do_package_cmd(int cmd, int argc, char **argv)
100 {
101         char **configs = NULL;
102         char **p;
103
104         if (argc > 2)
105                 return 255;
106
107         if (argc == 2)
108                 return package_cmd(cmd, argv[1]);
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                 package_cmd(cmd, *p);
117         }
118
119         return 0;
120 }
121
122
123 static int uci_do_section_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         switch(cmd) {
144         case CMD_GET:
145                 if (uci_lookup(ctx, &e, p, section, option) != UCI_OK)
146                         return 1;
147
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 = 0;
192
193         if (!strcasecmp(argv[0], "show"))
194                 cmd = CMD_SHOW;
195         else if (!strcasecmp(argv[0], "export"))
196                 cmd = CMD_EXPORT;
197         else if (!strcasecmp(argv[0], "commit"))
198                 cmd = CMD_COMMIT;
199         else if (!strcasecmp(argv[0], "get"))
200                 cmd = CMD_GET;
201         else if (!strcasecmp(argv[0], "set"))
202                 cmd = CMD_SET;
203         else if (!strcasecmp(argv[0], "del"))
204                 cmd = CMD_DEL;
205
206         switch(cmd) {
207                 case CMD_GET:
208                 case CMD_SET:
209                 case CMD_DEL:
210                         return uci_do_section_cmd(cmd, argc, argv);
211                 case CMD_SHOW:
212                 case CMD_EXPORT:
213                 case CMD_COMMIT:
214                         return uci_do_package_cmd(cmd, argc, argv);
215                 default:
216                         return 255;
217         }
218 }
219
220 int main(int argc, char **argv)
221 {
222         int ret;
223         int c;
224
225         ctx = uci_alloc_context();
226         if (!ctx) {
227                 fprintf(stderr, "Out of memory\n");
228                 return 1;
229         }
230
231         while((c = getopt(argc, argv, "sS")) != -1) {
232                 switch(c) {
233                         case 's':
234                                 ctx->flags |= UCI_FLAG_STRICT;
235                                 break;
236                         case 'S':
237                                 ctx->flags &= ~UCI_FLAG_STRICT;
238                                 ctx->flags |= UCI_FLAG_PERROR;
239                                 break;
240                         default:
241                                 uci_usage(argc, argv);
242                                 break;
243                 }
244         }
245         if (optind > 1)
246                 argv[optind - 1] = argv[0];
247         argv += optind - 1;
248         argc -= optind - 1;
249
250         if (argc < 2)
251                 uci_usage(argc, argv);
252         ret = uci_cmd(argc - 1, argv + 1);
253         if (ret == 255)
254                 uci_usage(argc, argv);
255
256         uci_free_context(ctx);
257
258         return ret;
259 }