1 /****************************************************************************
2 * Copyright (c) 1998,2000,2002 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 ****************************************************************************/
39 #include <curses.priv.h>
40 #include <term.h> /* cur_term, pad_char */
41 #include <termcap.h> /* ospeed */
44 * These systems use similar header files, which define B1200 as 1200, etc.,
45 * but can be overridden by defining USE_OLD_TTY so B1200 is 9, which makes all
46 * of the indices up to B115200 fit nicely in a 'short', allowing us to retain
47 * ospeed's type for compatibility.
49 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
74 #include <sys/ttydev.h>
77 #endif /* USE_OLD_TTY */
79 MODULE_ID("$Id: lib_baudrate.c,v 1.22 2002/01/19 23:07:53 Andrey.A.Chernov Exp $")
85 * Returns the current terminal's baud rate.
90 int s; /* value for 'ospeed' is an index */
91 int sp; /* the actual speed */
94 static struct speed const speeds[] =
142 _nc_baudrate(int OSpeed)
144 static int last_OSpeed;
145 static int last_baudrate;
150 if (OSpeed == last_OSpeed) {
151 result = last_baudrate;
155 for (i = 0; i < SIZEOF(speeds); i++) {
156 if (speeds[i].s == OSpeed) {
157 result = speeds[i].sp;
162 last_baudrate = result;
168 _nc_ospeed(int BaudRate)
174 for (i = 0; i < SIZEOF(speeds); i++) {
175 if (speeds[i].sp == BaudRate) {
176 result = speeds[i].s;
189 T((T_CALLED("baudrate()")));
192 * In debugging, allow the environment symbol to override when we're
193 * redirecting to a file, so we can construct repeatable test-cases
194 * that take into account costs that depend on baudrate.
197 if (SP && !isatty(fileno(SP->_ofp))
198 && getenv("BAUDRATE") != 0) {
200 if ((ret = _nc_getenv_num("BAUDRATE")) <= 0)
202 ospeed = _nc_ospeed(ret);
208 result = cfgetospeed(&cur_term->Nttyb);
209 ospeed = _nc_ospeed(result);
210 #else /* !USE_OLD_TTY */
212 ospeed = cfgetospeed(&cur_term->Nttyb);
214 ospeed = cur_term->Nttyb.sg_ospeed;
216 result = _nc_baudrate(ospeed);
219 cur_term->_baudrate = result;