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

Annotation of src/usr.bin/telnet/commands.c, Revision 1.9

1.9     ! tholo       1: /*     $OpenBSD: commands.c,v 1.8 1996/12/06 15:21:53 robin Exp $      */
1.4       deraadt     2: /*     $NetBSD: commands.c,v 1.14 1996/03/24 22:03:48 jtk Exp $        */
1.3       niklas      3:
1.1       deraadt     4: /*
                      5:  * Copyright (c) 1988, 1990, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *     This product includes software developed by the University of
                     19:  *     California, Berkeley and its contributors.
                     20:  * 4. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  */
                     36:
                     37: #ifndef lint
1.3       niklas     38: #if 0
                     39: static char sccsid[] = "@(#)commands.c 8.4 (Berkeley) 5/30/95";
1.4       deraadt    40: static char rcsid[] = "$NetBSD: commands.c,v 1.14 1996/03/24 22:03:48 jtk Exp $";
1.3       niklas     41: #else
1.9     ! tholo      42: static char rcsid[] = "$OpenBSD: commands.c,v 1.8 1996/12/06 15:21:53 robin Exp $";
1.3       niklas     43: #endif
1.1       deraadt    44: #endif /* not lint */
                     45:
                     46: #if    defined(unix)
                     47: #include <sys/param.h>
                     48: #if    defined(CRAY) || defined(sysV88)
                     49: #include <sys/types.h>
                     50: #endif
                     51: #include <sys/file.h>
                     52: #else
                     53: #include <sys/types.h>
                     54: #endif /* defined(unix) */
                     55: #include <sys/socket.h>
                     56: #include <netinet/in.h>
                     57: #ifdef CRAY
                     58: #include <fcntl.h>
                     59: #endif /* CRAY */
                     60:
                     61: #include <signal.h>
                     62: #include <netdb.h>
                     63: #include <ctype.h>
                     64: #include <pwd.h>
                     65: #include <varargs.h>
                     66: #include <errno.h>
                     67:
                     68: #include <arpa/telnet.h>
1.3       niklas     69: #include <sys/cdefs.h>
                     70: #define P __P
1.1       deraadt    71:
                     72: #include "general.h"
                     73:
                     74: #include "ring.h"
                     75:
                     76: #include "externs.h"
                     77: #include "defines.h"
                     78: #include "types.h"
                     79:
                     80: #if !defined(CRAY) && !defined(sysV88)
                     81: #include <netinet/in_systm.h>
                     82: # if (defined(vax) || defined(tahoe) || defined(hp300)) && !defined(ultrix)
                     83: # include <machine/endian.h>
                     84: # endif /* vax */
                     85: #endif /* !defined(CRAY) && !defined(sysV88) */
                     86: #include <netinet/ip.h>
                     87:
                     88:
1.3       niklas     89: #ifndef        MAXHOSTNAMELEN
                     90: #define        MAXHOSTNAMELEN 64
                     91: #endif MAXHOSTNAMELEN
1.1       deraadt    92:
                     93: #if    defined(IPPROTO_IP) && defined(IP_TOS)
                     94: int tos = -1;
                     95: #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
                     96:
                     97: char   *hostname;
                     98: static char _hostname[MAXHOSTNAMELEN];
                     99:
                    100: extern char *getenv();
                    101:
                    102: extern int isprefix();
                    103: extern char **genget();
                    104: extern int Ambiguous();
                    105:
                    106: static call();
                    107:
                    108: typedef struct {
                    109:        char    *name;          /* command name */
                    110:        char    *help;          /* help string (NULL for no help) */
                    111:        int     (*handler)();   /* routine which executes command */
                    112:        int     needconnect;    /* Do we need to be connected to execute? */
                    113: } Command;
                    114:
                    115: static char line[256];
                    116: static char saveline[256];
                    117: static int margc;
                    118: static char *margv[20];
                    119:
1.6       deraadt   120: #if    defined(SKEY)
                    121: #include <sys/wait.h>
                    122: #define PATH_SKEY      "/usr/bin/skey"
                    123:     int
                    124: skey_calc(argc, argv)
                    125:        int argc;
                    126:        char **argv;
                    127: {
                    128:        int status;
                    129:
                    130:        if(argc != 3) {
                    131:                printf("%s sequence challenge\n", argv[0]);
                    132:                return;
                    133:        }
                    134:
                    135:        switch(fork()) {
                    136:        case 0:
                    137:                execv(PATH_SKEY, argv);
                    138:                exit (1);
                    139:        case -1:
                    140:                perror("fork");
                    141:                break;
                    142:        default:
                    143:                (void) wait(&status);
                    144:                if (WIFEXITED(status))
                    145:                        return (WEXITSTATUS(status));
                    146:                return (0);
                    147:        }
                    148: }
                    149: #endif
                    150:
                    151:
                    152:
1.1       deraadt   153:     static void
                    154: makeargv()
                    155: {
                    156:     register char *cp, *cp2, c;
                    157:     register char **argp = margv;
                    158:
                    159:     margc = 0;
                    160:     cp = line;
                    161:     if (*cp == '!') {          /* Special case shell escape */
                    162:        strcpy(saveline, line); /* save for shell command */
                    163:        *argp++ = "!";          /* No room in string to get this */
                    164:        margc++;
                    165:        cp++;
                    166:     }
                    167:     while (c = *cp) {
                    168:        register int inquote = 0;
                    169:        while (isspace(c))
                    170:            c = *++cp;
                    171:        if (c == '\0')
                    172:            break;
                    173:        *argp++ = cp;
                    174:        margc += 1;
                    175:        for (cp2 = cp; c != '\0'; c = *++cp) {
                    176:            if (inquote) {
                    177:                if (c == inquote) {
                    178:                    inquote = 0;
                    179:                    continue;
                    180:                }
                    181:            } else {
                    182:                if (c == '\\') {
                    183:                    if ((c = *++cp) == '\0')
                    184:                        break;
                    185:                } else if (c == '"') {
                    186:                    inquote = '"';
                    187:                    continue;
                    188:                } else if (c == '\'') {
                    189:                    inquote = '\'';
                    190:                    continue;
                    191:                } else if (isspace(c))
                    192:                    break;
                    193:            }
                    194:            *cp2++ = c;
                    195:        }
                    196:        *cp2 = '\0';
                    197:        if (c == '\0')
                    198:            break;
                    199:        cp++;
                    200:     }
                    201:     *argp++ = 0;
                    202: }
                    203:
                    204: /*
                    205:  * Make a character string into a number.
                    206:  *
                    207:  * Todo:  1.  Could take random integers (12, 0x12, 012, 0b1).
                    208:  */
                    209:
                    210:        static
                    211: special(s)
                    212:        register char *s;
                    213: {
                    214:        register char c;
                    215:        char b;
                    216:
                    217:        switch (*s) {
                    218:        case '^':
                    219:                b = *++s;
                    220:                if (b == '?') {
                    221:                    c = b | 0x40;               /* DEL */
                    222:                } else {
                    223:                    c = b & 0x1f;
                    224:                }
                    225:                break;
                    226:        default:
                    227:                c = *s;
                    228:                break;
                    229:        }
                    230:        return c;
                    231: }
                    232:
                    233: /*
                    234:  * Construct a control character sequence
                    235:  * for a special character.
                    236:  */
                    237:        static char *
                    238: control(c)
                    239:        register cc_t c;
                    240: {
                    241:        static char buf[5];
                    242:        /*
                    243:         * The only way I could get the Sun 3.5 compiler
                    244:         * to shut up about
                    245:         *      if ((unsigned int)c >= 0x80)
                    246:         * was to assign "c" to an unsigned int variable...
                    247:         * Arggg....
                    248:         */
                    249:        register unsigned int uic = (unsigned int)c;
                    250:
                    251:        if (uic == 0x7f)
                    252:                return ("^?");
                    253:        if (c == (cc_t)_POSIX_VDISABLE) {
                    254:                return "off";
                    255:        }
                    256:        if (uic >= 0x80) {
                    257:                buf[0] = '\\';
                    258:                buf[1] = ((c>>6)&07) + '0';
                    259:                buf[2] = ((c>>3)&07) + '0';
                    260:                buf[3] = (c&07) + '0';
                    261:                buf[4] = 0;
                    262:        } else if (uic >= 0x20) {
                    263:                buf[0] = c;
                    264:                buf[1] = 0;
                    265:        } else {
                    266:                buf[0] = '^';
                    267:                buf[1] = '@'+c;
                    268:                buf[2] = 0;
                    269:        }
                    270:        return (buf);
                    271: }
                    272:
                    273:
                    274:
                    275: /*
                    276:  *     The following are data structures and routines for
                    277:  *     the "send" command.
                    278:  *
                    279:  */
