1 /* $OpenBSD: lib_raw.c,v 1.3 1999/12/28 15:57:59 millert Exp $ */ 2 3 /**************************************************************************** 4 * Copyright (c) 1998,1999 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.5 1999/12/19 01:57:23 tom Exp $") 54 55 #if defined(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 #include <fcntl.h> 66 #endif 67 68 #define COOKED_INPUT (IXON|BRKINT|PARMRK) 69 70 #ifdef TRACE 71 #define BEFORE(N) if (_nc_tracing&TRACE_BITS) _tracef("%s before bits: %s", N, _nc_tracebits()) 72 #define AFTER(N) if (_nc_tracing&TRACE_BITS) _tracef("%s after bits: %s", N, _nc_tracebits()) 73 #else 74 #define BEFORE(s) 75 #define AFTER(s) 76 #endif /* TRACE */ 77 78 int 79 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 | IEXTEN); 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 107 cbreak(void) 108 { 109 T((T_CALLED("cbreak()"))); 110 111 SP->_cbreak = 1; 112 113 #ifdef __EMX__ 114 setmode(SP->_ifd, O_BINARY); 115 #endif 116 117 #ifdef TERMIOS 118 BEFORE("cbreak"); 119 cur_term->Nttyb.c_lflag &= ~ICANON; 120 cur_term->Nttyb.c_iflag &= ~ICRNL; 121 cur_term->Nttyb.c_lflag |= ISIG; 122 cur_term->Nttyb.c_cc[VMIN] = 1; 123 cur_term->Nttyb.c_cc[VTIME] = 0; 124 AFTER("cbreak"); 125 #else 126 cur_term->Nttyb.sg_flags |= CBREAK; 127 #endif 128 returnCode(_nc_set_tty_mode(&cur_term->Nttyb)); 129 } 130 131 void 132 qiflush(void) 133 { 134 T((T_CALLED("qiflush()"))); 135 136 /* 137 * Note: this implementation may be wrong. See the comment under 138 * intrflush(). 139 */ 140 141 #ifdef TERMIOS 142 BEFORE("qiflush"); 143 cur_term->Nttyb.c_lflag &= ~(NOFLSH); 144 AFTER("qiflush"); 145 (void) _nc_set_tty_mode(&cur_term->Nttyb); 146 returnVoid; 147 #endif 148 } 149 150 int 151 noraw(void) 152 { 153 T((T_CALLED("noraw()"))); 154 155 SP->_raw = FALSE; 156 SP->_cbreak = 0; 157 158 #ifdef __EMX__ 159 setmode(SP->_ifd, O_TEXT); 160 #endif 161 162 #ifdef TERMIOS 163 BEFORE("noraw"); 164 cur_term->Nttyb.c_lflag |= ISIG | ICANON | 165 (cur_term->Ottyb.c_lflag & IEXTEN); 166 cur_term->Nttyb.c_iflag |= COOKED_INPUT; 167 AFTER("noraw"); 168 #else 169 cur_term->Nttyb.sg_flags &= ~(RAW | CBREAK); 170 #endif 171 returnCode(_nc_set_tty_mode(&cur_term->Nttyb)); 172 } 173 174 int 175 nocbreak(void) 176 { 177 T((T_CALLED("nocbreak()"))); 178 179 SP->_cbreak = 0; 180 181 #ifdef __EMX__ 182 setmode(SP->_ifd, O_TEXT); 183 #endif 184 185 #ifdef TERMIOS 186 BEFORE("nocbreak"); 187 cur_term->Nttyb.c_lflag |= ICANON; 188 cur_term->Nttyb.c_iflag |= ICRNL; 189 AFTER("nocbreak"); 190 #else 191 cur_term->Nttyb.sg_flags &= ~CBREAK; 192 #endif 193 returnCode(_nc_set_tty_mode(&cur_term->Nttyb)); 194 } 195 196 void 197 noqiflush(void) 198 { 199 T((T_CALLED("noqiflush()"))); 200 201 /* 202 * Note: this implementation may be wrong. See the comment under 203 * intrflush(). 204 */ 205 206 #ifdef TERMIOS 207 BEFORE("noqiflush"); 208 cur_term->Nttyb.c_lflag |= NOFLSH; 209 AFTER("noqiflush"); 210 (void) _nc_set_tty_mode(&cur_term->Nttyb); 211 returnVoid; 212 #endif 213 } 214 215 int 216 intrflush(WINDOW *win GCC_UNUSED, bool flag) 217 { 218 T((T_CALLED("intrflush(%d)"), flag)); 219 220 /* 221 * This call does the same thing as the qiflush()/noqiflush() 222 * pair. We know for certain that SVr3 intrflush() tweaks the 223 * NOFLSH bit; on the other hand, the match (in the SVr4 man 224 * pages) between the language describing NOFLSH in termio(7) 225 * and the language describing qiflush()/noqiflush() in 226 * curs_inopts(3x) is too exact to be coincidence. 227 */ 228 229 #ifdef TERMIOS 230 BEFORE("intrflush"); 231 if (flag) 232 cur_term->Nttyb.c_lflag &= ~(NOFLSH); 233 else 234 cur_term->Nttyb.c_lflag |= (NOFLSH); 235 AFTER("intrflush"); 236 returnCode(_nc_set_tty_mode(&cur_term->Nttyb)); 237 #else 238 returnCode(ERR); 239 #endif 240 } 241