1 /* $NetBSD: tty.c,v 1.4 2003/06/23 11:39:06 agc Exp $ */
6 __RCSID("$NetBSD: tty.c,v 1.4 2003/06/23 11:39:06 agc Exp $");
23 # ifdef HAVE_TERMIOS_H
24 ret
= tcgetattr(fd
, ts
);
25 # else /* HAVE_TERIOS_H */
27 ret
= ioctl(fd
, TCGETA
, ts
);
28 # else /* HAVE_TERMIO_H */
29 ret
= ioctl(fd
, TIOCGETP
, &ts
->sgttyb
);
31 if (ioctl(fd
, TIOCGATC
, &ts
->lchars
) < 0)
34 if (ioctl(fd
, TIOCGETC
, &ts
->tchars
) < 0)
37 if (ioctl(fd
, TIOCGLTC
, &ts
->ltchars
) < 0)
39 # endif /* TIOCGLTC */
40 # endif /* TIOCGATC */
41 # endif /* HAVE_TERMIO_H */
42 # endif /* HAVE_TERIOS_H */
47 set_tty(fd
, ts
, flags
)
54 # ifdef HAVE_TERMIOS_H
55 ret
= tcsetattr(fd
, TCSADRAIN
, ts
);
56 # else /* HAVE_TERIOS_H */
58 # ifndef TCSETAW /* e.g. Cray-2 */
59 /* first wait for output to drain */
61 if (ioctl(tty_fd
, TCSBRK
, 1) < 0)
63 # else /* the following kludge is minimally intrusive, but sometimes fails */
65 sleep((unsigned)1); /* fake it */
67 # endif /* !TCSETAW */
68 # if defined(_BSD_SYSV) || !defined(TCSETAW)
69 /* _BSD_SYSV must force TIOCSETN instead of TIOCSETP (preserve type-ahead) */
70 if (ioctl(tty_fd
, TCSETA
, ts
) < 0)
73 if (ioctl(tty_fd
, TCSETAW
, ts
) < 0)
76 # else /* HAVE_TERMIO_H */
77 # if defined(__mips) && (defined(_SYSTYPE_BSD43) || defined(__SYSTYPE_BSD43))
78 /* Under RISC/os 5.00, bsd43 environment, after a tty driver
79 * generated interrupt (eg, INTR, TSTP), all output to tty is
80 * lost until a SETP is done (there must be a better way of
83 if (flags
& TF_MIPSKLUDGE
)
84 ret
= ioctl(fd
, TIOCSETP
, &ts
->sgttyb
);
86 # endif /* _SYSTYPE_BSD43 */
87 ret
= ioctl(fd
, TIOCSETN
, &ts
->sgttyb
);
89 if (ioctl(fd
, TIOCSATC
, &ts
->lchars
) < 0)
92 if (ioctl(fd
, TIOCSETC
, &ts
->tchars
) < 0)
95 if (ioctl(fd
, TIOCSLTC
, &ts
->ltchars
) < 0)
97 # endif /* TIOCGLTC */
98 # endif /* TIOCGATC */
99 # endif /* HAVE_TERMIO_H */
100 # endif /* HAVE_TERIOS_H */
105 /* Initialize tty_fd. Used for saving/reseting tty modes upon
106 * foreground job completion and for setting up tty process group.
109 tty_init(init_ttystate
)
114 const char *devtty
= _PATH_TTY
;
122 /* SCO can't job control on /dev/tty, so don't try... */
123 #if !defined(__SCO__)
124 if ((tfd
= open(devtty
, O_RDWR
, 0)) < 0) {
126 /* rlogin on NeXT boxes does not set up the controlling tty,
127 * so force it to be done here...
130 extern char *ttyname
ARGS((int));
131 char *s
= ttyname(isatty(2) ? 2 : 0);
134 if (s
&& (fd
= open(s
, O_RDWR
, 0)) >= 0) {
136 tfd
= open(devtty
, O_RDWR
, 0);
141 /* X11R5 xterm on mips doesn't set controlling tty properly - temporary hack */
142 # if !defined(__mips) || !(defined(_SYSTYPE_BSD43) || defined(__SYSTYPE_BSD43))
146 "No controlling tty (open %s: %s)",
147 devtty
, strerror(errno
));
162 warningf(FALSE
, "Can't find tty file descriptor");
166 if ((tty_fd
= ksh_dupbase(tfd
, FDBASE
)) < 0) {
167 warningf(FALSE
, "j_ttyinit: dup of tty fd failed: %s",
169 } else if (fd_clexec(tty_fd
) < 0) {
170 warningf(FALSE
, "j_ttyinit: can't set close-on-exec flag: %s",
174 } else if (init_ttystate
)
175 get_tty(tty_fd
, &tty_state
);