1 /* $OpenBSD: lib_raw.c,v 1.7 2001/01/22 18:01:53 millert Exp $ */ 2 3 /**************************************************************************** 4 * Copyright (c) 1998,1999,2000 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 * raw.c 38 * 39 * Routines: 40 * raw() 41 * cbreak() 42 * noraw() 43 * nocbreak() 44 * qiflush() 45 * noqiflush() 46 * intrflush() 47 * 48 */ 49 50 #include <curses.priv.h> 51 #include <term.h> /* cur_term */ 52 53 MODULE_ID("$From: lib_raw.c,v 1.10 2000/12/10 02:55:07 tom Exp $") 54 55 #if SVR4_TERMIO && !defined(_POSIX_SOURCE) 56 #define _POSIX_SOURCE 57 #endif 58 59 #if HAVE_SYS_TERMIO_H 60 #include <sys/termio.h> /* needed for ISC */ 61 #endif 62 63 #ifdef __EMX__ 64 #include <io.h> 65 #endif 66 67 #define COOKED_INPUT (IXON|BRKINT|PARMRK) 68 69 #ifdef TRACE 70 #define BEFORE(N) if (_nc_tracing&TRACE_BITS) _tracef("%s before bits: %s", N, _nc_tracebits()) 71 #define AFTER(N) if (_nc_tracing&TRACE_BITS) _tracef("%s after bits: %s", N, _nc_tracebits()) 72 #else 73 #define BEFORE(s) 74 #define AFTER(s) 75 #endif /* TRACE */ 76 77 NCURSES_EXPORT(int) 78 raw(void) 79 { 80 T((T_CALLED("raw()"))); 81 if (SP != 0 && cur_term != 0) { 82 83 SP->_raw = TRUE; 84 SP->_cbreak = 1; 85 86 #ifdef __EMX__ 87 setmode(SP->_ifd, O_BINARY); 88 #endif 89 90 #ifdef TERMIOS 91 BEFORE("raw"); 92 cur_term->Nttyb.c_lflag &= ~(ICANON | ISIG | IEXTEN); 93 cur_term->Nttyb.c_iflag &= ~(COOKED_INPUT); 94 cur_term->Nttyb.c_cc[VMIN] = 1; 95 cur_term->Nttyb.c_cc[VTIME] = 0; 96 AFTER("raw"); 97 #else 98 cur_term->Nttyb.sg_flags |= RAW; 99 #endif 100 returnCode(_nc_set_tty_mode(&cur_term->Nttyb)); 101 } 102 returnCode(ERR); 103 } 104 105 NCURSES_EXPORT(int) 106 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 NCURSES_EXPORT(void) 131 qiflush(void) 132 { 133 T((T_CALLED("qiflush()"))); 134 135 /* 136 * Note: this implementation may be wrong. See the comment under 137 * intrflush(). 138 */ 139 140 #ifdef TERMIOS 141 BEFORE("qiflush"); 142 cur_term->Nttyb.c_lflag &= ~(NOFLSH); 143 AFTER("qiflush"); 144 (void) _nc_set_tty_mode(&cur_term->Nttyb); 145 returnVoid; 146 #endif 147 } 148 149 NCURSES_EXPORT(int) 150 noraw(void) 151 { 152 T((T_CALLED("noraw()"))); 153 154 SP->_raw = FALSE; 155 SP->_cbreak = 0; 156 157 #ifdef __EMX__ 158 setmode(SP->_ifd, O_TEXT); 159 #endif 160 161 #ifdef TERMIOS 162 BEFORE("noraw"); 163 cur_term->Nttyb.c_lflag |= ISIG | ICANON | 164 (cur_term->Ottyb.c_lflag & IEXTEN); 165 cur_term->Nttyb.c_iflag |= COOKED_INPUT; 166 AFTER("noraw"); 167 #else 168 cur_term->Nttyb.sg_flags &= ~(RAW | CBREAK); 169 #endif 170 returnCode(_nc_set_tty_mode(&cur_term->Nttyb)); 171 } 172 173 NCURSES_EXPORT(int) 174 nocbreak(void) 175 { 176 T((T_CALLED("nocbreak()"))); 177 178 SP->_cbreak = 0; 179 180 #ifdef __EMX__ 181 setmode(SP->_ifd, O_TEXT); 182 #endif 183 184 #ifdef TERMIOS 185 BEFORE("nocbreak"); 186 cur_term->Nttyb.c_lflag |= ICANON; 187 cur_term->Nttyb.c_iflag |= ICRNL; 188 AFTER("nocbreak"); 189 #else 190 cur_term->Nttyb.sg_flags &= ~CBREAK; 191 #endif 192 returnCode(_nc_set_tty_mode(&cur_term->Nttyb)); 193 } 194 195 NCURSES_EXPORT(void) 196 noqiflush(void) 197 { 198 T((T_CALLED("noqiflush()"))); 199 200 /* 201 * Note: this implementation may be wrong. See the comment under 202 * intrflush(). 203 */ 204 205 #ifdef TERMIOS 206 BEFORE("noqiflush"); 207 cur_term->Nttyb.c_lflag |= NOFLSH; 208 AFTER("noqiflush"); 209 (void) _nc_set_tty_mode(&cur_term->Nttyb); 210 returnVoid; 211 #endif 212 } 213 214 NCURSES_EXPORT(int) 215 intrflush(WINDOW *win GCC_UNUSED, bool flag) 216 { 217 T((T_CALLED("intrflush(%d)"), flag)); 218 219 /* 220 * This call does the same thing as the qiflush()/noqiflush() pair. We 221 * know for certain that SVr3 intrflush() tweaks the NOFLSH bit; on the 222 * other hand, the match (in the SVr4 man pages) between the language 223 * describing NOFLSH in termio(7) and the language describing 224 * qiflush()/noqiflush() in curs_inopts(3x) is too exact to be coincidence. 225 */ 226 227 #ifdef TERMIOS 228 BEFORE("intrflush"); 229 if (flag) 230 cur_term->Nttyb.c_lflag &= ~(NOFLSH); 231 else 232 cur_term->Nttyb.c_lflag |= (NOFLSH); 233 AFTER("intrflush"); 234 returnCode(_nc_set_tty_mode(&cur_term->Nttyb)); 235 #else 236 returnCode(ERR); 237 #endif 238 } 239