From 9616ad4d9407611cee6028043f4dfebb1f4d40c7 Mon Sep 17 00:00:00 2001 From: Peter Marschall Date: Sun, 16 Sep 2018 12:51:50 +0200 Subject: [PATCH] opensc-explorer: fix&clarify handling of interactive mode * make 'interactive' a global variable * set it when opensc was called with the SCRIPT argument * document the behaviour in the manual page Make interactive a global variable and set it in main. --- doc/tools/opensc-explorer.1.xml | 10 +++++++++- src/tools/opensc-explorer.c | 18 ++++++++---------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/doc/tools/opensc-explorer.1.xml b/doc/tools/opensc-explorer.1.xml index d924a934..128b486a 100644 --- a/doc/tools/opensc-explorer.1.xml +++ b/doc/tools/opensc-explorer.1.xml @@ -28,10 +28,18 @@ Description The opensc-explorer utility can be - used interactively to perform miscellaneous operations + used to perform miscellaneous operations such as exploring the contents of or sending arbitrary APDU commands to a smart card or similar security token. + + If a SCRIPT is given, + opensc-explorer runs in non-interactive mode, + reading the commands from SCRIPT, + one command per line. + If no script is given, opensc-explorer + runs in interactive mode, reading commands from standard input. + diff --git a/src/tools/opensc-explorer.c b/src/tools/opensc-explorer.c index 1126cf77..48ca31c4 100644 --- a/src/tools/opensc-explorer.c +++ b/src/tools/opensc-explorer.c @@ -63,6 +63,7 @@ static sc_file_t *current_file = NULL; static sc_path_t current_path; static sc_context_t *ctx = NULL; static sc_card_t *card = NULL; +static int interactive = 1; static const struct option options[] = { { "reader", 1, NULL, 'r' }, @@ -2051,19 +2052,14 @@ static char *read_cmdline(FILE *script, char *prompt) { static char buf[SC_MAX_EXT_APDU_BUFFER_SIZE]; static int initialized; - static int interactive; - - if (!initialized) { - initialized = 1; - interactive = isatty(fileno(script)); -#ifdef ENABLE_READLINE - if (interactive) - using_history(); -#endif - } if (interactive) { #ifdef ENABLE_READLINE + if (!initialized) { + initialized = 1; + using_history(); + } + char *line = readline(prompt); /* add line to history if longer than 2 characters */ @@ -2196,9 +2192,11 @@ int main(int argc, char *argv[]) util_print_usage_and_die(app_name, options, option_help, "[SCRIPT]"); break; case 0: + interactive = 1; script = stdin; break; case 1: + interactive = 0; if (strcmp(argv[optind], "-") == 0) { script = stdin; }