1 /**************************************************************************** 2 * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc. * 3 * * 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: * 11 * * 12 * The above copyright notice and this permission notice shall be included * 13 * in all copies or substantial portions of the Software. * 14 * * 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. * 22 * * 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 * 26 * authorization. * 27 ****************************************************************************/ 28 29 /**************************************************************************** 30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * 31 * and: Eric S. Raymond <esr@snark.thyrsus.com> * 32 * and: Thomas E. Dickey 1996-on * 33 ****************************************************************************/ 34 35 /* 36 * lib_baudrate.c 37 * 38 */ 39 40 #include <curses.priv.h> 41 #include <term.h> /* cur_term, pad_char */ 42 #include <termcap.h> /* ospeed */ 43 #if defined(__FreeBSD__) 44 #include <sys/param.h> 45 #endif 46 47 /* 48 * These systems use similar header files, which define B1200 as 1200, etc., 49 * but can be overridden by defining USE_OLD_TTY so B1200 is 9, which makes all 50 * of the indices up to B115200 fit nicely in a 'short', allowing us to retain 51 * ospeed's type for compatibility. 52 */ 53 #if (defined(__FreeBSD__) && (__FreeBSD_version < 700000)) || defined(__NetBSD__) || defined(__OpenBSD__) 54 #undef B0 55 #undef B50 56 #undef B75 57 #undef B110 58 #undef B134 59 #undef B150 60 #undef B200 61 #undef B300 62 #undef B600 63 #undef B1200 64 #undef B1800 65 #undef B2400 66 #undef B4800 67 #undef B9600 68 #undef B19200 69 #undef EXTA 70 #undef B38400 71 #undef EXTB 72 #undef B57600 73 #undef B115200 74 #undef B230400 75 #undef B460800 76 #undef B921600 77 #define USE_OLD_TTY 78 #include <sys/ttydev.h> 79 #else 80 #undef USE_OLD_TTY 81 #endif /* USE_OLD_TTY */ 82 83 MODULE_ID("$Id: lib_baudrate.c,v 1.27 2008/06/28 15:19:24 tom Exp $") 84 85 /* 86 * int 87 * baudrate() 88 * 89 * Returns the current terminal's baud rate. 90 * 91 */ 92 93 struct speed { 94 int s; /* value for 'ospeed' is an index */ 95 int sp; /* the actual speed */ 96 }; 97 98 static struct speed const speeds[] = 99 { 100 {B0, 0}, 101 {B50, 50}, 102 {B75, 75}, 103 {B110, 110}, 104 {B134, 134}, 105 {B150, 150}, 106 {B200, 200}, 107 {B300, 300}, 108 {B600, 600}, 109 {B1200, 1200}, 110 {B1800, 1800}, 111 {B2400, 2400}, 112 {B4800, 4800}, 113 {B9600, 9600}, 114 #ifdef B19200 115 {B19200, 19200}, 116 #else 117 #ifdef EXTA 118 {EXTA, 19200}, 119 #endif 120 #endif 121 #ifdef B38400 122 {B38400, 38400}, 123 #else 124 #ifdef EXTB 125 {EXTB, 38400}, 126 #endif 127 #endif 128 #ifdef B57600 129 {B57600, 57600}, 130 #endif 131 #ifdef B115200 132 {B115200, 115200}, 133 #endif 134 #ifdef B230400 135 {B230400, 230400}, 136 #endif 137 #ifdef B460800 138 {B460800, 460800}, 139 #endif 140 #ifdef B921600 141 {B921600, 921600}, 142 #endif 143 }; 144 145 NCURSES_EXPORT(int) 146 _nc_baudrate(int OSpeed) 147 { 148 #if !USE_REENTRANT 149 static int last_OSpeed; 150 static int last_baudrate; 151 #endif 152 153 int result = ERR; 154 unsigned i; 155 156 #if !USE_REENTRANT 157 if (OSpeed == last_OSpeed) { 158 result = last_baudrate; 159 } 160 #endif 161 if (result == ERR) { 162 if (OSpeed >= 0) { 163 for (i = 0; i < SIZEOF(speeds); i++) { 164 if (speeds[i].s == OSpeed) { 165 result = speeds[i].sp; 166 break; 167 } 168 } 169 } 170 #if !USE_REENTRANT 171 if (OSpeed == last_OSpeed) { 172 last_OSpeed = OSpeed; 173 last_baudrate = result; 174 } 175 #endif 176 } 177 return (result); 178 } 179 180 NCURSES_EXPORT(int) 181 _nc_ospeed(int BaudRate) 182 { 183 int result = 1; 184 unsigned i; 185 186 if (BaudRate >= 0) { 187 for (i = 0; i < SIZEOF(speeds); i++) { 188 if (speeds[i].sp == BaudRate) { 189 result = speeds[i].s; 190 break; 191 } 192 } 193 } 194 return (result); 195 } 196 197 NCURSES_EXPORT(int) 198 baudrate(void) 199 { 200 int result; 201 202 T((T_CALLED("baudrate()"))); 203 204 /* 205 * In debugging, allow the environment symbol to override when we're 206 * redirecting to a file, so we can construct repeatable test-cases 207 * that take into account costs that depend on baudrate. 208 */ 209 #ifdef TRACE 210 if (!isatty(fileno(SP ? SP->_ofp : stdout)) 211 && getenv("BAUDRATE") != 0) { 212 int ret; 213 if ((ret = _nc_getenv_num("BAUDRATE")) <= 0) 214 ret = 9600; 215 ospeed = _nc_ospeed(ret); 216 returnCode(ret); 217 } 218 #endif 219 220 if (cur_term != 0) { 221 #ifdef USE_OLD_TTY 222 result = cfgetospeed(&cur_term->Nttyb); 223 ospeed = _nc_ospeed(result); 224 #else /* !USE_OLD_TTY */ 225 #ifdef TERMIOS 226 ospeed = cfgetospeed(&cur_term->Nttyb); 227 #else 228 ospeed = cur_term->Nttyb.sg_ospeed; 229 #endif 230 result = _nc_baudrate(ospeed); 231 #endif 232 cur_term->_baudrate = result; 233 } else { 234 result = ERR; 235 } 236 237 returnCode(result); 238 } 239