Home | History | Annotate | Line # | Download | only in libcurses
tty.c revision 1.32
      1 /*	$NetBSD: tty.c,v 1.32 2003/04/05 10:07:00 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.32 2003/04/05 10:07:00 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 int
    469 intrflush(WINDOW *win, bool bf)	/*ARGSUSED*/
    470 {
    471 	/* Check if we need to restart ... */
    472 	if (_cursesi_screen->endwin)
    473 		__restartwin();
    474 
    475 	if (_cursesi_screen->notty == TRUE)
    476 		return OK;
    477 	if (bf) {
    478 		_cursesi_screen->rawt.c_lflag &= ~NOFLSH;
    479 		_cursesi_screen->cbreakt.c_lflag &= ~NOFLSH;
    480 		_cursesi_screen->baset.c_lflag &= ~NOFLSH;
    481 	} else {
    482 		_cursesi_screen->rawt.c_lflag |= NOFLSH;
    483 		_cursesi_screen->cbreakt.c_lflag |= NOFLSH;
    484 		_cursesi_screen->baset.c_lflag |= NOFLSH;
    485 	}
    486 
    487 	__pfast = 1;
    488 	return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
    489 			  TCSASOFT | TCSADRAIN : TCSADRAIN,
    490 			  _cursesi_screen->curt) ? ERR : OK);
    491 }
    492 
    493 void
    494 __startwin(SCREEN *screen)
    495 {
    496 
    497 	(void) fflush(screen->infd);
    498 
    499 	/*
    500 	 * Some C libraries default to a 1K buffer when talking to a tty.
    501 	 * With a larger screen, especially across a network, we'd like
    502 	 * to get it to all flush in a single write.  Make it twice as big
    503 	 * as just the characters (so that we have room for cursor motions
    504 	 * and attribute information) but no more than 8K.
    505 	 */
    506 	if (screen->stdbuf == NULL) {
    507 		screen->len = LINES * COLS * 2;
    508 		if (screen->len > 8192)
    509 			screen->len = 8192;
    510 		if ((screen->stdbuf = malloc(screen->len)) == NULL)
    511 			screen->len = 0;
    512 	}
    513 	(void) setvbuf(screen->outfd, screen->stdbuf, _IOFBF, screen->len);
    514 
    515 	t_puts(screen->cursesi_genbuf, __tc_ti, 0, __cputchar_args,
    516 	       (void *) screen->outfd);
    517 	t_puts(screen->cursesi_genbuf, __tc_vs, 0, __cputchar_args,
    518 	       (void *) screen->outfd);
    519 	if (screen->curscr->flags & __KEYPAD)
    520 		t_puts(screen->cursesi_genbuf, __tc_ks, 0, __cputchar_args,
    521 		       (void *) screen->outfd);
    522 	screen->endwin = 0;
    523 }
    524 
    525 int
    526 endwin(void)
    527 {
    528 	return __stopwin();
    529 }
    530 
    531 bool
    532 isendwin(void)
    533 {
    534 	return (_cursesi_screen->endwin ? TRUE : FALSE);
    535 }
    536 
    537 int
    538 flushinp(void)
    539 {
    540 	(void) fpurge(_cursesi_screen->infd);
    541 	return (OK);
    542 }
    543 
    544 /*
    545  * The following routines, savetty and resetty are completely useless and
    546  * are left in only as stubs.  If people actually use them they will almost
    547  * certainly screw up the state of the world.
    548  */
    549 /*static struct termios savedtty;*/
    550 int
    551 savetty(void)
    552 {
    553 	if (_cursesi_screen->notty == TRUE)
    554 		return OK;
    555 	return (tcgetattr(fileno(_cursesi_screen->infd),
    556 			  &_cursesi_screen->savedtty) ? ERR : OK);
    557 }
    558 
    559 int
    560 resetty(void)
    561 {
    562 	if (_cursesi_screen->notty == TRUE)
    563 		return OK;
    564 	return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
    565 			  TCSASOFT | TCSADRAIN : TCSADRAIN,
    566 			  &_cursesi_screen->savedtty) ? ERR : OK);
    567 }
    568 
    569 /*
    570  * erasechar --
    571  *     Return the character of the erase key.
    572  *
    573  */
    574 char
    575 erasechar(void)
    576 {
    577 	if (_cursesi_screen->notty == TRUE)
    578 		return 0;
    579 	return _cursesi_screen->baset.c_cc[VERASE];
    580 }
    581 
    582 /*
    583  * killchar --
    584  *     Return the character of the kill key.
    585  */
    586 char
    587 killchar(void)
    588 {
    589 	if (_cursesi_screen->notty == TRUE)
    590 		return 0;
    591 	return _cursesi_screen->baset.c_cc[VKILL];
    592 }
    593