Home | History | Annotate | Line # | Download | only in libcurses
tty.c revision 1.29
      1 /*	$NetBSD: tty.c,v 1.29 2002/06/26 18:23:31 itojun 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.29 2002/06/26 18:23:31 itojun 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 	/* Check if we need to restart ... */
    173 	if (_cursesi_screen->endwin)
    174 		__restartwin();
    175 
    176 	_cursesi_screen->useraw = __pfast = __rawmode = 1;
    177 	_cursesi_screen->curt = &_cursesi_screen->rawt;
    178 	if (_cursesi_screen->notty == TRUE)
    179 		return OK;
    180 	return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
    181 			  TCSASOFT | TCSADRAIN : TCSADRAIN,
    182 			  _cursesi_screen->curt) ? ERR : OK);
    183 }
    184 
    185 int
    186 noraw(void)
    187 {
    188 	/* Check if we need to restart ... */
    189 	if (_cursesi_screen->endwin)
    190 		__restartwin();
    191 
    192 	_cursesi_screen->useraw = __pfast = __rawmode = 0;
    193 	if (_cursesi_screen->notty == TRUE)
    194 		return OK;
    195 	_cursesi_screen->curt = &_cursesi_screen->baset;
    196 	return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
    197 			  TCSASOFT | TCSADRAIN : TCSADRAIN,
    198 			  _cursesi_screen->curt) ? ERR : OK);
    199 }
    200 
    201 int
    202 cbreak(void)
    203 {
    204 	/* Check if we need to restart ... */
    205 	if (_cursesi_screen->endwin)
    206 		__restartwin();
    207 
    208 	__rawmode = 1;
    209 	if (_cursesi_screen->notty == TRUE)
    210 		return OK;
    211 	_cursesi_screen->curt = _cursesi_screen->useraw ?
    212 		&_cursesi_screen->rawt : &_cursesi_screen->cbreakt;
    213 	return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
    214 			  TCSASOFT | TCSADRAIN : TCSADRAIN,
    215 			  _cursesi_screen->curt) ? ERR : OK);
    216 }
    217 
    218 int
    219 nocbreak(void)
    220 {
    221 	/* Check if we need to restart ... */
    222 	if (_cursesi_screen->endwin)
    223 		__restartwin();
    224 
    225 	__rawmode = 0;
    226 	if (_cursesi_screen->notty == TRUE)
    227 		return OK;
    228 	_cursesi_screen->curt = _cursesi_screen->useraw ?
    229 		&_cursesi_screen->rawt : &_cursesi_screen->baset;
    230 	return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
    231 			  TCSASOFT | TCSADRAIN : TCSADRAIN,
    232 			  _cursesi_screen->curt) ? ERR : OK);
    233 }
    234 
    235 int
    236 __delay(void)
    237  {
    238 	/* Check if we need to restart ... */
    239 	if (_cursesi_screen->endwin)
    240 		__restartwin();
    241 
    242 	if (_cursesi_screen->notty == TRUE)
    243 		return OK;
    244 	_cursesi_screen->rawt.c_cc[VMIN] = 1;
    245 	_cursesi_screen->rawt.c_cc[VTIME] = 0;
    246 	_cursesi_screen->cbreakt.c_cc[VMIN] = 1;
    247 	_cursesi_screen->cbreakt.c_cc[VTIME] = 0;
    248 	_cursesi_screen->baset.c_cc[VMIN] = 1;
    249 	_cursesi_screen->baset.c_cc[VTIME] = 0;
    250 
    251 	return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
    252 		TCSASOFT : TCSANOW, _cursesi_screen->curt) ? ERR : OK);
    253 }
    254 
    255 int
    256 __nodelay(void)
    257 {
    258 	/* Check if we need to restart ... */
    259 	if (_cursesi_screen->endwin)
    260 		__restartwin();
    261 
    262 	if (_cursesi_screen->notty == TRUE)
    263 		return OK;
    264 	_cursesi_screen->rawt.c_cc[VMIN] = 0;
    265 	_cursesi_screen->rawt.c_cc[VTIME] = 0;
    266 	_cursesi_screen->cbreakt.c_cc[VMIN] = 0;
    267 	_cursesi_screen->cbreakt.c_cc[VTIME] = 0;
    268 	_cursesi_screen->baset.c_cc[VMIN] = 0;
    269 	_cursesi_screen->baset.c_cc[VTIME] = 0;
    270 
    271 	return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
    272 		TCSASOFT : TCSANOW, _cursesi_screen->curt) ? ERR : OK);
    273 }
    274 
    275 void
    276 __save_termios(void)
    277 {
    278 	/* Check if we need to restart ... */
    279 	if (_cursesi_screen->endwin)
    280 		__restartwin();
    281 
    282 	if (_cursesi_screen->notty == TRUE)
    283 		return;
    284 	_cursesi_screen->ovmin = _cursesi_screen->cbreakt.c_cc[VMIN];
    285 	_cursesi_screen->ovtime = _cursesi_screen->cbreakt.c_cc[VTIME];
    286 }
    287 
    288 void
    289 __restore_termios(void)
    290 {
    291 	/* Check if we need to restart ... */
    292 	if (_cursesi_screen->endwin)
    293 		__restartwin();
    294 
    295 	if (_cursesi_screen->notty == TRUE)
    296 		return;
    297 	_cursesi_screen->rawt.c_cc[VMIN] = _cursesi_screen->ovmin;
    298 	_cursesi_screen->rawt.c_cc[VTIME] = _cursesi_screen->ovtime;
    299 	_cursesi_screen->cbreakt.c_cc[VMIN] = _cursesi_screen->ovmin;
    300 	_cursesi_screen->cbreakt.c_cc[VTIME] = _cursesi_screen->ovtime;
    301 	_cursesi_screen->baset.c_cc[VMIN] = _cursesi_screen->ovmin;
    302 	_cursesi_screen->baset.c_cc[VTIME] = _cursesi_screen->ovtime;
    303 }
    304 
    305 int
    306 __timeout(int delay)
    307 {
    308 	/* Check if we need to restart ... */
    309 	if (_cursesi_screen->endwin)
    310 		__restartwin();
    311 
    312 	if (_cursesi_screen->notty == TRUE)
    313 		return OK;
    314 	_cursesi_screen->ovmin = _cursesi_screen->cbreakt.c_cc[VMIN];
    315 	_cursesi_screen->ovtime = _cursesi_screen->cbreakt.c_cc[VTIME];
    316 	_cursesi_screen->rawt.c_cc[VMIN] = 0;
    317 	_cursesi_screen->rawt.c_cc[VTIME] = delay;
    318 	_cursesi_screen->cbreakt.c_cc[VMIN] = 0;
    319 	_cursesi_screen->cbreakt.c_cc[VTIME] = delay;
    320 	_cursesi_screen->baset.c_cc[VMIN] = 0;
    321 	_cursesi_screen->baset.c_cc[VTIME] = delay;
    322 
    323 	return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
    324 			  TCSASOFT | TCSANOW : TCSANOW,
    325 			  _cursesi_screen->curt) ? ERR : OK);
    326 }
    327 
    328 int
    329 __notimeout(void)
    330 {
    331 	/* Check if we need to restart ... */
    332 	if (_cursesi_screen->endwin)
    333 		__restartwin();
    334 
    335 	if (_cursesi_screen->notty == TRUE)
    336 		return OK;
    337 	_cursesi_screen->rawt.c_cc[VMIN] = 1;
    338 	_cursesi_screen->rawt.c_cc[VTIME] = 0;
    339 	_cursesi_screen->cbreakt.c_cc[VMIN] = 1;
    340 	_cursesi_screen->cbreakt.c_cc[VTIME] = 0;
    341 	_cursesi_screen->baset.c_cc[VMIN] = 1;
    342 	_cursesi_screen->baset.c_cc[VTIME] = 0;
    343 
    344 	return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
    345 			  TCSASOFT | TCSANOW : TCSANOW,
    346 			  _cursesi_screen->curt) ? ERR : OK);
    347 }
    348 
    349 int
    350 echo(void)
    351 {
    352 	/* Check if we need to restart ... */
    353 	if (_cursesi_screen->endwin)
    354 		__restartwin();
    355 
    356 	__echoit = 1;
    357 	return (OK);
    358 }
    359 
    360 int
    361 noecho(void)
    362 {
    363 	/* Check if we need to restart ... */
    364 	if (_cursesi_screen->endwin)
    365 		__restartwin();
    366 
    367 	__echoit = 0;
    368 	return (OK);
    369 }
    370 
    371 int
    372 nl(void)
    373 {
    374 	/* Check if we need to restart ... */
    375 	if (_cursesi_screen->endwin)
    376 		__restartwin();
    377 
    378 	if (_cursesi_screen->notty == TRUE)
    379 		return OK;
    380 	_cursesi_screen->rawt.c_iflag |= ICRNL;
    381 	_cursesi_screen->rawt.c_oflag |= ONLCR;
    382 	_cursesi_screen->cbreakt.c_iflag |= ICRNL;
    383 	_cursesi_screen->cbreakt.c_oflag |= ONLCR;
    384 	_cursesi_screen->baset.c_iflag |= ICRNL;
    385 	_cursesi_screen->baset.c_oflag |= ONLCR;
    386 
    387 	_cursesi_screen->pfast = _cursesi_screen->rawmode;
    388 	return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
    389 			  TCSASOFT | TCSADRAIN : TCSADRAIN,
    390 			  _cursesi_screen->curt) ? ERR : OK);
    391 }
    392 
    393 int
    394 nonl(void)
    395 {
    396 	/* Check if we need to restart ... */
    397 	if (_cursesi_screen->endwin)
    398 		__restartwin();
    399 
    400 	if (_cursesi_screen->notty == TRUE)
    401 		return OK;
    402 	_cursesi_screen->rawt.c_iflag &= ~ICRNL;
    403 	_cursesi_screen->rawt.c_oflag &= ~ONLCR;
    404 	_cursesi_screen->cbreakt.c_iflag &= ~ICRNL;
    405 	_cursesi_screen->cbreakt.c_oflag &= ~ONLCR;
    406 	_cursesi_screen->baset.c_iflag &= ~ICRNL;
    407 	_cursesi_screen->baset.c_oflag &= ~ONLCR;
    408 
    409 	__pfast = 1;
    410 	return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
    411 			  TCSASOFT | TCSADRAIN : TCSADRAIN,
    412 			  _cursesi_screen->curt) ? ERR : OK);
    413 }
    414 
    415 int
    416 intrflush(WINDOW *win, bool bf)	/*ARGSUSED*/
    417 {
    418 	/* Check if we need to restart ... */
    419 	if (_cursesi_screen->endwin)
    420 		__restartwin();
    421 
    422 	if (_cursesi_screen->notty == TRUE)
    423 		return OK;
    424 	if (bf) {
    425 		_cursesi_screen->rawt.c_lflag &= ~NOFLSH;
    426 		_cursesi_screen->cbreakt.c_lflag &= ~NOFLSH;
    427 		_cursesi_screen->baset.c_lflag &= ~NOFLSH;
    428 	} else {
    429 		_cursesi_screen->rawt.c_lflag |= NOFLSH;
    430 		_cursesi_screen->cbreakt.c_lflag |= NOFLSH;
    431 		_cursesi_screen->baset.c_lflag |= NOFLSH;
    432 	}
    433 
    434 	__pfast = 1;
    435 	return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
    436 			  TCSASOFT | TCSADRAIN : TCSADRAIN,
    437 			  _cursesi_screen->curt) ? ERR : OK);
    438 }
    439 
    440 void
    441 __startwin(SCREEN *screen)
    442 {
    443 
    444 	(void) fflush(screen->infd);
    445 
    446 	/*
    447 	 * Some C libraries default to a 1K buffer when talking to a tty.
    448 	 * With a larger screen, especially across a network, we'd like
    449 	 * to get it to all flush in a single write.  Make it twice as big
    450 	 * as just the characters (so that we have room for cursor motions
    451 	 * and attribute information) but no more than 8K.
    452 	 */
    453 	if (screen->stdbuf == NULL) {
    454 		screen->len = LINES * COLS * 2;
    455 		if (screen->len > 8192)
    456 			screen->len = 8192;
    457 		if ((screen->stdbuf = malloc(screen->len)) == NULL)
    458 			screen->len = 0;
    459 	}
    460 	(void) setvbuf(screen->outfd, screen->stdbuf, _IOFBF, screen->len);
    461 
    462 	t_puts(screen->cursesi_genbuf, __tc_ti, 0, __cputchar_args,
    463 	       (void *) screen->outfd);
    464 	t_puts(screen->cursesi_genbuf, __tc_vs, 0, __cputchar_args,
    465 	       (void *) screen->outfd);
    466 	if (screen->curscr->flags & __KEYPAD)
    467 		t_puts(screen->cursesi_genbuf, __tc_ks, 0, __cputchar_args,
    468 		       (void *) screen->outfd);
    469 	screen->endwin = 0;
    470 }
    471 
    472 int
    473 endwin(void)
    474 {
    475 	return __stopwin();
    476 }
    477 
    478 bool
    479 isendwin(void)
    480 {
    481 	return (_cursesi_screen->endwin ? TRUE : FALSE);
    482 }
    483 
    484 int
    485 flushinp(void)
    486 {
    487 	(void) fpurge(_cursesi_screen->infd);
    488 	return (OK);
    489 }
    490 
    491 /*
    492  * The following routines, savetty and resetty are completely useless and
    493  * are left in only as stubs.  If people actually use them they will almost
    494  * certainly screw up the state of the world.
    495  */
    496 /*static struct termios savedtty;*/
    497 int
    498 savetty(void)
    499 {
    500 	if (_cursesi_screen->notty == TRUE)
    501 		return OK;
    502 	return (tcgetattr(fileno(_cursesi_screen->infd),
    503 			  &_cursesi_screen->savedtty) ? ERR : OK);
    504 }
    505 
    506 int
    507 resetty(void)
    508 {
    509 	if (_cursesi_screen->notty == TRUE)
    510 		return OK;
    511 	return (tcsetattr(fileno(_cursesi_screen->infd), __tcaction ?
    512 			  TCSASOFT | TCSADRAIN : TCSADRAIN,
    513 			  &_cursesi_screen->savedtty) ? ERR : OK);
    514 }
    515 
    516 /*
    517  * erasechar --
    518  *     Return the character of the erase key.
    519  *
    520  */
    521 char
    522 erasechar(void)
    523 {
    524 	if (_cursesi_screen->notty == TRUE)
    525 		return 0;
    526 	return _cursesi_screen->baset.c_cc[VERASE];
    527 }
    528 
    529 /*
    530  * killchar --
    531  *     Return the character of the kill key.
    532  */
    533 char
    534 killchar(void)
    535 {
    536 	if (_cursesi_screen->notty == TRUE)
    537 		return 0;
    538 	return _cursesi_screen->baset.c_cc[VKILL];
    539 }
    540