tty.c revision 1.18 1 1.18 mycroft /* $NetBSD: tty.c,v 1.18 2000/04/27 17:50:01 mycroft 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.18 mycroft __RCSID("$NetBSD: tty.c,v 1.18 2000/04/27 17:50:01 mycroft 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.2 cgd struct termios __orig_termios, __baset;
70 1.11 mrg int __endwin;
71 1.2 cgd static struct termios cbreakt, rawt, *curt;
72 1.1 mycroft static int useraw;
73 1.11 mrg static int ovmin = 1;
74 1.11 mrg 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.1 mycroft
99 1.11 mrg GT = 0; /* historical. was used before we wired OXTABS
100 1.11 mrg * off */
101 1.2 cgd NONL = (__baset.c_oflag & ONLCR) == 0;
102 1.1 mycroft
103 1.2 cgd /*
104 1.2 cgd * XXX
105 1.2 cgd * System V and SMI systems overload VMIN and VTIME, such that
106 1.2 cgd * VMIN is the same as the VEOF element, and VTIME is the same
107 1.2 cgd * as the VEOL element. This means that, if VEOF was ^D, the
108 1.2 cgd * default VMIN is 4. Majorly stupid.
109 1.2 cgd */
110 1.2 cgd cbreakt = __baset;
111 1.18 mycroft cbreakt.c_lflag &= ~(ECHO | ECHONL | ICANON);
112 1.2 cgd cbreakt.c_cc[VMIN] = 1;
113 1.2 cgd cbreakt.c_cc[VTIME] = 0;
114 1.2 cgd
115 1.2 cgd rawt = cbreakt;
116 1.11 mrg rawt.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | INLCR | IGNCR | ICRNL | IXON);
117 1.2 cgd rawt.c_oflag &= ~OPOST;
118 1.18 mycroft rawt.c_lflag &= ~(ISIG | IEXTEN);
119 1.2 cgd
120 1.2 cgd /*
121 1.2 cgd * In general, curses should leave hardware-related settings alone.
122 1.2 cgd * This includes parity and word size. Older versions set the tty
123 1.2 cgd * to 8 bits, no parity in raw(), but this is considered to be an
124 1.2 cgd * artifact of the old tty interface. If it's desired to change
125 1.2 cgd * parity and word size, the TCSASOFT bit has to be removed from the
126 1.2 cgd * calls that switch to/from "raw" mode.
127 1.2 cgd */
128 1.2 cgd if (!__tcaction) {
129 1.2 cgd rawt.c_iflag &= ~ISTRIP;
130 1.11 mrg rawt.c_cflag &= ~(CSIZE | PARENB);
131 1.2 cgd rawt.c_cflag |= CS8;
132 1.2 cgd }
133 1.1 mycroft
134 1.2 cgd curt = &__baset;
135 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
136 1.2 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
137 1.1 mycroft }
138 1.1 mycroft
139 1.1 mycroft int
140 1.15 blymn raw(void)
141 1.1 mycroft {
142 1.9 phil /* Check if we need to restart ... */
143 1.9 phil if (__endwin) {
144 1.9 phil __endwin = 0;
145 1.9 phil __restartwin();
146 1.9 phil }
147 1.11 mrg
148 1.1 mycroft useraw = __pfast = __rawmode = 1;
149 1.2 cgd curt = &rawt;
150 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
151 1.6 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
152 1.1 mycroft }
153 1.1 mycroft
154 1.1 mycroft int
155 1.15 blymn noraw(void)
156 1.1 mycroft {
157 1.9 phil /* Check if we need to restart ... */
158 1.9 phil if (__endwin) {
159 1.9 phil __endwin = 0;
160 1.9 phil __restartwin();
161 1.9 phil }
162 1.11 mrg
163 1.1 mycroft useraw = __pfast = __rawmode = 0;
164 1.2 cgd curt = &__baset;
165 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
166 1.6 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
167 1.1 mycroft }
168 1.1 mycroft
169 1.1 mycroft int
170 1.15 blymn cbreak(void)
171 1.1 mycroft {
172 1.9 phil /* Check if we need to restart ... */
173 1.9 phil if (__endwin) {
174 1.9 phil __endwin = 0;
175 1.9 phil __restartwin();
176 1.9 phil }
177 1.11 mrg
178 1.1 mycroft __rawmode = 1;
179 1.2 cgd curt = useraw ? &rawt : &cbreakt;
180 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
181 1.6 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
182 1.1 mycroft }
183 1.1 mycroft
184 1.1 mycroft int
185 1.15 blymn nocbreak(void)
186 1.1 mycroft {
187 1.9 phil /* Check if we need to restart ... */
188 1.9 phil if (__endwin) {
189 1.9 phil __endwin = 0;
190 1.9 phil __restartwin();
191 1.9 phil }
192 1.1 mycroft
193 1.1 mycroft __rawmode = 0;
194 1.2 cgd curt = useraw ? &rawt : &__baset;
195 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
196 1.6 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
197 1.1 mycroft }
198 1.11 mrg
199 1.11 mrg int
200 1.15 blymn __delay(void)
201 1.11 mrg {
202 1.11 mrg /* Check if we need to restart ... */
203 1.11 mrg if (__endwin) {
204 1.11 mrg __endwin = 0;
205 1.11 mrg __restartwin();
206 1.11 mrg }
207 1.11 mrg
208 1.11 mrg rawt.c_cc[VMIN] = 1;
209 1.11 mrg rawt.c_cc[VTIME] = 0;
210 1.11 mrg cbreakt.c_cc[VMIN] = 1;
211 1.11 mrg cbreakt.c_cc[VTIME] = 0;
212 1.11 mrg __baset.c_cc[VMIN] = 1;
213 1.11 mrg __baset.c_cc[VTIME] = 0;
214 1.11 mrg
215 1.11 mrg return (tcsetattr(STDIN_FILENO, __tcaction ?
216 1.11 mrg TCSASOFT : TCSANOW, curt) ? ERR : OK);
217 1.11 mrg }
218 1.11 mrg
219 1.11 mrg int
220 1.15 blymn __nodelay(void)
221 1.11 mrg {
222 1.11 mrg /* Check if we need to restart ... */
223 1.11 mrg if (__endwin) {
224 1.11 mrg __endwin = 0;
225 1.11 mrg __restartwin();
226 1.11 mrg }
227 1.11 mrg
228 1.11 mrg rawt.c_cc[VMIN] = 0;
229 1.11 mrg rawt.c_cc[VTIME] = 0;
230 1.11 mrg cbreakt.c_cc[VMIN] = 0;
231 1.11 mrg cbreakt.c_cc[VTIME] = 0;
232 1.11 mrg __baset.c_cc[VMIN] = 0;
233 1.11 mrg __baset.c_cc[VTIME] = 0;
234 1.11 mrg
235 1.11 mrg return (tcsetattr(STDIN_FILENO, __tcaction ?
236 1.11 mrg TCSASOFT : TCSANOW, curt) ? ERR : OK);
237 1.11 mrg }
238 1.11 mrg
239 1.11 mrg void
240 1.15 blymn __save_termios(void)
241 1.11 mrg {
242 1.11 mrg /* Check if we need to restart ... */
243 1.11 mrg if (__endwin) {
244 1.11 mrg __endwin = 0;
245 1.11 mrg __restartwin();
246 1.11 mrg }
247 1.11 mrg
248 1.11 mrg ovmin = cbreakt.c_cc[VMIN];
249 1.11 mrg ovtime = cbreakt.c_cc[VTIME];
250 1.11 mrg }
251 1.11 mrg
252 1.11 mrg void
253 1.15 blymn __restore_termios(void)
254 1.11 mrg {
255 1.11 mrg /* Check if we need to restart ... */
256 1.11 mrg if (__endwin) {
257 1.11 mrg __endwin = 0;
258 1.11 mrg __restartwin();
259 1.11 mrg }
260 1.11 mrg
261 1.11 mrg rawt.c_cc[VMIN] = ovmin;
262 1.11 mrg rawt.c_cc[VTIME] = ovtime;
263 1.11 mrg cbreakt.c_cc[VMIN] = ovmin;
264 1.11 mrg cbreakt.c_cc[VTIME] = ovtime;
265 1.11 mrg __baset.c_cc[VMIN] = ovmin;
266 1.11 mrg __baset.c_cc[VTIME] = ovtime;
267 1.11 mrg }
268 1.11 mrg
269 1.11 mrg int
270 1.15 blymn __timeout(int delay)
271 1.11 mrg {
272 1.11 mrg /* Check if we need to restart ... */
273 1.11 mrg if (__endwin) {
274 1.11 mrg __endwin = 0;
275 1.11 mrg __restartwin();
276 1.11 mrg }
277 1.11 mrg
278 1.11 mrg ovmin = cbreakt.c_cc[VMIN];
279 1.11 mrg ovtime = cbreakt.c_cc[VTIME];
280 1.11 mrg rawt.c_cc[VMIN] = 0;
281 1.11 mrg rawt.c_cc[VTIME] = delay;
282 1.11 mrg cbreakt.c_cc[VMIN] = 0;
283 1.11 mrg cbreakt.c_cc[VTIME] = delay;
284 1.11 mrg __baset.c_cc[VMIN] = 0;
285 1.11 mrg __baset.c_cc[VTIME] = delay;
286 1.11 mrg
287 1.11 mrg return (tcsetattr(STDIN_FILENO, __tcaction ?
288 1.11 mrg TCSASOFT | TCSANOW : TCSANOW, curt) ? ERR : OK);
289 1.11 mrg }
290 1.11 mrg
291 1.11 mrg int
292 1.15 blymn __notimeout(void)
293 1.11 mrg {
294 1.11 mrg /* Check if we need to restart ... */
295 1.11 mrg if (__endwin) {
296 1.11 mrg __endwin = 0;
297 1.11 mrg __restartwin();
298 1.11 mrg }
299 1.11 mrg
300 1.11 mrg rawt.c_cc[VMIN] = 1;
301 1.11 mrg rawt.c_cc[VTIME] = 0;
302 1.11 mrg cbreakt.c_cc[VMIN] = 1;
303 1.11 mrg cbreakt.c_cc[VTIME] = 0;
304 1.11 mrg __baset.c_cc[VMIN] = 1;
305 1.11 mrg __baset.c_cc[VTIME] = 0;
306 1.11 mrg
307 1.11 mrg return (tcsetattr(STDIN_FILENO, __tcaction ?
308 1.11 mrg TCSASOFT | TCSANOW : TCSANOW, curt) ? ERR : OK);
309 1.11 mrg }
310 1.11 mrg
311 1.1 mycroft int
312 1.15 blymn echo(void)
313 1.1 mycroft {
314 1.9 phil /* Check if we need to restart ... */
315 1.9 phil if (__endwin) {
316 1.9 phil __endwin = 0;
317 1.9 phil __restartwin();
318 1.9 phil }
319 1.9 phil
320 1.1 mycroft __echoit = 1;
321 1.17 mycroft return (OK);
322 1.1 mycroft }
323 1.1 mycroft
324 1.1 mycroft int
325 1.15 blymn noecho(void)
326 1.1 mycroft {
327 1.9 phil /* Check if we need to restart ... */
328 1.9 phil if (__endwin) {
329 1.9 phil __endwin = 0;
330 1.9 phil __restartwin();
331 1.9 phil }
332 1.9 phil
333 1.1 mycroft __echoit = 0;
334 1.17 mycroft return (OK);
335 1.1 mycroft }
336 1.1 mycroft
337 1.1 mycroft int
338 1.15 blymn nl(void)
339 1.1 mycroft {
340 1.9 phil /* Check if we need to restart ... */
341 1.9 phil if (__endwin) {
342 1.9 phil __endwin = 0;
343 1.9 phil __restartwin();
344 1.9 phil }
345 1.9 phil
346 1.1 mycroft rawt.c_iflag |= ICRNL;
347 1.1 mycroft rawt.c_oflag |= ONLCR;
348 1.2 cgd cbreakt.c_iflag |= ICRNL;
349 1.2 cgd cbreakt.c_oflag |= ONLCR;
350 1.2 cgd __baset.c_iflag |= ICRNL;
351 1.2 cgd __baset.c_oflag |= ONLCR;
352 1.1 mycroft
353 1.1 mycroft __pfast = __rawmode;
354 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
355 1.6 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
356 1.1 mycroft }
357 1.1 mycroft
358 1.1 mycroft int
359 1.15 blymn nonl(void)
360 1.1 mycroft {
361 1.9 phil /* Check if we need to restart ... */
362 1.9 phil if (__endwin) {
363 1.9 phil __endwin = 0;
364 1.9 phil __restartwin();
365 1.9 phil }
366 1.9 phil
367 1.1 mycroft rawt.c_iflag &= ~ICRNL;
368 1.1 mycroft rawt.c_oflag &= ~ONLCR;
369 1.2 cgd cbreakt.c_iflag &= ~ICRNL;
370 1.2 cgd cbreakt.c_oflag &= ~ONLCR;
371 1.2 cgd __baset.c_iflag &= ~ICRNL;
372 1.2 cgd __baset.c_oflag &= ~ONLCR;
373 1.1 mycroft
374 1.1 mycroft __pfast = 1;
375 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
376 1.6 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
377 1.2 cgd }
378 1.2 cgd
379 1.14 jdc int
380 1.15 blymn intrflush(WINDOW *win, bool bf) /*ARGSUSED*/
381 1.14 jdc {
382 1.14 jdc /* Check if we need to restart ... */
383 1.14 jdc if (__endwin) {
384 1.14 jdc __endwin = 0;
385 1.14 jdc __restartwin();
386 1.14 jdc }
387 1.14 jdc
388 1.14 jdc if (bf) {
389 1.14 jdc rawt.c_lflag &= ~NOFLSH;
390 1.14 jdc cbreakt.c_lflag &= ~NOFLSH;
391 1.14 jdc __baset.c_lflag &= ~NOFLSH;
392 1.14 jdc } else {
393 1.14 jdc rawt.c_lflag |= NOFLSH;
394 1.14 jdc cbreakt.c_lflag |= NOFLSH;
395 1.14 jdc __baset.c_lflag |= NOFLSH;
396 1.14 jdc }
397 1.14 jdc
398 1.14 jdc __pfast = 1;
399 1.14 jdc return (tcsetattr(STDIN_FILENO, __tcaction ?
400 1.14 jdc TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
401 1.14 jdc }
402 1.14 jdc
403 1.2 cgd void
404 1.15 blymn __startwin(void)
405 1.2 cgd {
406 1.6 cgd static char *stdbuf;
407 1.6 cgd static size_t len;
408 1.6 cgd
409 1.11 mrg (void) fflush(stdout);
410 1.6 cgd
411 1.6 cgd /*
412 1.6 cgd * Some C libraries default to a 1K buffer when talking to a tty.
413 1.6 cgd * With a larger screen, especially across a network, we'd like
414 1.6 cgd * to get it to all flush in a single write. Make it twice as big
415 1.6 cgd * as just the characters (so that we have room for cursor motions
416 1.11 mrg * and attribute information) but no more than 8K.
417 1.6 cgd */
418 1.6 cgd if (stdbuf == NULL) {
419 1.6 cgd if ((len = LINES * COLS * 2) > 8192)
420 1.6 cgd len = 8192;
421 1.6 cgd if ((stdbuf = malloc(len)) == NULL)
422 1.6 cgd len = 0;
423 1.6 cgd }
424 1.11 mrg (void) setvbuf(stdout, stdbuf, _IOFBF, len);
425 1.2 cgd
426 1.2 cgd tputs(TI, 0, __cputchar);
427 1.2 cgd tputs(VS, 0, __cputchar);
428 1.11 mrg tputs(KS, 0, __cputchar);
429 1.1 mycroft }
430 1.1 mycroft
431 1.1 mycroft int
432 1.15 blymn endwin(void)
433 1.1 mycroft {
434 1.8 phil __endwin = 1;
435 1.8 phil return __stopwin();
436 1.11 mrg }
437 1.11 mrg
438 1.13 blymn bool
439 1.15 blymn isendwin(void)
440 1.11 mrg {
441 1.13 blymn return (__endwin ? TRUE : FALSE);
442 1.11 mrg }
443 1.11 mrg
444 1.11 mrg int
445 1.15 blymn flushinp(void)
446 1.11 mrg {
447 1.13 blymn (void) fpurge(stdin);
448 1.11 mrg return (OK);
449 1.14 jdc }
450 1.14 jdc
451 1.14 jdc int
452 1.15 blymn def_shell_mode(void)
453 1.14 jdc {
454 1.14 jdc return (tcgetattr(STDIN_FILENO, &__orig_termios) ? ERR : OK);
455 1.14 jdc }
456 1.14 jdc
457 1.14 jdc int
458 1.15 blymn reset_shell_mode(void)
459 1.14 jdc {
460 1.14 jdc return (tcsetattr(STDIN_FILENO, __tcaction ?
461 1.14 jdc TCSASOFT | TCSADRAIN : TCSADRAIN, &__orig_termios) ? ERR : OK);
462 1.1 mycroft }
463 1.1 mycroft
464 1.1 mycroft /*
465 1.1 mycroft * The following routines, savetty and resetty are completely useless and
466 1.1 mycroft * are left in only as stubs. If people actually use them they will almost
467 1.1 mycroft * certainly screw up the state of the world.
468 1.1 mycroft */
469 1.1 mycroft static struct termios savedtty;
470 1.1 mycroft int
471 1.15 blymn savetty(void)
472 1.1 mycroft {
473 1.6 cgd return (tcgetattr(STDIN_FILENO, &savedtty) ? ERR : OK);
474 1.1 mycroft }
475 1.1 mycroft
476 1.1 mycroft int
477 1.15 blymn resetty(void)
478 1.1 mycroft {
479 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
480 1.6 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, &savedtty) ? ERR : OK);
481 1.1 mycroft }
482