- added getopt option parsing (-r reader -c driver -dddd)
What's a test app when you can't enable debugging?! git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@699 c6295689-39f2-0310-b995-f0e70906c6a9
This commit is contained in:
parent
4811eeca8d
commit
b1881beb01
@ -9,50 +9,106 @@
|
||||
#endif
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <getopt.h>
|
||||
#include <opensc/opensc.h>
|
||||
#include "sc-test.h"
|
||||
|
||||
struct sc_context *ctx;
|
||||
struct sc_card *card;
|
||||
|
||||
const struct option options[] = {
|
||||
{ "reader", 1, 0, 'r' },
|
||||
{ "driver", 1, 0, 'c' },
|
||||
{ "debug", 0, 0, 'd' },
|
||||
{ 0, 0, 0, 0 }
|
||||
};
|
||||
|
||||
#if 0
|
||||
const char * option_help[] = {
|
||||
"Uses reader number <arg> [0]",
|
||||
"Forces the use of driver <arg> [auto-detect]",
|
||||
"Debug output -- may be supplied several times",
|
||||
};
|
||||
#endif
|
||||
|
||||
int sc_test_init(int *argc, char *argv[])
|
||||
{
|
||||
int i, c;
|
||||
char *opt_driver = NULL, *app_name;
|
||||
int opt_debug = 0, opt_reader = -1;
|
||||
int i, c, rc;
|
||||
|
||||
if ((app_name = strrchr(argv[0], '/')) != NULL)
|
||||
app_name++;
|
||||
else
|
||||
app_name = argv[0];
|
||||
|
||||
while ((c = getopt_long(*argc, argv, "r:c:d", options, NULL)) != -1) {
|
||||
switch (c) {
|
||||
case 'r':
|
||||
opt_reader = atoi(optarg);
|
||||
break;
|
||||
case 'c':
|
||||
opt_driver = optarg;
|
||||
break;
|
||||
case 'd':
|
||||
opt_debug++;
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr,
|
||||
"usage: %s [-r reader] [-c driver] [-d]\n",
|
||||
app_name);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
*argc = optind;
|
||||
|
||||
printf("Using libopensc version %s.\n", sc_get_version());
|
||||
i = sc_establish_context(&ctx, "tests");
|
||||
i = sc_establish_context(&ctx, app_name);
|
||||
if (i != SC_SUCCESS) {
|
||||
printf("Failed to establish context: %s\n", sc_strerror(i));
|
||||
return i;
|
||||
}
|
||||
i = sc_detect_card_presence(ctx->reader[0], 0);
|
||||
printf("Card %s.\n", i == 1 ? "present" : "absent");
|
||||
if (i < 0) {
|
||||
return i;
|
||||
ctx->debug = opt_debug;
|
||||
|
||||
if (opt_reader >= ctx->reader_count) {
|
||||
fprintf(stderr, "Illegal reader number.\n"
|
||||
"Only %d reader(s) configured.\n",
|
||||
ctx->reader_count);
|
||||
exit(1);
|
||||
}
|
||||
if (i == 0) {
|
||||
printf("Please insert a smart card.\n");
|
||||
fflush(stdout);
|
||||
#if 0
|
||||
i = sc_wait_for_card(ctx, -1, -1);
|
||||
if (i < 0)
|
||||
return i;
|
||||
if (i != 1)
|
||||
return -1;
|
||||
#endif
|
||||
c = -1;
|
||||
for (i = 0; i < ctx->reader_count; i++) {
|
||||
if (sc_detect_card_presence(ctx->reader[i], 0) == 1) {
|
||||
printf("Card detected in reader '%s'\n", ctx->reader[i]->name);
|
||||
c = i;
|
||||
break;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
if (opt_reader >= 0) {
|
||||
rc = sc_detect_card_presence(
|
||||
ctx->reader[opt_reader], 0);
|
||||
printf("Card %s.\n", rc == 1 ? "present" : "absent");
|
||||
if (rc < 0)
|
||||
return rc;
|
||||
} else {
|
||||
for (i = rc = 0; rc != 1 && i < ctx->reader_count; i++)
|
||||
rc = sc_detect_card_presence(ctx->reader[i], 0);
|
||||
if (rc == 1)
|
||||
opt_reader = i - 1;
|
||||
}
|
||||
} else
|
||||
c = 0;
|
||||
|
||||
if (rc == 1) {
|
||||
printf("Card detected in reader '%s'\n",
|
||||
ctx->reader[opt_reader]->name);
|
||||
break;
|
||||
}
|
||||
if (rc < 0)
|
||||
return rc;
|
||||
|
||||
printf("Please insert a smart card. Press return to continue");
|
||||
fflush(stdout);
|
||||
while (getc(stdin) != '\n')
|
||||
;
|
||||
}
|
||||
|
||||
printf("Connecting... ");
|
||||
fflush(stdout);
|
||||
i = sc_connect_card(ctx->reader[c], 0, &card);
|
||||
i = sc_connect_card(ctx->reader[opt_reader], 0, &card);
|
||||
if (i != SC_SUCCESS) {
|
||||
printf("Connecting to card failed: %s\n", sc_strerror(i));
|
||||
return i;
|
||||
@ -66,6 +122,15 @@ int sc_test_init(int *argc, char *argv[])
|
||||
printf("\n");
|
||||
fflush(stdout);
|
||||
|
||||
if (opt_driver != NULL) {
|
||||
rc = sc_set_card_driver(ctx, opt_driver);
|
||||
if (rc != 0) {
|
||||
fprintf(stderr,
|
||||
"Driver '%s' not found!\n", opt_driver);
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user