[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.9

1.9     ! deraadt     1: /*     $OpenBSD: main.c,v 1.8 2002/06/17 07:10:52 deraadt Exp $ */
1.1       rees        2:
                      3: /*
                      4:  * Smartcard commander.
                      5:  * Written by Jim Rees and others at University of Michigan.
                      6:  */
                      7:
                      8: /*
1.8       deraadt     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:  */
1.1       rees       37:
                     38: #include <unistd.h>
                     39: #include <stdlib.h>
                     40: #include <stdio.h>
                     41: #include <signal.h>
1.8       deraadt    42: #include <limits.h>
1.1       rees       43: #include <string.h>
1.2       rees       44: #include <errno.h>
1.1       rees       45: #include <sectok.h>
                     46:
                     47: #include "sc.h"
                     48:
                     49: #define MAXTOKENS 300
                     50: #define CARDIOSIZE 200
                     51:
1.8       deraadt    52: void    onintr(int sigraised);
1.2       rees       53:
1.8       deraadt    54: int     port, fd = -1, cla, sleepytime;
                     55: volatile sig_atomic_t interrupted;
                     56: FILE   *cmdf;
1.1       rees       57:
1.9     ! deraadt    58: static void
1.8       deraadt    59: usage(void)
                     60: {
                     61:        fprintf(stderr,
                     62:            "usage: sectok [-0123] [-f scriptfile] [-s sleeptime] [cmd] [args]\n");
                     63:        exit(1);
                     64: }
1.1       rees       65:
                     66: int
1.9     ! deraadt    67: main(int argc, char *argv[])
1.1       rees       68: {
1.8       deraadt    69:        char    buf[_POSIX2_LINE_MAX], *scriptfile = NULL, *tp, *tv[MAXTOKENS];
                     70:        int     i, tc;
1.4       rees       71:
1.8       deraadt    72:        tp = getenv("SCPORT");
                     73:        if (tp)
                     74:                port = atoi(tp);
                     75:
                     76:        while ((i = getopt(argc, argv, "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:                        scriptfile = optarg;
                     86:                        break;
                     87:                case 's':
                     88:                        sleepytime = atoi(optarg);
                     89:                        break;
                     90:                case 'h':
                     91:                default:
                     92:                        usage();
                     93:                        break;
                     94:                }
1.1       rees       95:        }
                     96:
1.8       deraadt    97:        if (optind != argc) {
                     98:                /* Dispatch from command line */
                     99:                dispatch(argc - optind, &argv[optind]);
                    100:                exit(0);
1.1       rees      101:        }
1.8       deraadt   102:        if (scriptfile != NULL) {
                    103:                cmdf = fopen(scriptfile, "r");
                    104:                if (cmdf == NULL) {
                    105:                        perror(scriptfile);
                    106:                        exit(2);
                    107:                }
                    108:        } else
                    109:                cmdf = stdin;
1.1       rees      110:
1.8       deraadt   111:        /* Interactive mode, or script file */
1.1       rees      112:
1.8       deraadt   113:        signal(SIGINT, onintr);
1.7       rees      114: #ifdef __OpenBSD__
1.8       deraadt   115:        siginterrupt(SIGINT, 1);
1.7       rees      116: #endif
1.2       rees      117:
1.8       deraadt   118:        /* The Main Loop */
                    119:        while (1) {
                    120:                fflush(stdout);
                    121:                interrupted = 0;
                    122:                if (sleepytime)
                    123:                        usleep(sleepytime * 1000);
                    124:                if (cmdf == stdin) {
                    125:                        fprintf(stderr, "sectok> ");
                    126:                        fflush(stderr);
                    127:                }
                    128:                if (!fgets(buf, sizeof buf, cmdf)) {
                    129:                        putchar('\n');
                    130:                        if (interrupted)
                    131:                                continue;
                    132:                        else
                    133:                                break;
                    134:                }
                    135:                if (cmdf != stdin)
                    136:                        printf("sectok> %s", buf);
                    137:
                    138:                for ((tp = strtok(buf, " \t\n\r")), tc = 0; tp;
                    139:                    (tp = strtok(NULL, " \t\n\r")), tc++) {
                    140:                        if (tc < MAXTOKENS - 1)
                    141:                                tv[tc] = tp;
                    142:                }
                    143:                tv[tc] = NULL;
1.1       rees      144:
1.8       deraadt   145:                dispatch(tc, tv);
1.2       rees      146:        }
1.1       rees      147:
1.8       deraadt   148:        quit(0, NULL);
                    149:        return 0;
1.2       rees      150: }
                    151:
1.8       deraadt   152: void
                    153: onintr(int sigraised)
1.2       rees      154: {
1.8       deraadt   155:        interrupted++;
1.1       rees      156: }