version 1.7, 2002/03/14 15:47:31 |
version 1.8, 2002/06/17 07:10:52 |
|
|
/* $Id$ */ |
/* $OpenBSD$ */ |
|
|
/* |
/* |
* Smartcard commander. |
* Smartcard commander. |
|
|
*/ |
*/ |
|
|
/* |
/* |
copyright 2001 |
* copyright 2001 |
the regents of the university of michigan |
* the regents of the university of michigan |
all rights reserved |
* all rights reserved |
|
* |
|
* permission is granted to use, copy, create derivative works |
|
* and redistribute this software and such derivative works |
|
* for any purpose, so long as the name of the university of |
|
* michigan is not used in any advertising or publicity |
|
* pertaining to the use or distribution of this software |
|
* without specific, written prior authorization. if the |
|
* above copyright notice or any other identification of the |
|
* university of michigan is included in any copy of any |
|
* portion of this software, then the disclaimer below must |
|
* also be included. |
|
* |
|
* this software is provided as is, without representation |
|
* from the university of michigan as to its fitness for any |
|
* purpose, and without warranty by the university of |
|
* michigan of any kind, either express or implied, including |
|
* without limitation the implied warranties of |
|
* merchantability and fitness for a particular purpose. the |
|
* regents of the university of michigan shall not be liable |
|
* for any damages, including special, indirect, incidental, or |
|
* consequential damages, with respect to any claim arising |
|
* out of or in connection with the use of the software, even |
|
* if it has been or is hereafter advised of the possibility of |
|
* such damages. |
|
*/ |
|
|
permission is granted to use, copy, create derivative works |
|
and redistribute this software and such derivative works |
|
for any purpose, so long as the name of the university of |
|
michigan is not used in any advertising or publicity |
|
pertaining to the use or distribution of this software |
|
without specific, written prior authorization. if the |
|
above copyright notice or any other identification of the |
|
university of michigan is included in any copy of any |
|
portion of this software, then the disclaimer below must |
|
also be included. |
|
|
|
this software is provided as is, without representation |
|
from the university of michigan as to its fitness for any |
|
purpose, and without warranty by the university of |
|
michigan of any kind, either express or implied, including |
|
without limitation the implied warranties of |
|
merchantability and fitness for a particular purpose. the |
|
regents of the university of michigan shall not be liable |
|
for any damages, including special, indirect, incidental, or |
|
consequential damages, with respect to any claim arising |
|
out of or in connection with the use of the software, even |
|
if it has been or is hereafter advised of the possibility of |
|
such damages. |
|
*/ |
|
|
|
#include <unistd.h> |
#include <unistd.h> |
#include <stdlib.h> |
#include <stdlib.h> |
#include <stdio.h> |
#include <stdio.h> |
#include <signal.h> |
#include <signal.h> |
|
#include <limits.h> |
#include <string.h> |
#include <string.h> |
#include <errno.h> |
#include <errno.h> |
#include <sectok.h> |
#include <sectok.h> |
|
|
#define MAXTOKENS 300 |
#define MAXTOKENS 300 |
#define CARDIOSIZE 200 |
#define CARDIOSIZE 200 |
|
|
void onintr(int sigraised); |
void onintr(int sigraised); |
|
|
const char usage[] = |
int port, fd = -1, cla, sleepytime; |
"Usage: sectok [-0123hf:s:]\n" |
volatile sig_atomic_t interrupted; |
" 0 - 3 : specify card reader number\n" |
FILE *cmdf; |
" f script_file : run commands from the script file\n" |
|
" s sleep_time : set sleep between commands in the script\n" |
|
" h : this message\n" |
|
; |
|
|
|
int port, fd = -1, cla, sleepytime, interrupted; |
void |
FILE *cmdf; |
usage(void) |
|
{ |
|
fprintf(stderr, |
|
"usage: sectok [-0123] [-f scriptfile] [-s sleeptime] [cmd] [args]\n"); |
|
exit(1); |
|
} |
|
|
int |
int |
main(ac, av) |
main(argc, argv) |
int ac; |
int argc; |
char *av[]; |
char *argv[]; |
{ |
{ |
int i, tc; |
char buf[_POSIX2_LINE_MAX], *scriptfile = NULL, *tp, *tv[MAXTOKENS]; |
char buf[256], *scriptfile = NULL, *tp, *tv[MAXTOKENS]; |
int i, tc; |
|
|
tp = getenv("SCPORT"); |
tp = getenv("SCPORT"); |
if (tp) |
if (tp) |
port = atoi(tp); |
port = atoi(tp); |
|
|
while ((i = getopt(ac, av, "0123f:s:h")) != -1) { |
while ((i = getopt(argc, argv, "0123f:s:h")) != -1) { |
switch (i) { |
switch (i) { |
case '0': |
case '0': |
case '1': |
case '1': |
case '2': |
case '2': |
case '3': |
case '3': |
port = i - '0'; |
port = i - '0'; |
break; |
break; |
case 'f': |
case 'f': |
scriptfile = optarg; |
scriptfile = optarg; |
break; |
break; |
case 's': |
case 's': |
sleepytime = atoi(optarg); |
sleepytime = atoi(optarg); |
break; |
break; |
case 'h': |
case 'h': |
case '?': |
default: |
fputs(usage, stdout); |
usage(); |
exit(0); |
break; |
break; |
} |
} |
} |
} |
|
|
|
if (optind != ac) { |
if (optind != argc) { |
/* Dispatch from command line */ |
/* Dispatch from command line */ |
dispatch(ac - optind, &av[optind]); |
dispatch(argc - optind, &argv[optind]); |
exit(0); |
exit(0); |
} |
|
|
|
if (scriptfile != NULL) { |
|
cmdf = fopen(scriptfile, "r"); |
|
if (cmdf == NULL) { |
|
perror(scriptfile); |
|
exit(2); |
|
} |
} |
} else |
if (scriptfile != NULL) { |
cmdf = stdin; |
cmdf = fopen(scriptfile, "r"); |
|
if (cmdf == NULL) { |
|
perror(scriptfile); |
|
exit(2); |
|
} |
|
} else |
|
cmdf = stdin; |
|
|
/* Interactive mode, or script file */ |
/* Interactive mode, or script file */ |
|
|
signal(SIGINT, onintr); |
signal(SIGINT, onintr); |
#ifdef __OpenBSD__ |
#ifdef __OpenBSD__ |
siginterrupt(SIGINT, 1); |
siginterrupt(SIGINT, 1); |
#endif |
#endif |
|
|
/* The Main Loop */ |
/* The Main Loop */ |
while (1) { |
while (1) { |
fflush(stdout); |
fflush(stdout); |
interrupted = 0; |
interrupted = 0; |
if (sleepytime) |
if (sleepytime) |
usleep(sleepytime * 1000); |
usleep(sleepytime * 1000); |
if (cmdf == stdin) { |
if (cmdf == stdin) { |
fprintf(stderr, "sectok> "); |
fprintf(stderr, "sectok> "); |
fflush(stderr); |
fflush(stderr); |
} |
} |
|
if (!fgets(buf, sizeof buf, cmdf)) { |
|
putchar('\n'); |
|
if (interrupted) |
|
continue; |
|
else |
|
break; |
|
} |
|
if (cmdf != stdin) |
|
printf("sectok> %s", buf); |
|
|
if (!fgets(buf, sizeof buf, cmdf)) { |
for ((tp = strtok(buf, " \t\n\r")), tc = 0; tp; |
putchar('\n'); |
(tp = strtok(NULL, " \t\n\r")), tc++) { |
if (interrupted) |
if (tc < MAXTOKENS - 1) |
continue; |
tv[tc] = tp; |
else |
} |
break; |
tv[tc] = NULL; |
} |
|
if (cmdf != stdin) |
|
printf("sectok> %s", buf); |
|
|
|
for ((tp = strtok(buf, " \t\n\r")), tc = 0; tp; (tp = strtok(NULL, " \t\n\r")), tc++) { |
dispatch(tc, tv); |
if (tc < MAXTOKENS - 1) |
|
tv[tc] = tp; |
|
} |
} |
tv[tc] = NULL; |
|
|
|
dispatch(tc, tv); |
quit(0, NULL); |
} |
return 0; |
|
|
quit(0, NULL); |
|
return 0; |
|
} |
} |
|
|
void onintr(int sigraised) |
void |
|
onintr(int sigraised) |
{ |
{ |
interrupted++; |
interrupted++; |
} |
} |