tty.c revision 1.38.4.2 1 /* $NetBSD: tty.c,v 1.38.4.2 2007/01/21 17:43:36 jdc 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.38.4.2 2007/01/21 17:43:36 jdc 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 return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
306 TCSASOFT : TCSANOW, _cursesi_screen->curt) ? ERR : OK);
307 }
308
309 int
310 __nodelay(void)
311 {
312 #ifdef DEBUG
313 __CTRACE(__CTRACE_MISC, "__nodelay()\n");
314 #endif
315 /* Check if we need to restart ... */
316 if (_cursesi_screen->endwin)
317 __restartwin();
318
319 if (_cursesi_screen->notty == TRUE)
320 return OK;
321 _cursesi_screen->rawt.c_cc[VMIN] = 0;
322 _cursesi_screen->rawt.c_cc[VTIME] = 0;
323 _cursesi_screen->cbreakt.c_cc[VMIN] = 0;
324 _cursesi_screen->cbreakt.c_cc[VTIME] = 0;
325 _cursesi_screen->baset.c_cc[VMIN] = 0;
326 _cursesi_screen->baset.c_cc[VTIME] = 0;
327
328 return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
329 TCSASOFT : TCSANOW, _cursesi_screen->curt) ? ERR : OK);
330 }
331
332 void
333 __save_termios(void)
334 {
335 /* Check if we need to restart ... */
336 if (_cursesi_screen->endwin)
337 __restartwin();
338
339 if (_cursesi_screen->notty == TRUE)
340 return;
341 _cursesi_screen->ovmin = _cursesi_screen->cbreakt.c_cc[VMIN];
342 _cursesi_screen->ovtime = _cursesi_screen->cbreakt.c_cc[VTIME];
343 }
344
345 void
346 __restore_termios(void)
347 {
348 /* Check if we need to restart ... */
349 if (_cursesi_screen->endwin)
350 __restartwin();
351
352 if (_cursesi_screen->notty == TRUE)
353 return;
354 _cursesi_screen->rawt.c_cc[VMIN] = _cursesi_screen->ovmin;
355 _cursesi_screen->rawt.c_cc[VTIME] = _cursesi_screen->ovtime;
356 _cursesi_screen->cbreakt.c_cc[VMIN] = _cursesi_screen->ovmin;
357 _cursesi_screen->cbreakt.c_cc[VTIME] = _cursesi_screen->ovtime;
358 _cursesi_screen->baset.c_cc[VMIN] = _cursesi_screen->ovmin;
359 _cursesi_screen->baset.c_cc[VTIME] = _cursesi_screen->ovtime;
360 }
361
362 int
363 __timeout(int delay)
364 {
365 #ifdef DEBUG
366 __CTRACE(__CTRACE_MISC, "__timeout()\n");
367 #endif
368 /* Check if we need to restart ... */
369 if (_cursesi_screen->endwin)
370 __restartwin();
371
372 if (_cursesi_screen->notty == TRUE)
373 return OK;
374 _cursesi_screen->ovmin = _cursesi_screen->cbreakt.c_cc[VMIN];
375 _cursesi_screen->ovtime = _cursesi_screen->cbreakt.c_cc[VTIME];
376 _cursesi_screen->rawt.c_cc[VMIN] = 0;
377 _cursesi_screen->rawt.c_cc[VTIME] = delay;
378 _cursesi_screen->cbreakt.c_cc[VMIN] = 0;
379 _cursesi_screen->cbreakt.c_cc[VTIME] = delay;
380 _cursesi_screen->baset.c_cc[VMIN] = 0;
381 _cursesi_screen->baset.c_cc[VTIME] = delay;
382
383 return (tcsetattr(fileno(_cursesi_screen->infd),
384 __tcaction ? TCSASOFT | TCSANOW : TCSANOW,
385 _cursesi_screen->curt) ? ERR : OK);
386 }
387
388 int
389 __notimeout(void)
390 {
391 #ifdef DEBUG
392 __CTRACE(__CTRACE_MISC, "__notimeout()\n");
393 #endif
394 /* Check if we need to restart ... */
395 if (_cursesi_screen->endwin)
396 __restartwin();
397
398 if (_cursesi_screen->notty == TRUE)
399 return OK;
400 _cursesi_screen->rawt.c_cc[VMIN] = 1;
401 _cursesi_screen->rawt.c_cc[VTIME] = 0;
402 _cursesi_screen->cbreakt.c_cc[VMIN] = 1;
403 _cursesi_screen->cbreakt.c_cc[VTIME] = 0;
404 _cursesi_screen->baset.c_cc[VMIN] = 1;
405 _cursesi_screen->baset.c_cc[VTIME] = 0;
406
407 return (tcsetattr(fileno(_cursesi_screen->infd),
408 __tcaction ? TCSASOFT | TCSANOW : TCSANOW,
409 _cursesi_screen->curt) ? ERR : OK);
410 }
411
412 int
413 echo(void)
414 {
415 #ifdef DEBUG
416 __CTRACE(__CTRACE_MISC, "echo()\n");
417 #endif
418 /* Check if we need to restart ... */
419 if (_cursesi_screen->endwin)
420 __restartwin();
421
422 __echoit = 1;
423 return (OK);
424 }
425
426 int
427 noecho(void)
428 {
429 #ifdef DEBUG
430 __CTRACE(__CTRACE_MISC, "noecho()\n");
431 #endif
432 /* Check if we need to restart ... */
433 if (_cursesi_screen->endwin)
434 __restartwin();
435
436 __echoit = 0;
437 return (OK);
438 }
439
440 int
441 nl(void)
442 {
443 #ifdef DEBUG
444 __CTRACE(__CTRACE_MISC, "nl()\n");
445 #endif
446 /* Check if we need to restart ... */
447 if (_cursesi_screen->endwin)
448 __restartwin();
449
450 if (_cursesi_screen->notty == TRUE)
451 return OK;
452 _cursesi_screen->rawt.c_iflag |= ICRNL;
453 _cursesi_screen->rawt.c_oflag |= ONLCR;
454 _cursesi_screen->cbreakt.c_iflag |= ICRNL;
455 _cursesi_screen->cbreakt.c_oflag |= ONLCR;
456 _cursesi_screen->baset.c_iflag |= ICRNL;
457 _cursesi_screen->baset.c_oflag |= ONLCR;
458
459 _cursesi_screen->nl = 1;
460 _cursesi_screen->pfast = _cursesi_screen->rawmode;
461 return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
462 TCSASOFT | TCSADRAIN : TCSADRAIN,
463 _cursesi_screen->curt) ? ERR : OK);
464 }
465
466 int
467 nonl(void)
468 {
469 #ifdef DEBUG
470 __CTRACE(__CTRACE_MISC, "nonl()\n");
471 #endif
472 /* Check if we need to restart ... */
473 if (_cursesi_screen->endwin)
474 __restartwin();
475
476 if (_cursesi_screen->notty == TRUE)
477 return OK;
478 _cursesi_screen->rawt.c_iflag &= ~ICRNL;
479 _cursesi_screen->rawt.c_oflag &= ~ONLCR;
480 _cursesi_screen->cbreakt.c_iflag &= ~ICRNL;
481 _cursesi_screen->cbreakt.c_oflag &= ~ONLCR;
482 _cursesi_screen->baset.c_iflag &= ~ICRNL;
483 _cursesi_screen->baset.c_oflag &= ~ONLCR;
484
485 _cursesi_screen->nl = 0;
486 __pfast = 1;
487 return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
488 TCSASOFT | TCSADRAIN : TCSADRAIN,
489 _cursesi_screen->curt) ? ERR : OK);
490 }
491
492 #ifndef _CURSES_USE_MACROS
493 void
494 noqiflush(void)
495 {
496 (void) intrflush(stdscr, FALSE);
497 }
498
499 void
500 qiflush(void)
501 {
502 (void) intrflush(stdscr, TRUE);
503 }
504 #endif /* _CURSES_USE_MACROS */
505
506 int
507 intrflush(WINDOW *win, bool bf) /*ARGSUSED*/
508 {
509 /* Check if we need to restart ... */
510 if (_cursesi_screen->endwin)
511 __restartwin();
512
513 if (_cursesi_screen->notty == TRUE)
514 return OK;
515 if (bf) {
516 _cursesi_screen->rawt.c_lflag &= ~NOFLSH;
517 _cursesi_screen->cbreakt.c_lflag &= ~NOFLSH;
518 _cursesi_screen->baset.c_lflag &= ~NOFLSH;
519 } else {
520 _cursesi_screen->rawt.c_lflag |= NOFLSH;
521 _cursesi_screen->cbreakt.c_lflag |= NOFLSH;
522 _cursesi_screen->baset.c_lflag |= NOFLSH;
523 }
524
525 __pfast = 1;
526 return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
527 TCSASOFT | TCSADRAIN : TCSADRAIN,
528 _cursesi_screen->curt) ? ERR : OK);
529 }
530
531 void
532 __startwin(SCREEN *screen)
533 {
534
535 (void) fflush(screen->infd);
536
537 /*
538 * Some C libraries default to a 1K buffer when talking to a tty.
539 * With a larger screen, especially across a network, we'd like
540 * to get it to all flush in a single write. Make it twice as big
541 * as just the characters (so that we have room for cursor motions
542 * and attribute information) but no more than 8K.
543 */
544 if (screen->stdbuf == NULL) {
545 screen->len = LINES * COLS * 2;
546 if (screen->len > 8192)
547 screen->len = 8192;
548 if ((screen->stdbuf = malloc(screen->len)) == NULL)
549 screen->len = 0;
550 }
551 (void) setvbuf(screen->outfd, screen->stdbuf, _IOFBF, screen->len);
552
553 t_puts(screen->cursesi_genbuf, __tc_ti, 0, __cputchar_args,
554 (void *) screen->outfd);
555 t_puts(screen->cursesi_genbuf, __tc_vs, 0, __cputchar_args,
556 (void *) screen->outfd);
557 if (screen->curscr->flags & __KEYPAD)
558 t_puts(screen->cursesi_genbuf, __tc_ks, 0, __cputchar_args,
559 (void *) screen->outfd);
560 screen->endwin = 0;
561 }
562
563 int
564 endwin(void)
565 {
566 #ifdef DEBUG
567 __CTRACE(__CTRACE_MISC, "endwin\n");
568 #endif
569 return __stopwin();
570 }
571
572 bool
573 isendwin(void)
574 {
575 return (_cursesi_screen->endwin ? TRUE : FALSE);
576 }
577
578 int
579 flushinp(void)
580 {
581 (void) fpurge(_cursesi_screen->infd);
582 return (OK);
583 }
584
585 /*
586 * The following routines, savetty and resetty are completely useless and
587 * are left in only as stubs. If people actually use them they will almost
588 * certainly screw up the state of the world.
589 */
590 /*static struct termios savedtty;*/
591 int
592 savetty(void)
593 {
594 if (_cursesi_screen->notty == TRUE)
595 return OK;
596 return (tcgetattr(fileno(_cursesi_screen->infd),
597 &_cursesi_screen->savedtty) ? ERR : OK);
598 }
599
600 int
601 resetty(void)
602 {
603 if (_cursesi_screen->notty == TRUE)
604 return OK;
605 return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
606 TCSASOFT | TCSADRAIN : TCSADRAIN,
607 &_cursesi_screen->savedtty) ? ERR : OK);
608 }
609
610 /*
611 * erasechar --
612 * Return the character of the erase key.
613 */
614 char
615 erasechar(void)
616 {
617 if (_cursesi_screen->notty == TRUE)
618 return 0;
619 return _cursesi_screen->baset.c_cc[VERASE];
620 }
621
622 /*
623 * killchar --
624 * Return the character of the kill key.
625 */
626 char
627 killchar(void)
628 {
629 if (_cursesi_screen->notty == TRUE)
630 return 0;
631 return _cursesi_screen->baset.c_cc[VKILL];
632 }
633
634 /*
635 * erasewchar --
636 * Return the wide character of the erase key.
637 */
638 int
639 erasewchar( wchar_t *ch )
640 {
641 #ifndef HAVE_WCHAR
642 return ERR;
643 #else
644 if (_cursesi_screen->notty == TRUE)
645 return ERR;
646 *ch = _cursesi_screen->baset.c_cc[VERASE];
647 return OK;
648 #endif /* HAVE_WCHAR */
649 }
650
651 /*
652 * killwchar --
653 * Return the wide character of the kill key.
654 */
655 int
656 killwchar( wchar_t *ch )
657 {
658 #ifndef HAVE_WCHAR
659 return ERR;
660 #else
661 if (_cursesi_screen->notty == TRUE)
662 return 0;
663 *ch = _cursesi_screen->baset.c_cc[VKILL];
664 return OK;
665 #endif /* HAVE_WCHAR */
666 }
667