implement uci rename
[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,
24         CMD_SET,
25         CMD_DEL,
26         CMD_RENAME,
27         /* package cmds */
28         CMD_SHOW,
29         CMD_EXPORT,
30         CMD_COMMIT,
31 };
32
33 static void uci_usage(int argc, char **argv)
34 {
35         fprintf(stderr,
36                 "Usage: %s [<options>] <command> [<arguments>]\n\n"
37                 "Commands:\n"
38                 "\texport   [<config>]\n"
39                 "\tshow     [<config>[.<section>[.<option>]]]\n"
40                 "\tget      <config>.<section>[.<option>]\n"
41                 "\tset      <config>.<section>[.<option>]=<value>\n"
42                 "\n"
43                 "Options:\n"
44                 "\t-s       force strict mode (stop on parser errors)\n"
45                 "\t-S       disable strict mode\n"
46                 "\n",
47                 argv[0]
48         );
49         exit(255);
50 }
51
52 static void uci_show_section(struct uci_section *p)
53 {
54         struct uci_element *e;
55         const char *cname, *sname;
56
57         cname = p->package->e.name;
58         sname = p->e.name;
59         printf("%s.%s=%s\n", cname, sname, p->type);
60         uci_foreach_element(&p->options, e) {
61                 printf("%s.%s.%s=%s\n", cname, sname, e->name, uci_to_option(e)->value);
62         }
63 }
64
65 static void uci_show_package(struct uci_package *p)
66 {
67         struct uci_element *e;
68
69         uci_foreach_element( &p->sections, e) {
70                 uci_show_section(uci_to_section(e));
71         }
72 }
73
74
75 static int package_cmd(int cmd, char *package)
76 {
77         struct uci_package *p = NULL;
78
79         if (uci_load(ctx, package, &p) != UCI_OK) {
80                 uci_perror(ctx, appname);
81                 return 1;
82         }
83         switch(cmd) {
84         case CMD_COMMIT:
85                 if (uci_commit(ctx, &p) != UCI_OK)
86                         uci_perror(ctx, appname);
87                 break;
88         case CMD_EXPORT:
89                 uci_export(ctx, stdout, p, true);
90                 break;
91         case CMD_SHOW:
92                 uci_show_package(p);
93                 break;
94         }
95
96         uci_unload(ctx, p);
97         return 0;
98 }
99
100 static int uci_do_package_cmd(int cmd, int argc, char **argv)
101 {
102         char **configs = NULL;
103         char **p;
104
105         if (argc > 2)
106                 return 255;
107
108         if (argc == 2)
109                 return package_cmd(cmd, argv[1]);
110
111         if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
112                 uci_perror(ctx, appname);
113                 return 1;
114         }
115
116         for (p = configs; *p; p++) {
117                 package_cmd(cmd, *p);
118         }
119
120         return 0;
121 }
122
123
124 static int uci_do_section_cmd(int cmd, int argc, char **argv)
125 {
126         char *package = NULL;
127         char *section = NULL;
128         char *option = NULL;
129         char *value = NULL;
130         char **ptr = NULL;
131         struct uci_package *p = NULL;
132         struct uci_element *e = NULL;
133
134         if (argc != 2)
135                 return 255;
136
137         switch(cmd) {
138         case CMD_SET:
139         case CMD_RENAME:
140                 ptr = &value;
141                 break;
142         default:
143                 break;
144         }
145         if (uci_parse_tuple(ctx, argv[1], &package, &section, &option, ptr) != UCI_OK)
146                 return 1;
147
148         if (uci_load(ctx, package, &p) != UCI_OK) {
149                 uci_perror(ctx, appname);
150                 return 1;
151         }
152
153         switch(cmd) {
154         case CMD_GET:
155                 if (uci_lookup(ctx, &e, p, section, option) != UCI_OK)
156                         return 1;
157
158                 switch(e->type) {
159                 case UCI_TYPE_SECTION:
160                         value = uci_to_section(e)->type;
161                         break;
162                 case UCI_TYPE_OPTION:
163                         value = uci_to_option(e)->value;
164                         break;
165                 default:
166                         /* should not happen */
167                         return 1;
168                 }
169                 /* throw the value to stdout */
170                 printf("%s\n", value);
171                 break;
172         case CMD_RENAME:
173                 if (uci_rename(ctx, p, section, option, value) != UCI_OK) {
174                         uci_perror(ctx, appname);
175                         return 1;
176                 }
177                 break;
178         case CMD_SET:
179                 if (uci_set(ctx, p, section, option, value) != UCI_OK) {
180                         uci_perror(ctx, appname);
181                         return 1;
182                 }
183                 break;
184         case CMD_DEL:
185                 if (uci_delete(ctx, p, section, option) != UCI_OK) {
186                         uci_perror(ctx, appname);
187                         return 1;
188                 }
189                 break;
190         }
191
192         /* no save necessary for get */
193         if (cmd == CMD_GET)
194                 return 0;
195
196         /* save changes, but don't commit them yet */
197         if (uci_save(ctx, p) != UCI_OK) {
198                 uci_perror(ctx, appname);
199                 return 1;
200         }
201
202         return 0;
203 }
204
205 static int uci_cmd(int argc, char **argv)
206 {
207         int cmd = 0;
208
209         if (!strcasecmp(argv[0], "show"))
210                 cmd = CMD_SHOW;
211         else if (!strcasecmp(argv[0], "export"))
212                 cmd = CMD_EXPORT;
213         else if (!strcasecmp(argv[0], "commit"))
214                 cmd = CMD_COMMIT;
215         else if (!strcasecmp(argv[0], "get"))
216                 cmd = CMD_GET;
217         else if (!strcasecmp(argv[0], "set"))
218                 cmd = CMD_SET;
219         else if (!strcasecmp(argv[0], "ren") ||
220                  !strcasecmp(argv[0], "rename"))
221                 cmd = CMD_RENAME;
222         else if (!strcasecmp(argv[0], "del"))
223                 cmd = CMD_DEL;
224         else
225                 cmd = -1;
226
227         switch(cmd) {
228                 case CMD_GET:
229                 case CMD_SET:
230                 case CMD_DEL:
231                 case CMD_RENAME:
232                         return uci_do_section_cmd(cmd, argc, argv);
233                 case CMD_SHOW:
234                 case CMD_EXPORT:
235                 case CMD_COMMIT:
236                         return uci_do_package_cmd(cmd, argc, argv);
237                 default:
238                         return 255;
239         }
240 }
241
242 int main(int argc, char **argv)
243 {
244         int ret;
245         int c;
246
247         ctx = uci_alloc_context();
248         if (!ctx) {
249                 fprintf(stderr, "Out of memory\n");
250                 return 1;
251         }
252
253         while((c = getopt(argc, argv, "sS")) != -1) {
254                 switch(c) {
255                         case 's':
256                                 ctx->flags |= UCI_FLAG_STRICT;
257                                 break;
258                         case 'S':
259                                 ctx->flags &= ~UCI_FLAG_STRICT;
260                                 ctx->flags |= UCI_FLAG_PERROR;
261                                 break;
262                         default:
263                                 uci_usage(argc, argv);
264                                 break;
265                 }
266         }
267         if (optind > 1)
268                 argv[optind - 1] = argv[0];
269         argv += optind - 1;
270         argc -= optind - 1;
271
272         if (argc < 2)
273                 uci_usage(argc, argv);
274         ret = uci_cmd(argc - 1, argv + 1);
275         if (ret == 255)
276                 uci_usage(argc, argv);
277
278         uci_free_context(ctx);
279
280         return ret;
281 }