Home | History | Annotate | Line # | Download | only in libcurses
tty.c revision 1.46.14.1
      1  1.46.14.1  christos /*	$NetBSD: tty.c,v 1.46.14.1 2019/06/10 22:05:22 christos Exp $	*/
      2        1.7     mikel 
      3        1.1   mycroft /*-
      4        1.6       cgd  * Copyright (c) 1992, 1993, 1994
      5        1.2       cgd  *	The Regents of the University of California.  All rights reserved.
      6        1.1   mycroft  *
      7        1.1   mycroft  * Redistribution and use in source and binary forms, with or without
      8        1.1   mycroft  * modification, are permitted provided that the following conditions
      9        1.1   mycroft  * are met:
     10        1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     11        1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     12        1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     14        1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     15       1.35       agc  * 3. Neither the name of the University nor the names of its contributors
     16        1.1   mycroft  *    may be used to endorse or promote products derived from this software
     17        1.1   mycroft  *    without specific prior written permission.
     18        1.1   mycroft  *
     19        1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20        1.1   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21        1.1   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22        1.1   mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23        1.1   mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24        1.1   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25        1.1   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26        1.1   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27        1.1   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28        1.1   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29        1.1   mycroft  * SUCH DAMAGE.
     30        1.1   mycroft  */
     31        1.1   mycroft 
     32        1.7     mikel #include <sys/cdefs.h>
     33        1.1   mycroft #ifndef lint
     34        1.7     mikel #if 0
     35       1.10     perry static char sccsid[] = "@(#)tty.c	8.6 (Berkeley) 1/10/95";
     36        1.7     mikel #else
     37  1.46.14.1  christos __RCSID("$NetBSD: tty.c,v 1.46.14.1 2019/06/10 22:05:22 christos Exp $");
     38        1.7     mikel #endif
     39       1.11       mrg #endif				/* not lint */
     40       1.11       mrg 
     41  1.46.14.1  christos #include <sys/fcntl.h>
     42  1.46.14.1  christos #include <sys/ioctl.h>
     43  1.46.14.1  christos #include <sys/param.h>
     44       1.11       mrg #include <sys/types.h>
     45        1.1   mycroft 
     46        1.6       cgd #include <stdlib.h>
     47        1.1   mycroft #include <termios.h>
     48        1.1   mycroft #include <unistd.h>
     49        1.1   mycroft 
     50        1.6       cgd #include "curses.h"
     51       1.14       jdc #include "curses_private.h"
     52        1.6       cgd 
     53        1.2       cgd /*
     54        1.2       cgd  * In general, curses should leave tty hardware settings alone (speed, parity,
     55        1.2       cgd  * word size).  This is most easily done in BSD by using TCSASOFT on all
     56        1.2       cgd  * tcsetattr calls.  On other systems, it would be better to get and restore
     57        1.2       cgd  * those attributes at each change, or at least when stopped and restarted.
     58        1.2       cgd  * See also the comments in getterm().
     59        1.2       cgd  */
     60       1.43  christos #ifndef TCSASOFT
     61       1.43  christos #define	TCSASOFT	0
     62        1.2       cgd #endif
     63        1.2       cgd 
     64       1.43  christos int __tcaction = TCSASOFT != 0;		/* Ignore hardware settings */
     65       1.43  christos 
     66        1.2       cgd #ifndef	OXTABS
     67        1.2       cgd #ifdef	XTABS			/* SMI uses XTABS. */
     68        1.2       cgd #define	OXTABS	XTABS
     69        1.2       cgd #else
     70        1.2       cgd #define	OXTABS	0
     71        1.2       cgd #endif
     72        1.2       cgd #endif
     73       1.26  christos 
     74       1.26  christos /*
     75       1.26  christos  * baudrate --
     76       1.26  christos  *	Return the current baudrate
     77       1.26  christos  */
     78       1.26  christos int
     79       1.26  christos baudrate(void)
     80       1.26  christos {
     81       1.46       roy 
     82       1.29    itojun 	if (_cursesi_screen->notty == TRUE)
     83       1.29    itojun 		return 0;
     84       1.29    itojun 
     85       1.29    itojun 	return cfgetospeed(&_cursesi_screen->baset);
     86       1.26  christos }
     87        1.2       cgd 
     88        1.1   mycroft /*
     89        1.1   mycroft  * gettmode --
     90        1.1   mycroft  *	Do terminal type initialization.
     91        1.1   mycroft  */
     92        1.1   mycroft int
     93       1.15     blymn gettmode(void)
     94        1.1   mycroft {
     95       1.46       roy 
     96       1.25     blymn 	if (_cursesi_gettmode(_cursesi_screen) == ERR)
     97       1.25     blymn 		return ERR;
     98       1.11       mrg 
     99       1.25     blymn 	__GT = _cursesi_screen->GT;
    100       1.25     blymn 	__NONL = _cursesi_screen->NONL;
    101       1.25     blymn 	return OK;
    102       1.25     blymn }
    103       1.25     blymn 
    104       1.25     blymn /*
    105       1.25     blymn  * _cursesi_gettmode --
    106       1.25     blymn  *      Do the terminal type initialisation for the tty attached to the
    107       1.25     blymn  *  given screen.
    108       1.25     blymn  */
    109       1.25     blymn int
    110       1.25     blymn _cursesi_gettmode(SCREEN *screen)
    111       1.25     blymn {
    112       1.25     blymn 	screen->useraw = 0;
    113       1.25     blymn 
    114       1.29    itojun 	if (tcgetattr(fileno(screen->infd), &screen->orig_termios)) {
    115       1.29    itojun 		/* if the input fd is not a tty try the output */
    116       1.29    itojun 		if (tcgetattr(fileno(screen->infd), &screen->orig_termios)) {
    117       1.29    itojun 			/* not a tty ... we will disable tty related stuff */
    118       1.29    itojun 			screen->notty = TRUE;
    119       1.29    itojun 			__GT = 0;
    120       1.29    itojun 			__NONL = 0;
    121       1.43  christos 			return OK;
    122       1.29    itojun 		}
    123       1.29    itojun 	}
    124        1.2       cgd 
    125       1.25     blymn 	screen->baset = screen->orig_termios;
    126       1.25     blymn 	screen->baset.c_oflag &= ~OXTABS;
    127       1.28     blymn 
    128       1.25     blymn 	screen->GT = 0;	/* historical. was used before we wired OXTABS off */
    129       1.25     blymn 	screen->NONL = (screen->baset.c_oflag & ONLCR) == 0;
    130        1.1   mycroft 
    131        1.2       cgd 	/*
    132        1.2       cgd 	 * XXX
    133        1.2       cgd 	 * System V and SMI systems overload VMIN and VTIME, such that
    134        1.2       cgd 	 * VMIN is the same as the VEOF element, and VTIME is the same
    135        1.2       cgd 	 * as the VEOL element.  This means that, if VEOF was ^D, the
    136        1.2       cgd 	 * default VMIN is 4.  Majorly stupid.
    137        1.2       cgd 	 */
    138       1.25     blymn 	screen->cbreakt = screen->baset;
    139       1.25     blymn 	screen->cbreakt.c_lflag &= ~(ECHO | ECHONL | ICANON);
    140       1.25     blymn 	screen->cbreakt.c_cc[VMIN] = 1;
    141       1.25     blymn 	screen->cbreakt.c_cc[VTIME] = 0;
    142       1.25     blymn 
    143       1.25     blymn 	screen->rawt = screen->cbreakt;
    144       1.25     blymn 	screen->rawt.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | INLCR | IGNCR |
    145       1.25     blymn 				  ICRNL | IXON);
    146       1.25     blymn 	screen->rawt.c_oflag &= ~OPOST;
    147       1.25     blymn 	screen->rawt.c_lflag &= ~(ISIG | IEXTEN);
    148        1.2       cgd 
    149       1.43  christos #if TCSASOFT == 0
    150        1.2       cgd 	/*
    151        1.2       cgd 	 * In general, curses should leave hardware-related settings alone.
    152        1.2       cgd 	 * This includes parity and word size.  Older versions set the tty
    153        1.2       cgd 	 * to 8 bits, no parity in raw(), but this is considered to be an
    154        1.2       cgd 	 * artifact of the old tty interface.  If it's desired to change
    155        1.2       cgd 	 * parity and word size, the TCSASOFT bit has to be removed from the
    156        1.2       cgd 	 * calls that switch to/from "raw" mode.
    157        1.2       cgd 	 */
    158       1.43  christos 	screen->rawt.c_iflag &= ~ISTRIP;
    159       1.43  christos 	screen->rawt.c_cflag &= ~(CSIZE | PARENB);
    160       1.43  christos 	screen->rawt.c_cflag |= CS8;
    161       1.43  christos #endif
    162        1.1   mycroft 
    163       1.25     blymn 	screen->curt = &screen->baset;
    164       1.43  christos 	return tcsetattr(fileno(screen->infd), TCSASOFT | TCSADRAIN,
    165       1.43  christos 	    screen->curt) ? ERR : OK;
    166        1.1   mycroft }
    167        1.1   mycroft 
    168       1.36       wiz /*
    169       1.36       wiz  * raw --
    170       1.36       wiz  *	Put the terminal into raw mode
    171       1.36       wiz  */
    172        1.1   mycroft int
    173       1.15     blymn raw(void)
    174        1.1   mycroft {
    175       1.32       jdc #ifdef DEBUG
    176       1.39       jdc 	__CTRACE(__CTRACE_MISC, "raw()\n");
    177       1.32       jdc #endif
    178        1.9      phil 	/* Check if we need to restart ... */
    179       1.25     blymn 	if (_cursesi_screen->endwin)
    180       1.23       jdc 		__restartwin();
    181       1.11       mrg 
    182       1.25     blymn 	_cursesi_screen->useraw = __pfast = __rawmode = 1;
    183       1.25     blymn 	_cursesi_screen->curt = &_cursesi_screen->rawt;
    184       1.29    itojun 	if (_cursesi_screen->notty == TRUE)
    185       1.29    itojun 		return OK;
    186       1.43  christos 	return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN,
    187       1.43  christos 	    _cursesi_screen->curt) ? ERR : OK;
    188        1.1   mycroft }
    189        1.1   mycroft 
    190       1.36       wiz /*
    191       1.36       wiz  * noraw --
    192       1.36       wiz  *	Put the terminal into cooked mode
    193       1.36       wiz  */
    194        1.1   mycroft int
    195       1.15     blymn noraw(void)
    196        1.1   mycroft {
    197       1.32       jdc #ifdef DEBUG
    198       1.39       jdc 	__CTRACE(__CTRACE_MISC, "noraw()\n");
    199       1.32       jdc #endif
    200        1.9      phil 	/* Check if we need to restart ... */
    201       1.25     blymn 	if (_cursesi_screen->endwin)
    202       1.23       jdc 		__restartwin();
    203       1.11       mrg 
    204       1.25     blymn 	_cursesi_screen->useraw = __pfast = __rawmode = 0;
    205       1.29    itojun 	if (_cursesi_screen->notty == TRUE)
    206       1.29    itojun 		return OK;
    207       1.25     blymn 	_cursesi_screen->curt = &_cursesi_screen->baset;
    208       1.43  christos 	return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN,
    209       1.43  christos 	    _cursesi_screen->curt) ? ERR : OK;
    210        1.1   mycroft }
    211        1.1   mycroft 
    212       1.36       wiz /*
    213       1.36       wiz  * cbreak --
    214       1.36       wiz  * 	Enable cbreak mode
    215       1.36       wiz  */
    216        1.1   mycroft int
    217       1.15     blymn cbreak(void)
    218        1.1   mycroft {
    219       1.32       jdc #ifdef DEBUG
    220       1.39       jdc 	__CTRACE(__CTRACE_MISC, "cbreak()\n");
    221       1.32       jdc #endif
    222        1.9      phil 	/* Check if we need to restart ... */
    223       1.25     blymn 	if (_cursesi_screen->endwin)
    224       1.23       jdc 		__restartwin();
    225       1.11       mrg 
    226        1.1   mycroft 	__rawmode = 1;
    227       1.29    itojun 	if (_cursesi_screen->notty == TRUE)
    228       1.29    itojun 		return OK;
    229       1.25     blymn 	_cursesi_screen->curt = _cursesi_screen->useraw ?
    230       1.25     blymn 		&_cursesi_screen->rawt : &_cursesi_screen->cbreakt;
    231       1.43  christos 	return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN,
    232       1.43  christos 	    _cursesi_screen->curt) ? ERR : OK;
    233        1.1   mycroft }
    234        1.1   mycroft 
    235       1.36       wiz /*
    236       1.36       wiz  * nocbreak --
    237       1.36       wiz  *	Disable cbreak mode
    238       1.36       wiz  */
    239        1.1   mycroft int
    240       1.15     blymn nocbreak(void)
    241        1.1   mycroft {
    242       1.32       jdc #ifdef DEBUG
    243       1.39       jdc 	__CTRACE(__CTRACE_MISC, "nocbreak()\n");
    244       1.32       jdc #endif
    245        1.9      phil 	/* Check if we need to restart ... */
    246       1.25     blymn 	if (_cursesi_screen->endwin)
    247       1.23       jdc 		__restartwin();
    248        1.1   mycroft 
    249        1.1   mycroft 	__rawmode = 0;
    250       1.29    itojun 	if (_cursesi_screen->notty == TRUE)
    251       1.29    itojun 		return OK;
    252       1.30     blymn 	  /* if we were in halfdelay mode then nuke the timeout */
    253  1.46.14.1  christos 	if ((stdscr->flags & __HALFDELAY) &&
    254       1.30     blymn 	    (__notimeout() == ERR))
    255       1.30     blymn 		return ERR;
    256       1.30     blymn 
    257  1.46.14.1  christos 	stdscr->flags &= ~__HALFDELAY;
    258       1.25     blymn 	_cursesi_screen->curt = _cursesi_screen->useraw ?
    259       1.25     blymn 		&_cursesi_screen->rawt : &_cursesi_screen->baset;
    260       1.43  christos 	return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN,
    261       1.43  christos 	    _cursesi_screen->curt) ? ERR : OK;
    262        1.1   mycroft }
    263       1.11       mrg 
    264       1.30     blymn /*
    265       1.30     blymn  * halfdelay --
    266       1.30     blymn  *    Put the terminal into cbreak mode with the specified timeout.
    267       1.30     blymn  *
    268       1.30     blymn  */
    269       1.30     blymn int
    270       1.31    atatat halfdelay(int duration)
    271       1.30     blymn {
    272       1.31    atatat 	if ((duration < 1) || (duration > 255))
    273       1.30     blymn 		return ERR;
    274       1.30     blymn 
    275       1.30     blymn 	if (cbreak() == ERR)
    276       1.30     blymn 		return ERR;
    277       1.30     blymn 
    278  1.46.14.1  christos 	if (duration > 255)
    279  1.46.14.1  christos 		stdscr->delay = 255;
    280  1.46.14.1  christos 	else
    281  1.46.14.1  christos 		stdscr->delay = duration;
    282       1.30     blymn 
    283  1.46.14.1  christos 	stdscr->flags |= __HALFDELAY;
    284       1.30     blymn 	return OK;
    285       1.30     blymn }
    286       1.46       roy 
    287       1.11       mrg int
    288       1.15     blymn __delay(void)
    289       1.11       mrg  {
    290       1.37  christos #ifdef DEBUG
    291       1.39       jdc 	__CTRACE(__CTRACE_MISC, "__delay()\n");
    292       1.37  christos #endif
    293       1.11       mrg 	/* Check if we need to restart ... */
    294       1.25     blymn 	if (_cursesi_screen->endwin)
    295       1.23       jdc 		__restartwin();
    296       1.11       mrg 
    297       1.29    itojun 	if (_cursesi_screen->notty == TRUE)
    298       1.29    itojun 		return OK;
    299       1.25     blymn 	_cursesi_screen->rawt.c_cc[VMIN] = 1;
    300       1.25     blymn 	_cursesi_screen->rawt.c_cc[VTIME] = 0;
    301       1.25     blymn 	_cursesi_screen->cbreakt.c_cc[VMIN] = 1;
    302       1.25     blymn 	_cursesi_screen->cbreakt.c_cc[VTIME] = 0;
    303       1.25     blymn 	_cursesi_screen->baset.c_cc[VMIN] = 1;
    304       1.25     blymn 	_cursesi_screen->baset.c_cc[VTIME] = 0;
    305       1.11       mrg 
    306       1.43  christos 	if (tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSANOW,
    307       1.43  christos 	    _cursesi_screen->curt)) {
    308       1.41       dsl 		__restore_termios();
    309       1.41       dsl 		return ERR;
    310       1.41       dsl 	}
    311       1.41       dsl 
    312       1.41       dsl 	return OK;
    313       1.11       mrg }
    314       1.11       mrg 
    315       1.11       mrg int
    316       1.15     blymn __nodelay(void)
    317       1.11       mrg {
    318       1.37  christos #ifdef DEBUG
    319       1.39       jdc 	__CTRACE(__CTRACE_MISC, "__nodelay()\n");
    320       1.37  christos #endif
    321       1.11       mrg 	/* Check if we need to restart ... */
    322       1.25     blymn 	if (_cursesi_screen->endwin)
    323       1.23       jdc 		__restartwin();
    324       1.11       mrg 
    325       1.29    itojun 	if (_cursesi_screen->notty == TRUE)
    326       1.29    itojun 		return OK;
    327       1.25     blymn 	_cursesi_screen->rawt.c_cc[VMIN] = 0;
    328       1.25     blymn 	_cursesi_screen->rawt.c_cc[VTIME] = 0;
    329       1.25     blymn 	_cursesi_screen->cbreakt.c_cc[VMIN] = 0;
    330       1.25     blymn 	_cursesi_screen->cbreakt.c_cc[VTIME] = 0;
    331       1.25     blymn 	_cursesi_screen->baset.c_cc[VMIN] = 0;
    332       1.25     blymn 	_cursesi_screen->baset.c_cc[VTIME] = 0;
    333       1.11       mrg 
    334       1.43  christos 	if (tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSANOW,
    335       1.43  christos 	    _cursesi_screen->curt)) {
    336       1.41       dsl 		__restore_termios();
    337       1.41       dsl 		return ERR;
    338       1.41       dsl 	}
    339       1.41       dsl 
    340       1.41       dsl 	return OK;
    341       1.11       mrg }
    342       1.11       mrg 
    343       1.11       mrg void
    344       1.15     blymn __save_termios(void)
    345       1.11       mrg {
    346       1.11       mrg 	/* Check if we need to restart ... */
    347       1.25     blymn 	if (_cursesi_screen->endwin)
    348       1.23       jdc 		__restartwin();
    349       1.11       mrg 
    350       1.29    itojun 	if (_cursesi_screen->notty == TRUE)
    351       1.29    itojun 		return;
    352       1.25     blymn 	_cursesi_screen->ovmin = _cursesi_screen->cbreakt.c_cc[VMIN];
    353       1.25     blymn 	_cursesi_screen->ovtime = _cursesi_screen->cbreakt.c_cc[VTIME];
    354       1.11       mrg }
    355       1.11       mrg 
    356       1.11       mrg void
    357       1.15     blymn __restore_termios(void)
    358       1.11       mrg {
    359       1.11       mrg 	/* Check if we need to restart ... */
    360       1.25     blymn 	if (_cursesi_screen->endwin)
    361       1.23       jdc 		__restartwin();
    362       1.11       mrg 
    363       1.29    itojun 	if (_cursesi_screen->notty == TRUE)
    364       1.29    itojun 		return;
    365       1.25     blymn 	_cursesi_screen->rawt.c_cc[VMIN] = _cursesi_screen->ovmin;
    366       1.25     blymn 	_cursesi_screen->rawt.c_cc[VTIME] = _cursesi_screen->ovtime;
    367       1.25     blymn 	_cursesi_screen->cbreakt.c_cc[VMIN] = _cursesi_screen->ovmin;
    368       1.25     blymn 	_cursesi_screen->cbreakt.c_cc[VTIME] = _cursesi_screen->ovtime;
    369       1.25     blymn 	_cursesi_screen->baset.c_cc[VMIN] = _cursesi_screen->ovmin;
    370       1.25     blymn 	_cursesi_screen->baset.c_cc[VTIME] = _cursesi_screen->ovtime;
    371       1.11       mrg }
    372       1.11       mrg 
    373       1.11       mrg int
    374       1.15     blymn __timeout(int delay)
    375       1.11       mrg {
    376       1.37  christos #ifdef DEBUG
    377       1.39       jdc 	__CTRACE(__CTRACE_MISC, "__timeout()\n");
    378       1.37  christos #endif
    379       1.11       mrg 	/* Check if we need to restart ... */
    380       1.25     blymn 	if (_cursesi_screen->endwin)
    381       1.23       jdc 		__restartwin();
    382       1.11       mrg 
    383       1.29    itojun 	if (_cursesi_screen->notty == TRUE)
    384       1.29    itojun 		return OK;
    385       1.25     blymn 	_cursesi_screen->ovmin = _cursesi_screen->cbreakt.c_cc[VMIN];
    386       1.25     blymn 	_cursesi_screen->ovtime = _cursesi_screen->cbreakt.c_cc[VTIME];
    387       1.25     blymn 	_cursesi_screen->rawt.c_cc[VMIN] = 0;
    388       1.25     blymn 	_cursesi_screen->rawt.c_cc[VTIME] = delay;
    389       1.25     blymn 	_cursesi_screen->cbreakt.c_cc[VMIN] = 0;
    390       1.25     blymn 	_cursesi_screen->cbreakt.c_cc[VTIME] = delay;
    391       1.25     blymn 	_cursesi_screen->baset.c_cc[VMIN] = 0;
    392       1.25     blymn 	_cursesi_screen->baset.c_cc[VTIME] = delay;
    393       1.25     blymn 
    394       1.43  christos 	if (tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSANOW,
    395       1.43  christos 	    _cursesi_screen->curt)) {
    396       1.41       dsl 		__restore_termios();
    397       1.41       dsl 		return ERR;
    398       1.41       dsl 	}
    399       1.41       dsl 
    400       1.41       dsl 	return OK;
    401       1.11       mrg }
    402       1.11       mrg 
    403       1.11       mrg int
    404       1.15     blymn __notimeout(void)
    405       1.11       mrg {
    406       1.37  christos #ifdef DEBUG
    407       1.39       jdc 	__CTRACE(__CTRACE_MISC, "__notimeout()\n");
    408       1.37  christos #endif
    409       1.11       mrg 	/* Check if we need to restart ... */
    410       1.25     blymn 	if (_cursesi_screen->endwin)
    411       1.23       jdc 		__restartwin();
    412       1.11       mrg 
    413       1.29    itojun 	if (_cursesi_screen->notty == TRUE)
    414       1.29    itojun 		return OK;
    415       1.25     blymn 	_cursesi_screen->rawt.c_cc[VMIN] = 1;
    416       1.25     blymn 	_cursesi_screen->rawt.c_cc[VTIME] = 0;
    417       1.25     blymn 	_cursesi_screen->cbreakt.c_cc[VMIN] = 1;
    418       1.25     blymn 	_cursesi_screen->cbreakt.c_cc[VTIME] = 0;
    419       1.25     blymn 	_cursesi_screen->baset.c_cc[VMIN] = 1;
    420       1.25     blymn 	_cursesi_screen->baset.c_cc[VTIME] = 0;
    421       1.25     blymn 
    422       1.43  christos 	return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSANOW,
    423       1.43  christos 	    _cursesi_screen->curt) ? ERR : OK;
    424       1.11       mrg }
    425       1.11       mrg 
    426        1.1   mycroft int
    427       1.15     blymn echo(void)
    428        1.1   mycroft {
    429       1.32       jdc #ifdef DEBUG
    430       1.39       jdc 	__CTRACE(__CTRACE_MISC, "echo()\n");
    431       1.32       jdc #endif
    432        1.9      phil 	/* Check if we need to restart ... */
    433       1.25     blymn 	if (_cursesi_screen->endwin)
    434       1.23       jdc 		__restartwin();
    435        1.9      phil 
    436        1.1   mycroft 	__echoit = 1;
    437       1.43  christos 	return OK;
    438        1.1   mycroft }
    439        1.1   mycroft 
    440        1.1   mycroft int
    441       1.15     blymn noecho(void)
    442        1.1   mycroft {
    443       1.32       jdc #ifdef DEBUG
    444       1.39       jdc 	__CTRACE(__CTRACE_MISC, "noecho()\n");
    445       1.32       jdc #endif
    446        1.9      phil 	/* Check if we need to restart ... */
    447       1.25     blymn 	if (_cursesi_screen->endwin)
    448       1.23       jdc 		__restartwin();
    449        1.9      phil 
    450        1.1   mycroft 	__echoit = 0;
    451       1.43  christos 	return OK;
    452        1.1   mycroft }
    453        1.1   mycroft 
    454        1.1   mycroft int
    455       1.15     blymn nl(void)
    456        1.1   mycroft {
    457       1.32       jdc #ifdef DEBUG
    458       1.39       jdc 	__CTRACE(__CTRACE_MISC, "nl()\n");
    459       1.32       jdc #endif
    460        1.9      phil 	/* Check if we need to restart ... */
    461       1.25     blymn 	if (_cursesi_screen->endwin)
    462       1.23       jdc 		__restartwin();
    463        1.9      phil 
    464       1.29    itojun 	if (_cursesi_screen->notty == TRUE)
    465       1.29    itojun 		return OK;
    466       1.25     blymn 	_cursesi_screen->rawt.c_iflag |= ICRNL;
    467       1.25     blymn 	_cursesi_screen->rawt.c_oflag |= ONLCR;
    468       1.25     blymn 	_cursesi_screen->cbreakt.c_iflag |= ICRNL;
    469       1.25     blymn 	_cursesi_screen->cbreakt.c_oflag |= ONLCR;
    470       1.25     blymn 	_cursesi_screen->baset.c_iflag |= ICRNL;
    471       1.25     blymn 	_cursesi_screen->baset.c_oflag |= ONLCR;
    472       1.25     blymn 
    473       1.32       jdc 	_cursesi_screen->nl = 1;
    474       1.25     blymn 	_cursesi_screen->pfast = _cursesi_screen->rawmode;
    475       1.43  christos 	return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN,
    476       1.43  christos 	    _cursesi_screen->curt) ? ERR : OK;
    477        1.1   mycroft }
    478        1.1   mycroft 
    479        1.1   mycroft int
    480       1.15     blymn nonl(void)
    481        1.1   mycroft {
    482       1.32       jdc #ifdef DEBUG
    483       1.39       jdc 	__CTRACE(__CTRACE_MISC, "nonl()\n");
    484       1.32       jdc #endif
    485        1.9      phil 	/* Check if we need to restart ... */
    486       1.25     blymn 	if (_cursesi_screen->endwin)
    487       1.23       jdc 		__restartwin();
    488        1.9      phil 
    489       1.29    itojun 	if (_cursesi_screen->notty == TRUE)
    490       1.29    itojun 		return OK;
    491       1.25     blymn 	_cursesi_screen->rawt.c_iflag &= ~ICRNL;
    492       1.25     blymn 	_cursesi_screen->rawt.c_oflag &= ~ONLCR;
    493       1.25     blymn 	_cursesi_screen->cbreakt.c_iflag &= ~ICRNL;
    494       1.25     blymn 	_cursesi_screen->cbreakt.c_oflag &= ~ONLCR;
    495       1.25     blymn 	_cursesi_screen->baset.c_iflag &= ~ICRNL;
    496       1.25     blymn 	_cursesi_screen->baset.c_oflag &= ~ONLCR;
    497        1.1   mycroft 
    498       1.32       jdc 	_cursesi_screen->nl = 0;
    499        1.1   mycroft 	__pfast = 1;
    500       1.43  christos 	return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN,
    501       1.43  christos 	    _cursesi_screen->curt) ? ERR : OK;
    502        1.2       cgd }
    503       1.33       jdc 
    504       1.33       jdc #ifndef _CURSES_USE_MACROS
    505       1.33       jdc void
    506       1.33       jdc noqiflush(void)
    507       1.33       jdc {
    508       1.46       roy 
    509       1.46       roy 	(void)intrflush(stdscr, FALSE);
    510       1.33       jdc }
    511       1.33       jdc 
    512       1.33       jdc void
    513       1.33       jdc qiflush(void)
    514       1.33       jdc {
    515       1.46       roy 
    516       1.46       roy 	(void)intrflush(stdscr, TRUE);
    517       1.33       jdc }
    518       1.33       jdc #endif	/* _CURSES_USE_MACROS */
    519        1.2       cgd 
    520       1.44  christos /*ARGSUSED*/
    521       1.14       jdc int
    522       1.44  christos intrflush(WINDOW *win, bool bf)
    523       1.14       jdc {
    524       1.14       jdc 	/* Check if we need to restart ... */
    525       1.25     blymn 	if (_cursesi_screen->endwin)
    526       1.23       jdc 		__restartwin();
    527       1.14       jdc 
    528       1.29    itojun 	if (_cursesi_screen->notty == TRUE)
    529       1.29    itojun 		return OK;
    530       1.14       jdc 	if (bf) {
    531       1.25     blymn 		_cursesi_screen->rawt.c_lflag &= ~NOFLSH;
    532       1.25     blymn 		_cursesi_screen->cbreakt.c_lflag &= ~NOFLSH;
    533       1.25     blymn 		_cursesi_screen->baset.c_lflag &= ~NOFLSH;
    534       1.14       jdc 	} else {
    535       1.25     blymn 		_cursesi_screen->rawt.c_lflag |= NOFLSH;
    536       1.25     blymn 		_cursesi_screen->cbreakt.c_lflag |= NOFLSH;
    537       1.25     blymn 		_cursesi_screen->baset.c_lflag |= NOFLSH;
    538       1.14       jdc 	}
    539       1.14       jdc 
    540       1.14       jdc 	__pfast = 1;
    541       1.43  christos 	return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN,
    542       1.43  christos 	    _cursesi_screen->curt) ? ERR : OK;
    543       1.14       jdc }
    544       1.14       jdc 
    545        1.2       cgd void
    546       1.25     blymn __startwin(SCREEN *screen)
    547        1.2       cgd {
    548        1.6       cgd 
    549       1.46       roy 	(void)fflush(screen->infd);
    550        1.6       cgd 
    551  1.46.14.1  christos #ifdef BSD
    552        1.6       cgd 	/*
    553        1.6       cgd 	 * Some C libraries default to a 1K buffer when talking to a tty.
    554        1.6       cgd 	 * With a larger screen, especially across a network, we'd like
    555        1.6       cgd 	 * to get it to all flush in a single write.  Make it twice as big
    556        1.6       cgd 	 * as just the characters (so that we have room for cursor motions
    557       1.11       mrg 	 * and attribute information) but no more than 8K.
    558  1.46.14.1  christos 	 *
    559  1.46.14.1  christos 	 * However, setvbuf may only be used after opening a stream and
    560  1.46.14.1  christos 	 * before any operations have been performed on it.
    561  1.46.14.1  christos 	 * This means we cannot work portably if an application wants
    562  1.46.14.1  christos 	 * to stop curses and start curses after a resize.
    563  1.46.14.1  christos 	 * Curses resizing is not standard, and thus not strictly portable
    564  1.46.14.1  christos 	 * even though all curses today support it.
    565  1.46.14.1  christos 	 * The BSD systems do not suffer from this limitation on setvbuf.
    566        1.6       cgd 	 */
    567       1.25     blymn 	if (screen->stdbuf == NULL) {
    568       1.25     blymn 		screen->len = LINES * COLS * 2;
    569       1.25     blymn 		if (screen->len > 8192)
    570       1.25     blymn 			screen->len = 8192;
    571       1.25     blymn 		if ((screen->stdbuf = malloc(screen->len)) == NULL)
    572       1.25     blymn 			screen->len = 0;
    573        1.6       cgd 	}
    574       1.46       roy 	(void)setvbuf(screen->outfd, screen->stdbuf, _IOFBF, screen->len);
    575  1.46.14.1  christos #endif
    576        1.2       cgd 
    577       1.42       roy 	ti_puts(screen->term, t_enter_ca_mode(screen->term), 0,
    578       1.42       roy 		__cputchar_args, (void *) screen->outfd);
    579       1.42       roy 	ti_puts(screen->term, t_cursor_normal(screen->term), 0,
    580       1.42       roy 	    __cputchar_args, (void *) screen->outfd);
    581       1.25     blymn 	if (screen->curscr->flags & __KEYPAD)
    582       1.42       roy 		ti_puts(screen->term, t_keypad_xmit(screen->term), 0,
    583       1.42       roy 		    __cputchar_args, (void *) screen->outfd);
    584       1.25     blymn 	screen->endwin = 0;
    585        1.1   mycroft }
    586        1.1   mycroft 
    587        1.1   mycroft int
    588       1.15     blymn endwin(void)
    589        1.1   mycroft {
    590       1.38       jdc #ifdef DEBUG
    591       1.39       jdc 	__CTRACE(__CTRACE_MISC, "endwin\n");
    592       1.38       jdc #endif
    593        1.8      phil 	return __stopwin();
    594       1.11       mrg }
    595       1.11       mrg 
    596       1.13     blymn bool
    597       1.15     blymn isendwin(void)
    598       1.11       mrg {
    599       1.46       roy 
    600       1.43  christos 	return _cursesi_screen->endwin ? TRUE : FALSE;
    601       1.11       mrg }
    602       1.11       mrg 
    603       1.11       mrg int
    604       1.15     blymn flushinp(void)
    605       1.11       mrg {
    606       1.46       roy 
    607       1.46       roy 	(void)fpurge(_cursesi_screen->infd);
    608       1.43  christos 	return OK;
    609       1.14       jdc }
    610       1.14       jdc 
    611        1.1   mycroft /*
    612        1.1   mycroft  * The following routines, savetty and resetty are completely useless and
    613        1.1   mycroft  * are left in only as stubs.  If people actually use them they will almost
    614        1.1   mycroft  * certainly screw up the state of the world.
    615        1.1   mycroft  */
    616       1.25     blymn /*static struct termios savedtty;*/
    617        1.1   mycroft int
    618       1.15     blymn savetty(void)
    619        1.1   mycroft {
    620       1.46       roy 
    621       1.29    itojun 	if (_cursesi_screen->notty == TRUE)
    622       1.29    itojun 		return OK;
    623       1.43  christos 	return tcgetattr(fileno(_cursesi_screen->infd),
    624       1.46       roy 			 &_cursesi_screen->savedtty) ? ERR : OK;
    625        1.1   mycroft }
    626        1.1   mycroft 
    627        1.1   mycroft int
    628       1.15     blymn resetty(void)
    629        1.1   mycroft {
    630       1.46       roy 
    631       1.29    itojun 	if (_cursesi_screen->notty == TRUE)
    632       1.29    itojun 		return OK;
    633       1.43  christos 	return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN,
    634       1.46       roy 			 &_cursesi_screen->savedtty) ? ERR : OK;
    635       1.19     blymn }
    636       1.19     blymn 
    637       1.19     blymn /*
    638       1.19     blymn  * erasechar --
    639       1.19     blymn  *     Return the character of the erase key.
    640       1.19     blymn  */
    641       1.19     blymn char
    642       1.19     blymn erasechar(void)
    643       1.19     blymn {
    644       1.46       roy 
    645       1.29    itojun 	if (_cursesi_screen->notty == TRUE)
    646       1.29    itojun 		return 0;
    647       1.25     blymn 	return _cursesi_screen->baset.c_cc[VERASE];
    648       1.19     blymn }
    649       1.19     blymn 
    650       1.19     blymn /*
    651       1.19     blymn  * killchar --
    652       1.19     blymn  *     Return the character of the kill key.
    653       1.19     blymn  */
    654       1.19     blymn char
    655       1.19     blymn killchar(void)
    656       1.19     blymn {
    657       1.46       roy 
    658       1.29    itojun 	if (_cursesi_screen->notty == TRUE)
    659       1.29    itojun 		return 0;
    660       1.25     blymn 	return _cursesi_screen->baset.c_cc[VKILL];
    661        1.1   mycroft }
    662       1.40     blymn 
    663       1.40     blymn /*
    664       1.40     blymn  * erasewchar --
    665       1.40     blymn  *     Return the wide character of the erase key.
    666       1.40     blymn  */
    667       1.40     blymn int
    668       1.46       roy erasewchar(wchar_t *ch)
    669       1.40     blymn {
    670       1.46       roy 
    671       1.40     blymn #ifndef HAVE_WCHAR
    672       1.40     blymn 	return ERR;
    673       1.40     blymn #else
    674       1.40     blymn 	if (_cursesi_screen->notty == TRUE)
    675       1.40     blymn 		return ERR;
    676       1.40     blymn 	*ch = _cursesi_screen->baset.c_cc[VERASE];
    677       1.40     blymn 	return OK;
    678       1.40     blymn #endif /* HAVE_WCHAR */
    679       1.40     blymn }
    680       1.40     blymn 
    681       1.40     blymn /*
    682       1.40     blymn  * killwchar --
    683       1.40     blymn  *     Return the wide character of the kill key.
    684       1.40     blymn  */
    685       1.40     blymn int
    686       1.40     blymn killwchar( wchar_t *ch )
    687       1.40     blymn {
    688       1.46       roy 
    689       1.40     blymn #ifndef HAVE_WCHAR
    690       1.40     blymn 	return ERR;
    691       1.40     blymn #else
    692       1.40     blymn 	if (_cursesi_screen->notty == TRUE)
    693       1.40     blymn 		return 0;
    694       1.40     blymn 	*ch = _cursesi_screen->baset.c_cc[VKILL];
    695       1.40     blymn 	return OK;
    696       1.40     blymn #endif /* HAVE_WCHAR */
    697       1.40     blymn }
    698       1.45       roy 
    699       1.45       roy int
    700       1.45       roy typeahead(int filedes)
    701       1.45       roy {
    702       1.45       roy 
    703       1.45       roy 	_cursesi_screen->checkfd = filedes;
    704       1.45       roy 	return OK;
    705       1.45       roy }
    706