Home | History | Annotate | Line # | Download | only in telnet
telnet.c revision 1.17
      1 /*	$NetBSD: telnet.c,v 1.17 2001/03/04 01:51:05 assar Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988, 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)telnet.c	8.4 (Berkeley) 5/30/95";
     40 #else
     41 __RCSID("$NetBSD: telnet.c,v 1.17 2001/03/04 01:51:05 assar Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <sys/param.h>
     46 
     47 #if	defined(unix)
     48 #include <signal.h>
     49 #include <termcap.h>
     50 #include <unistd.h>
     51 /* By the way, we need to include curses.h before telnet.h since,
     52  * among other things, telnet.h #defines 'DO', which is a variable
     53  * declared in curses.h.
     54  */
     55 #endif	/* defined(unix) */
     56 
     57 #include <arpa/telnet.h>
     58 
     59 #include <ctype.h>
     60 
     61 #include "ring.h"
     62 
     63 #include "defines.h"
     64 #include "externs.h"
     65 #include "types.h"
     66 #include "general.h"
     67 
     68 #include <libtelnet/misc.h>
     69 #ifdef AUTHENTICATION
     70 #include <libtelnet/auth.h>
     71 #endif
     72 #ifdef ENCRYPTION
     73 #include <libtelnet/encrypt.h>
     74 #endif
     75 
     76 
     77 #define	strip(x) ((my_want_state_is_wont(TELOPT_BINARY)) ? ((x)&0x7f) : (x))
     79 
     80 static unsigned char	subbuffer[SUBBUFSIZE],
     81 			*subpointer, *subend;	 /* buffer for sub-options */
     82 #define	SB_CLEAR()	subpointer = subbuffer;
     83 #define	SB_TERM()	{ subend = subpointer; SB_CLEAR(); }
     84 #define	SB_ACCUM(c)	if (subpointer < (subbuffer+sizeof subbuffer)) { \
     85 				*subpointer++ = (c); \
     86 			}
     87 
     88 #define	SB_GET()	((*subpointer++)&0xff)
     89 #define	SB_PEEK()	((*subpointer)&0xff)
     90 #define	SB_EOF()	(subpointer >= subend)
     91 #define	SB_LEN()	(subend - subpointer)
     92 
     93 char	options[256];		/* The combined options */
     94 char	do_dont_resp[256];
     95 char	will_wont_resp[256];
     96 
     97 int
     98 	eight = 0,
     99 	autologin = 0,	/* Autologin anyone? */
    100 	skiprc = 0,
    101 	connected,
    102 	showoptions,
    103 	In3270,		/* Are we in 3270 mode? */
    104 	ISend,		/* trying to send network data in */
    105 	debug = 0,
    106 	crmod,
    107 	netdata,	/* Print out network data flow */
    108 	crlf,		/* Should '\r' be mapped to <CR><LF> (or <CR><NUL>)? */
    109 #if	defined(TN3270)
    110 	noasynchtty = 0,/* User specified "-noasynch" on command line */
    111 	noasynchnet = 0,/* User specified "-noasynch" on command line */
    112 	askedSGA = 0,	/* We have talked about suppress go ahead */
    113 #endif	/* defined(TN3270) */
    114 	telnetport,
    115 	SYNCHing,	/* we are in TELNET SYNCH mode */
    116 	flushout,	/* flush output */
    117 	autoflush = 0,	/* flush output when interrupting? */
    118 	autosynch,	/* send interrupt characters with SYNCH? */
    119 	localflow,	/* we handle flow control locally */
    120 	restartany,	/* if flow control enabled, restart on any character */
    121 	localchars,	/* we recognize interrupt/quit */
    122 	donelclchars,	/* the user has set "localchars" */
    123 	donebinarytoggle,	/* the user has put us in binary */
    124 	dontlecho,	/* do we suppress local echoing right now? */
    125 	globalmode,
    126 	doaddrlookup = 1, /* do a reverse address lookup? */
    127 	clienteof = 0;
    128 
    129 char *prompt = 0;
    130 
    131 cc_t escape;
    132 cc_t rlogin;
    133 #ifdef	KLUDGELINEMODE
    134 cc_t echoc;
    135 #endif
    136 
    137 /*
    138  * Telnet receiver states for fsm
    139  */
    140 #define	TS_DATA		0
    141 #define	TS_IAC		1
    142 #define	TS_WILL		2
    143 #define	TS_WONT		3
    144 #define	TS_DO		4
    145 #define	TS_DONT		5
    146 #define	TS_CR		6
    147 #define	TS_SB		7		/* sub-option collection */
    148 #define	TS_SE		8		/* looking for sub-option end */
    149 
    150 static int	telrcv_state;
    151 #ifdef	OLD_ENVIRON
    152 unsigned char telopt_environ = TELOPT_NEW_ENVIRON;
    153 #else
    154 # define telopt_environ TELOPT_NEW_ENVIRON
    155 #endif
    156 
    157 jmp_buf	toplevel = { 0 };
    158 jmp_buf	peerdied;
    159 
    160 int	flushline;
    161 int	linemode;
    162 
    163 #ifdef	KLUDGELINEMODE
    164 int	kludgelinemode = 1;
    165 #endif
    166 
    167 static void dooption P((int));
    168 static void dontoption P((int));
    169 static void suboption P((void));
    170 static int telsnd P((void));
    171 static void netclear P((void));
    172 static void doflush P((void));
    173 
    174 /*
    175  * The following are some clocks used to decide how to interpret
    176  * the relationship between various variables.
    177  */
    178 
    179 Clocks clocks;
    180 
    181 #ifdef	notdef
    183 Modelist modelist[] = {
    184 	{ "telnet command mode", COMMAND_LINE },
    185 	{ "character-at-a-time mode", 0 },
    186 	{ "character-at-a-time mode (local echo)", LOCAL_ECHO|LOCAL_CHARS },
    187 	{ "line-by-line mode (remote echo)", LINE | LOCAL_CHARS },
    188 	{ "line-by-line mode", LINE | LOCAL_ECHO | LOCAL_CHARS },
    189 	{ "line-by-line mode (local echoing suppressed)", LINE | LOCAL_CHARS },
    190 	{ "3270 mode", 0 },
    191 };
    192 #endif
    193 
    194 
    195 /*
    197  * Initialize telnet environment.
    198  */
    199 
    200     void
    201 init_telnet()
    202 {
    203     env_init();
    204 
    205     SB_CLEAR();
    206     ClearArray(options);
    207 
    208     connected = In3270 = ISend = localflow = donebinarytoggle = 0;
    209 #if	defined(AUTHENTICATION) || defined(ENCRYPTION)
    210     auth_encrypt_connect(connected);
    211 #endif	/* defined(AUTHENTICATION) || defined(ENCRYPTION) */
    212     restartany = -1;
    213 
    214     SYNCHing = 0;
    215 
    216     /* Don't change NetTrace */
    217 
    218     escape = CONTROL(']');
    219     rlogin = _POSIX_VDISABLE;
    220 #ifdef	KLUDGELINEMODE
    221     echoc = CONTROL('E');
    222 #endif
    223 
    224     flushline = 1;
    225     telrcv_state = TS_DATA;
    226 }
    227 
    228 
    230 #ifdef	notdef
    231 #include <varargs.h>
    232 
    233     /*VARARGS*/
    234     static void
    235 printring(va_alist)
    236     va_dcl
    237 {
    238     va_list ap;
    239     char buffer[100];		/* where things go */
    240     char *ptr;
    241     char *format;
    242     char *string;
    243     Ring *ring;
    244     int i;
    245 
    246     va_start(ap);
    247 
    248     ring = va_arg(ap, Ring *);
    249     format = va_arg(ap, char *);
    250     ptr = buffer;
    251 
    252     while ((i = *format++) != 0) {
    253 	if (i == '%') {
    254 	    i = *format++;
    255 	    switch (i) {
    256 	    case 'c':
    257 		*ptr++ = va_arg(ap, int);
    258 		break;
    259 	    case 's':
    260 		string = va_arg(ap, char *);
    261 		ring_supply_data(ring, buffer, ptr-buffer);
    262 		ring_supply_data(ring, string, strlen(string));
    263 		ptr = buffer;
    264 		break;
    265 	    case 0:
    266 		ExitString("printring: trailing %%.\n", 1);
    267 		/*NOTREACHED*/
    268 	    default:
    269 		ExitString("printring: unknown format character.\n", 1);
    270 		/*NOTREACHED*/
    271 	    }
    272 	} else {
    273 	    *ptr++ = i;
    274 	}
    275     }
    276     ring_supply_data(ring, buffer, ptr-buffer);
    277 }
    278 #endif
    279 
    280 /*
    281  * These routines are in charge of sending option negotiations
    282  * to the other side.
    283  *
    284  * The basic idea is that we send the negotiation if either side
    285  * is in disagreement as to what the current state should be.
    286  */
    287 
    288     void
    289 send_do(c, init)
    290     register int c, init;
    291 {
    292     if (init) {
    293 	if (((do_dont_resp[c] == 0) && my_state_is_do(c)) ||
    294 				my_want_state_is_do(c))
    295 	    return;
    296 	set_my_want_state_do(c);
    297 	do_dont_resp[c]++;
    298     }
    299     NET2ADD(IAC, DO);
    300     NETADD(c);
    301     printoption("SENT", DO, c);
    302 }
    303 
    304     void
    305 send_dont(c, init)
    306     register int c, init;
    307 {
    308     if (init) {
    309 	if (((do_dont_resp[c] == 0) && my_state_is_dont(c)) ||
    310 				my_want_state_is_dont(c))
    311 	    return;
    312 	set_my_want_state_dont(c);
    313 	do_dont_resp[c]++;
    314     }
    315     NET2ADD(IAC, DONT);
    316     NETADD(c);
    317     printoption("SENT", DONT, c);
    318 }
    319 
    320     void
    321 send_will(c, init)
    322     register int c, init;
    323 {
    324     if (init) {
    325 	if (((will_wont_resp[c] == 0) && my_state_is_will(c)) ||
    326 				my_want_state_is_will(c))
    327 	    return;
    328 	set_my_want_state_will(c);
    329 	will_wont_resp[c]++;
    330     }
    331     NET2ADD(IAC, WILL);
    332     NETADD(c);
    333     printoption("SENT", WILL, c);
    334 }
    335 
    336     void
    337 send_wont(c, init)
    338     register int c, init;
    339 {
    340     if (init) {
    341 	if (((will_wont_resp[c] == 0) && my_state_is_wont(c)) ||
    342 				my_want_state_is_wont(c))
    343 	    return;
    344 	set_my_want_state_wont(c);
    345 	will_wont_resp[c]++;
    346     }
    347     NET2ADD(IAC, WONT);
    348     NETADD(c);
    349     printoption("SENT", WONT, c);
    350 }
    351 
    352 
    353 	void
    354 willoption(option)
    355 	int option;
    356 {
    357 	int new_state_ok = 0;
    358 
    359 	if (do_dont_resp[option]) {
    360 	    --do_dont_resp[option];
    361 	    if (do_dont_resp[option] && my_state_is_do(option))
    362 		--do_dont_resp[option];
    363 	}
    364 
    365 	if ((do_dont_resp[option] == 0) && my_want_state_is_dont(option)) {
    366 
    367 	    switch (option) {
    368 
    369 	    case TELOPT_ECHO:
    370 #	    if defined(TN3270)
    371 		/*
    372 		 * The following is a pain in the rear-end.
    373 		 * Various IBM servers (some versions of Wiscnet,
    374 		 * possibly Fibronics/Spartacus, and who knows who
    375 		 * else) will NOT allow us to send "DO SGA" too early
    376 		 * in the setup proceedings.  On the other hand,
    377 		 * 4.2 servers (telnetd) won't set SGA correctly.
    378 		 * So, we are stuck.  Empirically (but, based on
    379 		 * a VERY small sample), the IBM servers don't send
    380 		 * out anything about ECHO, so we postpone our sending
    381 		 * "DO SGA" until we see "WILL ECHO" (which 4.2 servers
    382 		 * DO send).
    383 		  */
    384 		{
    385 		    if (askedSGA == 0) {
    386 			askedSGA = 1;
    387 			if (my_want_state_is_dont(TELOPT_SGA))
    388 			    send_do(TELOPT_SGA, 1);
    389 		    }
    390 		}
    391 		    /* Fall through */
    392 	    case TELOPT_EOR:
    393 #endif	    /* defined(TN3270) */
    394 	    case TELOPT_BINARY:
    395 	    case TELOPT_SGA:
    396 		settimer(modenegotiated);
    397 		/* FALL THROUGH */
    398 	    case TELOPT_STATUS:
    399 #if	defined(AUTHENTICATION)
    400 	    case TELOPT_AUTHENTICATION:
    401 #ifdef	ENCRYPTION
    402 	    case TELOPT_ENCRYPT:
    403 #endif	/* ENCRYPTION */
    404 #endif
    405 		new_state_ok = 1;
    406 		break;
    407 
    408 	    case TELOPT_TM:
    409 		if (flushout)
    410 		    flushout = 0;
    411 		/*
    412 		 * Special case for TM.  If we get back a WILL,
    413 		 * pretend we got back a WONT.
    414 		 */
    415 		set_my_want_state_dont(option);
    416 		set_my_state_dont(option);
    417 		return;			/* Never reply to TM will's/wont's */
    418 
    419 	    case TELOPT_LINEMODE:
    420 	    default:
    421 		break;
    422 	    }
    423 
    424 	    if (new_state_ok) {
    425 		set_my_want_state_do(option);
    426 		send_do(option, 0);
    427 		setconnmode(0);		/* possibly set new tty mode */
    428 	    } else {
    429 		do_dont_resp[option]++;
    430 		send_dont(option, 0);
    431 	    }
    432 	}
    433 	set_my_state_do(option);
    434 #ifdef	ENCRYPTION
    435 	if (option == TELOPT_ENCRYPT)
    436 		encrypt_send_support();
    437 #endif	/* ENCRYPTION */
    438 }
    439 
    440 	void
    441 wontoption(option)
    442 	int option;
    443 {
    444 	if (do_dont_resp[option]) {
    445 	    --do_dont_resp[option];
    446 	    if (do_dont_resp[option] && my_state_is_dont(option))
    447 		--do_dont_resp[option];
    448 	}
    449 
    450 	if ((do_dont_resp[option] == 0) && my_want_state_is_do(option)) {
    451 
    452 	    switch (option) {
    453 
    454 #ifdef	KLUDGELINEMODE
    455 	    case TELOPT_SGA:
    456 		if (!kludgelinemode)
    457 		    break;
    458 		/* FALL THROUGH */
    459 #endif
    460 	    case TELOPT_ECHO:
    461 		settimer(modenegotiated);
    462 		break;
    463 
    464 	    case TELOPT_TM:
    465 		if (flushout)
    466 		    flushout = 0;
    467 		set_my_want_state_dont(option);
    468 		set_my_state_dont(option);
    469 		return;		/* Never reply to TM will's/wont's */
    470 
    471 	    default:
    472 		break;
    473 	    }
    474 	    set_my_want_state_dont(option);
    475 	    if (my_state_is_do(option))
    476 		send_dont(option, 0);
    477 	    setconnmode(0);			/* Set new tty mode */
    478 	} else if (option == TELOPT_TM) {
    479 	    /*
    480 	     * Special case for TM.
    481 	     */
    482 	    if (flushout)
    483 		flushout = 0;
    484 	    set_my_want_state_dont(option);
    485 	}
    486 	set_my_state_dont(option);
    487 }
    488 
    489 	static void
    490 dooption(option)
    491 	int option;
    492 {
    493 	int new_state_ok = 0;
    494 
    495 	if (will_wont_resp[option]) {
    496 	    --will_wont_resp[option];
    497 	    if (will_wont_resp[option] && my_state_is_will(option))
    498 		--will_wont_resp[option];
    499 	}
    500 
    501 	if (will_wont_resp[option] == 0) {
    502 	  if (my_want_state_is_wont(option)) {
    503 
    504 	    switch (option) {
    505 
    506 	    case TELOPT_TM:
    507 		/*
    508 		 * Special case for TM.  We send a WILL, but pretend
    509 		 * we sent WONT.
    510 		 */
    511 		send_will(option, 0);
    512 		set_my_want_state_wont(TELOPT_TM);
    513 		set_my_state_wont(TELOPT_TM);
    514 		return;
    515 
    516 #	if defined(TN3270)
    517 	    case TELOPT_EOR:		/* end of record */
    518 #	endif	/* defined(TN3270) */
    519 	    case TELOPT_BINARY:		/* binary mode */
    520 	    case TELOPT_NAWS:		/* window size */
    521 	    case TELOPT_TSPEED:		/* terminal speed */
    522 	    case TELOPT_LFLOW:		/* local flow control */
    523 	    case TELOPT_TTYPE:		/* terminal type option */
    524 	    case TELOPT_SGA:		/* no big deal */
    525 #ifdef	ENCRYPTION
    526 	    case TELOPT_ENCRYPT:	/* encryption variable option */
    527 #endif	/* ENCRYPTION */
    528 		new_state_ok = 1;
    529 		break;
    530 
    531 	    case TELOPT_NEW_ENVIRON:	/* New environment variable option */
    532 #ifdef	OLD_ENVIRON
    533 		if (my_state_is_will(TELOPT_OLD_ENVIRON))
    534 			send_wont(TELOPT_OLD_ENVIRON, 1); /* turn off the old */
    535 		goto env_common;
    536 	    case TELOPT_OLD_ENVIRON:	/* Old environment variable option */
    537 		if (my_state_is_will(TELOPT_NEW_ENVIRON))
    538 			break;		/* Don't enable if new one is in use! */
    539 	    env_common:
    540 		telopt_environ = option;
    541 #endif
    542 		new_state_ok = 1;
    543 		break;
    544 
    545 #if	defined(AUTHENTICATION)
    546 	    case TELOPT_AUTHENTICATION:
    547 		if (autologin)
    548 			new_state_ok = 1;
    549 		break;
    550 #endif
    551 
    552 	    case TELOPT_XDISPLOC:	/* X Display location */
    553 		if (env_getvalue((unsigned char *)"DISPLAY"))
    554 		    new_state_ok = 1;
    555 		break;
    556 
    557 	    case TELOPT_LINEMODE:
    558 #ifdef	KLUDGELINEMODE
    559 		kludgelinemode = 0;
    560 		send_do(TELOPT_SGA, 1);
    561 #endif
    562 		set_my_want_state_will(TELOPT_LINEMODE);
    563 		send_will(option, 0);
    564 		set_my_state_will(TELOPT_LINEMODE);
    565 		slc_init();
    566 		return;
    567 
    568 	    case TELOPT_ECHO:		/* We're never going to echo... */
    569 	    default:
    570 		break;
    571 	    }
    572 
    573 	    if (new_state_ok) {
    574 		set_my_want_state_will(option);
    575 		send_will(option, 0);
    576 		setconnmode(0);			/* Set new tty mode */
    577 	    } else {
    578 		will_wont_resp[option]++;
    579 		send_wont(option, 0);
    580 	    }
    581 	  } else {
    582 	    /*
    583 	     * Handle options that need more things done after the
    584 	     * other side has acknowledged the option.
    585 	     */
    586 	    switch (option) {
    587 	    case TELOPT_LINEMODE:
    588 #ifdef	KLUDGELINEMODE
    589 		kludgelinemode = 0;
    590 		send_do(TELOPT_SGA, 1);
    591 #endif
    592 		set_my_state_will(option);
    593 		slc_init();
    594 		send_do(TELOPT_SGA, 0);
    595 		return;
    596 	    }
    597 	  }
    598 	}
    599 	set_my_state_will(option);
    600 }
    601 
    602 	static void
    603 dontoption(option)
    604 	int option;
    605 {
    606 
    607 	if (will_wont_resp[option]) {
    608 	    --will_wont_resp[option];
    609 	    if (will_wont_resp[option] && my_state_is_wont(option))
    610 		--will_wont_resp[option];
    611 	}
    612 
    613 	if ((will_wont_resp[option] == 0) && my_want_state_is_will(option)) {
    614 	    switch (option) {
    615 	    case TELOPT_LINEMODE:
    616 		linemode = 0;	/* put us back to the default state */
    617 		break;
    618 #ifdef	OLD_ENVIRON
    619 	    case TELOPT_NEW_ENVIRON:
    620 		/*
    621 		 * The new environ option wasn't recognized, try
    622 		 * the old one.
    623 		 */
    624 		send_will(TELOPT_OLD_ENVIRON, 1);
    625 		telopt_environ = TELOPT_OLD_ENVIRON;
    626 		break;
    627 #endif
    628 	    }
    629 	    /* we always accept a DONT */
    630 	    set_my_want_state_wont(option);
    631 	    if (my_state_is_will(option))
    632 		send_wont(option, 0);
    633 	    setconnmode(0);			/* Set new tty mode */
    634 	}
    635 	set_my_state_wont(option);
    636 }
    637 
    638 /*
    639  * Given a buffer returned by tgetent(), this routine will turn
    640  * the pipe seperated list of names in the buffer into an array
    641  * of pointers to null terminated names.  We toss out any bad,
    642  * duplicate, or verbose names (names with spaces).
    643  */
    644 
    645 static char *name_unknown = "UNKNOWN";
    646 static char *unknown[] = { 0, 0 };
    647 
    648 	char **
    649 mklist(buf, name)
    650 	char *buf, *name;
    651 {
    652 	register int n;
    653 	register char c, *cp, **argvp, *cp2, **argv, **avt;
    654 
    655 	if (name) {
    656 		if ((int)strlen(name) > 40) {
    657 			name = 0;
    658 			unknown[0] = name_unknown;
    659 		} else {
    660 			unknown[0] = name;
    661 			upcase(name);
    662 		}
    663 	} else
    664 		unknown[0] = name_unknown;
    665 	/*
    666 	 * Count up the number of names.
    667 	 */
    668 	for (n = 1, cp = buf; *cp && *cp != ':'; cp++) {
    669 		if (*cp == '|')
    670 			n++;
    671 	}
    672 	/*
    673 	 * Allocate an array to put the name pointers into
    674 	 */
    675 	argv = (char **)malloc((n+3)*sizeof(char *));
    676 	if (argv == 0)
    677 		return(unknown);
    678 
    679 	/*
    680 	 * Fill up the array of pointers to names.
    681 	 */
    682 	*argv = 0;
    683 	argvp = argv+1;
    684 	n = 0;
    685 	for (cp = cp2 = buf; (c = *cp);  cp++) {
    686 		if (c == '|' || c == ':') {
    687 			*cp++ = '\0';
    688 			/*
    689 			 * Skip entries that have spaces or are over 40
    690 			 * characters long.  If this is our environment
    691 			 * name, then put it up front.  Otherwise, as
    692 			 * long as this is not a duplicate name (case
    693 			 * insensitive) add it to the list.
    694 			 */
    695 			if (n || (cp - cp2 > 41))
    696 				;
    697 			else if (name && (strncasecmp(name, cp2, cp-cp2) == 0))
    698 				*argv = cp2;
    699 			else if (is_unique(cp2, argv+1, argvp))
    700 				*argvp++ = cp2;
    701 			if (c == ':')
    702 				break;
    703 			/*
    704 			 * Skip multiple delimiters. Reset cp2 to
    705 			 * the beginning of the next name. Reset n,
    706 			 * the flag for names with spaces.
    707 			 */
    708 			while ((c = *cp) == '|')
    709 				cp++;
    710 			cp2 = cp;
    711 			n = 0;
    712 		}
    713 		/*
    714 		 * Skip entries with spaces or non-ascii values.
    715 		 * Convert lower case letters to upper case.
    716 		 */
    717 		if ((c == ' ') || !isascii(c))
    718 			n = 1;
    719 		else if (islower((unsigned char)c))
    720 			*cp = toupper(c);
    721 	}
    722 
    723 	/*
    724 	 * Check for an old V6 2 character name.  If the second
    725 	 * name points to the beginning of the buffer, and is
    726 	 * only 2 characters long, move it to the end of the array.
    727 	 */
    728 	if ((argv[1] == buf) && (strlen(argv[1]) == 2)) {
    729 		--argvp;
    730 		for (avt = &argv[1]; avt < argvp; avt++)
    731 			*avt = *(avt+1);
    732 		*argvp++ = buf;
    733 	}
    734 
    735 	/*
    736 	 * Duplicate last name, for TTYPE option, and null
    737 	 * terminate the array.  If we didn't find a match on
    738 	 * our terminal name, put that name at the beginning.
    739 	 */
    740 	cp = *(argvp-1);
    741 	*argvp++ = cp;
    742 	*argvp = 0;
    743 
    744 	if (*argv == 0) {
    745 		if (name)
    746 			*argv = name;
    747 		else {
    748 			--argvp;
    749 			for (avt = argv; avt < argvp; avt++)
    750 				*avt = *(avt+1);
    751 		}
    752 	}
    753 	if (*argv)
    754 		return(argv);
    755 	else
    756 		return(unknown);
    757 }
    758 
    759 	int
    760 is_unique(name, as, ae)
    761 	register char *name, **as, **ae;
    762 {
    763 	register char **ap;
    764 	register int n;
    765 
    766 	n = strlen(name) + 1;
    767 	for (ap = as; ap < ae; ap++)
    768 		if (strncasecmp(*ap, name, n) == 0)
    769 			return(0);
    770 	return (1);
    771 }
    772 
    773 #ifdef	TERMCAP
    774 char *termbuf;
    775 
    776 	/*ARGSUSED*/
    777 	int
    778 setup_term(tname, fd, errp)
    779 	char *tname;
    780 	int fd, *errp;
    781 {
    782 	char zz[1024], *zz_ptr;
    783 	char *ext_tc, *newptr;
    784 
    785 	if ((termbuf = (char *) malloc(1024)) == NULL)
    786 		goto error;
    787 
    788 	if (tgetent(termbuf, tname) == 1) {
    789 		  /* check for ZZ capability, which indicates termcap truncated */
    790 		zz_ptr = zz;
    791 		if (tgetstr("ZZ", &zz_ptr) != NULL) {
    792 			  /* it was, fish back the full termcap */
    793 			sscanf(zz, "%p", &ext_tc);
    794 			if ((newptr = (char *) realloc(termbuf,
    795 						       strlen(ext_tc) + 1))
    796 			    == NULL) {
    797 				goto error;
    798 			}
    799 
    800 			strcpy(newptr, ext_tc);
    801 			termbuf = newptr;
    802 		}
    803 
    804 		if (errp)
    805 			*errp = 1;
    806 		return(0);
    807 	}
    808   error:
    809 	if (errp)
    810 		*errp = 0;
    811 	return(-1);
    812 }
    813 #else
    814 #define	termbuf	ttytype
    815 extern char ttytype[];
    816 #endif
    817 
    818 int resettermname = 1;
    819 
    820 	char *
    821 gettermname()
    822 {
    823 	char *tname;
    824 	static char **tnamep = 0;
    825 	static char **next;
    826 	int err;
    827 
    828 	if (resettermname) {
    829 		resettermname = 0;
    830 		if (tnamep && tnamep != unknown)
    831 			free(tnamep);
    832 		if ((tname = (char *)env_getvalue((unsigned char *)"TERM")) &&
    833 				(setup_term(tname, 1, &err) == 0)) {
    834 			tnamep = mklist(termbuf, tname);
    835 		} else {
    836 			if (tname && ((int)strlen(tname) <= 40)) {
    837 				unknown[0] = tname;
    838 				upcase(tname);
    839 			} else
    840 				unknown[0] = name_unknown;
    841 			tnamep = unknown;
    842 		}
    843 		next = tnamep;
    844 	}
    845 	if (*next == 0)
    846 		next = tnamep;
    847 	return(*next++);
    848 }
    849 /*
    850  * suboption()
    851  *
    852  *	Look at the sub-option buffer, and try to be helpful to the other
    853  * side.
    854  *
    855  *	Currently we recognize:
    856  *
    857  *		Terminal type, send request.
    858  *		Terminal speed (send request).
    859  *		Local flow control (is request).
    860  *		Linemode
    861  */
    862 
    863     static void
    864 suboption()
    865 {
    866     unsigned char subchar;
    867 
    868     printsub('<', subbuffer, SB_LEN()+2);
    869     switch (subchar = SB_GET()) {
    870     case TELOPT_TTYPE:
    871 	if (my_want_state_is_wont(TELOPT_TTYPE))
    872 	    return;
    873 	if (SB_EOF() || SB_GET() != TELQUAL_SEND) {
    874 	    return;
    875 	} else {
    876 	    char *name;
    877 	    unsigned char temp[50];
    878 	    int len;
    879 
    880 #if	defined(TN3270)
    881 	    if (tn3270_ttype()) {
    882 		return;
    883 	    }
    884 #endif	/* defined(TN3270) */
    885 	    name = gettermname();
    886 	    len = strlen(name) + 4 + 2;
    887 	    if (len < NETROOM()) {
    888 		sprintf((char *)temp, "%c%c%c%c%s%c%c", IAC, SB, TELOPT_TTYPE,
    889 				TELQUAL_IS, name, IAC, SE);
    890 		ring_supply_data(&netoring, temp, len);
    891 		printsub('>', &temp[2], len-2);
    892 	    } else {
    893 		ExitString("No room in buffer for terminal type.\n", 1);
    894 		/*NOTREACHED*/
    895 	    }
    896 	}
    897 	break;
    898     case TELOPT_TSPEED:
    899 	if (my_want_state_is_wont(TELOPT_TSPEED))
    900 	    return;
    901 	if (SB_EOF())
    902 	    return;
    903 	if (SB_GET() == TELQUAL_SEND) {
    904 	    long ospeed, ispeed;
    905 	    unsigned char temp[50];
    906 	    int len;
    907 
    908 	    TerminalSpeeds(&ispeed, &ospeed);
    909 
    910 	    sprintf((char *)temp, "%c%c%c%c%ld,%ld%c%c", IAC, SB, TELOPT_TSPEED,
    911 		    TELQUAL_IS, (long)ospeed, (long)ispeed, IAC, SE);
    912 	    len = strlen((char *)temp+4) + 4;	/* temp[3] is 0 ... */
    913 
    914 	    if (len < NETROOM()) {
    915 		ring_supply_data(&netoring, temp, len);
    916 		printsub('>', temp+2, len - 2);
    917 	    }
    918 /*@*/	    else printf("lm_will: not enough room in buffer\n");
    919 	}
    920 	break;
    921     case TELOPT_LFLOW:
    922 	if (my_want_state_is_wont(TELOPT_LFLOW))
    923 	    return;
    924 	if (SB_EOF())
    925 	    return;
    926 	switch(SB_GET()) {
    927 	case LFLOW_RESTART_ANY:
    928 	    restartany = 1;
    929 	    break;
    930 	case LFLOW_RESTART_XON:
    931 	    restartany = 0;
    932 	    break;
    933 	case LFLOW_ON:
    934 	    localflow = 1;
    935 	    break;
    936 	case LFLOW_OFF:
    937 	    localflow = 0;
    938 	    break;
    939 	default:
    940 	    return;
    941 	}
    942 	setcommandmode();
    943 	setconnmode(0);
    944 	break;
    945 
    946     case TELOPT_LINEMODE:
    947 	if (my_want_state_is_wont(TELOPT_LINEMODE))
    948 	    return;
    949 	if (SB_EOF())
    950 	    return;
    951 	switch (SB_GET()) {
    952 	case WILL:
    953 	    lm_will(subpointer, SB_LEN());
    954 	    break;
    955 	case WONT:
    956 	    lm_wont(subpointer, SB_LEN());
    957 	    break;
    958 	case DO:
    959 	    lm_do(subpointer, SB_LEN());
    960 	    break;
    961 	case DONT:
    962 	    lm_dont(subpointer, SB_LEN());
    963 	    break;
    964 	case LM_SLC:
    965 	    slc(subpointer, SB_LEN());
    966 	    break;
    967 	case LM_MODE:
    968 	    lm_mode(subpointer, SB_LEN(), 0);
    969 	    break;
    970 	default:
    971 	    break;
    972 	}
    973 	break;
    974 
    975 #ifdef	OLD_ENVIRON
    976     case TELOPT_OLD_ENVIRON:
    977 #endif
    978     case TELOPT_NEW_ENVIRON:
    979 	if (SB_EOF())
    980 	    return;
    981 	switch(SB_PEEK()) {
    982 	case TELQUAL_IS:
    983 	case TELQUAL_INFO:
    984 	    if (my_want_state_is_dont(subchar))
    985 		return;
    986 	    break;
    987 	case TELQUAL_SEND:
    988 	    if (my_want_state_is_wont(subchar)) {
    989 		return;
    990 	    }
    991 	    break;
    992 	default:
    993 	    return;
    994 	}
    995 	env_opt(subpointer, SB_LEN());
    996 	break;
    997 
    998     case TELOPT_XDISPLOC:
    999 	if (my_want_state_is_wont(TELOPT_XDISPLOC))
   1000 	    return;
   1001 	if (SB_EOF())
   1002 	    return;
   1003 	if (SB_GET() == TELQUAL_SEND) {
   1004 	    unsigned char temp[50], *dp;
   1005 	    int len;
   1006 
   1007 	    if ((dp = env_getvalue((unsigned char *)"DISPLAY")) == NULL) {
   1008 		/*
   1009 		 * Something happened, we no longer have a DISPLAY
   1010 		 * variable.  So, turn off the option.
   1011 		 */
   1012 		send_wont(TELOPT_XDISPLOC, 1);
   1013 		break;
   1014 	    }
   1015 	    sprintf((char *)temp, "%c%c%c%c%s%c%c", IAC, SB, TELOPT_XDISPLOC,
   1016 		    TELQUAL_IS, dp, IAC, SE);
   1017 	    len = strlen((char *)temp+4) + 4;	/* temp[3] is 0 ... */
   1018 
   1019 	    if (len < NETROOM()) {
   1020 		ring_supply_data(&netoring, temp, len);
   1021 		printsub('>', temp+2, len - 2);
   1022 	    }
   1023 /*@*/	    else printf("lm_will: not enough room in buffer\n");
   1024 	}
   1025 	break;
   1026 
   1027 #if	defined(AUTHENTICATION)
   1028 	case TELOPT_AUTHENTICATION: {
   1029 		if (!autologin)
   1030 			break;
   1031 		if (SB_EOF())
   1032 			return;
   1033 		switch(SB_GET()) {
   1034 		case TELQUAL_IS:
   1035 			if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
   1036 				return;
   1037 			auth_is(subpointer, SB_LEN());
   1038 			break;
   1039 		case TELQUAL_SEND:
   1040 			if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
   1041 				return;
   1042 			auth_send(subpointer, SB_LEN());
   1043 			break;
   1044 		case TELQUAL_REPLY:
   1045 			if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
   1046 				return;
   1047 			auth_reply(subpointer, SB_LEN());
   1048 			break;
   1049 		case TELQUAL_NAME:
   1050 			if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
   1051 				return;
   1052 			auth_name(subpointer, SB_LEN());
   1053 			break;
   1054 		}
   1055 	}
   1056 	break;
   1057 #endif
   1058 #ifdef	ENCRYPTION
   1059     case TELOPT_ENCRYPT:
   1060 	if (SB_EOF())
   1061 		return;
   1062 	switch(SB_GET()) {
   1063 	case ENCRYPT_START:
   1064 		if (my_want_state_is_dont(TELOPT_ENCRYPT))
   1065 			return;
   1066 		encrypt_start(subpointer, SB_LEN());
   1067 		break;
   1068 	case ENCRYPT_END:
   1069 		if (my_want_state_is_dont(TELOPT_ENCRYPT))
   1070 			return;
   1071 		encrypt_end();
   1072 		break;
   1073 	case ENCRYPT_SUPPORT:
   1074 		if (my_want_state_is_wont(TELOPT_ENCRYPT))
   1075 			return;
   1076 		encrypt_support(subpointer, SB_LEN());
   1077 		break;
   1078 	case ENCRYPT_REQSTART:
   1079 		if (my_want_state_is_wont(TELOPT_ENCRYPT))
   1080 			return;
   1081 		encrypt_request_start(subpointer, SB_LEN());
   1082 		break;
   1083 	case ENCRYPT_REQEND:
   1084 		if (my_want_state_is_wont(TELOPT_ENCRYPT))
   1085 			return;
   1086 		/*
   1087 		 * We can always send an REQEND so that we cannot
   1088 		 * get stuck encrypting.  We should only get this
   1089 		 * if we have been able to get in the correct mode
   1090 		 * anyhow.
   1091 		 */
   1092 		encrypt_request_end();
   1093 		break;
   1094 	case ENCRYPT_IS:
   1095 		if (my_want_state_is_dont(TELOPT_ENCRYPT))
   1096 			return;
   1097 		encrypt_is(subpointer, SB_LEN());
   1098 		break;
   1099 	case ENCRYPT_REPLY:
   1100 		if (my_want_state_is_wont(TELOPT_ENCRYPT))
   1101 			return;
   1102 		encrypt_reply(subpointer, SB_LEN());
   1103 		break;
   1104 	case ENCRYPT_ENC_KEYID:
   1105 		if (my_want_state_is_dont(TELOPT_ENCRYPT))
   1106 			return;
   1107 		encrypt_enc_keyid(subpointer, SB_LEN());
   1108 		break;
   1109 	case ENCRYPT_DEC_KEYID:
   1110 		if (my_want_state_is_wont(TELOPT_ENCRYPT))
   1111 			return;
   1112 		encrypt_dec_keyid(subpointer, SB_LEN());
   1113 		break;
   1114 	default:
   1115 		break;
   1116 	}
   1117 	break;
   1118 #endif	/* ENCRYPTION */
   1119     default:
   1120 	break;
   1121     }
   1122 }
   1123 
   1124 static unsigned char str_lm[] = { IAC, SB, TELOPT_LINEMODE, 0, 0, IAC, SE };
   1125 
   1126     void
   1127 lm_will(cmd, len)
   1128     unsigned char *cmd;
   1129     int len;
   1130 {
   1131     if (len < 1) {
   1132 /*@*/	printf("lm_will: no command!!!\n");	/* Should not happen... */
   1133 	return;
   1134     }
   1135     switch(cmd[0]) {
   1136     case LM_FORWARDMASK:	/* We shouldn't ever get this... */
   1137     default:
   1138 	str_lm[3] = DONT;
   1139 	str_lm[4] = cmd[0];
   1140 	if (NETROOM() > sizeof(str_lm)) {
   1141 	    ring_supply_data(&netoring, str_lm, sizeof(str_lm));
   1142 	    printsub('>', &str_lm[2], sizeof(str_lm)-2);
   1143 	}
   1144 /*@*/	else printf("lm_will: not enough room in buffer\n");
   1145 	break;
   1146     }
   1147 }
   1148 
   1149     void
   1150 lm_wont(cmd, len)
   1151     unsigned char *cmd;
   1152     int len;
   1153 {
   1154     if (len < 1) {
   1155 /*@*/	printf("lm_wont: no command!!!\n");	/* Should not happen... */
   1156 	return;
   1157     }
   1158     switch(cmd[0]) {
   1159     case LM_FORWARDMASK:	/* We shouldn't ever get this... */
   1160     default:
   1161 	/* We are always DONT, so don't respond */
   1162 	return;
   1163     }
   1164 }
   1165 
   1166     void
   1167 lm_do(cmd, len)
   1168     unsigned char *cmd;
   1169     int len;
   1170 {
   1171     if (len < 1) {
   1172 /*@*/	printf("lm_do: no command!!!\n");	/* Should not happen... */
   1173 	return;
   1174     }
   1175     switch(cmd[0]) {
   1176     case LM_FORWARDMASK:
   1177     default:
   1178 	str_lm[3] = WONT;
   1179 	str_lm[4] = cmd[0];
   1180 	if (NETROOM() > sizeof(str_lm)) {
   1181 	    ring_supply_data(&netoring, str_lm, sizeof(str_lm));
   1182 	    printsub('>', &str_lm[2], sizeof(str_lm)-2);
   1183 	}
   1184 /*@*/	else printf("lm_do: not enough room in buffer\n");
   1185 	break;
   1186     }
   1187 }
   1188 
   1189     void
   1190 lm_dont(cmd, len)
   1191     unsigned char *cmd;
   1192     int len;
   1193 {
   1194     if (len < 1) {
   1195 /*@*/	printf("lm_dont: no command!!!\n");	/* Should not happen... */
   1196 	return;
   1197     }
   1198     switch(cmd[0]) {
   1199     case LM_FORWARDMASK:
   1200     default:
   1201 	/* we are always WONT, so don't respond */
   1202 	break;
   1203     }
   1204 }
   1205 
   1206 static unsigned char str_lm_mode[] = {
   1207 	IAC, SB, TELOPT_LINEMODE, LM_MODE, 0, IAC, SE
   1208 };
   1209 
   1210 	void
   1211 lm_mode(cmd, len, init)
   1212 	unsigned char *cmd;
   1213 	int len, init;
   1214 {
   1215 	if (len != 1)
   1216 		return;
   1217 	if ((linemode&MODE_MASK&~MODE_ACK) == *cmd)
   1218 		return;
   1219 	if (*cmd&MODE_ACK)
   1220 		return;
   1221 	linemode = *cmd&(MODE_MASK&~MODE_ACK);
   1222 	str_lm_mode[4] = linemode;
   1223 	if (!init)
   1224 	    str_lm_mode[4] |= MODE_ACK;
   1225 	if (NETROOM() > sizeof(str_lm_mode)) {
   1226 	    ring_supply_data(&netoring, str_lm_mode, sizeof(str_lm_mode));
   1227 	    printsub('>', &str_lm_mode[2], sizeof(str_lm_mode)-2);
   1228 	}
   1229 /*@*/	else printf("lm_mode: not enough room in buffer\n");
   1230 	setconnmode(0);	/* set changed mode */
   1231 }
   1232 
   1233 
   1234 
   1236 /*
   1237  * slc()
   1238  * Handle special character suboption of LINEMODE.
   1239  */
   1240 
   1241 struct spc {
   1242 	cc_t val;
   1243 	cc_t *valp;
   1244 	char flags;	/* Current flags & level */
   1245 	char mylevel;	/* Maximum level & flags */
   1246 } spc_data[NSLC+1];
   1247 
   1248 #define SLC_IMPORT	0
   1249 #define	SLC_EXPORT	1
   1250 #define SLC_RVALUE	2
   1251 static int slc_mode = SLC_EXPORT;
   1252 
   1253 	void
   1254 slc_init()
   1255 {
   1256 	register struct spc *spcp;
   1257 
   1258 	localchars = 1;
   1259 	for (spcp = spc_data; spcp < &spc_data[NSLC+1]; spcp++) {
   1260 		spcp->val = 0;
   1261 		spcp->valp = 0;
   1262 		spcp->flags = spcp->mylevel = SLC_NOSUPPORT;
   1263 	}
   1264 
   1265 #define	initfunc(func, flags) { \
   1266 					spcp = &spc_data[func]; \
   1267 					if ((spcp->valp = tcval(func)) != NULL){ \
   1268 					    spcp->val = *spcp->valp; \
   1269 					    spcp->mylevel = SLC_VARIABLE|flags; \
   1270 					} else { \
   1271 					    spcp->val = 0; \
   1272 					    spcp->mylevel = SLC_DEFAULT; \
   1273 					} \
   1274 				    }
   1275 
   1276 	initfunc(SLC_SYNCH, 0);
   1277 	/* No BRK */
   1278 	initfunc(SLC_AO, 0);
   1279 	initfunc(SLC_AYT, 0);
   1280 	/* No EOR */
   1281 	initfunc(SLC_ABORT, SLC_FLUSHIN|SLC_FLUSHOUT);
   1282 	initfunc(SLC_EOF, 0);
   1283 #ifndef	SYSV_TERMIO
   1284 	initfunc(SLC_SUSP, SLC_FLUSHIN);
   1285 #endif
   1286 	initfunc(SLC_EC, 0);
   1287 	initfunc(SLC_EL, 0);
   1288 #ifndef	SYSV_TERMIO
   1289 	initfunc(SLC_EW, 0);
   1290 	initfunc(SLC_RP, 0);
   1291 	initfunc(SLC_LNEXT, 0);
   1292 #endif
   1293 	initfunc(SLC_XON, 0);
   1294 	initfunc(SLC_XOFF, 0);
   1295 #ifdef	SYSV_TERMIO
   1296 	spc_data[SLC_XON].mylevel = SLC_CANTCHANGE;
   1297 	spc_data[SLC_XOFF].mylevel = SLC_CANTCHANGE;
   1298 #endif
   1299 	initfunc(SLC_FORW1, 0);
   1300 #ifdef	USE_TERMIO
   1301 	initfunc(SLC_FORW2, 0);
   1302 	/* No FORW2 */
   1303 #endif
   1304 
   1305 	initfunc(SLC_IP, SLC_FLUSHIN|SLC_FLUSHOUT);
   1306 #undef	initfunc
   1307 
   1308 	if (slc_mode == SLC_EXPORT)
   1309 		slc_export();
   1310 	else
   1311 		slc_import(1);
   1312 
   1313 }
   1314 
   1315     void
   1316 slcstate()
   1317 {
   1318     printf("Special characters are %s values\n",
   1319 		slc_mode == SLC_IMPORT ? "remote default" :
   1320 		slc_mode == SLC_EXPORT ? "local" :
   1321 					 "remote");
   1322 }
   1323 
   1324     void
   1325 slc_mode_export(n)
   1326     int n;
   1327 {
   1328     slc_mode = SLC_EXPORT;
   1329     if (my_state_is_will(TELOPT_LINEMODE))
   1330 	slc_export();
   1331 }
   1332 
   1333     void
   1334 slc_mode_import(def)
   1335     int def;
   1336 {
   1337     slc_mode = def ? SLC_IMPORT : SLC_RVALUE;
   1338     if (my_state_is_will(TELOPT_LINEMODE))
   1339 	slc_import(def);
   1340 }
   1341 
   1342 unsigned char slc_import_val[] = {
   1343 	IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_VARIABLE, 0, IAC, SE
   1344 };
   1345 unsigned char slc_import_def[] = {
   1346 	IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_DEFAULT, 0, IAC, SE
   1347 };
   1348 
   1349     void
   1350 slc_import(def)
   1351     int def;
   1352 {
   1353     if (NETROOM() > sizeof(slc_import_val)) {
   1354 	if (def) {
   1355 	    ring_supply_data(&netoring, slc_import_def, sizeof(slc_import_def));
   1356 	    printsub('>', &slc_import_def[2], sizeof(slc_import_def)-2);
   1357 	} else {
   1358 	    ring_supply_data(&netoring, slc_import_val, sizeof(slc_import_val));
   1359 	    printsub('>', &slc_import_val[2], sizeof(slc_import_val)-2);
   1360 	}
   1361     }
   1362 /*@*/ else printf("slc_import: not enough room\n");
   1363 }
   1364 
   1365     void
   1366 slc_export()
   1367 {
   1368     register struct spc *spcp;
   1369 
   1370     TerminalDefaultChars();
   1371 
   1372     slc_start_reply();
   1373     for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
   1374 	if (spcp->mylevel != SLC_NOSUPPORT) {
   1375 	    if (spcp->val == (cc_t)(_POSIX_VDISABLE))
   1376 		spcp->flags = SLC_NOSUPPORT;
   1377 	    else
   1378 		spcp->flags = spcp->mylevel;
   1379 	    if (spcp->valp)
   1380 		spcp->val = *spcp->valp;
   1381 	    slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
   1382 	}
   1383     }
   1384     slc_end_reply();
   1385     (void)slc_update();
   1386     setconnmode(1);	/* Make sure the character values are set */
   1387 }
   1388 
   1389 	void
   1390 slc(cp, len)
   1391 	register unsigned char *cp;
   1392 	int len;
   1393 {
   1394 	register struct spc *spcp;
   1395 	register int func,level;
   1396 
   1397 	slc_start_reply();
   1398 
   1399 	for (; len >= 3; len -=3, cp +=3) {
   1400 
   1401 		func = cp[SLC_FUNC];
   1402 
   1403 		if (func == 0) {
   1404 			/*
   1405 			 * Client side: always ignore 0 function.
   1406 			 */
   1407 			continue;
   1408 		}
   1409 		if (func > NSLC) {
   1410 			if ((cp[SLC_FLAGS] & SLC_LEVELBITS) != SLC_NOSUPPORT)
   1411 				slc_add_reply(func, SLC_NOSUPPORT, 0);
   1412 			continue;
   1413 		}
   1414 
   1415 		spcp = &spc_data[func];
   1416 
   1417 		level = cp[SLC_FLAGS]&(SLC_LEVELBITS|SLC_ACK);
   1418 
   1419 		if ((cp[SLC_VALUE] == (unsigned char)spcp->val) &&
   1420 		    ((level&SLC_LEVELBITS) == (spcp->flags&SLC_LEVELBITS))) {
   1421 			continue;
   1422 		}
   1423 
   1424 		if (level == (SLC_DEFAULT|SLC_ACK)) {
   1425 			/*
   1426 			 * This is an error condition, the SLC_ACK
   1427 			 * bit should never be set for the SLC_DEFAULT
   1428 			 * level.  Our best guess to recover is to
   1429 			 * ignore the SLC_ACK bit.
   1430 			 */
   1431 			cp[SLC_FLAGS] &= ~SLC_ACK;
   1432 		}
   1433 
   1434 		if (level == ((spcp->flags&SLC_LEVELBITS)|SLC_ACK)) {
   1435 			spcp->val = (cc_t)cp[SLC_VALUE];
   1436 			spcp->flags = cp[SLC_FLAGS];	/* include SLC_ACK */
   1437 			continue;
   1438 		}
   1439 
   1440 		level &= ~SLC_ACK;
   1441 
   1442 		if (level <= (spcp->mylevel&SLC_LEVELBITS)) {
   1443 			spcp->flags = cp[SLC_FLAGS]|SLC_ACK;
   1444 			spcp->val = (cc_t)cp[SLC_VALUE];
   1445 		}
   1446 		if (level == SLC_DEFAULT) {
   1447 			if ((spcp->mylevel&SLC_LEVELBITS) != SLC_DEFAULT)
   1448 				spcp->flags = spcp->mylevel;
   1449 			else
   1450 				spcp->flags = SLC_NOSUPPORT;
   1451 		}
   1452 		slc_add_reply(func, spcp->flags, spcp->val);
   1453 	}
   1454 	slc_end_reply();
   1455 	if (slc_update())
   1456 		setconnmode(1);	/* set the  new character values */
   1457 }
   1458 
   1459     void
   1460 slc_check()
   1461 {
   1462     register struct spc *spcp;
   1463 
   1464     slc_start_reply();
   1465     for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
   1466 	if (spcp->valp && spcp->val != *spcp->valp) {
   1467 	    spcp->val = *spcp->valp;
   1468 	    if (spcp->val == (cc_t)(_POSIX_VDISABLE))
   1469 		spcp->flags = SLC_NOSUPPORT;
   1470 	    else
   1471 		spcp->flags = spcp->mylevel;
   1472 	    slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
   1473 	}
   1474     }
   1475     slc_end_reply();
   1476     setconnmode(1);
   1477 }
   1478 
   1479 
   1480 unsigned char slc_reply[128];
   1481 unsigned char *slc_replyp;
   1482 
   1483 	void
   1484 slc_start_reply()
   1485 {
   1486 	slc_replyp = slc_reply;
   1487 	*slc_replyp++ = IAC;
   1488 	*slc_replyp++ = SB;
   1489 	*slc_replyp++ = TELOPT_LINEMODE;
   1490 	*slc_replyp++ = LM_SLC;
   1491 }
   1492 
   1493 	void
   1494 slc_add_reply(func, flags, value)
   1495 	unsigned int func;
   1496 	unsigned int flags;
   1497 	cc_t value;
   1498 {
   1499 	if ((*slc_replyp++ = func) == IAC)
   1500 		*slc_replyp++ = IAC;
   1501 	if ((*slc_replyp++ = flags) == IAC)
   1502 		*slc_replyp++ = IAC;
   1503 	if ((*slc_replyp++ = (unsigned char)value) == IAC)
   1504 		*slc_replyp++ = IAC;
   1505 }
   1506 
   1507     void
   1508 slc_end_reply()
   1509 {
   1510     register int len;
   1511 
   1512     *slc_replyp++ = IAC;
   1513     *slc_replyp++ = SE;
   1514     len = slc_replyp - slc_reply;
   1515     if (len <= 6)
   1516 	return;
   1517     if (NETROOM() > len) {
   1518 	ring_supply_data(&netoring, slc_reply, slc_replyp - slc_reply);
   1519 	printsub('>', &slc_reply[2], slc_replyp - slc_reply - 2);
   1520     }
   1521 /*@*/else printf("slc_end_reply: not enough room\n");
   1522 }
   1523 
   1524 	int
   1525 slc_update()
   1526 {
   1527 	register struct spc *spcp;
   1528 	int need_update = 0;
   1529 
   1530 	for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
   1531 		if (!(spcp->flags&SLC_ACK))
   1532 			continue;
   1533 		spcp->flags &= ~SLC_ACK;
   1534 		if (spcp->valp && (*spcp->valp != spcp->val)) {
   1535 			*spcp->valp = spcp->val;
   1536 			need_update = 1;
   1537 		}
   1538 	}
   1539 	return(need_update);
   1540 }
   1541 
   1542 #ifdef	OLD_ENVIRON
   1543 # ifdef	ENV_HACK
   1544 /*
   1545  * Earlier version of telnet/telnetd from the BSD code had
   1546  * the definitions of VALUE and VAR reversed.  To ensure
   1547  * maximum interoperability, we assume that the server is
   1548  * an older BSD server, until proven otherwise.  The newer
   1549  * BSD servers should be able to handle either definition,
   1550  * so it is better to use the wrong values if we don't
   1551  * know what type of server it is.
   1552  */
   1553 int env_auto = 1;
   1554 int old_env_var = OLD_ENV_VAR;
   1555 int old_env_value = OLD_ENV_VALUE;
   1556 # else
   1557 #  define old_env_var OLD_ENV_VAR
   1558 #  define old_env_value OLD_ENV_VALUE
   1559 # endif
   1560 #endif
   1561 
   1562 	void
   1563 env_opt(buf, len)
   1564 	register unsigned char *buf;
   1565 	register int len;
   1566 {
   1567 	register unsigned char *ep = 0, *epc = 0;
   1568 	register int i;
   1569 
   1570 	switch(buf[0]&0xff) {
   1571 	case TELQUAL_SEND:
   1572 		env_opt_start();
   1573 		if (len == 1) {
   1574 			env_opt_add(NULL);
   1575 		} else for (i = 1; i < len; i++) {
   1576 			switch (buf[i]&0xff) {
   1577 #ifdef	OLD_ENVIRON
   1578 			case OLD_ENV_VAR:
   1579 # ifdef	ENV_HACK
   1580 				if (telopt_environ == TELOPT_OLD_ENVIRON
   1581 				    && env_auto) {
   1582 					/* Server has the same definitions */
   1583 					old_env_var = OLD_ENV_VAR;
   1584 					old_env_value = OLD_ENV_VALUE;
   1585 				}
   1586 				/* FALL THROUGH */
   1587 # endif
   1588 			case OLD_ENV_VALUE:
   1589 				/*
   1590 				 * Although OLD_ENV_VALUE is not legal, we will
   1591 				 * still recognize it, just in case it is an
   1592 				 * old server that has VAR & VALUE mixed up...
   1593 				 */
   1594 				/* FALL THROUGH */
   1595 #else
   1596 			case NEW_ENV_VAR:
   1597 #endif
   1598 			case ENV_USERVAR:
   1599 				if (ep) {
   1600 					*epc = 0;
   1601 					env_opt_add(ep);
   1602 				}
   1603 				ep = epc = &buf[i+1];
   1604 				break;
   1605 			case ENV_ESC:
   1606 				i++;
   1607 				/*FALL THROUGH*/
   1608 			default:
   1609 				if (epc)
   1610 					*epc++ = buf[i];
   1611 				break;
   1612 			}
   1613 		}
   1614 		if (ep) {
   1615 			*epc = 0;
   1616 			env_opt_add(ep);
   1617 		}
   1618 		env_opt_end(1);
   1619 		break;
   1620 
   1621 	case TELQUAL_IS:
   1622 	case TELQUAL_INFO:
   1623 		/* Ignore for now.  We shouldn't get it anyway. */
   1624 		break;
   1625 
   1626 	default:
   1627 		break;
   1628 	}
   1629 }
   1630 
   1631 #define	OPT_REPLY_SIZE	256
   1632 unsigned char *opt_reply;
   1633 unsigned char *opt_replyp;
   1634 unsigned char *opt_replyend;
   1635 
   1636 	void
   1637 env_opt_start()
   1638 {
   1639 	unsigned char *p;
   1640 
   1641 	if (opt_reply) {
   1642 		p = (unsigned char *)realloc(opt_reply, OPT_REPLY_SIZE);
   1643 		if (p == NULL)
   1644 			free(opt_reply);
   1645 	} else
   1646 		p = (unsigned char *)malloc(OPT_REPLY_SIZE);
   1647 	opt_reply = p;
   1648 	if (opt_reply == NULL) {
   1649 /*@*/		printf("env_opt_start: malloc()/realloc() failed!!!\n");
   1650 		opt_reply = opt_replyp = opt_replyend = NULL;
   1651 		return;
   1652 	}
   1653 	opt_replyp = opt_reply;
   1654 	opt_replyend = opt_reply + OPT_REPLY_SIZE;
   1655 	*opt_replyp++ = IAC;
   1656 	*opt_replyp++ = SB;
   1657 	*opt_replyp++ = telopt_environ;
   1658 	*opt_replyp++ = TELQUAL_IS;
   1659 }
   1660 
   1661 	void
   1662 env_opt_start_info()
   1663 {
   1664 	env_opt_start();
   1665 	if (opt_replyp)
   1666 	    opt_replyp[-1] = TELQUAL_INFO;
   1667 }
   1668 
   1669 	void
   1670 env_opt_add(ep)
   1671 	register unsigned char *ep;
   1672 {
   1673 	register unsigned char *vp, c;
   1674 
   1675 	if (opt_reply == NULL)		/*XXX*/
   1676 		return;			/*XXX*/
   1677 
   1678 	if (ep == NULL || *ep == '\0') {
   1679 		/* Send user defined variables first. */
   1680 		env_default(1, 0);
   1681 		while ((ep = env_default(0, 0)) != NULL)
   1682 			env_opt_add(ep);
   1683 
   1684 		/* Now add the list of well know variables.  */
   1685 		env_default(1, 1);
   1686 		while ((ep = env_default(0, 1)) != NULL)
   1687 			env_opt_add(ep);
   1688 		return;
   1689 	}
   1690 	vp = env_getvalue(ep);
   1691 	if (opt_replyp + (vp ? strlen((char *)vp) : 0) +
   1692 				strlen((char *)ep) + 6 > opt_replyend)
   1693 	{
   1694 		register int len;
   1695 		unsigned char *p;
   1696 		opt_replyend += OPT_REPLY_SIZE;
   1697 		len = opt_replyend - opt_reply;
   1698 		p = (unsigned char *)realloc(opt_reply, len);
   1699 		if (p == NULL)
   1700 			free(opt_reply);
   1701 		opt_reply = p;
   1702 		if (opt_reply == NULL) {
   1703 /*@*/			printf("env_opt_add: realloc() failed!!!\n");
   1704 			opt_reply = opt_replyp = opt_replyend = NULL;
   1705 			return;
   1706 		}
   1707 		opt_replyp = opt_reply + len - (opt_replyend - opt_replyp);
   1708 		opt_replyend = opt_reply + len;
   1709 	}
   1710 	if (opt_welldefined(ep))
   1711 #ifdef	OLD_ENVIRON
   1712 		if (telopt_environ == TELOPT_OLD_ENVIRON)
   1713 			*opt_replyp++ = old_env_var;
   1714 		else
   1715 #endif
   1716 			*opt_replyp++ = NEW_ENV_VAR;
   1717 	else
   1718 		*opt_replyp++ = ENV_USERVAR;
   1719 	for (;;) {
   1720 		while ((c = *ep++) != '\0') {
   1721 			switch(c&0xff) {
   1722 			case IAC:
   1723 				*opt_replyp++ = IAC;
   1724 				break;
   1725 			case NEW_ENV_VAR:
   1726 			case NEW_ENV_VALUE:
   1727 			case ENV_ESC:
   1728 			case ENV_USERVAR:
   1729 				*opt_replyp++ = ENV_ESC;
   1730 				break;
   1731 			}
   1732 			*opt_replyp++ = c;
   1733 		}
   1734 		if ((ep = vp) != NULL) {
   1735 #ifdef	OLD_ENVIRON
   1736 			if (telopt_environ == TELOPT_OLD_ENVIRON)
   1737 				*opt_replyp++ = old_env_value;
   1738 			else
   1739 #endif
   1740 				*opt_replyp++ = NEW_ENV_VALUE;
   1741 			vp = NULL;
   1742 		} else
   1743 			break;
   1744 	}
   1745 }
   1746 
   1747 	int
   1748 opt_welldefined(ep)
   1749 	char *ep;
   1750 {
   1751 	if ((strcmp(ep, "USER") == 0) ||
   1752 	    (strcmp(ep, "DISPLAY") == 0) ||
   1753 	    (strcmp(ep, "PRINTER") == 0) ||
   1754 	    (strcmp(ep, "SYSTEMTYPE") == 0) ||
   1755 	    (strcmp(ep, "JOB") == 0) ||
   1756 	    (strcmp(ep, "ACCT") == 0))
   1757 		return(1);
   1758 	return(0);
   1759 }
   1760 	void
   1761 env_opt_end(emptyok)
   1762 	register int emptyok;
   1763 {
   1764 	register int len;
   1765 
   1766 	len = opt_replyp - opt_reply + 2;
   1767 	if (emptyok || len > 6) {
   1768 		*opt_replyp++ = IAC;
   1769 		*opt_replyp++ = SE;
   1770 		if (NETROOM() > len) {
   1771 			ring_supply_data(&netoring, opt_reply, len);
   1772 			printsub('>', &opt_reply[2], len - 2);
   1773 		}
   1774 /*@*/		else printf("slc_end_reply: not enough room\n");
   1775 	}
   1776 	if (opt_reply) {
   1777 		free(opt_reply);
   1778 		opt_reply = opt_replyp = opt_replyend = NULL;
   1779 	}
   1780 }
   1781 
   1782 
   1783 
   1785     int
   1786 telrcv()
   1787 {
   1788     register int c;
   1789     register int scc;
   1790     register unsigned char *sbp = NULL;
   1791     int count;
   1792     int returnValue = 0;
   1793 
   1794     scc = 0;
   1795     count = 0;
   1796     while (TTYROOM() > 2) {
   1797 	if (scc == 0) {
   1798 	    if (count) {
   1799 		ring_consumed(&netiring, count);
   1800 		returnValue = 1;
   1801 		count = 0;
   1802 	    }
   1803 	    sbp = netiring.consume;
   1804 	    scc = ring_full_consecutive(&netiring);
   1805 	    if (scc == 0) {
   1806 		/* No more data coming in */
   1807 		break;
   1808 	    }
   1809 	}
   1810 
   1811 	c = *sbp++ & 0xff, scc--; count++;
   1812 #ifdef	ENCRYPTION
   1813 	if (decrypt_input)
   1814 		c = (*decrypt_input)(c);
   1815 #endif	/* ENCRYPTION */
   1816 
   1817 	switch (telrcv_state) {
   1818 
   1819 	case TS_CR:
   1820 	    telrcv_state = TS_DATA;
   1821 	    if (c == '\0') {
   1822 		break;	/* Ignore \0 after CR */
   1823 	    }
   1824 	    else if ((c == '\n') && my_want_state_is_dont(TELOPT_ECHO) && !crmod) {
   1825 		TTYADD(c);
   1826 		break;
   1827 	    }
   1828 	    /* Else, fall through */
   1829 
   1830 	case TS_DATA:
   1831 	    if (c == IAC) {
   1832 		telrcv_state = TS_IAC;
   1833 		break;
   1834 	    }
   1835 #	    if defined(TN3270)
   1836 	    if (In3270) {
   1837 		*Ifrontp++ = c;
   1838 		while (scc > 0) {
   1839 		    c = *sbp++ & 0377, scc--; count++;
   1840 #ifdef	ENCRYPTION
   1841 		    if (decrypt_input)
   1842 			c = (*decrypt_input)(c);
   1843 #endif	/* ENCRYPTION */
   1844 		    if (c == IAC) {
   1845 			telrcv_state = TS_IAC;
   1846 			break;
   1847 		    }
   1848 		    *Ifrontp++ = c;
   1849 		}
   1850 	    } else
   1851 #	    endif /* defined(TN3270) */
   1852 		    /*
   1853 		     * The 'crmod' hack (see following) is needed
   1854 		     * since we can't * set CRMOD on output only.
   1855 		     * Machines like MULTICS like to send \r without
   1856 		     * \n; since we must turn off CRMOD to get proper
   1857 		     * input, the mapping is done here (sigh).
   1858 		     */
   1859 	    if ((c == '\r') && my_want_state_is_dont(TELOPT_BINARY)) {
   1860 		if (scc > 0) {
   1861 		    c = *sbp&0xff;
   1862 #ifdef	ENCRYPTION
   1863 		    if (decrypt_input)
   1864 			c = (*decrypt_input)(c);
   1865 #endif	/* ENCRYPTION */
   1866 		    if (c == 0) {
   1867 			sbp++, scc--; count++;
   1868 			/* a "true" CR */
   1869 			TTYADD('\r');
   1870 		    } else if (my_want_state_is_dont(TELOPT_ECHO) &&
   1871 					(c == '\n')) {
   1872 			sbp++, scc--; count++;
   1873 			TTYADD('\n');
   1874 		    } else {
   1875 #ifdef	ENCRYPTION
   1876 			if (decrypt_input)
   1877 			    (*decrypt_input)(-1);
   1878 #endif	/* ENCRYPTION */
   1879 
   1880 			TTYADD('\r');
   1881 			if (crmod) {
   1882 				TTYADD('\n');
   1883 			}
   1884 		    }
   1885 		} else {
   1886 		    telrcv_state = TS_CR;
   1887 		    TTYADD('\r');
   1888 		    if (crmod) {
   1889 			    TTYADD('\n');
   1890 		    }
   1891 		}
   1892 	    } else {
   1893 		TTYADD(c);
   1894 	    }
   1895 	    continue;
   1896 
   1897 	case TS_IAC:
   1898 process_iac:
   1899 	    switch (c) {
   1900 
   1901 	    case WILL:
   1902 		telrcv_state = TS_WILL;
   1903 		continue;
   1904 
   1905 	    case WONT:
   1906 		telrcv_state = TS_WONT;
   1907 		continue;
   1908 
   1909 	    case DO:
   1910 		telrcv_state = TS_DO;
   1911 		continue;
   1912 
   1913 	    case DONT:
   1914 		telrcv_state = TS_DONT;
   1915 		continue;
   1916 
   1917 	    case DM:
   1918 		    /*
   1919 		     * We may have missed an urgent notification,
   1920 		     * so make sure we flush whatever is in the
   1921 		     * buffer currently.
   1922 		     */
   1923 		printoption("RCVD", IAC, DM);
   1924 		SYNCHing = 1;
   1925 		(void) ttyflush(1);
   1926 		SYNCHing = stilloob();
   1927 		settimer(gotDM);
   1928 		break;
   1929 
   1930 	    case SB:
   1931 		SB_CLEAR();
   1932 		telrcv_state = TS_SB;
   1933 		continue;
   1934 
   1935 #	    if defined(TN3270)
   1936 	    case EOR:
   1937 		if (In3270) {
   1938 		    if (Ibackp == Ifrontp) {
   1939 			Ibackp = Ifrontp = Ibuf;
   1940 			ISend = 0;	/* should have been! */
   1941 		    } else {
   1942 			Ibackp += DataFromNetwork(Ibackp, Ifrontp-Ibackp, 1);
   1943 			ISend = 1;
   1944 		    }
   1945 		}
   1946 		printoption("RCVD", IAC, EOR);
   1947 		break;
   1948 #	    endif /* defined(TN3270) */
   1949 
   1950 	    case IAC:
   1951 #	    if !defined(TN3270)
   1952 		TTYADD(IAC);
   1953 #	    else /* !defined(TN3270) */
   1954 		if (In3270) {
   1955 		    *Ifrontp++ = IAC;
   1956 		} else {
   1957 		    TTYADD(IAC);
   1958 		}
   1959 #	    endif /* !defined(TN3270) */
   1960 		break;
   1961 
   1962 	    case NOP:
   1963 	    case GA:
   1964 	    default:
   1965 		printoption("RCVD", IAC, c);
   1966 		break;
   1967 	    }
   1968 	    telrcv_state = TS_DATA;
   1969 	    continue;
   1970 
   1971 	case TS_WILL:
   1972 	    printoption("RCVD", WILL, c);
   1973 	    willoption(c);
   1974 	    SetIn3270();
   1975 	    telrcv_state = TS_DATA;
   1976 	    continue;
   1977 
   1978 	case TS_WONT:
   1979 	    printoption("RCVD", WONT, c);
   1980 	    wontoption(c);
   1981 	    SetIn3270();
   1982 	    telrcv_state = TS_DATA;
   1983 	    continue;
   1984 
   1985 	case TS_DO:
   1986 	    printoption("RCVD", DO, c);
   1987 	    dooption(c);
   1988 	    SetIn3270();
   1989 	    if (c == TELOPT_NAWS) {
   1990 		sendnaws();
   1991 	    } else if (c == TELOPT_LFLOW) {
   1992 		localflow = 1;
   1993 		setcommandmode();
   1994 		setconnmode(0);
   1995 	    }
   1996 	    telrcv_state = TS_DATA;
   1997 	    continue;
   1998 
   1999 	case TS_DONT:
   2000 	    printoption("RCVD", DONT, c);
   2001 	    dontoption(c);
   2002 	    flushline = 1;
   2003 	    setconnmode(0);	/* set new tty mode (maybe) */
   2004 	    SetIn3270();
   2005 	    telrcv_state = TS_DATA;
   2006 	    continue;
   2007 
   2008 	case TS_SB:
   2009 	    if (c == IAC) {
   2010 		telrcv_state = TS_SE;
   2011 	    } else {
   2012 		SB_ACCUM(c);
   2013 	    }
   2014 	    continue;
   2015 
   2016 	case TS_SE:
   2017 	    if (c != SE) {
   2018 		if (c != IAC) {
   2019 		    /*
   2020 		     * This is an error.  We only expect to get
   2021 		     * "IAC IAC" or "IAC SE".  Several things may
   2022 		     * have happend.  An IAC was not doubled, the
   2023 		     * IAC SE was left off, or another option got
   2024 		     * inserted into the suboption are all possibilities.
   2025 		     * If we assume that the IAC was not doubled,
   2026 		     * and really the IAC SE was left off, we could
   2027 		     * get into an infinate loop here.  So, instead,
   2028 		     * we terminate the suboption, and process the
   2029 		     * partial suboption if we can.
   2030 		     */
   2031 		    SB_ACCUM(IAC);
   2032 		    SB_ACCUM(c);
   2033 		    subpointer -= 2;
   2034 		    SB_TERM();
   2035 
   2036 		    printoption("In SUBOPTION processing, RCVD", IAC, c);
   2037 		    suboption();	/* handle sub-option */
   2038 		    SetIn3270();
   2039 		    telrcv_state = TS_IAC;
   2040 		    goto process_iac;
   2041 		}
   2042 		SB_ACCUM(c);
   2043 		telrcv_state = TS_SB;
   2044 	    } else {
   2045 		SB_ACCUM(IAC);
   2046 		SB_ACCUM(SE);
   2047 		subpointer -= 2;
   2048 		SB_TERM();
   2049 		suboption();	/* handle sub-option */
   2050 		SetIn3270();
   2051 		telrcv_state = TS_DATA;
   2052 	    }
   2053 	}
   2054     }
   2055     if (count)
   2056 	ring_consumed(&netiring, count);
   2057     return returnValue||count;
   2058 }
   2059 
   2060 static int bol = 1, local = 0;
   2061 
   2062     int
   2063 rlogin_susp()
   2064 {
   2065     if (local) {
   2066 	local = 0;
   2067 	bol = 1;
   2068 	command(0, "z\n", 2);
   2069 	return(1);
   2070     }
   2071     return(0);
   2072 }
   2073 
   2074     static int
   2075 telsnd()
   2076 {
   2077     int tcc;
   2078     int count;
   2079     int returnValue = 0;
   2080     unsigned char *tbp = NULL;
   2081 
   2082     tcc = 0;
   2083     count = 0;
   2084     while (NETROOM() > 2) {
   2085 	register int sc;
   2086 	register int c;
   2087 
   2088 	if (tcc == 0) {
   2089 	    if (count) {
   2090 		ring_consumed(&ttyiring, count);
   2091 		returnValue = 1;
   2092 		count = 0;
   2093 	    }
   2094 	    tbp = ttyiring.consume;
   2095 	    tcc = ring_full_consecutive(&ttyiring);
   2096 	    if (tcc == 0) {
   2097 		break;
   2098 	    }
   2099 	}
   2100 	c = *tbp++ & 0xff, sc = strip(c), tcc--; count++;
   2101 	if (rlogin != _POSIX_VDISABLE) {
   2102 		if (bol) {
   2103 			bol = 0;
   2104 			if (sc == rlogin) {
   2105 				local = 1;
   2106 				continue;
   2107 			}
   2108 		} else if (local) {
   2109 			local = 0;
   2110 			if (sc == '.' || c == termEofChar) {
   2111 				bol = 1;
   2112 				command(0, "close\n", 6);
   2113 				continue;
   2114 			}
   2115 			if (sc == termSuspChar) {
   2116 				bol = 1;
   2117 				command(0, "z\n", 2);
   2118 				continue;
   2119 			}
   2120 			if (sc == escape) {
   2121 				command(0, (char *)tbp, tcc);
   2122 				bol = 1;
   2123 				count += tcc;
   2124 				tcc = 0;
   2125 				flushline = 1;
   2126 				break;
   2127 			}
   2128 			if (sc != rlogin) {
   2129 				++tcc;
   2130 				--tbp;
   2131 				--count;
   2132 				c = sc = rlogin;
   2133 			}
   2134 		}
   2135 		if ((sc == '\n') || (sc == '\r'))
   2136 			bol = 1;
   2137 	} else if (sc == escape && escape != _POSIX_VDISABLE) {
   2138 	    /*
   2139 	     * Double escape is a pass through of a single escape character.
   2140 	     */
   2141 	    if (tcc && strip(*tbp) == escape) {
   2142 		tbp++;
   2143 		tcc--;
   2144 		count++;
   2145 		bol = 0;
   2146 	    } else {
   2147 		command(0, (char *)tbp, tcc);
   2148 		bol = 1;
   2149 		count += tcc;
   2150 		tcc = 0;
   2151 		flushline = 1;
   2152 		break;
   2153 	    }
   2154 	} else
   2155 	    bol = 0;
   2156 #ifdef	KLUDGELINEMODE
   2157 	if (kludgelinemode && (globalmode&MODE_EDIT) && (sc == echoc)) {
   2158 	    if (tcc > 0 && strip(*tbp) == echoc) {
   2159 		tcc--; tbp++; count++;
   2160 	    } else {
   2161 		dontlecho = !dontlecho;
   2162 		settimer(echotoggle);
   2163 		setconnmode(0);
   2164 		flushline = 1;
   2165 		break;
   2166 	    }
   2167 	}
   2168 #endif
   2169 	if (sc != _POSIX_VDISABLE && MODE_LOCAL_CHARS(globalmode)) {
   2170 	    if (TerminalSpecialChars(sc) == 0) {
   2171 		bol = 1;
   2172 		break;
   2173 	    }
   2174 	}
   2175 	if (my_want_state_is_wont(TELOPT_BINARY)) {
   2176 	    switch (c) {
   2177 	    case '\n':
   2178 		    /*
   2179 		     * If we are in CRMOD mode (\r ==> \n)
   2180 		     * on our local machine, then probably
   2181 		     * a newline (unix) is CRLF (TELNET).
   2182 		     */
   2183 		if (MODE_LOCAL_CHARS(globalmode)) {
   2184 		    NETADD('\r');
   2185 		}
   2186 		NETADD('\n');
   2187 		bol = flushline = 1;
   2188 		break;
   2189 	    case '\r':
   2190 		if (!crlf) {
   2191 		    NET2ADD('\r', '\0');
   2192 		} else {
   2193 		    NET2ADD('\r', '\n');
   2194 		}
   2195 		bol = flushline = 1;
   2196 		break;
   2197 	    case IAC:
   2198 		NET2ADD(IAC, IAC);
   2199 		break;
   2200 	    default:
   2201 		NETADD(c);
   2202 		break;
   2203 	    }
   2204 	} else if (c == IAC) {
   2205 	    NET2ADD(IAC, IAC);
   2206 	} else {
   2207 	    NETADD(c);
   2208 	}
   2209     }
   2210     if (count)
   2211 	ring_consumed(&ttyiring, count);
   2212     return returnValue||count;		/* Non-zero if we did anything */
   2213 }
   2214 
   2215 /*
   2217  * Scheduler()
   2218  *
   2219  * Try to do something.
   2220  *
   2221  * If we do something useful, return 1; else return 0.
   2222  *
   2223  */
   2224 
   2225 
   2226     int
   2227 Scheduler(block)
   2228     int	block;			/* should we block in the select ? */
   2229 {
   2230 		/* One wants to be a bit careful about setting returnValue
   2231 		 * to one, since a one implies we did some useful work,
   2232 		 * and therefore probably won't be called to block next
   2233 		 * time (TN3270 mode only).
   2234 		 */
   2235     int returnValue;
   2236     int netin, netout, netex, ttyin, ttyout;
   2237 
   2238     /* Decide which rings should be processed */
   2239 
   2240     netout = ring_full_count(&netoring) &&
   2241 	    (flushline ||
   2242 		(my_want_state_is_wont(TELOPT_LINEMODE)
   2243 #ifdef	KLUDGELINEMODE
   2244 			&& (!kludgelinemode || my_want_state_is_do(TELOPT_SGA))
   2245 #endif
   2246 		) ||
   2247 			my_want_state_is_will(TELOPT_BINARY));
   2248     ttyout = ring_full_count(&ttyoring);
   2249 
   2250 #if	defined(TN3270)
   2251     ttyin = ring_empty_count(&ttyiring) && (clienteof == 0) && (shell_active == 0);
   2252 #else	/* defined(TN3270) */
   2253     ttyin = ring_empty_count(&ttyiring) && (clienteof == 0);
   2254 #endif	/* defined(TN3270) */
   2255 
   2256 #if	defined(TN3270)
   2257     netin = ring_empty_count(&netiring);
   2258 #   else /* !defined(TN3270) */
   2259     netin = !ISend && ring_empty_count(&netiring);
   2260 #   endif /* !defined(TN3270) */
   2261 
   2262     netex = !SYNCHing;
   2263 
   2264     /* If we have seen a signal recently, reset things */
   2265 #   if defined(TN3270) && defined(unix)
   2266     if (HaveInput) {
   2267 	HaveInput = 0;
   2268 	(void) signal(SIGIO, inputAvailable);
   2269     }
   2270 #endif	/* defined(TN3270) && defined(unix) */
   2271 
   2272     /* Call to system code to process rings */
   2273 
   2274     returnValue = process_rings(netin, netout, netex, ttyin, ttyout, !block);
   2275 
   2276     /* Now, look at the input rings, looking for work to do. */
   2277 
   2278     if (ring_full_count(&ttyiring)) {
   2279 #   if defined(TN3270)
   2280 	if (In3270) {
   2281 	    int c;
   2282 
   2283 	    c = DataFromTerminal(ttyiring.consume,
   2284 					ring_full_consecutive(&ttyiring));
   2285 	    if (c) {
   2286 		returnValue = 1;
   2287 		ring_consumed(&ttyiring, c);
   2288 	    }
   2289 	} else {
   2290 #   endif /* defined(TN3270) */
   2291 	    returnValue |= telsnd();
   2292 #   if defined(TN3270)
   2293 	}
   2294 #   endif /* defined(TN3270) */
   2295     }
   2296 
   2297     if (ring_full_count(&netiring)) {
   2298 #	if !defined(TN3270)
   2299 	returnValue |= telrcv();
   2300 #	else /* !defined(TN3270) */
   2301 	returnValue = Push3270();
   2302 #	endif /* !defined(TN3270) */
   2303     }
   2304     return returnValue;
   2305 }
   2306 
   2307 /*
   2309  * Select from tty and network...
   2310  */
   2311     void
   2312 telnet(user)
   2313     const char *user;
   2314 {
   2315     sys_telnet_init();
   2316 
   2317 #if	defined(AUTHENTICATION) || defined(ENCRYPTION)
   2318     {
   2319 	static char local_host[MAXHOSTNAMELEN + 1] = { 0 };
   2320 
   2321 	if (!local_host[0]) {
   2322 		gethostname(local_host, sizeof(local_host));
   2323 		local_host[sizeof(local_host)-1] = 0;
   2324 	}
   2325 	auth_encrypt_init(local_host, hostname, "TELNET", 0);
   2326 	auth_encrypt_user(user);
   2327     }
   2328 #endif	/* defined(AUTHENTICATION) || defined(ENCRYPTION) */
   2329 #   if !defined(TN3270)
   2330     if (telnetport) {
   2331 #if	defined(AUTHENTICATION)
   2332 	if (autologin)
   2333 		send_will(TELOPT_AUTHENTICATION, 1);
   2334 #endif
   2335 #ifdef	ENCRYPTION
   2336 	send_do(TELOPT_ENCRYPT, 1);
   2337 	send_will(TELOPT_ENCRYPT, 1);
   2338 #endif	/* ENCRYPTION */
   2339 	send_do(TELOPT_SGA, 1);
   2340 	send_will(TELOPT_TTYPE, 1);
   2341 	send_will(TELOPT_NAWS, 1);
   2342 	send_will(TELOPT_TSPEED, 1);
   2343 	send_will(TELOPT_LFLOW, 1);
   2344 	send_will(TELOPT_LINEMODE, 1);
   2345 	send_will(TELOPT_NEW_ENVIRON, 1);
   2346 	send_do(TELOPT_STATUS, 1);
   2347 	if (env_getvalue((unsigned char *)"DISPLAY"))
   2348 	    send_will(TELOPT_XDISPLOC, 1);
   2349 	if (eight)
   2350 	    tel_enter_binary(eight);
   2351     }
   2352 #   endif /* !defined(TN3270) */
   2353 
   2354 #   if !defined(TN3270)
   2355     for (;;) {
   2356 	int schedValue;
   2357 
   2358 	while ((schedValue = Scheduler(0)) != 0) {
   2359 	    if (schedValue == -1) {
   2360 		setcommandmode();
   2361 		return;
   2362 	    }
   2363 	}
   2364 
   2365 	if (Scheduler(1) == -1) {
   2366 	    setcommandmode();
   2367 	    return;
   2368 	}
   2369     }
   2370 #   else /* !defined(TN3270) */
   2371     for (;;) {
   2372 	int schedValue;
   2373 
   2374 	while (!In3270 && !shell_active) {
   2375 	    if (Scheduler(1) == -1) {
   2376 		setcommandmode();
   2377 		return;
   2378 	    }
   2379 	}
   2380 
   2381 	while ((schedValue = Scheduler(0)) != 0) {
   2382 	    if (schedValue == -1) {
   2383 		setcommandmode();
   2384 		return;
   2385 	    }
   2386 	}
   2387 		/* If there is data waiting to go out to terminal, don't
   2388 		 * schedule any more data for the terminal.
   2389 		 */
   2390 	if (ring_full_count(&ttyoring)) {
   2391 	    schedValue = 1;
   2392 	} else {
   2393 	    if (shell_active) {
   2394 		if (shell_continue() == 0) {
   2395 		    ConnectScreen();
   2396 		}
   2397 	    } else if (In3270) {
   2398 		schedValue = DoTerminalOutput();
   2399 	    }
   2400 	}
   2401 	if (schedValue && (shell_active == 0)) {
   2402 	    if (Scheduler(1) == -1) {
   2403 		setcommandmode();
   2404 		return;
   2405 	    }
   2406 	}
   2407     }
   2408 #   endif /* !defined(TN3270) */
   2409 }
   2410 
   2411 #if	0	/* XXX - this not being in is a bug */
   2413 /*
   2414  * nextitem()
   2415  *
   2416  *	Return the address of the next "item" in the TELNET data
   2417  * stream.  This will be the address of the next character if
   2418  * the current address is a user data character, or it will
   2419  * be the address of the character following the TELNET command
   2420  * if the current address is a TELNET IAC ("I Am a Command")
   2421  * character.
   2422  */
   2423 
   2424     static char *
   2425 nextitem(current)
   2426     char *current;
   2427 {
   2428     if ((*current&0xff) != IAC) {
   2429 	return current+1;
   2430     }
   2431     switch (*(current+1)&0xff) {
   2432     case DO:
   2433     case DONT:
   2434     case WILL:
   2435     case WONT:
   2436 	return current+3;
   2437     case SB:		/* loop forever looking for the SE */
   2438 	{
   2439 	    register char *look = current+2;
   2440 
   2441 	    for (;;) {
   2442 		if ((*look++&0xff) == IAC) {
   2443 		    if ((*look++&0xff) == SE) {
   2444 			return look;
   2445 		    }
   2446 		}
   2447 	    }
   2448 	}
   2449     default:
   2450 	return current+2;
   2451     }
   2452 }
   2453 #endif	/* 0 */
   2454 
   2455 /*
   2456  * netclear()
   2457  *
   2458  *	We are about to do a TELNET SYNCH operation.  Clear
   2459  * the path to the network.
   2460  *
   2461  *	Things are a bit tricky since we may have sent the first
   2462  * byte or so of a previous TELNET command into the network.
   2463  * So, we have to scan the network buffer from the beginning
   2464  * until we are up to where we want to be.
   2465  *
   2466  *	A side effect of what we do, just to keep things
   2467  * simple, is to clear the urgent data pointer.  The principal
   2468  * caller should be setting the urgent data pointer AFTER calling
   2469  * us in any case.
   2470  */
   2471 
   2472     static void
   2473 netclear()
   2474 {
   2475 #if	0	/* XXX */
   2476     register char *thisitem, *next;
   2477     char *good;
   2478 #define	wewant(p)	((nfrontp > p) && ((*p&0xff) == IAC) && \
   2479 				((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL))
   2480 
   2481     thisitem = netobuf;
   2482 
   2483     while ((next = nextitem(thisitem)) <= netobuf.send) {
   2484 	thisitem = next;
   2485     }
   2486 
   2487     /* Now, thisitem is first before/at boundary. */
   2488 
   2489     good = netobuf;	/* where the good bytes go */
   2490 
   2491     while (netoring.add > thisitem) {
   2492 	if (wewant(thisitem)) {
   2493 	    int length;
   2494 
   2495 	    next = thisitem;
   2496 	    do {
   2497 		next = nextitem(next);
   2498 	    } while (wewant(next) && (nfrontp > next));
   2499 	    length = next-thisitem;
   2500 	    memmove(good, thisitem, length);
   2501 	    good += length;
   2502 	    thisitem = next;
   2503 	} else {
   2504 	    thisitem = nextitem(thisitem);
   2505 	}
   2506     }
   2507 
   2508 #endif	/* 0 */
   2509 }
   2510 
   2511 /*
   2513  * These routines add various telnet commands to the data stream.
   2514  */
   2515 
   2516     static void
   2517 doflush()
   2518 {
   2519     NET2ADD(IAC, DO);
   2520     NETADD(TELOPT_TM);
   2521     flushline = 1;
   2522     flushout = 1;
   2523     (void) ttyflush(1);			/* Flush/drop output */
   2524     /* do printoption AFTER flush, otherwise the output gets tossed... */
   2525     printoption("SENT", DO, TELOPT_TM);
   2526 }
   2527 
   2528     void
   2529 xmitAO()
   2530 {
   2531     NET2ADD(IAC, AO);
   2532     printoption("SENT", IAC, AO);
   2533     if (autoflush) {
   2534 	doflush();
   2535     }
   2536 }
   2537 
   2538 
   2539     void
   2540 xmitEL()
   2541 {
   2542     NET2ADD(IAC, EL);
   2543     printoption("SENT", IAC, EL);
   2544 }
   2545 
   2546     void
   2547 xmitEC()
   2548 {
   2549     NET2ADD(IAC, EC);
   2550     printoption("SENT", IAC, EC);
   2551 }
   2552 
   2553 
   2554     int
   2555 dosynch(s)
   2556     char *s;
   2557 {
   2558     netclear();			/* clear the path to the network */
   2559     NETADD(IAC);
   2560     setneturg();
   2561     NETADD(DM);
   2562     printoption("SENT", IAC, DM);
   2563     return 1;
   2564 }
   2565 
   2566 int want_status_response = 0;
   2567 
   2568     int
   2569 get_status(s)
   2570     char *s;
   2571 {
   2572     unsigned char tmp[16];
   2573     register unsigned char *cp;
   2574 
   2575     if (my_want_state_is_dont(TELOPT_STATUS)) {
   2576 	printf("Remote side does not support STATUS option\n");
   2577 	return 0;
   2578     }
   2579     cp = tmp;
   2580 
   2581     *cp++ = IAC;
   2582     *cp++ = SB;
   2583     *cp++ = TELOPT_STATUS;
   2584     *cp++ = TELQUAL_SEND;
   2585     *cp++ = IAC;
   2586     *cp++ = SE;
   2587     if (NETROOM() >= cp - tmp) {
   2588 	ring_supply_data(&netoring, tmp, cp-tmp);
   2589 	printsub('>', tmp+2, cp - tmp - 2);
   2590     }
   2591     ++want_status_response;
   2592     return 1;
   2593 }
   2594 
   2595     void
   2596 intp()
   2597 {
   2598     NET2ADD(IAC, IP);
   2599     printoption("SENT", IAC, IP);
   2600     flushline = 1;
   2601     if (autoflush) {
   2602 	doflush();
   2603     }
   2604     if (autosynch) {
   2605 	dosynch(NULL);
   2606     }
   2607 }
   2608 
   2609     void
   2610 sendbrk()
   2611 {
   2612     NET2ADD(IAC, BREAK);
   2613     printoption("SENT", IAC, BREAK);
   2614     flushline = 1;
   2615     if (autoflush) {
   2616 	doflush();
   2617     }
   2618     if (autosynch) {
   2619 	dosynch(NULL);
   2620     }
   2621 }
   2622 
   2623     void
   2624 sendabort()
   2625 {
   2626     NET2ADD(IAC, ABORT);
   2627     printoption("SENT", IAC, ABORT);
   2628     flushline = 1;
   2629     if (autoflush) {
   2630 	doflush();
   2631     }
   2632     if (autosynch) {
   2633 	dosynch(NULL);
   2634     }
   2635 }
   2636 
   2637     void
   2638 sendsusp()
   2639 {
   2640     NET2ADD(IAC, SUSP);
   2641     printoption("SENT", IAC, SUSP);
   2642     flushline = 1;
   2643     if (autoflush) {
   2644 	doflush();
   2645     }
   2646     if (autosynch) {
   2647 	dosynch(NULL);
   2648     }
   2649 }
   2650 
   2651     void
   2652 sendeof()
   2653 {
   2654     NET2ADD(IAC, xEOF);
   2655     printoption("SENT", IAC, xEOF);
   2656 }
   2657 
   2658     void
   2659 sendayt()
   2660 {
   2661     NET2ADD(IAC, AYT);
   2662     printoption("SENT", IAC, AYT);
   2663 }
   2664 
   2665 /*
   2666  * Send a window size update to the remote system.
   2667  */
   2668 
   2669     void
   2670 sendnaws()
   2671 {
   2672     long rows, cols;
   2673     unsigned char tmp[16];
   2674     register unsigned char *cp;
   2675 
   2676     if (my_state_is_wont(TELOPT_NAWS))
   2677 	return;
   2678 
   2679 #define	PUTSHORT(cp, x) { if ((*cp++ = ((x)>>8)&0xff) == IAC) *cp++ = IAC; \
   2680 			    if ((*cp++ = ((x))&0xff) == IAC) *cp++ = IAC; }
   2681 
   2682     if (TerminalWindowSize(&rows, &cols) == 0) {	/* Failed */
   2683 	return;
   2684     }
   2685 
   2686     cp = tmp;
   2687 
   2688     *cp++ = IAC;
   2689     *cp++ = SB;
   2690     *cp++ = TELOPT_NAWS;
   2691     PUTSHORT(cp, cols);
   2692     PUTSHORT(cp, rows);
   2693     *cp++ = IAC;
   2694     *cp++ = SE;
   2695     if (NETROOM() >= cp - tmp) {
   2696 	ring_supply_data(&netoring, tmp, cp-tmp);
   2697 	printsub('>', tmp+2, cp - tmp - 2);
   2698     }
   2699 }
   2700 
   2701     void
   2702 tel_enter_binary(rw)
   2703     int rw;
   2704 {
   2705     if (rw&1)
   2706 	send_do(TELOPT_BINARY, 1);
   2707     if (rw&2)
   2708 	send_will(TELOPT_BINARY, 1);
   2709 }
   2710 
   2711     void
   2712 tel_leave_binary(rw)
   2713     int rw;
   2714 {
   2715     if (rw&1)
   2716 	send_dont(TELOPT_BINARY, 1);
   2717     if (rw&2)
   2718 	send_wont(TELOPT_BINARY, 1);
   2719 }
   2720