diff options
-rw-r--r-- | src/charon-cmd/Makefile.am | 3 | ||||
-rw-r--r-- | src/charon-cmd/charon-cmd.c | 74 | ||||
-rw-r--r-- | src/charon-cmd/cmd/cmd_options.c | 28 | ||||
-rw-r--r-- | src/charon-cmd/cmd/cmd_options.h | 58 |
4 files changed, 109 insertions, 54 deletions
diff --git a/src/charon-cmd/Makefile.am b/src/charon-cmd/Makefile.am index de1580e3b..cd949e08f 100644 --- a/src/charon-cmd/Makefile.am +++ b/src/charon-cmd/Makefile.am @@ -1,7 +1,8 @@ sbin_PROGRAMS = charon-cmd charon_cmd_SOURCES = \ -charon-cmd.c + cmd/cmd_options.h cmd/cmd_options.c \ + charon-cmd.c charon-cmd.o : $(top_builddir)/config.status diff --git a/src/charon-cmd/charon-cmd.c b/src/charon-cmd/charon-cmd.c index b2cb6e810..f93b619db 100644 --- a/src/charon-cmd/charon-cmd.c +++ b/src/charon-cmd/charon-cmd.c @@ -32,6 +32,8 @@ #include <utils/backtrace.h> #include <threading/thread.h> +#include "cmd/cmd_options.h" + /** * Loglevel configuration */ @@ -159,36 +161,16 @@ static void segv_handler(int signal) } /** - * Command line arguments, similar to "struct option", but with descriptions - */ -static struct { - /** long option name */ - const char *lng; - /** short option name */ - const char shrt; - /** takes argument */ - int has_arg; - /** decription of argument */ - const char *arg; - /** description to option */ - const char *desc; -} options[] = { - { "help", 'h', no_argument, "", - "print this usage information and exit" }, - { "version", 'v', no_argument, "", - "show version information and exit" }, -}; - -/** * Print command line usage and exit */ static void usage(FILE *out, char *msg, char *binary) { int i, pre, post, padto = 0, spacing = 2; - for (i = 0; i < countof(options); i++) + for (i = 0; i < CMD_OPT_COUNT; i++) { - padto = max(padto, strlen(options[i].lng) + strlen(options[i].arg)); + padto = max(padto, strlen(cmd_options[i].name) + + strlen(cmd_options[i].arg)); } padto += spacing; @@ -197,9 +179,9 @@ static void usage(FILE *out, char *msg, char *binary) fprintf(out, "%s\n", msg); } fprintf(out, "Usage: %s\n", binary); - for (i = 0; i < countof(options); i++) + for (i = 0; i < CMD_OPT_COUNT; i++) { - switch (options[i].has_arg) + switch (cmd_options[i].has_arg) { case required_argument: pre = '<'; @@ -214,11 +196,11 @@ static void usage(FILE *out, char *msg, char *binary) pre = post = ' '; break; } - fprintf(out, " --%s (-%-c) %c%s%c %-*s%s\n", - options[i].lng, options[i].shrt, - pre, options[i].arg, post, - padto - strlen(options[i].lng) - strlen(options[i].arg), "", - options[i].desc); + fprintf(out, " --%s %c%s%c %-*s%s\n", + cmd_options[i].name, + pre, cmd_options[i].arg, post, + padto - strlen(cmd_options[i].name) - strlen(cmd_options[i].arg), "", + cmd_options[i].desc); } } @@ -229,38 +211,24 @@ static void handle_arguments(int argc, char *argv[]) { while (TRUE) { - struct option long_opts[countof(options) + 1] = {}; - char optstring[countof(options) * 3 + 1] = {}; - int i, pos = 0; + struct option long_opts[CMD_OPT_COUNT + 1] = {}; + int i; - for (i = 0; i < countof(options); i++) + for (i = 0; i < CMD_OPT_COUNT; i++) { - long_opts[i].name = options[i].lng; - long_opts[i].val = options[i].shrt; - long_opts[i].has_arg = options[i].has_arg; - optstring[pos++] = options[i].shrt; - switch (options[i].has_arg) - { - case optional_argument: - optstring[pos++] = ':'; - /* FALL */ - case required_argument: - optstring[pos++] = ':'; - /* FALL */ - case no_argument: - default: - break; - } + long_opts[i].name = cmd_options[i].name; + long_opts[i].val = cmd_options[i].id; + long_opts[i].has_arg = cmd_options[i].has_arg; } - switch (getopt_long(argc, argv, optstring, long_opts, NULL)) + switch (getopt_long(argc, argv, "", long_opts, NULL)) { case EOF: break; - case 'h': + case CMD_OPT_HELP: usage(stdout, NULL, argv[0]); exit(0); - case 'v': + case CMD_OPT_VERSION: printf("%s, strongSwan %s\n", "charon-cmd", VERSION); exit(0); default: diff --git a/src/charon-cmd/cmd/cmd_options.c b/src/charon-cmd/cmd/cmd_options.c new file mode 100644 index 000000000..997cd3431 --- /dev/null +++ b/src/charon-cmd/cmd/cmd_options.c @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2013 Martin Willi + * Copyright (C) 2013 revosec AG + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#include "cmd_options.h" + +#include <getopt.h> + +/** + * See header. + */ +cmd_option_t cmd_options[CMD_OPT_COUNT] = { + { CMD_OPT_HELP, "help", no_argument, "", + "print this usage information and exit" }, + { CMD_OPT_VERSION, "version", no_argument, "", + "show version information and exit" }, +}; diff --git a/src/charon-cmd/cmd/cmd_options.h b/src/charon-cmd/cmd/cmd_options.h new file mode 100644 index 000000000..d453a53eb --- /dev/null +++ b/src/charon-cmd/cmd/cmd_options.h @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2013 Martin Willi + * Copyright (C) 2013 revosec AG + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +/** + * @defgroup cmd_option cmd_option + * @{ @ingroup cmd + */ + +#ifndef CMD_OPTION_H_ +#define CMD_OPTION_H_ + +typedef struct cmd_option_t cmd_option_t; +typedef enum cmd_option_type_t cmd_option_type_t; + +/** + * Command line options + */ +enum cmd_option_type_t { + CMD_OPT_HELP, + CMD_OPT_VERSION, + + CMD_OPT_COUNT +}; + +/** + * Command line arguments, similar to "struct option", but with descriptions + */ +struct cmd_option_t { + /** option identifier */ + cmd_option_type_t id; + /** long option name */ + const char *name; + /** takes argument */ + int has_arg; + /** decription of argument */ + const char *arg; + /** description to option */ + const char *desc; +}; + +/** + * Registered CMD options. + */ +extern cmd_option_t cmd_options[CMD_OPT_COUNT]; + +#endif /** CMD_OPTION_H_ @}*/ |