]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/lib_baudrate.c
ncurses 4.2
[ncurses.git] / ncurses / lib_baudrate.c
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
43 MODULE_ID("$Id: lib_baudrate.c,v 1.11 1998/02/11 12:13:58 tom Exp $")
44
45 /*
46  *      int
47  *      baudrate()
48  *
49  *      Returns the current terminal's baud rate.
50  *
51  */
52
53 struct speed {
54         speed_t s;
55         int sp;
56 };
57
58 static struct speed const speeds[] = {
59         {B0, 0},
60         {B50, 50},
61         {B75, 75},
62         {B110, 110},
63         {B134, 134},
64         {B150, 150},
65         {B200, 200},
66         {B300, 300},
67         {B600, 600},
68         {B1200, 1200},
69         {B1800, 1800},
70         {B2400, 2400},
71         {B4800, 4800},
72         {B9600, 9600},
73 #ifdef B19200
74         {B19200, 19200},
75 #else
76 #ifdef EXTA
77         {EXTA, 19200},
78 #endif
79 #endif
80 #ifdef B38400
81         {B38400, 38400},
82 #else
83 #ifdef EXTB
84         {EXTB, 38400},
85 #endif
86 #endif
87 #ifdef B57600
88         {B57600, 57600},
89 #endif
90 #ifdef B115200
91         {B115200, 115200},
92 #endif
93 #ifdef B230400
94         {B230400, 230400},
95 #endif
96 #ifdef B460800
97         {B460800, 460800},
98 #endif
99 };
100
101 int
102 baudrate(void)
103 {
104 size_t i;
105 int ret;
106 #ifdef TRACE
107 char *debug_rate;
108 #endif
109
110         T((T_CALLED("baudrate()")));
111
112         /*
113          * In debugging, allow the environment symbol to override when we're
114          * redirecting to a file, so we can construct repeatable test-cases
115          * that take into account costs that depend on baudrate.
116          */
117 #ifdef TRACE
118         if (SP && !isatty(fileno(SP->_ofp))
119          && (debug_rate = getenv("BAUDRATE")) != 0) {
120                 if (sscanf(debug_rate, "%d", &ret) != 1)
121                         ret = 9600;
122                 returnCode(ret);
123         }
124         else
125 #endif
126
127 #ifdef TERMIOS
128         ret = cfgetospeed(&cur_term->Nttyb);
129 #else
130         ret = cur_term->Nttyb.sg_ospeed;
131 #endif
132         if(ret < 0 || (speed_t)ret > speeds[SIZEOF(speeds)-1].s)
133                 returnCode(ERR);
134         cur_term->_baudrate = ERR;
135         for (i = 0; i < SIZEOF(speeds); i++)
136                 if (speeds[i].s == (speed_t)ret)
137                 {
138                         cur_term->_baudrate = speeds[i].sp;
139                         break;
140                 }
141         returnCode(cur_term->_baudrate);
142 }