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.44 2020/02/02 23:34:34 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 #define DATA(number) { B##number, number }
104 static struct speed const speeds[] =
136 /* ifdef to prevent overflow when OLD_TTY is not available */
137 #if !(NCURSES_OSPEED_COMPAT && defined(__FreeBSD__) && (__FreeBSD_version > 700000))
193 _nc_baudrate(int OSpeed)
196 static int last_OSpeed;
197 static int last_baudrate;
203 OSpeed = (NCURSES_OSPEED) OSpeed;
205 OSpeed = (unsigned short) OSpeed;
207 if (OSpeed == last_OSpeed) {
208 result = last_baudrate;
215 for (i = 0; i < SIZEOF(speeds); i++) {
216 if (speeds[i].given_speed > OSpeed) {
219 if (speeds[i].given_speed == OSpeed) {
220 result = speeds[i].actual_speed;
226 if (OSpeed != last_OSpeed) {
227 last_OSpeed = OSpeed;
228 last_baudrate = result;
236 _nc_ospeed(int BaudRate)
243 for (i = 0; i < SIZEOF(speeds); i++) {
244 if (speeds[i].actual_speed == BaudRate) {
245 result = speeds[i].given_speed;
254 NCURSES_SP_NAME(baudrate) (NCURSES_SP_DCL0)
258 T((T_CALLED("baudrate(%p)"), (void *) SP_PARM));
261 * In debugging, allow the environment symbol to override when we're
262 * redirecting to a file, so we can construct repeatable test-cases
263 * that take into account costs that depend on baudrate.
266 if (IsValidTIScreen(SP_PARM)
267 && !NC_ISATTY(fileno((SP_PARM && SP_PARM->_ofp) ? SP_PARM->_ofp : stdout))
268 && getenv("BAUDRATE") != 0) {
270 if ((ret = _nc_getenv_num("BAUDRATE")) <= 0)
272 ospeed = (NCURSES_OSPEED) _nc_ospeed(ret);
277 if (IsValidTIScreen(SP_PARM)) {
279 result = (int) cfgetospeed(&(TerminalOf(SP_PARM)->Nttyb));
280 ospeed = (NCURSES_OSPEED) _nc_ospeed(result);
281 #else /* !USE_OLD_TTY */
283 ospeed = (NCURSES_OSPEED) cfgetospeed(&(TerminalOf(SP_PARM)->Nttyb));
285 ospeed = (NCURSES_OSPEED) TerminalOf(SP_PARM)->Nttyb.sg_ospeed;
287 result = _nc_baudrate(ospeed);
289 TerminalOf(SP_PARM)->_baudrate = result;
301 return NCURSES_SP_NAME(baudrate) (CURRENT_SCREEN);