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