Home | History | Annotate | Line # | Download | only in libcurses
tty.c revision 1.1
      1 /*-
      2  * Copyright (c) 1992 The Regents of the University of California.
      3  * 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 /*static char sccsid[] = "from: @(#)tty.c	5.2 (Berkeley) 8/31/92";*/
     36 static char rcsid[] = "$Id: tty.c,v 1.1 1993/08/07 05:51:16 mycroft Exp $";
     37 #endif /* not lint */
     38 
     39 /*
     40  * Terminal initialization routines.
     41  */
     42 #include <sys/ioctl.h>
     43 
     44 #include <curses.h>
     45 #include <termios.h>
     46 #include <unistd.h>
     47 
     48 struct termios newtermio, origtermio;
     49 static struct termios norawt, rawt;
     50 static int useraw;
     51 
     52 /*
     53  * gettmode --
     54  *	Do terminal type initialization.
     55  */
     56 int
     57 gettmode()
     58 {
     59 	if (tcgetattr(STDIN_FILENO, &origtermio))
     60 		return (OK);
     61 
     62 	GT = (origtermio.c_oflag & OXTABS) == 0;
     63 	NONL = (origtermio.c_oflag & ONLCR) == 0;
     64 
     65 	norawt = origtermio;
     66 	norawt.c_oflag &= ~OXTABS;
     67 	rawt = norawt;
     68 	cfmakeraw(&rawt);
     69 
     70 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt) ? ERR : OK);
     71 }
     72 
     73 int
     74 raw()
     75 {
     76 	useraw = __pfast = __rawmode = 1;
     77 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
     78 }
     79 
     80 int
     81 noraw()
     82 {
     83 	useraw = __pfast = __rawmode = 0;
     84 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
     85 }
     86 
     87 int
     88 cbreak()
     89 {
     90 	rawt.c_lflag &= ~ICANON;
     91 	norawt.c_lflag &= ~ICANON;
     92 
     93 	__rawmode = 1;
     94 	if (useraw)
     95 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
     96 	else
     97 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
     98 }
     99 
    100 int
    101 nocbreak()
    102 {
    103 	rawt.c_lflag |= ICANON;
    104 	norawt.c_lflag |= ICANON;
    105 
    106 	__rawmode = 0;
    107 	if (useraw)
    108 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
    109 	else
    110 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
    111 }
    112 
    113 int
    114 echo()
    115 {
    116 	rawt.c_lflag |= ECHO;
    117 	norawt.c_lflag |= ECHO;
    118 
    119 	__echoit = 1;
    120 	if (useraw)
    121 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
    122 	else
    123 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
    124 }
    125 
    126 int
    127 noecho()
    128 {
    129 	rawt.c_lflag &= ~ECHO;
    130 	norawt.c_lflag &= ~ECHO;
    131 
    132 	__echoit = 0;
    133 	if (useraw)
    134 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
    135 	else
    136 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
    137 }
    138 
    139 int
    140 nl()
    141 {
    142 	rawt.c_iflag |= ICRNL;
    143 	rawt.c_oflag |= ONLCR;
    144 	norawt.c_iflag |= ICRNL;
    145 	norawt.c_oflag |= ONLCR;
    146 
    147 	__pfast = __rawmode;
    148 	if (useraw)
    149 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
    150 	else
    151 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
    152 }
    153 
    154 int
    155 nonl()
    156 {
    157 	rawt.c_iflag &= ~ICRNL;
    158 	rawt.c_oflag &= ~ONLCR;
    159 	norawt.c_iflag &= ~ICRNL;
    160 	norawt.c_oflag &= ~ONLCR;
    161 
    162 	__pfast = 1;
    163 	if (useraw)
    164 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
    165 	else
    166 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
    167 }
    168 
    169 int
    170 endwin()
    171 {
    172 	if (curscr) {
    173 		if (curscr->_flags & _STANDOUT) {
    174 			tputs(SE, 0, __cputchar);
    175 			curscr->_flags &= ~_STANDOUT;
    176 		}
    177 		__endwin = 1;
    178 	}
    179 
    180 	(void)tputs(VE, 0, __cputchar);
    181 	(void)tputs(TE, 0, __cputchar);
    182 	(void)fflush(stdout);
    183 
    184 	__echoit = origtermio.c_lflag & ECHO;
    185 	__rawmode = origtermio.c_lflag & ICANON;
    186 	__pfast = origtermio.c_iflag & ICRNL ? __rawmode : 1;
    187 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &origtermio));
    188 }
    189 
    190 /*
    191  * The following routines, savetty and resetty are completely useless and
    192  * are left in only as stubs.  If people actually use them they will almost
    193  * certainly screw up the state of the world.
    194  */
    195 static struct termios savedtty;
    196 int
    197 savetty()
    198 {
    199 	return (tcgetattr(STDIN_FILENO, &savedtty));
    200 }
    201 
    202 int
    203 resetty()
    204 {
    205 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &savedtty));
    206 }
    207