[BACK]Return to main.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / sectok

Annotation of src/usr.bin/sectok/main.c, Revision 1.7

1.7     ! rees        1: /* $Id: main.c,v 1.6 2001/10/02 16:22:40 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[] =
1.4       rees       54: "Usage: sectok [-0123hf:s:]\n"
                     55: "    0 - 3         : specify card reader number\n"
1.1       rees       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];
1.4       rees       71:
                     72:     tp = getenv("SCPORT");
                     73:     if (tp)
                     74:        port = atoi(tp);
1.1       rees       75:
1.3       rees       76:     while ((i = getopt(ac, av, "0123f:s:h")) != -1) {
1.1       rees       77:        switch (i) {
1.3       rees       78:        case '0':
1.1       rees       79:        case '1':
                     80:        case '2':
                     81:        case '3':
1.3       rees       82:            port = i - '0';
1.1       rees       83:            break;
                     84:        case 'f':
                     85:            scriptfile = optarg;
                     86:            break;
                     87:        case 's':
                     88:            sleepytime = atoi(optarg);
                     89:            break;
                     90:        case 'h':
                     91:        case '?':
                     92:            fputs(usage, stdout);
                     93:            exit(0);
                     94:            break;
                     95:        }
                     96:     }
                     97:
                     98:     if (optind != ac) {
                     99:        /* Dispatch from command line */
                    100:        dispatch(ac - optind, &av[optind]);
                    101:        exit(0);
                    102:     }
                    103:
                    104:     if (scriptfile != NULL) {
                    105:        cmdf = fopen(scriptfile, "r");
                    106:        if (cmdf == NULL) {
                    107:            perror(scriptfile);
                    108:            exit(2);
                    109:        }
                    110:     } else
                    111:        cmdf = stdin;
                    112:
                    113:     /* Interactive mode, or script file */
                    114:
1.2       rees      115:     signal(SIGINT, onintr);
1.7     ! rees      116: #ifdef __OpenBSD__
        !           117:     siginterrupt(SIGINT, 1);
        !           118: #endif
1.2       rees      119:
1.1       rees      120:     /* The Main Loop */
                    121:     while (1) {
                    122:        fflush(stdout);
1.2       rees      123:        interrupted = 0;
1.1       rees      124:        if (sleepytime)
                    125:            usleep(sleepytime * 1000);
                    126:        if (cmdf == stdin) {
                    127:            fprintf(stderr, "sectok> ");
                    128:            fflush(stderr);
                    129:        }
                    130:
1.2       rees      131:        if (!fgets(buf, sizeof buf, cmdf)) {
1.7     ! rees      132:            putchar('\n');
1.2       rees      133:            if (interrupted)
                    134:                continue;
1.7     ! rees      135:            else
1.2       rees      136:                break;
                    137:        }
1.1       rees      138:        if (cmdf != stdin)
                    139:            printf("sectok> %s", buf);
                    140:
                    141:        for ((tp = strtok(buf, " \t\n\r")), tc = 0; tp; (tp = strtok(NULL, " \t\n\r")), tc++) {
                    142:            if (tc < MAXTOKENS - 1)
                    143:                tv[tc] = tp;
                    144:        }
                    145:        tv[tc] = NULL;
                    146:
                    147:        dispatch(tc, tv);
                    148:     }
                    149:
1.6       rees      150:     quit(0, NULL);
                    151:     return 0;
1.2       rees      152: }
                    153:
                    154: void onintr(int sigraised)
                    155: {
                    156:     interrupted++;
1.1       rees      157: }