blob: 311c41ac97fa4743e88b47cc664d24409080cb01 [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2020 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05304 * *
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: *
12 * *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
15 * *
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. *
23 * *
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 *
27 * authorization. *
28 ****************************************************************************/
29
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 ****************************************************************************/
35
36/*
37 * lib_baudrate.c
38 *
39 */
40
41#include <curses.priv.h>
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053042#include <termcap.h> /* ospeed */
micky3879b9f5e72025-07-08 18:04:53 -040043#if defined(__FreeBSD__) || defined(__OpenBSD__)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053044#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 */
micky3879b9f5e72025-07-08 18:04:53 -040053#if NCURSES_OSPEED_COMPAT && \
54 ((defined(__FreeBSD__) && (__FreeBSD_version < 700000)) || \
55 defined(__NetBSD__) || \
56 ((defined(__OpenBSD__) && OpenBSD < 201510)) || \
57 defined(__APPLE__))
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053058#undef B0
59#undef B50
60#undef B75
61#undef B110
62#undef B134
63#undef B150
64#undef B200
65#undef B300
66#undef B600
67#undef B1200
68#undef B1800
69#undef B2400
70#undef B4800
71#undef B9600
72#undef B19200
73#undef EXTA
74#undef B38400
75#undef EXTB
76#undef B57600
77#undef B115200
78#undef B230400
79#undef B460800
80#undef B921600
81#define USE_OLD_TTY
82#include <sys/ttydev.h>
83#else
84#undef USE_OLD_TTY
85#endif /* USE_OLD_TTY */
86
micky3879b9f5e72025-07-08 18:04:53 -040087MODULE_ID("$Id: lib_baudrate.c,v 1.45 2020/09/05 21:15:32 tom Exp $")
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053088
89/*
90 * int
91 * baudrate()
92 *
93 * Returns the current terminal's baud rate.
94 *
95 */
96
97struct speed {
micky3879b9f5e72025-07-08 18:04:53 -040098 int given_speed; /* values for 'ospeed' */
99 int actual_speed; /* the actual speed */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530100};
101
micky3879b9f5e72025-07-08 18:04:53 -0400102#if !defined(EXP_WIN32_DRIVER)
Steve Kondikae271bc2015-11-15 02:50:53 +0100103#define DATA(number) { B##number, number }
104
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530105static struct speed const speeds[] =
106{
Steve Kondikae271bc2015-11-15 02:50:53 +0100107 DATA(0),
108 DATA(50),
109 DATA(75),
110 DATA(110),
111 DATA(134),
112 DATA(150),
113 DATA(200),
114 DATA(300),
115 DATA(600),
116 DATA(1200),
117 DATA(1800),
118 DATA(2400),
119 DATA(4800),
120 DATA(9600),
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530121#ifdef B19200
Steve Kondikae271bc2015-11-15 02:50:53 +0100122 DATA(19200),
123#elif defined(EXTA)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530124 {EXTA, 19200},
125#endif
micky3879b9f5e72025-07-08 18:04:53 -0400126#ifdef B28800
127 DATA(28800),
128#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530129#ifdef B38400
Steve Kondikae271bc2015-11-15 02:50:53 +0100130 DATA(38400),
131#elif defined(EXTB)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530132 {EXTB, 38400},
133#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530134#ifdef B57600
Steve Kondikae271bc2015-11-15 02:50:53 +0100135 DATA(57600),
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530136#endif
Steve Kondikae271bc2015-11-15 02:50:53 +0100137 /* ifdef to prevent overflow when OLD_TTY is not available */
138#if !(NCURSES_OSPEED_COMPAT && defined(__FreeBSD__) && (__FreeBSD_version > 700000))
micky3879b9f5e72025-07-08 18:04:53 -0400139#ifdef B76800
140 DATA(76800),
141#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530142#ifdef B115200
Steve Kondikae271bc2015-11-15 02:50:53 +0100143 DATA(115200),
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530144#endif
micky3879b9f5e72025-07-08 18:04:53 -0400145#ifdef B153600
146 DATA(153600),
147#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530148#ifdef B230400
Steve Kondikae271bc2015-11-15 02:50:53 +0100149 DATA(230400),
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530150#endif
micky3879b9f5e72025-07-08 18:04:53 -0400151#ifdef B307200
152 DATA(307200),
153#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530154#ifdef B460800
Steve Kondikae271bc2015-11-15 02:50:53 +0100155 DATA(460800),
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530156#endif
micky3879b9f5e72025-07-08 18:04:53 -0400157#ifdef B500000
158 DATA(500000),
159#endif
160#ifdef B576000
161 DATA(576000),
162#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530163#ifdef B921600
Steve Kondikae271bc2015-11-15 02:50:53 +0100164 DATA(921600),
165#endif
micky3879b9f5e72025-07-08 18:04:53 -0400166#ifdef B1000000
167 DATA(1000000),
168#endif
169#ifdef B1152000
170 DATA(1152000),
171#endif
172#ifdef B1500000
173 DATA(1500000),
174#endif
175#ifdef B2000000
176 DATA(2000000),
177#endif
178#ifdef B2500000
179 DATA(2500000),
180#endif
181#ifdef B3000000
182 DATA(3000000),
183#endif
184#ifdef B3500000
185 DATA(3500000),
186#endif
187#ifdef B4000000
188 DATA(4000000),
189#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530190#endif
191};
micky3879b9f5e72025-07-08 18:04:53 -0400192#endif /* !EXP_WIN32_DRIVER */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530193
194NCURSES_EXPORT(int)
195_nc_baudrate(int OSpeed)
196{
micky3879b9f5e72025-07-08 18:04:53 -0400197#if defined(EXP_WIN32_DRIVER)
198 /* On Windows this is a noop */
199 (void) OSpeed;
200 return (OK);
201#else
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530202#if !USE_REENTRANT
203 static int last_OSpeed;
204 static int last_baudrate;
205#endif
206
207 int result = ERR;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530208
micky3879b9f5e72025-07-08 18:04:53 -0400209 if (OSpeed < 0)
210 OSpeed = (NCURSES_OSPEED) OSpeed;
211 if (OSpeed < 0)
212 OSpeed = (unsigned short) OSpeed;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530213#if !USE_REENTRANT
214 if (OSpeed == last_OSpeed) {
215 result = last_baudrate;
216 }
217#endif
218 if (result == ERR) {
219 if (OSpeed >= 0) {
micky3879b9f5e72025-07-08 18:04:53 -0400220 unsigned i;
221
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530222 for (i = 0; i < SIZEOF(speeds); i++) {
micky3879b9f5e72025-07-08 18:04:53 -0400223 if (speeds[i].given_speed > OSpeed) {
224 break;
225 }
226 if (speeds[i].given_speed == OSpeed) {
227 result = speeds[i].actual_speed;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530228 break;
229 }
230 }
231 }
232#if !USE_REENTRANT
Steve Kondikae271bc2015-11-15 02:50:53 +0100233 if (OSpeed != last_OSpeed) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530234 last_OSpeed = OSpeed;
235 last_baudrate = result;
236 }
237#endif
238 }
239 return (result);
micky3879b9f5e72025-07-08 18:04:53 -0400240#endif /* !EXP_WIN32_DRIVER */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530241}
242
243NCURSES_EXPORT(int)
244_nc_ospeed(int BaudRate)
245{
246 int result = 1;
micky3879b9f5e72025-07-08 18:04:53 -0400247#if defined(EXP_WIN32_DRIVER)
248 (void) BaudRate;
249#else
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530250 if (BaudRate >= 0) {
micky3879b9f5e72025-07-08 18:04:53 -0400251 unsigned i;
252
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530253 for (i = 0; i < SIZEOF(speeds); i++) {
micky3879b9f5e72025-07-08 18:04:53 -0400254 if (speeds[i].actual_speed == BaudRate) {
255 result = speeds[i].given_speed;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530256 break;
257 }
258 }
259 }
micky3879b9f5e72025-07-08 18:04:53 -0400260#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530261 return (result);
262}
263
264NCURSES_EXPORT(int)
Steve Kondikae271bc2015-11-15 02:50:53 +0100265NCURSES_SP_NAME(baudrate) (NCURSES_SP_DCL0)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530266{
267 int result;
268
Steve Kondikae271bc2015-11-15 02:50:53 +0100269 T((T_CALLED("baudrate(%p)"), (void *) SP_PARM));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530270
micky3879b9f5e72025-07-08 18:04:53 -0400271#if defined(EXP_WIN32_DRIVER)
272 result = OK;
273#else
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530274 /*
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.
278 */
279#ifdef TRACE
Steve Kondikae271bc2015-11-15 02:50:53 +0100280 if (IsValidTIScreen(SP_PARM)
micky3879b9f5e72025-07-08 18:04:53 -0400281 && !NC_ISATTY(fileno((SP_PARM && SP_PARM->_ofp) ? SP_PARM->_ofp : stdout))
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530282 && getenv("BAUDRATE") != 0) {
283 int ret;
284 if ((ret = _nc_getenv_num("BAUDRATE")) <= 0)
285 ret = 9600;
Steve Kondikae271bc2015-11-15 02:50:53 +0100286 ospeed = (NCURSES_OSPEED) _nc_ospeed(ret);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530287 returnCode(ret);
288 }
289#endif
290
Steve Kondikae271bc2015-11-15 02:50:53 +0100291 if (IsValidTIScreen(SP_PARM)) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530292#ifdef USE_OLD_TTY
Steve Kondikae271bc2015-11-15 02:50:53 +0100293 result = (int) cfgetospeed(&(TerminalOf(SP_PARM)->Nttyb));
294 ospeed = (NCURSES_OSPEED) _nc_ospeed(result);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530295#else /* !USE_OLD_TTY */
296#ifdef TERMIOS
Steve Kondikae271bc2015-11-15 02:50:53 +0100297 ospeed = (NCURSES_OSPEED) cfgetospeed(&(TerminalOf(SP_PARM)->Nttyb));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530298#else
Steve Kondikae271bc2015-11-15 02:50:53 +0100299 ospeed = (NCURSES_OSPEED) TerminalOf(SP_PARM)->Nttyb.sg_ospeed;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530300#endif
301 result = _nc_baudrate(ospeed);
302#endif
Steve Kondikae271bc2015-11-15 02:50:53 +0100303 TerminalOf(SP_PARM)->_baudrate = result;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530304 } else {
305 result = ERR;
306 }
micky3879b9f5e72025-07-08 18:04:53 -0400307#endif /* !EXP_WIN32_DRIVER */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530308 returnCode(result);
309}
Steve Kondikae271bc2015-11-15 02:50:53 +0100310
311#if NCURSES_SP_FUNCS
312NCURSES_EXPORT(int)
313baudrate(void)
314{
315 return NCURSES_SP_NAME(baudrate) (CURRENT_SCREEN);
316}
317#endif