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