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

1.6     ! deraadt     1: /*     $OpenBSD: commands.c,v 1.5 1996/07/03 14:01:55 niklas 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.6     ! deraadt    42: static char rcsid[] = "$OpenBSD: commands.c,v 1.5 1996/07/03 14:01:55 niklas 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;
                    672: };
                    673:
                    674: static struct togglelist Togglelist[] = {
                    675:     { "autoflush",
                    676:        "flushing of output when sending interrupt characters",
                    677:            0,
                    678:                &autoflush,
                    679:                    "flush output when sending interrupt characters" },
                    680:     { "autosynch",
                    681:        "automatic sending of interrupt characters in urgent mode",
                    682:            0,
                    683:                &autosynch,
                    684:                    "send interrupt characters in urgent mode" },
                    685: #if    defined(AUTHENTICATION)
                    686:     { "autologin",
                    687:        "automatic sending of login and/or authentication info",
                    688:            0,
                    689:                &autologin,
                    690:                    "send login name and/or authentication information" },
                    691:     { "authdebug",
                    692:        "Toggle authentication debugging",
                    693:            auth_togdebug,
                    694:                0,
                    695:                     "print authentication debugging information" },
                    696: #endif
                    697:     { "skiprc",
                    698:        "don't read ~/.telnetrc file",
                    699:            0,
                    700:                &skiprc,
                    701:                    "skip reading of ~/.telnetrc file" },
                    702:     { "binary",
                    703:        "sending and receiving of binary data",
                    704:            togbinary,
                    705:                0,
                    706:                    0 },
                    707:     { "inbinary",
                    708:        "receiving of binary data",
                    709:            togrbinary,
                    710:                0,
                    711:                    0 },
                    712:     { "outbinary",
                    713:        "sending of binary data",
                    714:            togxbinary,
                    715:                0,
                    716:                    0 },
                    717:     { "crlf",
                    718:        "sending carriage returns as telnet <CR><LF>",
                    719:            togcrlf,
                    720:                &crlf,
                    721:                    0 },
                    722:     { "crmod",
                    723:        "mapping of received carriage returns",
                    724:            0,
                    725:                &crmod,
                    726:                    "map carriage return on output" },
                    727:     { "localchars",
                    728:        "local recognition of certain control characters",
                    729:            lclchars,
                    730:                &localchars,
                    731:                    "recognize certain control characters" },
                    732:     { " ", "", 0 },            /* empty line */
                    733: #if    defined(unix) && defined(TN3270)
                    734:     { "apitrace",
                    735:        "(debugging) toggle tracing of API transactions",
                    736:            0,
                    737:                &apitrace,
                    738:                    "trace API transactions" },
                    739:     { "cursesdata",
                    740:        "(debugging) toggle printing of hexadecimal curses data",
                    741:            0,
                    742:                &cursesdata,
                    743:                    "print hexadecimal representation of curses data" },
                    744: #endif /* defined(unix) && defined(TN3270) */
                    745:     { "debug",
                    746:        "debugging",
                    747:            togdebug,
                    748:                &debug,
                    749:                    "turn on socket level debugging" },
                    750:     { "netdata",
                    751:        "printing of hexadecimal network data (debugging)",
                    752:            0,
                    753:                &netdata,
                    754:                    "print hexadecimal representation of network traffic" },
                    755:     { "prettydump",
                    756:        "output of \"netdata\" to user readable format (debugging)",
                    757:            0,
                    758:                &prettydump,
                    759:                    "print user readable output for \"netdata\"" },
                    760:     { "options",
                    761:        "viewing of options processing (debugging)",
                    762:            0,
                    763:                &showoptions,
                    764:                    "show option processing" },
                    765: #if    defined(unix)
                    766:     { "termdata",
                    767:        "(debugging) toggle printing of hexadecimal terminal data",
                    768:            0,
                    769:                &termdata,
                    770:                    "print hexadecimal representation of terminal traffic" },
                    771: #endif /* defined(unix) */
                    772:     { "?",
                    773:        0,
                    774:            togglehelp },
                    775:     { "help",
                    776:        0,
                    777:            togglehelp },
                    778:     { 0 }
                    779: };
                    780:
                    781:     static int
                    782: togglehelp()
                    783: {
                    784:     struct togglelist *c;
                    785:
                    786:     for (c = Togglelist; c->name; c++) {
                    787:        if (c->help) {
                    788:            if (*c->help)
                    789:                printf("%-15s toggle %s\n", c->name, c->help);
                    790:            else
                    791:                printf("\n");
                    792:        }
                    793:     }
                    794:     printf("\n");
                    795:     printf("%-15s %s\n", "?", "display help information");
                    796:     return 0;
                    797: }
                    798:
                    799:     static void
                    800: settogglehelp(set)
                    801:     int set;
                    802: {
                    803:     struct togglelist *c;
                    804:
                    805:     for (c = Togglelist; c->name; c++) {
                    806:        if (c->help) {
                    807:            if (*c->help)
                    808:                printf("%-15s %s %s\n", c->name, set ? "enable" : "disable",
                    809:                                                c->help);
                    810:            else
                    811:                printf("\n");
                    812:        }
                    813:     }
                    814: }
                    815:
                    816: #define        GETTOGGLE(name) (struct togglelist *) \
                    817:                genget(name, (char **) Togglelist, sizeof(struct togglelist))
                    818:
                    819:     static int
                    820: toggle(argc, argv)
                    821:     int  argc;
                    822:     char *argv[];
                    823: {
                    824:     int retval = 1;
                    825:     char *name;
                    826:     struct togglelist *c;
                    827:
                    828:     if (argc < 2) {
                    829:        fprintf(stderr,
                    830:            "Need an argument to 'toggle' command.  'toggle ?' for help.\n");
                    831:        return 0;
                    832:     }
                    833:     argc--;
                    834:     argv++;
                    835:     while (argc--) {
                    836:        name = *argv++;
                    837:        c = GETTOGGLE(name);
                    838:        if (Ambiguous(c)) {
                    839:            fprintf(stderr, "'%s': ambiguous argument ('toggle ?' for help).\n",
                    840:                                        name);
                    841:            return 0;
                    842:        } else if (c == 0) {
                    843:            fprintf(stderr, "'%s': unknown argument ('toggle ?' for help).\n",
                    844:                                        name);
                    845:            return 0;
                    846:        } else {
                    847:            if (c->variable) {
                    848:                *c->variable = !*c->variable;           /* invert it */
                    849:                if (c->actionexplanation) {
                    850:                    printf("%s %s.\n", *c->variable? "Will" : "Won't",
                    851:                                                        c->actionexplanation);
                    852:                }
                    853:            }
                    854:            if (c->handler) {
                    855:                retval &= (*c->handler)(-1);
                    856:            }
                    857:        }
                    858:     }
                    859:     return retval;
                    860: }
                    861: 
                    862: /*
                    863:  * The following perform the "set" command.
                    864:  */
                    865:
                    866: #ifdef USE_TERMIO
                    867: struct termio new_tc = { 0 };
                    868: #endif
                    869:
                    870: struct setlist {
                    871:     char *name;                                /* name */
                    872:     char *help;                                /* help information */
                    873:     void (*handler)();
                    874:     cc_t *charp;                       /* where it is located at */
                    875: };
                    876:
                    877: static struct setlist Setlist[] = {
                    878: #ifdef KLUDGELINEMODE
                    879:     { "echo",  "character to toggle local echoing on/off", 0, &echoc },
                    880: #endif
                    881:     { "escape",        "character to escape back to telnet command mode", 0, &escape },
                    882:     { "rlogin", "rlogin escape character", 0, &rlogin },
                    883:     { "tracefile", "file to write trace information to", SetNetTrace, (cc_t *)NetTraceFile},
                    884:     { " ", "" },
                    885:     { " ", "The following need 'localchars' to be toggled true", 0, 0 },
                    886:     { "flushoutput", "character to cause an Abort Output", 0, termFlushCharp },
                    887:     { "interrupt", "character to cause an Interrupt Process", 0, termIntCharp },
                    888:     { "quit",  "character to cause an Abort process", 0, termQuitCharp },
                    889:     { "eof",   "character to cause an EOF ", 0, termEofCharp },
                    890:     { " ", "" },
                    891:     { " ", "The following are for local editing in linemode", 0, 0 },
                    892:     { "erase", "character to use to erase a character", 0, termEraseCharp },
                    893:     { "kill",  "character to use to erase a line", 0, termKillCharp },
                    894:     { "lnext", "character to use for literal next", 0, termLiteralNextCharp },
                    895:     { "susp",  "character to cause a Suspend Process", 0, termSuspCharp },
                    896:     { "reprint", "character to use for line reprint", 0, termRprntCharp },
                    897:     { "worderase", "character to use to erase a word", 0, termWerasCharp },
                    898:     { "start", "character to use for XON", 0, termStartCharp },
                    899:     { "stop",  "character to use for XOFF", 0, termStopCharp },
                    900:     { "forw1", "alternate end of line character", 0, termForw1Charp },
                    901:     { "forw2", "alternate end of line character", 0, termForw2Charp },
                    902:     { "ayt",   "alternate AYT character", 0, termAytCharp },
                    903:     { 0 }
                    904: };
                    905:
                    906: #if    defined(CRAY) && !defined(__STDC__)
                    907: /* Work around compiler bug in pcc 4.1.5 */
                    908:     void
                    909: _setlist_init()
                    910: {
                    911: #ifndef        KLUDGELINEMODE
                    912: #define        N 5
                    913: #else
                    914: #define        N 6
                    915: #endif
                    916:        Setlist[N+0].charp = &termFlushChar;
                    917:        Setlist[N+1].charp = &termIntChar;
                    918:        Setlist[N+2].charp = &termQuitChar;
                    919:        Setlist[N+3].charp = &termEofChar;
                    920:        Setlist[N+6].charp = &termEraseChar;
                    921:        Setlist[N+7].charp = &termKillChar;
                    922:        Setlist[N+8].charp = &termLiteralNextChar;
                    923:        Setlist[N+9].charp = &termSuspChar;
                    924:        Setlist[N+10].charp = &termRprntChar;
                    925:        Setlist[N+11].charp = &termWerasChar;
                    926:        Setlist[N+12].charp = &termStartChar;
                    927:        Setlist[N+13].charp = &termStopChar;
                    928:        Setlist[N+14].charp = &termForw1Char;
                    929:        Setlist[N+15].charp = &termForw2Char;
                    930:        Setlist[N+16].charp = &termAytChar;
                    931: #undef N
                    932: }
                    933: #endif /* defined(CRAY) && !defined(__STDC__) */
                    934:
                    935:     static struct setlist *
                    936: getset(name)
                    937:     char *name;
                    938: {
                    939:     return (struct setlist *)
                    940:                genget(name, (char **) Setlist, sizeof(struct setlist));
                    941: }
                    942:
                    943:     void
                    944: set_escape_char(s)
                    945:     char *s;
                    946: {
                    947:        if (rlogin != _POSIX_VDISABLE) {
                    948:                rlogin = (s && *s) ? special(s) : _POSIX_VDISABLE;
                    949:                printf("Telnet rlogin escape character is '%s'.\n",
                    950:                                        control(rlogin));
                    951:        } else {
                    952:                escape = (s && *s) ? special(s) : _POSIX_VDISABLE;
                    953:                printf("Telnet escape character is '%s'.\n", control(escape));
                    954:        }
                    955: }
                    956:
                    957:     static int
                    958: setcmd(argc, argv)
                    959:     int  argc;
                    960:     char *argv[];
                    961: {
                    962:     int value;
                    963:     struct setlist *ct;
                    964:     struct togglelist *c;
                    965:
                    966:     if (argc < 2 || argc > 3) {
                    967:        printf("Format is 'set Name Value'\n'set ?' for help.\n");
                    968:        return 0;
                    969:     }
                    970:     if ((argc == 2) && (isprefix(argv[1], "?") || isprefix(argv[1], "help"))) {
                    971:        for (ct = Setlist; ct->name; ct++)
                    972:            printf("%-15s %s\n", ct->name, ct->help);
                    973:        printf("\n");
                    974:        settogglehelp(1);
                    975:        printf("%-15s %s\n", "?", "display help information");
                    976:        return 0;
                    977:     }
                    978:
                    979:     ct = getset(argv[1]);
                    980:     if (ct == 0) {
                    981:        c = GETTOGGLE(argv[1]);
                    982:        if (c == 0) {
                    983:            fprintf(stderr, "'%s': unknown argument ('set ?' for help).\n",
                    984:                        argv[1]);
                    985:            return 0;
                    986:        } else if (Ambiguous(c)) {
                    987:            fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
                    988:                        argv[1]);
                    989:            return 0;
                    990:        }
                    991:        if (c->variable) {
                    992:            if ((argc == 2) || (strcmp("on", argv[2]) == 0))
                    993:                *c->variable = 1;
                    994:            else if (strcmp("off", argv[2]) == 0)
                    995:                *c->variable = 0;
                    996:            else {
                    997:                printf("Format is 'set togglename [on|off]'\n'set ?' for help.\n");
                    998:                return 0;
                    999:            }
                   1000:            if (c->actionexplanation) {
                   1001:                printf("%s %s.\n", *c->variable? "Will" : "Won't",
                   1002:                                                        c->actionexplanation);
                   1003:            }
                   1004:        }
                   1005:        if (c->handler)
                   1006:            (*c->handler)(1);
                   1007:     } else if (argc != 3) {
                   1008:        printf("Format is 'set Name Value'\n'set ?' for help.\n");
                   1009:        return 0;
                   1010:     } else if (Ambiguous(ct)) {
                   1011:        fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
                   1012:                        argv[1]);
                   1013:        return 0;
                   1014:     } else if (ct->handler) {
                   1015:        (*ct->handler)(argv[2]);
                   1016:        printf("%s set to \"%s\".\n", ct->name, (char *)ct->charp);
                   1017:     } else {
                   1018:        if (strcmp("off", argv[2])) {
                   1019:            value = special(argv[2]);
                   1020:        } else {
                   1021:            value = _POSIX_VDISABLE;
                   1022:        }
                   1023:        *(ct->charp) = (cc_t)value;
                   1024:        printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
                   1025:     }
                   1026:     slc_check();
                   1027:     return 1;
                   1028: }
                   1029:
                   1030:     static int
                   1031: unsetcmd(argc, argv)
                   1032:     int  argc;
                   1033:     char *argv[];
                   1034: {
                   1035:     struct setlist *ct;
                   1036:     struct togglelist *c;
                   1037:     register char *name;
                   1038:
                   1039:     if (argc < 2) {
                   1040:        fprintf(stderr,
                   1041:            "Need an argument to 'unset' command.  'unset ?' for help.\n");
                   1042:        return 0;
                   1043:     }
                   1044:     if (isprefix(argv[1], "?") || isprefix(argv[1], "help")) {
                   1045:        for (ct = Setlist; ct->name; ct++)
                   1046:            printf("%-15s %s\n", ct->name, ct->help);
                   1047:        printf("\n");
                   1048:        settogglehelp(0);
                   1049:        printf("%-15s %s\n", "?", "display help information");
                   1050:        return 0;
                   1051:     }
                   1052:
                   1053:     argc--;
                   1054:     argv++;
                   1055:     while (argc--) {
                   1056:        name = *argv++;
                   1057:        ct = getset(name);
                   1058:        if (ct == 0) {
                   1059:            c = GETTOGGLE(name);
                   1060:            if (c == 0) {
                   1061:                fprintf(stderr, "'%s': unknown argument ('unset ?' for help).\n",
                   1062:                        name);
                   1063:                return 0;
                   1064:            } else if (Ambiguous(c)) {
                   1065:                fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
                   1066:                        name);
                   1067:                return 0;
                   1068:            }
                   1069:            if (c->variable) {
                   1070:                *c->variable = 0;
                   1071:                if (c->actionexplanation) {
                   1072:                    printf("%s %s.\n", *c->variable? "Will" : "Won't",
                   1073:                                                        c->actionexplanation);
                   1074:                }
                   1075:            }
                   1076:            if (c->handler)
                   1077:                (*c->handler)(0);
                   1078:        } else if (Ambiguous(ct)) {
                   1079:            fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
                   1080:                        name);
                   1081:            return 0;
                   1082:        } else if (ct->handler) {
                   1083:            (*ct->handler)(0);
                   1084:            printf("%s reset to \"%s\".\n", ct->name, (char *)ct->charp);
                   1085:        } else {
                   1086:            *(ct->charp) = _POSIX_VDISABLE;
                   1087:            printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
                   1088:        }
                   1089:     }
                   1090:     return 1;
                   1091: }
                   1092: 
                   1093: /*
                   1094:  * The following are the data structures and routines for the
                   1095:  * 'mode' command.
                   1096:  */
                   1097: #ifdef KLUDGELINEMODE
                   1098: extern int kludgelinemode;
                   1099:
                   1100:     static int
                   1101: dokludgemode()
                   1102: {
                   1103:     kludgelinemode = 1;
                   1104:     send_wont(TELOPT_LINEMODE, 1);
                   1105:     send_dont(TELOPT_SGA, 1);
                   1106:     send_dont(TELOPT_ECHO, 1);
                   1107: }
                   1108: #endif
                   1109:
                   1110:     static int
                   1111: dolinemode()
                   1112: {
                   1113: #ifdef KLUDGELINEMODE
                   1114:     if (kludgelinemode)
                   1115:        send_dont(TELOPT_SGA, 1);
                   1116: #endif
                   1117:     send_will(TELOPT_LINEMODE, 1);
                   1118:     send_dont(TELOPT_ECHO, 1);
                   1119:     return 1;
                   1120: }
                   1121:
                   1122:     static int
                   1123: docharmode()
                   1124: {
                   1125: #ifdef KLUDGELINEMODE
                   1126:     if (kludgelinemode)
                   1127:        send_do(TELOPT_SGA, 1);
                   1128:     else
                   1129: #endif
                   1130:     send_wont(TELOPT_LINEMODE, 1);
                   1131:     send_do(TELOPT_ECHO, 1);
                   1132:     return 1;
                   1133: }
                   1134:
                   1135:     static int
                   1136: dolmmode(bit, on)
                   1137:     int bit, on;
                   1138: {
                   1139:     unsigned char c;
                   1140:     extern int linemode;
                   1141:
                   1142:     if (my_want_state_is_wont(TELOPT_LINEMODE)) {
                   1143:        printf("?Need to have LINEMODE option enabled first.\n");
                   1144:        printf("'mode ?' for help.\n");
                   1145:        return 0;
                   1146:     }
                   1147:
                   1148:     if (on)
                   1149:        c = (linemode | bit);
                   1150:     else
                   1151:        c = (linemode & ~bit);
                   1152:     lm_mode(&c, 1, 1);
                   1153:     return 1;
                   1154: }
                   1155:
                   1156:     int
                   1157: setmode(bit)
                   1158: {
                   1159:     return dolmmode(bit, 1);
                   1160: }
                   1161:
                   1162:     int
                   1163: clearmode(bit)
                   1164: {
                   1165:     return dolmmode(bit, 0);
                   1166: }
                   1167:
                   1168: struct modelist {
                   1169:        char    *name;          /* command name */
                   1170:        char    *help;          /* help string */
                   1171:        int     (*handler)();   /* routine which executes command */
                   1172:        int     needconnect;    /* Do we need to be connected to execute? */
                   1173:        int     arg1;
                   1174: };
                   1175:
                   1176: extern int modehelp();
                   1177:
                   1178: static struct modelist ModeList[] = {
                   1179:     { "character", "Disable LINEMODE option",  docharmode, 1 },
                   1180: #ifdef KLUDGELINEMODE
                   1181:     { "",      "(or disable obsolete line-by-line mode)", 0 },
                   1182: #endif
                   1183:     { "line",  "Enable LINEMODE option",       dolinemode, 1 },
                   1184: #ifdef KLUDGELINEMODE
                   1185:     { "",      "(or enable obsolete line-by-line mode)", 0 },
                   1186: #endif
                   1187:     { "", "", 0 },
                   1188:     { "",      "These require the LINEMODE option to be enabled", 0 },
                   1189:     { "isig",  "Enable signal trapping",       setmode, 1, MODE_TRAPSIG },
                   1190:     { "+isig", 0,                              setmode, 1, MODE_TRAPSIG },
                   1191:     { "-isig", "Disable signal trapping",      clearmode, 1, MODE_TRAPSIG },
                   1192:     { "edit",  "Enable character editing",     setmode, 1, MODE_EDIT },
                   1193:     { "+edit", 0,                              setmode, 1, MODE_EDIT },
                   1194:     { "-edit", "Disable character editing",    clearmode, 1, MODE_EDIT },
                   1195:     { "softtabs", "Enable tab expansion",      setmode, 1, MODE_SOFT_TAB },
                   1196:     { "+softtabs", 0,                          setmode, 1, MODE_SOFT_TAB },
                   1197:     { "-softtabs", "Disable character editing",        clearmode, 1, MODE_SOFT_TAB },
                   1198:     { "litecho", "Enable literal character echo", setmode, 1, MODE_LIT_ECHO },
                   1199:     { "+litecho", 0,                           setmode, 1, MODE_LIT_ECHO },
                   1200:     { "-litecho", "Disable literal character echo", clearmode, 1, MODE_LIT_ECHO },
                   1201:     { "help",  0,                              modehelp, 0 },
                   1202: #ifdef KLUDGELINEMODE
                   1203:     { "kludgeline", 0,                         dokludgemode, 1 },
                   1204: #endif
                   1205:     { "", "", 0 },
                   1206:     { "?",     "Print help information",       modehelp, 0 },
                   1207:     { 0 },
                   1208: };
                   1209:
                   1210:
                   1211:     int
                   1212: modehelp()
                   1213: {
                   1214:     struct modelist *mt;
                   1215:
                   1216:     printf("format is:  'mode Mode', where 'Mode' is one of:\n\n");
                   1217:     for (mt = ModeList; mt->name; mt++) {
                   1218:        if (mt->help) {
                   1219:            if (*mt->help)
                   1220:                printf("%-15s %s\n", mt->name, mt->help);
                   1221:            else
                   1222:                printf("\n");
                   1223:        }
                   1224:     }
                   1225:     return 0;
                   1226: }
                   1227:
                   1228: #define        GETMODECMD(name) (struct modelist *) \
                   1229:                genget(name, (char **) ModeList, sizeof(struct modelist))
                   1230:
                   1231:     static int
                   1232: modecmd(argc, argv)
                   1233:     int  argc;
                   1234:     char *argv[];
                   1235: {
                   1236:     struct modelist *mt;
                   1237:
                   1238:     if (argc != 2) {
                   1239:        printf("'mode' command requires an argument\n");
                   1240:        printf("'mode ?' for help.\n");
                   1241:     } else if ((mt = GETMODECMD(argv[1])) == 0) {
                   1242:        fprintf(stderr, "Unknown mode '%s' ('mode ?' for help).\n", argv[1]);
                   1243:     } else if (Ambiguous(mt)) {
                   1244:        fprintf(stderr, "Ambiguous mode '%s' ('mode ?' for help).\n", argv[1]);
                   1245:     } else if (mt->needconnect && !connected) {
                   1246:        printf("?Need to be connected first.\n");
                   1247:        printf("'mode ?' for help.\n");
                   1248:     } else if (mt->handler) {
                   1249:        return (*mt->handler)(mt->arg1);
                   1250:     }
                   1251:     return 0;
                   1252: }
                   1253: 
                   1254: /*
                   1255:  * The following data structures and routines implement the
                   1256:  * "display" command.
                   1257:  */
                   1258:
                   1259:     static int
                   1260: display(argc, argv)
                   1261:     int  argc;
                   1262:     char *argv[];
                   1263: {
                   1264:     struct togglelist *tl;
                   1265:     struct setlist *sl;
                   1266:
                   1267: #define        dotog(tl)       if (tl->variable && tl->actionexplanation) { \
                   1268:                            if (*tl->variable) { \
                   1269:                                printf("will"); \
                   1270:                            } else { \
                   1271:                                printf("won't"); \
                   1272:                            } \
                   1273:                            printf(" %s.\n", tl->actionexplanation); \
                   1274:                        }
                   1275:
                   1276: #define        doset(sl)   if (sl->name && *sl->name != ' ') { \
                   1277:                        if (sl->handler == 0) \
                   1278:                            printf("%-15s [%s]\n", sl->name, control(*sl->charp)); \
                   1279:                        else \
                   1280:                            printf("%-15s \"%s\"\n", sl->name, (char *)sl->charp); \
                   1281:                    }
                   1282:
                   1283:     if (argc == 1) {
                   1284:        for (tl = Togglelist; tl->name; tl++) {
                   1285:            dotog(tl);
                   1286:        }
                   1287:        printf("\n");
                   1288:        for (sl = Setlist; sl->name; sl++) {
                   1289:            doset(sl);
                   1290:        }
                   1291:     } else {
                   1292:        int i;
                   1293:
                   1294:        for (i = 1; i < argc; i++) {
                   1295:            sl = getset(argv[i]);
                   1296:            tl = GETTOGGLE(argv[i]);
                   1297:            if (Ambiguous(sl) || Ambiguous(tl)) {
                   1298:                printf("?Ambiguous argument '%s'.\n", argv[i]);
                   1299:                return 0;
                   1300:            } else if (!sl && !tl) {
                   1301:                printf("?Unknown argument '%s'.\n", argv[i]);
                   1302:                return 0;
                   1303:            } else {
                   1304:                if (tl) {
                   1305:                    dotog(tl);
                   1306:                }
                   1307:                if (sl) {
                   1308:                    doset(sl);
                   1309:                }
                   1310:            }
                   1311:        }
                   1312:     }
                   1313: /*@*/optionstatus();
                   1314:     return 1;
                   1315: #undef doset
                   1316: #undef dotog
                   1317: }
                   1318: 
                   1319: /*
                   1320:  * The following are the data structures, and many of the routines,
                   1321:  * relating to command processing.
                   1322:  */
                   1323:
                   1324: /*
                   1325:  * Set the escape character.
                   1326:  */
                   1327:        static int
                   1328: setescape(argc, argv)
                   1329:        int argc;
                   1330:        char *argv[];
                   1331: {
                   1332:        register char *arg;
                   1333:        char buf[50];
                   1334:
                   1335:        printf(
                   1336:            "Deprecated usage - please use 'set escape%s%s' in the future.\n",
                   1337:                                (argc > 2)? " ":"", (argc > 2)? argv[1]: "");
                   1338:        if (argc > 2)
                   1339:                arg = argv[1];
                   1340:        else {
                   1341:                printf("new escape character: ");
                   1342:                (void) fgets(buf, sizeof(buf), stdin);
                   1343:                arg = buf;
                   1344:        }
                   1345:        if (arg[0] != '\0')
                   1346:                escape = arg[0];
                   1347:        if (!In3270) {
                   1348:                printf("Escape character is '%s'.\n", control(escape));
                   1349:        }
                   1350:        (void) fflush(stdout);
                   1351:        return 1;
                   1352: }
                   1353:
                   1354:     /*VARARGS*/
                   1355:     static int
                   1356: togcrmod()
                   1357: {
                   1358:     crmod = !crmod;
                   1359:     printf("Deprecated usage - please use 'toggle crmod' in the future.\n");
                   1360:     printf("%s map carriage return on output.\n", crmod ? "Will" : "Won't");
                   1361:     (void) fflush(stdout);
                   1362:     return 1;
                   1363: }
                   1364:
                   1365:     /*VARARGS*/
                   1366:     int
                   1367: suspend()
                   1368: {
                   1369: #ifdef SIGTSTP
                   1370:     setcommandmode();
                   1371:     {
                   1372:        long oldrows, oldcols, newrows, newcols, err;
                   1373:
                   1374:        err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
                   1375:        (void) kill(0, SIGTSTP);
                   1376:        /*
                   1377:         * If we didn't get the window size before the SUSPEND, but we
1.3       niklas   1378:         * can get them now (?), then send the NAWS to make sure that
1.1       deraadt  1379:         * we are set up for the right window size.
                   1380:         */
                   1381:        if (TerminalWindowSize(&newrows, &newcols) && connected &&
                   1382:            (err || ((oldrows != newrows) || (oldcols != newcols)))) {
                   1383:                sendnaws();
                   1384:        }
                   1385:     }
                   1386:     /* reget parameters in case they were changed */
                   1387:     TerminalSaveState();
                   1388:     setconnmode(0);
                   1389: #else
                   1390:     printf("Suspend is not supported.  Try the '!' command instead\n");
                   1391: #endif
                   1392:     return 1;
                   1393: }
                   1394:
                   1395: #if    !defined(TN3270)
                   1396:     /*ARGSUSED*/
                   1397:     int
                   1398: shell(argc, argv)
                   1399:     int argc;
                   1400:     char *argv[];
                   1401: {
                   1402:     long oldrows, oldcols, newrows, newcols, err;
                   1403:
                   1404:     setcommandmode();
                   1405:
                   1406:     err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
                   1407:     switch(vfork()) {
                   1408:     case -1:
                   1409:        perror("Fork failed\n");
                   1410:        break;
                   1411:
                   1412:     case 0:
                   1413:        {
                   1414:            /*
                   1415:             * Fire up the shell in the child.
                   1416:             */
                   1417:            register char *shellp, *shellname;
1.3       niklas   1418:            extern char *strrchr();
1.1       deraadt  1419:
                   1420:            shellp = getenv("SHELL");
                   1421:            if (shellp == NULL)
                   1422:                shellp = "/bin/sh";
1.3       niklas   1423:            if ((shellname = strrchr(shellp, '/')) == 0)
1.1       deraadt  1424:                shellname = shellp;
                   1425:            else
                   1426:                shellname++;
                   1427:            if (argc > 1)
                   1428:                execl(shellp, shellname, "-c", &saveline[1], 0);
                   1429:            else
                   1430:                execl(shellp, shellname, 0);
                   1431:            perror("Execl");
                   1432:            _exit(1);
                   1433:        }
                   1434:     default:
                   1435:            (void)wait((int *)0);       /* Wait for the shell to complete */
                   1436:
                   1437:            if (TerminalWindowSize(&newrows, &newcols) && connected &&
                   1438:                (err || ((oldrows != newrows) || (oldcols != newcols)))) {
                   1439:                    sendnaws();
                   1440:            }
                   1441:            break;
                   1442:     }
                   1443:     return 1;
                   1444: }
                   1445: #else  /* !defined(TN3270) */
                   1446: extern int shell();
                   1447: #endif /* !defined(TN3270) */
                   1448:
                   1449:     /*VARARGS*/
                   1450:     static
                   1451: bye(argc, argv)
                   1452:     int  argc;         /* Number of arguments */
                   1453:     char *argv[];      /* arguments */
                   1454: {
                   1455:     extern int resettermname;
                   1456:
                   1457:     if (connected) {
                   1458:        (void) shutdown(net, 2);
                   1459:        printf("Connection closed.\n");
                   1460:        (void) NetClose(net);
                   1461:        connected = 0;
                   1462:        resettermname = 1;
                   1463: #if    defined(AUTHENTICATION)
                   1464:        auth_encrypt_connect(connected);
                   1465: #endif /* defined(AUTHENTICATION) */
                   1466:        /* reset options */
                   1467:        tninit();
                   1468: #if    defined(TN3270)
                   1469:        SetIn3270();            /* Get out of 3270 mode */
                   1470: #endif /* defined(TN3270) */
                   1471:     }
                   1472:     if ((argc != 2) || (strcmp(argv[1], "fromquit") != 0)) {
                   1473:        longjmp(toplevel, 1);
                   1474:        /* NOTREACHED */
                   1475:     }
                   1476:     return 1;                  /* Keep lint, etc., happy */
                   1477: }
                   1478:
                   1479: /*VARARGS*/
                   1480: quit()
                   1481: {
                   1482:        (void) call(bye, "bye", "fromquit", 0);
                   1483:        Exit(0);
                   1484:        /*NOTREACHED*/
                   1485: }
                   1486:
                   1487: /*VARARGS*/
                   1488:        int
                   1489: logout()
                   1490: {
                   1491:        send_do(TELOPT_LOGOUT, 1);
                   1492:        (void) netflush();
                   1493:        return 1;
                   1494: }
                   1495:
                   1496: 
                   1497: /*
                   1498:  * The SLC command.
                   1499:  */
                   1500:
                   1501: struct slclist {
                   1502:        char    *name;
                   1503:        char    *help;
                   1504:        void    (*handler)();
                   1505:        int     arg;
                   1506: };
                   1507:
                   1508: static void slc_help();
                   1509:
                   1510: struct slclist SlcList[] = {
                   1511:     { "export",        "Use local special character definitions",
                   1512:                                                slc_mode_export,        0 },
                   1513:     { "import",        "Use remote special character definitions",
                   1514:                                                slc_mode_import,        1 },
                   1515:     { "check", "Verify remote special character definitions",
                   1516:                                                slc_mode_import,        0 },
                   1517:     { "help",  0,                              slc_help,               0 },
                   1518:     { "?",     "Print help information",       slc_help,               0 },
                   1519:     { 0 },
                   1520: };
                   1521:
                   1522:     static void
                   1523: slc_help()
                   1524: {
                   1525:     struct slclist *c;
                   1526:
                   1527:     for (c = SlcList; c->name; c++) {
                   1528:        if (c->help) {
                   1529:            if (*c->help)
                   1530:                printf("%-15s %s\n", c->name, c->help);
                   1531:            else
                   1532:                printf("\n");
                   1533:        }
                   1534:     }
                   1535: }
                   1536:
                   1537:     static struct slclist *
                   1538: getslc(name)
                   1539:     char *name;
                   1540: {
                   1541:     return (struct slclist *)
                   1542:                genget(name, (char **) SlcList, sizeof(struct slclist));
                   1543: }
                   1544:
                   1545:     static
                   1546: slccmd(argc, argv)
                   1547:     int  argc;
                   1548:     char *argv[];
                   1549: {
                   1550:     struct slclist *c;
                   1551:
                   1552:     if (argc != 2) {
                   1553:        fprintf(stderr,
                   1554:            "Need an argument to 'slc' command.  'slc ?' for help.\n");
                   1555:        return 0;
                   1556:     }
                   1557:     c = getslc(argv[1]);
                   1558:     if (c == 0) {
1.3       niklas   1559:        fprintf(stderr, "'%s': unknown argument ('slc ?' for help).\n",
1.1       deraadt  1560:                                argv[1]);
1.3       niklas   1561:        return 0;
1.1       deraadt  1562:     }
                   1563:     if (Ambiguous(c)) {
1.3       niklas   1564:        fprintf(stderr, "'%s': ambiguous argument ('slc ?' for help).\n",
1.1       deraadt  1565:                                argv[1]);
1.3       niklas   1566:        return 0;
1.1       deraadt  1567:     }
                   1568:     (*c->handler)(c->arg);
                   1569:     slcstate();
                   1570:     return 1;
                   1571: }
                   1572: 
                   1573: /*
                   1574:  * The ENVIRON command.
                   1575:  */
                   1576:
                   1577: struct envlist {
                   1578:        char    *name;
                   1579:        char    *help;
                   1580:        void    (*handler)();
                   1581:        int     narg;
                   1582: };
                   1583:
                   1584: extern struct env_lst *
                   1585:        env_define P((unsigned char *, unsigned char *));
                   1586: extern void
                   1587:        env_undefine P((unsigned char *)),
                   1588:        env_export P((unsigned char *)),
                   1589:        env_unexport P((unsigned char *)),
                   1590:        env_send P((unsigned char *)),
                   1591: #if defined(OLD_ENVIRON) && defined(ENV_HACK)
                   1592:        env_varval P((unsigned char *)),
                   1593: #endif
                   1594:        env_list P((void));
                   1595: static void
                   1596:        env_help P((void));
                   1597:
                   1598: struct envlist EnvList[] = {
                   1599:     { "define",        "Define an environment variable",
                   1600:                                                (void (*)())env_define, 2 },
                   1601:     { "undefine", "Undefine an environment variable",
                   1602:                                                env_undefine,   1 },
                   1603:     { "export",        "Mark an environment variable for automatic export",
                   1604:                                                env_export,     1 },
                   1605:     { "unexport", "Don't mark an environment variable for automatic export",
                   1606:                                                env_unexport,   1 },
                   1607:     { "send",  "Send an environment variable", env_send,       1 },
                   1608:     { "list",  "List the current environment variables",
                   1609:                                                env_list,       0 },
                   1610: #if defined(OLD_ENVIRON) && defined(ENV_HACK)
                   1611:     { "varval", "Reverse VAR and VALUE (auto, right, wrong, status)",
                   1612:                                                env_varval,    1 },
                   1613: #endif
                   1614:     { "help",  0,                              env_help,               0 },
                   1615:     { "?",     "Print help information",       env_help,               0 },
                   1616:     { 0 },
                   1617: };
                   1618:
                   1619:     static void
                   1620: env_help()
                   1621: {
                   1622:     struct envlist *c;
                   1623:
                   1624:     for (c = EnvList; c->name; c++) {
                   1625:        if (c->help) {
                   1626:            if (*c->help)
                   1627:                printf("%-15s %s\n", c->name, c->help);
                   1628:            else
                   1629:                printf("\n");
                   1630:        }
                   1631:     }
                   1632: }
                   1633:
                   1634:     static struct envlist *
                   1635: getenvcmd(name)
                   1636:     char *name;
                   1637: {
                   1638:     return (struct envlist *)
                   1639:                genget(name, (char **) EnvList, sizeof(struct envlist));
                   1640: }
                   1641:
                   1642: env_cmd(argc, argv)
                   1643:     int  argc;
                   1644:     char *argv[];
                   1645: {
                   1646:     struct envlist *c;
                   1647:
                   1648:     if (argc < 2) {
                   1649:        fprintf(stderr,
                   1650:            "Need an argument to 'environ' command.  'environ ?' for help.\n");
                   1651:        return 0;
                   1652:     }
                   1653:     c = getenvcmd(argv[1]);
                   1654:     if (c == 0) {
1.3       niklas   1655:        fprintf(stderr, "'%s': unknown argument ('environ ?' for help).\n",
1.1       deraadt  1656:                                argv[1]);
1.3       niklas   1657:        return 0;
1.1       deraadt  1658:     }
                   1659:     if (Ambiguous(c)) {
1.3       niklas   1660:        fprintf(stderr, "'%s': ambiguous argument ('environ ?' for help).\n",
1.1       deraadt  1661:                                argv[1]);
1.3       niklas   1662:        return 0;
1.1       deraadt  1663:     }
                   1664:     if (c->narg + 2 != argc) {
                   1665:        fprintf(stderr,
                   1666:            "Need %s%d argument%s to 'environ %s' command.  'environ ?' for help.\n",
                   1667:                c->narg < argc + 2 ? "only " : "",
                   1668:                c->narg, c->narg == 1 ? "" : "s", c->name);
                   1669:        return 0;
                   1670:     }
                   1671:     (*c->handler)(argv[2], argv[3]);
                   1672:     return 1;
                   1673: }
                   1674:
                   1675: struct env_lst {
                   1676:        struct env_lst *next;   /* pointer to next structure */
                   1677:        struct env_lst *prev;   /* pointer to previous structure */
                   1678:        unsigned char *var;     /* pointer to variable name */
                   1679:        unsigned char *value;   /* pointer to variable value */
                   1680:        int export;             /* 1 -> export with default list of variables */
                   1681:        int welldefined;        /* A well defined variable */
                   1682: };
                   1683:
                   1684: struct env_lst envlisthead;
                   1685:
                   1686:        struct env_lst *
                   1687: env_find(var)
                   1688:        unsigned char *var;
                   1689: {
                   1690:        register struct env_lst *ep;
                   1691:
                   1692:        for (ep = envlisthead.next; ep; ep = ep->next) {
                   1693:                if (strcmp((char *)ep->var, (char *)var) == 0)
                   1694:                        return(ep);
                   1695:        }
                   1696:        return(NULL);
                   1697: }
                   1698:
                   1699:        void
                   1700: env_init()
                   1701: {
                   1702:        extern char **environ;
                   1703:        register char **epp, *cp;
                   1704:        register struct env_lst *ep;
1.3       niklas   1705:        extern char *strchr();
1.1       deraadt  1706:
                   1707:        for (epp = environ; *epp; epp++) {
1.3       niklas   1708:                if (cp = strchr(*epp, '=')) {
1.1       deraadt  1709:                        *cp = '\0';
                   1710:                        ep = env_define((unsigned char *)*epp,
                   1711:                                        (unsigned char *)cp+1);
                   1712:                        ep->export = 0;
                   1713:                        *cp = '=';
                   1714:                }
                   1715:        }
                   1716:        /*
                   1717:         * Special case for DISPLAY variable.  If it is ":0.0" or
                   1718:         * "unix:0.0", we have to get rid of "unix" and insert our
                   1719:         * hostname.
                   1720:         */
                   1721:        if ((ep = env_find("DISPLAY"))
                   1722:            && ((*ep->value == ':')
1.3       niklas   1723:                || (strncmp((char *)ep->value, "unix:", 5) == 0))) {
1.1       deraadt  1724:                char hbuf[256+1];
1.3       niklas   1725:                char *cp2 = strchr((char *)ep->value, ':');
1.1       deraadt  1726:
                   1727:                gethostname(hbuf, 256);
                   1728:                hbuf[256] = '\0';
                   1729:                cp = (char *)malloc(strlen(hbuf) + strlen(cp2) + 1);
                   1730:                sprintf((char *)cp, "%s%s", hbuf, cp2);
                   1731:                free(ep->value);
                   1732:                ep->value = (unsigned char *)cp;
                   1733:        }
                   1734:        /*
                   1735:         * If USER is not defined, but LOGNAME is, then add
                   1736:         * USER with the value from LOGNAME.  By default, we
                   1737:         * don't export the USER variable.
                   1738:         */
                   1739:        if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME"))) {
                   1740:                env_define((unsigned char *)"USER", ep->value);
                   1741:                env_unexport((unsigned char *)"USER");
                   1742:        }
                   1743:        env_export((unsigned char *)"DISPLAY");
                   1744:        env_export((unsigned char *)"PRINTER");
                   1745: }
                   1746:
                   1747:        struct env_lst *
                   1748: env_define(var, value)
                   1749:        unsigned char *var, *value;
                   1750: {
                   1751:        register struct env_lst *ep;
                   1752:
                   1753:        if (ep = env_find(var)) {
                   1754:                if (ep->var)
                   1755:                        free(ep->var);
                   1756:                if (ep->value)
                   1757:                        free(ep->value);
                   1758:        } else {
                   1759:                ep = (struct env_lst *)malloc(sizeof(struct env_lst));
                   1760:                ep->next = envlisthead.next;
                   1761:                envlisthead.next = ep;
                   1762:                ep->prev = &envlisthead;
                   1763:                if (ep->next)
                   1764:                        ep->next->prev = ep;
                   1765:        }
                   1766:        ep->welldefined = opt_welldefined(var);
                   1767:        ep->export = 1;
                   1768:        ep->var = (unsigned char *)strdup((char *)var);
                   1769:        ep->value = (unsigned char *)strdup((char *)value);
                   1770:        return(ep);
                   1771: }
                   1772:
                   1773:        void
                   1774: env_undefine(var)
                   1775:        unsigned char *var;
                   1776: {
                   1777:        register struct env_lst *ep;
                   1778:
                   1779:        if (ep = env_find(var)) {
                   1780:                ep->prev->next = ep->next;
                   1781:                if (ep->next)
                   1782:                        ep->next->prev = ep->prev;
                   1783:                if (ep->var)
                   1784:                        free(ep->var);
                   1785:                if (ep->value)
                   1786:                        free(ep->value);
                   1787:                free(ep);
                   1788:        }
                   1789: }
                   1790:
                   1791:        void
                   1792: env_export(var)
                   1793:        unsigned char *var;
                   1794: {
                   1795:        register struct env_lst *ep;
                   1796:
                   1797:        if (ep = env_find(var))
                   1798:                ep->export = 1;
                   1799: }
                   1800:
                   1801:        void
                   1802: env_unexport(var)
                   1803:        unsigned char *var;
                   1804: {
                   1805:        register struct env_lst *ep;
                   1806:
                   1807:        if (ep = env_find(var))
                   1808:                ep->export = 0;
                   1809: }
                   1810:
                   1811:        void
                   1812: env_send(var)
                   1813:        unsigned char *var;
                   1814: {
                   1815:        register struct env_lst *ep;
                   1816:
1.3       niklas   1817:        if (my_state_is_wont(TELOPT_NEW_ENVIRON)
1.1       deraadt  1818: #ifdef OLD_ENVIRON
                   1819:            && my_state_is_wont(TELOPT_OLD_ENVIRON)
                   1820: #endif
                   1821:                ) {
                   1822:                fprintf(stderr,
                   1823:                    "Cannot send '%s': Telnet ENVIRON option not enabled\n",
                   1824:                                                                        var);
                   1825:                return;
                   1826:        }
                   1827:        ep = env_find(var);
                   1828:        if (ep == 0) {
                   1829:                fprintf(stderr, "Cannot send '%s': variable not defined\n",
                   1830:                                                                        var);
                   1831:                return;
                   1832:        }
                   1833:        env_opt_start_info();
                   1834:        env_opt_add(ep->var);
                   1835:        env_opt_end(0);
                   1836: }
                   1837:
                   1838:        void
                   1839: env_list()
                   1840: {
                   1841:        register struct env_lst *ep;
                   1842:
                   1843:        for (ep = envlisthead.next; ep; ep = ep->next) {
                   1844:                printf("%c %-20s %s\n", ep->export ? '*' : ' ',
                   1845:                                        ep->var, ep->value);
                   1846:        }
                   1847: }
                   1848:
                   1849:        unsigned char *
                   1850: env_default(init, welldefined)
                   1851:        int init;
                   1852: {
                   1853:        static struct env_lst *nep = NULL;
                   1854:
                   1855:        if (init) {
                   1856:                nep = &envlisthead;
                   1857:                return;
                   1858:        }
                   1859:        if (nep) {
                   1860:                while (nep = nep->next) {
                   1861:                        if (nep->export && (nep->welldefined == welldefined))
                   1862:                                return(nep->var);
                   1863:                }
                   1864:        }
                   1865:        return(NULL);
                   1866: }
                   1867:
                   1868:        unsigned char *
                   1869: env_getvalue(var)
                   1870:        unsigned char *var;
                   1871: {
                   1872:        register struct env_lst *ep;
                   1873:
                   1874:        if (ep = env_find(var))
                   1875:                return(ep->value);
                   1876:        return(NULL);
                   1877: }
                   1878:
                   1879: #if defined(OLD_ENVIRON) && defined(ENV_HACK)
                   1880:        void
                   1881: env_varval(what)
                   1882:        unsigned char *what;
                   1883: {
                   1884:        extern int old_env_var, old_env_value, env_auto;
                   1885:        int len = strlen((char *)what);
                   1886:
                   1887:        if (len == 0)
                   1888:                goto unknown;
                   1889:
                   1890:        if (strncasecmp((char *)what, "status", len) == 0) {
                   1891:                if (env_auto)
                   1892:                        printf("%s%s", "VAR and VALUE are/will be ",
                   1893:                                        "determined automatically\n");
                   1894:                if (old_env_var == OLD_ENV_VAR)
                   1895:                        printf("VAR and VALUE set to correct definitions\n");
                   1896:                else
                   1897:                        printf("VAR and VALUE definitions are reversed\n");
                   1898:        } else if (strncasecmp((char *)what, "auto", len) == 0) {
                   1899:                env_auto = 1;
                   1900:                old_env_var = OLD_ENV_VALUE;
                   1901:                old_env_value = OLD_ENV_VAR;
                   1902:        } else if (strncasecmp((char *)what, "right", len) == 0) {
                   1903:                env_auto = 0;
                   1904:                old_env_var = OLD_ENV_VAR;
                   1905:                old_env_value = OLD_ENV_VALUE;
                   1906:        } else if (strncasecmp((char *)what, "wrong", len) == 0) {
                   1907:                env_auto = 0;
                   1908:                old_env_var = OLD_ENV_VALUE;
                   1909:                old_env_value = OLD_ENV_VAR;
                   1910:        } else {
                   1911: unknown:
                   1912:                printf("Unknown \"varval\" command. (\"auto\", \"right\", \"wrong\", \"status\")\n");
                   1913:        }
                   1914: }
                   1915: #endif
                   1916:
                   1917: #if    defined(AUTHENTICATION)
                   1918: /*
                   1919:  * The AUTHENTICATE command.
                   1920:  */
                   1921:
                   1922: struct authlist {
                   1923:        char    *name;
                   1924:        char    *help;
                   1925:        int     (*handler)();
                   1926:        int     narg;
                   1927: };
                   1928:
                   1929: extern int