1.3       niklas    280:
1.1       deraadt   281: struct sendlist {
                    282:     char       *name;          /* How user refers to it (case independent) */
                    283:     char       *help;          /* Help information (0 ==> no help) */
                    284:     int                needconnect;    /* Need to be connected */
                    285:     int                narg;           /* Number of arguments */
                    286:     int                (*handler)();   /* Routine to perform (for special ops) */
                    287:     int                nbyte;          /* Number of bytes to send this command */
                    288:     int                what;           /* Character to be sent (<0 ==> special) */
                    289: };
                    290: 
                    291:
                    292: static int
                    293:        send_esc P((void)),
                    294:        send_help P((void)),
                    295:        send_docmd P((char *)),
                    296:        send_dontcmd P((char *)),
                    297:        send_willcmd P((char *)),
                    298:        send_wontcmd P((char *));
                    299:
                    300: static struct sendlist Sendlist[] = {
                    301:     { "ao",    "Send Telnet Abort output",             1, 0, 0, 2, AO },
                    302:     { "ayt",   "Send Telnet 'Are You There'",          1, 0, 0, 2, AYT },
                    303:     { "brk",   "Send Telnet Break",                    1, 0, 0, 2, BREAK },
                    304:     { "break", 0,                                      1, 0, 0, 2, BREAK },
                    305:     { "ec",    "Send Telnet Erase Character",          1, 0, 0, 2, EC },
                    306:     { "el",    "Send Telnet Erase Line",               1, 0, 0, 2, EL },
                    307:     { "escape",        "Send current escape character",        1, 0, send_esc, 1, 0 },
                    308:     { "ga",    "Send Telnet 'Go Ahead' sequence",      1, 0, 0, 2, GA },
                    309:     { "ip",    "Send Telnet Interrupt Process",        1, 0, 0, 2, IP },
                    310:     { "intp",  0,                                      1, 0, 0, 2, IP },
                    311:     { "interrupt", 0,                                  1, 0, 0, 2, IP },
                    312:     { "intr",  0,                                      1, 0, 0, 2, IP },
                    313:     { "nop",   "Send Telnet 'No operation'",           1, 0, 0, 2, NOP },
                    314:     { "eor",   "Send Telnet 'End of Record'",          1, 0, 0, 2, EOR },
                    315:     { "abort", "Send Telnet 'Abort Process'",          1, 0, 0, 2, ABORT },
                    316:     { "susp",  "Send Telnet 'Suspend Process'",        1, 0, 0, 2, SUSP },
                    317:     { "eof",   "Send Telnet End of File Character",    1, 0, 0, 2, xEOF },
                    318:     { "synch", "Perform Telnet 'Synch operation'",     1, 0, dosynch, 2, 0 },
                    319:     { "getstatus", "Send request for STATUS",          1, 0, get_status, 6, 0 },
                    320:     { "?",     "Display send options",                 0, 0, send_help, 0, 0 },
                    321:     { "help",  0,                                      0, 0, send_help, 0, 0 },
                    322:     { "do",    0,                                      0, 1, send_docmd, 3, 0 },
                    323:     { "dont",  0,                                      0, 1, send_dontcmd, 3, 0 },
                    324:     { "will",  0,                                      0, 1, send_willcmd, 3, 0 },
                    325:     { "wont",  0,                                      0, 1, send_wontcmd, 3, 0 },
                    326:     { 0 }
                    327: };
                    328:
                    329: #define        GETSEND(name) ((struct sendlist *) genget(name, (char **) Sendlist, \
                    330:                                sizeof(struct sendlist)))
                    331:
                    332:     static int
                    333: sendcmd(argc, argv)
                    334:     int  argc;
                    335:     char **argv;
                    336: {
                    337:     int count;         /* how many bytes we are going to need to send */
                    338:     int i;
                    339:     int question = 0;  /* was at least one argument a question */
                    340:     struct sendlist *s;        /* pointer to current command */
                    341:     int success = 0;
                    342:     int needconnect = 0;
                    343:
                    344:     if (argc < 2) {
                    345:        printf("need at least one argument for 'send' command\n");
                    346:        printf("'send ?' for help\n");
                    347:        return 0;
                    348:     }
                    349:     /*
                    350:      * First, validate all the send arguments.
                    351:      * In addition, we see how much space we are going to need, and
                    352:      * whether or not we will be doing a "SYNCH" operation (which
                    353:      * flushes the network queue).
                    354:      */
                    355:     count = 0;
                    356:     for (i = 1; i < argc; i++) {
                    357:        s = GETSEND(argv[i]);
                    358:        if (s == 0) {
                    359:            printf("Unknown send argument '%s'\n'send ?' for help.\n",
                    360:                        argv[i]);
                    361:            return 0;
                    362:        } else if (Ambiguous(s)) {
                    363:            printf("Ambiguous send argument '%s'\n'send ?' for help.\n",
                    364:                        argv[i]);
                    365:            return 0;
                    366:        }
                    367:        if (i + s->narg >= argc) {
                    368:            fprintf(stderr,
                    369:            "Need %d argument%s to 'send %s' command.  'send %s ?' for help.\n",
                    370:                s->narg, s->narg == 1 ? "" : "s", s->name, s->name);
                    371:            return 0;
                    372:        }
                    373:        count += s->nbyte;
                    374:        if (s->handler == send_help) {
                    375:            send_help();
                    376:            return 0;
                    377:        }
                    378:
                    379:        i += s->narg;
                    380:        needconnect += s->needconnect;
                    381:     }
                    382:     if (!connected && needconnect) {
                    383:        printf("?Need to be connected first.\n");
                    384:        printf("'send ?' for help\n");
                    385:        return 0;
                    386:     }
                    387:     /* Now, do we have enough room? */
                    388:     if (NETROOM() < count) {
                    389:        printf("There is not enough room in the buffer TO the network\n");
                    390:        printf("to process your request.  Nothing will be done.\n");
                    391:        printf("('send synch' will throw away most data in the network\n");
                    392:        printf("buffer, if this might help.)\n");
                    393:        return 0;
                    394:     }
                    395:     /* OK, they are all OK, now go through again and actually send */
                    396:     count = 0;
                    397:     for (i = 1; i < argc; i++) {
                    398:        if ((s = GETSEND(argv[i])) == 0) {
                    399:            fprintf(stderr, "Telnet 'send' error - argument disappeared!\n");
                    400:            (void) quit();
                    401:            /*NOTREACHED*/
                    402:        }
                    403:        if (s->handler) {
                    404:            count++;
                    405:            success += (*s->handler)((s->narg > 0) ? argv[i+1] : 0,
                    406:                                  (s->narg > 1) ? argv[i+2] : 0);
                    407:            i += s->narg;
                    408:        } else {
                    409:            NET2ADD(IAC, s->what);
                    410:            printoption("SENT", IAC, s->what);
                    411:        }
                    412:     }
                    413:     return (count == success);
                    414: }
                    415:
                    416:     static int
                    417: send_esc()
                    418: {
                    419:     NETADD(escape);
                    420:     return 1;
                    421: }
                    422:
                    423:     static int
                    424: send_docmd(name)
                    425:     char *name;
                    426: {
                    427:     return(send_tncmd(send_do, "do", name));
                    428: }
                    429:
                    430:     static int
                    431: send_dontcmd(name)
                    432:     char *name;
                    433: {
                    434:     return(send_tncmd(send_dont, "dont", name));
                    435: }
                    436:     static int
                    437: send_willcmd(name)
                    438:     char *name;
                    439: {
                    440:     return(send_tncmd(send_will, "will", name));
                    441: }
                    442:     static int
                    443: send_wontcmd(name)
                    444:     char *name;
                    445: {
                    446:     return(send_tncmd(send_wont, "wont", name));
                    447: }
                    448:
                    449:     int
                    450: send_tncmd(func, cmd, name)
                    451:     void       (*func)();
                    452:     char       *cmd, *name;
                    453: {
                    454:     char **cpp;
                    455:     extern char *telopts[];
                    456:     register int val = 0;
                    457:
1.3       niklas    458:     if (isprefix(name, "?")) {
1.1       deraadt   459:        register int col, len;
                    460:
                    461:        printf("Usage: send %s <value|option>\n", cmd);
                    462:        printf("\"value\" must be from 0 to 255\n");
                    463:        printf("Valid options are:\n\t");
                    464:
                    465:        col = 8;
                    466:        for (cpp = telopts; *cpp; cpp++) {
                    467:            len = strlen(*cpp) + 3;
                    468:            if (col + len > 65) {
                    469:                printf("\n\t");
                    470:                col = 8;
                    471:            }
                    472:            printf(" \"%s\"", *cpp);
                    473:            col += len;
                    474:        }
                    475:        printf("\n");
                    476:        return 0;
                    477:     }
                    478:     cpp = (char **)genget(name, telopts, sizeof(char *));
                    479:     if (Ambiguous(cpp)) {
                    480:        fprintf(stderr,"'%s': ambiguous argument ('send %s ?' for help).\n",
                    481:                                        name, cmd);
                    482:        return 0;
                    483:     }
                    484:     if (cpp) {
                    485:        val = cpp - telopts;
                    486:     } else {
                    487:        register char *cp = name;
                    488:
                    489:        while (*cp >= '0' && *cp <= '9') {
                    490:            val *= 10;
                    491:            val += *cp - '0';
                    492:            cp++;
                    493:        }
                    494:        if (*cp != 0) {
                    495:            fprintf(stderr, "'%s': unknown argument ('send %s ?' for help).\n",
                    496:                                        name, cmd);
                    497:            return 0;
                    498:        } else if (val < 0 || val > 255) {
                    499:            fprintf(stderr, "'%s': bad value ('send %s ?' for help).\n",
                    500:                                        name, cmd);
                    501:            return 0;
                    502:        }
                    503:     }
                    504:     if (!connected) {
                    505:        printf("?Need to be connected first.\n");
                    506:        return 0;
                    507:     }
                    508:     (*func)(val, 1);
                    509:     return 1;
                    510: }
                    511:
                    512:     static int
                    513: send_help()
                    514: {
                    515:     struct sendlist *s;        /* pointer to current command */
                    516:     for (s = Sendlist; s->name; s++) {
                    517:        if (s->help)
                    518:            printf("%-15s %s\n", s->name, s->help);
                    519:     }
                    520:     return(0);
                    521: }
                    522: 
                    523: /*
                    524:  * The following are the routines and data structures referred
                    525:  * to by the arguments to the "toggle" command.
                    526:  */
                    527:
                    528:     static int
                    529: lclchars()
                    530: {
                    531:     donelclchars = 1;
                    532:     return 1;
                    533: }
                    534:
                    535:     static int
                    536: togdebug()
                    537: {
                    538: #ifndef        NOT43
                    539:     if (net > 0 &&
                    540:        (SetSockOpt(net, SOL_SOCKET, SO_DEBUG, debug)) < 0) {
                    541:            perror("setsockopt (SO_DEBUG)");
                    542:     }
                    543: #else  /* NOT43 */
                    544:     if (debug) {
                    545:        if (net > 0 && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0)
                    546:            perror("setsockopt (SO_DEBUG)");
                    547:     } else
                    548:        printf("Cannot turn off socket debugging\n");
                    549: #endif /* NOT43 */
                    550:     return 1;
                    551: }
                    552:
                    553:
                    554:     static int
                    555: togcrlf()
                    556: {
                    557:     if (crlf) {
                    558:        printf("Will send carriage returns as telnet <CR><LF>.\n");
                    559:     } else {
                    560:        printf("Will send carriage returns as telnet <CR><NUL>.\n");
                    561:     }
                    562:     return 1;
                    563: }
                    564:
                    565: int binmode;
                    566:
                    567:     static int
                    568: togbinary(val)
                    569:     int val;
                    570: {
                    571:     donebinarytoggle = 1;
                    572:
                    573:     if (val >= 0) {
                    574:        binmode = val;
                    575:     } else {
                    576:        if (my_want_state_is_will(TELOPT_BINARY) &&
                    577:                                my_want_state_is_do(TELOPT_BINARY)) {
                    578:            binmode = 1;
                    579:        } else if (my_want_state_is_wont(TELOPT_BINARY) &&
                    580:                                my_want_state_is_dont(TELOPT_BINARY)) {
                    581:            binmode = 0;
                    582:        }
                    583:        val = binmode ? 0 : 1;
                    584:     }
                    585:
                    586:     if (val == 1) {
                    587:        if (my_want_state_is_will(TELOPT_BINARY) &&
                    588:                                        my_want_state_is_do(TELOPT_BINARY)) {
                    589:            printf("Already operating in binary mode with remote host.\n");
                    590:        } else {
                    591:            printf("Negotiating binary mode with remote host.\n");
                    592:            tel_enter_binary(3);
                    593:        }
                    594:     } else {
                    595:        if (my_want_state_is_wont(TELOPT_BINARY) &&
                    596:                                        my_want_state_is_dont(TELOPT_BINARY)) {
                    597:            printf("Already in network ascii mode with remote host.\n");
                    598:        } else {
                    599:            printf("Negotiating network ascii mode with remote host.\n");
                    600:            tel_leave_binary(3);
                    601:        }
                    602:     }
                    603:     return 1;
                    604: }
                    605:
                    606:     static int
                    607: togrbinary(val)
                    608:     int val;
                    609: {
                    610:     donebinarytoggle = 1;
                    611:
                    612:     if (val == -1)
                    613:        val = my_want_state_is_do(TELOPT_BINARY) ? 0 : 1;
                    614:
                    615:     if (val == 1) {
                    616:        if (my_want_state_is_do(TELOPT_BINARY)) {
                    617:            printf("Already receiving in binary mode.\n");
                    618:        } else {
                    619:            printf("Negotiating binary mode on input.\n");
                    620:            tel_enter_binary(1);
                    621:        }
                    622:     } else {
                    623:        if (my_want_state_is_dont(TELOPT_BINARY)) {
                    624:            printf("Already receiving in network ascii mode.\n");
                    625:        } else {
                    626:            printf("Negotiating network ascii mode on input.\n");
                    627:            tel_leave_binary(1);
                    628:        }
                    629:     }
                    630:     return 1;
                    631: }
                    632:
                    633:     static int
                    634: togxbinary(val)
                    635:     int val;
                    636: {
                    637:     donebinarytoggle = 1;
                    638:
                    639:     if (val == -1)
                    640:        val = my_want_state_is_will(TELOPT_BINARY) ? 0 : 1;
                    641:
                    642:     if (val == 1) {
                    643:        if (my_want_state_is_will(TELOPT_BINARY)) {
                    644:            printf("Already transmitting in binary mode.\n");
                    645:        } else {
                    646:            printf("Negotiating binary mode on output.\n");
                    647:            tel_enter_binary(2);
                    648:        }
                    649:     } else {
                    650:        if (my_want_state_is_wont(TELOPT_BINARY)) {
                    651:            printf("Already transmitting in network ascii mode.\n");
                    652:        } else {
                    653:            printf("Negotiating network ascii mode on output.\n");
                    654:            tel_leave_binary(2);
                    655:        }
                    656:     }
                    657:     return 1;
                    658: }
                    659:
                    660:
                    661: static int togglehelp P((void));
                    662: #if    defined(AUTHENTICATION)
                    663: extern int auth_togdebug P((int));
                    664: #endif
                    665:
                    666: struct togglelist {
                    667:     char       *name;          /* name of toggle */
                    668:     char       *help;          /* help message */
                    669:     int                (*handler)();   /* routine to do actual setting */
                    670:     int                *variable;
                    671:     char       *actionexplanation;
1.8       robin     672:     int                needconnect;    /* Need to be connected */
1.1       deraadt   673: };
                    674:
                    675: static struct togglelist Togglelist[] = {
                    676:     { "autoflush",
                    677:        "flushing of output when sending interrupt characters",
                    678:            0,
                    679:                &autoflush,
1.8       robin     680:                    "flush output when sending interrupt characters", 0 },
1.1       deraadt   681:     { "autosynch",
                    682:        "automatic sending of interrupt characters in urgent mode",
                    683:            0,
                    684:                &autosynch,
1.8       robin     685:                    "send interrupt characters in urgent mode", 0 },
1.1       deraadt   686: #if    defined(AUTHENTICATION)
                    687:     { "autologin",
                    688:        "automatic sending of login and/or authentication info",
                    689:            0,
                    690:                &autologin,
1.8       robin     691:                    "send login name and/or authentication information", 0 },
1.1       deraadt   692:     { "authdebug",
                    693:        "Toggle authentication debugging",
                    694:            auth_togdebug,
                    695:                0,
1.8       robin     696:                     "print authentication debugging information", 0 },
1.1       deraadt   697: #endif
                    698:     { "skiprc",
                    699:        "don't read ~/.telnetrc file",
                    700:            0,
                    701:                &skiprc,
1.8       robin     702:                    "skip reading of ~/.telnetrc file", 0 },
1.1       deraadt   703:     { "binary",
                    704:        "sending and receiving of binary data",
                    705:            togbinary,
                    706:                0,
1.8       robin     707:                    0, 1 },
1.1       deraadt   708:     { "inbinary",
                    709:        "receiving of binary data",
                    710:            togrbinary,
                    711:                0,
1.8       robin     712:                    0, 1 },
1.1       deraadt   713:     { "outbinary",
                    714:        "sending of binary data",
                    715:            togxbinary,
                    716:                0,
1.8       robin     717:                    0, 1 },
1.1       deraadt   718:     { "crlf",
                    719:        "sending carriage returns as telnet <CR><LF>",
                    720:            togcrlf,
                    721:                &crlf,
1.8       robin     722:                    0, 0 },
1.1       deraadt   723:     { "crmod",
                    724:        "mapping of received carriage returns",
                    725:            0,
                    726:                &crmod,
1.8       robin     727:                    "map carriage return on output", 0 },
1.1       deraadt   728:     { "localchars",
                    729:        "local recognition of certain control characters",
                    730:            lclchars,
                    731:                &localchars,
1.8       robin     732:                    "recognize certain control characters", 0 },
                    733:     { " ", "", 0, 0 },         /* empty line */
1.1       deraadt   734: #if    defined(unix) && defined(TN3270)
                    735:     { "apitrace",
                    736:        "(debugging) toggle tracing of API transactions",
                    737:            0,
                    738:                &apitrace,
1.8       robin     739:                    "trace API transactions", 0 },
1.1       deraadt   740:     { "cursesdata",
                    741:        "(debugging) toggle printing of hexadecimal curses data",
                    742:            0,
                    743:                &cursesdata,
1.8       robin     744:                    "print hexadecimal representation of curses data", 0 },
1.1       deraadt   745: #endif /* defined(unix) && defined(TN3270) */
                    746:     { "debug",
                    747:        "debugging",
                    748:            togdebug,
                    749:                &debug,
1.8       robin     750:                    "turn on socket level debugging", 0 },
1.1       deraadt   751:     { "netdata",
                    752:        "printing of hexadecimal network data (debugging)",
                    753:            0,
                    754:                &netdata,
1.8       robin     755:                    "print hexadecimal representation of network traffic", 0 },
1.1       deraadt   756:     { "prettydump",
                    757:        "output of \"netdata\" to user readable format (debugging)",
                    758:            0,
                    759:                &prettydump,
1.8       robin     760:                    "print user readable output for \"netdata\"", 0 },
1.1       deraadt   761:     { "options",
                    762:        "viewing of options processing (debugging)",
                    763:            0,
                    764:                &showoptions,
1.8       robin     765:                    "show option processing", 0 },
1.1       deraadt   766: #if    defined(unix)
                    767:     { "termdata",
                    768:        "(debugging) toggle printing of hexadecimal terminal data",
                    769:            0,
                    770:                &termdata,
1.8       robin     771:                    "print hexadecimal representation of terminal traffic", 0 },
1.1       deraadt   772: #endif /* defined(unix) */
                    773:     { "?",
                    774:        0,
1.8       robin     775:            togglehelp, 0 },
1.1       deraadt   776:     { "help",
                    777:        0,
1.8       robin     778:            togglehelp, 0 },
1.1       deraadt   779:     { 0 }
                    780: };
                    781:
                    782:     static int
                    783: togglehelp()
                    784: {
                    785:     struct togglelist *c;
                    786:
                    787:     for (c = Togglelist; c->name; c++) {
                    788:        if (c->help) {
                    789:            if (*c->help)
                    790:                printf("%-15s toggle %s\n", c->name, c->help);
                    791:            else
                    792:                printf("\n");
                    793:        }
                    794:     }
                    795:     printf("\n");
                    796:     printf("%-15s %s\n", "?", "display help information");
                    797:     return 0;
                    798: }
                    799:
                    800:     static void
                    801: settogglehelp(set)
                    802:     int set;
                    803: {
                    804:     struct togglelist *c;
                    805:
                    806:     for (c = Togglelist; c->name; c++) {
                    807:        if (c->help) {
                    808:            if (*c->help)
                    809:                printf("%-15s %s %s\n", c->name, set ? "enable" : "disable",
                    810:                                                c->help);
                    811:            else
                    812:                printf("\n");
                    813:        }
                    814:     }
                    815: }
                    816:
                    817: #define        GETTOGGLE(name) (struct togglelist *) \
                    818:                genget(name, (char **) Togglelist, sizeof(struct togglelist))
                    819:
                    820:     static int
                    821: toggle(argc, argv)
                    822:     int  argc;
                    823:     char *argv[];
                    824: {
                    825:     int retval = 1;
                    826:     char *name;
                    827:     struct togglelist *c;
                    828:
                    829:     if (argc < 2) {
                    830:        fprintf(stderr,
                    831:            "Need an argument to 'toggle' command.  'toggle ?' for help.\n");
                    832:        return 0;
                    833:     }
                    834:     argc--;
                    835:     argv++;
                    836:     while (argc--) {
                    837:        name = *argv++;
                    838:        c = GETTOGGLE(name);
                    839:        if (Ambiguous(c)) {
                    840:            fprintf(stderr, "'%s': ambiguous argument ('toggle ?' for help).\n",
                    841:                                        name);
                    842:            return 0;
                    843:        } else if (c == 0) {
                    844:            fprintf(stderr, "'%s': unknown argument ('toggle ?' for help).\n",
                    845:                                        name);
                    846:            return 0;
1.8       robin     847:        } else if (!connected && c->needconnect) {
                    848:            printf("?Need to be connected first.\n");
                    849:            printf("'send ?' for help\n");
                    850:            return 0;
1.1       deraadt   851:        } else {
                    852:            if (c->variable) {
                    853:                *c->variable = !*c->variable;           /* invert it */
                    854:                if (c->actionexplanation) {
                    855:                    printf("%s %s.\n", *c->variable? "Will" : "Won't",
                    856:                                                        c->actionexplanation);
                    857:                }
                    858:            }
                    859:            if (c->handler) {
                    860:                retval &= (*c->handler)(-1);
                    861:            }
                    862:        }
                    863:     }
                    864:     return retval;
                    865: }
                    866: 
                    867: /*
                    868:  * The following perform the "set" command.
                    869:  */
                    870:
                    871: #ifdef USE_TERMIO
                    872: struct termio new_tc = { 0 };
                    873: #endif
                    874:
                    875: struct setlist {
                    876:     char *name;                                /* name */
                    877:     char *help;                                /* help information */
                    878:     void (*handler)();
                    879:     cc_t *charp;                       /* where it is located at */
                    880: };
                    881:
                    882: static struct setlist Setlist[] = {
                    883: #ifdef KLUDGELINEMODE
                    884:     { "echo",  "character to toggle local echoing on/off", 0, &echoc },
                    885: #endif
                    886:     { "escape",        "character to escape back to telnet command mode", 0, &escape },
                    887:     { "rlogin", "rlogin escape character", 0, &rlogin },
                    888:     { "tracefile", "file to write trace information to", SetNetTrace, (cc_t *)NetTraceFile},
                    889:     { " ", "" },
                    890:     { " ", "The following need 'localchars' to be toggled true", 0, 0 },
                    891:     { "flushoutput", "character to cause an Abort Output", 0, termFlushCharp },
                    892:     { "interrupt", "character to cause an Interrupt Process", 0, termIntCharp },
                    893:     { "quit",  "character to cause an Abort process", 0, termQuitCharp },
                    894:     { "eof",   "character to cause an EOF ", 0, termEofCharp },
                    895:     { " ", "" },
                    896:     { " ", "The following are for local editing in linemode", 0, 0 },
                    897:     { "erase", "character to use to erase a character", 0, termEraseCharp },
                    898:     { "kill",  "character to use to erase a line", 0, termKillCharp },
                    899:     { "lnext", "character to use for literal next", 0, termLiteralNextCharp },
                    900:     { "susp",  "character to cause a Suspend Process", 0, termSuspCharp },
                    901:     { "reprint", "character to use for line reprint", 0, termRprntCharp },
                    902:     { "worderase", "character to use to erase a word", 0, termWerasCharp },
                    903:     { "start", "character to use for XON", 0, termStartCharp },
                    904:     { "stop",  "character to use for XOFF", 0, termStopCharp },
                    905:     { "forw1", "alternate end of line character", 0, termForw1Charp },
                    906:     { "forw2", "alternate end of line character", 0, termForw2Charp },
                    907:     { "ayt",   "alternate AYT character", 0, termAytCharp },
                    908:     { 0 }
                    909: };
                    910:
                    911: #if    defined(CRAY) && !defined(__STDC__)
                    912: /* Work around compiler bug in pcc 4.1.5 */
                    913:     void
                    914: _setlist_init()
                    915: {
                    916: #ifndef        KLUDGELINEMODE
                    917: #define        N 5
                    918: #else
                    919: #define        N 6
                    920: #endif
                    921:        Setlist[N+0].charp = &termFlushChar;
                    922:        Setlist[N+1].charp = &termIntChar;
                    923:        Setlist[N+2].charp = &termQuitChar;
                    924:        Setlist[N+3].charp = &termEofChar;
                    925:        Setlist[N+6].charp = &termEraseChar;
                    926:        Setlist[N+7].charp = &termKillChar;
                    927:        Setlist[N+8].charp = &termLiteralNextChar;
                    928:        Setlist[N+9].charp = &termSuspChar;
                    929:        Setlist[N+10].charp = &termRprntChar;
                    930:        Setlist[N+11].charp = &termWerasChar;
                    931:        Setlist[N+12].charp = &termStartChar;
                    932:        Setlist[N+13].charp = &termStopChar;
                    933:        Setlist[N+14].charp = &termForw1Char;
                    934:        Setlist[N+15].charp = &termForw2Char;
                    935:        Setlist[N+16].charp = &termAytChar;
                    936: #undef N
                    937: }
                    938: #endif /* defined(CRAY) && !defined(__STDC__) */
                    939:
                    940:     static struct setlist *
                    941: getset(name)
                    942:     char *name;
                    943: {
                    944:     return (struct setlist *)
                    945:                genget(name, (char **) Setlist, sizeof(struct setlist));
                    946: }
                    947:
                    948:     void
                    949: set_escape_char(s)
                    950:     char *s;
                    951: {
                    952:        if (rlogin != _POSIX_VDISABLE) {
                    953:                rlogin = (s && *s) ? special(s) : _POSIX_VDISABLE;
                    954:                printf("Telnet rlogin escape character is '%s'.\n",
                    955:                                        control(rlogin));
                    956:        } else {
                    957:                escape = (s && *s) ? special(s) : _POSIX_VDISABLE;
                    958:                printf("Telnet escape character is '%s'.\n", control(escape));
                    959:        }
                    960: }
                    961:
                    962:     static int
                    963: setcmd(argc, argv)
                    964:     int  argc;
                    965:     char *argv[];
                    966: {
                    967:     int value;
                    968:     struct setlist *ct;
                    969:     struct togglelist *c;
                    970:
                    971:     if (argc < 2 || argc > 3) {
                    972:        printf("Format is 'set Name Value'\n'set ?' for help.\n");
                    973:        return 0;
                    974:     }
                    975:     if ((argc == 2) && (isprefix(argv[1], "?") || isprefix(argv[1], "help"))) {
                    976:        for (ct = Setlist; ct->name; ct++)
                    977:            printf("%-15s %s\n", ct->name, ct->help);
                    978:        printf("\n");
                    979:        settogglehelp(1);
                    980:        printf("%-15s %s\n", "?", "display help information");
                    981:        return 0;
                    982:     }
                    983:
                    984:     ct = getset(argv[1]);
                    985:     if (ct == 0) {
                    986:        c = GETTOGGLE(argv[1]);
                    987:        if (c == 0) {
                    988:            fprintf(stderr, "'%s': unknown argument ('set ?' for help).\n",
                    989:                        argv[1]);
                    990:            return 0;
                    991:        } else if (Ambiguous(c)) {
                    992:            fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
                    993:                        argv[1]);
                    994:            return 0;
1.8       robin     995:        } else if (!connected && c->needconnect) {
                    996:            printf("?Need to be connected first.\n");
                    997:            printf("'send ?' for help\n");
                    998:            return 0;
1.1       deraadt   999:        }
1.8       robin    1000:
1.1       deraadt  1001:        if (c->variable) {
                   1002:            if ((argc == 2) || (strcmp("on", argv[2]) == 0))
                   1003:                *c->variable = 1;
                   1004:            else if (strcmp("off", argv[2]) == 0)
                   1005:                *c->variable = 0;
                   1006:            else {
                   1007:                printf("Format is 'set togglename [on|off]'\n'set ?' for help.\n");
                   1008:                return 0;
                   1009:            }
                   1010:            if (c->actionexplanation) {
                   1011:                printf("%s %s.\n", *c->variable? "Will" : "Won't",
                   1012:                                                        c->actionexplanation);
                   1013:            }
                   1014:        }
                   1015:        if (c->handler)
                   1016:            (*c->handler)(1);
                   1017:     } else if (argc != 3) {
                   1018:        printf("Format is 'set Name Value'\n'set ?' for help.\n");
                   1019:        return 0;
                   1020:     } else if (Ambiguous(ct)) {
                   1021:        fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
                   1022:                        argv[1]);
                   1023:        return 0;
                   1024:     } else if (ct->handler) {
                   1025:        (*ct->handler)(argv[2]);
                   1026:        printf("%s set to \"%s\".\n", ct->name, (char *)ct->charp);
                   1027:     } else {
                   1028:        if (strcmp("off", argv[2])) {
                   1029:            value = special(argv[2]);
                   1030:        } else {
                   1031:            value = _POSIX_VDISABLE;
                   1032:        }
                   1033:        *(ct->charp) = (cc_t)value;
                   1034:        printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
                   1035:     }
                   1036:     slc_check();
                   1037:     return 1;
                   1038: }
                   1039:
                   1040:     static int
                   1041: unsetcmd(argc, argv)
                   1042:     int  argc;
                   1043:     char *argv[];
                   1044: {
                   1045:     struct setlist *ct;
                   1046:     struct togglelist *c;
                   1047:     register char *name;
                   1048:
                   1049:     if (argc < 2) {
                   1050:        fprintf(stderr,
                   1051:            "Need an argument to 'unset' command.  'unset ?' for help.\n");
                   1052:        return 0;
                   1053:     }
                   1054:     if (isprefix(argv[1], "?") || isprefix(argv[1], "help")) {
                   1055:        for (ct = Setlist; ct->name; ct++)
                   1056:            printf("%-15s %s\n", ct->name, ct->help);
                   1057:        printf("\n");
                   1058:        settogglehelp(0);
                   1059:        printf("%-15s %s\n", "?", "display help information");
                   1060:        return 0;
                   1061:     }
                   1062:
                   1063:     argc--;
                   1064:     argv++;
                   1065:     while (argc--) {
                   1066:        name = *argv++;
                   1067:        ct = getset(name);
                   1068:        if (ct == 0) {
                   1069:            c = GETTOGGLE(name);
                   1070:            if (c == 0) {
                   1071:                fprintf(stderr, "'%s': unknown argument ('unset ?' for help).\n",
                   1072:                        name);
                   1073:                return 0;
                   1074:            } else if (Ambiguous(c)) {
                   1075:                fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
                   1076:                        name);
                   1077:                return 0;
                   1078:            }
                   1079:            if (c->variable) {
                   1080:                *c->variable = 0;
                   1081:                if (c->actionexplanation) {
                   1082:                    printf("%s %s.\n", *c->variable? "Will" : "Won't",
                   1083:                                                        c->actionexplanation);
                   1084:                }
                   1085:            }
                   1086:            if (c->handler)
                   1087:                (*c->handler)(0);
                   1088:        } else if (Ambiguous(ct)) {
                   1089:            fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
                   1090:                        name);
                   1091:            return 0;
                   1092:        } else if (ct->handler) {
                   1093:            (*ct->handler)(0);
                   1094:            printf("%s reset to \"%s\".\n", ct->name, (char *)ct->charp);
                   1095:        } else {
                   1096:            *(ct->charp) = _POSIX_VDISABLE;
                   1097:            printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
                   1098:        }
                   1099:     }
                   1100:     return 1;
                   1101: }
                   1102: 
                   1103: /*
                   1104:  * The following are the data structures and routines for the
                   1105:  * 'mode' command.
                   1106:  */
                   1107: #ifdef KLUDGELINEMODE
                   1108: extern int kludgelinemode;
                   1109:
                   1110:     static int
                   1111: dokludgemode()
                   1112: {
                   1113:     kludgelinemode = 1;
                   1114:     send_wont(TELOPT_LINEMODE, 1);
                   1115:     send_dont(TELOPT_SGA, 1);
                   1116:     send_dont(TELOPT_ECHO, 1);
                   1117: }
                   1118: #endif
                   1119:
                   1120:     static int
                   1121: dolinemode()
                   1122: {
                   1123: #ifdef KLUDGELINEMODE
                   1124:     if (kludgelinemode)
                   1125:        send_dont(TELOPT_SGA, 1);
                   1126: #endif
                   1127:     send_will(TELOPT_LINEMODE, 1);
                   1128:     send_dont(TELOPT_ECHO, 1);
                   1129:     return 1;
                   1130: }
                   1131:
                   1132:     static int
                   1133: docharmode()
                   1134: {
                   1135: #ifdef KLUDGELINEMODE
                   1136:     if (kludgelinemode)
                   1137:        send_do(TELOPT_SGA, 1);
                   1138:     else
                   1139: #endif
                   1140:     send_wont(TELOPT_LINEMODE, 1);
                   1141:     send_do(TELOPT_ECHO, 1);
                   1142:     return 1;
                   1143: }
                   1144:
                   1145:     static int
                   1146: dolmmode(bit, on)
                   1147:     int bit, on;
                   1148: {
                   1149:     unsigned char c;
                   1150:     extern int linemode;
                   1151:
                   1152:     if (my_want_state_is_wont(TELOPT_LINEMODE)) {
                   1153:        printf("?Need to have LINEMODE option enabled first.\n");
                   1154:        printf("'mode ?' for help.\n");
                   1155:        return 0;
                   1156:     }
                   1157:
                   1158:     if (on)
                   1159:        c = (linemode | bit);
                   1160:     else
                   1161:        c = (linemode & ~bit);
                   1162:     lm_mode(&c, 1, 1);
                   1163:     return 1;
                   1164: }
                   1165:
                   1166:     int
                   1167: setmode(bit)
                   1168: {
                   1169:     return dolmmode(bit, 1);
                   1170: }
                   1171:
                   1172:     int
                   1173: clearmode(bit)
                   1174: {
                   1175:     return dolmmode(bit, 0);
                   1176: }
                   1177:
                   1178: struct modelist {
                   1179:        char    *name;          /* command name */
                   1180:        char    *help;          /* help string */
                   1181:        int     (*handler)();   /* routine which executes command */
                   1182:        int     needconnect;    /* Do we need to be connected to execute? */
                   1183:        int     arg1;
                   1184: };
                   1185:
                   1186: extern int modehelp();
                   1187:
                   1188: static struct modelist ModeList[] = {
                   1189:     { "character", "Disable LINEMODE option",  docharmode, 1 },
                   1190: #ifdef KLUDGELINEMODE
                   1191:     { "",      "(or disable obsolete line-by-line mode)", 0 },
                   1192: #endif
                   1193:     { "line",  "Enable LINEMODE option",       dolinemode, 1 },
                   1194: #ifdef KLUDGELINEMODE
                   1195:     { "",      "(or enable obsolete line-by-line mode)", 0 },
                   1196: #endif
                   1197:     { "", "", 0 },
                   1198:     { "",      "These require the LINEMODE option to be enabled", 0 },
                   1199:     { "isig",  "Enable signal trapping",       setmode, 1, MODE_TRAPSIG },
                   1200:     { "+isig", 0,                              setmode, 1, MODE_TRAPSIG },
                   1201:     { "-isig", "Disable signal trapping",      clearmode, 1, MODE_TRAPSIG },
                   1202:     { "edit",  "Enable character editing",     setmode, 1, MODE_EDIT },
                   1203:     { "+edit", 0,                              setmode, 1, MODE_EDIT },
                   1204:     { "-edit", "Disable character editing",    clearmode, 1, MODE_EDIT },
                   1205:     { "softtabs", "Enable tab expansion",      setmode, 1, MODE_SOFT_TAB },
                   1206:     { "+softtabs", 0,                          setmode, 1, MODE_SOFT_TAB },
                   1207:     { "-softtabs", "Disable character editing",        clearmode, 1, MODE_SOFT_TAB },
                   1208:     { "litecho", "Enable literal character echo", setmode, 1, MODE_LIT_ECHO },
                   1209:     { "+litecho", 0,                           setmode, 1, MODE_LIT_ECHO },
                   1210:     { "-litecho", "Disable literal character echo", clearmode, 1, MODE_LIT_ECHO },
                   1211:     { "help",  0,                              modehelp, 0 },
                   1212: #ifdef KLUDGELINEMODE
                   1213:     { "kludgeline", 0,                         dokludgemode, 1 },
                   1214: #endif
                   1215:     { "", "", 0 },
                   1216:     { "?",     "Print help information",       modehelp, 0 },
                   1217:     { 0 },
                   1218: };
                   1219:
                   1220:
                   1221:     int
                   1222: modehelp()
                   1223: {
                   1224:     struct modelist *mt;
                   1225:
                   1226:     printf("format is:  'mode Mode', where 'Mode' is one of:\n\n");
                   1227:     for (mt = ModeList; mt->name; mt++) {
                   1228:        if (mt->help) {
                   1229:            if (*mt->help)
                   1230:                printf("%-15s %s\n", mt->name, mt->help);
                   1231:            else
                   1232:                printf("\n");
                   1233:        }
                   1234:     }
                   1235:     return 0;
                   1236: }
                   1237:
                   1238: #define        GETMODECMD(name) (struct modelist *) \
                   1239:                genget(name, (char **) ModeList, sizeof(struct modelist))
                   1240:
                   1241:     static int
                   1242: modecmd(argc, argv)
                   1243:     int  argc;
                   1244:     char *argv[];
                   1245: {
                   1246:     struct modelist *mt;
                   1247:
                   1248:     if (argc != 2) {
                   1249:        printf("'mode' command requires an argument\n");
                   1250:        printf("'mode ?' for help.\n");
                   1251:     } else if ((mt = GETMODECMD(argv[1])) == 0) {
                   1252:        fprintf(stderr, "Unknown mode '%s' ('mode ?' for help).\n", argv[1]);
                   1253:     } else if (Ambiguous(mt)) {
                   1254:        fprintf(stderr, "Ambiguous mode '%s' ('mode ?' for help).\n", argv[1]);
                   1255:     } else if (mt->needconnect && !connected) {
                   1256:        printf("?Need to be connected first.\n");
                   1257:        printf("'mode ?' for help.\n");
                   1258:     } else if (mt->handler) {
                   1259:        return (*mt->handler)(mt->arg1);
                   1260:     }
                   1261:     return 0;
                   1262: }
                   1263: 
                   1264: /*
                   1265:  * The following data structures and routines implement the
                   1266:  * "display" command.
                   1267:  */
                   1268:
                   1269:     static int
                   1270: display(argc, argv)
                   1271:     int  argc;
                   1272:     char *argv[];
                   1273: {
                   1274:     struct togglelist *tl;
                   1275:     struct setlist *sl;
                   1276:
                   1277: #define        dotog(tl)       if (tl->variable && tl->actionexplanation) { \
                   1278:                            if (*tl->variable) { \
                   1279:                                printf("will"); \
                   1280:                            } else { \
                   1281:                                printf("won't"); \
                   1282:                            } \
                   1283:                            printf(" %s.\n", tl->actionexplanation); \
                   1284:                        }
                   1285:
                   1286: #define        doset(sl)   if (sl->name && *sl->name != ' ') { \
                   1287:                        if (sl->handler == 0) \
                   1288:                            printf("%-15s [%s]\n", sl->name, control(*sl->charp)); \
                   1289:                        else \
                   1290:                            printf("%-15s \"%s\"\n", sl->name, (char *)sl->charp); \
                   1291:                    }
                   1292:
                   1293:     if (argc == 1) {
                   1294:        for (tl = Togglelist; tl->name; tl++) {
                   1295:            dotog(tl);
                   1296:        }
                   1297:        printf("\n");
                   1298:        for (sl = Setlist; sl->name; sl++) {
                   1299:            doset(sl);
                   1300:        }
                   1301:     } else {
                   1302:        int i;
                   1303:
                   1304:        for (i = 1; i < argc; i++) {
                   1305:            sl = getset(argv[i]);
                   1306:            tl = GETTOGGLE(argv[i]);
                   1307:            if (Ambiguous(sl) || Ambiguous(tl)) {
                   1308:                printf("?Ambiguous argument '%s'.\n", argv[i]);
                   1309:                return 0;
                   1310:            } else if (!sl && !tl) {
                   1311:                printf("?Unknown argument '%s'.\n", argv[i]);
                   1312:                return 0;
                   1313:            } else {
                   1314:                if (tl) {
                   1315:                    dotog(tl);
                   1316:                }
                   1317:                if (sl) {
                   1318:                    doset(sl);
                   1319:                }
                   1320:            }
                   1321:        }
                   1322:     }
                   1323: /*@*/optionstatus();
                   1324:     return 1;
                   1325: #undef doset
                   1326: #undef dotog
                   1327: }
                   1328: 
                   1329: /*
                   1330:  * The following are the data structures, and many of the routines,
                   1331:  * relating to command processing.
                   1332:  */
                   1333:
                   1334: /*
                   1335:  * Set the escape character.
                   1336:  */
                   1337:        static int
                   1338: setescape(argc, argv)
                   1339:        int argc;
                   1340:        char *argv[];
                   1341: {
                   1342:        register char *arg;
                   1343:        char buf[50];
                   1344:
                   1345:        printf(
                   1346:            "Deprecated usage - please use 'set escape%s%s' in the future.\n",
                   1347:                                (argc > 2)? " ":"", (argc > 2)? argv[1]: "");
                   1348:        if (argc > 2)
                   1349:                arg = argv[1];
                   1350:        else {
                   1351:                printf("new escape character: ");
                   1352:                (void) fgets(buf, sizeof(buf), stdin);
                   1353:                arg = buf;
                   1354:        }
                   1355:        if (arg[0] != '\0')
                   1356:                escape = arg[0];
                   1357:        if (!In3270) {
                   1358:                printf("Escape character is '%s'.\n", control(escape));
                   1359:        }
                   1360:        (void) fflush(stdout);
                   1361:        return 1;
                   1362: }
                   1363:
                   1364:     /*VARARGS*/
                   1365:     static int
                   1366: togcrmod()
                   1367: {
                   1368:     crmod = !crmod;
                   1369:     printf("Deprecated usage - please use 'toggle crmod' in the future.\n");
                   1370:     printf("%s map carriage return on output.\n", crmod ? "Will" : "Won't");
                   1371:     (void) fflush(stdout);
                   1372:     return 1;
                   1373: }
                   1374:
                   1375:     /*VARARGS*/
                   1376:     int
                   1377: suspend()
                   1378: {
                   1379: #ifdef SIGTSTP
                   1380:     setcommandmode();
                   1381:     {
                   1382:        long oldrows, oldcols, newrows, newcols, err;
                   1383:
                   1384:        err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
                   1385:        (void) kill(0, SIGTSTP);
                   1386:        /*
                   1387:         * If we didn't get the window size before the SUSPEND, but we
1.3       niklas   1388:         * can get them now (?), then send the NAWS to make sure that
1.1       deraadt  1389:         * we are set up for the right window size.
                   1390:         */
                   1391:        if (TerminalWindowSize(&newrows, &newcols) && connected &&
                   1392:            (err || ((oldrows != newrows) || (oldcols != newcols)))) {
                   1393:                sendnaws();
                   1394:        }
                   1395:     }
                   1396:     /* reget parameters in case they were changed */
                   1397:     TerminalSaveState();
                   1398:     setconnmode(0);
                   1399: #else
                   1400:     printf("Suspend is not supported.  Try the '!' command instead\n");
                   1401: #endif
                   1402:     return 1;
                   1403: }
                   1404:
                   1405: #if    !defined(TN3270)
                   1406:     /*ARGSUSED*/
                   1407:     int
                   1408: shell(argc, argv)
                   1409:     int argc;
                   1410:     char *argv[];
                   1411: {
                   1412:     long oldrows, oldcols, newrows, newcols, err;
                   1413:
                   1414:     setcommandmode();
                   1415:
                   1416:     err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
                   1417:     switch(vfork()) {
                   1418:     case -1:
                   1419:        perror("Fork failed\n");
                   1420:        break;
                   1421:
                   1422:     case 0:
                   1423:        {
                   1424:            /*
                   1425:             * Fire up the shell in the child.
                   1426:             */
                   1427:            register char *shellp, *shellname;
1.3       niklas   1428:            extern char *strrchr();
1.1       deraadt  1429:
                   1430:            shellp = getenv("SHELL");
                   1431:            if (shellp == NULL)
                   1432:                shellp = "/bin/sh";
1.3       niklas   1433:            if ((shellname = strrchr(shellp, '/')) == 0)
1.1       deraadt  1434:                shellname = shellp;
                   1435:            else
                   1436:                shellname++;
                   1437:            if (argc > 1)
                   1438:                execl(shellp, shellname, "-c", &saveline[1], 0);
                   1439:            else
                   1440:                execl(shellp, shellname, 0);
                   1441:            perror("Execl");
                   1442:            _exit(1);
                   1443:        }
                   1444:     default:
                   1445:            (void)wait((int *)0);       /* Wait for the shell to complete */
                   1446:
                   1447:            if (TerminalWindowSize(&newrows, &newcols) && connected &&
                   1448:                (err || ((oldrows != newrows) || (oldcols != newcols)))) {
                   1449:                    sendnaws();
                   1450:            }
                   1451:            break;
                   1452:     }
                   1453:     return 1;
                   1454: }
                   1455: #else  /* !defined(TN3270) */
                   1456: extern int shell();
                   1457: #endif /* !defined(TN3270) */
                   1458:
                   1459:     /*VARARGS*/
                   1460:     static
                   1461: bye(argc, argv)
                   1462:     int  argc;         /* Number of arguments */
                   1463:     char *argv[];      /* arguments */
                   1464: {
                   1465:     extern int resettermname;
                   1466:
                   1467:     if (connected) {
                   1468:        (void) shutdown(net, 2);
                   1469:        printf("Connection closed.\n");
                   1470:        (void) NetClose(net);
                   1471:        connected = 0;
                   1472:        resettermname = 1;
                   1473: #if    defined(AUTHENTICATION)
                   1474:        auth_encrypt_connect(connected);
                   1475: #endif /* defined(AUTHENTICATION) */
                   1476:        /* reset options */
                   1477:        tninit();
                   1478: #if    defined(TN3270)
                   1479:        SetIn3270();            /* Get out of 3270 mode */
                   1480: #endif /* defined(TN3270) */
                   1481:     }
                   1482:     if ((argc != 2) || (strcmp(argv[1], "fromquit") != 0)) {
                   1483:        longjmp(toplevel, 1);
                   1484:        /* NOTREACHED */
                   1485:     }
                   1486:     return 1;                  /* Keep lint, etc., happy */
                   1487: }
                   1488:
                   1489: /*VARARGS*/
                   1490: quit()
                   1491: {
                   1492:        (void) call(bye, "bye", "fromquit", 0);
                   1493:        Exit(0);
                   1494:        /*NOTREACHED*/
                   1495: }
                   1496:
                   1497: /*VARARGS*/
                   1498:        int
                   1499: logout()
                   1500: {
                   1501:        send_do(TELOPT_LOGOUT, 1);
                   1502:        (void) netflush();
                   1503:        return 1;
                   1504: }
                   1505:
                   1506: 
                   1507: /*
                   1508:  * The SLC command.
                   1509:  */
                   1510:
                   1511: struct slclist {
                   1512:        char    *name;
                   1513:        char    *help;
                   1514:        void    (*handler)();
                   1515:        int     arg;
                   1516: };
                   1517:
                   1518: static void slc_help();
                   1519:
                   1520: struct slclist SlcList[] = {
                   1521:     { "export",        "Use local special character definitions",
                   1522:                                                slc_mode_export,        0 },
                   1523:     { "import",        "Use remote special character definitions",
                   1524:                                                slc_mode_import,        1 },
                   1525:     { "check", "Verify remote special character definitions",
                   1526:                                                slc_mode_import,        0 },
                   1527:     { "help",  0,                              slc_help,               0 },
                   1528:     { "?",     "Print help information",       slc_help,               0 },
                   1529:     { 0 },
                   1530: };
                   1531:
                   1532:     static void
                   1533: slc_help()
                   1534: {
                   1535:     struct slclist *c;
                   1536:
                   1537:     for (c = SlcList; c->name; c++) {
                   1538:        if (c->help) {
                   1539:            if (*c->help)
                   1540:                printf("%-15s %s\n", c->name, c->help);
                   1541:            else
                   1542:                printf("\n");
                   1543:        }
                   1544:     }
                   1545: }
                   1546:
                   1547:     static struct slclist *
                   1548: getslc(name)
                   1549:     char *name;
                   1550: {
                   1551:     return (struct slclist *)
                   1552:                genget(name, (char **) SlcList, sizeof(struct slclist));
                   1553: }
                   1554:
                   1555:     static
                   1556: slccmd(argc, argv)
                   1557:     int  argc;
                   1558:     char *argv[];
                   1559: {
                   1560:     struct slclist *c;
                   1561:
                   1562:     if (argc != 2) {
                   1563:        fprintf(stderr,
                   1564:            "Need an argument to 'slc' command.  'slc ?' for help.\n");
                   1565:        return 0;
                   1566:     }
                   1567:     c = getslc(argv[1]);
                   1568:     if (c == 0) {
1.3       niklas   1569:        fprintf(stderr, "'%s': unknown argument ('slc ?' for help).\n",
1.1       deraadt  1570:                                argv[1]);
1.3       niklas   1571:        return 0;
1.1       deraadt  1572:     }
                   1573:     if (Ambiguous(c)) {
1.3       niklas   1574:        fprintf(stderr, "'%s': ambiguous argument ('slc ?' for help).\n",
1.1       deraadt  1575:                                argv[1]);
1.3       niklas   1576:        return 0;
1.1       deraadt  1577:     }
                   1578:     (*c->handler)(c->arg);
                   1579:     slcstate();
                   1580:     return 1;
                   1581: }
                   1582: 
                   1583: /*
                   1584:  * The ENVIRON command.
                   1585:  */
                   1586:
                   1587: struct envlist {
                   1588:        char    *name;
                   1589:        char    *help;
                   1590:        void    (*handler)();
                   1591:        int     narg;
                   1592: };
                   1593:
                   1594: extern struct env_lst *
                   1595:        env_define P((unsigned char *, unsigned char *));
                   1596: extern void
                   1597:        env_undefine P((unsigned char *)),
                   1598:        env_export P((unsigned char *)),
                   1599:        env_unexport P((unsigned char *)),
                   1600:        env_send P((unsigned char *)),
                   1601: #if defined(OLD_ENVIRON) && defined(ENV_HACK)
                   1602:        env_varval P((unsigned char *)),
                   1603: #endif
                   1604:        env_list P((void));
                   1605: static void
                   1606:        env_help P((void));
                   1607:
                   1608: struct envlist EnvList[] = {
                   1609:     { "define",        "Define an environment variable",
                   1610:                                                (void (*)())env_define, 2 },
                   1611:     { "undefine", "Undefine an environment variable",
                   1612:                                                env_undefine,   1 },
                   1613:     { "export",        "Mark an environment variable for automatic export",
                   1614:                                                env_export,     1 },
                   1615:     { "unexport", "Don't mark an environment variable for automatic export",
                   1616:                                                env_unexport,   1 },
                   1617:     { "send",  "Send an environment variable", env_send,       1 },
                   1618:     { "list",  "List the current environment variables",
                   1619:                                                env_list,       0 },
                   1620: #if defined(OLD_ENVIRON) && defined(ENV_HACK)
                   1621:     { "varval", "Reverse VAR and VALUE (auto, right, wrong, status)",
                   1622:                                                env_varval,    1 },
                   1623: #endif
                   1624:     { "help",  0,                              env_help,               0 },
                   1625:     { "?",     "Print help information",       env_help,               0 },
                   1626:     { 0 },
                   1627: };
                   1628:
                   1629:     static void
                   1630: env_help()
                   1631: {
                   1632:     struct envlist *c;
                   1633:
                   1634:     for (c = EnvList; c->name; c++) {
                   1635:        if (c->help) {
                   1636:            if (*c->help)
                   1637:                printf("%-15s %s\n", c->name, c->help);
                   1638:            else
                   1639:                printf("\n");
                   1640:        }
                   1641:     }
                   1642: }
                   1643:
                   1644:     static struct envlist *
                   1645: getenvcmd(name)
                   1646:     char *name;
                   1647: {
                   1648:     return (struct envlist *)
                   1649:                genget(name, (char **) EnvList, sizeof(struct envlist));
                   1650: }
                   1651:
                   1652: env_cmd(argc, argv)
                   1653:     int  argc;
                   1654:     char *argv[];
                   1655: {
                   1656:     struct envlist *c;
                   1657:
                   1658:     if (argc < 2) {
                   1659:        fprintf(stderr,
                   1660:            "Need an argument to 'environ' command.  'environ ?' for help.\n");
                   1661:        return 0;
                   1662:     }
                   1663:     c = getenvcmd(argv[1]);
                   1664:     if (c == 0) {
1.3       niklas   1665:        fprintf(stderr, "'%s': unknown argument ('environ ?' for help).\n",
1.1       deraadt  1666:                                argv[1]);
1.3       niklas   1667:        return 0;
1.1       deraadt  1668:     }
                   1669:     if (Ambiguous(c)) {
1.3       niklas   1670:        fprintf(stderr, "'%s': ambiguous argument ('environ ?' for help).\n",
1.1       deraadt  1671:                                argv[1]);
1.3       niklas   1672:        return 0;
1.1       deraadt  1673:     }
                   1674:     if (c->narg + 2 != argc) {
                   1675:        fprintf(stderr,
                   1676:            "Need %s%d argument%s to 'environ %s' command.  'environ ?' for help.\n",
                   1677:                c->narg < argc + 2 ? "only " : "",
                   1678:                c->narg, c->narg == 1 ? "" : "s", c->name);
                   1679:        return 0;
                   1680:     }
                   1681:     (*c->handler)(argv[2], argv[3]);
                   1682:     return 1;
                   1683: }
                   1684:
                   1685: struct env_lst {
                   1686:        struct env_lst *next;   /* pointer to next structure */
                   1687:        struct env_lst *prev;   /* pointer to previous structure */
                   1688:        unsigned char *var;     /* pointer to variable name */
                   1689:        unsigned char *value;   /* pointer to variable value */
                   1690:        int export;             /* 1 -> export with default list of variables */
                   1691:        int welldefined;        /* A well defined variable */
                   1692: };
                   1693:
                   1694: struct env_lst envlisthead;
                   1695:
                   1696:        struct env_lst *
                   1697: env_find(var)
                   1698:        unsigned char *var;
                   1699: {
                   1700:        register struct env_lst *ep;
                   1701:
                   1702:        for (ep = envlisthead.next; ep; ep = ep->next) {
                   1703:                if (strcmp((char *)ep->var, (char *)var) == 0)
                   1704:                        return(ep);
                   1705:        }
                   1706:        return(NULL);
                   1707: }
                   1708:
                   1709:        void
                   1710: env_init()
                   1711: {
                   1712:        extern char **environ;
                   1713:        register char **epp, *cp;
                   1714:        register struct env_lst *ep;
1.3       niklas   1715:        extern char *strchr();
1.1       deraadt  1716:
                   1717:        for (epp = environ; *epp; epp++) {
1.3       niklas   1718:                if (cp = strchr(*epp, '=')) {
1.1       deraadt  1719:                        *cp = '\0';
                   1720:                        ep = env_define((unsigned char *)*epp,
                   1721:                                        (unsigned char *)cp+1);
                   1722:                        ep->export = 0;
                   1723:                        *cp = '=';
                   1724:                }
                   1725:        }
                   1726:        /*
                   1727:         * Special case for DISPLAY variable.  If it is ":0.0" or
                   1728:         * "unix:0.0", we have to get rid of "unix" and insert our
                   1729:         * hostname.
                   1730:         */
                   1731:        if ((ep = env_find("DISPLAY"))
                   1732:            && ((*ep->value == ':')
1.3       niklas   1733:                || (strncmp((char *)ep->value, "unix:", 5) == 0))) {
1.1       deraadt  1734:                char hbuf[256+1];
1.3       niklas   1735:                char *cp2 = strchr((char *)ep->value, ':');
1.1       deraadt  1736:
                   1737:                gethostname(hbuf, 256);
                   1738:                hbuf[256] = '\0';
                   1739:                cp = (char *)malloc(strlen(hbuf) + strlen(cp2) + 1);
                   1740:                sprintf((char *)cp, "%s%s", hbuf, cp2);
                   1741:                free(ep->value);
                   1742:                ep->value = (unsigned char *)cp;
                   1743:        }
                   1744:        /*
                   1745:         * If USER is not defined, but LOGNAME is, then add
                   1746:         * USER with the value from LOGNAME.  By default, we
                   1747:         * don't export the USER variable.
                   1748:         */
                   1749:        if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME"))) {
                   1750:                env_define((unsigned char *)"USER", ep->value);
                   1751:                env_unexport((unsigned char *)"USER");
                   1752:        }
                   1753:        env_export((unsigned char *)"DISPLAY");
                   1754:        env_export((unsigned char *)"PRINTER");
                   1755: }
                   1756:
                   1757:        struct env_lst *
                   1758: env_define(var, value)
                   1759:        unsigned char *var, *value;
                   1760: {
                   1761:        register struct env_lst *ep;
                   1762:
                   1763:        if (ep = env_find(var)) {
                   1764:                if (ep->var)
                   1765:                        free(ep->var);
                   1766:                if (ep->value)
                   1767:                        free(ep->value);
                   1768:        } else {
                   1769:                ep = (struct env_lst *)malloc(sizeof(struct env_lst));
                   1770:                ep->next = envlisthead.next;
                   1771:                envlisthead.next = ep;
                   1772:                ep->prev = &envlisthead;
                   1773:                if (ep->next)
                   1774:                        ep->next->prev = ep;
                   1775:        }
                   1776:        ep->welldefined = opt_welldefined(var);
                   1777:        ep->export = 1;
                   1778:        ep->var = (unsigned char *)strdup((char *)var);
                   1779:        ep->value = (unsigned char *)strdup((char *)value);
                   1780:        return(ep);
                   1781: }
                   1782:
                   1783:        void
                   1784: env_undefine(var)
                   1785:        unsigned char *var;
                   1786: {
                   1787:        register struct env_lst *ep;
                   1788:
                   1789:        if (ep = env_find(var)) {
                   1790:                ep->prev->next = ep->next;
                   1791:                if (ep->next)
                   1792:                        ep->next->prev = ep->prev;
                   1793:                if (ep->var)
                   1794:                        free(ep->var);
                   1795:                if (ep->value)
                   1796:                        free(ep->value);
                   1797:                free(ep);
                   1798:        }
                   1799: }
                   1800:
                   1801:        void
                   1802: env_export(var)
                   1803:        unsigned char *var;
                   1804: {
                   1805:        register struct env_lst *ep;
                   1806:
                   1807:        if (ep = env_find(var))
                   1808:                ep->export = 1;
                   1809: }
                   1810:
                   1811:        void
                   1812: env_unexport(var)
                   1813:        unsigned char *var;
                   1814: {
                   1815:        register struct env_lst *ep;
                   1816:
                   1817:        if (ep = env_find(var))
                   1818:                ep->export = 0;
                   1819: }
                   1820:
                   1821:        void
                   1822: env_send(var)
                   1823:        unsigned char *var;
                   1824: {
                   1825:        register struct env_lst *ep;
                   1826:
1.3       niklas   1827:        if (my_state_is_wont(TELOPT_NEW_ENVIRON)
1.1       deraadt  1828: #ifdef OLD_ENVIRON
                   1829:            && my_state_is_wont(TELOPT_OLD_ENVIRON)
                   1830: #endif
                   1831:                ) {
                   1832:                fprintf(stderr,
                   1833:                    "Cannot send '%s': Telnet ENVIRON option not enabled\n",
                   1834:                                                                        var);
                   1835:                return;
                   1836:        }
                   1837:        ep = env_find(var);
                   1838:        if (ep == 0) {
                   1839:                fprintf(stderr, "Cannot send '%s': variable not defined\n",
                   1840:                                                                        var);
                   1841:                return;
                   1842:        }
                   1843:        env_opt_start_info();
                   1844:        env_opt_add(ep->var);
                   1845:        env_opt_end(0);
                   1846: }
                   1847:
                   1848:        void
                   1849: env_list()
                   1850: {
                   1851:        register struct env_lst *ep;
                   1852:
                   1853:        for (ep = envlisthead.next; ep; ep = ep->next) {
                   1854:                printf("%c %-20s %s\n", ep->export ? '*' : ' ',
                   1855:                                        ep->var, ep->value);
                   1856:        }
                   1857: }
                   1858:
                   1859:        unsigned char *
                   1860: env_default(init, welldefined)
                   1861:        int init;
                   1862: {
                   1863:        static struct env_lst *nep = NULL;
                   1864:
                   1865:        if (init) {
                   1866:                nep = &envlisthead;
                   1867:                return;
                   1868:        }
                   1869:        if (nep) {
                   1870:                while (nep = nep->next) {
                   1871:                        if (nep->export && (nep->welldefined == welldefined))
                   1872:                                return(nep->var);
                   1873:                }
                   1874:        }
                   1875:        return(NULL);
                   1876: }
                   1877:
                   1878:        unsigned char *
                   1879: env_getvalue(var)
                   1880:        unsigned char *var;
                   1881: {
                   1882:        register struct env_lst *ep;
                   1883:
                   1884:        if (ep = env_find(var))
                   1885:                return(ep->value);
                   1886:        return(NULL);
                   1887: }
                   1888:
                   1889: #if defined(OLD_ENVIRON) && defined(ENV_HACK)
                   1890:        void
                   1891: env_varval(what)
                   1892:        unsigned char *what;
                   1893: {
                   1894:        extern int old_env_var, old_env_value, env_auto;
                   1895:        int len = strlen((char *)what);
                   1896:
                   1897:        if (len == 0)
                   1898:                goto unknown;
                   1899:
                   1900:        if (strncasecmp((char *)what, "status", len) == 0) {
                   1901:                if (env_auto)
                   1902:                        printf("%s%s", "VAR and VALUE are/will be ",
                   1903:                                        "determined automatically\n");
                   1904:                if (old_env_var == OLD_ENV_VAR)
                   1905:                        printf("VAR and VALUE set to correct definitions\n");
                   1906:                else
                   1907:                        printf("VAR and VALUE definitions are reversed\n");
                   1908:        } else if (strncasecmp((char *)what, "auto", len) == 0) {
                   1909:                env_auto = 1;
                   1910:                old_env_var = OLD_ENV_VALUE;
                   1911:                old_env_value = OLD_ENV_VAR;
                   1912:        } else if (strncasecmp((char *)what, "right", len) == 0) {
                   1913:                env_auto = 0;
                   1914:                old_env_var = OLD_ENV_VAR;
                   1915:                old_env_value = OLD_ENV_VALUE;
                   1916:        } else if (strncasecmp((char *)what, "wrong", len) == 0) {
                   1917:                env_auto = 0;
                   1918:                old_env_var = OLD_ENV_VALUE;
                   1919:                old_env_value = OLD_ENV_VAR;
                   1920:        } else {
                   1921: unknown:
                   1922:                printf("Unknown \"varval\" command. (\"auto\", \"right\", \"wrong\", \"status\")\n");
                   1923:        }
                   1924: }
                   1925: #endif
                   1926:
                   1927: #if    defined(AUTHENTICATION)
                   1928: /*
                   1929:  * The AUTHENTICATE command.
                   1930:  */
                   1931:
                   1932: struct authlist {
                   1933:        char    *name;
                   1934:        char    *help;
                   1935:        int     (*handler)();
                   1936:        int     narg;
                   1937: };
                   1938:
                   1939: extern int
