1 /****************************************************************************
2  * Copyright (c) 1998,2000 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 #define USE_OLD_TTY
73 #include <sys/ttydev.h>
74 #else
75 #undef USE_OLD_TTY
76 #endif /* USE_OLD_TTY */
77 
78 MODULE_ID("$Id: lib_baudrate.c,v 1.21 2001/06/30 22:59:22 tom Exp $")
79 
80 /*
81  *	int
82  *	baudrate()
83  *
84  *	Returns the current terminal's baud rate.
85  *
86  */
87 
88 struct speed {
89     int s;			/* value for 'ospeed' is an index */
90     int sp;			/* the actual speed */
91 };
92 
93 static struct speed const speeds[] =
94 {
95     {B0, 0},
96     {B50, 50},
97     {B75, 75},
98     {B110, 110},
99     {B134, 134},
100     {B150, 150},
101     {B200, 200},
102     {B300, 300},
103     {B600, 600},
104     {B1200, 1200},
105     {B1800, 1800},
106     {B2400, 2400},
107     {B4800, 4800},
108     {B9600, 9600},
109 #ifdef B19200
110     {B19200, 19200},
111 #else
112 #ifdef EXTA
113     {EXTA, 19200},
114 #endif
115 #endif
116 #ifdef B38400
117     {B38400, 38400},
118 #else
119 #ifdef EXTB
120     {EXTB, 38400},
121 #endif
122 #endif
123 #ifdef B57600
124     {B57600, 57600},
125 #endif
126 #ifdef B115200
127     {B115200, 115200},
128 #endif
129 #ifdef B230400
130     {B230400, 230400},
131 #endif
132 #ifdef B460800
133     {B460800, 460800},
134 #endif
135 };
136 
137 int
138 _nc_baudrate(int OSpeed)
139 {
140     static int last_OSpeed;
141     static int last_baudrate;
142 
143     int result;
144     unsigned i;
145 
146     if (OSpeed == last_OSpeed) {
147 	result = last_baudrate;
148     } else {
149 	result = ERR;
150 	if (OSpeed >= 0) {
151 	    for (i = 0; i < SIZEOF(speeds); i++) {
152 		if (speeds[i].s == OSpeed) {
153 		    result = speeds[i].sp;
154 		    break;
155 		}
156 	    }
157 	}
158 	last_baudrate = result;
159     }
160     return (result);
161 }
162 
163 int
164 _nc_ospeed(int BaudRate)
165 {
166     int result = 1;
167     unsigned i;
168 
169     if (BaudRate >= 0) {
170 	for (i = 0; i < SIZEOF(speeds); i++) {
171 	    if (speeds[i].sp == BaudRate) {
172 		result = speeds[i].s;
173 		break;
174 	    }
175 	}
176     }
177     return (result);
178 }
179 
180 int
181 baudrate(void)
182 {
183     int result;
184 
185     T((T_CALLED("baudrate()")));
186 
187     /*
188      * In debugging, allow the environment symbol to override when we're
189      * redirecting to a file, so we can construct repeatable test-cases
190      * that take into account costs that depend on baudrate.
191      */
192 #ifdef TRACE
193     if (SP && !isatty(fileno(SP->_ofp))
194 	&& getenv("BAUDRATE") != 0) {
195 	int ret;
196 	if ((ret = _nc_getenv_num("BAUDRATE")) <= 0)
197 	    ret = 9600;
198 	ospeed = _nc_ospeed(ret);
199 	returnCode(ret);
200     }
201 #endif
202 
203 #ifdef USE_OLD_TTY
204     result = cfgetospeed(&cur_term->Nttyb);
205     ospeed = _nc_ospeed(result);
206 #else
207 #ifdef TERMIOS
208     ospeed = cfgetospeed(&cur_term->Nttyb);
209 #else
210     ospeed = cur_term->Nttyb.sg_ospeed;
211 #endif
212     result = _nc_baudrate(ospeed);
213 #endif /* __FreeBSD__ */
214     if (cur_term != 0)
215 	cur_term->_baudrate = result;
216 
217     returnCode(result);
218 }
219