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