lib_baudrate.c revision 50276
1/****************************************************************************
2 * Copyright (c) 1998 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 ****************************************************************************/
33
34
35/*
36 *	lib_baudrate.c
37 *
38 */
39
40#include <curses.priv.h>
41#include <term.h>	/* cur_term, pad_char */
42#include <termcap.h>	/* ospeed */
43
44MODULE_ID("$Id: lib_baudrate.c,v 1.15 1999/01/31 03:05:25 tom Exp $")
45
46/*
47 *	int
48 *	baudrate()
49 *
50 *	Returns the current terminal's baud rate.
51 *
52 */
53
54struct speed {
55	speed_t s;
56	int sp;
57};
58
59static struct speed const speeds[] = {
60	{B0, 0},
61	{B50, 50},
62	{B75, 75},
63	{B110, 110},
64	{B134, 134},
65	{B150, 150},
66	{B200, 200},
67	{B300, 300},
68	{B600, 600},
69	{B1200, 1200},
70	{B1800, 1800},
71	{B2400, 2400},
72	{B4800, 4800},
73	{B9600, 9600},
74#ifdef B19200
75	{B19200, 19200},
76#else
77#ifdef EXTA
78	{EXTA, 19200},
79#endif
80#endif
81#ifdef B38400
82	{B38400, 38400},
83#else
84#ifdef EXTB
85	{EXTB, 38400},
86#endif
87#endif
88#ifdef B57600
89	{B57600, 57600},
90#endif
91#ifdef B115200
92	{B115200, 115200},
93#endif
94#ifdef B230400
95	{B230400, 230400},
96#endif
97#ifdef B460800
98	{B460800, 460800},
99#endif
100};
101
102int _nc_baudrate(int OSpeed)
103{
104	static int last_OSpeed;
105	static int last_baudrate;
106
107	int result;
108	unsigned i;
109
110	if (OSpeed == last_OSpeed) {
111		result = last_baudrate;
112	} else {
113		result = ERR;
114		if (OSpeed >= 0) {
115			for (i = 0; i < SIZEOF(speeds); i++) {
116				if (speeds[i].s == (speed_t)OSpeed) {
117					result = speeds[i].sp;
118					break;
119				}
120			}
121		}
122		last_baudrate = result;
123	}
124	return (result);
125}
126
127
128int _nc_ospeed(int BaudRate)
129{
130	speed_t result = 1;
131	unsigned i;
132
133	if (BaudRate >= 0) {
134		for (i = 0; i < SIZEOF(speeds); i++) {
135			if (speeds[i].sp == BaudRate) {
136				result = speeds[i].s;
137				break;
138			}
139		}
140	}
141	return (result);
142}
143
144int
145baudrate(void)
146{
147int result;
148
149	T((T_CALLED("baudrate()")));
150
151	/*
152	 * In debugging, allow the environment symbol to override when we're
153	 * redirecting to a file, so we can construct repeatable test-cases
154	 * that take into account costs that depend on baudrate.
155	 */
156#ifdef TRACE
157	if (SP && !isatty(fileno(SP->_ofp))
158	 && getenv("BAUDRATE") != 0) {
159		int ret;
160		if ((ret = _nc_getenv_num("BAUDRATE")) <= 0)
161			ret = 9600;
162		ospeed = _nc_ospeed(ret);
163		returnCode(ret);
164	}
165	else
166#endif
167
168#ifdef TERMIOS
169	ospeed = cfgetospeed(&cur_term->Nttyb);
170#else
171	ospeed = cur_term->Nttyb.sg_ospeed;
172#endif
173	result = _nc_baudrate(ospeed);
174	if (cur_term != 0)
175		cur_term->_baudrate = result;
176
177	returnCode(result);
178}
179