tty.c revision 1.3 1 1.3 fair /* $NetBSD: tty.c,v 1.3 1998/04/07 10:29:50 fair Exp $ */
2 1.2 tls
3 1.1 jtc #include "sh.h"
4 1.1 jtc #include "ksh_stat.h"
5 1.1 jtc #define EXTERN
6 1.1 jtc #include "tty.h"
7 1.1 jtc #undef EXTERN
8 1.1 jtc
9 1.1 jtc int
10 1.1 jtc get_tty(fd, ts)
11 1.1 jtc int fd;
12 1.1 jtc TTY_state *ts;
13 1.1 jtc {
14 1.1 jtc int ret;
15 1.1 jtc
16 1.1 jtc # ifdef HAVE_TERMIOS_H
17 1.1 jtc ret = tcgetattr(fd, ts);
18 1.1 jtc # else /* HAVE_TERIOS_H */
19 1.1 jtc # ifdef HAVE_TERMIO_H
20 1.1 jtc ret = ioctl(fd, TCGETA, ts);
21 1.1 jtc # else /* HAVE_TERMIO_H */
22 1.1 jtc ret = ioctl(fd, TIOCGETP, &ts->sgttyb);
23 1.1 jtc # ifdef TIOCGATC
24 1.1 jtc if (ioctl(fd, TIOCGATC, &ts->lchars) < 0)
25 1.1 jtc ret = -1;
26 1.1 jtc # else
27 1.1 jtc if (ioctl(fd, TIOCGETC, &ts->tchars) < 0)
28 1.1 jtc ret = -1;
29 1.1 jtc # ifdef TIOCGLTC
30 1.1 jtc if (ioctl(fd, TIOCGLTC, &ts->ltchars) < 0)
31 1.1 jtc ret = -1;
32 1.1 jtc # endif /* TIOCGLTC */
33 1.1 jtc # endif /* TIOCGATC */
34 1.1 jtc # endif /* HAVE_TERMIO_H */
35 1.1 jtc # endif /* HAVE_TERIOS_H */
36 1.1 jtc return ret;
37 1.1 jtc }
38 1.1 jtc
39 1.1 jtc int
40 1.1 jtc set_tty(fd, ts, flags)
41 1.1 jtc int fd;
42 1.1 jtc TTY_state *ts;
43 1.1 jtc int flags;
44 1.1 jtc {
45 1.1 jtc int ret = 0;
46 1.1 jtc
47 1.1 jtc # ifdef HAVE_TERMIOS_H
48 1.1 jtc ret = tcsetattr(fd, TCSADRAIN, ts);
49 1.1 jtc # else /* HAVE_TERIOS_H */
50 1.1 jtc # ifdef HAVE_TERMIO_H
51 1.1 jtc # ifndef TCSETAW /* e.g. Cray-2 */
52 1.1 jtc /* first wait for output to drain */
53 1.1 jtc # ifdef TCSBRK
54 1.1 jtc if (ioctl(tty_fd, TCSBRK, 1) < 0)
55 1.1 jtc ret = -1;
56 1.1 jtc # else /* the following kludge is minimally intrusive, but sometimes fails */
57 1.1 jtc if (flags & TF_WAIT)
58 1.1 jtc sleep((unsigned)1); /* fake it */
59 1.1 jtc # endif
60 1.1 jtc # endif /* !TCSETAW */
61 1.1 jtc # if defined(_BSD_SYSV) || !defined(TCSETAW)
62 1.1 jtc /* _BSD_SYSV must force TIOCSETN instead of TIOCSETP (preserve type-ahead) */
63 1.1 jtc if (ioctl(tty_fd, TCSETA, ts) < 0)
64 1.1 jtc ret = -1;
65 1.1 jtc # else
66 1.1 jtc if (ioctl(tty_fd, TCSETAW, ts) < 0)
67 1.1 jtc ret = -1;
68 1.1 jtc # endif
69 1.1 jtc # else /* HAVE_TERMIO_H */
70 1.1 jtc # if defined(__mips) && (defined(_SYSTYPE_BSD43) || defined(__SYSTYPE_BSD43))
71 1.1 jtc /* Under RISC/os 5.00, bsd43 environment, after a tty driver
72 1.1 jtc * generated interrupt (eg, INTR, TSTP), all output to tty is
73 1.1 jtc * lost until a SETP is done (there must be a better way of
74 1.1 jtc * doing this...).
75 1.1 jtc */
76 1.1 jtc if (flags & TF_MIPSKLUDGE)
77 1.1 jtc ret = ioctl(fd, TIOCSETP, &ts->sgttyb);
78 1.1 jtc else
79 1.1 jtc # endif /* _SYSTYPE_BSD43 */
80 1.1 jtc ret = ioctl(fd, TIOCSETN, &ts->sgttyb);
81 1.1 jtc # ifdef TIOCGATC
82 1.1 jtc if (ioctl(fd, TIOCSATC, &ts->lchars) < 0)
83 1.1 jtc ret = -1;
84 1.1 jtc # else
85 1.1 jtc if (ioctl(fd, TIOCSETC, &ts->tchars) < 0)
86 1.1 jtc ret = -1;
87 1.1 jtc # ifdef TIOCGLTC
88 1.1 jtc if (ioctl(fd, TIOCSLTC, &ts->ltchars) < 0)
89 1.1 jtc ret = -1;
90 1.1 jtc # endif /* TIOCGLTC */
91 1.1 jtc # endif /* TIOCGATC */
92 1.1 jtc # endif /* HAVE_TERMIO_H */
93 1.1 jtc # endif /* HAVE_TERIOS_H */
94 1.1 jtc return ret;
95 1.1 jtc }
96 1.1 jtc
97 1.1 jtc
98 1.1 jtc /* Initialize tty_fd. Used for saving/reseting tty modes upon
99 1.1 jtc * foreground job completion and for setting up tty process group.
100 1.1 jtc */
101 1.1 jtc void
102 1.1 jtc tty_init(init_ttystate)
103 1.1 jtc int init_ttystate;
104 1.1 jtc {
105 1.1 jtc int do_close = 1;
106 1.1 jtc int tfd;
107 1.3 fair const char *devtty = _PATH_TTY;
108 1.1 jtc
109 1.1 jtc if (tty_fd >= 0) {
110 1.1 jtc close(tty_fd);
111 1.1 jtc tty_fd = -1;
112 1.1 jtc }
113 1.1 jtc tty_devtty = 1;
114 1.1 jtc
115 1.1 jtc /* SCO can't job control on /dev/tty, so don't try... */
116 1.1 jtc #if !defined(__SCO__)
117 1.3 fair if ((tfd = open(devtty, O_RDWR, 0)) < 0) {
118 1.2 tls #ifdef __NeXT
119 1.2 tls /* rlogin on NeXT boxes does not set up the controlling tty,
120 1.2 tls * so force it to be done here...
121 1.2 tls */
122 1.2 tls {
123 1.2 tls extern char *ttyname ARGS((int));
124 1.2 tls char *s = ttyname(isatty(2) ? 2 : 0);
125 1.2 tls int fd;
126 1.2 tls
127 1.2 tls if (s && (fd = open(s, O_RDWR, 0)) >= 0) {
128 1.2 tls close(fd);
129 1.3 fair tfd = open(devtty, O_RDWR, 0);
130 1.2 tls }
131 1.2 tls }
132 1.2 tls #endif /* __NeXT */
133 1.2 tls
134 1.1 jtc /* X11R5 xterm on mips doesn't set controlling tty properly - temporary hack */
135 1.1 jtc # if !defined(__mips) || !(defined(_SYSTYPE_BSD43) || defined(__SYSTYPE_BSD43))
136 1.2 tls if (tfd < 0) {
137 1.2 tls tty_devtty = 0;
138 1.2 tls warningf(FALSE,
139 1.3 fair "No controlling tty (open %s: %s)",
140 1.3 fair devtty, strerror(errno));
141 1.2 tls }
142 1.1 jtc # endif /* __mips */
143 1.1 jtc }
144 1.1 jtc #else /* !__SCO__ */
145 1.1 jtc tfd = -1;
146 1.1 jtc #endif /* __SCO__ */
147 1.1 jtc
148 1.1 jtc if (tfd < 0) {
149 1.1 jtc do_close = 0;
150 1.1 jtc if (isatty(0))
151 1.1 jtc tfd = 0;
152 1.1 jtc else if (isatty(2))
153 1.1 jtc tfd = 2;
154 1.1 jtc else {
155 1.1 jtc warningf(FALSE, "Can't find tty file descriptor");
156 1.1 jtc return;
157 1.1 jtc }
158 1.1 jtc }
159 1.1 jtc if ((tty_fd = ksh_dupbase(tfd, FDBASE)) < 0) {
160 1.1 jtc warningf(FALSE, "j_ttyinit: dup of tty fd failed: %s",
161 1.1 jtc strerror(errno));
162 1.1 jtc } else if (fd_clexec(tty_fd) < 0) {
163 1.1 jtc warningf(FALSE, "j_ttyinit: can't set close-on-exec flag: %s",
164 1.1 jtc strerror(errno));
165 1.1 jtc close(tty_fd);
166 1.1 jtc tty_fd = -1;
167 1.1 jtc } else if (init_ttystate)
168 1.1 jtc get_tty(tty_fd, &tty_state);
169 1.1 jtc if (do_close)
170 1.1 jtc close(tfd);
171 1.1 jtc }
172 1.1 jtc
173 1.1 jtc void
174 1.1 jtc tty_close()
175 1.1 jtc {
176 1.1 jtc if (tty_fd >= 0) {
177 1.1 jtc close(tty_fd);
178 1.1 jtc tty_fd = -1;
179 1.1 jtc }
180 1.1 jtc }
181