1 /****************************************************************************
2  * Copyright (c) 1998,2000,2002 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  *	lib_baudrate.c
36  *
37  */
38 
39 #include <curses.priv.h>
40 #include <term.h>		/* cur_term, pad_char */
41 #include <termcap.h>		/* ospeed */
42 
43 /*
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.
48  */
49 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
50 #undef B0
51 #undef B50
52 #undef B75
53 #undef B110
54 #undef B134
55 #undef B150
56 #undef B200
57 #undef B300
58 #undef B600
59 #undef B1200
60 #undef B1800
61 #undef B2400
62 #undef B4800
63 #undef B9600
64 #undef B19200
65 #undef EXTA
66 #undef B38400
67 #undef EXTB
68 #undef B57600
69 #undef B115200
70 #undef B230400
71 #undef B460800
72 #undef B921600
73 #define USE_OLD_TTY
74 #include <sys/ttydev.h>
75 #else
76 #undef USE_OLD_TTY
77 #endif /* USE_OLD_TTY */
78 
79 MODULE_ID("$Id: lib_baudrate.c,v 1.22 2002/01/19 23:07:53 Andrey.A.Chernov Exp $")
80 
81 /*
82  *	int
83  *	baudrate()
84  *
85  *	Returns the current terminal's baud rate.
86  *
87  */
88 
89 struct speed {
90     int s;			/* value for 'ospeed' is an index */
91     int sp;			/* the actual speed */
92 };
93 
94 static struct speed const speeds[] =
95 {
96     {B0, 0},
97     {B50, 50},
98     {B75, 75},
99     {B110, 110},
100     {B134, 134},
101     {B150, 150},
102     {B200, 200},
103     {B300, 300},
104     {B600, 600},
105     {B1200, 1200},
106     {B1800, 1800},
107     {B2400, 2400},
108     {B4800, 4800},
109     {B9600, 9600},
110 #ifdef B19200
111     {B19200, 19200},
112 #else
113 #ifdef EXTA
114     {EXTA, 19200},
115 #endif
116 #endif
117 #ifdef B38400
118     {B38400, 38400},
119 #else
120 #ifdef EXTB
121     {EXTB, 38400},
122 #endif
123 #endif
124 #ifdef B57600
125     {B57600, 57600},
126 #endif
127 #ifdef B115200
128     {B115200, 115200},
129 #endif
130 #ifdef B230400
131     {B230400, 230400},
132 #endif
133 #ifdef B460800
134     {B460800, 460800},
135 #endif
136 #ifdef B921600
137     {B921600, 921600},
138 #endif
139 };
140 
141 NCURSES_EXPORT(int)
_nc_baudrate(int OSpeed)142 _nc_baudrate(int OSpeed)
143 {
144     static int last_OSpeed;
145     static int last_baudrate;
146 
147     int result;
148     unsigned i;
149 
150     if (OSpeed == last_OSpeed) {
151 	result = last_baudrate;
152     } else {
153 	result = ERR;
154 	if (OSpeed >= 0) {
155 	    for (i = 0; i < SIZEOF(speeds); i++) {
156 		if (speeds[i].s == OSpeed) {
157 		    result = speeds[i].sp;
158 		    break;
159 		}
160 	    }
161 	}
162 	last_baudrate = result;
163     }
164     return (result);
165 }
166 
167 NCURSES_EXPORT(int)
_nc_ospeed(int BaudRate)168 _nc_ospeed(int BaudRate)
169 {
170     int result = 1;
171     unsigned i;
172 
173     if (BaudRate >= 0) {
174 	for (i = 0; i < SIZEOF(speeds); i++) {
175 	    if (speeds[i].sp == BaudRate) {
176 		result = speeds[i].s;
177 		break;
178 	    }
179 	}
180     }
181     return (result);
182 }
183 
184 NCURSES_EXPORT(int)
baudrate(void)185 baudrate(void)
186 {
187     int result;
188 
189     T((T_CALLED("baudrate()")));
190 
191     /*
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.
195      */
196 #ifdef TRACE
197     if (SP && !isatty(fileno(SP->_ofp))
198 	&& getenv("BAUDRATE") != 0) {
199 	int ret;
200 	if ((ret = _nc_getenv_num("BAUDRATE")) <= 0)
201 	    ret = 9600;
202 	ospeed = _nc_ospeed(ret);
203 	returnCode(ret);
204     }
205 #endif
206 
207 #ifdef USE_OLD_TTY
208     result = cfgetospeed(&cur_term->Nttyb);
209     ospeed = _nc_ospeed(result);
210 #else /* !USE_OLD_TTY */
211 #ifdef TERMIOS
212     ospeed = cfgetospeed(&cur_term->Nttyb);
213 #else
214     ospeed = cur_term->Nttyb.sg_ospeed;
215 #endif
216     result = _nc_baudrate(ospeed);
217 #endif
218     if (cur_term != 0)
219 	cur_term->_baudrate = result;
220 
221     returnCode(result);
222 }
223