opensc-explorer: allow acting as script interpreter

Take a filename as argument and interpret the commands in it.
This commit is contained in:
Peter Marschall 2012-05-20 17:12:14 +02:00
parent 69e9861ddd
commit f8f02dbd65
1 changed files with 27 additions and 8 deletions

View File

@ -1612,7 +1612,7 @@ static int parse_line(char *in, char **argv, int maxargc)
return argc;
}
static char * my_readline(char *prompt)
static char * my_readline(FILE *script, char *prompt)
{
static char buf[256];
static int initialized;
@ -1620,10 +1620,10 @@ static char * my_readline(char *prompt)
if (!initialized) {
initialized = 1;
interactive = isatty(fileno(stdin));
interactive = isatty(fileno(script));
#ifdef ENABLE_READLINE
if (interactive)
using_history ();
using_history();
#endif
}
#ifdef ENABLE_READLINE
@ -1637,10 +1637,11 @@ static char * my_readline(char *prompt)
/* Either we don't have readline or we are not running
interactively */
#ifndef ENABLE_READLINE
printf("%s", prompt);
if (interactive)
printf("%s", prompt);
#endif
fflush(stdout);
if (fgets(buf, sizeof(buf), stdin) == NULL)
if (fgets(buf, sizeof(buf), script) == NULL)
return NULL;
if (strlen(buf) == 0)
return NULL;
@ -1657,6 +1658,7 @@ int main(int argc, char * const argv[])
char *cargv[260];
sc_context_param_t ctx_param;
int lcycle = SC_CARDCTRL_LIFECYCLE_ADMIN;
FILE *script;
printf("OpenSC Explorer version %s\n", sc_get_version());
@ -1739,16 +1741,33 @@ int main(int argc, char * const argv[])
if (r && r != SC_ERROR_NOT_SUPPORTED)
printf("unable to change lifecycle: %s\n", sc_strerror(r));
while (1) {
switch (argc - optind) {
default:
util_print_usage_and_die(app_name, options, option_help);
break;
case 0:
script = stdin;
break;
case 1:
if (strcmp(argv[optind], "-") == 0) {
script = stdin;
}
else if ((script = fopen(argv[optind], "r")) == NULL) {
util_print_usage_and_die(app_name, options, option_help);
}
break;
}
while (!feof(script)) {
struct command *cmd;
char prompt[3*SC_MAX_PATH_STRING_SIZE];
sprintf(prompt, "OpenSC [%s]> ", path_to_filename(&current_path, '/'));
line = my_readline(prompt);
line = my_readline(script, prompt);
if (line == NULL)
break;
cargc = parse_line(line, cargv, DIM(cargv));
if (cargc < 1)
if ((cargc < 1) || (*cargv[0] == '#'))
continue;
for (r=cargc; r < (int)DIM(cargv); r++)
cargv[r] = "";