2 * Copyright (C) 2009 Martin Willi
3 * HSR Hochschule fuer Technik Rapperswil
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>.
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
26 #include <utils/debug.h>
27 #include <utils/optionsfrom.h>
30 * Registered commands.
32 static command_t cmds
[MAX_COMMANDS
];
37 static int active
= 0;
40 * number of registered commands
42 static int registered
= 0;
53 static options_t
*options
;
56 * Global options used by all subcommands
58 static struct option command_opts
[MAX_COMMANDS
> MAX_OPTIONS ? MAX_COMMANDS
: MAX_OPTIONS
];
61 * Global optstring used by all subcommands
63 static char command_optstring
[(MAX_COMMANDS
> MAX_OPTIONS ? MAX_COMMANDS
: MAX_OPTIONS
) * 3];
66 * Build command_opts/command_optstr for the active command
68 static void build_opts()
72 memset(command_opts
, 0, sizeof(command_opts
));
73 memset(command_optstring
, 0, sizeof(command_optstring
));
74 if (active
== help_idx
)
76 for (i
= 0; i
< MAX_COMMANDS
&& cmds
[i
].cmd
; i
++)
78 command_opts
[i
].name
= cmds
[i
].cmd
;
79 command_opts
[i
].val
= cmds
[i
].op
;
80 command_optstring
[i
] = cmds
[i
].op
;
85 for (i
= 0; cmds
[active
].options
[i
].name
; i
++)
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
)
93 case optional_argument
:
94 command_optstring
[pos
++] = ':';
96 case required_argument
:
97 command_optstring
[pos
++] = ':';
108 * getopt_long wrapper
110 int command_getopt(char **arg
)
116 op
= getopt_long(argc
, argv
, command_optstring
, command_opts
, NULL
);
120 if (!options
->from(options
, optarg
, &argc
, &argv
, optind
))
127 dbg_default_set_level(atoi(optarg
));
139 void command_register(command_t command
)
143 if (registered
== MAX_COMMANDS
)
145 fprintf(stderr
, "unable to register command, please increase "
150 cmds
[registered
] = command
;
151 /* append default options, but not to --help */
154 for (i
= 0; i
< countof(cmds
[registered
].options
) - 1; i
++)
156 if (!cmds
[registered
].options
[i
].name
)
161 if (i
> countof(cmds
[registered
].options
) - 3)
163 fprintf(stderr
, "command '%s' registered too many options, please "
164 "increase MAX_OPTIONS\n", command
.cmd
);
168 cmds
[registered
].options
[i
++] = (command_option_t
) {
169 "debug", 'v', 1, "set debug level, default: 1"
171 cmds
[registered
].options
[i
++] = (command_option_t
) {
172 "options", '+', 1, "read command line options from file"
175 for (i
= 0; cmds
[registered
].line
[i
]; i
++)
177 if (i
== MAX_LINES
- 1)
179 fprintf(stderr
, "command '%s' specifies too many usage summary "
180 "lines, please increase MAX_LINES\n", command
.cmd
);
189 * Print usage text, with an optional error
191 int command_usage(char *error
)
199 fprintf(out
, "Error: %s\n", error
);
201 fprintf(out
, "strongSwan %s PKI tool\n", VERSION
);
203 if (active
== help_idx
)
205 fprintf(out
, "loaded plugins: %s\n",
206 lib
->plugins
->loaded_plugins(lib
->plugins
));
209 fprintf(out
, "usage:\n");
210 if (active
== help_idx
)
212 for (i
= 0; i
< MAX_COMMANDS
&& cmds
[i
].cmd
; i
++)
214 fprintf(out
, " pki --%-7s (-%c) %s\n",
215 cmds
[i
].cmd
, cmds
[i
].op
, cmds
[i
].description
);
220 for (i
= 0; i
< MAX_LINES
&& cmds
[active
].line
[i
]; i
++)
224 indent
= fprintf(out
, " pki --%s ", cmds
[active
].cmd
);
225 fprintf(out
, "%s\n", cmds
[active
].line
[i
]);
229 fprintf(out
, "%*s%s\n", indent
, "", cmds
[active
].line
[i
]);
232 for (i
= 0; cmds
[active
].options
[i
].name
; i
++)
234 fprintf(out
, " --%-15s (-%c) %s\n",
235 cmds
[active
].options
[i
].name
, cmds
[active
].options
[i
].op
,
236 cmds
[active
].options
[i
].desc
);
239 return error
!= NULL
;
244 * Show usage information
246 static int help(int argc
, char *argv
[])
248 return command_usage(NULL
);
252 * Dispatch cleanup hook
254 static void cleanup()
256 options
->destroy(options
);
262 int command_dispatch(int c
, char *v
[])
266 options
= options_create();
268 active
= help_idx
= registered
;
271 command_register((command_t
){help
, 'h', "help", "show usage information"});
274 op
= getopt_long(c
, v
, command_optstring
, command_opts
, NULL
);
275 for (i
= 0; i
< MAX_COMMANDS
&& cmds
[i
].cmd
; i
++)
277 if (cmds
[i
].op
== op
)
281 return cmds
[i
].call();
284 return command_usage(c
> 1 ?
"invalid operation" : NULL
);