Back to index

tetex-bin  3.0
lib_baudrate.c
Go to the documentation of this file.
00001 /****************************************************************************
00002  * Copyright (c) 1998,2000,2002 Free Software Foundation, Inc.              *
00003  *                                                                          *
00004  * Permission is hereby granted, free of charge, to any person obtaining a  *
00005  * copy of this software and associated documentation files (the            *
00006  * "Software"), to deal in the Software without restriction, including      *
00007  * without limitation the rights to use, copy, modify, merge, publish,      *
00008  * distribute, distribute with modifications, sublicense, and/or sell       *
00009  * copies of the Software, and to permit persons to whom the Software is    *
00010  * furnished to do so, subject to the following conditions:                 *
00011  *                                                                          *
00012  * The above copyright notice and this permission notice shall be included  *
00013  * in all copies or substantial portions of the Software.                   *
00014  *                                                                          *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
00016  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
00017  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
00018  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
00019  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
00020  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
00021  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
00022  *                                                                          *
00023  * Except as contained in this notice, the name(s) of the above copyright   *
00024  * holders shall not be used in advertising or otherwise to promote the     *
00025  * sale, use or other dealings in this Software without prior written       *
00026  * authorization.                                                           *
00027  ****************************************************************************/
00028 
00029 /****************************************************************************
00030  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
00031  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
00032  ****************************************************************************/
00033 
00034 /*
00035  *     lib_baudrate.c
00036  *
00037  */
00038 
00039 #include <curses.priv.h>
00040 #include <term.h>           /* cur_term, pad_char */
00041 #include <termcap.h>        /* ospeed */
00042 
00043 /*
00044  * These systems use similar header files, which define B1200 as 1200, etc.,
00045  * but can be overridden by defining USE_OLD_TTY so B1200 is 9, which makes all
00046  * of the indices up to B115200 fit nicely in a 'short', allowing us to retain
00047  * ospeed's type for compatibility.
00048  */
00049 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
00050 #undef B0
00051 #undef B50
00052 #undef B75
00053 #undef B110
00054 #undef B134
00055 #undef B150
00056 #undef B200
00057 #undef B300
00058 #undef B600
00059 #undef B1200
00060 #undef B1800
00061 #undef B2400
00062 #undef B4800
00063 #undef B9600
00064 #undef B19200
00065 #undef EXTA
00066 #undef B38400
00067 #undef EXTB
00068 #undef B57600
00069 #undef B115200
00070 #undef B230400
00071 #undef B460800
00072 #undef B921600
00073 #define USE_OLD_TTY
00074 #include <sys/ttydev.h>
00075 #else
00076 #undef USE_OLD_TTY
00077 #endif /* USE_OLD_TTY */
00078 
00079 MODULE_ID("$Id: lib_baudrate.c,v 1.22 2002/01/19 23:07:53 Andrey.A.Chernov Exp $")
00080 
00081 /*
00082  *     int
00083  *     baudrate()
00084  *
00085  *     Returns the current terminal's baud rate.
00086  *
00087  */
00088 
00089 struct speed {
00090     int s;                  /* value for 'ospeed' is an index */
00091     int sp;                 /* the actual speed */
00092 };
00093 
00094 static struct speed const speeds[] =
00095 {
00096     {B0, 0},
00097     {B50, 50},
00098     {B75, 75},
00099     {B110, 110},
00100     {B134, 134},
00101     {B150, 150},
00102     {B200, 200},
00103     {B300, 300},
00104     {B600, 600},
00105     {B1200, 1200},
00106     {B1800, 1800},
00107     {B2400, 2400},
00108     {B4800, 4800},
00109     {B9600, 9600},
00110 #ifdef B19200
00111     {B19200, 19200},
00112 #else
00113 #ifdef EXTA
00114     {EXTA, 19200},
00115 #endif
00116 #endif
00117 #ifdef B38400
00118     {B38400, 38400},
00119 #else
00120 #ifdef EXTB
00121     {EXTB, 38400},
00122 #endif
00123 #endif
00124 #ifdef B57600
00125     {B57600, 57600},
00126 #endif
00127 #ifdef B115200
00128     {B115200, 115200},
00129 #endif
00130 #ifdef B230400
00131     {B230400, 230400},
00132 #endif
00133 #ifdef B460800
00134     {B460800, 460800},
00135 #endif
00136 #ifdef B921600
00137     {B921600, 921600},
00138 #endif
00139 };
00140 
00141 NCURSES_EXPORT(int)
00142 _nc_baudrate(int OSpeed)
00143 {
00144     static int last_OSpeed;
00145     static int last_baudrate;
00146 
00147     int result;
00148     unsigned i;
00149 
00150     if (OSpeed == last_OSpeed) {
00151        result = last_baudrate;
00152     } else {
00153        result = ERR;
00154        if (OSpeed >= 0) {
00155            for (i = 0; i < SIZEOF(speeds); i++) {
00156               if (speeds[i].s == OSpeed) {
00157                   result = speeds[i].sp;
00158                   break;
00159               }
00160            }
00161        }
00162        last_baudrate = result;
00163     }
00164     return (result);
00165 }
00166 
00167 NCURSES_EXPORT(int)
00168 _nc_ospeed(int BaudRate)
00169 {
00170     int result = 1;
00171     unsigned i;
00172 
00173     if (BaudRate >= 0) {
00174        for (i = 0; i < SIZEOF(speeds); i++) {
00175            if (speeds[i].sp == BaudRate) {
00176               result = speeds[i].s;
00177               break;
00178            }
00179        }
00180     }
00181     return (result);
00182 }
00183 
00184 NCURSES_EXPORT(int)
00185 baudrate(void)
00186 {
00187     int result;
00188 
00189     T((T_CALLED("baudrate()")));
00190 
00191     /*
00192      * In debugging, allow the environment symbol to override when we're
00193      * redirecting to a file, so we can construct repeatable test-cases
00194      * that take into account costs that depend on baudrate.
00195      */
00196 #ifdef TRACE
00197     if (SP && !isatty(fileno(SP->_ofp))
00198        && getenv("BAUDRATE") != 0) {
00199        int ret;
00200        if ((ret = _nc_getenv_num("BAUDRATE")) <= 0)
00201            ret = 9600;
00202        ospeed = _nc_ospeed(ret);
00203        returnCode(ret);
00204     }
00205 #endif
00206 
00207 #ifdef USE_OLD_TTY
00208     result = cfgetospeed(&cur_term->Nttyb);
00209     ospeed = _nc_ospeed(result);
00210 #else /* !USE_OLD_TTY */
00211 #ifdef TERMIOS
00212     ospeed = cfgetospeed(&cur_term->Nttyb);
00213 #else
00214     ospeed = cur_term->Nttyb.sg_ospeed;
00215 #endif
00216     result = _nc_baudrate(ospeed);
00217 #endif
00218     if (cur_term != 0)
00219        cur_term->_baudrate = result;
00220 
00221     returnCode(result);
00222 }