Compatibility fix to satisfy tn3270.
[unix-history] / lib / libcurses / tty.c
CommitLineData
b80b8de3
AC
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
35static char sccsid[] = "@(#)tty.c 8.2 (Berkeley) 1/2/94";
36#endif /* not lint */
37
38#include <sys/ioctl.h>
39
40#include <curses.h>
41#include <termios.h>
42#include <unistd.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
52int __tcaction = 1; /* Ignore hardware settings. */
53#else
54int __tcaction = 0;
55#endif
56
57struct termios __orig_termios, __baset;
58static struct termios cbreakt, rawt, *curt;
59static 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 */
73int
74gettmode()
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
123int
124raw()
125{
126 useraw = __pfast = __rawmode = 1;
127 curt = &rawt;
128 return (tcsetattr(STDIN_FILENO, __tcaction ?
129 TCSASOFT | TCSADRAIN : TCSADRAIN, curt));
130}
131
132int
133noraw()
134{
135 useraw = __pfast = __rawmode = 0;
136 curt = &__baset;
137 return (tcsetattr(STDIN_FILENO, __tcaction ?
138 TCSASOFT | TCSADRAIN : TCSADRAIN, curt));
139}
140
141int
142cbreak()
143{
144
145 __rawmode = 1;
146 curt = useraw ? &rawt : &cbreakt;
147 return (tcsetattr(STDIN_FILENO, __tcaction ?
148 TCSASOFT | TCSADRAIN : TCSADRAIN, curt));
149}
150
151int
152nocbreak()
153{
154
155 __rawmode = 0;
156 curt = useraw ? &rawt : &__baset;
157 return (tcsetattr(STDIN_FILENO, __tcaction ?
158 TCSASOFT | TCSADRAIN : TCSADRAIN, curt));
159}
160
161int
162echo()
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));
171}
172
173int
174noecho()
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));
183}
184
185int
186nl()
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));
198}
199
200int
201nonl()
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));
213}
214
215void
216__startwin()
217{
218 (void)fflush(stdout);
219 (void)setvbuf(stdout, NULL, _IOFBF, 0);
220
221 tputs(TI, 0, __cputchar);
222 tputs(VS, 0, __cputchar);
223}
224
225int
226endwin()
227{
228 __restore_stophandler();
229
230 if (curscr != NULL) {
231 if (curscr->flags & __WSTANDOUT) {
232 tputs(SE, 0, __cputchar);
233 curscr->flags &= ~__WSTANDOUT;
234 }
235 __mvcur(curscr->cury, curscr->cury, curscr->maxy - 1, 0, 0);
236 }
237
238 (void)tputs(VE, 0, __cputchar);
239 (void)tputs(TE, 0, __cputchar);
240 (void)fflush(stdout);
241 (void)setvbuf(stdout, NULL, _IOLBF, 0);
242
243 return (tcsetattr(STDIN_FILENO, __tcaction ?
244 TCSASOFT | TCSADRAIN : TCSADRAIN, &__orig_termios));
245}
246
247/*
248 * The following routines, savetty and resetty are completely useless and
249 * are left in only as stubs. If people actually use them they will almost
250 * certainly screw up the state of the world.
251 */
252static struct termios savedtty;
253int
254savetty()
255{
256 return (tcgetattr(STDIN_FILENO, &savedtty));
257}
258
259int
260resetty()
261{
262 return (tcsetattr(STDIN_FILENO, __tcaction ?
263 TCSASOFT | TCSADRAIN : TCSADRAIN, &savedtty));
264}