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