]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/lib_baudrate.c
333fd03520ba88c26e540041be21b5d074c2943e
[ncurses.git] / ncurses / lib_baudrate.c
1
2 /***************************************************************************
3 *                            COPYRIGHT NOTICE                              *
4 ****************************************************************************
5 *                ncurses is copyright (C) 1992-1995                        *
6 *                          Zeyd M. Ben-Halim                               *
7 *                          zmbenhal@netcom.com                             *
8 *                          Eric S. Raymond                                 *
9 *                          esr@snark.thyrsus.com                           *
10 *                                                                          *
11 *        Permission is hereby granted to reproduce and distribute ncurses  *
12 *        by any means and for any fee, whether alone or as part of a       *
13 *        larger distribution, in source or in binary form, PROVIDED        *
14 *        this notice is included with any such distribution, and is not    *
15 *        removed from any of its header files. Mention of ncurses in any   *
16 *        applications linked with it is highly appreciated.                *
17 *                                                                          *
18 *        ncurses comes AS IS with no warranty, implied or expressed.       *
19 *                                                                          *
20 ***************************************************************************/
21
22
23 /*
24  *      lib_baudrate.c
25  *
26  */
27
28 #include <curses.priv.h>
29 #include <term.h>       /* cur_term, pad_char */
30
31 MODULE_ID("$Id: lib_baudrate.c,v 1.7 1997/04/26 17:41:48 tom Exp $")
32
33 /*
34  *      int
35  *      baudrate()
36  *
37  *      Returns the current terminal's baud rate.
38  *
39  */
40
41 struct speed {
42         speed_t s;
43         int sp;
44 };
45
46 static struct speed const speeds[] = {
47         {B0, 0},
48         {B50, 50},
49         {B75, 75},
50         {B110, 110},
51         {B134, 134},
52         {B150, 150},
53         {B200, 200},
54         {B300, 300},
55         {B600, 600},
56         {B1200, 1200},
57         {B1800, 1800},
58         {B2400, 2400},
59         {B4800, 4800},
60         {B9600, 9600},
61 #define MAX_BAUD        B9600
62 #ifdef B19200
63 #undef MAX_BAUD
64 #define MAX_BAUD        B19200
65         {B19200, 19200},
66 #else
67 #ifdef EXTA
68 #define MAX_BAUD        EXTA
69         {EXTA, 19200},
70 #endif
71 #endif
72 #ifdef B38400
73 #undef MAX_BAUD
74 #define MAX_BAUD        B38400
75         {B38400, 38400},
76 #else
77 #ifdef EXTB
78 #define MAX_BAUD        EXTB
79         {EXTB, 38400},
80 #endif
81 #endif
82 #ifdef B57600
83 #undef MAX_BAUD
84 #define MAX_BAUD        B57600
85         {B57600, 57600},
86 #endif
87 #ifdef B115200
88 #undef MAX_BAUD
89 #define MAX_BAUD        B115200
90         {B115200, 115200},
91 #endif
92 };
93
94 int
95 baudrate(void)
96 {
97 size_t i;
98 int ret;
99 #ifdef TRACE
100 char *debug_rate;
101 #endif
102
103         T((T_CALLED("baudrate()")));
104
105         /*
106          * In debugging, allow the environment symbol to override when we're
107          * redirecting to a file, so we can construct repeatable test-cases
108          * that take into account costs that depend on baudrate.
109          */
110 #ifdef TRACE
111         if (!isatty(fileno(SP->_ofp))
112          && (debug_rate = getenv("BAUDRATE")) != 0) {
113                 if (sscanf(debug_rate, "%d", &ret) != 1)
114                         ret = 9600;
115                 returnCode(ret);
116         }
117         else
118 #endif
119
120 #ifdef TERMIOS
121         ret = cfgetospeed(&cur_term->Nttyb);
122 #else
123         ret = cur_term->Nttyb.sg_ospeed;
124 #endif
125         if(ret < 0 || ret > MAX_BAUD)
126                 returnCode(ERR);
127         SP->_baudrate = ERR;
128         for (i = 0; i < (sizeof(speeds) / sizeof(struct speed)); i++)
129                 if (speeds[i].s == (speed_t)ret)
130                 {
131                         SP->_baudrate = speeds[i].sp;
132                         break;
133                 }
134         returnCode(SP->_baudrate);
135 }