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