tty.c revision 1.35 1 /* $NetBSD: tty.c,v 1.35 2003/08/07 16:44:25 agc Exp $ */
2
3 /*-
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)tty.c 8.6 (Berkeley) 1/10/95";
36 #else
37 __RCSID("$NetBSD: tty.c,v 1.35 2003/08/07 16:44:25 agc Exp $");
38 #endif
39 #endif /* not lint */
40
41 #include <sys/types.h>
42
43 #include <stdlib.h>
44 #include <termios.h>
45 #include <unistd.h>
46 #include <sys/fcntl.h>
47 #include <sys/ioctl.h>
48
49 #include "curses.h"
50 #include "curses_private.h"
51
52 /*
53 * In general, curses should leave tty hardware settings alone (speed, parity,
54 * word size). This is most easily done in BSD by using TCSASOFT on all
55 * tcsetattr calls. On other systems, it would be better to get and restore
56 * those attributes at each change, or at least when stopped and restarted.
57 * See also the comments in getterm().
58 */
59 #ifdef TCSASOFT
60 int __tcaction = 1; /* Ignore hardware settings. */
61 #else
62 int __tcaction = 0;
63 #endif
64
65 #ifndef OXTABS
66 #ifdef XTABS /* SMI uses XTABS. */
67 #define OXTABS XTABS
68 #else
69 #define OXTABS 0
70 #endif
71 #endif
72
73 /*
74 * baudrate --
75 * Return the current baudrate
76 */
77 int
78 baudrate(void)
79 {
80 if (_cursesi_screen->notty == TRUE)
81 return 0;
82
83 return cfgetospeed(&_cursesi_screen->baset);
84 }
85
86 /*
87 * gettmode --
88 * Do terminal type initialization.
89 */
90 int
91 gettmode(void)
92 {
93 if (_cursesi_gettmode(_cursesi_screen) == ERR)
94 return ERR;
95
96 __GT = _cursesi_screen->GT;
97 __NONL = _cursesi_screen->NONL;
98 return OK;
99 }
100
101 /*
102 * _cursesi_gettmode --
103 * Do the terminal type initialisation for the tty attached to the
104 * given screen.
105 */
106 int
107 _cursesi_gettmode(SCREEN *screen)
108 {
109 screen->useraw = 0;
110
111 if (tcgetattr(fileno(screen->infd), &screen->orig_termios)) {
112 /* if the input fd is not a tty try the output */
113 if (tcgetattr(fileno(screen->infd), &screen->orig_termios)) {
114 /* not a tty ... we will disable tty related stuff */
115 screen->notty = TRUE;
116 __GT = 0;
117 __NONL = 0;
118 return (OK);
119 }
120 }
121
122 screen->baset = screen->orig_termios;
123 screen->baset.c_oflag &= ~OXTABS;
124
125 screen->GT = 0; /* historical. was used before we wired OXTABS off */
126 screen->NONL = (screen->baset.c_oflag & ONLCR) == 0;
127
128 /*
129 * XXX
130 * System V and SMI systems overload VMIN and VTIME, such that
131 * VMIN is the same as the VEOF element, and VTIME is the same
132 * as the VEOL element. This means that, if VEOF was ^D, the
133 * default VMIN is 4. Majorly stupid.
134 */
135 screen->cbreakt = screen->baset;
136 screen->cbreakt.c_lflag &= ~(ECHO | ECHONL | ICANON);
137 screen->cbreakt.c_cc[VMIN] = 1;
138 screen->cbreakt.c_cc[VTIME] = 0;
139
140 screen->rawt = screen->cbreakt;
141 screen->rawt.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | INLCR | IGNCR |
142 ICRNL | IXON);
143 screen->rawt.c_oflag &= ~OPOST;
144 screen->rawt.c_lflag &= ~(ISIG | IEXTEN);
145
146 /*
147 * In general, curses should leave hardware-related settings alone.
148 * This includes parity and word size. Older versions set the tty
149 * to 8 bits, no parity in raw(), but this is considered to be an
150 * artifact of the old tty interface. If it's desired to change
151 * parity and word size, the TCSASOFT bit has to be removed from the
152 * calls that switch to/from "raw" mode.
153 */
154 if (!__tcaction) {
155 screen->rawt.c_iflag &= ~ISTRIP;
156 screen->rawt.c_cflag &= ~(CSIZE | PARENB);
157 screen->rawt.c_cflag |= CS8;
158 }
159
160 screen->curt = &screen->baset;
161 return (tcsetattr(fileno(screen->infd), __tcaction ?
162 TCSASOFT | TCSADRAIN : TCSADRAIN, screen->curt) ? ERR : OK);
163 }
164
165 int
166 raw(void)
167 {
168 #ifdef DEBUG
169 __CTRACE("raw()\n");
170 #endif
171 /* Check if we need to restart ... */
172 if (_cursesi_screen->endwin)
173 __restartwin();
174
175 _cursesi_screen->useraw = __pfast = __rawmode = 1;
176 _cursesi_screen->curt = &_cursesi_screen->rawt;
177 if (_cursesi_screen->notty == TRUE)
178 return OK;
179 return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
180 TCSASOFT | TCSADRAIN : TCSADRAIN,
181 _cursesi_screen->curt) ? ERR : OK);
182 }
183
184 int
185 noraw(void)
186 {
187 #ifdef DEBUG
188 __CTRACE("noraw()\n");
189 #endif
190 /* Check if we need to restart ... */
191 if (_cursesi_screen->endwin)
192 __restartwin();
193
194 _cursesi_screen->useraw = __pfast = __rawmode = 0;
195 if (_cursesi_screen->notty == TRUE)
196 return OK;
197 _cursesi_screen->curt = &_cursesi_screen->baset;
198 return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
199 TCSASOFT | TCSADRAIN : TCSADRAIN,
200 _cursesi_screen->curt) ? ERR : OK);
201 }
202
203 int
204 cbreak(void)
205 {
206 #ifdef DEBUG
207 __CTRACE("cbreak()\n");
208 #endif
209 /* Check if we need to restart ... */
210 if (_cursesi_screen->endwin)
211 __restartwin();
212
213 __rawmode = 1;
214 if (_cursesi_screen->notty == TRUE)
215 return OK;
216 _cursesi_screen->curt = _cursesi_screen->useraw ?
217 &_cursesi_screen->rawt : &_cursesi_screen->cbreakt;
218 return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
219 TCSASOFT | TCSADRAIN : TCSADRAIN,
220 _cursesi_screen->curt) ? ERR : OK);
221 }
222
223 int
224 nocbreak(void)
225 {
226 #ifdef DEBUG
227 __CTRACE("nocbreak()\n");
228 #endif
229 /* Check if we need to restart ... */
230 if (_cursesi_screen->endwin)
231 __restartwin();
232
233 __rawmode = 0;
234 if (_cursesi_screen->notty == TRUE)
235 return OK;
236 /* if we were in halfdelay mode then nuke the timeout */
237 if ((_cursesi_screen->half_delay == TRUE) &&
238 (__notimeout() == ERR))
239 return ERR;
240
241 _cursesi_screen->half_delay = FALSE;
242 _cursesi_screen->curt = _cursesi_screen->useraw ?
243 &_cursesi_screen->rawt : &_cursesi_screen->baset;
244 return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
245 TCSASOFT | TCSADRAIN : TCSADRAIN,
246 _cursesi_screen->curt) ? ERR : OK);
247 }
248
249 /*
250 * halfdelay --
251 * Put the terminal into cbreak mode with the specified timeout.
252 *
253 */
254 int
255 halfdelay(int duration)
256 {
257 if ((duration < 1) || (duration > 255))
258 return ERR;
259
260 if (cbreak() == ERR)
261 return ERR;
262
263 if (__timeout(duration) == ERR)
264 return ERR;
265
266 _cursesi_screen->half_delay = TRUE;
267 return OK;
268 }
269
270 int
271 __delay(void)
272 {
273 /* Check if we need to restart ... */
274 if (_cursesi_screen->endwin)
275 __restartwin();
276
277 if (_cursesi_screen->notty == TRUE)
278 return OK;
279 _cursesi_screen->rawt.c_cc[VMIN] = 1;
280 _cursesi_screen->rawt.c_cc[VTIME] = 0;
281 _cursesi_screen->cbreakt.c_cc[VMIN] = 1;
282 _cursesi_screen->cbreakt.c_cc[VTIME] = 0;
283 _cursesi_screen->baset.c_cc[VMIN] = 1;
284 _cursesi_screen->baset.c_cc[VTIME] = 0;
285
286 return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
287 TCSASOFT : TCSANOW, _cursesi_screen->curt) ? ERR : OK);
288 }
289
290 int
291 __nodelay(void)
292 {
293 /* Check if we need to restart ... */
294 if (_cursesi_screen->endwin)
295 __restartwin();
296
297 if (_cursesi_screen->notty == TRUE)
298 return OK;
299 _cursesi_screen->rawt.c_cc[VMIN] = 0;
300 _cursesi_screen->rawt.c_cc[VTIME] = 0;
301 _cursesi_screen->cbreakt.c_cc[VMIN] = 0;
302 _cursesi_screen->cbreakt.c_cc[VTIME] = 0;
303 _cursesi_screen->baset.c_cc[VMIN] = 0;
304 _cursesi_screen->baset.c_cc[VTIME] = 0;
305
306 return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
307 TCSASOFT : TCSANOW, _cursesi_screen->curt) ? ERR : OK);
308 }
309
310 void
311 __save_termios(void)
312 {
313 /* Check if we need to restart ... */
314 if (_cursesi_screen->endwin)
315 __restartwin();
316
317 if (_cursesi_screen->notty == TRUE)
318 return;
319 _cursesi_screen->ovmin = _cursesi_screen->cbreakt.c_cc[VMIN];
320 _cursesi_screen->ovtime = _cursesi_screen->cbreakt.c_cc[VTIME];
321 }
322
323 void
324 __restore_termios(void)
325 {
326 /* Check if we need to restart ... */
327 if (_cursesi_screen->endwin)
328 __restartwin();
329
330 if (_cursesi_screen->notty == TRUE)
331 return;
332 _cursesi_screen->rawt.c_cc[VMIN] = _cursesi_screen->ovmin;
333 _cursesi_screen->rawt.c_cc[VTIME] = _cursesi_screen->ovtime;
334 _cursesi_screen->cbreakt.c_cc[VMIN] = _cursesi_screen->ovmin;
335 _cursesi_screen->cbreakt.c_cc[VTIME] = _cursesi_screen->ovtime;
336 _cursesi_screen->baset.c_cc[VMIN] = _cursesi_screen->ovmin;
337 _cursesi_screen->baset.c_cc[VTIME] = _cursesi_screen->ovtime;
338 }
339
340 int
341 __timeout(int delay)
342 {
343 /* Check if we need to restart ... */
344 if (_cursesi_screen->endwin)
345 __restartwin();
346
347 if (_cursesi_screen->notty == TRUE)
348 return OK;
349 _cursesi_screen->ovmin = _cursesi_screen->cbreakt.c_cc[VMIN];
350 _cursesi_screen->ovtime = _cursesi_screen->cbreakt.c_cc[VTIME];
351 _cursesi_screen->rawt.c_cc[VMIN] = 0;
352 _cursesi_screen->rawt.c_cc[VTIME] = delay;
353 _cursesi_screen->cbreakt.c_cc[VMIN] = 0;
354 _cursesi_screen->cbreakt.c_cc[VTIME] = delay;
355 _cursesi_screen->baset.c_cc[VMIN] = 0;
356 _cursesi_screen->baset.c_cc[VTIME] = delay;
357
358 return (tcsetattr(fileno(_cursesi_screen->infd),
359 __tcaction ? TCSASOFT | TCSANOW : TCSANOW,
360 _cursesi_screen->curt) ? ERR : OK);
361 }
362
363 int
364 __notimeout(void)
365 {
366 /* Check if we need to restart ... */
367 if (_cursesi_screen->endwin)
368 __restartwin();
369
370 if (_cursesi_screen->notty == TRUE)
371 return OK;
372 _cursesi_screen->rawt.c_cc[VMIN] = 1;
373 _cursesi_screen->rawt.c_cc[VTIME] = 0;
374 _cursesi_screen->cbreakt.c_cc[VMIN] = 1;
375 _cursesi_screen->cbreakt.c_cc[VTIME] = 0;
376 _cursesi_screen->baset.c_cc[VMIN] = 1;
377 _cursesi_screen->baset.c_cc[VTIME] = 0;
378
379 return (tcsetattr(fileno(_cursesi_screen->infd),
380 __tcaction ? TCSASOFT | TCSANOW : TCSANOW,
381 _cursesi_screen->curt) ? ERR : OK);
382 }
383
384 int
385 echo(void)
386 {
387 #ifdef DEBUG
388 __CTRACE("echo()\n");
389 #endif
390 /* Check if we need to restart ... */
391 if (_cursesi_screen->endwin)
392 __restartwin();
393
394 __echoit = 1;
395 return (OK);
396 }
397
398 int
399 noecho(void)
400 {
401 #ifdef DEBUG
402 __CTRACE("noecho()\n");
403 #endif
404 /* Check if we need to restart ... */
405 if (_cursesi_screen->endwin)
406 __restartwin();
407
408 __echoit = 0;
409 return (OK);
410 }
411
412 int
413 nl(void)
414 {
415 #ifdef DEBUG
416 __CTRACE("nl()\n");
417 #endif
418 /* Check if we need to restart ... */
419 if (_cursesi_screen->endwin)
420 __restartwin();
421
422 if (_cursesi_screen->notty == TRUE)
423 return OK;
424 _cursesi_screen->rawt.c_iflag |= ICRNL;
425 _cursesi_screen->rawt.c_oflag |= ONLCR;
426 _cursesi_screen->cbreakt.c_iflag |= ICRNL;
427 _cursesi_screen->cbreakt.c_oflag |= ONLCR;
428 _cursesi_screen->baset.c_iflag |= ICRNL;
429 _cursesi_screen->baset.c_oflag |= ONLCR;
430
431 _cursesi_screen->nl = 1;
432 _cursesi_screen->pfast = _cursesi_screen->rawmode;
433 return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
434 TCSASOFT | TCSADRAIN : TCSADRAIN,
435 _cursesi_screen->curt) ? ERR : OK);
436 }
437
438 int
439 nonl(void)
440 {
441 #ifdef DEBUG
442 __CTRACE("nonl()\n");
443 #endif
444 /* Check if we need to restart ... */
445 if (_cursesi_screen->endwin)
446 __restartwin();
447
448 if (_cursesi_screen->notty == TRUE)
449 return OK;
450 _cursesi_screen->rawt.c_iflag &= ~ICRNL;
451 _cursesi_screen->rawt.c_oflag &= ~ONLCR;
452 _cursesi_screen->cbreakt.c_iflag &= ~ICRNL;
453 _cursesi_screen->cbreakt.c_oflag &= ~ONLCR;
454 _cursesi_screen->baset.c_iflag &= ~ICRNL;
455 _cursesi_screen->baset.c_oflag &= ~ONLCR;
456
457 _cursesi_screen->nl = 0;
458 __pfast = 1;
459 return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
460 TCSASOFT | TCSADRAIN : TCSADRAIN,
461 _cursesi_screen->curt) ? ERR : OK);
462 }
463
464 #ifndef _CURSES_USE_MACROS
465 void
466 noqiflush(void)
467 {
468 (void) intrflush(stdscr, FALSE);
469 }
470
471 void
472 qiflush(void)
473 {
474 (void) intrflush(stdscr, TRUE);
475 }
476 #endif /* _CURSES_USE_MACROS */
477
478 int
479 intrflush(WINDOW *win, bool bf) /*ARGSUSED*/
480 {
481 /* Check if we need to restart ... */
482 if (_cursesi_screen->endwin)
483 __restartwin();
484
485 if (_cursesi_screen->notty == TRUE)
486 return OK;
487 if (bf) {
488 _cursesi_screen->rawt.c_lflag &= ~NOFLSH;
489 _cursesi_screen->cbreakt.c_lflag &= ~NOFLSH;
490 _cursesi_screen->baset.c_lflag &= ~NOFLSH;
491 } else {
492 _cursesi_screen->rawt.c_lflag |= NOFLSH;
493 _cursesi_screen->cbreakt.c_lflag |= NOFLSH;
494 _cursesi_screen->baset.c_lflag |= NOFLSH;
495 }
496
497 __pfast = 1;
498 return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
499 TCSASOFT | TCSADRAIN : TCSADRAIN,
500 _cursesi_screen->curt) ? ERR : OK);
501 }
502
503 void
504 __startwin(SCREEN *screen)
505 {
506
507 (void) fflush(screen->infd);
508
509 /*
510 * Some C libraries default to a 1K buffer when talking to a tty.
511 * With a larger screen, especially across a network, we'd like
512 * to get it to all flush in a single write. Make it twice as big
513 * as just the characters (so that we have room for cursor motions
514 * and attribute information) but no more than 8K.
515 */
516 if (screen->stdbuf == NULL) {
517 screen->len = LINES * COLS * 2;
518 if (screen->len > 8192)
519 screen->len = 8192;
520 if ((screen->stdbuf = malloc(screen->len)) == NULL)
521 screen->len = 0;
522 }
523 (void) setvbuf(screen->outfd, screen->stdbuf, _IOFBF, screen->len);
524
525 t_puts(screen->cursesi_genbuf, __tc_ti, 0, __cputchar_args,
526 (void *) screen->outfd);
527 t_puts(screen->cursesi_genbuf, __tc_vs, 0, __cputchar_args,
528 (void *) screen->outfd);
529 if (screen->curscr->flags & __KEYPAD)
530 t_puts(screen->cursesi_genbuf, __tc_ks, 0, __cputchar_args,
531 (void *) screen->outfd);
532 screen->endwin = 0;
533 }
534
535 int
536 endwin(void)
537 {
538 return __stopwin();
539 }
540
541 bool
542 isendwin(void)
543 {
544 return (_cursesi_screen->endwin ? TRUE : FALSE);
545 }
546
547 int
548 flushinp(void)
549 {
550 (void) fpurge(_cursesi_screen->infd);
551 return (OK);
552 }
553
554 /*
555 * The following routines, savetty and resetty are completely useless and
556 * are left in only as stubs. If people actually use them they will almost
557 * certainly screw up the state of the world.
558 */
559 /*static struct termios savedtty;*/
560 int
561 savetty(void)
562 {
563 if (_cursesi_screen->notty == TRUE)
564 return OK;
565 return (tcgetattr(fileno(_cursesi_screen->infd),
566 &_cursesi_screen->savedtty) ? ERR : OK);
567 }
568
569 int
570 resetty(void)
571 {
572 if (_cursesi_screen->notty == TRUE)
573 return OK;
574 return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
575 TCSASOFT | TCSADRAIN : TCSADRAIN,
576 &_cursesi_screen->savedtty) ? ERR : OK);
577 }
578
579 /*
580 * erasechar --
581 * Return the character of the erase key.
582 *
583 */
584 char
585 erasechar(void)
586 {
587 if (_cursesi_screen->notty == TRUE)
588 return 0;
589 return _cursesi_screen->baset.c_cc[VERASE];
590 }
591
592 /*
593 * killchar --
594 * Return the character of the kill key.
595 */
596 char
597 killchar(void)
598 {
599 if (_cursesi_screen->notty == TRUE)
600 return 0;
601 return _cursesi_screen->baset.c_cc[VKILL];
602 }
603