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