xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.bin/telnet/sys_bsd.c (revision 9354:9559ac454e7e)
10Sstevel@tonic-gate /*
2*9354STim.Marsland@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate /*
70Sstevel@tonic-gate  * Copyright (c) 1988, 1990, 1993
80Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
90Sstevel@tonic-gate  *
100Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
110Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
120Sstevel@tonic-gate  * are met:
130Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
140Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
150Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
170Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
180Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
190Sstevel@tonic-gate  *    must display the following acknowledgement:
200Sstevel@tonic-gate  *	This product includes software developed by the University of
210Sstevel@tonic-gate  *	California, Berkeley and its contributors.
220Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
230Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
240Sstevel@tonic-gate  *    without specific prior written permission.
250Sstevel@tonic-gate  *
260Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
270Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
280Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
290Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
300Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
310Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
320Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
330Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
340Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
350Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
360Sstevel@tonic-gate  * SUCH DAMAGE.
370Sstevel@tonic-gate  *
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #ifndef lint
410Sstevel@tonic-gate static char sccsid[] = "@(#)sys_bsd.c	8.1 (Berkeley) 6/6/93";
420Sstevel@tonic-gate #endif /* not lint */
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate  * The following routines try to encapsulate what is system dependent
460Sstevel@tonic-gate  * (at least between 4.x and dos) which is used in telnet.c.
470Sstevel@tonic-gate  */
480Sstevel@tonic-gate 
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #include <fcntl.h>
510Sstevel@tonic-gate #include <sys/types.h>
520Sstevel@tonic-gate #include <sys/time.h>
530Sstevel@tonic-gate #include <sys/socket.h>
540Sstevel@tonic-gate #include <sys/uio.h>
550Sstevel@tonic-gate #include <signal.h>
560Sstevel@tonic-gate #include <errno.h>
570Sstevel@tonic-gate #include <arpa/telnet.h>
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include "ring.h"
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #include "defines.h"
620Sstevel@tonic-gate #include "externs.h"
630Sstevel@tonic-gate #include "types.h"
640Sstevel@tonic-gate 
650Sstevel@tonic-gate #define	SIG_FUNC_RET	void
660Sstevel@tonic-gate 
670Sstevel@tonic-gate int tout;			/* Output file descriptor */
680Sstevel@tonic-gate static int tin;			/* Input file descriptor */
690Sstevel@tonic-gate int net = -1;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #ifndef	USE_TERMIO
730Sstevel@tonic-gate struct	tchars otc = { 0 }, ntc = { 0 };
740Sstevel@tonic-gate struct	ltchars oltc = { 0 }, nltc = { 0 };
750Sstevel@tonic-gate struct	sgttyb ottyb = { 0 }, nttyb = { 0 };
760Sstevel@tonic-gate int	olmode = 0;
770Sstevel@tonic-gate #define	cfgetispeed(ptr)	(ptr)->sg_ispeed
780Sstevel@tonic-gate #define	cfgetospeed(ptr)	(ptr)->sg_ospeed
790Sstevel@tonic-gate #define	old_tc ottyb
800Sstevel@tonic-gate 
810Sstevel@tonic-gate #else	/* USE_TERMIO */
820Sstevel@tonic-gate static struct	termio old_tc = { 0 };
830Sstevel@tonic-gate extern struct termio new_tc;
840Sstevel@tonic-gate #endif	/* USE_TERMIO */
850Sstevel@tonic-gate 
860Sstevel@tonic-gate static fd_set ibits, obits, xbits;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate static SIG_FUNC_RET susp(int);
890Sstevel@tonic-gate void fatal_tty_error(char *doing_what);
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 
920Sstevel@tonic-gate void
init_sys()930Sstevel@tonic-gate init_sys()
940Sstevel@tonic-gate {
950Sstevel@tonic-gate 	tout = fileno(stdout);
960Sstevel@tonic-gate 	tin = fileno(stdin);
970Sstevel@tonic-gate 	FD_ZERO(&ibits);
980Sstevel@tonic-gate 	FD_ZERO(&obits);
990Sstevel@tonic-gate 	FD_ZERO(&xbits);
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	errno = 0;
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate int
TerminalWrite(buf,n)1060Sstevel@tonic-gate TerminalWrite(buf, n)
1070Sstevel@tonic-gate 	char *buf;
1080Sstevel@tonic-gate 	int  n;
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate 	return (write(tout, buf, n));
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate static int
TerminalRead(buf,n)1140Sstevel@tonic-gate TerminalRead(buf, n)
1150Sstevel@tonic-gate 	char *buf;
1160Sstevel@tonic-gate 	int  n;
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate 	return (read(tin, buf, n));
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate #ifdef	KLUDGELINEMODE
1220Sstevel@tonic-gate extern int kludgelinemode;
1230Sstevel@tonic-gate #endif
1240Sstevel@tonic-gate /*
1250Sstevel@tonic-gate  * TerminalSpecialChars()
1260Sstevel@tonic-gate  *
1270Sstevel@tonic-gate  * Look at an input character to see if it is a special character
1280Sstevel@tonic-gate  * and decide what to do.
1290Sstevel@tonic-gate  *
1300Sstevel@tonic-gate  * Output:
1310Sstevel@tonic-gate  *
1320Sstevel@tonic-gate  *	0	Don't add this character.
1330Sstevel@tonic-gate  *	1	Do add this character
1340Sstevel@tonic-gate  */
1350Sstevel@tonic-gate int
TerminalSpecialChars(c)1360Sstevel@tonic-gate TerminalSpecialChars(c)
1370Sstevel@tonic-gate 	int	c;
1380Sstevel@tonic-gate {
1390Sstevel@tonic-gate 	/*
1400Sstevel@tonic-gate 	 * Don't check for signal characters here.  If MODE_TRAPSIG is on,
1410Sstevel@tonic-gate 	 * then the various signal handlers will catch the characters.  If
1420Sstevel@tonic-gate 	 * the character in question gets here, then it must have been LNEXTed
1430Sstevel@tonic-gate 	 */
1440Sstevel@tonic-gate 	if (c == termQuitChar) {
1450Sstevel@tonic-gate #ifdef	KLUDGELINEMODE
1460Sstevel@tonic-gate 		if (kludgelinemode) {
1470Sstevel@tonic-gate 			if (sendbrk() == -1) {
1480Sstevel@tonic-gate 				/* This won't return. */
1490Sstevel@tonic-gate 				fatal_tty_error("write");
1500Sstevel@tonic-gate 			}
1510Sstevel@tonic-gate 			return (0);
1520Sstevel@tonic-gate 		}
1530Sstevel@tonic-gate #endif
1540Sstevel@tonic-gate 	} else if (c == termFlushChar) {
1550Sstevel@tonic-gate 		/* Transmit Abort Output */
1560Sstevel@tonic-gate 		if (xmitAO() == -1) {
1570Sstevel@tonic-gate 			/* This won't return. */
1580Sstevel@tonic-gate 			fatal_tty_error("write");
1590Sstevel@tonic-gate 		}
1600Sstevel@tonic-gate 		return (0);
1610Sstevel@tonic-gate 	} else if (!MODE_LOCAL_CHARS(globalmode)) {
1620Sstevel@tonic-gate 		if (c == termKillChar) {
1630Sstevel@tonic-gate 			xmitEL();
1640Sstevel@tonic-gate 			return (0);
1650Sstevel@tonic-gate 		} else if (c == termEraseChar) {
1660Sstevel@tonic-gate 			xmitEC();	/* Transmit Erase Character */
1670Sstevel@tonic-gate 			return (0);
1680Sstevel@tonic-gate 		}
1690Sstevel@tonic-gate 	}
1700Sstevel@tonic-gate 	return (1);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate /*
1750Sstevel@tonic-gate  * Flush output to the terminal
1760Sstevel@tonic-gate  */
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate void
TerminalFlushOutput()1790Sstevel@tonic-gate TerminalFlushOutput()
1800Sstevel@tonic-gate {
1810Sstevel@tonic-gate 	if (isatty(fileno(stdout))) {
1820Sstevel@tonic-gate 		(void) ioctl(fileno(stdout), TIOCFLUSH, NULL);
1830Sstevel@tonic-gate 	}
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate void
TerminalSaveState()1870Sstevel@tonic-gate TerminalSaveState()
1880Sstevel@tonic-gate {
1890Sstevel@tonic-gate #ifndef	USE_TERMIO
1900Sstevel@tonic-gate 	(void) ioctl(0, TIOCGETP, &ottyb);
1910Sstevel@tonic-gate 	(void) ioctl(0, TIOCGETC, &otc);
1920Sstevel@tonic-gate 	(void) ioctl(0, TIOCGLTC, &oltc);
1930Sstevel@tonic-gate 	(void) ioctl(0, TIOCLGET, &olmode);
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	ntc = otc;
1960Sstevel@tonic-gate 	nltc = oltc;
1970Sstevel@tonic-gate 	nttyb = ottyb;
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate #else	/* USE_TERMIO */
2000Sstevel@tonic-gate 	(void) tcgetattr(0, &old_tc);
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	new_tc = old_tc;
2030Sstevel@tonic-gate 	termAytChar = CONTROL('T');
2040Sstevel@tonic-gate #endif	/* USE_TERMIO */
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate cc_t *
tcval(func)2080Sstevel@tonic-gate tcval(func)
2090Sstevel@tonic-gate 	register int func;
2100Sstevel@tonic-gate {
2110Sstevel@tonic-gate 	switch (func) {
2120Sstevel@tonic-gate 	case SLC_IP:	return (&termIntChar);
2130Sstevel@tonic-gate 	case SLC_ABORT:	return (&termQuitChar);
2140Sstevel@tonic-gate 	case SLC_EOF:	return (&termEofChar);
2150Sstevel@tonic-gate 	case SLC_EC:	return (&termEraseChar);
2160Sstevel@tonic-gate 	case SLC_EL:	return (&termKillChar);
2170Sstevel@tonic-gate 	case SLC_XON:	return (&termStartChar);
2180Sstevel@tonic-gate 	case SLC_XOFF:	return (&termStopChar);
2190Sstevel@tonic-gate 	case SLC_FORW1:	return (&termForw1Char);
2200Sstevel@tonic-gate #ifdef	USE_TERMIO
2210Sstevel@tonic-gate 	case SLC_FORW2:	return (&termForw2Char);
2220Sstevel@tonic-gate 	case SLC_AO:	return (&termFlushChar);
2230Sstevel@tonic-gate 	case SLC_SUSP:	return (&termSuspChar);
2240Sstevel@tonic-gate 	case SLC_EW:	return (&termWerasChar);
2250Sstevel@tonic-gate 	case SLC_RP:	return (&termRprntChar);
2260Sstevel@tonic-gate 	case SLC_LNEXT:	return (&termLiteralNextChar);
2270Sstevel@tonic-gate #endif
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	case SLC_SYNCH:
2300Sstevel@tonic-gate 	case SLC_BRK:
2310Sstevel@tonic-gate 	case SLC_EOR:
2320Sstevel@tonic-gate 	default:
2330Sstevel@tonic-gate 		return ((cc_t *)0);
2340Sstevel@tonic-gate 	}
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate void
TerminalDefaultChars()2380Sstevel@tonic-gate TerminalDefaultChars()
2390Sstevel@tonic-gate {
2400Sstevel@tonic-gate #ifndef	USE_TERMIO
2410Sstevel@tonic-gate 	ntc = otc;
2420Sstevel@tonic-gate 	nltc = oltc;
2430Sstevel@tonic-gate 	nttyb.sg_kill = ottyb.sg_kill;
2440Sstevel@tonic-gate 	nttyb.sg_erase = ottyb.sg_erase;
2450Sstevel@tonic-gate #else	/* USE_TERMIO */
2460Sstevel@tonic-gate 	(void) memcpy(new_tc.c_cc, old_tc.c_cc, sizeof (old_tc.c_cc));
2470Sstevel@tonic-gate 	termAytChar = CONTROL('T');
2480Sstevel@tonic-gate #endif	/* USE_TERMIO */
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate /*
2520Sstevel@tonic-gate  * TerminalNewMode - set up terminal to a specific mode.
2530Sstevel@tonic-gate  *	MODE_ECHO: do local terminal echo
2540Sstevel@tonic-gate  *	MODE_FLOW: do local flow control
2550Sstevel@tonic-gate  *	MODE_TRAPSIG: do local mapping to TELNET IAC sequences
2560Sstevel@tonic-gate  *	MODE_EDIT: do local line editing
2570Sstevel@tonic-gate  *
2580Sstevel@tonic-gate  *	Command mode:
2590Sstevel@tonic-gate  *		MODE_ECHO|MODE_EDIT|MODE_FLOW|MODE_TRAPSIG
2600Sstevel@tonic-gate  *		local echo
2610Sstevel@tonic-gate  *		local editing
2620Sstevel@tonic-gate  *		local xon/xoff
2630Sstevel@tonic-gate  *		local signal mapping
2640Sstevel@tonic-gate  *
2650Sstevel@tonic-gate  *	Linemode:
2660Sstevel@tonic-gate  *		local/no editing
2670Sstevel@tonic-gate  *	Both Linemode and Single Character mode:
2680Sstevel@tonic-gate  *		local/remote echo
2690Sstevel@tonic-gate  *		local/no xon/xoff
2700Sstevel@tonic-gate  *		local/no signal mapping
2710Sstevel@tonic-gate  */
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate void
TerminalNewMode(f)2750Sstevel@tonic-gate TerminalNewMode(f)
2760Sstevel@tonic-gate 	register int f;
2770Sstevel@tonic-gate {
2780Sstevel@tonic-gate 	static int prevmode = -2;	/* guaranteed unique */
2790Sstevel@tonic-gate #ifndef	USE_TERMIO
2800Sstevel@tonic-gate 	struct tchars tc;
2810Sstevel@tonic-gate 	struct ltchars ltc;
2820Sstevel@tonic-gate 	struct sgttyb sb;
2830Sstevel@tonic-gate 	int lmode;
2840Sstevel@tonic-gate #else	/* USE_TERMIO */
2850Sstevel@tonic-gate 	struct termio tmp_tc;
2860Sstevel@tonic-gate #endif	/* USE_TERMIO */
2870Sstevel@tonic-gate 	int onoff;
2880Sstevel@tonic-gate 	int old;
2890Sstevel@tonic-gate 	cc_t esc;
2900Sstevel@tonic-gate 	sigset_t nset;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	globalmode = f&~MODE_FORCE;
2930Sstevel@tonic-gate 	if (prevmode == f)
2940Sstevel@tonic-gate 		return;
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	/*
2970Sstevel@tonic-gate 	 * Write any outstanding data before switching modes
2980Sstevel@tonic-gate 	 * ttyflush() returns 0 only when there was no data
2990Sstevel@tonic-gate 	 * to write out; it returns -1 if it couldn't do
3000Sstevel@tonic-gate 	 * anything at all, returns -2 if there was a write
3010Sstevel@tonic-gate 	 * error (other than EWOULDBLOCK), and otherwise it
3020Sstevel@tonic-gate 	 * returns 1 + the number of characters left to write.
3030Sstevel@tonic-gate 	 */
3040Sstevel@tonic-gate #ifndef	USE_TERMIO
3050Sstevel@tonic-gate 	/*
3060Sstevel@tonic-gate 	 * We would really like ask the kernel to wait for the output
3070Sstevel@tonic-gate 	 * to drain, like we can do with the TCSADRAIN, but we don't have
3080Sstevel@tonic-gate 	 * that option.  The only ioctl that waits for the output to
3090Sstevel@tonic-gate 	 * drain, TIOCSETP, also flushes the input queue, which is NOT
3100Sstevel@tonic-gate 	 * what we want(TIOCSETP is like TCSADFLUSH).
3110Sstevel@tonic-gate 	 */
3120Sstevel@tonic-gate #endif
3130Sstevel@tonic-gate 	old = ttyflush(SYNCHing|flushout);
3140Sstevel@tonic-gate 	if (old == -1 || old > 1) {
3150Sstevel@tonic-gate #ifdef	USE_TERMIO
3160Sstevel@tonic-gate 		(void) tcgetattr(tin, &tmp_tc);
3170Sstevel@tonic-gate #endif	/* USE_TERMIO */
3180Sstevel@tonic-gate 		do {
3190Sstevel@tonic-gate 			/*
3200Sstevel@tonic-gate 			 * Wait for data to drain, then flush again.
3210Sstevel@tonic-gate 			 */
3220Sstevel@tonic-gate #ifdef	USE_TERMIO
3230Sstevel@tonic-gate 			(void) tcsetattr(tin, TCSADRAIN, &tmp_tc);
3240Sstevel@tonic-gate #endif	/* USE_TERMIO */
3250Sstevel@tonic-gate 			old = ttyflush(SYNCHing|flushout);
3260Sstevel@tonic-gate 		} while (old == -1 || old > 1);
3270Sstevel@tonic-gate 	}
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	old = prevmode;
3300Sstevel@tonic-gate 	prevmode = f&~MODE_FORCE;
3310Sstevel@tonic-gate #ifndef	USE_TERMIO
3320Sstevel@tonic-gate 	sb = nttyb;
3330Sstevel@tonic-gate 	tc = ntc;
3340Sstevel@tonic-gate 	ltc = nltc;
3350Sstevel@tonic-gate 	lmode = olmode;
3360Sstevel@tonic-gate #else
3370Sstevel@tonic-gate 	tmp_tc = new_tc;
3380Sstevel@tonic-gate #endif
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	if (f&MODE_ECHO) {
3410Sstevel@tonic-gate #ifndef	USE_TERMIO
3420Sstevel@tonic-gate 		sb.sg_flags |= ECHO;
3430Sstevel@tonic-gate #else
3440Sstevel@tonic-gate 		tmp_tc.c_lflag |= ECHO;
3450Sstevel@tonic-gate 		tmp_tc.c_oflag |= ONLCR;
3460Sstevel@tonic-gate 		if (crlf)
3470Sstevel@tonic-gate 			tmp_tc.c_iflag |= ICRNL;
3480Sstevel@tonic-gate #endif
3490Sstevel@tonic-gate 	} else {
3500Sstevel@tonic-gate #ifndef	USE_TERMIO
3510Sstevel@tonic-gate 		sb.sg_flags &= ~ECHO;
3520Sstevel@tonic-gate #else
3530Sstevel@tonic-gate 		tmp_tc.c_lflag &= ~ECHO;
3540Sstevel@tonic-gate 		tmp_tc.c_oflag &= ~ONLCR;
3550Sstevel@tonic-gate #ifdef notdef
3560Sstevel@tonic-gate 		if (crlf)
3570Sstevel@tonic-gate 			tmp_tc.c_iflag &= ~ICRNL;
3580Sstevel@tonic-gate #endif
3590Sstevel@tonic-gate #endif
3600Sstevel@tonic-gate 	}
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	if ((f&MODE_FLOW) == 0) {
3630Sstevel@tonic-gate #ifndef	USE_TERMIO
3640Sstevel@tonic-gate 		tc.t_startc = _POSIX_VDISABLE;
3650Sstevel@tonic-gate 		tc.t_stopc = _POSIX_VDISABLE;
3660Sstevel@tonic-gate #else
3670Sstevel@tonic-gate 		tmp_tc.c_iflag &= ~(IXOFF|IXON); /* Leave the IXANY bit alone */
3680Sstevel@tonic-gate 	} else {
3690Sstevel@tonic-gate 		if (restartany < 0) {
3700Sstevel@tonic-gate 			/* Leave the IXANY bit alone */
3710Sstevel@tonic-gate 			tmp_tc.c_iflag |= IXOFF|IXON;
3720Sstevel@tonic-gate 		} else if (restartany > 0) {
3730Sstevel@tonic-gate 			tmp_tc.c_iflag |= IXOFF|IXON|IXANY;
3740Sstevel@tonic-gate 		} else {
3750Sstevel@tonic-gate 			tmp_tc.c_iflag |= IXOFF|IXON;
3760Sstevel@tonic-gate 			tmp_tc.c_iflag &= ~IXANY;
3770Sstevel@tonic-gate 		}
3780Sstevel@tonic-gate #endif
3790Sstevel@tonic-gate 	}
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	if ((f&MODE_TRAPSIG) == 0) {
3820Sstevel@tonic-gate #ifndef	USE_TERMIO
3830Sstevel@tonic-gate 		tc.t_intrc = _POSIX_VDISABLE;
3840Sstevel@tonic-gate 		tc.t_quitc = _POSIX_VDISABLE;
3850Sstevel@tonic-gate 		tc.t_eofc = _POSIX_VDISABLE;
3860Sstevel@tonic-gate 		ltc.t_suspc = _POSIX_VDISABLE;
3870Sstevel@tonic-gate 		ltc.t_dsuspc = _POSIX_VDISABLE;
3880Sstevel@tonic-gate #else
3890Sstevel@tonic-gate 		tmp_tc.c_lflag &= ~ISIG;
3900Sstevel@tonic-gate #endif
3910Sstevel@tonic-gate 		localchars = 0;
3920Sstevel@tonic-gate 	} else {
3930Sstevel@tonic-gate #ifdef	USE_TERMIO
3940Sstevel@tonic-gate 		tmp_tc.c_lflag |= ISIG;
3950Sstevel@tonic-gate #endif
3960Sstevel@tonic-gate 		localchars = 1;
3970Sstevel@tonic-gate 	}
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	if (f&MODE_EDIT) {
4000Sstevel@tonic-gate #ifndef	USE_TERMIO
4010Sstevel@tonic-gate 		sb.sg_flags &= ~CBREAK;
4020Sstevel@tonic-gate 		sb.sg_flags |= CRMOD;
4030Sstevel@tonic-gate #else
4040Sstevel@tonic-gate 		tmp_tc.c_lflag |= ICANON;
4050Sstevel@tonic-gate #endif
4060Sstevel@tonic-gate 	} else {
4070Sstevel@tonic-gate #ifndef	USE_TERMIO
4080Sstevel@tonic-gate 		sb.sg_flags |= CBREAK;
4090Sstevel@tonic-gate 		if (f&MODE_ECHO)
4100Sstevel@tonic-gate 			sb.sg_flags |= CRMOD;
4110Sstevel@tonic-gate 		else
4120Sstevel@tonic-gate 			sb.sg_flags &= ~CRMOD;
4130Sstevel@tonic-gate #else
4140Sstevel@tonic-gate 		tmp_tc.c_lflag &= ~ICANON;
4150Sstevel@tonic-gate 		tmp_tc.c_iflag &= ~ICRNL;
4160Sstevel@tonic-gate 		tmp_tc.c_cc[VMIN] = 1;
4170Sstevel@tonic-gate 		tmp_tc.c_cc[VTIME] = 0;
4180Sstevel@tonic-gate #endif
4190Sstevel@tonic-gate 	}
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	if ((f&(MODE_EDIT|MODE_TRAPSIG)) == 0) {
4220Sstevel@tonic-gate #ifndef	USE_TERMIO
4230Sstevel@tonic-gate 		ltc.t_lnextc = _POSIX_VDISABLE;
4240Sstevel@tonic-gate #else
4250Sstevel@tonic-gate 		tmp_tc.c_cc[VLNEXT] = (cc_t)(_POSIX_VDISABLE);
4260Sstevel@tonic-gate #endif
4270Sstevel@tonic-gate 	}
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	if (f&MODE_SOFT_TAB) {
4300Sstevel@tonic-gate #ifndef USE_TERMIO
4310Sstevel@tonic-gate 		sb.sg_flags |= XTABS;
4320Sstevel@tonic-gate #else
4330Sstevel@tonic-gate 		tmp_tc.c_oflag &= ~TABDLY;
4340Sstevel@tonic-gate 		tmp_tc.c_oflag |= TAB3;
4350Sstevel@tonic-gate #endif
4360Sstevel@tonic-gate 	} else {
4370Sstevel@tonic-gate #ifndef USE_TERMIO
4380Sstevel@tonic-gate 		sb.sg_flags &= ~XTABS;
4390Sstevel@tonic-gate #else
4400Sstevel@tonic-gate 		tmp_tc.c_oflag &= ~TABDLY;
4410Sstevel@tonic-gate #endif
4420Sstevel@tonic-gate 	}
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	if (f&MODE_LIT_ECHO) {
4450Sstevel@tonic-gate #ifndef USE_TERMIO
4460Sstevel@tonic-gate 		lmode &= ~LCTLECH;
4470Sstevel@tonic-gate #else
4480Sstevel@tonic-gate 		tmp_tc.c_lflag &= ~ECHOCTL;
4490Sstevel@tonic-gate #endif
4500Sstevel@tonic-gate 	} else {
4510Sstevel@tonic-gate #ifndef USE_TERMIO
4520Sstevel@tonic-gate 		lmode |= LCTLECH;
4530Sstevel@tonic-gate #else
4540Sstevel@tonic-gate 		tmp_tc.c_lflag |= ECHOCTL;
4550Sstevel@tonic-gate #endif
4560Sstevel@tonic-gate 	}
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	if (f == -1) {
4590Sstevel@tonic-gate 		onoff = 0;
4600Sstevel@tonic-gate 	} else {
4610Sstevel@tonic-gate #ifndef	USE_TERMIO
4620Sstevel@tonic-gate 		if (f & MODE_OUTBIN)
4630Sstevel@tonic-gate 			lmode |= LLITOUT;
4640Sstevel@tonic-gate 		else
4650Sstevel@tonic-gate 			lmode &= ~LLITOUT;
4660Sstevel@tonic-gate #else
4670Sstevel@tonic-gate 		if (f & MODE_OUTBIN) {
4680Sstevel@tonic-gate 			tmp_tc.c_cflag &= ~(CSIZE|PARENB);
4690Sstevel@tonic-gate 			tmp_tc.c_cflag |= CS8;
4700Sstevel@tonic-gate 			tmp_tc.c_oflag &= ~OPOST;
4710Sstevel@tonic-gate 		} else {
4720Sstevel@tonic-gate 			tmp_tc.c_cflag &= ~(CSIZE|PARENB);
4730Sstevel@tonic-gate 			tmp_tc.c_cflag |= old_tc.c_cflag & (CSIZE|PARENB);
4740Sstevel@tonic-gate 			tmp_tc.c_oflag |= OPOST;
4750Sstevel@tonic-gate 		}
4760Sstevel@tonic-gate #endif
4770Sstevel@tonic-gate 		onoff = 1;
4780Sstevel@tonic-gate 	}
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	if (f != -1) {
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 		(void) signal(SIGTSTP, susp);
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate #if	defined(USE_TERMIO) && defined(NOKERNINFO)
4850Sstevel@tonic-gate 		tmp_tc.c_lflag |= NOKERNINFO;
4860Sstevel@tonic-gate #endif
4870Sstevel@tonic-gate 		/*
4880Sstevel@tonic-gate 		 * We don't want to process ^Y here.  It's just another
4890Sstevel@tonic-gate 		 * character that we'll pass on to the back end.  It has
4900Sstevel@tonic-gate 		 * to process it because it will be processed when the
4910Sstevel@tonic-gate 		 * user attempts to read it, not when we send it.
4920Sstevel@tonic-gate 		 */
4930Sstevel@tonic-gate #ifndef	USE_TERMIO
4940Sstevel@tonic-gate 		ltc.t_dsuspc = _POSIX_VDISABLE;
4950Sstevel@tonic-gate #else
4960Sstevel@tonic-gate 		tmp_tc.c_cc[VDSUSP] = (cc_t)(_POSIX_VDISABLE);
4970Sstevel@tonic-gate #endif
4980Sstevel@tonic-gate #ifdef	USE_TERMIO
4990Sstevel@tonic-gate 		/*
5000Sstevel@tonic-gate 		 * If the VEOL character is already set, then use VEOL2,
5010Sstevel@tonic-gate 		 * otherwise use VEOL.
5020Sstevel@tonic-gate 		 */
5030Sstevel@tonic-gate 		esc = (rlogin != _POSIX_VDISABLE) ? rlogin : escape;
5040Sstevel@tonic-gate 		if ((tmp_tc.c_cc[VEOL] != esc)
5050Sstevel@tonic-gate 		    /* XXX */ &&
5060Sstevel@tonic-gate 		    (tmp_tc.c_cc[VEOL2] != esc)
5070Sstevel@tonic-gate 		    /* XXX */) {
5080Sstevel@tonic-gate 			if (tmp_tc.c_cc[VEOL] == (cc_t)(_POSIX_VDISABLE))
5090Sstevel@tonic-gate 				tmp_tc.c_cc[VEOL] = esc;
5100Sstevel@tonic-gate 			else if (tmp_tc.c_cc[VEOL2] == (cc_t)(_POSIX_VDISABLE))
5110Sstevel@tonic-gate 				tmp_tc.c_cc[VEOL2] = esc;
5120Sstevel@tonic-gate 		}
5130Sstevel@tonic-gate #else
5140Sstevel@tonic-gate 		if (tc.t_brkc == (cc_t)(_POSIX_VDISABLE))
5150Sstevel@tonic-gate 			tc.t_brkc = esc;
5160Sstevel@tonic-gate #endif
5170Sstevel@tonic-gate 	} else {
5180Sstevel@tonic-gate 		(void) signal(SIGTSTP, SIG_DFL);
5190Sstevel@tonic-gate 		(void) sigemptyset(&nset);
5200Sstevel@tonic-gate 		(void) sigaddset(&nset, SIGTSTP);
5210Sstevel@tonic-gate 		(void) sigprocmask(SIG_UNBLOCK, &nset, 0);
5220Sstevel@tonic-gate #ifndef USE_TERMIO
5230Sstevel@tonic-gate 		ltc = oltc;
5240Sstevel@tonic-gate 		tc = otc;
5250Sstevel@tonic-gate 		sb = ottyb;
5260Sstevel@tonic-gate 		lmode = olmode;
5270Sstevel@tonic-gate #else
5280Sstevel@tonic-gate 		tmp_tc = old_tc;
5290Sstevel@tonic-gate #endif
5300Sstevel@tonic-gate 	}
5310Sstevel@tonic-gate 	if (isatty(tin)) {
5320Sstevel@tonic-gate #ifndef USE_TERMIO
5330Sstevel@tonic-gate 		(void) ioctl(tin, TIOCLSET, &lmode);
5340Sstevel@tonic-gate 		(void) ioctl(tin, TIOCSLTC, &ltc);
5350Sstevel@tonic-gate 		(void) ioctl(tin, TIOCSETC, &tc);
5360Sstevel@tonic-gate 		(void) ioctl(tin, TIOCSETN, &sb);
5370Sstevel@tonic-gate #else
5380Sstevel@tonic-gate 		if (tcsetattr(tin, TCSADRAIN, &tmp_tc) < 0)
5390Sstevel@tonic-gate 			(void) tcsetattr(tin, TCSANOW, &tmp_tc);
5400Sstevel@tonic-gate #endif
5410Sstevel@tonic-gate 		(void) ioctl(tin, FIONBIO, &onoff);
5420Sstevel@tonic-gate 		(void) ioctl(tout, FIONBIO, &onoff);
5430Sstevel@tonic-gate 	}
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate }
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate /*
5480Sstevel@tonic-gate  * This code assumes that the values B0, B50, B75...
5490Sstevel@tonic-gate  * are in ascending order.  They do not have to be
5500Sstevel@tonic-gate  * contiguous.
5510Sstevel@tonic-gate  */
5520Sstevel@tonic-gate static struct termspeeds {
5530Sstevel@tonic-gate 	int speed;
5540Sstevel@tonic-gate 	int value;
5550Sstevel@tonic-gate } termspeeds[] = {
5560Sstevel@tonic-gate 	{ 0,	B0 },		{ 50,	B50 },		{ 75, B75 },
5570Sstevel@tonic-gate 	{ 110,	B110 },		{ 134,	B134 },		{ 150, B150 },
5580Sstevel@tonic-gate 	{ 200,	B200 },		{ 300,   B300 },	{ 600, B600 },
5590Sstevel@tonic-gate 	{ 1200,	B1200 },	{ 1800,  B1800 },	{ 2400, B2400 },
5600Sstevel@tonic-gate 	{ 4800,	B4800 },	{ 9600,  B9600 },	{ 19200, B19200 },
5610Sstevel@tonic-gate 	{ 38400, B38400 },	{ 57600, B57600 },	{ 76800, B76800 },
5620Sstevel@tonic-gate 	{ 115200, B115200 },	{ 153600, B153600 },	{ 230400, B230400 },
563*9354STim.Marsland@Sun.COM 	{ 307200, B307200 },	{ 460800, B460800 },	{ 921600, B921600 },
564*9354STim.Marsland@Sun.COM 	{ -1, B0 }
5650Sstevel@tonic-gate };
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate void
TerminalSpeeds(ispeed,ospeed)5680Sstevel@tonic-gate TerminalSpeeds(ispeed, ospeed)
5690Sstevel@tonic-gate 	int *ispeed;
5700Sstevel@tonic-gate 	int *ospeed;
5710Sstevel@tonic-gate {
5720Sstevel@tonic-gate 	register struct termspeeds *tp;
5730Sstevel@tonic-gate 	register int in, out;
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 	out = cfgetospeed(&old_tc);
5760Sstevel@tonic-gate 	in = cfgetispeed(&old_tc);
5770Sstevel@tonic-gate 	if (in == 0)
5780Sstevel@tonic-gate 		in = out;
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 	tp = termspeeds;
5810Sstevel@tonic-gate 	while ((tp->speed != -1) && (tp->value < in)) {
5820Sstevel@tonic-gate 		tp++;
5830Sstevel@tonic-gate 	}
5840Sstevel@tonic-gate 	if (tp->speed == -1)
5850Sstevel@tonic-gate 		tp--;			/* back up to fastest defined speed */
5860Sstevel@tonic-gate 	*ispeed = tp->speed;
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	tp = termspeeds;
5890Sstevel@tonic-gate 	while ((tp->speed != -1) && (tp->value < out)) {
5900Sstevel@tonic-gate 		tp++;
5910Sstevel@tonic-gate 	}
5920Sstevel@tonic-gate 	if (tp->speed == -1)
5930Sstevel@tonic-gate 		tp--;
5940Sstevel@tonic-gate 	*ospeed = tp->speed;
5950Sstevel@tonic-gate }
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate int
TerminalWindowSize(rows,cols)5980Sstevel@tonic-gate TerminalWindowSize(rows, cols)
5990Sstevel@tonic-gate 	unsigned short *rows, *cols;
6000Sstevel@tonic-gate {
6010Sstevel@tonic-gate 	struct winsize ws;
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) >= 0) {
6040Sstevel@tonic-gate 		*rows = ws.ws_row;
6050Sstevel@tonic-gate 		*cols = ws.ws_col;
6060Sstevel@tonic-gate 		return (1);
6070Sstevel@tonic-gate 	}
6080Sstevel@tonic-gate 	return (0);
6090Sstevel@tonic-gate }
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate static void
NetNonblockingIO(fd,onoff)6120Sstevel@tonic-gate NetNonblockingIO(fd, onoff)
6130Sstevel@tonic-gate 	int fd;
6140Sstevel@tonic-gate 	int onoff;
6150Sstevel@tonic-gate {
6160Sstevel@tonic-gate 	(void) ioctl(fd, FIONBIO, &onoff);
6170Sstevel@tonic-gate }
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate /*
6200Sstevel@tonic-gate  * Various signal handling routines.
6210Sstevel@tonic-gate  */
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate /* ARGSUSED */
6240Sstevel@tonic-gate static SIG_FUNC_RET
deadpeer(sig)6250Sstevel@tonic-gate deadpeer(sig)
6260Sstevel@tonic-gate 	int sig;
6270Sstevel@tonic-gate {
6280Sstevel@tonic-gate 	/*
6290Sstevel@tonic-gate 	 * Once is all we should catch SIGPIPE.  If we get it again,
6300Sstevel@tonic-gate 	 * it means we tried to put still more data out to a pipe
6310Sstevel@tonic-gate 	 * which has disappeared.  In that case, telnet will exit.
6320Sstevel@tonic-gate 	 */
6330Sstevel@tonic-gate 	(void) signal(SIGPIPE, SIG_IGN);
6340Sstevel@tonic-gate 	flushout = 1;
6350Sstevel@tonic-gate 	setcommandmode();
6360Sstevel@tonic-gate 	longjmp(peerdied, -1);
6370Sstevel@tonic-gate }
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate boolean_t intr_happened	= B_FALSE;
6400Sstevel@tonic-gate boolean_t intr_waiting	= B_FALSE;
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate /* ARGSUSED */
6430Sstevel@tonic-gate static SIG_FUNC_RET
intr(sig)6440Sstevel@tonic-gate intr(sig)
6450Sstevel@tonic-gate 	int sig;
6460Sstevel@tonic-gate {
6470Sstevel@tonic-gate 	if (intr_waiting) {
6480Sstevel@tonic-gate 		intr_happened = 1;
6490Sstevel@tonic-gate 		return;
6500Sstevel@tonic-gate 	}
6510Sstevel@tonic-gate 	(void) signal(SIGINT, intr);
6520Sstevel@tonic-gate 	if (localchars) {
6530Sstevel@tonic-gate 		intp();
6540Sstevel@tonic-gate 		return;
6550Sstevel@tonic-gate 	}
6560Sstevel@tonic-gate 	setcommandmode();
6570Sstevel@tonic-gate 	longjmp(toplevel, -1);
6580Sstevel@tonic-gate }
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate /* ARGSUSED */
6610Sstevel@tonic-gate static SIG_FUNC_RET
intr2(sig)6620Sstevel@tonic-gate intr2(sig)
6630Sstevel@tonic-gate 	int sig;
6640Sstevel@tonic-gate {
6650Sstevel@tonic-gate 	(void) signal(SIGQUIT, intr2);
6660Sstevel@tonic-gate 	if (localchars) {
6670Sstevel@tonic-gate 		/*
6680Sstevel@tonic-gate 		 * Ignore return to the next two function calls
6690Sstevel@tonic-gate 		 * since we're doing SIGQUIT
6700Sstevel@tonic-gate 		 */
6710Sstevel@tonic-gate #ifdef	KLUDGELINEMODE
6720Sstevel@tonic-gate 		if (kludgelinemode) {
6730Sstevel@tonic-gate 			(void) sendbrk();
6740Sstevel@tonic-gate 		}
6750Sstevel@tonic-gate 		else
6760Sstevel@tonic-gate #endif
6770Sstevel@tonic-gate 			sendabort();
6780Sstevel@tonic-gate 		return;
6790Sstevel@tonic-gate 	}
6800Sstevel@tonic-gate }
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate /* ARGSUSED */
6830Sstevel@tonic-gate static SIG_FUNC_RET
susp(sig)6840Sstevel@tonic-gate susp(sig)
6850Sstevel@tonic-gate 	int sig;
6860Sstevel@tonic-gate {
6870Sstevel@tonic-gate 	(void) signal(SIGTSTP, susp);
6880Sstevel@tonic-gate 	if ((rlogin != _POSIX_VDISABLE) && rlogin_susp())
6890Sstevel@tonic-gate 		return;
6900Sstevel@tonic-gate 	if (localchars)
6910Sstevel@tonic-gate 		sendsusp();
6920Sstevel@tonic-gate }
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate /* ARGSUSED */
6950Sstevel@tonic-gate static SIG_FUNC_RET
sendwin(sig)6960Sstevel@tonic-gate sendwin(sig)
6970Sstevel@tonic-gate 	int sig;
6980Sstevel@tonic-gate {
6990Sstevel@tonic-gate 	(void) signal(SIGWINCH, sendwin);
7000Sstevel@tonic-gate 	if (connected) {
7010Sstevel@tonic-gate 		sendnaws();
7020Sstevel@tonic-gate 	}
7030Sstevel@tonic-gate }
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate void
sys_telnet_init()7060Sstevel@tonic-gate sys_telnet_init()
7070Sstevel@tonic-gate {
7080Sstevel@tonic-gate 	(void) signal(SIGINT, intr);
7090Sstevel@tonic-gate 	(void) signal(SIGQUIT, intr2);
7100Sstevel@tonic-gate 	(void) signal(SIGPIPE, deadpeer);
7110Sstevel@tonic-gate 	(void) signal(SIGWINCH, sendwin);
7120Sstevel@tonic-gate 	(void) signal(SIGTSTP, susp);
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 	setconnmode(0);
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	NetNonblockingIO(net, 1);
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	if (SetSockOpt(net, SOL_SOCKET, SO_OOBINLINE, 1) == -1) {
7190Sstevel@tonic-gate 		perror("SetSockOpt");
7200Sstevel@tonic-gate 	}
7210Sstevel@tonic-gate }
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate /*
7250Sstevel@tonic-gate  * fatal_tty_error -
7260Sstevel@tonic-gate  *	Handle case where there is an unrecoverable error on the tty
7270Sstevel@tonic-gate  *      connections.  Print an error, reset the terminal settings
7280Sstevel@tonic-gate  *	and get out as painlessly as possible.
7290Sstevel@tonic-gate  */
7300Sstevel@tonic-gate void
fatal_tty_error(char * doing_what)7310Sstevel@tonic-gate fatal_tty_error(char *doing_what)
7320Sstevel@tonic-gate {
7330Sstevel@tonic-gate 	TerminalNewMode(-1);
7340Sstevel@tonic-gate 	(void) fprintf(stderr, "Error processing %s:  %s\n", doing_what,
735*9354STim.Marsland@Sun.COM 	    strerror(errno));
7360Sstevel@tonic-gate 	exit(1);
7370Sstevel@tonic-gate }
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate /*
7410Sstevel@tonic-gate  * Process rings -
7420Sstevel@tonic-gate  *
7430Sstevel@tonic-gate  *	This routine tries to fill up/empty our various rings.
7440Sstevel@tonic-gate  *
7450Sstevel@tonic-gate  *	The parameter specifies whether this is a poll operation,
7460Sstevel@tonic-gate  *	or a block-until-something-happens operation.
7470Sstevel@tonic-gate  *
7480Sstevel@tonic-gate  *	The return value is 1 if something happened, 0 if not.
7490Sstevel@tonic-gate  */
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate int
process_rings(netin,netout,netex,ttyin,ttyout,poll)7520Sstevel@tonic-gate process_rings(netin, netout, netex, ttyin, ttyout, poll)
7530Sstevel@tonic-gate 	int poll;		/* If 0, then block until something to do */
7540Sstevel@tonic-gate {
7550Sstevel@tonic-gate 	register int c;
7560Sstevel@tonic-gate 	/*
7570Sstevel@tonic-gate 	 * One wants to be a bit careful about setting returnValue
7580Sstevel@tonic-gate 	 * to one, since a one implies we did some useful work,
7590Sstevel@tonic-gate 	 * and therefore probably won't be called to block next
7600Sstevel@tonic-gate 	 * time (TN3270 mode only).
7610Sstevel@tonic-gate 	 */
7620Sstevel@tonic-gate 	int returnValue = 0;
7630Sstevel@tonic-gate 	static struct timeval TimeValue = { 0 };
7640Sstevel@tonic-gate 	int i;
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	if (netout) {
7670Sstevel@tonic-gate 		FD_SET(net, &obits);
7680Sstevel@tonic-gate 	}
7690Sstevel@tonic-gate 	if (ttyout) {
7700Sstevel@tonic-gate 		FD_SET(tout, &obits);
7710Sstevel@tonic-gate 	}
7720Sstevel@tonic-gate 	if (ttyin) {
7730Sstevel@tonic-gate 		FD_SET(tin, &ibits);
7740Sstevel@tonic-gate 	}
7750Sstevel@tonic-gate 	if (netin) {
7760Sstevel@tonic-gate 		FD_SET(net, &ibits);
7770Sstevel@tonic-gate 	}
7780Sstevel@tonic-gate 	if (netex) {
7790Sstevel@tonic-gate 		FD_SET(net, &xbits);
7800Sstevel@tonic-gate 	}
7810Sstevel@tonic-gate 	if ((c = select(16, &ibits, &obits, &xbits,
7820Sstevel@tonic-gate 			(poll == 0) ? NULL : &TimeValue)) < 0) {
7830Sstevel@tonic-gate 		if (c == -1) {
7840Sstevel@tonic-gate 			/*
7850Sstevel@tonic-gate 			 * we can get EINTR if we are in line mode,
7860Sstevel@tonic-gate 			 * and the user does an escape (TSTP), or
7870Sstevel@tonic-gate 			 * some other signal generator.
7880Sstevel@tonic-gate 			 */
7890Sstevel@tonic-gate 			if (errno == EINTR) {
7900Sstevel@tonic-gate 				return (0);
7910Sstevel@tonic-gate 			}
7920Sstevel@tonic-gate 			/* I don't like this, does it ever happen? */
7930Sstevel@tonic-gate 			(void) printf("sleep(5) from telnet, after select\r\n");
7940Sstevel@tonic-gate 			(void) sleep(5);
7950Sstevel@tonic-gate 		}
7960Sstevel@tonic-gate 		return (0);
7970Sstevel@tonic-gate 	}
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 	/*
8000Sstevel@tonic-gate 	 * Any urgent data?
8010Sstevel@tonic-gate 	 */
8020Sstevel@tonic-gate 	if (FD_ISSET(net, &xbits)) {
8030Sstevel@tonic-gate 		FD_CLR(net, &xbits);
8040Sstevel@tonic-gate 		SYNCHing = 1;
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 		/* flush any data that is already enqueued */
8070Sstevel@tonic-gate 		i = ttyflush(1);
8080Sstevel@tonic-gate 		if (i == -2) {
8090Sstevel@tonic-gate 			/* This will not return. */
8100Sstevel@tonic-gate 			fatal_tty_error("write");
8110Sstevel@tonic-gate 		}
8120Sstevel@tonic-gate 	}
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 	/*
8150Sstevel@tonic-gate 	 * Something to read from the network...
8160Sstevel@tonic-gate 	 */
8170Sstevel@tonic-gate 	if (FD_ISSET(net, &ibits)) {
8180Sstevel@tonic-gate 		int canread;
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 		FD_CLR(net, &ibits);
8210Sstevel@tonic-gate 		canread = ring_empty_consecutive(&netiring);
8220Sstevel@tonic-gate 		c = recv(net, netiring.supply, canread, 0);
8230Sstevel@tonic-gate 		if (c < 0 && errno == EWOULDBLOCK) {
8240Sstevel@tonic-gate 			c = 0;
8250Sstevel@tonic-gate 		} else if (c <= 0) {
8260Sstevel@tonic-gate 			return (-1);
8270Sstevel@tonic-gate 		}
8280Sstevel@tonic-gate 		if (netdata) {
8290Sstevel@tonic-gate 			Dump('<', netiring.supply, c);
8300Sstevel@tonic-gate 		}
8310Sstevel@tonic-gate 		if (c)
8320Sstevel@tonic-gate 			ring_supplied(&netiring, c);
8330Sstevel@tonic-gate 		returnValue = 1;
8340Sstevel@tonic-gate 	}
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	/*
8370Sstevel@tonic-gate 	 * Something to read from the tty...
8380Sstevel@tonic-gate 	 */
8390Sstevel@tonic-gate 	if (FD_ISSET(tin, &ibits)) {
8400Sstevel@tonic-gate 		FD_CLR(tin, &ibits);
8410Sstevel@tonic-gate 		c = TerminalRead((char *)ttyiring.supply,
8420Sstevel@tonic-gate 		    ring_empty_consecutive(&ttyiring));
8430Sstevel@tonic-gate 		if (c < 0) {
8440Sstevel@tonic-gate 			if (errno != EWOULDBLOCK) {
8450Sstevel@tonic-gate 				/* This will not return. */
8460Sstevel@tonic-gate 				fatal_tty_error("read");
8470Sstevel@tonic-gate 			}
8480Sstevel@tonic-gate 			c = 0;
8490Sstevel@tonic-gate 		} else {
8500Sstevel@tonic-gate 			/* EOF detection for line mode!!!! */
8510Sstevel@tonic-gate 			if ((c == 0) && MODE_LOCAL_CHARS(globalmode) &&
8520Sstevel@tonic-gate 			    isatty(tin)) {
8530Sstevel@tonic-gate 				/* must be an EOF... */
8540Sstevel@tonic-gate 				eof_pending = 1;
8550Sstevel@tonic-gate 				return (1);
8560Sstevel@tonic-gate 			}
8570Sstevel@tonic-gate 			if (c <= 0) {
8580Sstevel@tonic-gate 				returnValue = -1;
8590Sstevel@tonic-gate 				goto next;
8600Sstevel@tonic-gate 			}
8610Sstevel@tonic-gate 			if (termdata) {
8620Sstevel@tonic-gate 				Dump('<', ttyiring.supply, c);
8630Sstevel@tonic-gate 			}
8640Sstevel@tonic-gate 			ring_supplied(&ttyiring, c);
8650Sstevel@tonic-gate 		}
8660Sstevel@tonic-gate 		returnValue = 1;		/* did something useful */
8670Sstevel@tonic-gate 	}
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate next:
8700Sstevel@tonic-gate 	if (FD_ISSET(net, &obits)) {
8710Sstevel@tonic-gate 		FD_CLR(net, &obits);
8720Sstevel@tonic-gate 		returnValue |= netflush();
8730Sstevel@tonic-gate 	}
8740Sstevel@tonic-gate 	if (FD_ISSET(tout, &obits)) {
8750Sstevel@tonic-gate 		FD_CLR(tout, &obits);
8760Sstevel@tonic-gate 		i = ttyflush(SYNCHing|flushout);
8770Sstevel@tonic-gate 		if (i == -2) {
8780Sstevel@tonic-gate 			/* This will not return. */
8790Sstevel@tonic-gate 			fatal_tty_error("write");
8800Sstevel@tonic-gate 		}
8810Sstevel@tonic-gate 		returnValue |= (i > 0);
8820Sstevel@tonic-gate 	}
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 	return (returnValue);
8850Sstevel@tonic-gate }
886