Home | History | Annotate | Line # | Download | only in libcurses
tty.c revision 1.7
      1 /*	$NetBSD: tty.c,v 1.7 1997/07/22 07:37:11 mikel 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.5 (Berkeley) 8/13/94";
     40 #else
     41 __RCSID("$NetBSD: tty.c,v 1.7 1997/07/22 07:37:11 mikel Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <stdlib.h>
     46 #include <termios.h>
     47 #include <unistd.h>
     48 
     49 #include "curses.h"
     50 
     51 /*
     52  * In general, curses should leave tty hardware settings alone (speed, parity,
     53  * word size).  This is most easily done in BSD by using TCSASOFT on all
     54  * tcsetattr calls.  On other systems, it would be better to get and restore
     55  * those attributes at each change, or at least when stopped and restarted.
     56  * See also the comments in getterm().
     57  */
     58 #ifdef TCSASOFT
     59 int __tcaction = 1;			/* Ignore hardware settings. */
     60 #else
     61 int __tcaction = 0;
     62 #endif
     63 
     64 struct termios __orig_termios, __baset;
     65 static struct termios cbreakt, rawt, *curt;
     66 static int useraw;
     67 
     68 #ifndef	OXTABS
     69 #ifdef	XTABS			/* SMI uses XTABS. */
     70 #define	OXTABS	XTABS
     71 #else
     72 #define	OXTABS	0
     73 #endif
     74 #endif
     75 
     76 /*
     77  * gettmode --
     78  *	Do terminal type initialization.
     79  */
     80 int
     81 gettmode()
     82 {
     83 	useraw = 0;
     84 
     85 	if (tcgetattr(STDIN_FILENO, &__orig_termios))
     86 		return (ERR);
     87 
     88 	__baset = __orig_termios;
     89 	__baset.c_oflag &= ~OXTABS;
     90 
     91 	GT = 0;		/* historical. was used before we wired OXTABS off */
     92 	NONL = (__baset.c_oflag & ONLCR) == 0;
     93 
     94 	/*
     95 	 * XXX
     96 	 * System V and SMI systems overload VMIN and VTIME, such that
     97 	 * VMIN is the same as the VEOF element, and VTIME is the same
     98 	 * as the VEOL element.  This means that, if VEOF was ^D, the
     99 	 * default VMIN is 4.  Majorly stupid.
    100 	 */
    101 	cbreakt = __baset;
    102 	cbreakt.c_lflag &= ~ICANON;
    103 	cbreakt.c_cc[VMIN] = 1;
    104 	cbreakt.c_cc[VTIME] = 0;
    105 
    106 	rawt = cbreakt;
    107 	rawt.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|INLCR|IGNCR|ICRNL|IXON);
    108 	rawt.c_oflag &= ~OPOST;
    109 	rawt.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
    110 
    111 	/*
    112 	 * In general, curses should leave hardware-related settings alone.
    113 	 * This includes parity and word size.  Older versions set the tty
    114 	 * to 8 bits, no parity in raw(), but this is considered to be an
    115 	 * artifact of the old tty interface.  If it's desired to change
    116 	 * parity and word size, the TCSASOFT bit has to be removed from the
    117 	 * calls that switch to/from "raw" mode.
    118 	 */
    119 	if (!__tcaction) {
    120 		rawt.c_iflag &= ~ISTRIP;
    121 		rawt.c_cflag &= ~(CSIZE|PARENB);
    122 		rawt.c_cflag |= CS8;
    123 	}
    124 
    125 	curt = &__baset;
    126 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    127 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    128 }
    129 
    130 int
    131 raw()
    132 {
    133 	useraw = __pfast = __rawmode = 1;
    134 	curt = &rawt;
    135 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    136 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    137 }
    138 
    139 int
    140 noraw()
    141 {
    142 	useraw = __pfast = __rawmode = 0;
    143 	curt = &__baset;
    144 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    145 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    146 }
    147 
    148 int
    149 cbreak()
    150 {
    151 
    152 	__rawmode = 1;
    153 	curt = useraw ? &rawt : &cbreakt;
    154 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    155 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    156 }
    157 
    158 int
    159 nocbreak()
    160 {
    161 
    162 	__rawmode = 0;
    163 	curt = useraw ? &rawt : &__baset;
    164 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    165 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    166 }
    167 
    168 int
    169 echo()
    170 {
    171 	rawt.c_lflag |= ECHO;
    172 	cbreakt.c_lflag |= ECHO;
    173 	__baset.c_lflag |= ECHO;
    174 
    175 	__echoit = 1;
    176 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    177 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    178 }
    179 
    180 int
    181 noecho()
    182 {
    183 	rawt.c_lflag &= ~ECHO;
    184 	cbreakt.c_lflag &= ~ECHO;
    185 	__baset.c_lflag &= ~ECHO;
    186 
    187 	__echoit = 0;
    188 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    189 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    190 }
    191 
    192 int
    193 nl()
    194 {
    195 	rawt.c_iflag |= ICRNL;
    196 	rawt.c_oflag |= ONLCR;
    197 	cbreakt.c_iflag |= ICRNL;
    198 	cbreakt.c_oflag |= ONLCR;
    199 	__baset.c_iflag |= ICRNL;
    200 	__baset.c_oflag |= ONLCR;
    201 
    202 	__pfast = __rawmode;
    203 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    204 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    205 }
    206 
    207 int
    208 nonl()
    209 {
    210 	rawt.c_iflag &= ~ICRNL;
    211 	rawt.c_oflag &= ~ONLCR;
    212 	cbreakt.c_iflag &= ~ICRNL;
    213 	cbreakt.c_oflag &= ~ONLCR;
    214 	__baset.c_iflag &= ~ICRNL;
    215 	__baset.c_oflag &= ~ONLCR;
    216 
    217 	__pfast = 1;
    218 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    219 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    220 }
    221 
    222 void
    223 __startwin()
    224 {
    225 	static char *stdbuf;
    226 	static size_t len;
    227 
    228 	(void)fflush(stdout);
    229 
    230 	/*
    231 	 * Some C libraries default to a 1K buffer when talking to a tty.
    232 	 * With a larger screen, especially across a network, we'd like
    233 	 * to get it to all flush in a single write.  Make it twice as big
    234 	 * as just the characters (so that we have room for cursor motions
    235 	 * and standout information) but no more than 8K.
    236 	 */
    237 	if (stdbuf == NULL) {
    238 		if ((len = LINES * COLS * 2) > 8192)
    239 			len = 8192;
    240 		if ((stdbuf = malloc(len)) == NULL)
    241 			len = 0;
    242 	}
    243 	(void)setvbuf(stdout, stdbuf, _IOFBF, len);
    244 
    245 	tputs(TI, 0, __cputchar);
    246 	tputs(VS, 0, __cputchar);
    247 }
    248 
    249 int
    250 endwin()
    251 {
    252 	__restore_stophandler();
    253 
    254 	if (curscr != NULL) {
    255 		if (curscr->flags & __WSTANDOUT) {
    256 			tputs(SE, 0, __cputchar);
    257 			curscr->flags &= ~__WSTANDOUT;
    258 		}
    259 		__mvcur(curscr->cury, curscr->cury, curscr->maxy - 1, 0, 0);
    260 	}
    261 
    262 	(void)tputs(VE, 0, __cputchar);
    263 	(void)tputs(TE, 0, __cputchar);
    264 	(void)fflush(stdout);
    265 	(void)setvbuf(stdout, NULL, _IOLBF, 0);
    266 
    267 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    268 	    TCSASOFT | TCSADRAIN : TCSADRAIN, &__orig_termios) ? ERR : OK);
    269 }
    270 
    271 /*
    272  * The following routines, savetty and resetty are completely useless and
    273  * are left in only as stubs.  If people actually use them they will almost
    274  * certainly screw up the state of the world.
    275  */
    276 static struct termios savedtty;
    277 int
    278 savetty()
    279 {
    280 	return (tcgetattr(STDIN_FILENO, &savedtty) ? ERR : OK);
    281 }
    282 
    283 int
    284 resetty()
    285 {
    286 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    287 	    TCSASOFT | TCSADRAIN : TCSADRAIN, &savedtty) ? ERR : OK);
    288 }
    289