Merge branch 'man-pki'
[strongswan.git] / src / pki / command.c
1 /*
2 * Copyright (C) 2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "command.h"
17 #include "pki.h"
18
19 #define _GNU_SOURCE
20 #include <getopt.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdio.h>
24
25 #include <library.h>
26 #include <utils/debug.h>
27 #include <utils/optionsfrom.h>
28
29 /**
30 * Registered commands.
31 */
32 command_t cmds[MAX_COMMANDS];
33
34 /**
35 * active command.
36 */
37 static int active = 0;
38
39 /**
40 * number of registered commands
41 */
42 static int registered = 0;
43
44 /**
45 * help command index
46 */
47 static int help_idx;
48
49 static int argc;
50
51 static char **argv;
52
53 static options_t *options;
54
55 /**
56 * Global options used by all subcommands
57 */
58 static struct option command_opts[MAX_COMMANDS > MAX_OPTIONS ?: MAX_OPTIONS];
59
60 /**
61 * Global optstring used by all subcommands
62 */
63 static char command_optstring[(MAX_COMMANDS > MAX_OPTIONS ?: MAX_OPTIONS) * 3];
64
65 /**
66 * Build command_opts/command_optstr for the active command
67 */
68 static void build_opts()
69 {
70 int i, pos = 0;
71
72 memset(command_opts, 0, sizeof(command_opts));
73 memset(command_optstring, 0, sizeof(command_optstring));
74 if (active == help_idx)
75 {
76 for (i = 0; cmds[i].cmd; i++)
77 {
78 command_opts[i].name = cmds[i].cmd;
79 command_opts[i].val = cmds[i].op;
80 command_optstring[i] = cmds[i].op;
81 }
82 }
83 else
84 {
85 for (i = 0; cmds[active].options[i].name; i++)
86 {
87 command_opts[i].name = cmds[active].options[i].name;
88 command_opts[i].has_arg = cmds[active].options[i].arg;
89 command_opts[i].val = cmds[active].options[i].op;
90 command_optstring[pos++] = cmds[active].options[i].op;
91 switch (cmds[active].options[i].arg)
92 {
93 case optional_argument:
94 command_optstring[pos++] = ':';
95 /* FALL */
96 case required_argument:
97 command_optstring[pos++] = ':';
98 /* FALL */
99 case no_argument:
100 default:
101 break;
102 }
103 }
104 }
105 }
106
107 /**
108 * getopt_long wrapper
109 */
110 int command_getopt(char **arg)
111 {
112 int op;
113
114 while (TRUE)
115 {
116 op = getopt_long(argc, argv, command_optstring, command_opts, NULL);
117 switch (op)
118 {
119 case '+':
120 if (!options->from(options, optarg, &argc, &argv, optind))
121 {
122 /* a error value */
123 return 255;
124 }
125 continue;
126 case 'v':
127 dbg_default_set_level(atoi(optarg));
128 continue;
129 default:
130 *arg = optarg;
131 return op;
132 }
133 }
134 }
135
136 /**
137 * Register a command
138 */
139 void command_register(command_t command)
140 {
141 int i;
142
143 cmds[registered] = command;
144 /* append default options, but not to --help */
145 if (!active)
146 {
147 for (i = 0; i < countof(cmds[registered].options) - 1; i++)
148 {
149 if (cmds[registered].options[i].name)
150 {
151 continue;
152 }
153 cmds[registered].options[i++] = (command_option_t) {
154 "debug", 'v', 1, "set debug level, default: 1"
155 };
156 cmds[registered].options[i++] = (command_option_t) {
157 "options", '+', 1, "read command line options from file"
158 };
159 break;
160 }
161 }
162 registered++;
163 }
164
165 /**
166 * Print usage text, with an optional error
167 */
168 int command_usage(char *error)
169 {
170 FILE *out = stdout;
171 int i;
172
173 if (error)
174 {
175 out = stderr;
176 fprintf(out, "Error: %s\n", error);
177 }
178 fprintf(out, "strongSwan %s PKI tool\n", VERSION);
179
180 if (active == help_idx)
181 {
182 fprintf(out, "loaded plugins: %s\n",
183 lib->plugins->loaded_plugins(lib->plugins));
184 }
185
186 fprintf(out, "usage:\n");
187 if (active == help_idx)
188 {
189 for (i = 0; cmds[i].cmd; i++)
190 {
191 fprintf(out, " pki --%-7s (-%c) %s\n",
192 cmds[i].cmd, cmds[i].op, cmds[i].description);
193 }
194 }
195 else
196 {
197 for (i = 0; cmds[active].line[i]; i++)
198 {
199 if (i == 0)
200 {
201 fprintf(out, " pki --%s %s\n",
202 cmds[active].cmd, cmds[active].line[i]);
203 }
204 else
205 {
206 fprintf(out, " %s\n", cmds[active].line[i]);
207 }
208 }
209 for (i = 0; cmds[active].options[i].name; i++)
210 {
211 fprintf(out, " --%-15s (-%c) %s\n",
212 cmds[active].options[i].name, cmds[active].options[i].op,
213 cmds[active].options[i].desc);
214 }
215 }
216 return error != NULL;
217 }
218
219
220 /**
221 * Show usage information
222 */
223 static int help(int argc, char *argv[])
224 {
225 return command_usage(NULL);
226 }
227
228 /**
229 * Dispatch cleanup hook
230 */
231 static void cleanup()
232 {
233 options->destroy(options);
234 }
235
236 /**
237 * Dispatch commands.
238 */
239 int command_dispatch(int c, char *v[])
240 {
241 int op, i;
242
243 options = options_create();
244 atexit(cleanup);
245 active = help_idx = registered;
246 argc = c;
247 argv = v;
248 command_register((command_t){help, 'h', "help", "show usage information"});
249
250 build_opts();
251 op = getopt_long(c, v, command_optstring, command_opts, NULL);
252 for (i = 0; cmds[i].cmd; i++)
253 {
254 if (cmds[i].op == op)
255 {
256 active = i;
257 build_opts();
258 return cmds[i].call();
259 }
260 }
261 return command_usage(c > 1 ? "invalid operation" : NULL);
262 }
263