improve of cmpl.
[bush.git] / lib / sh / shtty.c
blob3090d077e30167a9eccb0f0a8e35350a05c889d2
1 /*
2 * shtty.c -- abstract interface to the terminal, focusing on capabilities.
3 */
5 /* Copyright (C) 1999 Free Software Foundation, Inc.
7 This file is part of GNU Bush, the Bourne Again SHell.
9 Bush 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.
14 Bush 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.
19 You should have received a copy of the GNU General Public License
20 along with Bush. If not, see <http://www.gnu.org/licenses/>.
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
31 #include <shtty.h>
33 static TTYSTRUCT ttin, ttout;
34 static int ttsaved = 0;
36 int
37 ttgetattr(fd, ttp)
38 int fd;
39 TTYSTRUCT *ttp;
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
52 int
53 ttsetattr(fd, ttp)
54 int fd;
55 TTYSTRUCT *ttp;
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
68 void
69 ttsave()
71 if (ttsaved)
72 return;
73 ttgetattr (0, &ttin);
74 ttgetattr (1, &ttout);
75 ttsaved = 1;
78 void
79 ttrestore()
81 if (ttsaved == 0)
82 return;
83 ttsetattr (0, &ttin);
84 ttsetattr (1, &ttout);
85 ttsaved = 0;
88 /* Retrieve the internally-saved attributes associated with tty fd FD. */
89 TTYSTRUCT *
90 ttattr (fd)
91 int fd;
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);
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.
108 tt_setonechar(ttp)
109 TTYSTRUCT *ttp;
111 #if defined (TERMIOS_TTY_DRIVER) || defined (TERMIO_TTY_DRIVER)
113 /* XXX - might not want this -- it disables erase and kill processing. */
114 ttp->c_lflag &= ~ICANON;
116 ttp->c_lflag |= ISIG;
117 # ifdef IEXTEN
118 ttp->c_lflag |= IEXTEN;
119 # endif
121 ttp->c_iflag |= ICRNL; /* make sure we get CR->NL on input */
122 ttp->c_iflag &= ~INLCR; /* but no NL->CR */
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
140 ttp->c_cc[VMIN] = 1;
141 ttp->c_cc[VTIME] = 0;
143 #else
145 ttp->sg_flags |= CBREAK;
147 #endif
149 return 0;
152 /* Set the tty associated with FD and TTP into one-character-at-a-time mode */
154 ttfd_onechar (fd, ttp)
155 int fd;
156 TTYSTRUCT *ttp;
158 if (tt_setonechar(ttp) < 0)
159 return -1;
160 return (ttsetattr (fd, ttp));
163 /* Set the terminal into one-character-at-a-time mode */
165 ttonechar ()
167 TTYSTRUCT tt;
169 if (ttsaved == 0)
170 return -1;
171 tt = ttin;
172 return (ttfd_onechar (0, &tt));
176 * Change attributes in ttp so that when it is installed using
177 * ttsetattr, the terminal will be in no-echo mode.
180 tt_setnoecho(ttp)
181 TTYSTRUCT *ttp;
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
189 return 0;
192 /* Set the tty associated with FD and TTP into no-echo mode */
194 ttfd_noecho (fd, ttp)
195 int fd;
196 TTYSTRUCT *ttp;
198 if (tt_setnoecho (ttp) < 0)
199 return -1;
200 return (ttsetattr (fd, ttp));
203 /* Set the terminal into no-echo mode */
205 ttnoecho ()
207 TTYSTRUCT tt;
209 if (ttsaved == 0)
210 return -1;
211 tt = ttin;
212 return (ttfd_noecho (0, &tt));
216 * Change attributes in ttp so that when it is installed using
217 * ttsetattr, the terminal will be in eight-bit mode (pass8).
220 tt_seteightbit (ttp)
221 TTYSTRUCT *ttp;
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
231 return 0;
234 /* Set the tty associated with FD and TTP into eight-bit mode */
236 ttfd_eightbit (fd, ttp)
237 int fd;
238 TTYSTRUCT *ttp;
240 if (tt_seteightbit (ttp) < 0)
241 return -1;
242 return (ttsetattr (fd, ttp));
245 /* Set the terminal into eight-bit mode */
247 tteightbit ()
249 TTYSTRUCT tt;
251 if (ttsaved == 0)
252 return -1;
253 tt = ttin;
254 return (ttfd_eightbit (0, &tt));
258 * Change attributes in ttp so that when it is installed using
259 * ttsetattr, the terminal will be in non-canonical input mode.
262 tt_setnocanon (ttp)
263 TTYSTRUCT *ttp;
265 #if defined (TERMIOS_TTY_DRIVER) || defined (TERMIO_TTY_DRIVER)
266 ttp->c_lflag &= ~ICANON;
267 #endif
269 return 0;
272 /* Set the tty associated with FD and TTP into non-canonical mode */
274 ttfd_nocanon (fd, ttp)
275 int fd;
276 TTYSTRUCT *ttp;
278 if (tt_setnocanon (ttp) < 0)
279 return -1;
280 return (ttsetattr (fd, ttp));
283 /* Set the terminal into non-canonical mode */
285 ttnocanon ()
287 TTYSTRUCT tt;
289 if (ttsaved == 0)
290 return -1;
291 tt = ttin;
292 return (ttfd_nocanon (0, &tt));
296 * Change attributes in ttp so that when it is installed using
297 * ttsetattr, the terminal will be in cbreak, no-echo mode.
300 tt_setcbreak(ttp)
301 TTYSTRUCT *ttp;
303 if (tt_setonechar (ttp) < 0)
304 return -1;
305 return (tt_setnoecho (ttp));
308 /* Set the tty associated with FD and TTP into cbreak (no-echo,
309 one-character-at-a-time) mode */
311 ttfd_cbreak (fd, ttp)
312 int fd;
313 TTYSTRUCT *ttp;
315 if (tt_setcbreak (ttp) < 0)
316 return -1;
317 return (ttsetattr (fd, ttp));
320 /* Set the terminal into cbreak (no-echo, one-character-at-a-time) mode */
322 ttcbreak ()
324 TTYSTRUCT tt;
326 if (ttsaved == 0)
327 return -1;
328 tt = ttin;
329 return (ttfd_cbreak (0, &tt));