tty.c revision 1.42 1 /* $NetBSD: tty.c,v 1.42 2010/02/03 15:34:40 roy 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.42 2010/02/03 15:34:40 roy 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 /*
166 * raw --
167 * Put the terminal into raw mode
168 */
169 int
170 raw(void)
171 {
172 #ifdef DEBUG
173 __CTRACE(__CTRACE_MISC, "raw()\n");
174 #endif
175 /* Check if we need to restart ... */
176 if (_cursesi_screen->endwin)
177 __restartwin();
178
179 _cursesi_screen->useraw = __pfast = __rawmode = 1;
180 _cursesi_screen->curt = &_cursesi_screen->rawt;
181 if (_cursesi_screen->notty == TRUE)
182 return OK;
183 return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
184 TCSASOFT | TCSADRAIN : TCSADRAIN,
185 _cursesi_screen->curt) ? ERR : OK);
186 }
187
188 /*
189 * noraw --
190 * Put the terminal into cooked mode
191 */
192 int
193 noraw(void)
194 {
195 #ifdef DEBUG
196 __CTRACE(__CTRACE_MISC, "noraw()\n");
197 #endif
198 /* Check if we need to restart ... */
199 if (_cursesi_screen->endwin)
200 __restartwin();
201
202 _cursesi_screen->useraw = __pfast = __rawmode = 0;
203 if (_cursesi_screen->notty == TRUE)
204 return OK;
205 _cursesi_screen->curt = &_cursesi_screen->baset;
206 return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
207 TCSASOFT | TCSADRAIN : TCSADRAIN,
208 _cursesi_screen->curt) ? ERR : OK);
209 }
210
211 /*
212 * cbreak --
213 * Enable cbreak mode
214 */
215 int
216 cbreak(void)
217 {
218 #ifdef DEBUG
219 __CTRACE(__CTRACE_MISC, "cbreak()\n");
220 #endif
221 /* Check if we need to restart ... */
222 if (_cursesi_screen->endwin)
223 __restartwin();
224
225 __rawmode = 1;
226 if (_cursesi_screen->notty == TRUE)
227 return OK;
228 _cursesi_screen->curt = _cursesi_screen->useraw ?
229 &_cursesi_screen->rawt : &_cursesi_screen->cbreakt;
230 return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
231 TCSASOFT | TCSADRAIN : 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), __tcaction ?
261 TCSASOFT | TCSADRAIN : TCSADRAIN,
262 _cursesi_screen->curt) ? ERR : OK);
263 }
264
265 /*
266 * halfdelay --
267 * Put the terminal into cbreak mode with the specified timeout.
268 *
269 */
270 int
271 halfdelay(int duration)
272 {
273 if ((duration < 1) || (duration > 255))
274 return ERR;
275
276 if (cbreak() == ERR)
277 return ERR;
278
279 if (__timeout(duration) == ERR)
280 return ERR;
281
282 _cursesi_screen->half_delay = TRUE;
283 return OK;
284 }
285
286 int
287 __delay(void)
288 {
289 #ifdef DEBUG
290 __CTRACE(__CTRACE_MISC, "__delay()\n");
291 #endif
292 /* Check if we need to restart ... */
293 if (_cursesi_screen->endwin)
294 __restartwin();
295
296 if (_cursesi_screen->notty == TRUE)
297 return OK;
298 _cursesi_screen->rawt.c_cc[VMIN] = 1;
299 _cursesi_screen->rawt.c_cc[VTIME] = 0;
300 _cursesi_screen->cbreakt.c_cc[VMIN] = 1;
301 _cursesi_screen->cbreakt.c_cc[VTIME] = 0;
302 _cursesi_screen->baset.c_cc[VMIN] = 1;
303 _cursesi_screen->baset.c_cc[VTIME] = 0;
304
305 if (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
306 TCSASOFT : TCSANOW, _cursesi_screen->curt)) {
307 __restore_termios();
308 return ERR;
309 }
310
311 return OK;
312 }
313
314 int
315 __nodelay(void)
316 {
317 #ifdef DEBUG
318 __CTRACE(__CTRACE_MISC, "__nodelay()\n");
319 #endif
320 /* Check if we need to restart ... */
321 if (_cursesi_screen->endwin)
322 __restartwin();
323
324 if (_cursesi_screen->notty == TRUE)
325 return OK;
326 _cursesi_screen->rawt.c_cc[VMIN] = 0;
327 _cursesi_screen->rawt.c_cc[VTIME] = 0;
328 _cursesi_screen->cbreakt.c_cc[VMIN] = 0;
329 _cursesi_screen->cbreakt.c_cc[VTIME] = 0;
330 _cursesi_screen->baset.c_cc[VMIN] = 0;
331 _cursesi_screen->baset.c_cc[VTIME] = 0;
332
333 if (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
334 TCSASOFT : TCSANOW, _cursesi_screen->curt)) {
335 __restore_termios();
336 return ERR;
337 }
338
339 return OK;
340 }
341
342 void
343 __save_termios(void)
344 {
345 /* Check if we need to restart ... */
346 if (_cursesi_screen->endwin)
347 __restartwin();
348
349 if (_cursesi_screen->notty == TRUE)
350 return;
351 _cursesi_screen->ovmin = _cursesi_screen->cbreakt.c_cc[VMIN];
352 _cursesi_screen->ovtime = _cursesi_screen->cbreakt.c_cc[VTIME];
353 }
354
355 void
356 __restore_termios(void)
357 {
358 /* Check if we need to restart ... */
359 if (_cursesi_screen->endwin)
360 __restartwin();
361
362 if (_cursesi_screen->notty == TRUE)
363 return;
364 _cursesi_screen->rawt.c_cc[VMIN] = _cursesi_screen->ovmin;
365 _cursesi_screen->rawt.c_cc[VTIME] = _cursesi_screen->ovtime;
366 _cursesi_screen->cbreakt.c_cc[VMIN] = _cursesi_screen->ovmin;
367 _cursesi_screen->cbreakt.c_cc[VTIME] = _cursesi_screen->ovtime;
368 _cursesi_screen->baset.c_cc[VMIN] = _cursesi_screen->ovmin;
369 _cursesi_screen->baset.c_cc[VTIME] = _cursesi_screen->ovtime;
370 }
371
372 int
373 __timeout(int delay)
374 {
375 #ifdef DEBUG
376 __CTRACE(__CTRACE_MISC, "__timeout()\n");
377 #endif
378 /* Check if we need to restart ... */
379 if (_cursesi_screen->endwin)
380 __restartwin();
381
382 if (_cursesi_screen->notty == TRUE)
383 return OK;
384 _cursesi_screen->ovmin = _cursesi_screen->cbreakt.c_cc[VMIN];
385 _cursesi_screen->ovtime = _cursesi_screen->cbreakt.c_cc[VTIME];
386 _cursesi_screen->rawt.c_cc[VMIN] = 0;
387 _cursesi_screen->rawt.c_cc[VTIME] = delay;
388 _cursesi_screen->cbreakt.c_cc[VMIN] = 0;
389 _cursesi_screen->cbreakt.c_cc[VTIME] = delay;
390 _cursesi_screen->baset.c_cc[VMIN] = 0;
391 _cursesi_screen->baset.c_cc[VTIME] = delay;
392
393 if (tcsetattr(fileno(_cursesi_screen->infd),
394 __tcaction ? TCSASOFT | TCSANOW : TCSANOW,
395 _cursesi_screen->curt)) {
396 __restore_termios();
397 return ERR;
398 }
399
400 return OK;
401 }
402
403 int
404 __notimeout(void)
405 {
406 #ifdef DEBUG
407 __CTRACE(__CTRACE_MISC, "__notimeout()\n");
408 #endif
409 /* Check if we need to restart ... */
410 if (_cursesi_screen->endwin)
411 __restartwin();
412
413 if (_cursesi_screen->notty == TRUE)
414 return OK;
415 _cursesi_screen->rawt.c_cc[VMIN] = 1;
416 _cursesi_screen->rawt.c_cc[VTIME] = 0;
417 _cursesi_screen->cbreakt.c_cc[VMIN] = 1;
418 _cursesi_screen->cbreakt.c_cc[VTIME] = 0;
419 _cursesi_screen->baset.c_cc[VMIN] = 1;
420 _cursesi_screen->baset.c_cc[VTIME] = 0;
421
422 return (tcsetattr(fileno(_cursesi_screen->infd),
423 __tcaction ? TCSASOFT | TCSANOW : TCSANOW,
424 _cursesi_screen->curt) ? ERR : OK);
425 }
426
427 int
428 echo(void)
429 {
430 #ifdef DEBUG
431 __CTRACE(__CTRACE_MISC, "echo()\n");
432 #endif
433 /* Check if we need to restart ... */
434 if (_cursesi_screen->endwin)
435 __restartwin();
436
437 __echoit = 1;
438 return (OK);
439 }
440
441 int
442 noecho(void)
443 {
444 #ifdef DEBUG
445 __CTRACE(__CTRACE_MISC, "noecho()\n");
446 #endif
447 /* Check if we need to restart ... */
448 if (_cursesi_screen->endwin)
449 __restartwin();
450
451 __echoit = 0;
452 return (OK);
453 }
454
455 int
456 nl(void)
457 {
458 #ifdef DEBUG
459 __CTRACE(__CTRACE_MISC, "nl()\n");
460 #endif
461 /* Check if we need to restart ... */
462 if (_cursesi_screen->endwin)
463 __restartwin();
464
465 if (_cursesi_screen->notty == TRUE)
466 return OK;
467 _cursesi_screen->rawt.c_iflag |= ICRNL;
468 _cursesi_screen->rawt.c_oflag |= ONLCR;
469 _cursesi_screen->cbreakt.c_iflag |= ICRNL;
470 _cursesi_screen->cbreakt.c_oflag |= ONLCR;
471 _cursesi_screen->baset.c_iflag |= ICRNL;
472 _cursesi_screen->baset.c_oflag |= ONLCR;
473
474 _cursesi_screen->nl = 1;
475 _cursesi_screen->pfast = _cursesi_screen->rawmode;
476 return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
477 TCSASOFT | TCSADRAIN : TCSADRAIN,
478 _cursesi_screen->curt) ? ERR : OK);
479 }
480
481 int
482 nonl(void)
483 {
484 #ifdef DEBUG
485 __CTRACE(__CTRACE_MISC, "nonl()\n");
486 #endif
487 /* Check if we need to restart ... */
488 if (_cursesi_screen->endwin)
489 __restartwin();
490
491 if (_cursesi_screen->notty == TRUE)
492 return OK;
493 _cursesi_screen->rawt.c_iflag &= ~ICRNL;
494 _cursesi_screen->rawt.c_oflag &= ~ONLCR;
495 _cursesi_screen->cbreakt.c_iflag &= ~ICRNL;
496 _cursesi_screen->cbreakt.c_oflag &= ~ONLCR;
497 _cursesi_screen->baset.c_iflag &= ~ICRNL;
498 _cursesi_screen->baset.c_oflag &= ~ONLCR;
499
500 _cursesi_screen->nl = 0;
501 __pfast = 1;
502 return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
503 TCSASOFT | TCSADRAIN : TCSADRAIN,
504 _cursesi_screen->curt) ? ERR : OK);
505 }
506
507 #ifndef _CURSES_USE_MACROS
508 void
509 noqiflush(void)
510 {
511 (void) intrflush(stdscr, FALSE);
512 }
513
514 void
515 qiflush(void)
516 {
517 (void) intrflush(stdscr, TRUE);
518 }
519 #endif /* _CURSES_USE_MACROS */
520
521 int
522 intrflush(WINDOW *win, bool bf) /*ARGSUSED*/
523 {
524 /* Check if we need to restart ... */
525 if (_cursesi_screen->endwin)
526 __restartwin();
527
528 if (_cursesi_screen->notty == TRUE)
529 return OK;
530 if (bf) {
531 _cursesi_screen->rawt.c_lflag &= ~NOFLSH;
532 _cursesi_screen->cbreakt.c_lflag &= ~NOFLSH;
533 _cursesi_screen->baset.c_lflag &= ~NOFLSH;
534 } else {
535 _cursesi_screen->rawt.c_lflag |= NOFLSH;
536 _cursesi_screen->cbreakt.c_lflag |= NOFLSH;
537 _cursesi_screen->baset.c_lflag |= NOFLSH;
538 }
539
540 __pfast = 1;
541 return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
542 TCSASOFT | TCSADRAIN : TCSADRAIN,
543 _cursesi_screen->curt) ? ERR : OK);
544 }
545
546 void
547 __startwin(SCREEN *screen)
548 {
549
550 (void) fflush(screen->infd);
551
552 /*
553 * Some C libraries default to a 1K buffer when talking to a tty.
554 * With a larger screen, especially across a network, we'd like
555 * to get it to all flush in a single write. Make it twice as big
556 * as just the characters (so that we have room for cursor motions
557 * and attribute information) but no more than 8K.
558 */
559 if (screen->stdbuf == NULL) {
560 screen->len = LINES * COLS * 2;
561 if (screen->len > 8192)
562 screen->len = 8192;
563 if ((screen->stdbuf = malloc(screen->len)) == NULL)
564 screen->len = 0;
565 }
566 (void) setvbuf(screen->outfd, screen->stdbuf, _IOFBF, screen->len);
567
568 ti_puts(screen->term, t_enter_ca_mode(screen->term), 0,
569 __cputchar_args, (void *) screen->outfd);
570 ti_puts(screen->term, t_cursor_normal(screen->term), 0,
571 __cputchar_args, (void *) screen->outfd);
572 if (screen->curscr->flags & __KEYPAD)
573 ti_puts(screen->term, t_keypad_xmit(screen->term), 0,
574 __cputchar_args, (void *) screen->outfd);
575 screen->endwin = 0;
576 }
577
578 int
579 endwin(void)
580 {
581 #ifdef DEBUG
582 __CTRACE(__CTRACE_MISC, "endwin\n");
583 #endif
584 return __stopwin();
585 }
586
587 bool
588 isendwin(void)
589 {
590 return (_cursesi_screen->endwin ? TRUE : FALSE);
591 }
592
593 int
594 flushinp(void)
595 {
596 (void) fpurge(_cursesi_screen->infd);
597 return (OK);
598 }
599
600 /*
601 * The following routines, savetty and resetty are completely useless and
602 * are left in only as stubs. If people actually use them they will almost
603 * certainly screw up the state of the world.
604 */
605 /*static struct termios savedtty;*/
606 int
607 savetty(void)
608 {
609 if (_cursesi_screen->notty == TRUE)
610 return OK;
611 return (tcgetattr(fileno(_cursesi_screen->infd),
612 &_cursesi_screen->savedtty) ? ERR : OK);
613 }
614
615 int
616 resetty(void)
617 {
618 if (_cursesi_screen->notty == TRUE)
619 return OK;
620 return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
621 TCSASOFT | TCSADRAIN : TCSADRAIN,
622 &_cursesi_screen->savedtty) ? ERR : OK);
623 }
624
625 /*
626 * erasechar --
627 * Return the character of the erase key.
628 */
629 char
630 erasechar(void)
631 {
632 if (_cursesi_screen->notty == TRUE)
633 return 0;
634 return _cursesi_screen->baset.c_cc[VERASE];
635 }
636
637 /*
638 * killchar --
639 * Return the character of the kill key.
640 */
641 char
642 killchar(void)
643 {
644 if (_cursesi_screen->notty == TRUE)
645 return 0;
646 return _cursesi_screen->baset.c_cc[VKILL];
647 }
648
649 /*
650 * erasewchar --
651 * Return the wide character of the erase key.
652 */
653 int
654 erasewchar( wchar_t *ch )
655 {
656 #ifndef HAVE_WCHAR
657 return ERR;
658 #else
659 if (_cursesi_screen->notty == TRUE)
660 return ERR;
661 *ch = _cursesi_screen->baset.c_cc[VERASE];
662 return OK;
663 #endif /* HAVE_WCHAR */
664 }
665
666 /*
667 * killwchar --
668 * Return the wide character of the kill key.
669 */
670 int
671 killwchar( wchar_t *ch )
672 {
673 #ifndef HAVE_WCHAR
674 return ERR;
675 #else
676 if (_cursesi_screen->notty == TRUE)
677 return 0;
678 *ch = _cursesi_screen->baset.c_cc[VKILL];
679 return OK;
680 #endif /* HAVE_WCHAR */
681 }
682