Home | History | Annotate | Line # | Download | only in telnet
sys_bsd.c revision 1.9
      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[] = "@(#)sys_bsd.c	8.4 (Berkeley) 5/30/95"; */
     36 static char rcsid[] = "$NetBSD: sys_bsd.c,v 1.9 1996/02/24 07:32:01 jtk Exp $";
     37 #endif /* not lint */
     38 
     39 /*
     40  * The following routines try to encapsulate what is system dependent
     41  * (at least between 4.x and dos) which is used in telnet.c.
     42  */
     43 
     44 
     45 #include <fcntl.h>
     46 #include <sys/types.h>
     47 #include <sys/time.h>
     48 #include <sys/socket.h>
     49 #include <signal.h>
     50 #include <errno.h>
     51 #include <arpa/telnet.h>
     52 
     53 #include "ring.h"
     54 
     55 #include "fdset.h"
     56 
     57 #include "defines.h"
     58 #include "externs.h"
     59 #include "types.h"
     60 
     61 #if	defined(CRAY) || (defined(USE_TERMIO) && !defined(SYSV_TERMIO))
     62 #define	SIG_FUNC_RET	void
     63 #else
     64 #define	SIG_FUNC_RET	int
     65 #endif
     66 
     67 #ifdef	SIGINFO
     68 extern SIG_FUNC_RET ayt_status();
     69 #endif
     70 
     71 int
     72 	tout,			/* Output file descriptor */
     73 	tin,			/* Input file descriptor */
     74 	net;
     75 
     76 #ifndef	USE_TERMIO
     77 struct	tchars otc = { 0 }, ntc = { 0 };
     78 struct	ltchars oltc = { 0 }, nltc = { 0 };
     79 struct	sgttyb ottyb = { 0 }, nttyb = { 0 };
     80 int	olmode = 0;
     81 # define cfgetispeed(ptr)	(ptr)->sg_ispeed
     82 # define cfgetospeed(ptr)	(ptr)->sg_ospeed
     83 # define old_tc ottyb
     84 
     85 #else	/* USE_TERMIO */
     86 struct	termio old_tc = { 0 };
     87 extern struct termio new_tc;
     88 
     89 # ifndef	TCSANOW
     90 #  ifdef TCSETS
     91 #   define	TCSANOW		TCSETS
     92 #   define	TCSADRAIN	TCSETSW
     93 #   define	tcgetattr(f, t) ioctl(f, TCGETS, (char *)t)
     94 #  else
     95 #   ifdef TCSETA
     96 #    define	TCSANOW		TCSETA
     97 #    define	TCSADRAIN	TCSETAW
     98 #    define	tcgetattr(f, t) ioctl(f, TCGETA, (char *)t)
     99 #   else
    100 #    define	TCSANOW		TIOCSETA
    101 #    define	TCSADRAIN	TIOCSETAW
    102 #    define	tcgetattr(f, t) ioctl(f, TIOCGETA, (char *)t)
    103 #   endif
    104 #  endif
    105 #  define	tcsetattr(f, a, t) ioctl(f, a, (char *)t)
    106 #  define	cfgetospeed(ptr)	((ptr)->c_cflag&CBAUD)
    107 #  ifdef CIBAUD
    108 #   define	cfgetispeed(ptr)	(((ptr)->c_cflag&CIBAUD) >> IBSHIFT)
    109 #  else
    110 #   define	cfgetispeed(ptr)	cfgetospeed(ptr)
    111 #  endif
    112 # endif /* TCSANOW */
    113 # ifdef	sysV88
    114 # define TIOCFLUSH TC_PX_DRAIN
    115 # endif
    116 #endif	/* USE_TERMIO */
    117 
    118 static fd_set ibits, obits, xbits;
    119 
    120 
    121     void
    122 init_sys()
    123 {
    124     tout = fileno(stdout);
    125     tin = fileno(stdin);
    126     FD_ZERO(&ibits);
    127     FD_ZERO(&obits);
    128     FD_ZERO(&xbits);
    129 
    130     errno = 0;
    131 }
    132 
    133 
    134     int
    135 TerminalWrite(buf, n)
    136     char *buf;
    137     int  n;
    138 {
    139     return write(tout, buf, n);
    140 }
    141 
    142     int
    143 TerminalRead(buf, n)
    144     char *buf;
    145     int  n;
    146 {
    147     return read(tin, buf, n);
    148 }
    149 
    150 /*
    151  *
    152  */
    153 
    154     int
    155 TerminalAutoFlush()
    156 {
    157 #if	defined(LNOFLSH)
    158     int flush;
    159 
    160     ioctl(0, TIOCLGET, (char *)&flush);
    161     return !(flush&LNOFLSH);	/* if LNOFLSH, no autoflush */
    162 #else	/* LNOFLSH */
    163     return 1;
    164 #endif	/* LNOFLSH */
    165 }
    166 
    167 #ifdef	KLUDGELINEMODE
    168 extern int kludgelinemode;
    169 #endif
    170 /*
    171  * TerminalSpecialChars()
    172  *
    173  * Look at an input character to see if it is a special character
    174  * and decide what to do.
    175  *
    176  * Output:
    177  *
    178  *	0	Don't add this character.
    179  *	1	Do add this character
    180  */
    181 
    182 extern void xmitAO(), xmitEL(), xmitEC(), intp(), sendbrk();
    183 
    184     int
    185 TerminalSpecialChars(c)
    186     int	c;
    187 {
    188     if (c == termIntChar) {
    189 	intp();
    190 	return 0;
    191     } else if (c == termQuitChar) {
    192 #ifdef	KLUDGELINEMODE
    193 	if (kludgelinemode)
    194 	    sendbrk();
    195 	else
    196 #endif
    197 	    sendabort();
    198 	return 0;
    199     } else if (c == termEofChar) {
    200 	if (my_want_state_is_will(TELOPT_LINEMODE)) {
    201 	    sendeof();
    202 	    return 0;
    203 	}
    204 	return 1;
    205     } else if (c == termSuspChar) {
    206 	sendsusp();
    207 	return(0);
    208     } else if (c == termFlushChar) {
    209 	xmitAO();		/* Transmit Abort Output */
    210 	return 0;
    211     } else if (!MODE_LOCAL_CHARS(globalmode)) {
    212 	if (c == termKillChar) {
    213 	    xmitEL();
    214 	    return 0;
    215 	} else if (c == termEraseChar) {
    216 	    xmitEC();		/* Transmit Erase Character */
    217 	    return 0;
    218 	}
    219     }
    220     return 1;
    221 }
    222 
    223 
    224 /*
    225  * Flush output to the terminal
    226  */
    227 
    228     void
    229 TerminalFlushOutput()
    230 {
    231 #ifdef	TIOCFLUSH
    232     (void) ioctl(fileno(stdout), TIOCFLUSH, (char *) 0);
    233 #else
    234     (void) ioctl(fileno(stdout), TCFLSH, (char *) 0);
    235 #endif
    236 }
    237 
    238     void
    239 TerminalSaveState()
    240 {
    241 #ifndef	USE_TERMIO
    242     ioctl(0, TIOCGETP, (char *)&ottyb);
    243     ioctl(0, TIOCGETC, (char *)&otc);
    244     ioctl(0, TIOCGLTC, (char *)&oltc);
    245     ioctl(0, TIOCLGET, (char *)&olmode);
    246 
    247     ntc = otc;
    248     nltc = oltc;
    249     nttyb = ottyb;
    250 
    251 #else	/* USE_TERMIO */
    252     tcgetattr(0, &old_tc);
    253 
    254     new_tc = old_tc;
    255 
    256 #ifndef	VDISCARD
    257     termFlushChar = CONTROL('O');
    258 #endif
    259 #ifndef	VWERASE
    260     termWerasChar = CONTROL('W');
    261 #endif
    262 #ifndef	VREPRINT
    263     termRprntChar = CONTROL('R');
    264 #endif
    265 #ifndef	VLNEXT
    266     termLiteralNextChar = CONTROL('V');
    267 #endif
    268 #ifndef	VSTART
    269     termStartChar = CONTROL('Q');
    270 #endif
    271 #ifndef	VSTOP
    272     termStopChar = CONTROL('S');
    273 #endif
    274 #ifndef	VSTATUS
    275     termAytChar = CONTROL('T');
    276 #endif
    277 #endif	/* USE_TERMIO */
    278 }
    279 
    280     cc_t *
    281 tcval(func)
    282     register int func;
    283 {
    284     switch(func) {
    285     case SLC_IP:	return(&termIntChar);
    286     case SLC_ABORT:	return(&termQuitChar);
    287     case SLC_EOF:	return(&termEofChar);
    288     case SLC_EC:	return(&termEraseChar);
    289     case SLC_EL:	return(&termKillChar);
    290     case SLC_XON:	return(&termStartChar);
    291     case SLC_XOFF:	return(&termStopChar);
    292     case SLC_FORW1:	return(&termForw1Char);
    293 #ifdef	USE_TERMIO
    294     case SLC_FORW2:	return(&termForw2Char);
    295 # ifdef	VDISCARD
    296     case SLC_AO:	return(&termFlushChar);
    297 # endif
    298 # ifdef	VSUSP
    299     case SLC_SUSP:	return(&termSuspChar);
    300 # endif
    301 # ifdef	VWERASE
    302     case SLC_EW:	return(&termWerasChar);
    303 # endif
    304 # ifdef	VREPRINT
    305     case SLC_RP:	return(&termRprntChar);
    306 # endif
    307 # ifdef	VLNEXT
    308     case SLC_LNEXT:	return(&termLiteralNextChar);
    309 # endif
    310 # ifdef	VSTATUS
    311     case SLC_AYT:	return(&termAytChar);
    312 # endif
    313 #endif
    314 
    315     case SLC_SYNCH:
    316     case SLC_BRK:
    317     case SLC_EOR:
    318     default:
    319 	return((cc_t *)0);
    320     }
    321 }
    322 
    323     void
    324 TerminalDefaultChars()
    325 {
    326 #ifndef	USE_TERMIO
    327     ntc = otc;
    328     nltc = oltc;
    329     nttyb.sg_kill = ottyb.sg_kill;
    330     nttyb.sg_erase = ottyb.sg_erase;
    331 #else	/* USE_TERMIO */
    332     memmove(new_tc.c_cc, old_tc.c_cc, sizeof(old_tc.c_cc));
    333 # ifndef	VDISCARD
    334     termFlushChar = CONTROL('O');
    335 # endif
    336 # ifndef	VWERASE
    337     termWerasChar = CONTROL('W');
    338 # endif
    339 # ifndef	VREPRINT
    340     termRprntChar = CONTROL('R');
    341 # endif
    342 # ifndef	VLNEXT
    343     termLiteralNextChar = CONTROL('V');
    344 # endif
    345 # ifndef	VSTART
    346     termStartChar = CONTROL('Q');
    347 # endif
    348 # ifndef	VSTOP
    349     termStopChar = CONTROL('S');
    350 # endif
    351 # ifndef	VSTATUS
    352     termAytChar = CONTROL('T');
    353 # endif
    354 #endif	/* USE_TERMIO */
    355 }
    356 
    357 #ifdef notdef
    358 void
    359 TerminalRestoreState()
    360 {
    361 }
    362 #endif
    363 
    364 /*
    365  * TerminalNewMode - set up terminal to a specific mode.
    366  *	MODE_ECHO: do local terminal echo
    367  *	MODE_FLOW: do local flow control
    368  *	MODE_TRAPSIG: do local mapping to TELNET IAC sequences
    369  *	MODE_EDIT: do local line editing
    370  *
    371  *	Command mode:
    372  *		MODE_ECHO|MODE_EDIT|MODE_FLOW|MODE_TRAPSIG
    373  *		local echo
    374  *		local editing
    375  *		local xon/xoff
    376  *		local signal mapping
    377  *
    378  *	Linemode:
    379  *		local/no editing
    380  *	Both Linemode and Single Character mode:
    381  *		local/remote echo
    382  *		local/no xon/xoff
    383  *		local/no signal mapping
    384  */
    385 
    386 
    387     void
    388 TerminalNewMode(f)
    389     register int f;
    390 {
    391     static int prevmode = 0;
    392 #ifndef	USE_TERMIO
    393     struct tchars tc;
    394     struct ltchars ltc;
    395     struct sgttyb sb;
    396     int lmode;
    397 #else	/* USE_TERMIO */
    398     struct termio tmp_tc;
    399 #endif	/* USE_TERMIO */
    400     int onoff;
    401     int old;
    402     cc_t esc;
    403 
    404     globalmode = f&~MODE_FORCE;
    405     if (prevmode == f)
    406 	return;
    407 
    408     /*
    409      * Write any outstanding data before switching modes
    410      * ttyflush() returns 0 only when there is no more data
    411      * left to write out, it returns -1 if it couldn't do
    412      * anything at all, otherwise it returns 1 + the number
    413      * of characters left to write.
    414 #ifndef	USE_TERMIO
    415      * We would really like ask the kernel to wait for the output
    416      * to drain, like we can do with the TCSADRAIN, but we don't have
    417      * that option.  The only ioctl that waits for the output to
    418      * drain, TIOCSETP, also flushes the input queue, which is NOT
    419      * what we want (TIOCSETP is like TCSADFLUSH).
    420 #endif
    421      */
    422     old = ttyflush(SYNCHing|flushout);
    423     if (old < 0 || old > 1) {
    424 #ifdef	USE_TERMIO
    425 	tcgetattr(tin, &tmp_tc);
    426 #endif	/* USE_TERMIO */
    427 	do {
    428 	    /*
    429 	     * Wait for data to drain, then flush again.
    430 	     */
    431 #ifdef	USE_TERMIO
    432 	    tcsetattr(tin, TCSADRAIN, &tmp_tc);
    433 #endif	/* USE_TERMIO */
    434 	    old = ttyflush(SYNCHing|flushout);
    435 	} while (old < 0 || old > 1);
    436     }
    437 
    438     old = prevmode;
    439     prevmode = f&~MODE_FORCE;
    440 #ifndef	USE_TERMIO
    441     sb = nttyb;
    442     tc = ntc;
    443     ltc = nltc;
    444     lmode = olmode;
    445 #else
    446     tmp_tc = new_tc;
    447 #endif
    448 
    449     if (f&MODE_ECHO) {
    450 #ifndef	USE_TERMIO
    451 	sb.sg_flags |= ECHO;
    452 #else
    453 	tmp_tc.c_lflag |= ECHO;
    454 	tmp_tc.c_oflag |= ONLCR;
    455 	if (crlf)
    456 		tmp_tc.c_iflag |= ICRNL;
    457 #endif
    458     } else {
    459 #ifndef	USE_TERMIO
    460 	sb.sg_flags &= ~ECHO;
    461 #else
    462 	tmp_tc.c_lflag &= ~ECHO;
    463 	tmp_tc.c_oflag &= ~ONLCR;
    464 # ifdef notdef
    465 	if (crlf)
    466 		tmp_tc.c_iflag &= ~ICRNL;
    467 # endif
    468 #endif
    469     }
    470 
    471     if ((f&MODE_FLOW) == 0) {
    472 #ifndef	USE_TERMIO
    473 	tc.t_startc = _POSIX_VDISABLE;
    474 	tc.t_stopc = _POSIX_VDISABLE;
    475 #else
    476 	tmp_tc.c_iflag &= ~(IXOFF|IXON);	/* Leave the IXANY bit alone */
    477     } else {
    478 	if (restartany < 0) {
    479 		tmp_tc.c_iflag |= IXOFF|IXON;	/* Leave the IXANY bit alone */
    480 	} else if (restartany > 0) {
    481 		tmp_tc.c_iflag |= IXOFF|IXON|IXANY;
    482 	} else {
    483 		tmp_tc.c_iflag |= IXOFF|IXON;
    484 		tmp_tc.c_iflag &= ~IXANY;
    485 	}
    486 #endif
    487     }
    488 
    489     if ((f&MODE_TRAPSIG) == 0) {
    490 #ifndef	USE_TERMIO
    491 	tc.t_intrc = _POSIX_VDISABLE;
    492 	tc.t_quitc = _POSIX_VDISABLE;
    493 	tc.t_eofc = _POSIX_VDISABLE;
    494 	ltc.t_suspc = _POSIX_VDISABLE;
    495 	ltc.t_dsuspc = _POSIX_VDISABLE;
    496 #else
    497 	tmp_tc.c_lflag &= ~ISIG;
    498 #endif
    499 	localchars = 0;
    500     } else {
    501 #ifdef	USE_TERMIO
    502 	tmp_tc.c_lflag |= ISIG;
    503 #endif
    504 	localchars = 1;
    505     }
    506 
    507     if (f&MODE_EDIT) {
    508 #ifndef	USE_TERMIO
    509 	sb.sg_flags &= ~CBREAK;
    510 	sb.sg_flags |= CRMOD;
    511 #else
    512 	tmp_tc.c_lflag |= ICANON;
    513 #endif
    514     } else {
    515 #ifndef	USE_TERMIO
    516 	sb.sg_flags |= CBREAK;
    517 	if (f&MODE_ECHO)
    518 	    sb.sg_flags |= CRMOD;
    519 	else
    520 	    sb.sg_flags &= ~CRMOD;
    521 #else
    522 	tmp_tc.c_lflag &= ~ICANON;
    523 	tmp_tc.c_iflag &= ~ICRNL;
    524 	tmp_tc.c_cc[VMIN] = 1;
    525 	tmp_tc.c_cc[VTIME] = 0;
    526 #endif
    527     }
    528 
    529     if ((f&(MODE_EDIT|MODE_TRAPSIG)) == 0) {
    530 #ifndef	USE_TERMIO
    531 	ltc.t_lnextc = _POSIX_VDISABLE;
    532 #else
    533 	tmp_tc.c_lflag &= ~IEXTEN;
    534 #endif
    535     }
    536 
    537     if (f&MODE_SOFT_TAB) {
    538 #ifndef USE_TERMIO
    539 	sb.sg_flags |= XTABS;
    540 #else
    541 # ifdef	OXTABS
    542 	tmp_tc.c_oflag |= OXTABS;
    543 # endif
    544 # ifdef	TABDLY
    545 	tmp_tc.c_oflag &= ~TABDLY;
    546 	tmp_tc.c_oflag |= TAB3;
    547 # endif
    548 #endif
    549     } else {
    550 #ifndef USE_TERMIO
    551 	sb.sg_flags &= ~XTABS;
    552 #else
    553 # ifdef	OXTABS
    554 	tmp_tc.c_oflag &= ~OXTABS;
    555 # endif
    556 # ifdef	TABDLY
    557 	tmp_tc.c_oflag &= ~TABDLY;
    558 # endif
    559 #endif
    560     }
    561 
    562     if (f&MODE_LIT_ECHO) {
    563 #ifndef USE_TERMIO
    564 	lmode &= ~LCTLECH;
    565 #else
    566 # ifdef	ECHOCTL
    567 	tmp_tc.c_lflag &= ~ECHOCTL;
    568 # endif
    569 #endif
    570     } else {
    571 #ifndef USE_TERMIO
    572 	lmode |= LCTLECH;
    573 #else
    574 # ifdef	ECHOCTL
    575 	tmp_tc.c_lflag |= ECHOCTL;
    576 # endif
    577 #endif
    578     }
    579 
    580     if (f == -1) {
    581 	onoff = 0;
    582     } else {
    583 #ifndef	USE_TERMIO
    584 	if (f & MODE_OUTBIN)
    585 		lmode |= LLITOUT;
    586 	else
    587 		lmode &= ~LLITOUT;
    588 
    589 	if (f & MODE_INBIN)
    590 		lmode |= LPASS8;
    591 	else
    592 		lmode &= ~LPASS8;
    593 #else
    594 	if (f & MODE_INBIN)
    595 		tmp_tc.c_iflag &= ~ISTRIP;
    596 	else
    597 		tmp_tc.c_iflag |= ISTRIP;
    598 	if (f & MODE_OUTBIN) {
    599 		tmp_tc.c_cflag &= ~(CSIZE|PARENB);
    600 		tmp_tc.c_cflag |= CS8;
    601 		tmp_tc.c_oflag &= ~OPOST;
    602 	} else {
    603 		tmp_tc.c_cflag &= ~(CSIZE|PARENB);
    604 		tmp_tc.c_cflag |= old_tc.c_cflag & (CSIZE|PARENB);
    605 		tmp_tc.c_oflag |= OPOST;
    606 	}
    607 #endif
    608 	onoff = 1;
    609     }
    610 
    611     if (f != -1) {
    612 #ifdef	SIGTSTP
    613 	SIG_FUNC_RET susp();
    614 #endif	/* SIGTSTP */
    615 #ifdef	SIGINFO
    616 	SIG_FUNC_RET ayt();
    617 #endif
    618 
    619 #ifdef	SIGTSTP
    620 	(void) signal(SIGTSTP, susp);
    621 #endif	/* SIGTSTP */
    622 #ifdef	SIGINFO
    623 	(void) signal(SIGINFO, ayt);
    624 #endif
    625 #if	defined(USE_TERMIO) && defined(NOKERNINFO)
    626 	tmp_tc.c_lflag |= NOKERNINFO;
    627 #endif
    628 	/*
    629 	 * We don't want to process ^Y here.  It's just another
    630 	 * character that we'll pass on to the back end.  It has
    631 	 * to process it because it will be processed when the
    632 	 * user attempts to read it, not when we send it.
    633 	 */
    634 #ifndef	USE_TERMIO
    635 	ltc.t_dsuspc = _POSIX_VDISABLE;
    636 #else
    637 # ifdef	VDSUSP
    638 	tmp_tc.c_cc[VDSUSP] = (cc_t)(_POSIX_VDISABLE);
    639 # endif
    640 #endif
    641 #ifdef	USE_TERMIO
    642 	/*
    643 	 * If the VEOL character is already set, then use VEOL2,
    644 	 * otherwise use VEOL.
    645 	 */
    646 	esc = (rlogin != _POSIX_VDISABLE) ? rlogin : escape;
    647 	if ((tmp_tc.c_cc[VEOL] != esc)
    648 # ifdef	VEOL2
    649 	    && (tmp_tc.c_cc[VEOL2] != esc)
    650 # endif
    651 	    ) {
    652 		if (tmp_tc.c_cc[VEOL] == (cc_t)(_POSIX_VDISABLE))
    653 		    tmp_tc.c_cc[VEOL] = esc;
    654 # ifdef	VEOL2
    655 		else if (tmp_tc.c_cc[VEOL2] == (cc_t)(_POSIX_VDISABLE))
    656 		    tmp_tc.c_cc[VEOL2] = esc;
    657 # endif
    658 	}
    659 #else
    660 	if (tc.t_brkc == (cc_t)(_POSIX_VDISABLE))
    661 		tc.t_brkc = esc;
    662 #endif
    663     } else {
    664 #ifdef	SIGINFO
    665 	SIG_FUNC_RET ayt_status();
    666 
    667 	(void) signal(SIGINFO, ayt_status);
    668 #endif
    669 #ifdef	SIGTSTP
    670 	(void) signal(SIGTSTP, SIG_DFL);
    671 # ifndef SOLARIS
    672 	(void) sigsetmask(sigblock(0) & ~(1<<(SIGTSTP-1)));
    673 # else	SOLARIS
    674 	(void) sigrelse(SIGTSTP);
    675 # endif	SOLARIS
    676 #endif	/* SIGTSTP */
    677 #ifndef USE_TERMIO
    678 	ltc = oltc;
    679 	tc = otc;
    680 	sb = ottyb;
    681 	lmode = olmode;
    682 #else
    683 	tmp_tc = old_tc;
    684 #endif
    685     }
    686 #ifndef USE_TERMIO
    687     ioctl(tin, TIOCLSET, (char *)&lmode);
    688     ioctl(tin, TIOCSLTC, (char *)&ltc);
    689     ioctl(tin, TIOCSETC, (char *)&tc);
    690     ioctl(tin, TIOCSETN, (char *)&sb);
    691 #else
    692     if (tcsetattr(tin, TCSADRAIN, &tmp_tc) < 0)
    693 	tcsetattr(tin, TCSANOW, &tmp_tc);
    694 #endif
    695 
    696 #if	(!defined(TN3270)) || ((!defined(NOT43)) || defined(PUTCHAR))
    697 # if	!defined(sysV88)
    698     ioctl(tin, FIONBIO, (char *)&onoff);
    699     ioctl(tout, FIONBIO, (char *)&onoff);
    700 # endif
    701 #endif	/* (!defined(TN3270)) || ((!defined(NOT43)) || defined(PUTCHAR)) */
    702 #if	defined(TN3270)
    703     if (noasynchtty == 0) {
    704 	ioctl(tin, FIOASYNC, (char *)&onoff);
    705     }
    706 #endif	/* defined(TN3270) */
    707 
    708 }
    709 
    710 /*
    711  * Try to guess whether speeds are "encoded" (4.2BSD) or just numeric (4.4BSD).
    712  */
    713 #if B4800 != 4800
    714 #define	DECODE_BAUD
    715 #endif
    716 
    717 #ifdef	DECODE_BAUD
    718 #ifndef	B7200
    719 #define B7200   B4800
    720 #endif
    721 
    722 #ifndef	B14400
    723 #define B14400  B9600
    724 #endif
    725 
    726 #ifndef	B19200
    727 # define B19200 B14400
    728 #endif
    729 
    730 #ifndef	B28800
    731 #define B28800  B19200
    732 #endif
    733 
    734 #ifndef	B38400
    735 # define B38400 B28800
    736 #endif
    737 
    738 #ifndef B57600
    739 #define B57600  B38400
    740 #endif
    741 
    742 #ifndef B76800
    743 #define B76800  B57600
    744 #endif
    745 
    746 #ifndef B115200
    747 #define B115200 B76800
    748 #endif
    749 
    750 #ifndef B230400
    751 #define B230400 B115200
    752 #endif
    753 
    754 
    755 /*
    756  * This code assumes that the values B0, B50, B75...
    757  * are in ascending order.  They do not have to be
    758  * contiguous.
    759  */
    760 struct termspeeds {
    761 	long speed;
    762 	long value;
    763 } termspeeds[] = {
    764 	{ 0,      B0 },      { 50,    B50 },    { 75,     B75 },
    765 	{ 110,    B110 },    { 134,   B134 },   { 150,    B150 },
    766 	{ 200,    B200 },    { 300,   B300 },   { 600,    B600 },
    767 	{ 1200,   B1200 },   { 1800,  B1800 },  { 2400,   B2400 },
    768 	{ 4800,   B4800 },   { 7200,  B7200 },  { 9600,   B9600 },
    769 	{ 14400,  B14400 },  { 19200, B19200 }, { 28800,  B28800 },
    770 	{ 38400,  B38400 },  { 57600, B57600 }, { 115200, B115200 },
    771 	{ 230400, B230400 }, { -1,    B230400 }
    772 };
    773 #endif	/* DECODE_BAUD */
    774 
    775     void
    776 TerminalSpeeds(ispeed, ospeed)
    777     long *ispeed;
    778     long *ospeed;
    779 {
    780 #ifdef	DECODE_BAUD
    781     register struct termspeeds *tp;
    782 #endif	/* DECODE_BAUD */
    783     register long in, out;
    784 
    785     out = cfgetospeed(&old_tc);
    786     in = cfgetispeed(&old_tc);
    787     if (in == 0)
    788 	in = out;
    789 
    790 #ifdef	DECODE_BAUD
    791     tp = termspeeds;
    792     while ((tp->speed != -1) && (tp->value < in))
    793 	tp++;
    794     *ispeed = tp->speed;
    795 
    796     tp = termspeeds;
    797     while ((tp->speed != -1) && (tp->value < out))
    798 	tp++;
    799     *ospeed = tp->speed;
    800 #else	/* DECODE_BAUD */
    801 	*ispeed = in;
    802 	*ospeed = out;
    803 #endif	/* DECODE_BAUD */
    804 }
    805 
    806     int
    807 TerminalWindowSize(rows, cols)
    808     long *rows, *cols;
    809 {
    810 #ifdef	TIOCGWINSZ
    811     struct winsize ws;
    812 
    813     if (ioctl(fileno(stdin), TIOCGWINSZ, (char *)&ws) >= 0) {
    814 	*rows = ws.ws_row;
    815 	*cols = ws.ws_col;
    816 	return 1;
    817     }
    818 #endif	/* TIOCGWINSZ */
    819     return 0;
    820 }
    821 
    822     int
    823 NetClose(fd)
    824     int	fd;
    825 {
    826     return close(fd);
    827 }
    828 
    829 
    830     void
    831 NetNonblockingIO(fd, onoff)
    832     int fd;
    833     int onoff;
    834 {
    835     ioctl(fd, FIONBIO, (char *)&onoff);
    836 }
    837 
    838 #if	defined(TN3270)
    839     void
    840 NetSigIO(fd, onoff)
    841     int fd;
    842     int onoff;
    843 {
    844     ioctl(fd, FIOASYNC, (char *)&onoff);	/* hear about input */
    845 }
    846 
    847     void
    848 NetSetPgrp(fd)
    849     int fd;
    850 {
    851     int myPid;
    852 
    853     myPid = getpid();
    854     fcntl(fd, F_SETOWN, myPid);
    855 }
    856 #endif	/*defined(TN3270)*/
    857 
    858 /*
    860  * Various signal handling routines.
    861  */
    862 
    863     /* ARGSUSED */
    864     SIG_FUNC_RET
    865 deadpeer(sig)
    866     int sig;
    867 {
    868 	setcommandmode();
    869 	longjmp(peerdied, -1);
    870 }
    871 
    872     /* ARGSUSED */
    873     SIG_FUNC_RET
    874 intr(sig)
    875     int sig;
    876 {
    877     if (localchars) {
    878 	intp();
    879 	return;
    880     }
    881     setcommandmode();
    882     longjmp(toplevel, -1);
    883 }
    884 
    885     /* ARGSUSED */
    886     SIG_FUNC_RET
    887 intr2(sig)
    888     int sig;
    889 {
    890     if (localchars) {
    891 #ifdef	KLUDGELINEMODE
    892 	if (kludgelinemode)
    893 	    sendbrk();
    894 	else
    895 #endif
    896 	    sendabort();
    897 	return;
    898     }
    899 }
    900 
    901 #ifdef	SIGTSTP
    902     /* ARGSUSED */
    903     SIG_FUNC_RET
    904 susp(sig)
    905     int sig;
    906 {
    907     if ((rlogin != _POSIX_VDISABLE) && rlogin_susp())
    908 	return;
    909     if (localchars)
    910 	sendsusp();
    911 }
    912 #endif
    913 
    914 #ifdef	SIGWINCH
    915     /* ARGSUSED */
    916     SIG_FUNC_RET
    917 sendwin(sig)
    918     int sig;
    919 {
    920     if (connected) {
    921 	sendnaws();
    922     }
    923 }
    924 #endif
    925 
    926 #ifdef	SIGINFO
    927     /* ARGSUSED */
    928     SIG_FUNC_RET
    929 ayt(sig)
    930     int sig;
    931 {
    932     if (connected)
    933 	sendayt();
    934     else
    935 	ayt_status();
    936 }
    937 #endif
    938 
    939 
    940     void
    942 sys_telnet_init()
    943 {
    944     (void) signal(SIGINT, intr);
    945     (void) signal(SIGQUIT, intr2);
    946     (void) signal(SIGPIPE, deadpeer);
    947 #ifdef	SIGWINCH
    948     (void) signal(SIGWINCH, sendwin);
    949 #endif
    950 #ifdef	SIGTSTP
    951     (void) signal(SIGTSTP, susp);
    952 #endif
    953 #ifdef	SIGINFO
    954     (void) signal(SIGINFO, ayt);
    955 #endif
    956 
    957     setconnmode(0);
    958 
    959     NetNonblockingIO(net, 1);
    960 
    961 #if	defined(TN3270)
    962     if (noasynchnet == 0) {			/* DBX can't handle! */
    963 	NetSigIO(net, 1);
    964 	NetSetPgrp(net);
    965     }
    966 #endif	/* defined(TN3270) */
    967 
    968 #if	defined(SO_OOBINLINE)
    969     if (SetSockOpt(net, SOL_SOCKET, SO_OOBINLINE, 1) == -1) {
    970 	perror("SetSockOpt");
    971     }
    972 #endif	/* defined(SO_OOBINLINE) */
    973 }
    974 
    975 /*
    976  * Process rings -
    977  *
    978  *	This routine tries to fill up/empty our various rings.
    979  *
    980  *	The parameter specifies whether this is a poll operation,
    981  *	or a block-until-something-happens operation.
    982  *
    983  *	The return value is 1 if something happened, 0 if not.
    984  */
    985 
    986     int
    987 process_rings(netin, netout, netex, ttyin, ttyout, poll)
    988     int poll;		/* If 0, then block until something to do */
    989 {
    990     register int c;
    991 		/* One wants to be a bit careful about setting returnValue
    992 		 * to one, since a one implies we did some useful work,
    993 		 * and therefore probably won't be called to block next
    994 		 * time (TN3270 mode only).
    995 		 */
    996     int returnValue = 0;
    997     static struct timeval TimeValue = { 0 };
    998 
    999     if (netout) {
   1000 	FD_SET(net, &obits);
   1001     }
   1002     if (ttyout) {
   1003 	FD_SET(tout, &obits);
   1004     }
   1005 #if	defined(TN3270)
   1006     if (ttyin) {
   1007 	FD_SET(tin, &ibits);
   1008     }
   1009 #else	/* defined(TN3270) */
   1010     if (ttyin) {
   1011 	FD_SET(tin, &ibits);
   1012     }
   1013 #endif	/* defined(TN3270) */
   1014 #if	defined(TN3270)
   1015     if (netin) {
   1016 	FD_SET(net, &ibits);
   1017     }
   1018 #   else /* !defined(TN3270) */
   1019     if (netin) {
   1020 	FD_SET(net, &ibits);
   1021     }
   1022 #   endif /* !defined(TN3270) */
   1023     if (netex) {
   1024 	FD_SET(net, &xbits);
   1025     }
   1026     if ((c = select(16, &ibits, &obits, &xbits,
   1027 			(poll == 0)? (struct timeval *)0 : &TimeValue)) < 0) {
   1028 	if (c == -1) {
   1029 		    /*
   1030 		     * we can get EINTR if we are in line mode,
   1031 		     * and the user does an escape (TSTP), or
   1032 		     * some other signal generator.
   1033 		     */
   1034 	    if (errno == EINTR) {
   1035 		return 0;
   1036 	    }
   1037 #	    if defined(TN3270)
   1038 		    /*
   1039 		     * we can get EBADF if we were in transparent
   1040 		     * mode, and the transcom process died.
   1041 		    */
   1042 	    if (errno == EBADF) {
   1043 			/*
   1044 			 * zero the bits (even though kernel does it)
   1045 			 * to make sure we are selecting on the right
   1046 			 * ones.
   1047 			*/
   1048 		FD_ZERO(&ibits);
   1049 		FD_ZERO(&obits);
   1050 		FD_ZERO(&xbits);
   1051 		return 0;
   1052 	    }
   1053 #	    endif /* defined(TN3270) */
   1054 		    /* I don't like this, does it ever happen? */
   1055 	    printf("sleep(5) from telnet, after select\r\n");
   1056 	    sleep(5);
   1057 	}
   1058 	return 0;
   1059     }
   1060 
   1061     /*
   1062      * Any urgent data?
   1063      */
   1064     if (FD_ISSET(net, &xbits)) {
   1065 	FD_CLR(net, &xbits);
   1066 	SYNCHing = 1;
   1067 	(void) ttyflush(1);	/* flush already enqueued data */
   1068     }
   1069 
   1070     /*
   1071      * Something to read from the network...
   1072      */
   1073     if (FD_ISSET(net, &ibits)) {
   1074 	int canread;
   1075 
   1076 	FD_CLR(net, &ibits);
   1077 	canread = ring_empty_consecutive(&netiring);
   1078 #if	!defined(SO_OOBINLINE)
   1079 	    /*
   1080 	     * In 4.2 (and some early 4.3) systems, the
   1081 	     * OOB indication and data handling in the kernel
   1082 	     * is such that if two separate TCP Urgent requests
   1083 	     * come in, one byte of TCP data will be overlaid.
   1084 	     * This is fatal for Telnet, but we try to live
   1085 	     * with it.
   1086 	     *
   1087 	     * In addition, in 4.2 (and...), a special protocol
   1088 	     * is needed to pick up the TCP Urgent data in
   1089 	     * the correct sequence.
   1090 	     *
   1091 	     * What we do is:  if we think we are in urgent
   1092 	     * mode, we look to see if we are "at the mark".
   1093 	     * If we are, we do an OOB receive.  If we run
   1094 	     * this twice, we will do the OOB receive twice,
   1095 	     * but the second will fail, since the second
   1096 	     * time we were "at the mark", but there wasn't
   1097 	     * any data there (the kernel doesn't reset
   1098 	     * "at the mark" until we do a normal read).
   1099 	     * Once we've read the OOB data, we go ahead
   1100 	     * and do normal reads.
   1101 	     *
   1102 	     * There is also another problem, which is that
   1103 	     * since the OOB byte we read doesn't put us
   1104 	     * out of OOB state, and since that byte is most
   1105 	     * likely the TELNET DM (data mark), we would
   1106 	     * stay in the TELNET SYNCH (SYNCHing) state.
   1107 	     * So, clocks to the rescue.  If we've "just"
   1108 	     * received a DM, then we test for the
   1109 	     * presence of OOB data when the receive OOB
   1110 	     * fails (and AFTER we did the normal mode read
   1111 	     * to clear "at the mark").
   1112 	     */
   1113 	if (SYNCHing) {
   1114 	    int atmark;
   1115 	    static int bogus_oob = 0, first = 1;
   1116 
   1117 	    ioctl(net, SIOCATMARK, (char *)&atmark);
   1118 	    if (atmark) {
   1119 		c = recv(net, netiring.supply, canread, MSG_OOB);
   1120 		if ((c == -1) && (errno == EINVAL)) {
   1121 		    c = recv(net, netiring.supply, canread, 0);
   1122 		    if (clocks.didnetreceive < clocks.gotDM) {
   1123 			SYNCHing = stilloob(net);
   1124 		    }
   1125 		} else if (first && c > 0) {
   1126 		    /*
   1127 		     * Bogosity check.  Systems based on 4.2BSD
   1128 		     * do not return an error if you do a second
   1129 		     * recv(MSG_OOB).  So, we do one.  If it
   1130 		     * succeeds and returns exactly the same
   1131 		     * data, then assume that we are running
   1132 		     * on a broken system and set the bogus_oob
   1133 		     * flag.  (If the data was different, then
   1134 		     * we probably got some valid new data, so
   1135 		     * increment the count...)
   1136 		     */
   1137 		    int i;
   1138 		    i = recv(net, netiring.supply + c, canread - c, MSG_OOB);
   1139 		    if (i == c &&
   1140 			 memcmp(netiring.supply, netiring.supply + c, i) == 0) {
   1141 			bogus_oob = 1;
   1142 			first = 0;
   1143 		    } else if (i < 0) {
   1144 			bogus_oob = 0;
   1145 			first = 0;
   1146 		    } else
   1147 			c += i;
   1148 		}
   1149 		if (bogus_oob && c > 0) {
   1150 		    int i;
   1151 		    /*
   1152 		     * Bogosity.  We have to do the read
   1153 		     * to clear the atmark to get out of
   1154 		     * an infinate loop.
   1155 		     */
   1156 		    i = read(net, netiring.supply + c, canread - c);
   1157 		    if (i > 0)
   1158 			c += i;
   1159 		}
   1160 	    } else {
   1161 		c = recv(net, netiring.supply, canread, 0);
   1162 	    }
   1163 	} else {
   1164 	    c = recv(net, netiring.supply, canread, 0);
   1165 	}
   1166 	settimer(didnetreceive);
   1167 #else	/* !defined(SO_OOBINLINE) */
   1168 	c = recv(net, (char *)netiring.supply, canread, 0);
   1169 #endif	/* !defined(SO_OOBINLINE) */
   1170 	if (c < 0 && errno == EWOULDBLOCK) {
   1171 	    c = 0;
   1172 	} else if (c <= 0) {
   1173 	    return -1;
   1174 	}
   1175 	if (netdata) {
   1176 	    Dump('<', netiring.supply, c);
   1177 	}
   1178 	if (c)
   1179 	    ring_supplied(&netiring, c);
   1180 	returnValue = 1;
   1181     }
   1182 
   1183     /*
   1184      * Something to read from the tty...
   1185      */
   1186     if (FD_ISSET(tin, &ibits)) {
   1187 	FD_CLR(tin, &ibits);
   1188 	c = TerminalRead(ttyiring.supply, ring_empty_consecutive(&ttyiring));
   1189 	if (c < 0 && errno == EIO)
   1190 	    c = 0;
   1191 	if (c < 0 && errno == EWOULDBLOCK) {
   1192 	    c = 0;
   1193 	} else {
   1194 	    if (c < 0) {
   1195 		return -1;
   1196 	    }
   1197 	    if (c == 0) {
   1198 		/* must be an EOF... */
   1199 		if (MODE_LOCAL_CHARS(globalmode) && isatty(tin)) {
   1200 		    *ttyiring.supply = termEofChar;
   1201 		    c = 1;
   1202 		} else {
   1203 		    clienteof = 1;
   1204 		    shutdown(net, 1);
   1205 		    return 0;
   1206 		}
   1207 	    }
   1208 	    if (termdata) {
   1209 		Dump('<', ttyiring.supply, c);
   1210 	    }
   1211 	    ring_supplied(&ttyiring, c);
   1212 	}
   1213 	returnValue = 1;		/* did something useful */
   1214     }
   1215 
   1216     if (FD_ISSET(net, &obits)) {
   1217 	FD_CLR(net, &obits);
   1218 	returnValue |= netflush();
   1219     }
   1220     if (FD_ISSET(tout, &obits)) {
   1221 	FD_CLR(tout, &obits);
   1222 	returnValue |= (ttyflush(SYNCHing|flushout) > 0);
   1223     }
   1224 
   1225     return returnValue;
   1226 }
   1227