1 /****************************************************************************
2 * Copyright (c) 1998 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 ****************************************************************************/
49 #include <curses.priv.h>
50 #include <term.h> /* cur_term */
52 MODULE_ID("$Id: lib_raw.c,v 1.25 1998/02/11 12:13:59 tom Exp $")
54 #if defined(SVR4_TERMIO) && !defined(_POSIX_SOURCE)
59 #include <sys/termio.h> /* needed for ISC */
67 /* may be undefined if we're using termio.h */
75 #define COOKED_INPUT (IXON|BRKINT|PARMRK)
79 typedef struct {unsigned int val; const char *name;} BITNAMES;
81 static void lookup_bits(char *buf, const BITNAMES *table, const char *label, unsigned int val)
85 (void) strcat(buf, label);
86 (void) strcat(buf, ": {");
87 for (sp = table; sp->name; sp++)
89 && (val & sp->val) == sp->val)
91 (void) strcat(buf, sp->name);
92 (void) strcat(buf, ", ");
94 if (buf[strlen(buf) - 2] == ',')
95 buf[strlen(buf) - 2] = '\0';
96 (void) strcat(buf,"} ");
99 char *_tracebits(void)
100 /* describe the state of the terminal control bits exactly */
103 static const BITNAMES
120 #define ALLIN (BRKINT|IGNBRK|IGNPAR|PARMRK|INPCK|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IXOFF)
126 #define ALLOUT (OPOST)
133 #if !defined(CS5) || !defined(CS8)
138 {PARODD|PARENB, "PARODD"}, /* concession to readability */
140 #define ALLCTRL (CLOCAL|CREAD|CSIZE|CSTOPB|HUPCL|PARENB|PARODD)
145 {ECHOE|ECHO, "ECHOE"}, /* concession to readability */
146 {ECHOK|ECHO, "ECHOK"}, /* concession to readability */
154 #define ALLLOCAL (ECHO|ECHONL|ICANON|ISIG|NOFLSH|TOSTOP|IEXTEN)
158 buf = _nc_trace_buf(0,
165 if (cur_term->Nttyb.c_iflag & ALLIN)
166 lookup_bits(buf, iflags, "iflags", cur_term->Nttyb.c_iflag);
168 if (cur_term->Nttyb.c_oflag & ALLOUT)
169 lookup_bits(buf, oflags, "oflags", cur_term->Nttyb.c_oflag);
171 if (cur_term->Nttyb.c_cflag & ALLCTRL)
172 lookup_bits(buf, cflags, "cflags", cur_term->Nttyb.c_cflag);
174 #if defined(CS5) && defined(CS8)
175 switch (cur_term->Nttyb.c_cflag & CSIZE) {
176 case CS5: strcat(buf, "CS5 "); break;
177 case CS6: strcat(buf, "CS6 "); break;
178 case CS7: strcat(buf, "CS7 "); break;
179 case CS8: strcat(buf, "CS8 "); break;
180 default: strcat(buf, "CSIZE? "); break;
184 if (cur_term->Nttyb.c_lflag & ALLLOCAL)
185 lookup_bits(buf, lflags, "lflags", cur_term->Nttyb.c_lflag);
188 /* reference: ttcompat(4M) on SunOS 4.1 */
212 {LLITOUT, "LLITOUT"},
218 #define ALLCTRL (CBREAK|CRMOD|ECHO|EVENP|LCASE|LLITOUT|ODDP|RAW|TANDEM|XTABS)
221 buf = _nc_trace_buf(0,
224 if (cur_term->Nttyb.sg_flags & ALLCTRL)
226 lookup_bits(buf, cflags, "cflags", cur_term->Nttyb.sg_flags);
233 #define BEFORE(N) if (_nc_tracing&TRACE_BITS) _tracef("%s before bits: %s", N, _tracebits())
234 #define AFTER(N) if (_nc_tracing&TRACE_BITS) _tracef("%s after bits: %s", N, _tracebits())
242 T((T_CALLED("raw()")));
243 if (SP != 0 && cur_term != 0) {
249 setmode(SP->_ifd, O_BINARY);
254 cur_term->Nttyb.c_lflag &= ~(ICANON|ISIG);
255 cur_term->Nttyb.c_iflag &= ~(COOKED_INPUT);
256 cur_term->Nttyb.c_cc[VMIN] = 1;
257 cur_term->Nttyb.c_cc[VTIME] = 0;
260 cur_term->Nttyb.sg_flags |= RAW;
262 returnCode(_nc_set_curterm(&cur_term->Nttyb));
269 T((T_CALLED("cbreak()")));
274 setmode(SP->_ifd, O_BINARY);
279 cur_term->Nttyb.c_lflag &= ~ICANON;
280 cur_term->Nttyb.c_iflag &= ~ICRNL;
281 cur_term->Nttyb.c_lflag |= ISIG;
282 cur_term->Nttyb.c_cc[VMIN] = 1;
283 cur_term->Nttyb.c_cc[VTIME] = 0;
286 cur_term->Nttyb.sg_flags |= CBREAK;
288 returnCode(_nc_set_curterm( &cur_term->Nttyb));
293 T((T_CALLED("qiflush()")));
296 * Note: this implementation may be wrong. See the comment under
302 cur_term->Nttyb.c_lflag &= ~(NOFLSH);
304 returnCode(_nc_set_curterm( &cur_term->Nttyb));
313 T((T_CALLED("noraw()")));
319 setmode(SP->_ifd, O_TEXT);
324 cur_term->Nttyb.c_lflag |= ISIG|ICANON;
325 cur_term->Nttyb.c_iflag |= COOKED_INPUT;
328 cur_term->Nttyb.sg_flags &= ~(RAW|CBREAK);
330 returnCode(_nc_set_curterm( &cur_term->Nttyb));
336 T((T_CALLED("nocbreak()")));
341 setmode(SP->_ifd, O_TEXT);
346 cur_term->Nttyb.c_lflag |= ICANON;
347 cur_term->Nttyb.c_iflag |= ICRNL;
350 cur_term->Nttyb.sg_flags &= ~CBREAK;
352 returnCode(_nc_set_curterm( &cur_term->Nttyb));
357 T((T_CALLED("noqiflush()")));
360 * Note: this implementation may be wrong. See the comment under
366 cur_term->Nttyb.c_lflag |= NOFLSH;
368 returnCode(_nc_set_curterm( &cur_term->Nttyb));
374 int intrflush(WINDOW *win GCC_UNUSED, bool flag)
376 T((T_CALLED("intrflush(%d)"), flag));
379 * This call does the same thing as the qiflush()/noqiflush()
380 * pair. We know for certain that SVr3 intrflush() tweaks the
381 * NOFLSH bit; on the other hand, the match (in the SVr4 man
382 * pages) between the language describing NOFLSH in termio(7)
383 * and the language describing qiflush()/noqiflush() in
384 * curs_inopts(3x) is too exact to be coincidence.
390 cur_term->Nttyb.c_lflag &= ~(NOFLSH);
392 cur_term->Nttyb.c_lflag |= (NOFLSH);
394 returnCode(_nc_set_curterm( &cur_term->Nttyb));