]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/lib_baudrate.c
ncurses 6.1 - patch 20180908
[ncurses.git] / ncurses / tinfo / lib_baudrate.c
1 /****************************************************************************
2  * Copyright (c) 1998-2016,2017 Free Software Foundation, Inc.              *
3  *                                                                          *
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:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
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.                               *
22  *                                                                          *
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       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
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  ****************************************************************************/
34
35 /*
36  *      lib_baudrate.c
37  *
38  */
39
40 #include <curses.priv.h>
41 #include <termcap.h>            /* ospeed */
42 #if defined(__FreeBSD__) || defined(__OpenBSD__)
43 #include <sys/param.h>
44 #endif
45
46 /*
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.
51  */
52 #if NCURSES_OSPEED_COMPAT && \
53         ((defined(__FreeBSD__) && (__FreeBSD_version < 700000)) || \
54         defined(__NetBSD__) || \
55         ((defined(__OpenBSD__) && OpenBSD < 201510)) || \
56         defined(__APPLE__))
57 #undef B0
58 #undef B50
59 #undef B75
60 #undef B110
61 #undef B134
62 #undef B150
63 #undef B200
64 #undef B300
65 #undef B600
66 #undef B1200
67 #undef B1800
68 #undef B2400
69 #undef B4800
70 #undef B9600
71 #undef B19200
72 #undef EXTA
73 #undef B38400
74 #undef EXTB
75 #undef B57600
76 #undef B115200
77 #undef B230400
78 #undef B460800
79 #undef B921600
80 #define USE_OLD_TTY
81 #include <sys/ttydev.h>
82 #else
83 #undef USE_OLD_TTY
84 #endif /* USE_OLD_TTY */
85
86 MODULE_ID("$Id: lib_baudrate.c,v 1.43 2017/03/31 17:19:30 tom Exp $")
87
88 /*
89  *      int
90  *      baudrate()
91  *
92  *      Returns the current terminal's baud rate.
93  *
94  */
95
96 struct speed {
97     int given_speed;            /* values for 'ospeed' */
98     int actual_speed;           /* the actual speed */
99 };
100
101 #define DATA(number) { B##number, number }
102
103 static struct speed const speeds[] =
104 {
105     DATA(0),
106     DATA(50),
107     DATA(75),
108     DATA(110),
109     DATA(134),
110     DATA(150),
111     DATA(200),
112     DATA(300),
113     DATA(600),
114     DATA(1200),
115     DATA(1800),
116     DATA(2400),
117     DATA(4800),
118     DATA(9600),
119 #ifdef B19200
120     DATA(19200),
121 #elif defined(EXTA)
122     {EXTA, 19200},
123 #endif
124 #ifdef B28800
125     DATA(28800),
126 #endif
127 #ifdef B38400
128     DATA(38400),
129 #elif defined(EXTB)
130     {EXTB, 38400},
131 #endif
132 #ifdef B57600
133     DATA(57600),
134 #endif
135     /* ifdef to prevent overflow when OLD_TTY is not available */
136 #if !(NCURSES_OSPEED_COMPAT && defined(__FreeBSD__) && (__FreeBSD_version > 700000))
137 #ifdef B76800
138     DATA(76800),
139 #endif
140 #ifdef B115200
141     DATA(115200),
142 #endif
143 #ifdef B153600
144     DATA(153600),
145 #endif
146 #ifdef B230400
147     DATA(230400),
148 #endif
149 #ifdef B307200
150     DATA(307200),
151 #endif
152 #ifdef B460800
153     DATA(460800),
154 #endif
155 #ifdef B500000
156     DATA(500000),
157 #endif
158 #ifdef B576000
159     DATA(576000),
160 #endif
161 #ifdef B921600
162     DATA(921600),
163 #endif
164 #ifdef B1000000
165     DATA(1000000),
166 #endif
167 #ifdef B1152000
168     DATA(1152000),
169 #endif
170 #ifdef B1500000
171     DATA(1500000),
172 #endif
173 #ifdef B2000000
174     DATA(2000000),
175 #endif
176 #ifdef B2500000
177     DATA(2500000),
178 #endif
179 #ifdef B3000000
180     DATA(3000000),
181 #endif
182 #ifdef B3500000
183     DATA(3500000),
184 #endif
185 #ifdef B4000000
186     DATA(4000000),
187 #endif
188 #endif
189 };
190
191 NCURSES_EXPORT(int)
192 _nc_baudrate(int OSpeed)
193 {
194 #if !USE_REENTRANT
195     static int last_OSpeed;
196     static int last_baudrate;
197 #endif
198
199     int result = ERR;
200
201     if (OSpeed < 0)
202         OSpeed = (NCURSES_OSPEED) OSpeed;
203     if (OSpeed < 0)
204         OSpeed = (unsigned short) OSpeed;
205 #if !USE_REENTRANT
206     if (OSpeed == last_OSpeed) {
207         result = last_baudrate;
208     }
209 #endif
210     if (result == ERR) {
211         if (OSpeed >= 0) {
212             unsigned i;
213
214             for (i = 0; i < SIZEOF(speeds); i++) {
215                 if (speeds[i].given_speed > OSpeed) {
216                     break;
217                 }
218                 if (speeds[i].given_speed == OSpeed) {
219                     result = speeds[i].actual_speed;
220                     break;
221                 }
222             }
223         }
224 #if !USE_REENTRANT
225         if (OSpeed != last_OSpeed) {
226             last_OSpeed = OSpeed;
227             last_baudrate = result;
228         }
229 #endif
230     }
231     return (result);
232 }
233
234 NCURSES_EXPORT(int)
235 _nc_ospeed(int BaudRate)
236 {
237     int result = 1;
238
239     if (BaudRate >= 0) {
240         unsigned i;
241
242         for (i = 0; i < SIZEOF(speeds); i++) {
243             if (speeds[i].actual_speed == BaudRate) {
244                 result = speeds[i].given_speed;
245                 break;
246             }
247         }
248     }
249     return (result);
250 }
251
252 NCURSES_EXPORT(int)
253 NCURSES_SP_NAME(baudrate) (NCURSES_SP_DCL0)
254 {
255     int result;
256
257     T((T_CALLED("baudrate(%p)"), (void *) SP_PARM));
258
259     /*
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.
263      */
264 #ifdef TRACE
265     if (IsValidTIScreen(SP_PARM)
266         && !NC_ISATTY(fileno((SP_PARM && SP_PARM->_ofp) ? SP_PARM->_ofp : stdout))
267         && getenv("BAUDRATE") != 0) {
268         int ret;
269         if ((ret = _nc_getenv_num("BAUDRATE")) <= 0)
270             ret = 9600;
271         ospeed = (NCURSES_OSPEED) _nc_ospeed(ret);
272         returnCode(ret);
273     }
274 #endif
275
276     if (IsValidTIScreen(SP_PARM)) {
277 #ifdef USE_OLD_TTY
278         result = (int) cfgetospeed(&(TerminalOf(SP_PARM)->Nttyb));
279         ospeed = (NCURSES_OSPEED) _nc_ospeed(result);
280 #else /* !USE_OLD_TTY */
281 #ifdef TERMIOS
282         ospeed = (NCURSES_OSPEED) cfgetospeed(&(TerminalOf(SP_PARM)->Nttyb));
283 #else
284         ospeed = (NCURSES_OSPEED) TerminalOf(SP_PARM)->Nttyb.sg_ospeed;
285 #endif
286         result = _nc_baudrate(ospeed);
287 #endif
288         TerminalOf(SP_PARM)->_baudrate = result;
289     } else {
290         result = ERR;
291     }
292
293     returnCode(result);
294 }
295
296 #if NCURSES_SP_FUNCS
297 NCURSES_EXPORT(int)
298 baudrate(void)
299 {
300     return NCURSES_SP_NAME(baudrate) (CURRENT_SCREEN);
301 }
302 #endif