lib_baudrate.c revision 174998
1/****************************************************************************
2 * Copyright (c) 1998-2006,2007 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/* $FreeBSD: head/contrib/ncurses/ncurses/tinfo/lib_baudrate.c 174998 2007-12-30 11:15:46Z rafan $ */
36
37/*
38 *	lib_baudrate.c
39 *
40 */
41
42#include <curses.priv.h>
43#include <term.h>		/* cur_term, pad_char */
44#include <termcap.h>		/* ospeed */
45#if defined(__FreeBSD__)
46#include <sys/param.h>
47#endif
48
49/*
50 * These systems use similar header files, which define B1200 as 1200, etc.,
51 * but can be overridden by defining USE_OLD_TTY so B1200 is 9, which makes all
52 * of the indices up to B115200 fit nicely in a 'short', allowing us to retain
53 * ospeed's type for compatibility.
54 */
55#if (defined(__FreeBSD__) && (__FreeBSD_version < 700000)) || defined(__NetBSD__) || defined(__OpenBSD__)
56#undef B0
57#undef B50
58#undef B75
59#undef B110
60#undef B134
61#undef B150
62#undef B200
63#undef B300
64#undef B600
65#undef B1200
66#undef B1800
67#undef B2400
68#undef B4800
69#undef B9600
70#undef B19200
71#undef EXTA
72#undef B38400
73#undef EXTB
74#undef B57600
75#undef B115200
76#undef B230400
77#undef B460800
78#undef B921600
79#define USE_OLD_TTY
80#include <sys/ttydev.h>
81#else
82#undef USE_OLD_TTY
83#endif /* USE_OLD_TTY */
84
85MODULE_ID("$Id: lib_baudrate.c,v 1.25 2007/10/20 15:00:41 Rong-En.Fan Exp $")
86
87/*
88 *	int
89 *	baudrate()
90 *
91 *	Returns the current terminal's baud rate.
92 *
93 */
94
95struct speed {
96    int s;			/* value for 'ospeed' is an index */
97    int sp;			/* the actual speed */
98};
99
100static struct speed const speeds[] =
101{
102    {B0, 0},
103    {B50, 50},
104    {B75, 75},
105    {B110, 110},
106    {B134, 134},
107    {B150, 150},
108    {B200, 200},
109    {B300, 300},
110    {B600, 600},
111    {B1200, 1200},
112    {B1800, 1800},
113    {B2400, 2400},
114    {B4800, 4800},
115    {B9600, 9600},
116#ifdef B19200
117    {B19200, 19200},
118#else
119#ifdef EXTA
120    {EXTA, 19200},
121#endif
122#endif
123#ifdef B38400
124    {B38400, 38400},
125#else
126#ifdef EXTB
127    {EXTB, 38400},
128#endif
129#endif
130#ifdef B57600
131    {B57600, 57600},
132#endif
133#ifdef B115200
134    {B115200, 115200},
135#endif
136#ifdef B230400
137    {B230400, 230400},
138#endif
139#ifdef B460800
140    {B460800, 460800},
141#endif
142#ifdef B921600
143    {B921600, 921600},
144#endif
145};
146
147NCURSES_EXPORT(int)
148_nc_baudrate(int OSpeed)
149{
150#if !USE_REENTRANT
151    static int last_OSpeed;
152    static int last_baudrate;
153#endif
154
155    int result = ERR;
156    unsigned i;
157
158#if !USE_REENTRANT
159    if (OSpeed == last_OSpeed) {
160	result = last_baudrate;
161    }
162#endif
163    if (result == ERR) {
164	if (OSpeed >= 0) {
165	    for (i = 0; i < SIZEOF(speeds); i++) {
166		if (speeds[i].s == OSpeed) {
167		    result = speeds[i].sp;
168		    break;
169		}
170	    }
171	}
172#if !USE_REENTRANT
173	if (OSpeed == last_OSpeed) {
174	    last_OSpeed = OSpeed;
175	    last_baudrate = result;
176	}
177#endif
178    }
179    return (result);
180}
181
182NCURSES_EXPORT(int)
183_nc_ospeed(int BaudRate)
184{
185    int result = 1;
186    unsigned i;
187
188    if (BaudRate >= 0) {
189	for (i = 0; i < SIZEOF(speeds); i++) {
190	    if (speeds[i].sp == BaudRate) {
191		result = speeds[i].s;
192		break;
193	    }
194	}
195    }
196    return (result);
197}
198
199NCURSES_EXPORT(int)
200baudrate(void)
201{
202    int result;
203
204    T((T_CALLED("baudrate()")));
205
206    /*
207     * In debugging, allow the environment symbol to override when we're
208     * redirecting to a file, so we can construct repeatable test-cases
209     * that take into account costs that depend on baudrate.
210     */
211#ifdef TRACE
212    if (SP && !isatty(fileno(SP->_ofp))
213	&& getenv("BAUDRATE") != 0) {
214	int ret;
215	if ((ret = _nc_getenv_num("BAUDRATE")) <= 0)
216	    ret = 9600;
217	ospeed = _nc_ospeed(ret);
218	returnCode(ret);
219    }
220#endif
221
222#ifdef USE_OLD_TTY
223    result = cfgetospeed(&cur_term->Nttyb);
224    ospeed = _nc_ospeed(result);
225#else /* !USE_OLD_TTY */
226#ifdef TERMIOS
227    ospeed = cfgetospeed(&cur_term->Nttyb);
228#else
229    ospeed = cur_term->Nttyb.sg_ospeed;
230#endif
231    result = _nc_baudrate(ospeed);
232#endif
233    if (cur_term != 0)
234	cur_term->_baudrate = result;
235
236    returnCode(result);
237}
238