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