tty.c revision 1.24 1 1.24 jdc /* $NetBSD: tty.c,v 1.24 2000/12/19 21:34:24 jdc Exp $ */
2 1.7 mikel
3 1.1 mycroft /*-
4 1.6 cgd * Copyright (c) 1992, 1993, 1994
5 1.2 cgd * The Regents of the University of California. All rights reserved.
6 1.1 mycroft *
7 1.1 mycroft * Redistribution and use in source and binary forms, with or without
8 1.1 mycroft * modification, are permitted provided that the following conditions
9 1.1 mycroft * are met:
10 1.1 mycroft * 1. Redistributions of source code must retain the above copyright
11 1.1 mycroft * notice, this list of conditions and the following disclaimer.
12 1.1 mycroft * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 mycroft * notice, this list of conditions and the following disclaimer in the
14 1.1 mycroft * documentation and/or other materials provided with the distribution.
15 1.1 mycroft * 3. All advertising materials mentioning features or use of this software
16 1.1 mycroft * must display the following acknowledgement:
17 1.1 mycroft * This product includes software developed by the University of
18 1.1 mycroft * California, Berkeley and its contributors.
19 1.1 mycroft * 4. Neither the name of the University nor the names of its contributors
20 1.1 mycroft * may be used to endorse or promote products derived from this software
21 1.1 mycroft * without specific prior written permission.
22 1.1 mycroft *
23 1.1 mycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 mycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 mycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 mycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 mycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 mycroft * SUCH DAMAGE.
34 1.1 mycroft */
35 1.1 mycroft
36 1.7 mikel #include <sys/cdefs.h>
37 1.1 mycroft #ifndef lint
38 1.7 mikel #if 0
39 1.10 perry static char sccsid[] = "@(#)tty.c 8.6 (Berkeley) 1/10/95";
40 1.7 mikel #else
41 1.24 jdc __RCSID("$NetBSD: tty.c,v 1.24 2000/12/19 21:34:24 jdc Exp $");
42 1.7 mikel #endif
43 1.11 mrg #endif /* not lint */
44 1.11 mrg
45 1.11 mrg #include <sys/types.h>
46 1.1 mycroft
47 1.6 cgd #include <stdlib.h>
48 1.1 mycroft #include <termios.h>
49 1.1 mycroft #include <unistd.h>
50 1.16 blymn #include <sys/fcntl.h>
51 1.16 blymn #include <sys/ioctl.h>
52 1.1 mycroft
53 1.6 cgd #include "curses.h"
54 1.14 jdc #include "curses_private.h"
55 1.6 cgd
56 1.2 cgd /*
57 1.2 cgd * In general, curses should leave tty hardware settings alone (speed, parity,
58 1.2 cgd * word size). This is most easily done in BSD by using TCSASOFT on all
59 1.2 cgd * tcsetattr calls. On other systems, it would be better to get and restore
60 1.2 cgd * those attributes at each change, or at least when stopped and restarted.
61 1.2 cgd * See also the comments in getterm().
62 1.2 cgd */
63 1.2 cgd #ifdef TCSASOFT
64 1.11 mrg int __tcaction = 1; /* Ignore hardware settings. */
65 1.2 cgd #else
66 1.11 mrg int __tcaction = 0;
67 1.2 cgd #endif
68 1.2 cgd
69 1.22 jdc struct termios __orig_termios, __baset;
70 1.22 jdc int __endwin;
71 1.22 jdc static struct termios cbreakt, rawt, *curt;
72 1.22 jdc static int useraw;
73 1.22 jdc static int ovmin = 1;
74 1.22 jdc static int ovtime = 0;
75 1.1 mycroft
76 1.2 cgd #ifndef OXTABS
77 1.2 cgd #ifdef XTABS /* SMI uses XTABS. */
78 1.2 cgd #define OXTABS XTABS
79 1.2 cgd #else
80 1.2 cgd #define OXTABS 0
81 1.2 cgd #endif
82 1.2 cgd #endif
83 1.2 cgd
84 1.1 mycroft /*
85 1.1 mycroft * gettmode --
86 1.1 mycroft * Do terminal type initialization.
87 1.1 mycroft */
88 1.1 mycroft int
89 1.15 blymn gettmode(void)
90 1.1 mycroft {
91 1.2 cgd useraw = 0;
92 1.11 mrg
93 1.2 cgd if (tcgetattr(STDIN_FILENO, &__orig_termios))
94 1.2 cgd return (ERR);
95 1.2 cgd
96 1.2 cgd __baset = __orig_termios;
97 1.2 cgd __baset.c_oflag &= ~OXTABS;
98 1.19 blymn
99 1.24 jdc __GT = 0; /* historical. was used before we wired OXTABS off */
100 1.24 jdc __NONL = (__baset.c_oflag & ONLCR) == 0;
101 1.1 mycroft
102 1.2 cgd /*
103 1.2 cgd * XXX
104 1.2 cgd * System V and SMI systems overload VMIN and VTIME, such that
105 1.2 cgd * VMIN is the same as the VEOF element, and VTIME is the same
106 1.2 cgd * as the VEOL element. This means that, if VEOF was ^D, the
107 1.2 cgd * default VMIN is 4. Majorly stupid.
108 1.2 cgd */
109 1.2 cgd cbreakt = __baset;
110 1.18 mycroft cbreakt.c_lflag &= ~(ECHO | ECHONL | ICANON);
111 1.2 cgd cbreakt.c_cc[VMIN] = 1;
112 1.2 cgd cbreakt.c_cc[VTIME] = 0;
113 1.2 cgd
114 1.2 cgd rawt = cbreakt;
115 1.11 mrg rawt.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | INLCR | IGNCR | ICRNL | IXON);
116 1.2 cgd rawt.c_oflag &= ~OPOST;
117 1.18 mycroft rawt.c_lflag &= ~(ISIG | IEXTEN);
118 1.2 cgd
119 1.2 cgd /*
120 1.2 cgd * In general, curses should leave hardware-related settings alone.
121 1.2 cgd * This includes parity and word size. Older versions set the tty
122 1.2 cgd * to 8 bits, no parity in raw(), but this is considered to be an
123 1.2 cgd * artifact of the old tty interface. If it's desired to change
124 1.2 cgd * parity and word size, the TCSASOFT bit has to be removed from the
125 1.2 cgd * calls that switch to/from "raw" mode.
126 1.2 cgd */
127 1.2 cgd if (!__tcaction) {
128 1.2 cgd rawt.c_iflag &= ~ISTRIP;
129 1.11 mrg rawt.c_cflag &= ~(CSIZE | PARENB);
130 1.2 cgd rawt.c_cflag |= CS8;
131 1.2 cgd }
132 1.1 mycroft
133 1.2 cgd curt = &__baset;
134 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
135 1.2 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
136 1.1 mycroft }
137 1.1 mycroft
138 1.1 mycroft int
139 1.15 blymn raw(void)
140 1.1 mycroft {
141 1.9 phil /* Check if we need to restart ... */
142 1.22 jdc if (__endwin)
143 1.23 jdc __restartwin();
144 1.11 mrg
145 1.1 mycroft useraw = __pfast = __rawmode = 1;
146 1.2 cgd curt = &rawt;
147 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
148 1.6 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
149 1.1 mycroft }
150 1.1 mycroft
151 1.1 mycroft int
152 1.15 blymn noraw(void)
153 1.1 mycroft {
154 1.9 phil /* Check if we need to restart ... */
155 1.22 jdc if (__endwin)
156 1.23 jdc __restartwin();
157 1.11 mrg
158 1.1 mycroft useraw = __pfast = __rawmode = 0;
159 1.2 cgd curt = &__baset;
160 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
161 1.6 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
162 1.1 mycroft }
163 1.1 mycroft
164 1.1 mycroft int
165 1.15 blymn cbreak(void)
166 1.1 mycroft {
167 1.9 phil /* Check if we need to restart ... */
168 1.22 jdc if (__endwin)
169 1.23 jdc __restartwin();
170 1.11 mrg
171 1.1 mycroft __rawmode = 1;
172 1.2 cgd curt = useraw ? &rawt : &cbreakt;
173 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
174 1.6 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
175 1.1 mycroft }
176 1.1 mycroft
177 1.1 mycroft int
178 1.15 blymn nocbreak(void)
179 1.1 mycroft {
180 1.9 phil /* Check if we need to restart ... */
181 1.22 jdc if (__endwin)
182 1.23 jdc __restartwin();
183 1.1 mycroft
184 1.1 mycroft __rawmode = 0;
185 1.2 cgd curt = useraw ? &rawt : &__baset;
186 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
187 1.6 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
188 1.1 mycroft }
189 1.11 mrg
190 1.11 mrg int
191 1.15 blymn __delay(void)
192 1.11 mrg {
193 1.11 mrg /* Check if we need to restart ... */
194 1.22 jdc if (__endwin)
195 1.23 jdc __restartwin();
196 1.11 mrg
197 1.11 mrg rawt.c_cc[VMIN] = 1;
198 1.11 mrg rawt.c_cc[VTIME] = 0;
199 1.11 mrg cbreakt.c_cc[VMIN] = 1;
200 1.11 mrg cbreakt.c_cc[VTIME] = 0;
201 1.11 mrg __baset.c_cc[VMIN] = 1;
202 1.11 mrg __baset.c_cc[VTIME] = 0;
203 1.11 mrg
204 1.11 mrg return (tcsetattr(STDIN_FILENO, __tcaction ?
205 1.11 mrg TCSASOFT : TCSANOW, curt) ? ERR : OK);
206 1.11 mrg }
207 1.11 mrg
208 1.11 mrg int
209 1.15 blymn __nodelay(void)
210 1.11 mrg {
211 1.11 mrg /* Check if we need to restart ... */
212 1.22 jdc if (__endwin)
213 1.23 jdc __restartwin();
214 1.11 mrg
215 1.11 mrg rawt.c_cc[VMIN] = 0;
216 1.11 mrg rawt.c_cc[VTIME] = 0;
217 1.11 mrg cbreakt.c_cc[VMIN] = 0;
218 1.11 mrg cbreakt.c_cc[VTIME] = 0;
219 1.11 mrg __baset.c_cc[VMIN] = 0;
220 1.11 mrg __baset.c_cc[VTIME] = 0;
221 1.11 mrg
222 1.11 mrg return (tcsetattr(STDIN_FILENO, __tcaction ?
223 1.11 mrg TCSASOFT : TCSANOW, curt) ? ERR : OK);
224 1.11 mrg }
225 1.11 mrg
226 1.11 mrg void
227 1.15 blymn __save_termios(void)
228 1.11 mrg {
229 1.11 mrg /* Check if we need to restart ... */
230 1.22 jdc if (__endwin)
231 1.23 jdc __restartwin();
232 1.11 mrg
233 1.11 mrg ovmin = cbreakt.c_cc[VMIN];
234 1.11 mrg ovtime = cbreakt.c_cc[VTIME];
235 1.11 mrg }
236 1.11 mrg
237 1.11 mrg void
238 1.15 blymn __restore_termios(void)
239 1.11 mrg {
240 1.11 mrg /* Check if we need to restart ... */
241 1.22 jdc if (__endwin)
242 1.23 jdc __restartwin();
243 1.11 mrg
244 1.11 mrg rawt.c_cc[VMIN] = ovmin;
245 1.11 mrg rawt.c_cc[VTIME] = ovtime;
246 1.11 mrg cbreakt.c_cc[VMIN] = ovmin;
247 1.11 mrg cbreakt.c_cc[VTIME] = ovtime;
248 1.11 mrg __baset.c_cc[VMIN] = ovmin;
249 1.11 mrg __baset.c_cc[VTIME] = ovtime;
250 1.11 mrg }
251 1.11 mrg
252 1.11 mrg int
253 1.15 blymn __timeout(int delay)
254 1.11 mrg {
255 1.11 mrg /* Check if we need to restart ... */
256 1.22 jdc if (__endwin)
257 1.23 jdc __restartwin();
258 1.11 mrg
259 1.11 mrg ovmin = cbreakt.c_cc[VMIN];
260 1.11 mrg ovtime = cbreakt.c_cc[VTIME];
261 1.11 mrg rawt.c_cc[VMIN] = 0;
262 1.11 mrg rawt.c_cc[VTIME] = delay;
263 1.11 mrg cbreakt.c_cc[VMIN] = 0;
264 1.11 mrg cbreakt.c_cc[VTIME] = delay;
265 1.11 mrg __baset.c_cc[VMIN] = 0;
266 1.11 mrg __baset.c_cc[VTIME] = delay;
267 1.11 mrg
268 1.11 mrg return (tcsetattr(STDIN_FILENO, __tcaction ?
269 1.11 mrg TCSASOFT | TCSANOW : TCSANOW, curt) ? ERR : OK);
270 1.11 mrg }
271 1.11 mrg
272 1.11 mrg int
273 1.15 blymn __notimeout(void)
274 1.11 mrg {
275 1.11 mrg /* Check if we need to restart ... */
276 1.22 jdc if (__endwin)
277 1.23 jdc __restartwin();
278 1.11 mrg
279 1.11 mrg rawt.c_cc[VMIN] = 1;
280 1.11 mrg rawt.c_cc[VTIME] = 0;
281 1.11 mrg cbreakt.c_cc[VMIN] = 1;
282 1.11 mrg cbreakt.c_cc[VTIME] = 0;
283 1.11 mrg __baset.c_cc[VMIN] = 1;
284 1.11 mrg __baset.c_cc[VTIME] = 0;
285 1.11 mrg
286 1.11 mrg return (tcsetattr(STDIN_FILENO, __tcaction ?
287 1.11 mrg TCSASOFT | TCSANOW : TCSANOW, curt) ? ERR : OK);
288 1.11 mrg }
289 1.11 mrg
290 1.1 mycroft int
291 1.15 blymn echo(void)
292 1.1 mycroft {
293 1.9 phil /* Check if we need to restart ... */
294 1.22 jdc if (__endwin)
295 1.23 jdc __restartwin();
296 1.9 phil
297 1.1 mycroft __echoit = 1;
298 1.17 mycroft return (OK);
299 1.1 mycroft }
300 1.1 mycroft
301 1.1 mycroft int
302 1.15 blymn noecho(void)
303 1.1 mycroft {
304 1.9 phil /* Check if we need to restart ... */
305 1.22 jdc if (__endwin)
306 1.23 jdc __restartwin();
307 1.9 phil
308 1.1 mycroft __echoit = 0;
309 1.17 mycroft return (OK);
310 1.1 mycroft }
311 1.1 mycroft
312 1.1 mycroft int
313 1.15 blymn nl(void)
314 1.1 mycroft {
315 1.9 phil /* Check if we need to restart ... */
316 1.22 jdc if (__endwin)
317 1.23 jdc __restartwin();
318 1.9 phil
319 1.1 mycroft rawt.c_iflag |= ICRNL;
320 1.1 mycroft rawt.c_oflag |= ONLCR;
321 1.2 cgd cbreakt.c_iflag |= ICRNL;
322 1.2 cgd cbreakt.c_oflag |= ONLCR;
323 1.2 cgd __baset.c_iflag |= ICRNL;
324 1.2 cgd __baset.c_oflag |= ONLCR;
325 1.1 mycroft
326 1.1 mycroft __pfast = __rawmode;
327 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
328 1.6 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
329 1.1 mycroft }
330 1.1 mycroft
331 1.1 mycroft int
332 1.15 blymn nonl(void)
333 1.1 mycroft {
334 1.9 phil /* Check if we need to restart ... */
335 1.22 jdc if (__endwin)
336 1.23 jdc __restartwin();
337 1.9 phil
338 1.1 mycroft rawt.c_iflag &= ~ICRNL;
339 1.1 mycroft rawt.c_oflag &= ~ONLCR;
340 1.2 cgd cbreakt.c_iflag &= ~ICRNL;
341 1.2 cgd cbreakt.c_oflag &= ~ONLCR;
342 1.2 cgd __baset.c_iflag &= ~ICRNL;
343 1.2 cgd __baset.c_oflag &= ~ONLCR;
344 1.1 mycroft
345 1.1 mycroft __pfast = 1;
346 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
347 1.6 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
348 1.2 cgd }
349 1.2 cgd
350 1.14 jdc int
351 1.15 blymn intrflush(WINDOW *win, bool bf) /*ARGSUSED*/
352 1.14 jdc {
353 1.14 jdc /* Check if we need to restart ... */
354 1.22 jdc if (__endwin)
355 1.23 jdc __restartwin();
356 1.14 jdc
357 1.14 jdc if (bf) {
358 1.14 jdc rawt.c_lflag &= ~NOFLSH;
359 1.14 jdc cbreakt.c_lflag &= ~NOFLSH;
360 1.14 jdc __baset.c_lflag &= ~NOFLSH;
361 1.14 jdc } else {
362 1.14 jdc rawt.c_lflag |= NOFLSH;
363 1.14 jdc cbreakt.c_lflag |= NOFLSH;
364 1.14 jdc __baset.c_lflag |= NOFLSH;
365 1.14 jdc }
366 1.14 jdc
367 1.14 jdc __pfast = 1;
368 1.14 jdc return (tcsetattr(STDIN_FILENO, __tcaction ?
369 1.14 jdc TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
370 1.14 jdc }
371 1.14 jdc
372 1.2 cgd void
373 1.15 blymn __startwin(void)
374 1.2 cgd {
375 1.6 cgd static char *stdbuf;
376 1.6 cgd static size_t len;
377 1.6 cgd
378 1.11 mrg (void) fflush(stdout);
379 1.6 cgd
380 1.6 cgd /*
381 1.6 cgd * Some C libraries default to a 1K buffer when talking to a tty.
382 1.6 cgd * With a larger screen, especially across a network, we'd like
383 1.6 cgd * to get it to all flush in a single write. Make it twice as big
384 1.6 cgd * as just the characters (so that we have room for cursor motions
385 1.11 mrg * and attribute information) but no more than 8K.
386 1.6 cgd */
387 1.6 cgd if (stdbuf == NULL) {
388 1.6 cgd if ((len = LINES * COLS * 2) > 8192)
389 1.6 cgd len = 8192;
390 1.6 cgd if ((stdbuf = malloc(len)) == NULL)
391 1.6 cgd len = 0;
392 1.6 cgd }
393 1.11 mrg (void) setvbuf(stdout, stdbuf, _IOFBF, len);
394 1.2 cgd
395 1.24 jdc tputs(__tc_ti, 0, __cputchar);
396 1.24 jdc tputs(__tc_vs, 0, __cputchar);
397 1.20 jdc if (curscr->flags & __KEYPAD)
398 1.24 jdc tputs(__tc_ks, 0, __cputchar);
399 1.22 jdc __endwin = 0;
400 1.1 mycroft }
401 1.1 mycroft
402 1.1 mycroft int
403 1.15 blymn endwin(void)
404 1.1 mycroft {
405 1.8 phil return __stopwin();
406 1.11 mrg }
407 1.11 mrg
408 1.13 blymn bool
409 1.15 blymn isendwin(void)
410 1.11 mrg {
411 1.13 blymn return (__endwin ? TRUE : FALSE);
412 1.11 mrg }
413 1.11 mrg
414 1.11 mrg int
415 1.15 blymn flushinp(void)
416 1.11 mrg {
417 1.13 blymn (void) fpurge(stdin);
418 1.11 mrg return (OK);
419 1.14 jdc }
420 1.14 jdc
421 1.1 mycroft /*
422 1.1 mycroft * The following routines, savetty and resetty are completely useless and
423 1.1 mycroft * are left in only as stubs. If people actually use them they will almost
424 1.1 mycroft * certainly screw up the state of the world.
425 1.1 mycroft */
426 1.1 mycroft static struct termios savedtty;
427 1.1 mycroft int
428 1.15 blymn savetty(void)
429 1.1 mycroft {
430 1.6 cgd return (tcgetattr(STDIN_FILENO, &savedtty) ? ERR : OK);
431 1.1 mycroft }
432 1.1 mycroft
433 1.1 mycroft int
434 1.15 blymn resetty(void)
435 1.1 mycroft {
436 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
437 1.6 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, &savedtty) ? ERR : OK);
438 1.19 blymn }
439 1.19 blymn
440 1.19 blymn /*
441 1.19 blymn * erasechar --
442 1.19 blymn * Return the character of the erase key.
443 1.19 blymn *
444 1.19 blymn */
445 1.19 blymn char
446 1.19 blymn erasechar(void)
447 1.19 blymn {
448 1.19 blymn return __baset.c_cc[VERASE];
449 1.19 blymn }
450 1.19 blymn
451 1.19 blymn /*
452 1.19 blymn * killchar --
453 1.19 blymn * Return the character of the kill key.
454 1.19 blymn */
455 1.19 blymn char
456 1.19 blymn killchar(void)
457 1.19 blymn {
458 1.19 blymn return __baset.c_cc[VKILL];
459 1.1 mycroft }
460