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