lib_baudrate.c revision 262629
1/****************************************************************************
2 * Copyright (c) 1998-2009,2010 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 (defined(__FreeBSD__) && (__FreeBSD_version < 700000)) || defined(__NetBSD__) || defined(__OpenBSD__)
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
82MODULE_ID("$Id: lib_baudrate.c,v 1.31 2010/12/19 01:50:50 tom Exp $")
83
84/*
85 *	int
86 *	baudrate()
87 *
88 *	Returns the current terminal's baud rate.
89 *
90 */
91
92struct speed {
93    int s;			/* value for 'ospeed' is an index */
94    int sp;			/* the actual speed */
95};
96
97static struct speed const speeds[] =
98{
99    {B0, 0},
100    {B50, 50},
101    {B75, 75},
102    {B110, 110},
103    {B134, 134},
104    {B150, 150},
105    {B200, 200},
106    {B300, 300},
107    {B600, 600},
108    {B1200, 1200},
109    {B1800, 1800},
110    {B2400, 2400},
111    {B4800, 4800},
112    {B9600, 9600},
113#ifdef B19200
114    {B19200, 19200},
115#else
116#ifdef EXTA
117    {EXTA, 19200},
118#endif
119#endif
120#ifdef B38400
121    {B38400, 38400},
122#else
123#ifdef EXTB
124    {EXTB, 38400},
125#endif
126#endif
127#ifdef B57600
128    {B57600, 57600},
129#endif
130#ifdef B115200
131    {B115200, 115200},
132#endif
133#ifdef B230400
134    {B230400, 230400},
135#endif
136#ifdef B460800
137    {B460800, 460800},
138#endif
139#ifdef B921600
140    {B921600, 921600},
141#endif
142};
143
144NCURSES_EXPORT(int)
145_nc_baudrate(int OSpeed)
146{
147#if !USE_REENTRANT
148    static int last_OSpeed;
149    static int last_baudrate;
150#endif
151
152    int result = ERR;
153    unsigned i;
154
155#if !USE_REENTRANT
156    if (OSpeed == last_OSpeed) {
157	result = last_baudrate;
158    }
159#endif
160    if (result == ERR) {
161	if (OSpeed >= 0) {
162	    for (i = 0; i < SIZEOF(speeds); i++) {
163		if (speeds[i].s == OSpeed) {
164		    result = speeds[i].sp;
165		    break;
166		}
167	    }
168	}
169#if !USE_REENTRANT
170	if (OSpeed == last_OSpeed) {
171	    last_OSpeed = OSpeed;
172	    last_baudrate = result;
173	}
174#endif
175    }
176    return (result);
177}
178
179NCURSES_EXPORT(int)
180_nc_ospeed(int BaudRate)
181{
182    int result = 1;
183    unsigned i;
184
185    if (BaudRate >= 0) {
186	for (i = 0; i < SIZEOF(speeds); i++) {
187	    if (speeds[i].sp == BaudRate) {
188		result = speeds[i].s;
189		break;
190	    }
191	}
192    }
193    return (result);
194}
195
196NCURSES_EXPORT(int)
197NCURSES_SP_NAME(baudrate) (NCURSES_SP_DCL0)
198{
199    int result;
200
201    T((T_CALLED("baudrate(%p)"), (void *) SP_PARM));
202
203    /*
204     * In debugging, allow the environment symbol to override when we're
205     * redirecting to a file, so we can construct repeatable test-cases
206     * that take into account costs that depend on baudrate.
207     */
208#ifdef TRACE
209    if (IsValidTIScreen(SP_PARM)
210	&& !isatty(fileno(SP_PARM ? SP_PARM->_ofp : stdout))
211	&& getenv("BAUDRATE") != 0) {
212	int ret;
213	if ((ret = _nc_getenv_num("BAUDRATE")) <= 0)
214	    ret = 9600;
215	ospeed = (NCURSES_OSPEED) _nc_ospeed(ret);
216	returnCode(ret);
217    }
218#endif
219
220    if (IsValidTIScreen(SP_PARM)) {
221#ifdef USE_OLD_TTY
222	result = cfgetospeed(&(TerminalOf(SP_PARM)->Nttyb));
223	ospeed = _nc_ospeed(result);
224#else /* !USE_OLD_TTY */
225#ifdef TERMIOS
226	ospeed = (NCURSES_OSPEED) cfgetospeed(&(TerminalOf(SP_PARM)->Nttyb));
227#else
228	ospeed = (NCURSES_OSPEED) TerminalOf(SP_PARM)->Nttyb.sg_ospeed;
229#endif
230	result = _nc_baudrate(ospeed);
231#endif
232	TerminalOf(SP_PARM)->_baudrate = result;
233    } else {
234	result = ERR;
235    }
236
237    returnCode(result);
238}
239
240#if NCURSES_SP_FUNCS
241NCURSES_EXPORT(int)
242baudrate(void)
243{
244    return NCURSES_SP_NAME(baudrate) (CURRENT_SCREEN);
245}
246#endif
247