Home | History | Annotate | Line # | Download | only in libcurses
tty.c revision 1.4
      1 /*-
      2  * Copyright (c) 1992, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 /* from: static char sccsid[] = "@(#)tty.c	8.2 (Berkeley) 1/2/94"; */
     36 static char *rcsid = "$Id: tty.c,v 1.4 1994/01/24 08:36:59 cgd Exp $";
     37 #endif /* not lint */
     38 
     39 #include <sys/ioctl.h>
     40 
     41 #include <curses.h>
     42 #include <termios.h>
     43 #include <unistd.h>
     44 
     45 /*
     46  * In general, curses should leave tty hardware settings alone (speed, parity,
     47  * word size).  This is most easily done in BSD by using TCSASOFT on all
     48  * tcsetattr calls.  On other systems, it would be better to get and restore
     49  * those attributes at each change, or at least when stopped and restarted.
     50  * See also the comments in getterm().
     51  */
     52 #ifdef TCSASOFT
     53 int __tcaction = 1;			/* Ignore hardware settings. */
     54 #else
     55 int __tcaction = 0;
     56 #endif
     57 
     58 struct termios __orig_termios, __baset;
     59 static struct termios cbreakt, rawt, *curt;
     60 static int useraw;
     61 
     62 #ifndef	OXTABS
     63 #ifdef	XTABS			/* SMI uses XTABS. */
     64 #define	OXTABS	XTABS
     65 #else
     66 #define	OXTABS	0
     67 #endif
     68 #endif
     69 
     70 /*
     71  * gettmode --
     72  *	Do terminal type initialization.
     73  */
     74 int
     75 gettmode()
     76 {
     77 	useraw = 0;
     78 
     79 	if (tcgetattr(STDIN_FILENO, &__orig_termios))
     80 		return (ERR);
     81 
     82 	__baset = __orig_termios;
     83 	__baset.c_oflag &= ~OXTABS;
     84 
     85 	GT = 0;		/* historical. was used before we wired OXTABS off */
     86 	NONL = (__baset.c_oflag & ONLCR) == 0;
     87 
     88 	/*
     89 	 * XXX
     90 	 * System V and SMI systems overload VMIN and VTIME, such that
     91 	 * VMIN is the same as the VEOF element, and VTIME is the same
     92 	 * as the VEOL element.  This means that, if VEOF was ^D, the
     93 	 * default VMIN is 4.  Majorly stupid.
     94 	 */
     95 	cbreakt = __baset;
     96 	cbreakt.c_lflag &= ~ICANON;
     97 	cbreakt.c_cc[VMIN] = 1;
     98 	cbreakt.c_cc[VTIME] = 0;
     99 
    100 	rawt = cbreakt;
    101 	rawt.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|INLCR|IGNCR|ICRNL|IXON);
    102 	rawt.c_oflag &= ~OPOST;
    103 	rawt.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
    104 
    105 	/*
    106 	 * In general, curses should leave hardware-related settings alone.
    107 	 * This includes parity and word size.  Older versions set the tty
    108 	 * to 8 bits, no parity in raw(), but this is considered to be an
    109 	 * artifact of the old tty interface.  If it's desired to change
    110 	 * parity and word size, the TCSASOFT bit has to be removed from the
    111 	 * calls that switch to/from "raw" mode.
    112 	 */
    113 	if (!__tcaction) {
    114 		rawt.c_iflag &= ~ISTRIP;
    115 		rawt.c_cflag &= ~(CSIZE|PARENB);
    116 		rawt.c_cflag |= CS8;
    117 	}
    118 
    119 	curt = &__baset;
    120 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    121 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
    122 }
    123 
    124 int
    125 raw()
    126 {
    127 	useraw = __pfast = __rawmode = 1;
    128 	curt = &rawt;
    129 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    130 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt));
    131 }
    132 
    133 int
    134 noraw()
    135 {
    136 	useraw = __pfast = __rawmode = 0;
    137 	curt = &__baset;
    138 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    139 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt));
    140 }
    141 
    142 int
    143 cbreak()
    144 {
    145 
    146 	__rawmode = 1;
    147 	curt = useraw ? &rawt : &cbreakt;
    148 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    149 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt));
    150 }
    151 
    152 int
    153 nocbreak()
    154 {
    155 
    156 	__rawmode = 0;
    157 	curt = useraw ? &rawt : &__baset;
    158 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    159 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt));
    160 }
    161 
    162 int
    163 echo()
    164 {
    165 	rawt.c_lflag |= ECHO;
    166 	cbreakt.c_lflag |= ECHO;
    167 	__baset.c_lflag |= ECHO;
    168 
    169 	__echoit = 1;
    170 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    171 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt));
    172 }
    173 
    174 int
    175 noecho()
    176 {
    177 	rawt.c_lflag &= ~ECHO;
    178 	cbreakt.c_lflag &= ~ECHO;
    179 	__baset.c_lflag &= ~ECHO;
    180 
    181 	__echoit = 0;
    182 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    183 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt));
    184 }
    185 
    186 int
    187 nl()
    188 {
    189 	rawt.c_iflag |= ICRNL;
    190 	rawt.c_oflag |= ONLCR;
    191 	cbreakt.c_iflag |= ICRNL;
    192 	cbreakt.c_oflag |= ONLCR;
    193 	__baset.c_iflag |= ICRNL;
    194 	__baset.c_oflag |= ONLCR;
    195 
    196 	__pfast = __rawmode;
    197 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    198 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt));
    199 }
    200 
    201 int
    202 nonl()
    203 {
    204 	rawt.c_iflag &= ~ICRNL;
    205 	rawt.c_oflag &= ~ONLCR;
    206 	cbreakt.c_iflag &= ~ICRNL;
    207 	cbreakt.c_oflag &= ~ONLCR;
    208 	__baset.c_iflag &= ~ICRNL;
    209 	__baset.c_oflag &= ~ONLCR;
    210 
    211 	__pfast = 1;
    212 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    213 	    TCSASOFT | TCSADRAIN : TCSADRAIN, curt));
    214 }
    215 
    216 void
    217 __startwin()
    218 {
    219 	(void)fflush(stdout);
    220 	(void)setvbuf(stdout, NULL, _IOFBF, 0);
    221 
    222 	tputs(TI, 0, __cputchar);
    223 	tputs(VS, 0, __cputchar);
    224 }
    225 
    226 int
    227 endwin()
    228 {
    229 	__restore_stophandler();
    230 
    231 	if (curscr != NULL) {
    232 		if (curscr->flags & __WSTANDOUT) {
    233 			tputs(SE, 0, __cputchar);
    234 			curscr->flags &= ~__WSTANDOUT;
    235 		}
    236 		__mvcur(curscr->cury, curscr->cury, curscr->maxy - 1, 0, 0);
    237 	}
    238 
    239 	(void)tputs(VE, 0, __cputchar);
    240 	(void)tputs(TE, 0, __cputchar);
    241 	(void)fflush(stdout);
    242 	(void)setvbuf(stdout, NULL, _IOLBF, 0);
    243 
    244 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    245 	    TCSASOFT | TCSADRAIN : TCSADRAIN, &__orig_termios));
    246 }
    247 
    248 /*
    249  * The following routines, savetty and resetty are completely useless and
    250  * are left in only as stubs.  If people actually use them they will almost
    251  * certainly screw up the state of the world.
    252  */
    253 static struct termios savedtty;
    254 int
    255 savetty()
    256 {
    257 	return (tcgetattr(STDIN_FILENO, &savedtty));
    258 }
    259 
    260 int
    261 resetty()
    262 {
    263 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    264 	    TCSASOFT | TCSADRAIN : TCSADRAIN, &savedtty));
    265 }
    266