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