GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libcurses/tinfo/lib_baudrate.c Lines: 26 26 100.0 %
Date: 2017-11-13 Branches: 13 20 65.0 %

Line Branch Exec Source
1
/* $OpenBSD: lib_baudrate.c,v 1.6 2013/12/10 20:33:51 naddy Exp $ */
2
3
/****************************************************************************
4
 * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc.              *
5
 *                                                                          *
6
 * Permission is hereby granted, free of charge, to any person obtaining a  *
7
 * copy of this software and associated documentation files (the            *
8
 * "Software"), to deal in the Software without restriction, including      *
9
 * without limitation the rights to use, copy, modify, merge, publish,      *
10
 * distribute, distribute with modifications, sublicense, and/or sell       *
11
 * copies of the Software, and to permit persons to whom the Software is    *
12
 * furnished to do so, subject to the following conditions:                 *
13
 *                                                                          *
14
 * The above copyright notice and this permission notice shall be included  *
15
 * in all copies or substantial portions of the Software.                   *
16
 *                                                                          *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20
 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24
 *                                                                          *
25
 * Except as contained in this notice, the name(s) of the above copyright   *
26
 * holders shall not be used in advertising or otherwise to promote the     *
27
 * sale, use or other dealings in this Software without prior written       *
28
 * authorization.                                                           *
29
 ****************************************************************************/
30
31
/****************************************************************************
32
 *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
33
 *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
34
 *     and: Thomas E. Dickey                        1996-on                 *
35
 ****************************************************************************/
36
37
/*
38
 *	lib_baudrate.c
39
 *
40
 */
41
42
#include <curses.priv.h>
43
#include <term.h>		/* cur_term, pad_char */
44
#include <termcap.h>		/* ospeed */
45
#if defined(__FreeBSD__)
46
#include <sys/param.h>
47
#endif
48
49
/*
50
 * These systems use similar header files, which define B1200 as 1200, etc.,
51
 * but can be overridden by defining USE_OLD_TTY so B1200 is 9, which makes all
52
 * of the indices up to B115200 fit nicely in a 'short', allowing us to retain
53
 * ospeed's type for compatibility.
54
 */
55
#if (defined(__FreeBSD__) && (__FreeBSD_version < 700000)) || defined(__NetBSD__)
56
#undef B0
57
#undef B50
58
#undef B75
59
#undef B110
60
#undef B134
61
#undef B150
62
#undef B200
63
#undef B300
64
#undef B600
65
#undef B1200
66
#undef B1800
67
#undef B2400
68
#undef B4800
69
#undef B9600
70
#undef B19200
71
#undef EXTA
72
#undef B38400
73
#undef EXTB
74
#undef B57600
75
#undef B115200
76
#undef B230400
77
#undef B460800
78
#undef B921600
79
#define USE_OLD_TTY
80
#include <sys/ttydev.h>
81
#else
82
#undef USE_OLD_TTY
83
#endif /* USE_OLD_TTY */
84
85
MODULE_ID("$Id: lib_baudrate.c,v 1.6 2013/12/10 20:33:51 naddy Exp $")
86
87
/*
88
 *	int
89
 *	baudrate()
90
 *
91
 *	Returns the current terminal's baud rate.
92
 *
93
 */
94
95
struct speed {
96
    int s;			/* value for 'ospeed' is an index */
97
    int sp;			/* the actual speed */
98
};
99
100
static struct speed const speeds[] =
101
{
102
    {B0, 0},
103
    {B50, 50},
104
    {B75, 75},
105
    {B110, 110},
106
    {B134, 134},
107
    {B150, 150},
108
    {B200, 200},
109
    {B300, 300},
110
    {B600, 600},
111
    {B1200, 1200},
112
    {B1800, 1800},
113
    {B2400, 2400},
114
    {B4800, 4800},
115
    {B9600, 9600},
116
#ifdef B19200
117
    {B19200, 19200},
118
#else
119
#ifdef EXTA
120
    {EXTA, 19200},
121
#endif
122
#endif
123
#ifdef B38400
124
    {B38400, 38400},
125
#else
126
#ifdef EXTB
127
    {EXTB, 38400},
128
#endif
129
#endif
130
#ifdef B57600
131
    {B57600, 57600},
132
#endif
133
#ifdef B115200
134
    {B115200, 115200},
135
#endif
136
#ifdef B230400
137
    {B230400, 230400},
138
#endif
139
#ifdef B460800
140
    {B460800, 460800},
141
#endif
142
#ifdef B921600
143
    {B921600, 921600},
144
#endif
145
};
146
147
NCURSES_EXPORT(int)
148
_nc_baudrate(int OSpeed)
149
{
150
#if !USE_REENTRANT
151
    static int last_OSpeed;
152
    static int last_baudrate;
153
#endif
154
155
    int result = ERR;
156
    unsigned i;
157
158
#if !USE_REENTRANT
159
10
    if (OSpeed == last_OSpeed) {
160
3
	result = last_baudrate;
161
3
    }
162
#endif
163
5
    if (result == ERR) {
164
2
	if (OSpeed >= 0) {
165
72
	    for (i = 0; i < SIZEOF(speeds); i++) {
166
36
		if (speeds[i].s == OSpeed) {
167
2
		    result = speeds[i].sp;
168
2
		    break;
169
		}
170
	    }
171
	}
172
#if !USE_REENTRANT
173
2
	if (OSpeed != last_OSpeed) {
174
2
	    last_OSpeed = OSpeed;
175
2
	    last_baudrate = result;
176
2
	}
177
#endif
178
    }
179
5
    return (result);
180
}
181
182
NCURSES_EXPORT(int)
183
_nc_ospeed(int BaudRate)
184
{
185
    int result = 1;
186
    unsigned i;
187
188
6
    if (BaudRate >= 0) {
189
6
	for (i = 0; i < SIZEOF(speeds); i++) {
190
3
	    if (speeds[i].sp == BaudRate) {
191
3
		result = speeds[i].s;
192
3
		break;
193
	    }
194
	}
195
    }
196
3
    return (result);
197
}
198
199
NCURSES_EXPORT(int)
200
baudrate(void)
201
{
202
    int result;
203
204
    T((T_CALLED("baudrate()")));
205
206
    /*
207
     * In debugging, allow the environment symbol to override when we're
208
     * redirecting to a file, so we can construct repeatable test-cases
209
     * that take into account costs that depend on baudrate.
210
     */
211
#ifdef TRACE
212
    if (!isatty(fileno(SP ? SP->_ofp : stdout))
213
	&& getenv("BAUDRATE") != 0) {
214
	int ret;
215
	if ((ret = _nc_getenv_num("BAUDRATE")) <= 0)
216
	    ret = 9600;
217
	ospeed = _nc_ospeed(ret);
218
	returnCode(ret);
219
    }
220
#endif
221
222
10
    if (cur_term != 0) {
223
#ifdef USE_OLD_TTY
224
	result = cfgetospeed(&cur_term->Nttyb);
225
	ospeed = _nc_ospeed(result);
226
#else /* !USE_OLD_TTY */
227
#ifdef TERMIOS
228
5
	ospeed = cfgetospeed(&cur_term->Nttyb);
229
#else
230
	ospeed = cur_term->Nttyb.sg_ospeed;
231
#endif
232
5
	result = _nc_baudrate(ospeed);
233
#endif
234
5
	cur_term->_baudrate = result;
235
5
    } else {
236
	result = ERR;
237
    }
238
239
5
    returnCode(result);
240
}