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