charon-cmd: move command line options to separate file, obsolete short options

This commit is contained in:
Martin Willi
2013-05-06 15:28:26 +02:00
parent 1fed10beb2
commit be44723de0
4 changed files with 109 additions and 54 deletions
+2 -1
View File
@@ -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
+21 -53
View File
@@ -32,6 +32,8 @@
#include <utils/backtrace.h>
#include <threading/thread.h>
#include "cmd/cmd_options.h"
/**
* Loglevel configuration
*/
@@ -158,27 +160,6 @@ static void segv_handler(int signal)
abort();
}
/**
* 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
*/
@@ -186,9 +167,10 @@ 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:
+28
View File
@@ -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" },
};
+58
View File
@@ -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_ @}*/