Line data Source code
1 : /*
2 : * shtty.c -- abstract interface to the terminal, focusing on capabilities.
3 : */
4 :
5 : /* Copyright (C) 1999 Free Software Foundation, Inc.
6 :
7 : This file is part of GNU Bash, the Bourne Again SHell.
8 :
9 : Bash is free software: you can redistribute it and/or modify
10 : it under the terms of the GNU General Public License as published by
11 : the Free Software Foundation, either version 3 of the License, or
12 : (at your option) any later version.
13 :
14 : Bash is distributed in the hope that it will be useful,
15 : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : GNU General Public License for more details.
18 :
19 : You should have received a copy of the GNU General Public License
20 : along with Bash. If not, see <http://www.gnu.org/licenses/>.
21 : */
22 :
23 : #ifdef HAVE_CONFIG_H
24 : # include <config.h>
25 : #endif
26 :
27 : #ifdef HAVE_UNISTD_H
28 : # include <unistd.h>
29 : #endif
30 :
31 : #include <shtty.h>
32 :
33 : static TTYSTRUCT ttin, ttout;
34 : static int ttsaved = 0;
35 :
36 : int
37 0 : ttgetattr(fd, ttp)
38 : int fd;
39 : TTYSTRUCT *ttp;
40 : {
41 : #ifdef TERMIOS_TTY_DRIVER
42 0 : return tcgetattr(fd, ttp);
43 : #else
44 : # ifdef TERMIO_TTY_DRIVER
45 : return ioctl(fd, TCGETA, ttp);
46 : # else
47 : return ioctl(fd, TIOCGETP, ttp);
48 : # endif
49 : #endif
50 : }
51 :
52 : int
53 0 : ttsetattr(fd, ttp)
54 : int fd;
55 : TTYSTRUCT *ttp;
56 : {
57 : #ifdef TERMIOS_TTY_DRIVER
58 0 : return tcsetattr(fd, TCSADRAIN, ttp);
59 : #else
60 : # ifdef TERMIO_TTY_DRIVER
61 : return ioctl(fd, TCSETAW, ttp);
62 : # else
63 : return ioctl(fd, TIOCSETN, ttp);
64 : # endif
65 : #endif
66 : }
67 :
68 : void
69 0 : ttsave()
70 : {
71 0 : if (ttsaved)
72 : return;
73 0 : ttgetattr (0, &ttin);
74 0 : ttgetattr (1, &ttout);
75 0 : ttsaved = 1;
76 : }
77 :
78 : void
79 0 : ttrestore()
80 : {
81 0 : if (ttsaved == 0)
82 : return;
83 0 : ttsetattr (0, &ttin);
84 0 : ttsetattr (1, &ttout);
85 0 : ttsaved = 0;
86 : }
87 :
88 : /* Retrieve the internally-saved attributes associated with tty fd FD. */
89 : TTYSTRUCT *
90 0 : ttattr (fd)
91 : int fd;
92 : {
93 0 : if (ttsaved == 0)
94 : return ((TTYSTRUCT *)0);
95 0 : if (fd == 0)
96 : return &ttin;
97 0 : else if (fd == 1)
98 : return &ttout;
99 : else
100 0 : return ((TTYSTRUCT *)0);
101 : }
102 :
103 : /*
104 : * Change attributes in ttp so that when it is installed using
105 : * ttsetattr, the terminal will be in one-char-at-a-time mode.
106 : */
107 : int
108 0 : tt_setonechar(ttp)
109 : TTYSTRUCT *ttp;
110 : {
111 : #if defined (TERMIOS_TTY_DRIVER) || defined (TERMIO_TTY_DRIVER)
112 :
113 : /* XXX - might not want this -- it disables erase and kill processing. */
114 0 : ttp->c_lflag &= ~ICANON;
115 :
116 0 : ttp->c_lflag |= ISIG;
117 : # ifdef IEXTEN
118 0 : ttp->c_lflag |= IEXTEN;
119 : # endif
120 :
121 0 : ttp->c_iflag |= ICRNL; /* make sure we get CR->NL on input */
122 0 : ttp->c_iflag &= ~INLCR; /* but no NL->CR */
123 :
124 : # ifdef OPOST
125 0 : ttp->c_oflag |= OPOST;
126 : # endif
127 : # ifdef ONLCR
128 0 : ttp->c_oflag |= ONLCR;
129 : # endif
130 : # ifdef OCRNL
131 0 : ttp->c_oflag &= ~OCRNL;
132 : # endif
133 : # ifdef ONOCR
134 0 : ttp->c_oflag &= ~ONOCR;
135 : # endif
136 : # ifdef ONLRET
137 0 : ttp->c_oflag &= ~ONLRET;
138 : # endif
139 :
140 0 : ttp->c_cc[VMIN] = 1;
141 0 : ttp->c_cc[VTIME] = 0;
142 :
143 : #else
144 :
145 : ttp->sg_flags |= CBREAK;
146 :
147 : #endif
148 :
149 0 : return 0;
150 : }
151 :
152 : /* Set the tty associated with FD and TTP into one-character-at-a-time mode */
153 : int
154 0 : ttfd_onechar (fd, ttp)
155 : int fd;
156 : TTYSTRUCT *ttp;
157 : {
158 0 : if (tt_setonechar(ttp) < 0)
159 : return -1;
160 0 : return (ttsetattr (fd, ttp));
161 : }
162 :
163 : /* Set the terminal into one-character-at-a-time mode */
164 : int
165 0 : ttonechar ()
166 : {
167 0 : TTYSTRUCT tt;
168 :
169 0 : if (ttsaved == 0)
170 : return -1;
171 0 : tt = ttin;
172 0 : return (ttfd_onechar (0, &tt));
173 : }
174 :
175 : /*
176 : * Change attributes in ttp so that when it is installed using
177 : * ttsetattr, the terminal will be in no-echo mode.
178 : */
179 : int
180 0 : tt_setnoecho(ttp)
181 : TTYSTRUCT *ttp;
182 : {
183 : #if defined (TERMIOS_TTY_DRIVER) || defined (TERMIO_TTY_DRIVER)
184 0 : ttp->c_lflag &= ~(ECHO|ECHOK|ECHONL);
185 : #else
186 : ttp->sg_flags &= ~ECHO;
187 : #endif
188 :
189 0 : return 0;
190 : }
191 :
192 : /* Set the tty associated with FD and TTP into no-echo mode */
193 : int
194 0 : ttfd_noecho (fd, ttp)
195 : int fd;
196 : TTYSTRUCT *ttp;
197 : {
198 0 : if (tt_setnoecho (ttp) < 0)
199 : return -1;
200 0 : return (ttsetattr (fd, ttp));
201 : }
202 :
203 : /* Set the terminal into no-echo mode */
204 : int
205 0 : ttnoecho ()
206 : {
207 0 : TTYSTRUCT tt;
208 :
209 0 : if (ttsaved == 0)
210 : return -1;
211 0 : tt = ttin;
212 0 : return (ttfd_noecho (0, &tt));
213 : }
214 :
215 : /*
216 : * Change attributes in ttp so that when it is installed using
217 : * ttsetattr, the terminal will be in eight-bit mode (pass8).
218 : */
219 : int
220 0 : tt_seteightbit (ttp)
221 : TTYSTRUCT *ttp;
222 : {
223 : #if defined (TERMIOS_TTY_DRIVER) || defined (TERMIO_TTY_DRIVER)
224 0 : ttp->c_iflag &= ~ISTRIP;
225 0 : ttp->c_cflag |= CS8;
226 0 : ttp->c_cflag &= ~PARENB;
227 : #else
228 : ttp->sg_flags |= ANYP;
229 : #endif
230 :
231 0 : return 0;
232 : }
233 :
234 : /* Set the tty associated with FD and TTP into eight-bit mode */
235 : int
236 0 : ttfd_eightbit (fd, ttp)
237 : int fd;
238 : TTYSTRUCT *ttp;
239 : {
240 0 : if (tt_seteightbit (ttp) < 0)
241 : return -1;
242 0 : return (ttsetattr (fd, ttp));
243 : }
244 :
245 : /* Set the terminal into eight-bit mode */
246 : int
247 0 : tteightbit ()
248 : {
249 0 : TTYSTRUCT tt;
250 :
251 0 : if (ttsaved == 0)
252 : return -1;
253 0 : tt = ttin;
254 0 : return (ttfd_eightbit (0, &tt));
255 : }
256 :
257 : /*
258 : * Change attributes in ttp so that when it is installed using
259 : * ttsetattr, the terminal will be in non-canonical input mode.
260 : */
261 : int
262 0 : tt_setnocanon (ttp)
263 : TTYSTRUCT *ttp;
264 : {
265 : #if defined (TERMIOS_TTY_DRIVER) || defined (TERMIO_TTY_DRIVER)
266 0 : ttp->c_lflag &= ~ICANON;
267 : #endif
268 :
269 0 : return 0;
270 : }
271 :
272 : /* Set the tty associated with FD and TTP into non-canonical mode */
273 : int
274 0 : ttfd_nocanon (fd, ttp)
275 : int fd;
276 : TTYSTRUCT *ttp;
277 : {
278 0 : if (tt_setnocanon (ttp) < 0)
279 : return -1;
280 0 : return (ttsetattr (fd, ttp));
281 : }
282 :
283 : /* Set the terminal into non-canonical mode */
284 : int
285 0 : ttnocanon ()
286 : {
287 0 : TTYSTRUCT tt;
288 :
289 0 : if (ttsaved == 0)
290 : return -1;
291 0 : tt = ttin;
292 0 : return (ttfd_nocanon (0, &tt));
293 : }
294 :
295 : /*
296 : * Change attributes in ttp so that when it is installed using
297 : * ttsetattr, the terminal will be in cbreak, no-echo mode.
298 : */
299 : int
300 0 : tt_setcbreak(ttp)
301 : TTYSTRUCT *ttp;
302 : {
303 0 : if (tt_setonechar (ttp) < 0)
304 : return -1;
305 0 : return (tt_setnoecho (ttp));
306 : }
307 :
308 : /* Set the tty associated with FD and TTP into cbreak (no-echo,
309 : one-character-at-a-time) mode */
310 : int
311 0 : ttfd_cbreak (fd, ttp)
312 : int fd;
313 : TTYSTRUCT *ttp;
314 : {
315 0 : if (tt_setcbreak (ttp) < 0)
316 : return -1;
317 0 : return (ttsetattr (fd, ttp));
318 : }
319 :
320 : /* Set the terminal into cbreak (no-echo, one-character-at-a-time) mode */
321 : int
322 0 : ttcbreak ()
323 : {
324 0 : TTYSTRUCT tt;
325 :
326 0 : if (ttsaved == 0)
327 : return -1;
328 0 : tt = ttin;
329 0 : return (ttfd_cbreak (0, &tt));
330 : }
|