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