Permalink
Browse files

opensc-explorer: allow acting as script interpreter

Take a filename as argument and interpret the commands in it.
  • Loading branch information...
1 parent 69e9861 commit f8f02dbd659581c7b8ade221a3ba628691a7436a @marschap committed May 20, 2012
Showing with 27 additions and 8 deletions.
  1. +27 −8 src/tools/opensc-explorer.c
@@ -1612,18 +1612,18 @@ 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;
static int interactive;
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] = "";

0 comments on commit f8f02db

Please sign in to comment.