Annotation of src/usr.bin/sectok/main.c, Revision 1.2
1.2 ! rees 1: /* $Id: main.c,v 1.1 2001/06/27 19:41:45 rees Exp $ */
1.1 rees 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>
1.2 ! rees 43: #include <errno.h>
1.1 rees 44: #include <sectok.h>
45:
46: #include "sc.h"
47:
48: #define MAXTOKENS 300
49: #define CARDIOSIZE 200
50:
1.2 ! rees 51: void onintr(int sigraised);
! 52:
1.1 rees 53: const char usage[] =
54: "Usage: sectok [-1234hHf:s:]\n"
55: " 1 - 4 : 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:
1.2 ! rees 61: int port, fd = -1, cla, sleepytime, interrupted;
1.1 rees 62: FILE *cmdf;
63:
64: int
65: main(ac, av)
66: int ac;
67: char *av[];
68: {
1.2 ! rees 69: int i, tc;
1.1 rees 70: char buf[256], *scriptfile = NULL, *tp, *tv[MAXTOKENS];
71:
72: while ((i = getopt(ac, av, "1234c:d:f:Hhs:")) != -1) {
73: switch (i) {
74: case '1':
75: case '2':
76: case '3':
77: case '4':
78: port = i - '1';
79: break;
80: case 'f':
81: scriptfile = optarg;
82: break;
83: case 's':
84: sleepytime = atoi(optarg);
85: break;
86: case 'h':
87: case '?':
88: fputs(usage, stdout);
89: exit(0);
90: break;
91: }
92: }
93:
94: if (optind != ac) {
95: /* Dispatch from command line */
96: dispatch(ac - optind, &av[optind]);
97: exit(0);
98: }
99:
100: if (scriptfile != NULL) {
101: cmdf = fopen(scriptfile, "r");
102: if (cmdf == NULL) {
103: perror(scriptfile);
104: exit(2);
105: }
106: } else
107: cmdf = stdin;
108:
109: /* Interactive mode, or script file */
110:
1.2 ! rees 111: signal(SIGINT, onintr);
! 112:
1.1 rees 113: /* The Main Loop */
114: while (1) {
115: fflush(stdout);
1.2 ! rees 116: interrupted = 0;
1.1 rees 117: if (sleepytime)
118: usleep(sleepytime * 1000);
119: if (cmdf == stdin) {
120: fprintf(stderr, "sectok> ");
121: fflush(stderr);
122: }
123:
1.2 ! rees 124: if (!fgets(buf, sizeof buf, cmdf)) {
! 125: if (interrupted)
! 126: continue;
! 127: else
! 128: break;
! 129: }
1.1 rees 130: if (cmdf != stdin)
131: printf("sectok> %s", buf);
132:
133: for ((tp = strtok(buf, " \t\n\r")), tc = 0; tp; (tp = strtok(NULL, " \t\n\r")), tc++) {
134: if (tc < MAXTOKENS - 1)
135: tv[tc] = tp;
136: }
137: tv[tc] = NULL;
138:
139: dispatch(tc, tv);
140: }
141:
142: exit(0);
1.2 ! rees 143: }
! 144:
! 145: void onintr(int sigraised)
! 146: {
! 147: interrupted++;
1.1 rees 148: }