1.3       niklas   1940:        auth_enable P((char *)),
                   1941:        auth_disable P((char *)),
1.1       deraadt  1942:        auth_status P((void));
                   1943: static int
                   1944:        auth_help P((void));
                   1945:
                   1946: struct authlist AuthList[] = {
                   1947:     { "status",        "Display current status of authentication information",
                   1948:                                                auth_status,    0 },
                   1949:     { "disable", "Disable an authentication type ('auth disable ?' for more)",
                   1950:                                                auth_disable,   1 },
                   1951:     { "enable", "Enable an authentication type ('auth enable ?' for more)",
                   1952:                                                auth_enable,    1 },
                   1953:     { "help",  0,                              auth_help,              0 },
                   1954:     { "?",     "Print help information",       auth_help,              0 },
                   1955:     { 0 },
                   1956: };
                   1957:
                   1958:     static int
                   1959: auth_help()
                   1960: {
                   1961:     struct authlist *c;
                   1962:
                   1963:     for (c = AuthList; c->name; c++) {
                   1964:        if (c->help) {
                   1965:            if (*c->help)
                   1966:                printf("%-15s %s\n", c->name, c->help);
                   1967:            else
                   1968:                printf("\n");
                   1969:        }
                   1970:     }
                   1971:     return 0;
                   1972: }
                   1973:
                   1974: auth_cmd(argc, argv)
                   1975:     int  argc;
                   1976:     char *argv[];
                   1977: {
                   1978:     struct authlist *c;
                   1979:
1.3       niklas   1980:     if (argc < 2) {
                   1981:        fprintf(stderr,
                   1982:            "Need an argument to 'auth' command.  'auth ?' for help.\n");
                   1983:        return 0;
                   1984:     }
                   1985:
1.1       deraadt  1986:     c = (struct authlist *)
                   1987:                genget(argv[1], (char **) AuthList, sizeof(struct authlist));
                   1988:     if (c == 0) {
1.3       niklas   1989:        fprintf(stderr, "'%s': unknown argument ('auth ?' for help).\n",
1.1       deraadt  1990:                                argv[1]);
1.3       niklas   1991:        return 0;
1.1       deraadt  1992:     }
                   1993:     if (Ambiguous(c)) {
1.3       niklas   1994:        fprintf(stderr, "'%s': ambiguous argument ('auth ?' for help).\n",
1.1       deraadt  1995:                                argv[1]);
1.3       niklas   1996:        return 0;
1.1       deraadt  1997:     }
                   1998:     if (c->narg + 2 != argc) {
                   1999:        fprintf(stderr,
                   2000:            "Need %s%d argument%s to 'auth %s' command.  'auth ?' for help.\n",
                   2001:                c->narg < argc + 2 ? "only " : "",
                   2002:                c->narg, c->narg == 1 ? "" : "s", c->name);
                   2003:        return 0;
                   2004:     }
                   2005:     return((*c->handler)(argv[2], argv[3]));
                   2006: }
                   2007: #endif
                   2008:
                   2009:
                   2010: #if    defined(unix) && defined(TN3270)
                   2011:     static void
                   2012: filestuff(fd)
                   2013:     int fd;
                   2014: {
                   2015:     int res;
                   2016:
                   2017: #ifdef F_GETOWN
                   2018:     setconnmode(0);
                   2019:     res = fcntl(fd, F_GETOWN, 0);
                   2020:     setcommandmode();
                   2021:
                   2022:     if (res == -1) {
                   2023:        perror("fcntl");
                   2024:        return;
                   2025:     }
                   2026:     printf("\tOwner is %d.\n", res);
                   2027: #endif
                   2028:
                   2029:     setconnmode(0);
                   2030:     res = fcntl(fd, F_GETFL, 0);
                   2031:     setcommandmode();
                   2032:
                   2033:     if (res == -1) {
                   2034:        perror("fcntl");
                   2035:        return;
                   2036:     }
                   2037: #ifdef notdef
                   2038:     printf("\tFlags are 0x%x: %s\n", res, decodeflags(res));
                   2039: #endif
                   2040: }
                   2041: #endif /* defined(unix) && defined(TN3270) */
                   2042:
                   2043: /*
                   2044:  * Print status about the connection.
                   2045:  */
                   2046:     /*ARGSUSED*/
                   2047:     static
                   2048: status(argc, argv)
                   2049:     int         argc;
                   2050:     char *argv[];
                   2051: {
                   2052:     if (connected) {
                   2053:        printf("Connected to %s.\n", hostname);
                   2054:        if ((argc < 2) || strcmp(argv[1], "notmuch")) {
                   2055:            int mode = getconnmode();
                   2056:
                   2057:            if (my_want_state_is_will(TELOPT_LINEMODE)) {
                   2058:                printf("Operating with LINEMODE option\n");
                   2059:                printf("%s line editing\n", (mode&MODE_EDIT) ? "Local" : "No");
                   2060:                printf("%s catching of signals\n",
                   2061:                                        (mode&MODE_TRAPSIG) ? "Local" : "No");
                   2062:                slcstate();
                   2063: #ifdef KLUDGELINEMODE
                   2064:            } else if (kludgelinemode && my_want_state_is_dont(TELOPT_SGA)) {
                   2065:                printf("Operating in obsolete linemode\n");
                   2066: #endif
                   2067:            } else {
                   2068:                printf("Operating in single character mode\n");
                   2069:                if (localchars)
                   2070:                    printf("Catching signals locally\n");
                   2071:            }
                   2072:            printf("%s character echo\n", (mode&MODE_ECHO) ? "Local" : "Remote");
                   2073:            if (my_want_state_is_will(TELOPT_LFLOW))
                   2074:                printf("%s flow control\n", (mode&MODE_FLOW) ? "Local" : "No");
                   2075:        }
                   2076:     } else {
                   2077:        printf("No connection.\n");
                   2078:     }
                   2079: #   if !defined(TN3270)
                   2080:     printf("Escape character is '%s'.\n", control(escape));
                   2081:     (void) fflush(stdout);
                   2082: #   else /* !defined(TN3270) */
                   2083:     if ((!In3270) && ((argc < 2) || strcmp(argv[1], "notmuch"))) {
                   2084:        printf("Escape character is '%s'.\n", control(escape));
                   2085:     }
                   2086: #   if defined(unix)
                   2087:     if ((argc >= 2) && !strcmp(argv[1], "everything")) {
                   2088:        printf("SIGIO received %d time%s.\n",
                   2089:                                sigiocount, (sigiocount == 1)? "":"s");
                   2090:        if (In3270) {
                   2091:            printf("Process ID %d, process group %d.\n",
                   2092:                                            getpid(), getpgrp(getpid()));
                   2093:            printf("Terminal input:\n");
                   2094:            filestuff(tin);
                   2095:            printf("Terminal output:\n");
                   2096:            filestuff(tout);
                   2097:            printf("Network socket:\n");
                   2098:            filestuff(net);
                   2099:        }
                   2100:     }
                   2101:     if (In3270 && transcom) {
                   2102:        printf("Transparent mode command is '%s'.\n", transcom);
                   2103:     }
                   2104: #   endif /* defined(unix) */
                   2105:     (void) fflush(stdout);
                   2106:     if (In3270) {
                   2107:        return 0;
                   2108:     }
                   2109: #   endif /* defined(TN3270) */
                   2110:     return 1;
                   2111: }
                   2112:
                   2113: #ifdef SIGINFO
                   2114: /*
                   2115:  * Function that gets called when SIGINFO is received.
                   2116:  */
                   2117: ayt_status()
                   2118: {
                   2119:     (void) call(status, "status", "notmuch", 0);
                   2120: }
                   2121: #endif
                   2122:
                   2123: unsigned long inet_addr();
                   2124:
                   2125:     int
                   2126: tn(argc, argv)
                   2127:     int argc;
                   2128:     char *argv[];
                   2129: {
1.5       niklas   2130:     register struct hostent *host = 0, *alias = 0;
1.1       deraadt  2131:     struct sockaddr_in sin;
1.5       niklas   2132:     struct sockaddr_in ladr;
1.1       deraadt  2133:     struct servent *sp = 0;
                   2134:     unsigned long temp;
                   2135:     extern char *inet_ntoa();
                   2136: #if    defined(IP_OPTIONS) && defined(IPPROTO_IP)
                   2137:     char *srp = 0, *strrchr();
                   2138:     unsigned long sourceroute(), srlen;
                   2139: #endif
1.5       niklas   2140:     char *cmd, *hostp = 0, *portp = 0, *user = 0, *aliasp = 0;
1.1       deraadt  2141:
                   2142:     /* clear the socket address prior to use */
1.3       niklas   2143:     memset((char *)&sin, 0, sizeof(sin));
1.1       deraadt  2144:
                   2145:     if (connected) {
                   2146:        printf("?Already connected to %s\n", hostname);
1.9     ! tholo    2147:        seteuid(getuid());
1.1       deraadt  2148:        setuid(getuid());
                   2149:        return 0;
                   2150:     }
                   2151:     if (argc < 2) {
                   2152:        (void) strcpy(line, "open ");
                   2153:        printf("(to) ");
                   2154:        (void) fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin);
                   2155:        makeargv();
                   2156:        argc = margc;
                   2157:        argv = margv;
                   2158:     }
                   2159:     cmd = *argv;
                   2160:     --argc; ++argv;
                   2161:     while (argc) {
1.3       niklas   2162:        if (strcmp(*argv, "help") == 0 || isprefix(*argv, "?"))
1.1       deraadt  2163:            goto usage;
                   2164:        if (strcmp(*argv, "-l") == 0) {
                   2165:            --argc; ++argv;
                   2166:            if (argc == 0)
                   2167:                goto usage;
                   2168:            user = *argv++;
                   2169:            --argc;
                   2170:            continue;
                   2171:        }
1.5       niklas   2172:        if (strcmp(*argv, "-b") == 0) {
                   2173:            --argc; ++argv;
                   2174:            if (argc == 0)
                   2175:                goto usage;
                   2176:            aliasp = *argv++;
                   2177:            --argc;
                   2178:            continue;
                   2179:        }
1.1       deraadt  2180:        if (strcmp(*argv, "-a") == 0) {
                   2181:            --argc; ++argv;
                   2182:            autologin = 1;
                   2183:            continue;
                   2184:        }
                   2185:        if (hostp == 0) {
                   2186:            hostp = *argv++;
                   2187:            --argc;
                   2188:            continue;
                   2189:        }
                   2190:        if (portp == 0) {
                   2191:            portp = *argv++;
                   2192:            --argc;
                   2193:            continue;
                   2194:        }
                   2195:     usage:
                   2196:        printf("usage: %s [-l user] [-a] host-name [port]\n", cmd);
1.9     ! tholo    2197:        seteuid(getuid());
1.1       deraadt  2198:        setuid(getuid());
                   2199:        return 0;
                   2200:     }
                   2201:     if (hostp == 0)
                   2202:        goto usage;
                   2203:
                   2204: #if    defined(IP_OPTIONS) && defined(IPPROTO_IP)
                   2205:     if (hostp[0] == '@' || hostp[0] == '!') {
                   2206:        if ((hostname = strrchr(hostp, ':')) == NULL)
                   2207:            hostname = strrchr(hostp, '@');
                   2208:        hostname++;
                   2209:        srp = 0;
                   2210:        temp = sourceroute(hostp, &srp, &srlen);
                   2211:        if (temp == 0) {
                   2212:            herror(srp);
1.9     ! tholo    2213:            seteuid(getuid());
1.1       deraadt  2214:            setuid(getuid());
                   2215:            return 0;
                   2216:        } else if (temp == -1) {
                   2217:            printf("Bad source route option: %s\n", hostp);
1.9     ! tholo    2218:            seteuid(getuid());
1.1       deraadt  2219:            setuid(getuid());
                   2220:            return 0;
                   2221:        } else {
                   2222:            sin.sin_addr.s_addr = temp;
                   2223:            sin.sin_family = AF_INET;
                   2224:        }
                   2225:     } else {
                   2226: #endif
                   2227:        temp = inet_addr(hostp);
                   2228:        if (temp != INADDR_NONE) {
                   2229:            sin.sin_addr.s_addr = temp;
                   2230:            sin.sin_family = AF_INET;
1.4       deraadt  2231:            host = gethostbyaddr((char *)&temp, sizeof(temp), AF_INET);
                   2232:            if (host)
                   2233:                (void) strcpy(_hostname, host->h_name);
                   2234:            else
                   2235:                (void) strcpy(_hostname, hostp);
1.1       deraadt  2236:            hostname = _hostname;
                   2237:        } else {
                   2238:            host = gethostbyname(hostp);
                   2239:            if (host) {
                   2240:                sin.sin_family = host->h_addrtype;
                   2241: #if    defined(h_addr)         /* In 4.3, this is a #define */
1.3       niklas   2242:                memmove((caddr_t)&sin.sin_addr,
1.1       deraadt  2243:                                host->h_addr_list[0], host->h_length);
                   2244: #else  /* defined(h_addr) */
1.3       niklas   2245:                memmove((caddr_t)&sin.sin_addr, host->h_addr, host->h_length);
1.1       deraadt  2246: #endif /* defined(h_addr) */
                   2247:                strncpy(_hostname, host->h_name, sizeof(_hostname));
                   2248:                _hostname[sizeof(_hostname)-1] = '\0';
                   2249:                hostname = _hostname;
                   2250:            } else {
                   2251:                herror(hostp);
1.9     ! tholo    2252:                seteuid(getuid());
1.3       niklas   2253:                setuid(getuid());
1.1       deraadt  2254:                return 0;
                   2255:            }
                   2256:        }
                   2257: #if    defined(IP_OPTIONS) && defined(IPPROTO_IP)
                   2258:     }
                   2259: #endif
                   2260:     if (portp) {
                   2261:        if (*portp == '-') {
                   2262:            portp++;
                   2263:            telnetport = 1;
                   2264:        } else
                   2265:            telnetport = 0;
                   2266:        sin.sin_port = atoi(portp);
                   2267:        if (sin.sin_port == 0) {
                   2268:            sp = getservbyname(portp, "tcp");
                   2269:            if (sp)
                   2270:                sin.sin_port = sp->s_port;
                   2271:            else {
                   2272:                printf("%s: bad port number\n", portp);
1.9     ! tholo    2273:                seteuid(getuid());
1.3       niklas   2274:                setuid(getuid());
1.1       deraadt  2275:                return 0;
                   2276:            }
                   2277:        } else {
                   2278: #if    !defined(htons)
                   2279:            u_short htons P((unsigned short));
                   2280: #endif /* !defined(htons) */
                   2281:            sin.sin_port = htons(sin.sin_port);
                   2282:        }
                   2283:     } else {
                   2284:        if (sp == 0) {
                   2285:            sp = getservbyname("telnet", "tcp");
                   2286:            if (sp == 0) {
                   2287:                fprintf(stderr, "telnet: tcp/telnet: unknown service\n");
1.9     ! tholo    2288:                seteuid(getuid());
1.3       niklas   2289:                setuid(getuid());
1.1       deraadt  2290:                return 0;
                   2291:            }
                   2292:            sin.sin_port = sp->s_port;
                   2293:        }
                   2294:        telnetport = 1;
                   2295:     }
                   2296:     printf("Trying %s...\n", inet_ntoa(sin.sin_addr));
                   2297:     do {
                   2298:        net = socket(AF_INET, SOCK_STREAM, 0);
1.9     ! tholo    2299:        seteuid(getuid());
1.1       deraadt  2300:        setuid(getuid());
                   2301:        if (net < 0) {
                   2302:            perror("telnet: socket");
                   2303:            return 0;
                   2304:        }
1.5       niklas   2305:        if (aliasp) {
                   2306:            memset ((caddr_t)&ladr, 0, sizeof (ladr));
                   2307:            temp = inet_addr(aliasp);
                   2308:            if (temp != INADDR_NONE) {
                   2309:                ladr.sin_addr.s_addr = temp;
                   2310:                ladr.sin_family = AF_INET;
                   2311:                alias = gethostbyaddr((char *)&temp, sizeof(temp), AF_INET);
                   2312:            } else {
                   2313:                alias = gethostbyname(aliasp);
                   2314:                if (alias) {
                   2315:                    ladr.sin_family = alias->h_addrtype;
                   2316: #if    defined(h_addr)         /* In 4.3, this is a #define */
                   2317:                    memmove((caddr_t)&ladr.sin_addr,
                   2318:                        alias->h_addr_list[0], alias->h_length);
                   2319: #else  /* defined(h_addr) */
                   2320:                    memmove((caddr_t)&ladr.sin_addr, alias->h_addr,
                   2321:                        alias->h_length);
                   2322: #endif /* defined(h_addr) */
                   2323:                } else {
                   2324:                    herror(aliasp);
                   2325:                    return 0;
                   2326:                }
                   2327:            }
                   2328:             ladr.sin_port = htons(0);
                   2329:
                   2330:             if (bind (net, (struct sockaddr *)&ladr, sizeof(ladr)) < 0) {
                   2331:                 perror(aliasp);;
                   2332:                 (void) close(net);   /* dump descriptor */
                   2333:                return 0;
                   2334:             }
                   2335:         }
                   2336:  #if   defined(IP_OPTIONS) && defined(IPPROTO_IP)
1.1       deraadt  2337:        if (srp && setsockopt(net, IPPROTO_IP, IP_OPTIONS, (char *)srp, srlen) < 0)
                   2338:                perror("setsockopt (IP_OPTIONS)");
                   2339: #endif
                   2340: #if    defined(IPPROTO_IP) && defined(IP_TOS)
                   2341:        {
                   2342: # if   defined(HAS_GETTOS)
                   2343:            struct tosent *tp;
                   2344:            if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
                   2345:                tos = tp->t_tos;
                   2346: # endif
                   2347:            if (tos < 0)
1.3       niklas   2348:                tos = IPTOS_LOWDELAY;   /* Low Delay bit */
1.1       deraadt  2349:            if (tos
                   2350:                && (setsockopt(net, IPPROTO_IP, IP_TOS,
                   2351:                    (char *)&tos, sizeof(int)) < 0)
                   2352:                && (errno != ENOPROTOOPT))
                   2353:                    perror("telnet: setsockopt (IP_TOS) (ignored)");
                   2354:        }
                   2355: #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
                   2356:
                   2357:        if (debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) {
                   2358:                perror("setsockopt (SO_DEBUG)");
                   2359:        }
                   2360:
                   2361:        if (connect(net, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
                   2362: #if    defined(h_addr)         /* In 4.3, this is a #define */
                   2363:            if (host && host->h_addr_list[1]) {
                   2364:                int oerrno = errno;
                   2365:
                   2366:                fprintf(stderr, "telnet: connect to address %s: ",
                   2367:                                                inet_ntoa(sin.sin_addr));
                   2368:                errno = oerrno;
                   2369:                perror((char *)0);
                   2370:                host->h_addr_list++;
1.3       niklas   2371:                memmove((caddr_t)&sin.sin_addr,
1.1       deraadt  2372:                        host->h_addr_list[0], host->h_length);
                   2373:                (void) NetClose(net);
                   2374:                continue;
                   2375:            }
                   2376: #endif /* defined(h_addr) */
                   2377:            perror("telnet: Unable to connect to remote host");
                   2378:            return 0;
                   2379:        }
                   2380:        connected++;
                   2381: #if    defined(AUTHENTICATION)
                   2382:        auth_encrypt_connect(connected);
                   2383: #endif /* defined(AUTHENTICATION) */
                   2384:     } while (connected == 0);
                   2385:     cmdrc(hostp, hostname);
                   2386:     if (autologin && user == NULL) {
                   2387:        struct passwd *pw;
                   2388:
                   2389:        user = getenv("USER");
                   2390:        if (user == NULL ||
                   2391:            (pw = getpwnam(user)) && pw->pw_uid != getuid()) {
                   2392:                if (pw = getpwuid(getuid()))
                   2393:                        user = pw->pw_name;
                   2394:                else
                   2395:                        user = NULL;
                   2396:        }
                   2397:     }
                   2398:     if (user) {
                   2399:        env_define((unsigned char *)"USER", (unsigned char *)user);
                   2400:        env_export((unsigned char *)"USER");
                   2401:     }
                   2402:     (void) call(status, "status", "notmuch", 0);
                   2403:     if (setjmp(peerdied) == 0)
                   2404:        telnet(user);
                   2405:     (void) NetClose(net);
                   2406:     ExitString("Connection closed by foreign host.\n",1);
                   2407:     /*NOTREACHED*/
                   2408: }
                   2409:
                   2410: #define HELPINDENT (sizeof ("connect"))
                   2411:
                   2412: static char
                   2413:        openhelp[] =    "connect to a site",
                   2414:        closehelp[] =   "close current connection",
                   2415:        logouthelp[] =  "forcibly logout remote user and close the connection",
                   2416:        quithelp[] =    "exit telnet",
                   2417:        statushelp[] =  "print status information",
                   2418:        helphelp[] =    "print help information",
                   2419:        sendhelp[] =    "transmit special characters ('send ?' for more)",
                   2420:        sethelp[] =     "set operating parameters ('set ?' for more)",
                   2421:        unsethelp[] =   "unset operating parameters ('unset ?' for more)",
                   2422:        togglestring[] ="toggle operating parameters ('toggle ?' for more)",
                   2423:        slchelp[] =     "change state of special charaters ('slc ?' for more)",
                   2424:        displayhelp[] = "display operating parameters",
                   2425: #if    defined(TN3270) && defined(unix)
                   2426:        transcomhelp[] = "specify Unix command for transparent mode pipe",
                   2427: #endif /* defined(TN3270) && defined(unix) */
                   2428: #if    defined(AUTHENTICATION)
                   2429:        authhelp[] =    "turn on (off) authentication ('auth ?' for more)",
                   2430: #endif
                   2431: #if    defined(unix)
                   2432:        zhelp[] =       "suspend telnet",
                   2433: #endif /* defined(unix) */
                   2434:        shellhelp[] =   "invoke a subshell",
                   2435:        envhelp[] =     "change environment variables ('environ ?' for more)",
                   2436:        modestring[] = "try to enter line or character mode ('mode ?' for more)";
                   2437:
                   2438: static int     help();
                   2439:
                   2440: static Command cmdtab[] = {
                   2441:        { "close",      closehelp,      bye,            1 },
                   2442:        { "logout",     logouthelp,     logout,         1 },
                   2443:        { "display",    displayhelp,    display,        0 },
                   2444:        { "mode",       modestring,     modecmd,        0 },
                   2445:        { "open",       openhelp,       tn,             0 },
                   2446:        { "quit",       quithelp,       quit,           0 },
                   2447:        { "send",       sendhelp,       sendcmd,        0 },
                   2448:        { "set",        sethelp,        setcmd,         0 },
                   2449:        { "unset",      unsethelp,      unsetcmd,       0 },
                   2450:        { "status",     statushelp,     status,         0 },
                   2451:        { "toggle",     togglestring,   toggle,         0 },
                   2452:        { "slc",        slchelp,        slccmd,         0 },
                   2453: #if    defined(TN3270) && defined(unix)
                   2454:        { "transcom",   transcomhelp,   settranscom,    0 },
                   2455: #endif /* defined(TN3270) && defined(unix) */
                   2456: #if    defined(AUTHENTICATION)
                   2457:        { "auth",       authhelp,       auth_cmd,       0 },
                   2458: #endif
                   2459: #if    defined(unix)
                   2460:        { "z",          zhelp,          suspend,        0 },
                   2461: #endif /* defined(unix) */
                   2462: #if    defined(TN3270)
                   2463:        { "!",          shellhelp,      shell,          1 },
                   2464: #else
                   2465:        { "!",          shellhelp,      shell,          0 },
                   2466: #endif
                   2467:        { "environ",    envhelp,        env_cmd,        0 },
                   2468:        { "?",          helphelp,       help,           0 },
1.6       deraadt  2469: #if    defined(SKEY)
                   2470:        { "skey",       NULL,           skey_calc,      0 },
                   2471: #endif
1.1       deraadt  2472:        0
                   2473: };
                   2474:
                   2475: static char    crmodhelp[] =   "deprecated command -- use 'toggle crmod' instead";
                   2476: static char    escapehelp[] =  "deprecated command -- use 'set escape' instead";
                   2477:
                   2478: static Command cmdtab2[] = {
                   2479:        { "help",       0,              help,           0 },
                   2480:        { "escape",     escapehelp,     setescape,      0 },
                   2481:        { "crmod",      crmodhelp,      togcrmod,       0 },
                   2482:        0
                   2483: };
                   2484:
                   2485:
                   2486: /*
                   2487:  * Call routine with argc, argv set from args (terminated by 0).
                   2488:  */
                   2489:
                   2490:     /*VARARGS1*/
                   2491:     static
                   2492: call(va_alist)
                   2493:     va_dcl
                   2494: {
                   2495:     va_list ap;
                   2496:     typedef int (*intrtn_t)();
                   2497:     intrtn_t routine;
                   2498:     char *args[100];
                   2499:     int argno = 0;
                   2500:
                   2501:     va_start(ap);
                   2502:     routine = (va_arg(ap, intrtn_t));
                   2503:     while ((args[argno++] = va_arg(ap, char *)) != 0) {
                   2504:        ;
                   2505:     }
                   2506:     va_end(ap);
                   2507:     return (*routine)(argno-1, args);
                   2508: }
                   2509:
                   2510:
                   2511:     static Command *
                   2512: getcmd(name)
                   2513:     char *name;
                   2514: {
                   2515:     Command *cm;
                   2516:
                   2517:     if (cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command)))
                   2518:        return cm;
                   2519:     return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));
                   2520: }
                   2521:
                   2522:     void
                   2523: command(top, tbuf, cnt)
                   2524:     int top;
                   2525:     char *tbuf;
                   2526:     int cnt;
                   2527: {
                   2528:     register Command *c;
                   2529:
                   2530:     setcommandmode();
                   2531:     if (!top) {
                   2532:        putchar('\n');
                   2533: #if    defined(unix)
                   2534:     } else {
                   2535:        (void) signal(SIGINT, SIG_DFL);
                   2536:        (void) signal(SIGQUIT, SIG_DFL);
                   2537: #endif /* defined(unix) */
                   2538:     }
                   2539:     for (;;) {
                   2540:        if (rlogin == _POSIX_VDISABLE)
                   2541:                printf("%s> ", prompt);
                   2542:        if (tbuf) {
                   2543:            register char *cp;
                   2544:            cp = line;
                   2545:            while (cnt > 0 && (*cp++ = *tbuf++) != '\n')
                   2546:                cnt--;
                   2547:            tbuf = 0;
                   2548:            if (cp == line || *--cp != '\n' || cp == line)
                   2549:                goto getline;
                   2550:            *cp = '\0';
                   2551:            if (rlogin == _POSIX_VDISABLE)
1.3       niklas   2552:                printf("%s\n", line);
1.1       deraadt  2553:        } else {
                   2554:        getline:
                   2555:            if (rlogin != _POSIX_VDISABLE)
                   2556:                printf("%s> ", prompt);
                   2557:            if (fgets(line, sizeof(line), stdin) == NULL) {
                   2558:                if (feof(stdin) || ferror(stdin)) {
                   2559:                    (void) quit();
                   2560:                    /*NOTREACHED*/
                   2561:                }
                   2562:                break;
                   2563:            }
                   2564:        }
                   2565:        if (line[0] == 0)
                   2566:            break;
                   2567:        makeargv();
                   2568:        if (margv[0] == 0) {
                   2569:            break;
                   2570:        }
                   2571:        c = getcmd(margv[0]);
                   2572:        if (Ambiguous(c)) {
                   2573:            printf("?Ambiguous command\n");
                   2574:            continue;
                   2575:        }
                   2576:        if (c == 0) {
                   2577:            printf("?Invalid command\n");
                   2578:            continue;
                   2579:        }
                   2580:        if (c->needconnect && !connected) {
                   2581:            printf("?Need to be connected first.\n");
                   2582:            continue;
                   2583:        }
                   2584:        if ((*c->handler)(margc, margv)) {
                   2585:            break;
                   2586:        }
                   2587:     }
                   2588:     if (!top) {
                   2589:        if (!connected) {
                   2590:            longjmp(toplevel, 1);
                   2591:            /*NOTREACHED*/
                   2592:        }
                   2593: #if    defined(TN3270)
                   2594:        if (shell_active == 0) {
                   2595:            setconnmode(0);
                   2596:        }
                   2597: #else  /* defined(TN3270) */
                   2598:        setconnmode(0);
                   2599: #endif /* defined(TN3270) */
                   2600:     }
                   2601: }
                   2602: 
                   2603: /*
                   2604:  * Help command.
                   2605:  */
                   2606:        static
                   2607: help(argc, argv)
                   2608:        int argc;
                   2609:        char *argv[];
                   2610: {
                   2611:        register Command *c;
                   2612:
                   2613:        if (argc == 1) {
                   2614:                printf("Commands may be abbreviated.  Commands are:\n\n");
                   2615:                for (c = cmdtab; c->name; c++)
                   2616:                        if (c->help) {
                   2617:                                printf("%-*s\t%s\n", HELPINDENT, c->name,
                   2618:                                                                    c->help);
                   2619:                        }
                   2620:                return 0;
                   2621:        }
                   2622:        while (--argc > 0) {
                   2623:                register char *arg;
                   2624:                arg = *++argv;
                   2625:                c = getcmd(arg);
                   2626:                if (Ambiguous(c))
                   2627:                        printf("?Ambiguous help command %s\n", arg);
                   2628:                else if (c == (Command *)0)
                   2629:                        printf("?Invalid help command %s\n", arg);
                   2630:                else
                   2631:                        printf("%s\n", c->help);
                   2632:        }
                   2633:        return 0;
                   2634: }
                   2635:
                   2636: static char *rcname = 0;
                   2637: static char rcbuf[128];
                   2638:
                   2639: cmdrc(m1, m2)
                   2640:        char *m1, *m2;
                   2641: {
                   2642:     register Command *c;
                   2643:     FILE *rcfile;
                   2644:     int gotmachine = 0;
                   2645:     int l1 = strlen(m1);
                   2646:     int l2 = strlen(m2);
                   2647:     char m1save[64];
                   2648:
                   2649:     if (skiprc)
                   2650:        return;
                   2651:
                   2652:     strcpy(m1save, m1);
                   2653:     m1 = m1save;
                   2654:
                   2655:     if (rcname == 0) {
                   2656:        rcname = getenv("HOME");
1.7       millert  2657:        if (rcname && (strlen(rcname) + 10) < sizeof(rcbuf))
1.1       deraadt  2658:            strcpy(rcbuf, rcname);
                   2659:        else
                   2660:            rcbuf[0] = '\0';
                   2661:        strcat(rcbuf, "/.telnetrc");
                   2662:        rcname = rcbuf;
                   2663:     }
                   2664:
                   2665:     if ((rcfile = fopen(rcname, "r")) == 0) {
                   2666:        return;
                   2667:     }
                   2668:
                   2669:     for (;;) {
                   2670:        if (fgets(line, sizeof(line), rcfile) == NULL)
                   2671:            break;
                   2672:        if (line[0] == 0)
                   2673:            break;
                   2674:        if (line[0] == '#')
                   2675:            continue;
                   2676:        if (gotmachine) {
                   2677:            if (!isspace(line[0]))
                   2678:                gotmachine = 0;
                   2679:        }
                   2680:        if (gotmachine == 0) {
                   2681:            if (isspace(line[0]))
                   2682:                continue;
                   2683:            if (strncasecmp(line, m1, l1) == 0)
                   2684:                strncpy(line, &line[l1], sizeof(line) - l1);
                   2685:            else if (strncasecmp(line, m2, l2) == 0)
                   2686:                strncpy(line, &line[l2], sizeof(line) - l2);
                   2687:            else if (strncasecmp(line, "DEFAULT", 7) == 0)
                   2688:                strncpy(line, &line[7], sizeof(line) - 7);
                   2689:            else
                   2690:                continue;
                   2691:            if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n')
                   2692:                continue;
                   2693:            gotmachine = 1;
                   2694:        }
                   2695:        makeargv();
                   2696:        if (margv[0] == 0)
                   2697:            continue;
                   2698:        c = getcmd(margv[0]);
                   2699:        if (Ambiguous(c)) {
                   2700:            printf("?Ambiguous command: %s\n", margv[0]);
                   2701:            continue;
                   2702:        }
                   2703:        if (c == 0) {
                   2704:            printf("?Invalid command: %s\n", margv[0]);
                   2705:            continue;
                   2706:        }
                   2707:        /*
                   2708:         * This should never happen...
                   2709:         */
                   2710:        if (c->needconnect && !connected) {
                   2711:            printf("?Need to be connected first for %s.\n", margv[0]);
                   2712:            continue;
                   2713:        }
                   2714:        (*c->handler)(margc, margv);
                   2715:     }
                   2716:     fclose(rcfile);
                   2717: }
                   2718:
                   2719: #if    defined(IP_OPTIONS) && defined(IPPROTO_IP)
                   2720:
                   2721: /*
                   2722:  * Source route is handed in as
                   2723:  *     [!]@hop1@hop2...[@|:]dst
                   2724:  * If the leading ! is present, it is a
                   2725:  * strict source route, otherwise it is
                   2726:  * assmed to be a loose source route.
                   2727:  *
                   2728:  * We fill in the source route option as
                   2729:  *     hop1,hop2,hop3...dest
                   2730:  * and return a pointer to hop1, which will
                   2731:  * be the address to connect() to.
                   2732:  *
                   2733:  * Arguments:
                   2734:  *     arg:    pointer to route list to decipher
                   2735:  *
                   2736:  *     cpp:    If *cpp is not equal to NULL, this is a
                   2737:  *             pointer to a pointer to a character array
                   2738:  *             that should be filled in with the option.
                   2739:  *
                   2740:  *     lenp:   pointer to an integer that contains the
                   2741:  *             length of *cpp if *cpp != NULL.
                   2742:  *
                   2743:  * Return values:
                   2744:  *
                   2745:  *     Returns the address of the host to connect to.  If the
                   2746:  *     return value is -1, there was a syntax error in the
                   2747:  *     option, either unknown characters, or too many hosts.
                   2748:  *     If the return value is 0, one of the hostnames in the
                   2749:  *     path is unknown, and *cpp is set to point to the bad
                   2750:  *     hostname.
                   2751:  *
                   2752:  *     *cpp:   If *cpp was equal to NULL, it will be filled
                   2753:  *             in with a pointer to our static area that has
                   2754:  *             the option filled in.  This will be 32bit aligned.
1.3       niklas   2755:  *
1.1       deraadt  2756:  *     *lenp:  This will be filled in with how long the option
                   2757:  *             pointed to by *cpp is.
1.3       niklas   2758:  *
1.1       deraadt  2759:  */
                   2760:        unsigned long
                   2761: sourceroute(arg, cpp, lenp)
                   2762:        char    *arg;
                   2763:        char    **cpp;
                   2764:        int     *lenp;
                   2765: {
                   2766:        static char lsr[44];
                   2767: #ifdef sysV88
                   2768:        static IOPTN ipopt;
                   2769: #endif
                   2770:        char *cp, *cp2, *lsrp, *lsrep;
                   2771:        register int tmp;
                   2772:        struct in_addr sin_addr;
                   2773:        register struct hostent *host = 0;
                   2774:        register char c;
                   2775:
                   2776:        /*
                   2777:         * Verify the arguments, and make sure we have
                   2778:         * at least 7 bytes for the option.
                   2779:         */
                   2780:        if (cpp == NULL || lenp == NULL)
                   2781:                return((unsigned long)-1);
                   2782:        if (*cpp != NULL && *lenp < 7)
                   2783:                return((unsigned long)-1);
                   2784:        /*
                   2785:         * Decide whether we have a buffer passed to us,
                   2786:         * or if we need to use our own static buffer.
                   2787:         */
                   2788:        if (*cpp) {
                   2789:                lsrp = *cpp;
                   2790:                lsrep = lsrp + *lenp;
                   2791:        } else {
                   2792:                *cpp = lsrp = lsr;
                   2793:                lsrep = lsrp + 44;
                   2794:        }
                   2795:
                   2796:        cp = arg;
                   2797:
                   2798:        /*
                   2799:         * Next, decide whether we have a loose source
                   2800:         * route or a strict source route, and fill in
                   2801:         * the begining of the option.
                   2802:         */
                   2803: #ifndef        sysV88
                   2804:        if (*cp == '!') {
                   2805:                cp++;
                   2806:                *lsrp++ = IPOPT_SSRR;
                   2807:        } else
                   2808:                *lsrp++ = IPOPT_LSRR;
                   2809: #else
                   2810:        if (*cp == '!') {
                   2811:                cp++;
                   2812:                ipopt.io_type = IPOPT_SSRR;
                   2813:        } else
                   2814:                ipopt.io_type = IPOPT_LSRR;
                   2815: #endif
                   2816:
                   2817:        if (*cp != '@')
                   2818:                return((unsigned long)-1);
                   2819:
                   2820: #ifndef        sysV88
                   2821:        lsrp++;         /* skip over length, we'll fill it in later */
                   2822:        *lsrp++ = 4;
                   2823: #endif
                   2824:
                   2825:        cp++;
                   2826:
                   2827:        sin_addr.s_addr = 0;
                   2828:
                   2829:        for (c = 0;;) {
                   2830:                if (c == ':')
                   2831:                        cp2 = 0;
                   2832:                else for (cp2 = cp; c = *cp2; cp2++) {
                   2833:                        if (c == ',') {
                   2834:                                *cp2++ = '\0';
                   2835:                                if (*cp2 == '@')
                   2836:                                        cp2++;
                   2837:                        } else if (c == '@') {
                   2838:                                *cp2++ = '\0';
                   2839:                        } else if (c == ':') {
                   2840:                                *cp2++ = '\0';
                   2841:                        } else
                   2842:                                continue;
                   2843:                        break;
                   2844:                }
                   2845:                if (!c)
                   2846:                        cp2 = 0;
                   2847:
                   2848:                if ((tmp = inet_addr(cp)) != INADDR_NONE) {
                   2849:                        sin_addr.s_addr = tmp;
                   2850:                } else if (host = gethostbyname(cp)) {
                   2851: #if    defined(h_addr)
1.3       niklas   2852:                        memmove((caddr_t)&sin_addr,
1.1       deraadt  2853:                                host->h_addr_list[0], host->h_length);
                   2854: #else
1.3       niklas   2855:                        memmove((caddr_t)&sin_addr, host->h_addr, host->h_length);
1.1       deraadt  2856: #endif
                   2857:                } else {
                   2858:                        *cpp = cp;
                   2859:                        return(0);
                   2860:                }
1.3       niklas   2861:                memmove(lsrp, (char *)&sin_addr, 4);
1.1       deraadt  2862:                lsrp += 4;
                   2863:                if (cp2)
                   2864:                        cp = cp2;
                   2865:                else
                   2866:                        break;
                   2867:                /*
                   2868:                 * Check to make sure there is space for next address
                   2869:                 */
                   2870:                if (lsrp + 4 > lsrep)
                   2871:                        return((unsigned long)-1);
                   2872:        }
                   2873: #ifndef        sysV88
                   2874:        if ((*(*cpp+IPOPT_OLEN) = lsrp - *cpp) <= 7) {
                   2875:                *cpp = 0;
                   2876:                *lenp = 0;
                   2877:                return((unsigned long)-1);
                   2878:        }
                   2879:        *lsrp++ = IPOPT_NOP; /* 32 bit word align it */
                   2880:        *lenp = lsrp - *cpp;
                   2881: #else
                   2882:        ipopt.io_len = lsrp - *cpp;
                   2883:        if (ipopt.io_len <= 5) {                /* Is 3 better ? */
                   2884:                *cpp = 0;
                   2885:                *lenp = 0;
                   2886:                return((unsigned long)-1);
                   2887:        }
                   2888:        *lenp = sizeof(ipopt);
                   2889:        *cpp = (char *) &ipopt;
                   2890: #endif
                   2891:        return(sin_addr.s_addr);
                   2892: }
                   2893: #endif