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