1 /****************************************************************************
2 * Copyright (c) 1998-2016,2017 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 * and: Thomas E. Dickey 1996-on *
33 ****************************************************************************/
40 #include <curses.priv.h>
41 #include <termcap.h> /* ospeed */
42 #if defined(__FreeBSD__) || defined(__OpenBSD__)
43 #include <sys/param.h>
47 * These systems use similar header files, which define B1200 as 1200, etc.,
48 * but can be overridden by defining USE_OLD_TTY so B1200 is 9, which makes all
49 * of the indices up to B115200 fit nicely in a 'short', allowing us to retain
50 * ospeed's type for compatibility.
52 #if NCURSES_OSPEED_COMPAT && \
53 ((defined(__FreeBSD__) && (__FreeBSD_version < 700000)) || \
54 defined(__NetBSD__) || \
55 ((defined(__OpenBSD__) && OpenBSD < 201510)) || \
81 #include <sys/ttydev.h>
84 #endif /* USE_OLD_TTY */
86 MODULE_ID("$Id: lib_baudrate.c,v 1.43 2017/03/31 17:19:30 tom Exp $")
92 * Returns the current terminal's baud rate.
97 int given_speed; /* values for 'ospeed' */
98 int actual_speed; /* the actual speed */
101 #define DATA(number) { B##number, number }
103 static struct speed const speeds[] =
135 /* ifdef to prevent overflow when OLD_TTY is not available */
136 #if !(NCURSES_OSPEED_COMPAT && defined(__FreeBSD__) && (__FreeBSD_version > 700000))
192 _nc_baudrate(int OSpeed)
195 static int last_OSpeed;
196 static int last_baudrate;
202 OSpeed = (NCURSES_OSPEED) OSpeed;
204 OSpeed = (unsigned short) OSpeed;
206 if (OSpeed == last_OSpeed) {
207 result = last_baudrate;
214 for (i = 0; i < SIZEOF(speeds); i++) {
215 if (speeds[i].given_speed > OSpeed) {
218 if (speeds[i].given_speed == OSpeed) {
219 result = speeds[i].actual_speed;
225 if (OSpeed != last_OSpeed) {
226 last_OSpeed = OSpeed;
227 last_baudrate = result;
235 _nc_ospeed(int BaudRate)
242 for (i = 0; i < SIZEOF(speeds); i++) {
243 if (speeds[i].actual_speed == BaudRate) {
244 result = speeds[i].given_speed;
253 NCURSES_SP_NAME(baudrate) (NCURSES_SP_DCL0)
257 T((T_CALLED("baudrate(%p)"), (void *) SP_PARM));
260 * In debugging, allow the environment symbol to override when we're
261 * redirecting to a file, so we can construct repeatable test-cases
262 * that take into account costs that depend on baudrate.
265 if (IsValidTIScreen(SP_PARM)
266 && !NC_ISATTY(fileno((SP_PARM && SP_PARM->_ofp) ? SP_PARM->_ofp : stdout))
267 && getenv("BAUDRATE") != 0) {
269 if ((ret = _nc_getenv_num("BAUDRATE")) <= 0)
271 ospeed = (NCURSES_OSPEED) _nc_ospeed(ret);
276 if (IsValidTIScreen(SP_PARM)) {
278 result = (int) cfgetospeed(&(TerminalOf(SP_PARM)->Nttyb));
279 ospeed = (NCURSES_OSPEED) _nc_ospeed(result);
280 #else /* !USE_OLD_TTY */
282 ospeed = (NCURSES_OSPEED) cfgetospeed(&(TerminalOf(SP_PARM)->Nttyb));
284 ospeed = (NCURSES_OSPEED) TerminalOf(SP_PARM)->Nttyb.sg_ospeed;
286 result = _nc_baudrate(ospeed);
288 TerminalOf(SP_PARM)->_baudrate = result;
300 return NCURSES_SP_NAME(baudrate) (CURRENT_SCREEN);