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