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