1 /****************************************************************************
2  * Copyright 2020 Thomas E. Dickey                                          *
3  * Copyright 1998-2016,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
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>
42 #include <termcap.h>		/* ospeed */
43 #if defined(__FreeBSD__) || defined(__OpenBSD__)
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 NCURSES_OSPEED_COMPAT && \
54  	((defined(__FreeBSD__) && (__FreeBSD_version < 700000)) || \
55 	defined(__NetBSD__) || \
56 	((defined(__OpenBSD__) && OpenBSD < 201510)) || \
57 	defined(__APPLE__))
58 #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 
87 MODULE_ID("$Id: lib_baudrate.c,v 1.44 2020/02/02 23:34:34 tom Exp $")
88 
89 /*
90  *	int
91  *	baudrate()
92  *
93  *	Returns the current terminal's baud rate.
94  *
95  */
96 
97 struct speed {
98     int given_speed;		/* values for 'ospeed' */
99     int actual_speed;		/* the actual speed */
100 };
101 
102 #define DATA(number) { (NCURSES_OSPEED)B##number, number }
103 
104 static struct speed const speeds[] =
105 {
106     DATA(0),
107     DATA(50),
108     DATA(75),
109     DATA(110),
110     DATA(134),
111     DATA(150),
112     DATA(200),
113     DATA(300),
114     DATA(600),
115     DATA(1200),
116     DATA(1800),
117     DATA(2400),
118     DATA(4800),
119     DATA(9600),
120 #ifdef B19200
121     DATA(19200),
122 #elif defined(EXTA)
123     {EXTA, 19200},
124 #endif
125 #ifdef B28800
126     DATA(28800),
127 #endif
128 #ifdef B38400
129     DATA(38400),
130 #elif defined(EXTB)
131     {EXTB, 38400},
132 #endif
133 #ifdef B57600
134     DATA(57600),
135 #endif
136     /* ifdef to prevent overflow when OLD_TTY is not available */
137 #if !(NCURSES_OSPEED_COMPAT && defined(__FreeBSD__) && (__FreeBSD_version > 700000))
138 #ifdef B76800
139     DATA(76800),
140 #endif
141 #ifdef B115200
142     DATA(115200),
143 #endif
144 #ifdef B153600
145     DATA(153600),
146 #endif
147 #ifdef B230400
148     DATA(230400),
149 #endif
150 #ifdef B307200
151     DATA(307200),
152 #endif
153 #ifdef B460800
154     DATA(460800),
155 #endif
156 #ifdef B500000
157     DATA(500000),
158 #endif
159 #ifdef B576000
160     DATA(576000),
161 #endif
162 #ifdef B921600
163     DATA(921600),
164 #endif
165 #ifdef B1000000
166     DATA(1000000),
167 #endif
168 #ifdef B1152000
169     DATA(1152000),
170 #endif
171 #ifdef B1500000
172     DATA(1500000),
173 #endif
174 #ifdef B2000000
175     DATA(2000000),
176 #endif
177 #ifdef B2500000
178     DATA(2500000),
179 #endif
180 #ifdef B3000000
181     DATA(3000000),
182 #endif
183 #ifdef B3500000
184     DATA(3500000),
185 #endif
186 #ifdef B4000000
187     DATA(4000000),
188 #endif
189 #endif
190 };
191 
192 NCURSES_EXPORT(int)
_nc_baudrate(int OSpeed)193 _nc_baudrate(int OSpeed)
194 {
195 #if !USE_REENTRANT
196     static int last_OSpeed;
197     static int last_baudrate;
198 #endif
199 
200     int result = ERR;
201 
202     if (OSpeed < 0)
203 	OSpeed = (NCURSES_OSPEED) OSpeed;
204     if (OSpeed < 0)
205 	OSpeed = (unsigned short) OSpeed;
206 #if !USE_REENTRANT
207     if (OSpeed == last_OSpeed) {
208 	result = last_baudrate;
209     }
210 #endif
211     if (result == ERR) {
212 	if (OSpeed >= 0) {
213 	    unsigned i;
214 
215 	    for (i = 0; i < SIZEOF(speeds); i++) {
216 		if (speeds[i].given_speed > OSpeed) {
217 		    break;
218 		}
219 		if (speeds[i].given_speed == OSpeed) {
220 		    result = speeds[i].actual_speed;
221 		    break;
222 		}
223 	    }
224 	}
225 #if !USE_REENTRANT
226 	if (OSpeed != last_OSpeed) {
227 	    last_OSpeed = OSpeed;
228 	    last_baudrate = result;
229 	}
230 #endif
231     }
232     return (result);
233 }
234 
235 NCURSES_EXPORT(int)
_nc_ospeed(int BaudRate)236 _nc_ospeed(int BaudRate)
237 {
238     int result = 1;
239 
240     if (BaudRate >= 0) {
241 	unsigned i;
242 
243 	for (i = 0; i < SIZEOF(speeds); i++) {
244 	    if (speeds[i].actual_speed == BaudRate) {
245 		result = speeds[i].given_speed;
246 		break;
247 	    }
248 	}
249     }
250     return (result);
251 }
252 
253 NCURSES_EXPORT(int)
NCURSES_SP_NAME(baudrate)254 NCURSES_SP_NAME(baudrate) (NCURSES_SP_DCL0)
255 {
256     int result;
257 
258     T((T_CALLED("baudrate(%p)"), (void *) SP_PARM));
259 
260     /*
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.
264      */
265 #ifdef TRACE
266     if (IsValidTIScreen(SP_PARM)
267 	&& !NC_ISATTY(fileno((SP_PARM && SP_PARM->_ofp) ? SP_PARM->_ofp : stdout))
268 	&& getenv("BAUDRATE") != 0) {
269 	int ret;
270 	if ((ret = _nc_getenv_num("BAUDRATE")) <= 0)
271 	    ret = 9600;
272 	ospeed = (NCURSES_OSPEED) _nc_ospeed(ret);
273 	returnCode(ret);
274     }
275 #endif
276 
277     if (IsValidTIScreen(SP_PARM)) {
278 #ifdef USE_OLD_TTY
279 	result = (int) cfgetospeed(&(TerminalOf(SP_PARM)->Nttyb));
280 	ospeed = (NCURSES_OSPEED) _nc_ospeed(result);
281 #else /* !USE_OLD_TTY */
282 #ifdef TERMIOS
283 	ospeed = (NCURSES_OSPEED) cfgetospeed(&(TerminalOf(SP_PARM)->Nttyb));
284 #else
285 	ospeed = (NCURSES_OSPEED) TerminalOf(SP_PARM)->Nttyb.sg_ospeed;
286 #endif
287 	result = _nc_baudrate(ospeed);
288 #endif
289 	TerminalOf(SP_PARM)->_baudrate = result;
290     } else {
291 	result = ERR;
292     }
293 
294     returnCode(result);
295 }
296 
297 #if NCURSES_SP_FUNCS
298 NCURSES_EXPORT(int)
baudrate(void)299 baudrate(void)
300 {
301     return NCURSES_SP_NAME(baudrate) (CURRENT_SCREEN);
302 }
303 #endif
304