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