xref: /openbsd-src/lib/libcurses/tinfo/lib_raw.c (revision bcc05361ef768670d0c5fa5affad379a9b45d34d)
1 /*	$OpenBSD: lib_raw.c,v 1.2 1999/03/11 21:03:57 millert Exp $	*/
2 
3 /****************************************************************************
4  * Copyright (c) 1998 Free Software Foundation, Inc.                        *
5  *                                                                          *
6  * Permission is hereby granted, free of charge, to any person obtaining a  *
7  * copy of this software and associated documentation files (the            *
8  * "Software"), to deal in the Software without restriction, including      *
9  * without limitation the rights to use, copy, modify, merge, publish,      *
10  * distribute, distribute with modifications, sublicense, and/or sell       *
11  * copies of the Software, and to permit persons to whom the Software is    *
12  * furnished to do so, subject to the following conditions:                 *
13  *                                                                          *
14  * The above copyright notice and this permission notice shall be included  *
15  * in all copies or substantial portions of the Software.                   *
16  *                                                                          *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24  *                                                                          *
25  * Except as contained in this notice, the name(s) of the above copyright   *
26  * holders shall not be used in advertising or otherwise to promote the     *
27  * sale, use or other dealings in this Software without prior written       *
28  * authorization.                                                           *
29  ****************************************************************************/
30 
31 /****************************************************************************
32  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
33  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
34  ****************************************************************************/
35 
36 
37 /*
38  *	raw.c
39  *
40  *	Routines:
41  *		raw()
42  *		cbreak()
43  *		noraw()
44  *		nocbreak()
45  *		qiflush()
46  *		noqiflush()
47  *		intrflush()
48  *
49  */
50 
51 #include <curses.priv.h>
52 #include <term.h>	/* cur_term */
53 
54 MODULE_ID("$From: lib_raw.c,v 1.3 1999/03/06 22:28:24 tom Exp $")
55 
56 #if defined(SVR4_TERMIO) && !defined(_POSIX_SOURCE)
57 #define _POSIX_SOURCE
58 #endif
59 
60 #if HAVE_SYS_TERMIO_H
61 #include <sys/termio.h>	/* needed for ISC */
62 #endif
63 
64 #ifdef __EMX__
65 #include <io.h>
66 #include <fcntl.h>
67 #endif
68 
69 #define COOKED_INPUT	(IXON|BRKINT|PARMRK)
70 
71 #ifdef TRACE
72 #define BEFORE(N)	if (_nc_tracing&TRACE_BITS) _tracef("%s before bits: %s", N, _nc_tracebits())
73 #define AFTER(N)	if (_nc_tracing&TRACE_BITS) _tracef("%s after bits: %s", N, _nc_tracebits())
74 #else
75 #define BEFORE(s)
76 #define AFTER(s)
77 #endif /* TRACE */
78 
79 int raw(void)
80 {
81 	T((T_CALLED("raw()")));
82 	if (SP != 0 && cur_term != 0) {
83 
84 		SP->_raw = TRUE;
85 		SP->_cbreak = 1;
86 
87 #ifdef __EMX__
88 		setmode(SP->_ifd, O_BINARY);
89 #endif
90 
91 #ifdef TERMIOS
92 		BEFORE("raw");
93 		cur_term->Nttyb.c_lflag &= ~(ICANON|ISIG);
94 		cur_term->Nttyb.c_iflag &= ~(COOKED_INPUT);
95 		cur_term->Nttyb.c_cc[VMIN] = 1;
96 		cur_term->Nttyb.c_cc[VTIME] = 0;
97 		AFTER("raw");
98 #else
99 		cur_term->Nttyb.sg_flags |= RAW;
100 #endif
101 		returnCode(_nc_set_tty_mode(&cur_term->Nttyb));
102 	}
103 	returnCode(ERR);
104 }
105 
106 int cbreak(void)
107 {
108 	T((T_CALLED("cbreak()")));
109 
110 	SP->_cbreak = 1;
111 
112 #ifdef __EMX__
113 	setmode(SP->_ifd, O_BINARY);
114 #endif
115 
116 #ifdef TERMIOS
117 	BEFORE("cbreak");
118 	cur_term->Nttyb.c_lflag &= ~ICANON;
119 	cur_term->Nttyb.c_iflag &= ~ICRNL;
120 	cur_term->Nttyb.c_lflag |= ISIG;
121 	cur_term->Nttyb.c_cc[VMIN] = 1;
122 	cur_term->Nttyb.c_cc[VTIME] = 0;
123 	AFTER("cbreak");
124 #else
125 	cur_term->Nttyb.sg_flags |= CBREAK;
126 #endif
127 	returnCode(_nc_set_tty_mode( &cur_term->Nttyb));
128 }
129 
130 void qiflush(void)
131 {
132 	T((T_CALLED("qiflush()")));
133 
134 	/*
135 	 * Note: this implementation may be wrong.  See the comment under
136 	 * intrflush().
137 	 */
138 
139 #ifdef TERMIOS
140 	BEFORE("qiflush");
141 	cur_term->Nttyb.c_lflag &= ~(NOFLSH);
142 	AFTER("qiflush");
143 	(void)_nc_set_tty_mode( &cur_term->Nttyb);
144 	returnVoid;
145 #endif
146 }
147 
148 
149 int noraw(void)
150 {
151 	T((T_CALLED("noraw()")));
152 
153 	SP->_raw = FALSE;
154 	SP->_cbreak = 0;
155 
156 #ifdef __EMX__
157 	setmode(SP->_ifd, O_TEXT);
158 #endif
159 
160 #ifdef TERMIOS
161 	BEFORE("noraw");
162 	cur_term->Nttyb.c_lflag |= ISIG|ICANON;
163 	cur_term->Nttyb.c_iflag |= COOKED_INPUT;
164 	AFTER("noraw");
165 #else
166 	cur_term->Nttyb.sg_flags &= ~(RAW|CBREAK);
167 #endif
168 	returnCode(_nc_set_tty_mode( &cur_term->Nttyb));
169 }
170 
171 
172 int nocbreak(void)
173 {
174 	T((T_CALLED("nocbreak()")));
175 
176 	SP->_cbreak = 0;
177 
178 #ifdef __EMX__
179 	setmode(SP->_ifd, O_TEXT);
180 #endif
181 
182 #ifdef TERMIOS
183 	BEFORE("nocbreak");
184 	cur_term->Nttyb.c_lflag |= ICANON;
185 	cur_term->Nttyb.c_iflag |= ICRNL;
186 	AFTER("nocbreak");
187 #else
188 	cur_term->Nttyb.sg_flags &= ~CBREAK;
189 #endif
190 	returnCode(_nc_set_tty_mode( &cur_term->Nttyb));
191 }
192 
193 void noqiflush(void)
194 {
195 	T((T_CALLED("noqiflush()")));
196 
197 	/*
198 	 * Note: this implementation may be wrong.  See the comment under
199 	 * intrflush().
200 	 */
201 
202 #ifdef TERMIOS
203 	BEFORE("noqiflush");
204 	cur_term->Nttyb.c_lflag |= NOFLSH;
205 	AFTER("noqiflush");
206 	(void)_nc_set_tty_mode( &cur_term->Nttyb);
207 	returnVoid;
208 #endif
209 }
210 
211 int intrflush(WINDOW *win GCC_UNUSED, bool flag)
212 {
213 	T((T_CALLED("intrflush(%d)"), flag));
214 
215 	/*
216 	 * This call does the same thing as the qiflush()/noqiflush()
217 	 * pair.  We know for certain that SVr3 intrflush() tweaks the
218 	 * NOFLSH bit; on the other hand, the match (in the SVr4 man
219 	 * pages) between the language describing NOFLSH in termio(7)
220 	 * and the language describing qiflush()/noqiflush() in
221 	 * curs_inopts(3x) is too exact to be coincidence.
222 	 */
223 
224 #ifdef TERMIOS
225 	BEFORE("intrflush");
226 	if (flag)
227 		cur_term->Nttyb.c_lflag &= ~(NOFLSH);
228 	else
229 		cur_term->Nttyb.c_lflag |= (NOFLSH);
230 	AFTER("intrflush");
231 	returnCode(_nc_set_tty_mode( &cur_term->Nttyb));
232 #else
233 	returnCode(ERR);
234 #endif
235 }
236