diff --git a/src/tools/opensc-explorer.c b/src/tools/opensc-explorer.c index 34b54c9b..60cc2a87 100644 --- a/src/tools/opensc-explorer.c +++ b/src/tools/opensc-explorer.c @@ -36,7 +36,7 @@ const char *app_name = "opensc-explorer"; -int opt_reader = 0, opt_debug = 0; +int opt_reader = -1, opt_debug = 0, opt_wait = 0; const char *opt_driver = NULL; struct sc_file *current_file = NULL; @@ -47,6 +47,7 @@ struct sc_card *card = NULL; const struct option options[] = { { "reader", 1, 0, 'r' }, { "card-driver", 1, 0, 'c' }, + { "wait", 1, 0, 'w' }, { "debug", 0, 0, 'd' }, { 0, 0, 0, 0 } }; @@ -1238,7 +1239,7 @@ int main(int argc, char * const argv[]) printf("OpenSC Explorer version %s\n", sc_get_version()); while (1) { - c = getopt_long(argc, argv, "r:c:d", options, &long_optind); + c = getopt_long(argc, argv, "r:c:dw", options, &long_optind); if (c == -1) break; if (c == '?') @@ -1253,6 +1254,9 @@ int main(int argc, char * const argv[]) case 'd': opt_debug++; break; + case 'w': + opt_wait = 1; + break; } } @@ -1263,16 +1267,7 @@ int main(int argc, char * const argv[]) } if (opt_debug) ctx->debug = opt_debug; - if (opt_reader >= ctx->reader_count || opt_reader < 0) { - fprintf(stderr, "Illegal reader number. Only %d reader(s) configured.\n", ctx->reader_count); - err = 1; - goto end; - } - if (sc_detect_card_presence(ctx->reader[opt_reader], 0) != 1) { - fprintf(stderr, "Card not present.\n"); - err = 3; - goto end; - } + if (opt_driver != NULL) { err = sc_set_card_driver(ctx, opt_driver); if (err) { @@ -1281,13 +1276,11 @@ int main(int argc, char * const argv[]) goto end; } } - fprintf(stderr, "Connecting to card in reader %s...\n", ctx->reader[opt_reader]->name); - r = sc_connect_card(ctx->reader[opt_reader], 0, &card); - if (r) { - fprintf(stderr, "Failed to connect to card: %s\n", sc_strerror(r)); - err = 1; + + err = connect_card(ctx, &card, opt_reader, 0, opt_wait, 0); + if (err) goto end; - } + printf("Using card driver: %s\n", card->driver->name); r = sc_lock(card); if (r) { diff --git a/src/tools/opensc-tool.c b/src/tools/opensc-tool.c index 757457c8..e8b29968 100644 --- a/src/tools/opensc-tool.c +++ b/src/tools/opensc-tool.c @@ -41,7 +41,7 @@ const char *app_name = "opensc-tool"; -int opt_reader = 0, opt_no_cache = 0, opt_debug = 0; +int opt_reader = -1, opt_no_cache = 0, opt_debug = 0, opt_wait = 0; char * opt_apdus[8]; int opt_apdu_count = 0; int quiet = 0; @@ -56,6 +56,7 @@ const struct option options[] = { { "reader", 1, 0, 'r' }, { "card-driver", 1, 0, 'c' }, { "quiet", 0, 0, 'q' }, + { "wait", 0, 0, 'w' }, { "debug", 0, 0, 'd' }, { 0, 0, 0, 0 } }; @@ -70,6 +71,7 @@ const char *option_help[] = { "Uses reader number [0]", "Forces the use of driver [auto-detect]", "Quiet operation", + "Wait for a card to be inserted", "Debug output -- may be supplied several times", }; @@ -355,7 +357,7 @@ int main(int argc, char * const argv[]) const char *opt_driver = NULL; while (1) { - c = getopt_long(argc, argv, "lfr:qds:DRc:a", options, &long_optind); + c = getopt_long(argc, argv, "lfr:qds:DRc:aw", options, &long_optind); if (c == -1) break; if (c == '?') @@ -400,6 +402,9 @@ int main(int argc, char * const argv[]) case 'c': opt_driver = optarg; break; + case 'w': + opt_wait = 1; + break; } } if (action_count == 0) @@ -428,16 +433,7 @@ int main(int argc, char * const argv[]) } if (action_count <= 0) goto end; - if (opt_reader >= ctx->reader_count || opt_reader < 0) { - fprintf(stderr, "Illegal reader number. Only %d reader(s) configured.\n", ctx->reader_count); - err = 1; - goto end; - } - if (sc_detect_card_presence(ctx->reader[opt_reader], 0) != 1) { - fprintf(stderr, "Card not present.\n"); - err = 3; - goto end; - } + if (opt_driver != NULL) { err = sc_set_card_driver(ctx, opt_driver); if (err) { @@ -446,14 +442,11 @@ int main(int argc, char * const argv[]) goto end; } } - if (!quiet) - fprintf(stderr, "Connecting to card in reader %s...\n", ctx->reader[opt_reader]->name); - r = sc_connect_card(ctx->reader[opt_reader], 0, &card); - if (r) { - fprintf(stderr, "Failed to connect to card: %s\n", sc_strerror(r)); - err = 1; + + err = connect_card(ctx, &card, opt_reader, 0, opt_wait, quiet); + if (err) goto end; - } + printf("Using card driver: %s\n", card->driver->name); r = sc_lock(card); if (r) { diff --git a/src/tools/pkcs15-crypt.c b/src/tools/pkcs15-crypt.c index 79da4a8c..cbbde017 100644 --- a/src/tools/pkcs15-crypt.c +++ b/src/tools/pkcs15-crypt.c @@ -39,7 +39,7 @@ const char *app_name = "pkcs15-crypt"; -int opt_reader = 0, quiet = 0; +int opt_reader = -1, quiet = 0, opt_wait = 0; int opt_debug = 0; char * opt_pincode = NULL, * opt_key_id = NULL; char * opt_input = NULL, * opt_output = NULL; @@ -62,6 +62,7 @@ const struct option options[] = { { "quiet", 0, 0, 'q' }, { "debug", 0, 0, 'd' }, { "pin", 1, 0, 'p' }, + { "wait", 0, 0, 'w' }, { 0, 0, 0, 0 } }; @@ -399,7 +400,7 @@ int main(int argc, char * const argv[]) char *pincode; while (1) { - c = getopt_long(argc, argv, "sck:r:i:o:qp:d", options, &long_optind); + c = getopt_long(argc, argv, "sck:r:i:o:qp:dw", options, &long_optind); if (c == -1) break; if (c == '?') @@ -444,6 +445,9 @@ int main(int argc, char * const argv[]) case 'p': opt_pincode = optarg; break; + case 'w': + opt_wait = 1; + break; } } if (action_count == 0) @@ -455,23 +459,10 @@ int main(int argc, char * const argv[]) } if (opt_debug) ctx->debug = opt_debug; - if (opt_reader >= ctx->reader_count || opt_reader < 0) { - fprintf(stderr, "Illegal reader number. Only %d reader(s) configured.\n", ctx->reader_count); - err = 1; + + err = connect_card(ctx, &card, opt_reader, 0, opt_wait, quiet); + if (err) goto end; - } - if (sc_detect_card_presence(ctx->reader[opt_reader], 0) != 1) { - fprintf(stderr, "Card not present.\n"); - return 3; - } - if (!quiet) - fprintf(stderr, "Connecting to card in reader %s...\n", ctx->reader[opt_reader]->name); - r = sc_connect_card(ctx->reader[opt_reader], 0, &card); - if (r) { - fprintf(stderr, "Failed to connect to card: %s\n", sc_strerror(r)); - err = 1; - goto end; - } #if 1 r = sc_lock(card); diff --git a/src/tools/pkcs15-tool.c b/src/tools/pkcs15-tool.c index 5b7c0b2b..e3de3ac1 100644 --- a/src/tools/pkcs15-tool.c +++ b/src/tools/pkcs15-tool.c @@ -26,7 +26,7 @@ const char *app_name = "pkcs15-tool"; -int opt_reader = 0, opt_debug = 0; +int opt_reader = -1, opt_debug = 0, opt_wait = 0; int opt_no_cache = 0; char * opt_pin_id; char * opt_cert = NULL; @@ -66,6 +66,7 @@ const struct option options[] = { { "debug", 0, 0, 'd' }, { "no-cache", 0, 0, OPT_NO_CACHE }, { "pin-id", 1, 0, 'p' }, + { "wait", 0, 0, 'w' }, { 0, 0, 0, 0 } }; @@ -757,7 +758,7 @@ int main(int argc, char * const argv[]) int action_count = 0; while (1) { - c = getopt_long(argc, argv, "r:cko:qdp:LR:C", options, &long_optind); + c = getopt_long(argc, argv, "r:cko:qdp:LR:Cw", options, &long_optind); if (c == -1) break; if (c == '?') @@ -824,6 +825,9 @@ int main(int argc, char * const argv[]) case OPT_NO_CACHE: opt_no_cache++; break; + case 'w': + opt_wait = 1; + break; } } if (action_count == 0) @@ -835,29 +839,11 @@ int main(int argc, char * const argv[]) } if (opt_debug) ctx->debug = opt_debug; - if (ctx->reader_count == 0) { - fprintf(stderr, "No readers configured.\n"); - err = 1; + + err = connect_card(ctx, &card, opt_reader, 0, opt_wait, quiet); + if (err) goto end; - } - if (opt_reader >= ctx->reader_count || opt_reader < 0) { - fprintf(stderr, "Illegal reader number. Only %d reader(s) configured.\n", ctx->reader_count); - err = 1; - goto end; - } - if (sc_detect_card_presence(ctx->reader[opt_reader], 0) != 1) { - fprintf(stderr, "Card not present.\n"); - err = 3; - goto end; - } - if (!quiet) - fprintf(stderr, "Connecting to card in reader %s...\n", ctx->reader[opt_reader]->name); - r = sc_connect_card(ctx->reader[opt_reader], 0, &card); - if (r) { - fprintf(stderr, "Failed to connect to card: %s\n", sc_strerror(r)); - err = 1; - goto end; - } + printf("Using card driver: %s\n", card->driver->name); r = sc_lock(card); if (r) {