1 /****************************************************************************
2 * Copyright 2020 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 * and: Thomas E. Dickey 1996-on *
34 ****************************************************************************/
41 #include <curses.priv.h>
42 #include <termcap.h> /* ospeed */
43 #if defined(__FreeBSD__) || defined(__OpenBSD__)
44 #include <sys/param.h>
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.
53 #if NCURSES_OSPEED_COMPAT && \
54 ((defined(__FreeBSD__) && (__FreeBSD_version < 700000)) || \
55 defined(__NetBSD__) || \
56 ((defined(__OpenBSD__) && OpenBSD < 201510)) || \
82 #include <sys/ttydev.h>
85 #endif /* USE_OLD_TTY */
87 MODULE_ID("$Id: lib_baudrate.c,v 1.45 2020/09/05 21:15:32 tom Exp $")
93 * Returns the current terminal's baud rate.
98 int given_speed; /* values for 'ospeed' */
99 int actual_speed; /* the actual speed */
102 #if !defined(EXP_WIN32_DRIVER)
103 #define DATA(number) { B##number, number }
105 static struct speed const speeds[] =
137 /* ifdef to prevent overflow when OLD_TTY is not available */
138 #if !(NCURSES_OSPEED_COMPAT && defined(__FreeBSD__) && (__FreeBSD_version > 700000))
192 #endif /* !EXP_WIN32_DRIVER */
195 _nc_baudrate(int OSpeed)
197 #if defined(EXP_WIN32_DRIVER)
198 /* On Windows this is a noop */
203 static int last_OSpeed;
204 static int last_baudrate;
210 OSpeed = (NCURSES_OSPEED) OSpeed;
212 OSpeed = (unsigned short) OSpeed;
214 if (OSpeed == last_OSpeed) {
215 result = last_baudrate;
222 for (i = 0; i < SIZEOF(speeds); i++) {
223 if (speeds[i].given_speed > OSpeed) {
226 if (speeds[i].given_speed == OSpeed) {
227 result = speeds[i].actual_speed;
233 if (OSpeed != last_OSpeed) {
234 last_OSpeed = OSpeed;
235 last_baudrate = result;
240 #endif /* !EXP_WIN32_DRIVER */
244 _nc_ospeed(int BaudRate)
247 #if defined(EXP_WIN32_DRIVER)
253 for (i = 0; i < SIZEOF(speeds); i++) {
254 if (speeds[i].actual_speed == BaudRate) {
255 result = speeds[i].given_speed;
265 NCURSES_SP_NAME(baudrate) (NCURSES_SP_DCL0)
269 T((T_CALLED("baudrate(%p)"), (void *) SP_PARM));
271 #if defined(EXP_WIN32_DRIVER)
275 * In debugging, allow the environment symbol to override when we're
276 * redirecting to a file, so we can construct repeatable test-cases
277 * that take into account costs that depend on baudrate.
280 if (IsValidTIScreen(SP_PARM)
281 && !NC_ISATTY(fileno((SP_PARM && SP_PARM->_ofp) ? SP_PARM->_ofp : stdout))
282 && getenv("BAUDRATE") != 0) {
284 if ((ret = _nc_getenv_num("BAUDRATE")) <= 0)
286 ospeed = (NCURSES_OSPEED) _nc_ospeed(ret);
291 if (IsValidTIScreen(SP_PARM)) {
293 result = (int) cfgetospeed(&(TerminalOf(SP_PARM)->Nttyb));
294 ospeed = (NCURSES_OSPEED) _nc_ospeed(result);
295 #else /* !USE_OLD_TTY */
297 ospeed = (NCURSES_OSPEED) cfgetospeed(&(TerminalOf(SP_PARM)->Nttyb));
299 ospeed = (NCURSES_OSPEED) TerminalOf(SP_PARM)->Nttyb.sg_ospeed;
301 result = _nc_baudrate(ospeed);
303 TerminalOf(SP_PARM)->_baudrate = result;
307 #endif /* !EXP_WIN32_DRIVER */
315 return NCURSES_SP_NAME(baudrate) (CURRENT_SCREEN);