tty.c revision 1.46.12.1 1 /* $NetBSD: tty.c,v 1.46.12.1 2018/10/20 06:58:22 pgoyette 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.46.12.1 2018/10/20 06:58:22 pgoyette Exp $");
38 #endif
39 #endif /* not lint */
40
41 #include <sys/fcntl.h>
42 #include <sys/ioctl.h>
43 #include <sys/param.h>
44 #include <sys/types.h>
45
46 #include <stdlib.h>
47 #include <termios.h>
48 #include <unistd.h>
49
50 #include "curses.h"
51 #include "curses_private.h"
52
53 /*
54 * In general, curses should leave tty hardware settings alone (speed, parity,
55 * word size). This is most easily done in BSD by using TCSASOFT on all
56 * tcsetattr calls. On other systems, it would be better to get and restore
57 * those attributes at each change, or at least when stopped and restarted.
58 * See also the comments in getterm().
59 */
60 #ifndef TCSASOFT
61 #define TCSASOFT 0
62 #endif
63
64 int __tcaction = TCSASOFT != 0; /* Ignore hardware settings */
65
66 #ifndef OXTABS
67 #ifdef XTABS /* SMI uses XTABS. */
68 #define OXTABS XTABS
69 #else
70 #define OXTABS 0
71 #endif
72 #endif
73
74 /*
75 * baudrate --
76 * Return the current baudrate
77 */
78 int
79 baudrate(void)
80 {
81
82 if (_cursesi_screen->notty == TRUE)
83 return 0;
84
85 return cfgetospeed(&_cursesi_screen->baset);
86 }
87
88 /*
89 * gettmode --
90 * Do terminal type initialization.
91 */
92 int
93 gettmode(void)
94 {
95
96 if (_cursesi_gettmode(_cursesi_screen) == ERR)
97 return ERR;
98
99 __GT = _cursesi_screen->GT;
100 __NONL = _cursesi_screen->NONL;
101 return OK;
102 }
103
104 /*
105 * _cursesi_gettmode --
106 * Do the terminal type initialisation for the tty attached to the
107 * given screen.
108 */
109 int
110 _cursesi_gettmode(SCREEN *screen)
111 {
112 screen->useraw = 0;
113
114 if (tcgetattr(fileno(screen->infd), &screen->orig_termios)) {
115 /* if the input fd is not a tty try the output */
116 if (tcgetattr(fileno(screen->infd), &screen->orig_termios)) {
117 /* not a tty ... we will disable tty related stuff */
118 screen->notty = TRUE;
119 __GT = 0;
120 __NONL = 0;
121 return OK;
122 }
123 }
124
125 screen->baset = screen->orig_termios;
126 screen->baset.c_oflag &= ~OXTABS;
127
128 screen->GT = 0; /* historical. was used before we wired OXTABS off */
129 screen->NONL = (screen->baset.c_oflag & ONLCR) == 0;
130
131 /*
132 * XXX
133 * System V and SMI systems overload VMIN and VTIME, such that
134 * VMIN is the same as the VEOF element, and VTIME is the same
135 * as the VEOL element. This means that, if VEOF was ^D, the
136 * default VMIN is 4. Majorly stupid.
137 */
138 screen->cbreakt = screen->baset;
139 screen->cbreakt.c_lflag &= ~(ECHO | ECHONL | ICANON);
140 screen->cbreakt.c_cc[VMIN] = 1;
141 screen->cbreakt.c_cc[VTIME] = 0;
142
143 screen->rawt = screen->cbreakt;
144 screen->rawt.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | INLCR | IGNCR |
145 ICRNL | IXON);
146 screen->rawt.c_oflag &= ~OPOST;
147 screen->rawt.c_lflag &= ~(ISIG | IEXTEN);
148
149 #if TCSASOFT == 0
150 /*
151 * In general, curses should leave hardware-related settings alone.
152 * This includes parity and word size. Older versions set the tty
153 * to 8 bits, no parity in raw(), but this is considered to be an
154 * artifact of the old tty interface. If it's desired to change
155 * parity and word size, the TCSASOFT bit has to be removed from the
156 * calls that switch to/from "raw" mode.
157 */
158 screen->rawt.c_iflag &= ~ISTRIP;
159 screen->rawt.c_cflag &= ~(CSIZE | PARENB);
160 screen->rawt.c_cflag |= CS8;
161 #endif
162
163 screen->curt = &screen->baset;
164 return tcsetattr(fileno(screen->infd), TCSASOFT | TCSADRAIN,
165 screen->curt) ? ERR : OK;
166 }
167
168 /*
169 * raw --
170 * Put the terminal into raw mode
171 */
172 int
173 raw(void)
174 {
175 #ifdef DEBUG
176 __CTRACE(__CTRACE_MISC, "raw()\n");
177 #endif
178 /* Check if we need to restart ... */
179 if (_cursesi_screen->endwin)
180 __restartwin();
181
182 _cursesi_screen->useraw = __pfast = __rawmode = 1;
183 _cursesi_screen->curt = &_cursesi_screen->rawt;
184 if (_cursesi_screen->notty == TRUE)
185 return OK;
186 return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN,
187 _cursesi_screen->curt) ? ERR : OK;
188 }
189
190 /*
191 * noraw --
192 * Put the terminal into cooked mode
193 */
194 int
195 noraw(void)
196 {
197 #ifdef DEBUG
198 __CTRACE(__CTRACE_MISC, "noraw()\n");
199 #endif
200 /* Check if we need to restart ... */
201 if (_cursesi_screen->endwin)
202 __restartwin();
203
204 _cursesi_screen->useraw = __pfast = __rawmode = 0;
205 if (_cursesi_screen->notty == TRUE)
206 return OK;
207 _cursesi_screen->curt = &_cursesi_screen->baset;
208 return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN,
209 _cursesi_screen->curt) ? ERR : OK;
210 }
211
212 /*
213 * cbreak --
214 * Enable cbreak mode
215 */
216 int
217 cbreak(void)
218 {
219 #ifdef DEBUG
220 __CTRACE(__CTRACE_MISC, "cbreak()\n");
221 #endif
222 /* Check if we need to restart ... */
223 if (_cursesi_screen->endwin)
224 __restartwin();
225
226 __rawmode = 1;
227 if (_cursesi_screen->notty == TRUE)
228 return OK;
229 _cursesi_screen->curt = _cursesi_screen->useraw ?
230 &_cursesi_screen->rawt : &_cursesi_screen->cbreakt;
231 return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN,
232 _cursesi_screen->curt) ? ERR : OK;
233 }
234
235 /*
236 * nocbreak --
237 * Disable cbreak mode
238 */
239 int
240 nocbreak(void)
241 {
242 #ifdef DEBUG
243 __CTRACE(__CTRACE_MISC, "nocbreak()\n");
244 #endif
245 /* Check if we need to restart ... */
246 if (_cursesi_screen->endwin)
247 __restartwin();
248
249 __rawmode = 0;
250 if (_cursesi_screen->notty == TRUE)
251 return OK;
252 /* if we were in halfdelay mode then nuke the timeout */
253 if ((_cursesi_screen->half_delay == TRUE) &&
254 (__notimeout() == ERR))
255 return ERR;
256
257 _cursesi_screen->half_delay = FALSE;
258 _cursesi_screen->curt = _cursesi_screen->useraw ?
259 &_cursesi_screen->rawt : &_cursesi_screen->baset;
260 return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN,
261 _cursesi_screen->curt) ? ERR : OK;
262 }
263
264 /*
265 * halfdelay --
266 * Put the terminal into cbreak mode with the specified timeout.
267 *
268 */
269 int
270 halfdelay(int duration)
271 {
272 if ((duration < 1) || (duration > 255))
273 return ERR;
274
275 if (cbreak() == ERR)
276 return ERR;
277
278 if (__timeout(duration) == ERR)
279 return ERR;
280
281 _cursesi_screen->half_delay = TRUE;
282 return OK;
283 }
284
285 int
286 __delay(void)
287 {
288 #ifdef DEBUG
289 __CTRACE(__CTRACE_MISC, "__delay()\n");
290 #endif
291 /* Check if we need to restart ... */
292 if (_cursesi_screen->endwin)
293 __restartwin();
294
295 if (_cursesi_screen->notty == TRUE)
296 return OK;
297 _cursesi_screen->rawt.c_cc[VMIN] = 1;
298 _cursesi_screen->rawt.c_cc[VTIME] = 0;
299 _cursesi_screen->cbreakt.c_cc[VMIN] = 1;
300 _cursesi_screen->cbreakt.c_cc[VTIME] = 0;
301 _cursesi_screen->baset.c_cc[VMIN] = 1;
302 _cursesi_screen->baset.c_cc[VTIME] = 0;
303
304 if (tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSANOW,
305 _cursesi_screen->curt)) {
306 __restore_termios();
307 return ERR;
308 }
309
310 return OK;
311 }
312
313 int
314 __nodelay(void)
315 {
316 #ifdef DEBUG
317 __CTRACE(__CTRACE_MISC, "__nodelay()\n");
318 #endif
319 /* Check if we need to restart ... */
320 if (_cursesi_screen->endwin)
321 __restartwin();
322
323 if (_cursesi_screen->notty == TRUE)
324 return OK;
325 _cursesi_screen->rawt.c_cc[VMIN] = 0;
326 _cursesi_screen->rawt.c_cc[VTIME] = 0;
327 _cursesi_screen->cbreakt.c_cc[VMIN] = 0;
328 _cursesi_screen->cbreakt.c_cc[VTIME] = 0;
329 _cursesi_screen->baset.c_cc[VMIN] = 0;
330 _cursesi_screen->baset.c_cc[VTIME] = 0;
331
332 if (tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSANOW,
333 _cursesi_screen->curt)) {
334 __restore_termios();
335 return ERR;
336 }
337
338 return OK;
339 }
340
341 void
342 __save_termios(void)
343 {
344 /* Check if we need to restart ... */
345 if (_cursesi_screen->endwin)
346 __restartwin();
347
348 if (_cursesi_screen->notty == TRUE)
349 return;
350 _cursesi_screen->ovmin = _cursesi_screen->cbreakt.c_cc[VMIN];
351 _cursesi_screen->ovtime = _cursesi_screen->cbreakt.c_cc[VTIME];
352 }
353
354 void
355 __restore_termios(void)
356 {
357 /* Check if we need to restart ... */
358 if (_cursesi_screen->endwin)
359 __restartwin();
360
361 if (_cursesi_screen->notty == TRUE)
362 return;
363 _cursesi_screen->rawt.c_cc[VMIN] = _cursesi_screen->ovmin;
364 _cursesi_screen->rawt.c_cc[VTIME] = _cursesi_screen->ovtime;
365 _cursesi_screen->cbreakt.c_cc[VMIN] = _cursesi_screen->ovmin;
366 _cursesi_screen->cbreakt.c_cc[VTIME] = _cursesi_screen->ovtime;
367 _cursesi_screen->baset.c_cc[VMIN] = _cursesi_screen->ovmin;
368 _cursesi_screen->baset.c_cc[VTIME] = _cursesi_screen->ovtime;
369 }
370
371 int
372 __timeout(int delay)
373 {
374 #ifdef DEBUG
375 __CTRACE(__CTRACE_MISC, "__timeout()\n");
376 #endif
377 /* Check if we need to restart ... */
378 if (_cursesi_screen->endwin)
379 __restartwin();
380
381 if (_cursesi_screen->notty == TRUE)
382 return OK;
383 _cursesi_screen->ovmin = _cursesi_screen->cbreakt.c_cc[VMIN];
384 _cursesi_screen->ovtime = _cursesi_screen->cbreakt.c_cc[VTIME];
385 _cursesi_screen->rawt.c_cc[VMIN] = 0;
386 _cursesi_screen->rawt.c_cc[VTIME] = delay;
387 _cursesi_screen->cbreakt.c_cc[VMIN] = 0;
388 _cursesi_screen->cbreakt.c_cc[VTIME] = delay;
389 _cursesi_screen->baset.c_cc[VMIN] = 0;
390 _cursesi_screen->baset.c_cc[VTIME] = delay;
391
392 if (tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSANOW,
393 _cursesi_screen->curt)) {
394 __restore_termios();
395 return ERR;
396 }
397
398 return OK;
399 }
400
401 int
402 __notimeout(void)
403 {
404 #ifdef DEBUG
405 __CTRACE(__CTRACE_MISC, "__notimeout()\n");
406 #endif
407 /* Check if we need to restart ... */
408 if (_cursesi_screen->endwin)
409 __restartwin();
410
411 if (_cursesi_screen->notty == TRUE)
412 return OK;
413 _cursesi_screen->rawt.c_cc[VMIN] = 1;
414 _cursesi_screen->rawt.c_cc[VTIME] = 0;
415 _cursesi_screen->cbreakt.c_cc[VMIN] = 1;
416 _cursesi_screen->cbreakt.c_cc[VTIME] = 0;
417 _cursesi_screen->baset.c_cc[VMIN] = 1;
418 _cursesi_screen->baset.c_cc[VTIME] = 0;
419
420 return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSANOW,
421 _cursesi_screen->curt) ? ERR : OK;
422 }
423
424 int
425 echo(void)
426 {
427 #ifdef DEBUG
428 __CTRACE(__CTRACE_MISC, "echo()\n");
429 #endif
430 /* Check if we need to restart ... */
431 if (_cursesi_screen->endwin)
432 __restartwin();
433
434 __echoit = 1;
435 return OK;
436 }
437
438 int
439 noecho(void)
440 {
441 #ifdef DEBUG
442 __CTRACE(__CTRACE_MISC, "noecho()\n");
443 #endif
444 /* Check if we need to restart ... */
445 if (_cursesi_screen->endwin)
446 __restartwin();
447
448 __echoit = 0;
449 return OK;
450 }
451
452 int
453 nl(void)
454 {
455 #ifdef DEBUG
456 __CTRACE(__CTRACE_MISC, "nl()\n");
457 #endif
458 /* Check if we need to restart ... */
459 if (_cursesi_screen->endwin)
460 __restartwin();
461
462 if (_cursesi_screen->notty == TRUE)
463 return OK;
464 _cursesi_screen->rawt.c_iflag |= ICRNL;
465 _cursesi_screen->rawt.c_oflag |= ONLCR;
466 _cursesi_screen->cbreakt.c_iflag |= ICRNL;
467 _cursesi_screen->cbreakt.c_oflag |= ONLCR;
468 _cursesi_screen->baset.c_iflag |= ICRNL;
469 _cursesi_screen->baset.c_oflag |= ONLCR;
470
471 _cursesi_screen->nl = 1;
472 _cursesi_screen->pfast = _cursesi_screen->rawmode;
473 return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN,
474 _cursesi_screen->curt) ? ERR : OK;
475 }
476
477 int
478 nonl(void)
479 {
480 #ifdef DEBUG
481 __CTRACE(__CTRACE_MISC, "nonl()\n");
482 #endif
483 /* Check if we need to restart ... */
484 if (_cursesi_screen->endwin)
485 __restartwin();
486
487 if (_cursesi_screen->notty == TRUE)
488 return OK;
489 _cursesi_screen->rawt.c_iflag &= ~ICRNL;
490 _cursesi_screen->rawt.c_oflag &= ~ONLCR;
491 _cursesi_screen->cbreakt.c_iflag &= ~ICRNL;
492 _cursesi_screen->cbreakt.c_oflag &= ~ONLCR;
493 _cursesi_screen->baset.c_iflag &= ~ICRNL;
494 _cursesi_screen->baset.c_oflag &= ~ONLCR;
495
496 _cursesi_screen->nl = 0;
497 __pfast = 1;
498 return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN,
499 _cursesi_screen->curt) ? ERR : OK;
500 }
501
502 #ifndef _CURSES_USE_MACROS
503 void
504 noqiflush(void)
505 {
506
507 (void)intrflush(stdscr, FALSE);
508 }
509
510 void
511 qiflush(void)
512 {
513
514 (void)intrflush(stdscr, TRUE);
515 }
516 #endif /* _CURSES_USE_MACROS */
517
518 /*ARGSUSED*/
519 int
520 intrflush(WINDOW *win, bool bf)
521 {
522 /* Check if we need to restart ... */
523 if (_cursesi_screen->endwin)
524 __restartwin();
525
526 if (_cursesi_screen->notty == TRUE)
527 return OK;
528 if (bf) {
529 _cursesi_screen->rawt.c_lflag &= ~NOFLSH;
530 _cursesi_screen->cbreakt.c_lflag &= ~NOFLSH;
531 _cursesi_screen->baset.c_lflag &= ~NOFLSH;
532 } else {
533 _cursesi_screen->rawt.c_lflag |= NOFLSH;
534 _cursesi_screen->cbreakt.c_lflag |= NOFLSH;
535 _cursesi_screen->baset.c_lflag |= NOFLSH;
536 }
537
538 __pfast = 1;
539 return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN,
540 _cursesi_screen->curt) ? ERR : OK;
541 }
542
543 void
544 __startwin(SCREEN *screen)
545 {
546
547 (void)fflush(screen->infd);
548
549 #ifdef BSD
550 /*
551 * Some C libraries default to a 1K buffer when talking to a tty.
552 * With a larger screen, especially across a network, we'd like
553 * to get it to all flush in a single write. Make it twice as big
554 * as just the characters (so that we have room for cursor motions
555 * and attribute information) but no more than 8K.
556 *
557 * However, setvbuf may only be used after opening a stream and
558 * before any operations have been performed on it.
559 * This means we cannot work portably if an application wants
560 * to stop curses and start curses after a resize.
561 * Curses resizing is not standard, and thus not strictly portable
562 * even though all curses today support it.
563 * The BSD systems do not suffer from this limitation on setvbuf.
564 */
565 if (screen->stdbuf == NULL) {
566 screen->len = LINES * COLS * 2;
567 if (screen->len > 8192)
568 screen->len = 8192;
569 if ((screen->stdbuf = malloc(screen->len)) == NULL)
570 screen->len = 0;
571 }
572 (void)setvbuf(screen->outfd, screen->stdbuf, _IOFBF, screen->len);
573 #endif
574
575 ti_puts(screen->term, t_enter_ca_mode(screen->term), 0,
576 __cputchar_args, (void *) screen->outfd);
577 ti_puts(screen->term, t_cursor_normal(screen->term), 0,
578 __cputchar_args, (void *) screen->outfd);
579 if (screen->curscr->flags & __KEYPAD)
580 ti_puts(screen->term, t_keypad_xmit(screen->term), 0,
581 __cputchar_args, (void *) screen->outfd);
582 screen->endwin = 0;
583 }
584
585 int
586 endwin(void)
587 {
588 #ifdef DEBUG
589 __CTRACE(__CTRACE_MISC, "endwin\n");
590 #endif
591 return __stopwin();
592 }
593
594 bool
595 isendwin(void)
596 {
597
598 return _cursesi_screen->endwin ? TRUE : FALSE;
599 }
600
601 int
602 flushinp(void)
603 {
604
605 (void)fpurge(_cursesi_screen->infd);
606 return OK;
607 }
608
609 /*
610 * The following routines, savetty and resetty are completely useless and
611 * are left in only as stubs. If people actually use them they will almost
612 * certainly screw up the state of the world.
613 */
614 /*static struct termios savedtty;*/
615 int
616 savetty(void)
617 {
618
619 if (_cursesi_screen->notty == TRUE)
620 return OK;
621 return tcgetattr(fileno(_cursesi_screen->infd),
622 &_cursesi_screen->savedtty) ? ERR : OK;
623 }
624
625 int
626 resetty(void)
627 {
628
629 if (_cursesi_screen->notty == TRUE)
630 return OK;
631 return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN,
632 &_cursesi_screen->savedtty) ? ERR : OK;
633 }
634
635 /*
636 * erasechar --
637 * Return the character of the erase key.
638 */
639 char
640 erasechar(void)
641 {
642
643 if (_cursesi_screen->notty == TRUE)
644 return 0;
645 return _cursesi_screen->baset.c_cc[VERASE];
646 }
647
648 /*
649 * killchar --
650 * Return the character of the kill key.
651 */
652 char
653 killchar(void)
654 {
655
656 if (_cursesi_screen->notty == TRUE)
657 return 0;
658 return _cursesi_screen->baset.c_cc[VKILL];
659 }
660
661 /*
662 * erasewchar --
663 * Return the wide character of the erase key.
664 */
665 int
666 erasewchar(wchar_t *ch)
667 {
668
669 #ifndef HAVE_WCHAR
670 return ERR;
671 #else
672 if (_cursesi_screen->notty == TRUE)
673 return ERR;
674 *ch = _cursesi_screen->baset.c_cc[VERASE];
675 return OK;
676 #endif /* HAVE_WCHAR */
677 }
678
679 /*
680 * killwchar --
681 * Return the wide character of the kill key.
682 */
683 int
684 killwchar( wchar_t *ch )
685 {
686
687 #ifndef HAVE_WCHAR
688 return ERR;
689 #else
690 if (_cursesi_screen->notty == TRUE)
691 return 0;
692 *ch = _cursesi_screen->baset.c_cc[VKILL];
693 return OK;
694 #endif /* HAVE_WCHAR */
695 }
696
697 int
698 typeahead(int filedes)
699 {
700
701 _cursesi_screen->checkfd = filedes;
702 return OK;
703 }
704