blob: 0433f5e2774bfeac092633d77ebb22a1d0b0365a [file] [log] [blame]
Jari Aalto31859422009-01-12 13:36:28 +00001/*
2 * shtty.c -- abstract interface to the terminal, focusing on capabilities.
3 */
4
Jari Aaltobb706242000-03-17 21:46:59 +00005/* Copyright (C) 1999 Free Software Foundation, Inc.
6
7 This file is part of GNU Bash, the Bourne Again SHell.
8
Jari Aalto31859422009-01-12 13:36:28 +00009 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.
Jari Aaltobb706242000-03-17 21:46:59 +000013
Jari Aalto31859422009-01-12 13:36:28 +000014 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.
Jari Aaltobb706242000-03-17 21:46:59 +000018
Jari Aalto31859422009-01-12 13:36:28 +000019 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*/
Jari Aaltobb706242000-03-17 21:46:59 +000022
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
33static TTYSTRUCT ttin, ttout;
34static int ttsaved = 0;
35
36int
37ttgetattr(fd, ttp)
38int fd;
39TTYSTRUCT *ttp;
40{
41#ifdef TERMIOS_TTY_DRIVER
42 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
52int
53ttsetattr(fd, ttp)
54int fd;
55TTYSTRUCT *ttp;
56{
57#ifdef TERMIOS_TTY_DRIVER
58 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
68void
69ttsave()
70{
71 if (ttsaved)
72 return;
73 ttgetattr (0, &ttin);
74 ttgetattr (1, &ttout);
75 ttsaved = 1;
76}
77
78void
79ttrestore()
80{
81 if (ttsaved == 0)
82 return;
83 ttsetattr (0, &ttin);
84 ttsetattr (1, &ttout);
85 ttsaved = 0;
86}
87
Jari Aalto31859422009-01-12 13:36:28 +000088/* Retrieve the internally-saved attributes associated with tty fd FD. */
Jari Aaltobb706242000-03-17 21:46:59 +000089TTYSTRUCT *
90ttattr (fd)
91 int fd;
92{
93 if (ttsaved == 0)
94 return ((TTYSTRUCT *)0);
95 if (fd == 0)
96 return &ttin;
97 else if (fd == 1)
98 return &ttout;
99 else
100 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 */
107int
108tt_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 ttp->c_lflag &= ~ICANON;
115
116 ttp->c_lflag |= ISIG;
117# ifdef IEXTEN
118 ttp->c_lflag |= IEXTEN;
119# endif
120
121 ttp->c_iflag |= ICRNL; /* make sure we get CR->NL on input */
122 ttp->c_iflag &= ~INLCR; /* but no NL->CR */
123
124# ifdef OPOST
125 ttp->c_oflag |= OPOST;
126# endif
127# ifdef ONLCR
128 ttp->c_oflag |= ONLCR;
129# endif
130# ifdef OCRNL
131 ttp->c_oflag &= ~OCRNL;
132# endif
133# ifdef ONOCR
134 ttp->c_oflag &= ~ONOCR;
135# endif
136# ifdef ONLRET
137 ttp->c_oflag &= ~ONLRET;
138# endif
139
140 ttp->c_cc[VMIN] = 1;
141 ttp->c_cc[VTIME] = 0;
142
143#else
144
145 ttp->sg_flags |= CBREAK;
146
147#endif
148
149 return 0;
150}
151
Jari Aalto31859422009-01-12 13:36:28 +0000152/* Set the tty associated with FD and TTP into one-character-at-a-time mode */
153int
154ttfd_onechar (fd, ttp)
155 int fd;
156 TTYSTRUCT *ttp;
157{
158 if (tt_setonechar(ttp) < 0)
159 return -1;
160 return (ttsetattr (fd, ttp));
161}
162
Jari Aaltobb706242000-03-17 21:46:59 +0000163/* Set the terminal into one-character-at-a-time mode */
164int
165ttonechar ()
166{
167 TTYSTRUCT tt;
168
169 if (ttsaved == 0)
170 return -1;
171 tt = ttin;
Jari Aalto31859422009-01-12 13:36:28 +0000172 return (ttfd_onechar (0, &tt));
Jari Aaltobb706242000-03-17 21:46:59 +0000173}
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 */
179int
180tt_setnoecho(ttp)
181 TTYSTRUCT *ttp;
182{
183#if defined (TERMIOS_TTY_DRIVER) || defined (TERMIO_TTY_DRIVER)
184 ttp->c_lflag &= ~(ECHO|ECHOK|ECHONL);
185#else
186 ttp->sg_flags &= ~ECHO;
187#endif
188
189 return 0;
190}
191
Jari Aalto31859422009-01-12 13:36:28 +0000192/* Set the tty associated with FD and TTP into no-echo mode */
193int
194ttfd_noecho (fd, ttp)
195 int fd;
196 TTYSTRUCT *ttp;
197{
198 if (tt_setnoecho (ttp) < 0)
199 return -1;
200 return (ttsetattr (fd, ttp));
201}
202
Jari Aaltobb706242000-03-17 21:46:59 +0000203/* Set the terminal into no-echo mode */
204int
205ttnoecho ()
206{
207 TTYSTRUCT tt;
208
209 if (ttsaved == 0)
210 return -1;
211 tt = ttin;
Jari Aalto31859422009-01-12 13:36:28 +0000212 return (ttfd_noecho (0, &tt));
Jari Aaltobb706242000-03-17 21:46:59 +0000213}
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 */
219int
220tt_seteightbit (ttp)
221 TTYSTRUCT *ttp;
222{
223#if defined (TERMIOS_TTY_DRIVER) || defined (TERMIO_TTY_DRIVER)
224 ttp->c_iflag &= ~ISTRIP;
225 ttp->c_cflag |= CS8;
226 ttp->c_cflag &= ~PARENB;
227#else
228 ttp->sg_flags |= ANYP;
229#endif
230
231 return 0;
232}
233
Jari Aalto31859422009-01-12 13:36:28 +0000234/* Set the tty associated with FD and TTP into eight-bit mode */
235int
236ttfd_eightbit (fd, ttp)
237 int fd;
238 TTYSTRUCT *ttp;
239{
240 if (tt_seteightbit (ttp) < 0)
241 return -1;
242 return (ttsetattr (fd, ttp));
243}
244
Jari Aaltobb706242000-03-17 21:46:59 +0000245/* Set the terminal into eight-bit mode */
246int
247tteightbit ()
248{
249 TTYSTRUCT tt;
250
251 if (ttsaved == 0)
252 return -1;
253 tt = ttin;
Jari Aalto31859422009-01-12 13:36:28 +0000254 return (ttfd_eightbit (0, &tt));
Jari Aaltobb706242000-03-17 21:46:59 +0000255}
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 */
261int
262tt_setnocanon (ttp)
263 TTYSTRUCT *ttp;
264{
265#if defined (TERMIOS_TTY_DRIVER) || defined (TERMIO_TTY_DRIVER)
266 ttp->c_lflag &= ~ICANON;
267#endif
268
269 return 0;
270}
271
Jari Aalto31859422009-01-12 13:36:28 +0000272/* Set the tty associated with FD and TTP into non-canonical mode */
273int
274ttfd_nocanon (fd, ttp)
275 int fd;
276 TTYSTRUCT *ttp;
277{
278 if (tt_setnocanon (ttp) < 0)
279 return -1;
280 return (ttsetattr (fd, ttp));
281}
282
Jari Aaltobb706242000-03-17 21:46:59 +0000283/* Set the terminal into non-canonical mode */
284int
285ttnocanon ()
286{
287 TTYSTRUCT tt;
288
289 if (ttsaved == 0)
290 return -1;
291 tt = ttin;
Jari Aalto31859422009-01-12 13:36:28 +0000292 return (ttfd_nocanon (0, &tt));
Jari Aaltobb706242000-03-17 21:46:59 +0000293}
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 */
299int
300tt_setcbreak(ttp)
301 TTYSTRUCT *ttp;
302{
303 if (tt_setonechar (ttp) < 0)
304 return -1;
305 return (tt_setnoecho (ttp));
306}
307
Jari Aalto31859422009-01-12 13:36:28 +0000308/* Set the tty associated with FD and TTP into cbreak (no-echo,
309 one-character-at-a-time) mode */
310int
311ttfd_cbreak (fd, ttp)
312 int fd;
313 TTYSTRUCT *ttp;
314{
315 if (tt_setcbreak (ttp) < 0)
316 return -1;
317 return (ttsetattr (fd, ttp));
318}
319
Jari Aaltobb706242000-03-17 21:46:59 +0000320/* Set the terminal into cbreak (no-echo, one-character-at-a-time) mode */
321int
322ttcbreak ()
323{
324 TTYSTRUCT tt;
325
326 if (ttsaved == 0)
327 return -1;
328 tt = ttin;
Jari Aalto31859422009-01-12 13:36:28 +0000329 return (ttfd_cbreak (0, &tt));
Jari Aaltobb706242000-03-17 21:46:59 +0000330}