1 /* $Id: main.c,v 1.5 2002/07/16 22:37:04 rees Exp $ */
2
3 /*
4 * Smartcard commander.
5 * Written by Jim Rees and others at University of Michigan.
6 */
7
8 /*
9 copyright 2001
10 the regents of the university of michigan
11 all rights reserved
12
13 permission is granted to use, copy, create derivative works
14 and redistribute this software and such derivative works
15 for any purpose, so long as the name of the university of
16 michigan is not used in any advertising or publicity
17 pertaining to the use or distribution of this software
18 without specific, written prior authorization. if the
19 above copyright notice or any other identification of the
20 university of michigan is included in any copy of any
21 portion of this software, then the disclaimer below must
22 also be included.
23
24 this software is provided as is, without representation
25 from the university of michigan as to its fitness for any
26 purpose, and without warranty by the university of
27 michigan of any kind, either express or implied, including
28 without limitation the implied warranties of
29 merchantability and fitness for a particular purpose. the
30 regents of the university of michigan shall not be liable
31 for any damages, including special, indirect, incidental, or
32 consequential damages, with respect to any claim arising
33 out of or in connection with the use of the software, even
34 if it has been or is hereafter advised of the possibility of
35 such damages.
36 */
37
38 #include <unistd.h>
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <signal.h>
42 #include <string.h>
43 #include <errno.h>
44 #include <sectok.h>
45
46 #include "sc.h"
47
48 #define MAXTOKENS 300
49 #define CARDIOSIZE 200
50
51 void onintr(int sigraised);
52
53 const char usage[] =
54 "Usage: sectok [-0123hf:s:]\n"
55 " 0 - 3 : specify card reader number\n"
56 " f script_file : run commands from the script file\n"
57 " s sleep_time : set sleep between commands in the script\n"
58 " h : this message\n"
59 ;
60
61 int port, fd = -1, cla, sleepytime, interrupted, fflag;
62 FILE *cmdf;
63
64 int
main(ac,av)65 main(ac, av)
66 int ac;
67 char *av[];
68 {
69 int i, tc;
70 char buf[256], *scriptfile = NULL, *tp, *tv[MAXTOKENS];
71
72 tp = getenv("SCPORT");
73 if (tp)
74 port = atoi(tp);
75
76 while ((i = getopt(ac, av, "0123f:s:h")) != -1) {
77 switch (i) {
78 case '0':
79 case '1':
80 case '2':
81 case '3':
82 port = i - '0';
83 break;
84 case 'f':
85 fflag = 1;
86 scriptfile = optarg;
87 break;
88 case 's':
89 sleepytime = atoi(optarg);
90 break;
91 case 'h':
92 case '?':
93 fputs(usage, stdout);
94 exit(0);
95 break;
96 }
97 }
98
99 if (optind != ac) {
100 /* Dispatch from command line */
101 dispatch(ac - optind, &av[optind]);
102 exit(0);
103 }
104
105 if (scriptfile != NULL && strcmp(scriptfile, "-") != 0) {
106 cmdf = fopen(scriptfile, "r");
107 if (cmdf == NULL) {
108 perror(scriptfile);
109 exit(2);
110 }
111 } else
112 cmdf = stdin;
113
114 /* Interactive mode, or script file */
115
116 signal(SIGINT, onintr);
117 #ifdef __OpenBSD__
118 siginterrupt(SIGINT, 1);
119 #endif
120
121 /* The Main Loop */
122 while (1) {
123 fflush(stdout);
124 interrupted = 0;
125 if (sleepytime)
126 usleep(sleepytime * 1000);
127 if (!fflag) {
128 fprintf(stderr, "sectok> ");
129 fflush(stderr);
130 }
131
132 if (!fgets(buf, sizeof buf, cmdf)) {
133 putchar('\n');
134 if (interrupted)
135 continue;
136 else
137 break;
138 }
139 if (fflag)
140 printf("sectok> %s", buf);
141
142 for ((tp = strtok(buf, " \t\n\r")), tc = 0; tp; (tp = strtok(NULL, " \t\n\r")), tc++) {
143 if (tc < MAXTOKENS - 1)
144 tv[tc] = tp;
145 }
146 tv[tc] = NULL;
147
148 dispatch(tc, tv);
149 }
150
151 quit(0, NULL);
152 return 0;
153 }
154
onintr(int sigraised)155 void onintr(int sigraised)
156 {
157 interrupted++;
158 }
159