switched to command line handling with gengetopt

git-svn-id: https://vsmartcard.svn.sourceforge.net/svnroot/vsmartcard@737 96b47cad-a561-4643-ad3b-153ac7d7599c
This commit is contained in:
frankmorgner
2012-04-10 01:18:18 +00:00
parent 7549ed4d2b
commit 931f51e2f1
15 changed files with 361 additions and 759 deletions

View File

@@ -1,10 +1,29 @@
EXTRA_DIST = picc.py
do_subst = sed \
-e 's,[@]PACKAGE[@],$(PACKAGE),g' \
-e 's,[@]PACKAGE_NAME[@],$(PACKAGE_NAME),g' \
-e 's,[@]PACKAGE_TARNAME[@],$(PACKAGE_TARNAME),g' \
-e 's,[@]PACKAGE_BUGREPORT[@],$(PACKAGE_BUGREPORT),g' \
-e 's,[@]PACKAGE_URL[@],$(PACKAGE_URL),g' \
-e 's,[@]PACKAGE_VERSION[@],$(PACKAGE_VERSION),g'
BUILT_SOURCES = cmdline.h cmdline.c
EXTRA_DIST = pcsc-relay.ggo pcsc-relay.ggo.in
EXTRA_DIST += picc.py
bin_PROGRAMS = pcsc-relay
bin_SCRIPTS = picc.py
pcsc_relay_SOURCES = pcsc-relay.c pcscutil.c opicc.c lnfc.c binutil.c
pcsc_relay_SOURCES = pcsc-relay.c pcscutil.c opicc.c lnfc.c $(BUILT_SOURCES)
pcsc_relay_LDADD = $(PCSC_LIBS) $(LIBNFC_LIBS)
pcsc_relay_CFLAGS = $(PCSC_CFLAGS) $(LIBNFC_CFLAGS)
noinst_HEADERS = pcscutil.h pcsc-relay.h binutil.h
noinst_HEADERS = pcscutil.h pcsc-relay.h
pcsc-relay.c: $(BUILT_SOURCES)
$(BUILT_SOURCES): pcsc-relay.ggo
gengetopt --output-dir=$(srcdir) < $<
pcsc-relay.ggo: pcsc-relay.ggo.in
$(do_subst) < $< > $(srcdir)/$@

View File

@@ -1 +0,0 @@
../../ccid/src/binutil.c

View File

@@ -1 +0,0 @@
../../ccid/src/binutil.h

View File

@@ -1,5 +1,6 @@
/*
* Copyright (C) 2010 Dominik Oepen, Frank Morgner
* Copyright (C) 2009 by Dominik Oepen <oepen@informatik.hu-berlin.de>.
* Copyright (C) 2010-2012 by Frank Morgner <morgner@informatik.hu-berlin.de>.
*
* This file is part of pcsc-relay.
*
@@ -29,35 +30,13 @@
#include <string.h>
#include <errno.h>
#include "binutil.h"
#include "config.h"
#include "cmdline.h"
#include "pcsc-relay.h"
#include "pcscutil.h"
#define OPT_HELP 'h'
#define OPT_READER 'r'
#define OPT_VERBOSE 'v'
#define OPT_FOREGROUND 'f'
#define OPT_EMULATOR 'e'
static const struct option options[] = {
{ "help", no_argument, NULL, OPT_HELP },
{ "reader", required_argument, NULL, OPT_READER },
{ "foreground", no_argument, NULL, OPT_FOREGROUND },
{ "emulator", required_argument, NULL, OPT_EMULATOR },
{ "verbose", no_argument, NULL, OPT_VERBOSE },
{ NULL, 0, NULL, 0 }
};
static const char *option_help[] = {
"Print help and exit",
"Number of reader to use (default: 0)",
"Stay in foreground",
"NFC emulator backend (openpicc [default], libnfc)",
"Use (several times) to be more verbose",
};
static int doinfo = 0;
static int dodaemon = 1;
int verbose = 0;
static unsigned int readernum = 0;
static struct rf_driver *driver = &driver_openpicc;
static driver_data_t *driver_data = NULL;
@@ -137,63 +116,21 @@ int main (int argc, char **argv)
DWORD ctl, protocol;
struct sigaction new_sig, old_sig;
struct gengetopt_args_info args_info;
while (1) {
i = getopt_long(argc, argv, "hr:fe:v", options, &oindex);
if (i == -1)
/* Parse command line */
if (cmdline_parser (argc, argv, &args_info) != 0)
exit(1) ;
switch (args_info.emulator_arg) {
case emulator_arg_openpicc:
driver = &driver_openpicc;
break;
switch (i) {
case OPT_HELP:
print_usage(argv[0] , options, option_help);
exit(0);
break;
case OPT_READER:
if (sscanf(optarg, "%u", &readernum) != 1) {
parse_error(argv[0], options, option_help, optarg, oindex);
exit(2);
}
break;
case OPT_VERBOSE:
verbose++;
dodaemon = 0;
break;
case OPT_FOREGROUND:
dodaemon = 0;
break;
case OPT_EMULATOR:
if (strncmp(optarg, "openpicc", strlen("openpicc")) == 0)
driver = &driver_openpicc;
else
if (strncmp(optarg, "libnfc", strlen("libnfc")) == 0)
driver = &driver_libnfc;
else {
parse_error(argv[0], options, option_help, optarg, oindex);
exit(2);
}
break;
case '?':
/* fall through */
default:
exit(1);
break;
}
}
if (optind < argc) {
fprintf (stderr, "Unknown argument%s:", optind+1 == argc ? "" : "s");
while (optind < argc) {
fprintf(stderr, " \"%s\"", argv[optind++]);
fprintf(stderr, "%c", optind == argc ? '\n' : ',');
}
exit(1);
}
if (doinfo) {
fprintf(stderr, "%s written by Frank Morgner.\n" ,
PACKAGE_STRING);
return 0;
case emulator_arg_libnfc:
driver = &driver_libnfc;
break;
default:
exit(2);
}
@@ -214,13 +151,13 @@ int main (int argc, char **argv)
/* connect to reader and card */
r = pcsc_connect(readernum, SCARD_SHARE_EXCLUSIVE, SCARD_PROTOCOL_ANY,
r = pcsc_connect(args_info.reader_arg, SCARD_SHARE_EXCLUSIVE, SCARD_PROTOCOL_ANY,
&hContext, &readers, &hCard, &protocol);
if (r != SCARD_S_SUCCESS)
goto err;
if (dodaemon) {
if (!args_info.foreground_flag) {
INFO("Forking to background...\n");
verbose = -1;
daemonize();

View File

@@ -0,0 +1,24 @@
purpose "Emulate a contact-less smart card"
description "Using an contact-less interface (currently OpenPICC or libnfc) @PACKAGE@ receives command APDUs which are forwared to an existing smart card via PC/SC. @PACKAGE@ sends the response APDU back to the contact-less interface."
option "reader" r
"Number of the PC/SC reader to use (-1 for autodetect)"
int default="-1"
optional
option "emulator" e
"Contact-less emulator backend"
values="openpicc","libnfc" default="openpicc"
enum
optional
option "foreground" f
"Stay in foreground"
flag off
option "verbose" v
"Use (several times) to be more verbose"
multiple
optional
text "
Report bugs to @PACKAGE_BUGREPORT@
Written by Frank Morgner <morgner@informatik.hu-berlin.de> and Dominik Oepen <oepen@informatik.hu-berlin.de>"