blob: 1cf5505c4fa62287a1816a38438fcb4bf3f10f70 [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
Steve Kondikae271bc2015-11-15 02:50:53 +01002 * Copyright (c) 1998-2014,2015 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05303 * *
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>
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053041#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 */
Steve Kondikae271bc2015-11-15 02:50:53 +010052#if NCURSES_OSPEED_COMPAT && ((defined(__FreeBSD__) && (__FreeBSD_version < 700000)) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__))
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053053#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
Steve Kondikae271bc2015-11-15 02:50:53 +010082MODULE_ID("$Id: lib_baudrate.c,v 1.37 2015/06/14 00:34:12 tom Exp $")
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053083
84/*
85 * int
86 * baudrate()
87 *
88 * Returns the current terminal's baud rate.
89 *
90 */
91
92struct speed {
Steve Kondikae271bc2015-11-15 02:50:53 +010093 NCURSES_OSPEED s; /* values for 'ospeed' */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053094 int sp; /* the actual speed */
95};
96
Steve Kondikae271bc2015-11-15 02:50:53 +010097#define DATA(number) { B##number, number }
98
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053099static struct speed const speeds[] =
100{
Steve Kondikae271bc2015-11-15 02:50:53 +0100101 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),
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530115#ifdef B19200
Steve Kondikae271bc2015-11-15 02:50:53 +0100116 DATA(19200),
117#elif defined(EXTA)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530118 {EXTA, 19200},
119#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530120#ifdef B38400
Steve Kondikae271bc2015-11-15 02:50:53 +0100121 DATA(38400),
122#elif defined(EXTB)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530123 {EXTB, 38400},
124#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530125#ifdef B57600
Steve Kondikae271bc2015-11-15 02:50:53 +0100126 DATA(57600),
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530127#endif
Steve Kondikae271bc2015-11-15 02:50:53 +0100128 /* ifdef to prevent overflow when OLD_TTY is not available */
129#if !(NCURSES_OSPEED_COMPAT && defined(__FreeBSD__) && (__FreeBSD_version > 700000))
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530130#ifdef B115200
Steve Kondikae271bc2015-11-15 02:50:53 +0100131 DATA(115200),
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530132#endif
133#ifdef B230400
Steve Kondikae271bc2015-11-15 02:50:53 +0100134 DATA(230400),
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530135#endif
136#ifdef B460800
Steve Kondikae271bc2015-11-15 02:50:53 +0100137 DATA(460800),
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530138#endif
139#ifdef B921600
Steve Kondikae271bc2015-11-15 02:50:53 +0100140 DATA(921600),
141#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530142#endif
143};
144
145NCURSES_EXPORT(int)
146_nc_baudrate(int OSpeed)
147{
148#if !USE_REENTRANT
149 static int last_OSpeed;
150 static int last_baudrate;
151#endif
152
153 int result = ERR;
154 unsigned i;
155
156#if !USE_REENTRANT
157 if (OSpeed == last_OSpeed) {
158 result = last_baudrate;
159 }
160#endif
161 if (result == ERR) {
162 if (OSpeed >= 0) {
163 for (i = 0; i < SIZEOF(speeds); i++) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100164 if ((int) speeds[i].s == OSpeed) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530165 result = speeds[i].sp;
166 break;
167 }
168 }
169 }
170#if !USE_REENTRANT
Steve Kondikae271bc2015-11-15 02:50:53 +0100171 if (OSpeed != last_OSpeed) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530172 last_OSpeed = OSpeed;
173 last_baudrate = result;
174 }
175#endif
176 }
177 return (result);
178}
179
180NCURSES_EXPORT(int)
181_nc_ospeed(int BaudRate)
182{
183 int result = 1;
184 unsigned i;
185
186 if (BaudRate >= 0) {
187 for (i = 0; i < SIZEOF(speeds); i++) {
188 if (speeds[i].sp == BaudRate) {
189 result = speeds[i].s;
190 break;
191 }
192 }
193 }
194 return (result);
195}
196
197NCURSES_EXPORT(int)
Steve Kondikae271bc2015-11-15 02:50:53 +0100198NCURSES_SP_NAME(baudrate) (NCURSES_SP_DCL0)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530199{
200 int result;
201
Steve Kondikae271bc2015-11-15 02:50:53 +0100202 T((T_CALLED("baudrate(%p)"), (void *) SP_PARM));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530203
204 /*
205 * In debugging, allow the environment symbol to override when we're
206 * redirecting to a file, so we can construct repeatable test-cases
207 * that take into account costs that depend on baudrate.
208 */
209#ifdef TRACE
Steve Kondikae271bc2015-11-15 02:50:53 +0100210 if (IsValidTIScreen(SP_PARM)
211 && !NC_ISATTY(fileno(SP_PARM ? SP_PARM->_ofp : stdout))
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530212 && getenv("BAUDRATE") != 0) {
213 int ret;
214 if ((ret = _nc_getenv_num("BAUDRATE")) <= 0)
215 ret = 9600;
Steve Kondikae271bc2015-11-15 02:50:53 +0100216 ospeed = (NCURSES_OSPEED) _nc_ospeed(ret);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530217 returnCode(ret);
218 }
219#endif
220
Steve Kondikae271bc2015-11-15 02:50:53 +0100221 if (IsValidTIScreen(SP_PARM)) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530222#ifdef USE_OLD_TTY
Steve Kondikae271bc2015-11-15 02:50:53 +0100223 result = (int) cfgetospeed(&(TerminalOf(SP_PARM)->Nttyb));
224 ospeed = (NCURSES_OSPEED) _nc_ospeed(result);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530225#else /* !USE_OLD_TTY */
226#ifdef TERMIOS
Steve Kondikae271bc2015-11-15 02:50:53 +0100227 ospeed = (NCURSES_OSPEED) cfgetospeed(&(TerminalOf(SP_PARM)->Nttyb));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530228#else
Steve Kondikae271bc2015-11-15 02:50:53 +0100229 ospeed = (NCURSES_OSPEED) TerminalOf(SP_PARM)->Nttyb.sg_ospeed;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530230#endif
231 result = _nc_baudrate(ospeed);
232#endif
Steve Kondikae271bc2015-11-15 02:50:53 +0100233 TerminalOf(SP_PARM)->_baudrate = result;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530234 } else {
235 result = ERR;
236 }
237
238 returnCode(result);
239}
Steve Kondikae271bc2015-11-15 02:50:53 +0100240
241#if NCURSES_SP_FUNCS
242NCURSES_EXPORT(int)
243baudrate(void)
244{
245 return NCURSES_SP_NAME(baudrate) (CURRENT_SCREEN);
246}
247#endif