xref: /openbsd/lib/libcurses/tinfo/lib_raw.c (revision 4cfece93)
1 /* $OpenBSD: lib_raw.c,v 1.8 2010/01/12 23:22:06 nicm Exp $ */
2 
3 /****************************************************************************
4  * Copyright (c) 1998-2002,2007 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  *     and: Thomas E. Dickey 1998 on                                        *
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("$Id: lib_raw.c,v 1.8 2010/01/12 23:22:06 nicm Exp $")
55 
56 #if 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 #define _nc_setmode(mode) setmode(SP->_ifd, mode)
67 #else
68 #define _nc_setmode(mode)	/* nothing */
69 #endif
70 
71 #define COOKED_INPUT	(IXON|BRKINT|PARMRK)
72 
73 #ifdef TRACE
74 #define BEFORE(N)	if (USE_TRACEF(TRACE_BITS)) _nc_locked_tracef("%s before bits: %s", N, _nc_tracebits())
75 #define AFTER(N)	if (USE_TRACEF(TRACE_BITS)) _nc_locked_tracef("%s after bits: %s", N, _nc_tracebits())
76 #else
77 #define BEFORE(s)
78 #define AFTER(s)
79 #endif /* TRACE */
80 
81 NCURSES_EXPORT(int)
82 raw(void)
83 {
84     int result = ERR;
85 
86     T((T_CALLED("raw()")));
87 
88     if (SP != 0 && cur_term != 0) {
89 	TTY buf;
90 
91 	BEFORE("raw");
92 	_nc_setmode(O_BINARY);
93 
94 	buf = cur_term->Nttyb;
95 #ifdef TERMIOS
96 	buf.c_lflag &= ~(ICANON | ISIG | IEXTEN);
97 	buf.c_iflag &= ~(COOKED_INPUT);
98 	buf.c_cc[VMIN] = 1;
99 	buf.c_cc[VTIME] = 0;
100 #else
101 	buf.sg_flags |= RAW;
102 #endif
103 	if ((result = _nc_set_tty_mode(&buf)) == OK) {
104 	    SP->_raw = TRUE;
105 	    SP->_cbreak = 1;
106 	    cur_term->Nttyb = buf;
107 	}
108 	AFTER("raw");
109     }
110     returnCode(result);
111 }
112 
113 NCURSES_EXPORT(int)
114 cbreak(void)
115 {
116     int result = ERR;
117 
118     T((T_CALLED("cbreak()")));
119 
120     if (SP != 0 && cur_term != 0) {
121 	TTY buf;
122 
123 	BEFORE("cbreak");
124 	_nc_setmode(O_BINARY);
125 
126 	buf = cur_term->Nttyb;
127 #ifdef TERMIOS
128 	buf.c_lflag &= ~ICANON;
129 	buf.c_iflag &= ~ICRNL;
130 	buf.c_lflag |= ISIG;
131 	buf.c_cc[VMIN] = 1;
132 	buf.c_cc[VTIME] = 0;
133 #else
134 	buf.sg_flags |= CBREAK;
135 #endif
136 	if ((result = _nc_set_tty_mode(&buf)) == OK) {
137 	    SP->_cbreak = 1;
138 	    cur_term->Nttyb = buf;
139 	}
140 	AFTER("cbreak");
141     }
142     returnCode(result);
143 }
144 
145 /*
146  * Note:
147  * this implementation may be wrong.  See the comment under intrflush().
148  */
149 NCURSES_EXPORT(void)
150 qiflush(void)
151 {
152     int result = ERR;
153 
154     T((T_CALLED("qiflush()")));
155 
156     if (cur_term != 0) {
157 	TTY buf;
158 
159 	BEFORE("qiflush");
160 	buf = cur_term->Nttyb;
161 #ifdef TERMIOS
162 	buf.c_lflag &= ~(NOFLSH);
163 	result = _nc_set_tty_mode(&buf);
164 #else
165 	/* FIXME */
166 #endif
167 	if (result == OK)
168 	    cur_term->Nttyb = buf;
169 	AFTER("qiflush");
170     }
171     returnVoid;
172 }
173 
174 NCURSES_EXPORT(int)
175 noraw(void)
176 {
177     int result = ERR;
178 
179     T((T_CALLED("noraw()")));
180 
181     if (SP != 0 && cur_term != 0) {
182 	TTY buf;
183 
184 	BEFORE("noraw");
185 	_nc_setmode(O_TEXT);
186 
187 	buf = cur_term->Nttyb;
188 #ifdef TERMIOS
189 	buf.c_lflag |= ISIG | ICANON |
190 	    (cur_term->Ottyb.c_lflag & IEXTEN);
191 	buf.c_iflag |= COOKED_INPUT;
192 #else
193 	buf.sg_flags &= ~(RAW | CBREAK);
194 #endif
195 	if ((result = _nc_set_tty_mode(&buf)) == OK) {
196 	    SP->_raw = FALSE;
197 	    SP->_cbreak = 0;
198 	    cur_term->Nttyb = buf;
199 	}
200 	AFTER("noraw");
201     }
202     returnCode(result);
203 }
204 
205 NCURSES_EXPORT(int)
206 nocbreak(void)
207 {
208     int result = ERR;
209 
210     T((T_CALLED("nocbreak()")));
211 
212     if (SP != 0 && cur_term != 0) {
213 	TTY buf;
214 
215 	BEFORE("nocbreak");
216 	_nc_setmode(O_TEXT);
217 
218 	buf = cur_term->Nttyb;
219 #ifdef TERMIOS
220 	buf.c_lflag |= ICANON;
221 	buf.c_iflag |= ICRNL;
222 #else
223 	buf.sg_flags &= ~CBREAK;
224 #endif
225 	if ((result = _nc_set_tty_mode(&buf)) == OK) {
226 	    SP->_cbreak = 0;
227 	    cur_term->Nttyb = buf;
228 	}
229 	AFTER("nocbreak");
230     }
231     returnCode(result);
232 }
233 
234 /*
235  * Note:
236  * this implementation may be wrong.  See the comment under intrflush().
237  */
238 NCURSES_EXPORT(void)
239 noqiflush(void)
240 {
241     int result = ERR;
242 
243     T((T_CALLED("noqiflush()")));
244 
245     if (cur_term != 0) {
246 	TTY buf;
247 
248 	BEFORE("noqiflush");
249 	buf = cur_term->Nttyb;
250 #ifdef TERMIOS
251 	buf.c_lflag |= NOFLSH;
252 	result = _nc_set_tty_mode(&buf);
253 #else
254 	/* FIXME */
255 #endif
256 	if (result == OK) {
257 	    cur_term->Nttyb = buf;
258 	}
259 	AFTER("noqiflush");
260     }
261     returnVoid;
262 }
263 
264 /*
265  * This call does the same thing as the qiflush()/noqiflush() pair.  We know
266  * for certain that SVr3 intrflush() tweaks the NOFLSH bit; on the other hand,
267  * the match (in the SVr4 man pages) between the language describing NOFLSH in
268  * termio(7) and the language describing qiflush()/noqiflush() in
269  * curs_inopts(3x) is too exact to be coincidence.
270  */
271 NCURSES_EXPORT(int)
272 intrflush(WINDOW *win GCC_UNUSED, bool flag)
273 {
274     int result = ERR;
275 
276     T((T_CALLED("intrflush(%d)"), flag));
277 
278     if (cur_term != 0) {
279 	TTY buf;
280 
281 	BEFORE("intrflush");
282 	buf = cur_term->Nttyb;
283 #ifdef TERMIOS
284 	if (flag)
285 	    buf.c_lflag &= ~(NOFLSH);
286 	else
287 	    buf.c_lflag |= (NOFLSH);
288 	result = _nc_set_tty_mode(&buf);
289 #else
290 	/* FIXME */
291 #endif
292 	if (result == OK) {
293 	    cur_term->Nttyb = buf;
294 	}
295 	AFTER("intrflush");
296     }
297     returnCode(result);
298 }
299