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