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