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