1.3       niklas   1930:        auth_enable P((char *)),
                   1931:        auth_disable P((char *)),
1.1       deraadt  1932:        auth_status P((void));
                   1933: static int
                   1934:        auth_help P((void));
                   1935:
                   1936: struct authlist AuthList[] = {
                   1937:     { "status",        "Display current status of authentication information",
                   1938:                                                auth_status,    0 },
                   1939:     { "disable", "Disable an authentication type ('auth disable ?' for more)",
                   1940:                                                auth_disable,   1 },
                   1941:     { "enable", "Enable an authentication type ('auth enable ?' for more)",
                   1942:                                                auth_enable,    1 },
                   1943:     { "help",  0,                              auth_help,              0 },
                   1944:     { "?",     "Print help information",       auth_help,              0 },
                   1945:     { 0 },
                   1946: };
                   1947:
                   1948:     static int
                   1949: auth_help()
                   1950: {
                   1951:     struct authlist *c;
                   1952:
                   1953:     for (c = AuthList; c->name; c++) {
                   1954:        if (c->help) {
                   1955:            if (*c->help)
                   1956:                printf("%-15s %s\n", c->name, c->help);
                   1957:            else
                   1958:                printf("\n");
                   1959:        }
                   1960:     }
                   1961:     return 0;
                   1962: }
                   1963:
                   1964: auth_cmd(argc, argv)
                   1965:     int  argc;
                   1966:     char *argv[];
                   1967: {
                   1968:     struct authlist *c;
                   1969:
1.3       niklas   1970:     if (argc < 2) {
                   1971:        fprintf(stderr,
                   1972:            "Need an argument to 'auth' command.  'auth ?' for help.\n");
                   1973:        return 0;
                   1974:     }
                   1975:
1.1       deraadt  1976:     c = (struct authlist *)
                   1977:                genget(argv[1], (char **) AuthList, sizeof(struct authlist));
                   1978:     if (c == 0) {
1.3       niklas   1979:        fprintf(stderr, "'%s': unknown argument ('auth ?' for help).\n",
1.1       deraadt  1980:                                argv[1]);
1.3       niklas   1981:        return 0;
1.1       deraadt  1982:     }
                   1983:     if (Ambiguous(c)) {
1.3       niklas   1984:        fprintf(stderr, "'%s': ambiguous argument ('auth ?' for help).\n",
1.1       deraadt  1985:                                argv[1]);
1.3       niklas   1986:        return 0;
1.1       deraadt  1987:     }
                   1988:     if (c->narg + 2 != argc) {
                   1989:        fprintf(stderr,
                   1990:            "Need %s%d argument%s to 'auth %s' command.  'auth ?' for help.\n",
                   1991:                c->narg < argc + 2 ? "only " : "",
                   1992:                c->narg, c->narg == 1 ? "" : "s", c->name);
                   1993:        return 0;
                   1994:     }
                   1995:     return((*c->handler)(argv[2], argv[3]));
                   1996: }
                   1997: #endif
                   1998:
                   1999:
                   2000: #if    defined(unix) && defined(TN3270)
                   2001:     static void
                   2002: filestuff(fd)
                   2003:     int fd;
                   2004: {
                   2005:     int res;
                   2006:
                   2007: #ifdef F_GETOWN
                   2008:     setconnmode(0);
                   2009:     res = fcntl(fd, F_GETOWN, 0);
                   2010:     setcommandmode();
                   2011:
                   2012:     if (res == -1) {
                   2013:        perror("fcntl");
                   2014:        return;
                   2015:     }
                   2016:     printf("\tOwner is %d.\n", res);
                   2017: #endif
                   2018:
                   2019:     setconnmode(0);
                   2020:     res = fcntl(fd, F_GETFL, 0);
                   2021:     setcommandmode();
                   2022:
                   2023:     if (res == -1) {
                   2024:        perror("fcntl");
                   2025:        return;
                   2026:     }
                   2027: #ifdef notdef
                   2028:     printf("\tFlags are 0x%x: %s\n", res, decodeflags(res));
                   2029: #endif
                   2030: }
                   2031: #endif /* defined(unix) && defined(TN3270) */
                   2032:
                   2033: /*
                   2034:  * Print status about the connection.
                   2035:  */
                   2036:     /*ARGSUSED*/
                   2037:     static
                   2038: status(argc, argv)
                   2039:     int         argc;
                   2040:     char *argv[];
                   2041: {
                   2042:     if (connected) {
                   2043:        printf("Connected to %s.\n", hostname);
                   2044:        if ((argc < 2) || strcmp(argv[1], "notmuch")) {
                   2045:            int mode = getconnmode();
                   2046:
                   2047:            if (my_want_state_is_will(TELOPT_LINEMODE)) {
                   2048:                printf("Operating with LINEMODE option\n");
                   2049:                printf("%s line editing\n", (mode&MODE_EDIT) ? "Local" : "No");
                   2050:                printf("%s catching of signals\n",
                   2051:                                        (mode&MODE_TRAPSIG) ? "Local" : "No");
                   2052:                slcstate();
                   2053: #ifdef KLUDGELINEMODE
                   2054:            } else if (kludgelinemode && my_want_state_is_dont(TELOPT_SGA)) {
                   2055:                printf("Operating in obsolete linemode\n");
                   2056: #endif
                   2057:            } else {
                   2058:                printf("Operating in single character mode\n");
                   2059:                if (localchars)
                   2060:                    printf("Catching signals locally\n");
                   2061:            }
                   2062:            printf("%s character echo\n", (mode&MODE_ECHO) ? "Local" : "Remote");
                   2063:            if (my_want_state_is_will(TELOPT_LFLOW))
                   2064:                printf("%s flow control\n", (mode&MODE_FLOW) ? "Local" : "No");
                   2065:        }
                   2066:     } else {
                   2067:        printf("No connection.\n");
                   2068:     }
                   2069: #   if !defined(TN3270)
                   2070:     printf("Escape character is '%s'.\n", control(escape));
                   2071:     (void) fflush(stdout);
                   2072: #   else /* !defined(TN3270) */
                   2073:     if ((!In3270) && ((argc < 2) || strcmp(argv[1], "notmuch"))) {
                   2074:        printf("Escape character is '%s'.\n", control(escape));
                   2075:     }
                   2076: #   if defined(unix)
                   2077:     if ((argc >= 2) && !strcmp(argv[1], "everything")) {
                   2078:        printf("SIGIO received %d time%s.\n",
                   2079:                                sigiocount, (sigiocount == 1)? "":"s");
                   2080:        if (In3270) {
                   2081:            printf("Process ID %d, process group %d.\n",
                   2082:                                            getpid(), getpgrp(getpid()));
                   2083:            printf("Terminal input:\n");
                   2084:            filestuff(tin);
                   2085:            printf("Terminal output:\n");
                   2086:            filestuff(tout);
                   2087:            printf("Network socket:\n");
                   2088:            filestuff(net);
                   2089:        }
                   2090:     }
                   2091:     if (In3270 && transcom) {
                   2092:        printf("Transparent mode command is '%s'.\n", transcom);
                   2093:     }
                   2094: #   endif /* defined(unix) */
                   2095:     (void) fflush(stdout);
                   2096:     if (In3270) {
                   2097:        return 0;
                   2098:     }
                   2099: #   endif /* defined(TN3270) */
                   2100:     return 1;
                   2101: }
                   2102:
                   2103: #ifdef SIGINFO
                   2104: /*
                   2105:  * Function that gets called when SIGINFO is received.
                   2106:  */
                   2107: ayt_status()
                   2108: {
                   2109:     (void) call(status, "status", "notmuch", 0);
                   2110: }
                   2111: #endif
                   2112:
                   2113: unsigned long inet_addr();
                   2114:
                   2115:     int
                   2116: tn(argc, argv)
                   2117:     int argc;
                   2118:     char *argv[];
                   2119: {
1.5       niklas   2120:     register struct hostent *host = 0, *alias = 0;
1.1       deraadt  2121:     struct sockaddr_in sin;
1.5       niklas   2122:     struct sockaddr_in ladr;
1.1       deraadt  2123:     struct servent *sp = 0;
                   2124:     unsigned long temp;
                   2125:     extern char *inet_ntoa();
                   2126: #if    defined(IP_OPTIONS) && defined(IPPROTO_IP)
                   2127:     char *srp = 0, *strrchr();
                   2128:     unsigned long sourceroute(), srlen;
                   2129: #endif
1.5       niklas   2130:     char *cmd, *hostp = 0, *portp = 0, *user = 0, *aliasp = 0;
1.1       deraadt  2131:
                   2132:     /* clear the socket address prior to use */
1.3       niklas   2133:     memset((char *)&sin, 0, sizeof(sin));
1.1       deraadt  2134:
                   2135:     if (connected) {
                   2136:        printf("?Already connected to %s\n", hostname);
                   2137:        setuid(getuid());
                   2138:        return 0;
                   2139:     }
                   2140:     if (argc < 2) {
                   2141:        (void) strcpy(line, "open ");
                   2142:        printf("(to) ");
                   2143:        (void) fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin);
                   2144:        makeargv();
                   2145:        argc = margc;
                   2146:        argv = margv;
                   2147:     }
                   2148:     cmd = *argv;
                   2149:     --argc; ++argv;
                   2150:     while (argc) {
1.3       niklas   2151:        if (strcmp(*argv, "help") == 0 || isprefix(*argv, "?"))
1.1       deraadt  2152:            goto usage;
                   2153:        if (strcmp(*argv, "-l") == 0) {
                   2154:            --argc; ++argv;
                   2155:            if (argc == 0)
                   2156:                goto usage;
                   2157:            user = *argv++;
                   2158:            --argc;
                   2159:            continue;
                   2160:        }
1.5       niklas   2161:        if (strcmp(*argv, "-b") == 0) {
                   2162:            --argc; ++argv;
                   2163:            if (argc == 0)
                   2164:                goto usage;
                   2165:            aliasp = *argv++;
                   2166:            --argc;
                   2167:            continue;
                   2168:        }
1.1       deraadt  2169:        if (strcmp(*argv, "-a") == 0) {
                   2170:            --argc; ++argv;
                   2171:            autologin = 1;
                   2172:            continue;
                   2173:        }
                   2174:        if (hostp == 0) {
                   2175:            hostp = *argv++;
                   2176:            --argc;
                   2177:            continue;
                   2178:        }
                   2179:        if (portp == 0) {
                   2180:            portp = *argv++;
                   2181:            --argc;
                   2182:            continue;
                   2183:        }
                   2184:     usage:
                   2185:        printf("usage: %s [-l user] [-a] host-name [port]\n", cmd);
                   2186:        setuid(getuid());
                   2187:        return 0;
                   2188:     }
                   2189:     if (hostp == 0)
                   2190:        goto usage;
                   2191:
                   2192: #if    defined(IP_OPTIONS) && defined(IPPROTO_IP)
                   2193:     if (hostp[0] == '@' || hostp[0] == '!') {
                   2194:        if ((hostname = strrchr(hostp, ':')) == NULL)
                   2195:            hostname = strrchr(hostp, '@');
                   2196:        hostname++;
                   2197:        srp = 0;
                   2198:        temp = sourceroute(hostp, &srp, &srlen);
                   2199:        if (temp == 0) {
                   2200:            herror(srp);
                   2201:            setuid(getuid());
                   2202:            return 0;
                   2203:        } else if (temp == -1) {
                   2204:            printf("Bad source route option: %s\n", hostp);
                   2205:            setuid(getuid());
                   2206:            return 0;
                   2207:        } else {
                   2208:            sin.sin_addr.s_addr = temp;
                   2209:            sin.sin_family = AF_INET;
                   2210:        }
                   2211:     } else {
                   2212: #endif
                   2213:        temp = inet_addr(hostp);
                   2214:        if (temp != INADDR_NONE) {
                   2215:            sin.sin_addr.s_addr = temp;
                   2216:            sin.sin_family = AF_INET;
1.4       deraadt  2217:            host = gethostbyaddr((char *)&temp, sizeof(temp), AF_INET);
                   2218:            if (host)
                   2219:                (void) strcpy(_hostname, host->h_name);
                   2220:            else
                   2221:                (void) strcpy(_hostname, hostp);
1.1       deraadt  2222:            hostname = _hostname;
                   2223:        } else {
                   2224:            host = gethostbyname(hostp);
                   2225:            if (host) {
                   2226:                sin.sin_family = host->h_addrtype;
                   2227: #if    defined(h_addr)         /* In 4.3, this is a #define */
1.3       niklas   2228:                memmove((caddr_t)&sin.sin_addr,
1.1       deraadt  2229:                                host->h_addr_list[0], host->h_length);
                   2230: #else  /* defined(h_addr) */
1.3       niklas   2231:                memmove((caddr_t)&sin.sin_addr, host->h_addr, host->h_length);
1.1       deraadt  2232: #endif /* defined(h_addr) */
                   2233:                strncpy(_hostname, host->h_name, sizeof(_hostname));
                   2234:                _hostname[sizeof(_hostname)-1] = '\0';
                   2235:                hostname = _hostname;
                   2236:            } else {
                   2237:                herror(hostp);
1.3       niklas   2238:                setuid(getuid());
1.1       deraadt  2239:                return 0;
                   2240:            }
                   2241:        }
                   2242: #if    defined(IP_OPTIONS) && defined(IPPROTO_IP)
                   2243:     }
                   2244: #endif
                   2245:     if (portp) {
                   2246:        if (*portp == '-') {
                   2247:            portp++;
                   2248:            telnetport = 1;
                   2249:        } else
                   2250:            telnetport = 0;
                   2251:        sin.sin_port = atoi(portp);
                   2252:        if (sin.sin_port == 0) {
                   2253:            sp = getservbyname(portp, "tcp");
                   2254:            if (sp)
                   2255:                sin.sin_port = sp->s_port;
                   2256:            else {
                   2257:                printf("%s: bad port number\n", portp);
1.3       niklas   2258:                setuid(getuid());
1.1       deraadt  2259:                return 0;
                   2260:            }
                   2261:        } else {
                   2262: #if    !defined(htons)
                   2263:            u_short htons P((unsigned short));
                   2264: #endif /* !defined(htons) */
                   2265:            sin.sin_port = htons(sin.sin_port);
                   2266:        }
                   2267:     } else {
                   2268:        if (sp == 0) {
                   2269:            sp = getservbyname("telnet", "tcp");
                   2270:            if (sp == 0) {
                   2271:                fprintf(stderr, "telnet: tcp/telnet: unknown service\n");
1.3       niklas   2272:                setuid(getuid());
1.1       deraadt  2273:                return 0;
                   2274:            }
                   2275:            sin.sin_port = sp->s_port;
                   2276:        }
                   2277:        telnetport = 1;
                   2278:     }
                   2279:     printf("Trying %s...\n", inet_ntoa(sin.sin_addr));
                   2280:     do {
                   2281:        net = socket(AF_INET, SOCK_STREAM, 0);
                   2282:        setuid(getuid());
                   2283:        if (net < 0) {
                   2284:            perror("telnet: socket");
                   2285:            return 0;
                   2286:        }
1.5       niklas   2287:        if (aliasp) {
                   2288:            memset ((caddr_t)&ladr, 0, sizeof (ladr));
                   2289:            temp = inet_addr(aliasp);
                   2290:            if (temp != INADDR_NONE) {
                   2291:                ladr.sin_addr.s_addr = temp;
                   2292:                ladr.sin_family = AF_INET;
                   2293:                alias = gethostbyaddr((char *)&temp, sizeof(temp), AF_INET);
                   2294:            } else {
                   2295:                alias = gethostbyname(aliasp);
                   2296:                if (alias) {
                   2297:                    ladr.sin_family = alias->h_addrtype;
                   2298: #if    defined(h_addr)         /* In 4.3, this is a #define */
                   2299:                    memmove((caddr_t)&ladr.sin_addr,
                   2300:                        alias->h_addr_list[0], alias->h_length);
                   2301: #else  /* defined(h_addr) */
                   2302:                    memmove((caddr_t)&ladr.sin_addr, alias->h_addr,
                   2303:                        alias->h_length);
                   2304: #endif /* defined(h_addr) */
                   2305:                } else {
                   2306:                    herror(aliasp);
                   2307:                    return 0;
                   2308:                }
                   2309:            }
                   2310:             ladr.sin_port = htons(0);
                   2311:
                   2312:             if (bind (net, (struct sockaddr *)&ladr, sizeof(ladr)) < 0) {
                   2313:                 perror(aliasp);;
                   2314:                 (void) close(net);   /* dump descriptor */
                   2315:                return 0;
                   2316:             }
                   2317:         }
                   2318:  #if   defined(IP_OPTIONS) && defined(IPPROTO_IP)
1.1       deraadt  2319:        if (srp && setsockopt(net, IPPROTO_IP, IP_OPTIONS, (char *)srp, srlen) < 0)
                   2320:                perror("setsockopt (IP_OPTIONS)");
                   2321: #endif
                   2322: #if    defined(IPPROTO_IP) && defined(IP_TOS)
                   2323:        {
                   2324: # if   defined(HAS_GETTOS)
                   2325:            struct tosent *tp;
                   2326:            if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
                   2327:                tos = tp->t_tos;
                   2328: # endif
                   2329:            if (tos < 0)
1.3       niklas   2330:                tos = IPTOS_LOWDELAY;   /* Low Delay bit */
1.1       deraadt  2331:            if (tos
                   2332:                && (setsockopt(net, IPPROTO_IP, IP_TOS,
                   2333:                    (char *)&tos, sizeof(int)) < 0)
                   2334:                && (errno != ENOPROTOOPT))
                   2335:                    perror("telnet: setsockopt (IP_TOS) (ignored)");
                   2336:        }
                   2337: #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
                   2338:
                   2339:        if (debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) {
                   2340:                perror("setsockopt (SO_DEBUG)");
                   2341:        }
                   2342:
                   2343:        if (connect(net, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
                   2344: #if    defined(h_addr)         /* In 4.3, this is a #define */
                   2345:            if (host && host->h_addr_list[1]) {
                   2346:                int oerrno = errno;
                   2347:
                   2348:                fprintf(stderr, "telnet: connect to address %s: ",
                   2349:                                                inet_ntoa(sin.sin_addr));
                   2350:                errno = oerrno;
                   2351:                perror((char *)0);
                   2352:                host->h_addr_list++;
1.3       niklas   2353:                memmove((caddr_t)&sin.sin_addr,
1.1       deraadt  2354:                        host->h_addr_list[0], host->h_length);
                   2355:                (void) NetClose(net);
                   2356:                continue;
                   2357:            }
                   2358: #endif /* defined(h_addr) */
                   2359:            perror("telnet: Unable to connect to remote host");
                   2360:            return 0;
                   2361:        }
                   2362:        connected++;
                   2363: #if    defined(AUTHENTICATION)
                   2364:        auth_encrypt_connect(connected);
                   2365: #endif /* defined(AUTHENTICATION) */
                   2366:     } while (connected == 0);
                   2367:     cmdrc(hostp, hostname);
                   2368:     if (autologin && user == NULL) {
                   2369:        struct passwd *pw;
                   2370:
                   2371:        user = getenv("USER");
                   2372:        if (user == NULL ||
                   2373:            (pw = getpwnam(user)) && pw->pw_uid != getuid()) {
                   2374:                if (pw = getpwuid(getuid()))
                   2375:                        user = pw->pw_name;
                   2376:                else
                   2377:                        user = NULL;
                   2378:        }
                   2379:     }
                   2380:     if (user) {
                   2381:        env_define((unsigned char *)"USER", (unsigned char *)user);
                   2382:        env_export((unsigned char *)"USER");
                   2383:     }
                   2384:     (void) call(status, "status", "notmuch", 0);
                   2385:     if (setjmp(peerdied) == 0)
                   2386:        telnet(user);
                   2387:     (void) NetClose(net);
                   2388:     ExitString("Connection closed by foreign host.\n",1);
                   2389:     /*NOTREACHED*/
                   2390: }
                   2391:
                   2392: #define HELPINDENT (sizeof ("connect"))
                   2393:
                   2394: static char
                   2395:        openhelp[] =    "connect to a site",
                   2396:        closehelp[] =   "close current connection",
                   2397:        logouthelp[] =  "forcibly logout remote user and close the connection",
                   2398:        quithelp[] =    "exit telnet",
                   2399:        statushelp[] =  "print status information",
                   2400:        helphelp[] =    "print help information",
                   2401:        sendhelp[] =    "transmit special characters ('send ?' for more)",
                   2402:        sethelp[] =     "set operating parameters ('set ?' for more)",
                   2403:        unsethelp[] =   "unset operating parameters ('unset ?' for more)",
                   2404:        togglestring[] ="toggle operating parameters ('toggle ?' for more)",
                   2405:        slchelp[] =     "change state of special charaters ('slc ?' for more)",
                   2406:        displayhelp[] = "display operating parameters",
                   2407: #if    defined(TN3270) && defined(unix)
                   2408:        transcomhelp[] = "specify Unix command for transparent mode pipe",
                   2409: #endif /* defined(TN3270) && defined(unix) */
                   2410: #if    defined(AUTHENTICATION)
                   2411:        authhelp[] =    "turn on (off) authentication ('auth ?' for more)",
                   2412: #endif
                   2413: #if    defined(unix)
                   2414:        zhelp[] =       "suspend telnet",
                   2415: #endif /* defined(unix) */
                   2416:        shellhelp[] =   "invoke a subshell",
                   2417:        envhelp[] =     "change environment variables ('environ ?' for more)",
                   2418:        modestring[] = "try to enter line or character mode ('mode ?' for more)";
                   2419:
                   2420: static int     help();
                   2421:
                   2422: static Command cmdtab[] = {
                   2423:        { "close",      closehelp,      bye,            1 },
                   2424:        { "logout",     logouthelp,     logout,         1 },
                   2425:        { "display",    displayhelp,    display,        0 },
                   2426:        { "mode",       modestring,     modecmd,        0 },
                   2427:        { "open",       openhelp,       tn,             0 },
                   2428:        { "quit",       quithelp,       quit,           0 },
                   2429:        { "send",       sendhelp,       sendcmd,        0 },
                   2430:        { "set",        sethelp,        setcmd,         0 },
                   2431:        { "unset",      unsethelp,      unsetcmd,       0 },
                   2432:        { "status",     statushelp,     status,         0 },
                   2433:        { "toggle",     togglestring,   toggle,         0 },
                   2434:        { "slc",        slchelp,        slccmd,         0 },
                   2435: #if    defined(TN3270) && defined(unix)
                   2436:        { "transcom",   transcomhelp,   settranscom,    0 },
                   2437: #endif /* defined(TN3270) && defined(unix) */
                   2438: #if    defined(AUTHENTICATION)
                   2439:        { "auth",       authhelp,       auth_cmd,       0 },
                   2440: #endif
                   2441: #if    defined(unix)
                   2442:        { "z",          zhelp,          suspend,        0 },
                   2443: #endif /* defined(unix) */
                   2444: #if    defined(TN3270)
                   2445:        { "!",          shellhelp,      shell,          1 },
                   2446: #else
                   2447:        { "!",          shellhelp,      shell,          0 },
                   2448: #endif
                   2449:        { "environ",    envhelp,        env_cmd,        0 },
                   2450:        { "?",          helphelp,       help,           0 },
1.6     ! deraadt  2451: #if    defined(SKEY)
        !          2452:        { "skey",       NULL,           skey_calc,      0 },
        !          2453: #endif
1.1       deraadt  2454:        0
                   2455: };
                   2456:
                   2457: static char    crmodhelp[] =   "deprecated command -- use 'toggle crmod' instead";
                   2458: static char    escapehelp[] =  "deprecated command -- use 'set escape' instead";
                   2459:
                   2460: static Command cmdtab2[] = {
                   2461:        { "help",       0,              help,           0 },
                   2462:        { "escape",     escapehelp,     setescape,      0 },
                   2463:        { "crmod",      crmodhelp,      togcrmod,       0 },
                   2464:        0
                   2465: };
                   2466:
                   2467:
                   2468: /*
                   2469:  * Call routine with argc, argv set from args (terminated by 0).
                   2470:  */
                   2471:
                   2472:     /*VARARGS1*/
                   2473:     static
                   2474: call(va_alist)
                   2475:     va_dcl
                   2476: {
                   2477:     va_list ap;
                   2478:     typedef int (*intrtn_t)();
                   2479:     intrtn_t routine;
                   2480:     char *args[100];
                   2481:     int argno = 0;
                   2482:
                   2483:     va_start(ap);
                   2484:     routine = (va_arg(ap, intrtn_t));
                   2485:     while ((args[argno++] = va_arg(ap, char *)) != 0) {
                   2486:        ;
                   2487:     }
                   2488:     va_end(ap);
                   2489:     return (*routine)(argno-1, args);
                   2490: }
                   2491:
                   2492:
                   2493:     static Command *
                   2494: getcmd(name)
                   2495:     char *name;
                   2496: {
                   2497:     Command *cm;
                   2498:
                   2499:     if (cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command)))
                   2500:        return cm;
                   2501:     return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));
                   2502: }
                   2503:
                   2504:     void
                   2505: command(top, tbuf, cnt)
                   2506:     int top;
                   2507:     char *tbuf;
                   2508:     int cnt;
                   2509: {
                   2510:     register Command *c;
                   2511:
                   2512:     setcommandmode();
                   2513:     if (!top) {
                   2514:        putchar('\n');
                   2515: #if    defined(unix)
                   2516:     } else {
                   2517:        (void) signal(SIGINT, SIG_DFL);
                   2518:        (void) signal(SIGQUIT, SIG_DFL);
                   2519: #endif /* defined(unix) */
                   2520:     }
                   2521:     for (;;) {
                   2522:        if (rlogin == _POSIX_VDISABLE)
                   2523:                printf("%s> ", prompt);
                   2524:        if (tbuf) {
                   2525:            register char *cp;
                   2526:            cp = line;
                   2527:            while (cnt > 0 && (*cp++ = *tbuf++) != '\n')
                   2528:                cnt--;
                   2529:            tbuf = 0;
                   2530:            if (cp == line || *--cp != '\n' || cp == line)
                   2531:                goto getline;
                   2532:            *cp = '\0';
                   2533:            if (rlogin == _POSIX_VDISABLE)
1.3       niklas   2534:                printf("%s\n", line);
1.1       deraadt  2535:        } else {
                   2536:        getline:
                   2537:            if (rlogin != _POSIX_VDISABLE)
                   2538:                printf("%s> ", prompt);
                   2539:            if (fgets(line, sizeof(line), stdin) == NULL) {
                   2540:                if (feof(stdin) || ferror(stdin)) {
                   2541:                    (void) quit();
                   2542:                    /*NOTREACHED*/
                   2543:                }
                   2544:                break;
                   2545:            }
                   2546:        }
                   2547:        if (line[0] == 0)
                   2548:            break;
                   2549:        makeargv();
                   2550:        if (margv[0] == 0) {
                   2551:            break;
                   2552:        }
                   2553:        c = getcmd(margv[0]);
                   2554:        if (Ambiguous(c)) {
                   2555:            printf("?Ambiguous command\n");
                   2556:            continue;
                   2557:        }
                   2558:        if (c == 0) {
                   2559:            printf("?Invalid command\n");
                   2560:            continue;
                   2561:        }
                   2562:        if (c->needconnect && !connected) {
                   2563:            printf("?Need to be connected first.\n");
                   2564:            continue;
                   2565:        }
                   2566:        if ((*c->handler)(margc, margv)) {
                   2567:            break;
                   2568:        }
                   2569:     }
                   2570:     if (!top) {
                   2571:        if (!connected) {
                   2572:            longjmp(toplevel, 1);
                   2573:            /*NOTREACHED*/
                   2574:        }
                   2575: #if    defined(TN3270)
                   2576:        if (shell_active == 0) {
                   2577:            setconnmode(0);
                   2578:        }
                   2579: #else  /* defined(TN3270) */
                   2580:        setconnmode(0);
                   2581: #endif /* defined(TN3270) */
                   2582:     }
                   2583: }
                   2584: 
                   2585: /*
                   2586:  * Help command.
                   2587:  */
                   2588:        static
                   2589: help(argc, argv)
                   2590:        int argc;
                   2591:        char *argv[];
                   2592: {
                   2593:        register Command *c;
                   2594:
                   2595:        if (argc == 1) {
                   2596:                printf("Commands may be abbreviated.  Commands are:\n\n");
                   2597:                for (c = cmdtab; c->name; c++)
                   2598:                        if (c->help) {
                   2599:                                printf("%-*s\t%s\n", HELPINDENT, c->name,
                   2600:                                                                    c->help);
                   2601:                        }
                   2602:                return 0;
                   2603:        }
                   2604:        while (--argc > 0) {
                   2605:                register char *arg;
                   2606:                arg = *++argv;
                   2607:                c = getcmd(arg);
                   2608:                if (Ambiguous(c))
                   2609:                        printf("?Ambiguous help command %s\n", arg);
                   2610:                else if (c == (Command *)0)
                   2611:                        printf("?Invalid help command %s\n", arg);
                   2612:                else
                   2613:                        printf("%s\n", c->help);
                   2614:        }
                   2615:        return 0;
                   2616: }
                   2617:
                   2618: static char *rcname = 0;
                   2619: static char rcbuf[128];
                   2620:
                   2621: cmdrc(m1, m2)
                   2622:        char *m1, *m2;
                   2623: {
                   2624:     register Command *c;
                   2625:     FILE *rcfile;
                   2626:     int gotmachine = 0;
                   2627:     int l1 = strlen(m1);
                   2628:     int l2 = strlen(m2);
                   2629:     char m1save[64];
                   2630:
                   2631:     if (skiprc)
                   2632:        return;
                   2633:
                   2634:     strcpy(m1save, m1);
                   2635:     m1 = m1save;
                   2636:
                   2637:     if (rcname == 0) {
                   2638:        rcname = getenv("HOME");
                   2639:        if (rcname)
                   2640:            strcpy(rcbuf, rcname);
                   2641:        else
                   2642:            rcbuf[0] = '\0';
                   2643:        strcat(rcbuf, "/.telnetrc");
                   2644:        rcname = rcbuf;
                   2645:     }
                   2646:
                   2647:     if ((rcfile = fopen(rcname, "r")) == 0) {
                   2648:        return;
                   2649:     }
                   2650:
                   2651:     for (;;) {
                   2652:        if (fgets(line, sizeof(line), rcfile) == NULL)
                   2653:            break;
                   2654:        if (line[0] == 0)
                   2655:            break;
                   2656:        if (line[0] == '#')
                   2657:            continue;
                   2658:        if (gotmachine) {
                   2659:            if (!isspace(line[0]))
                   2660:                gotmachine = 0;
                   2661:        }
                   2662:        if (gotmachine == 0) {
                   2663:            if (isspace(line[0]))
                   2664:                continue;
                   2665:            if (strncasecmp(line, m1, l1) == 0)
                   2666:                strncpy(line, &line[l1], sizeof(line) - l1);
                   2667:            else if (strncasecmp(line, m2, l2) == 0)
                   2668:                strncpy(line, &line[l2], sizeof(line) - l2);
                   2669:            else if (strncasecmp(line, "DEFAULT", 7) == 0)
                   2670:                strncpy(line, &line[7], sizeof(line) - 7);
                   2671:            else
                   2672:                continue;
                   2673:            if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n')
                   2674:                continue;
                   2675:            gotmachine = 1;
                   2676:        }
                   2677:        makeargv();
                   2678:        if (margv[0] == 0)
                   2679:            continue;
                   2680:        c = getcmd(margv[0]);
                   2681:        if (Ambiguous(c)) {
                   2682:            printf("?Ambiguous command: %s\n", margv[0]);
                   2683:            continue;
                   2684:        }
                   2685:        if (c == 0) {
                   2686:            printf("?Invalid command: %s\n", margv[0]);
                   2687:            continue;
                   2688:        }
                   2689:        /*
                   2690:         * This should never happen...
                   2691:         */
                   2692:        if (c->needconnect && !connected) {
                   2693:            printf("?Need to be connected first for %s.\n", margv[0]);
                   2694:            continue;
                   2695:        }
                   2696:        (*c->handler)(margc, margv);
                   2697:     }
                   2698:     fclose(rcfile);
                   2699: }
                   2700:
                   2701: #if    defined(IP_OPTIONS) && defined(IPPROTO_IP)
                   2702:
                   2703: /*
                   2704:  * Source route is handed in as
                   2705:  *     [!]@hop1@hop2...[@|:]dst
                   2706:  * If the leading ! is present, it is a
                   2707:  * strict source route, otherwise it is
                   2708:  * assmed to be a loose source route.
                   2709:  *
                   2710:  * We fill in the source route option as
                   2711:  *     hop1,hop2,hop3...dest
                   2712:  * and return a pointer to hop1, which will
                   2713:  * be the address to connect() to.
                   2714:  *
                   2715:  * Arguments:
                   2716:  *     arg:    pointer to route list to decipher
                   2717:  *
                   2718:  *     cpp:    If *cpp is not equal to NULL, this is a
                   2719:  *             pointer to a pointer to a character array
                   2720:  *             that should be filled in with the option.
                   2721:  *
                   2722:  *     lenp:   pointer to an integer that contains the
                   2723:  *             length of *cpp if *cpp != NULL.
                   2724:  *
                   2725:  * Return values:
                   2726:  *
                   2727:  *     Returns the address of the host to connect to.  If the
                   2728:  *     return value is -1, there was a syntax error in the
                   2729:  *     option, either unknown characters, or too many hosts.
                   2730:  *     If the return value is 0, one of the hostnames in the
                   2731:  *     path is unknown, and *cpp is set to point to the bad
                   2732:  *     hostname.
                   2733:  *
                   2734:  *     *cpp:   If *cpp was equal to NULL, it will be filled
                   2735:  *             in with a pointer to our static area that has
                   2736:  *             the option filled in.  This will be 32bit aligned.
1.3       niklas   2737:  *
1.1       deraadt  2738:  *     *lenp:  This will be filled in with how long the option
                   2739:  *             pointed to by *cpp is.
1.3       niklas   2740:  *
1.1       deraadt  2741:  */
                   2742:        unsigned long
                   2743: sourceroute(arg, cpp, lenp)
                   2744:        char    *arg;
                   2745:        char    **cpp;
                   2746:        int     *lenp;
                   2747: {
                   2748:        static char lsr[44];
                   2749: #ifdef sysV88
                   2750:        static IOPTN ipopt;
                   2751: #endif
                   2752:        char *cp, *cp2, *lsrp, *lsrep;
                   2753:        register int tmp;
                   2754:        struct in_addr sin_addr;
                   2755:        register struct hostent *host = 0;
                   2756:        register char c;
                   2757:
                   2758:        /*
                   2759:         * Verify the arguments, and make sure we have
                   2760:         * at least 7 bytes for the option.
                   2761:         */
                   2762:        if (cpp == NULL || lenp == NULL)
                   2763:                return((unsigned long)-1);
                   2764:        if (*cpp != NULL && *lenp < 7)
                   2765:                return((unsigned long)-1);
                   2766:        /*
                   2767:         * Decide whether we have a buffer passed to us,
                   2768:         * or if we need to use our own static buffer.
                   2769:         */
                   2770:        if (*cpp) {
                   2771:                lsrp = *cpp;
                   2772:                lsrep = lsrp + *lenp;
                   2773:        } else {
                   2774:                *cpp = lsrp = lsr;
                   2775:                lsrep = lsrp + 44;
                   2776:        }
                   2777:
                   2778:        cp = arg;
                   2779:
                   2780:        /*
                   2781:         * Next, decide whether we have a loose source
                   2782:         * route or a strict source route, and fill in
                   2783:         * the begining of the option.
                   2784:         */
                   2785: #ifndef        sysV88
                   2786:        if (*cp == '!') {
                   2787:                cp++;
                   2788:                *lsrp++ = IPOPT_SSRR;
                   2789:        } else
                   2790:                *lsrp++ = IPOPT_LSRR;
                   2791: #else
                   2792:        if (*cp == '!') {
                   2793:                cp++;
                   2794:                ipopt.io_type = IPOPT_SSRR;
                   2795:        } else
                   2796:                ipopt.io_type = IPOPT_LSRR;
                   2797: #endif
                   2798:
                   2799:        if (*cp != '@')
                   2800:                return((unsigned long)-1);
                   2801:
                   2802: #ifndef        sysV88
                   2803:        lsrp++;         /* skip over length, we'll fill it in later */
                   2804:        *lsrp++ = 4;
                   2805: #endif
                   2806:
                   2807:        cp++;
                   2808:
                   2809:        sin_addr.s_addr = 0;
                   2810:
                   2811:        for (c = 0;;) {
                   2812:                if (c == ':')
                   2813:                        cp2 = 0;
                   2814:                else for (cp2 = cp; c = *cp2; cp2++) {
                   2815:                        if (c == ',') {
                   2816:                                *cp2++ = '\0';
                   2817:                                if (*cp2 == '@')
                   2818:                                        cp2++;
                   2819:                        } else if (c == '@') {
                   2820:                                *cp2++ = '\0';
                   2821:                        } else if (c == ':') {
                   2822:                                *cp2++ = '\0';
                   2823:                        } else
                   2824:                                continue;
                   2825:                        break;
                   2826:                }
                   2827:                if (!c)
                   2828:                        cp2 = 0;
                   2829:
                   2830:                if ((tmp = inet_addr(cp)) != INADDR_NONE) {
                   2831:                        sin_addr.s_addr = tmp;
                   2832:                } else if (host = gethostbyname(cp)) {
                   2833: #if    defined(h_addr)
1.3       niklas   2834:                        memmove((caddr_t)&sin_addr,
1.1       deraadt  2835:                                host->h_addr_list[0], host->h_length);
                   2836: #else
1.3       niklas   2837:                        memmove((caddr_t)&sin_addr, host->h_addr, host->h_length);
1.1       deraadt  2838: #endif
                   2839:                } else {
                   2840:                        *cpp = cp;
                   2841:                        return(0);
                   2842:                }
1.3       niklas   2843:                memmove(lsrp, (char *)&sin_addr, 4);
1.1       deraadt  2844:                lsrp += 4;
                   2845:                if (cp2)
                   2846:                        cp = cp2;
                   2847:                else
                   2848:                        break;
                   2849:                /*
                   2850:                 * Check to make sure there is space for next address
                   2851:                 */
                   2852:                if (lsrp + 4 > lsrep)
                   2853:                        return((unsigned long)-1);
                   2854:        }
                   2855: #ifndef        sysV88
                   2856:        if ((*(*cpp+IPOPT_OLEN) = lsrp - *cpp) <= 7) {
                   2857:                *cpp = 0;
                   2858:                *lenp = 0;
                   2859:                return((unsigned long)-1);
                   2860:        }
                   2861:        *lsrp++ = IPOPT_NOP; /* 32 bit word align it */
                   2862:        *lenp = lsrp - *cpp;
                   2863: #else
                   2864:        ipopt.io_len = lsrp - *cpp;
                   2865:        if (ipopt.io_len <= 5) {                /* Is 3 better ? */
                   2866:                *cpp = 0;
                   2867:                *lenp = 0;
                   2868:                return((unsigned long)-1);
                   2869:        }
                   2870:        *lenp = sizeof(ipopt);
                   2871:        *cpp = (char *) &ipopt;
                   2872: #endif
                   2873:        return(sin_addr.s_addr);
                   2874: }
                   2875: #endif