Back to index

tetex-bin  3.0
lib_tracebits.c
Go to the documentation of this file.
00001 /****************************************************************************
00002  * Copyright (c) 1998-2001,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 #include <curses.priv.h>
00035 #include <term.h>           /* cur_term */
00036 
00037 MODULE_ID("$Id: lib_tracebits.c,v 1.12 2002/05/25 14:35:07 tom Exp $")
00038 
00039 #if SVR4_TERMIO && !defined(_POSIX_SOURCE)
00040 #define _POSIX_SOURCE
00041 #endif
00042 
00043 #if HAVE_SYS_TERMIO_H
00044 #include <sys/termio.h>            /* needed for ISC */
00045 #endif
00046 
00047 #ifdef __EMX__
00048 #include <io.h>
00049 #endif
00050 
00051 /* may be undefined if we're using termio.h */
00052 #ifndef TOSTOP
00053 #define TOSTOP 0
00054 #endif
00055 #ifndef IEXTEN
00056 #define IEXTEN 0
00057 #endif
00058 
00059 #ifdef TRACE
00060 
00061 typedef struct {
00062     unsigned int val;
00063     const char *name;
00064 } BITNAMES;
00065 
00066 static void
00067 lookup_bits(char *buf, const BITNAMES * table, const char *label, unsigned int val)
00068 {
00069     const BITNAMES *sp;
00070 
00071     (void) strcat(buf, label);
00072     (void) strcat(buf, ": {");
00073     for (sp = table; sp->name; sp++)
00074        if (sp->val != 0
00075            && (val & sp->val) == sp->val) {
00076            (void) strcat(buf, sp->name);
00077            (void) strcat(buf, ", ");
00078        }
00079     if (buf[strlen(buf) - 2] == ',')
00080        buf[strlen(buf) - 2] = '\0';
00081     (void) strcat(buf, "} ");
00082 }
00083 
00084 NCURSES_EXPORT(char *)
00085 _nc_trace_ttymode(TTY * tty)
00086 /* describe the state of the terminal control bits exactly */
00087 {
00088     char *buf;
00089 
00090 #ifdef TERMIOS
00091     static const BITNAMES iflags[] =
00092     {
00093        {BRKINT, "BRKINT"},
00094        {IGNBRK, "IGNBRK"},
00095        {IGNPAR, "IGNPAR"},
00096        {PARMRK, "PARMRK"},
00097        {INPCK, "INPCK"},
00098        {ISTRIP, "ISTRIP"},
00099        {INLCR, "INLCR"},
00100        {IGNCR, "IGNC"},
00101        {ICRNL, "ICRNL"},
00102        {IXON, "IXON"},
00103        {IXOFF, "IXOFF"},
00104        {0, NULL}
00105 #define ALLIN (BRKINT|IGNBRK|IGNPAR|PARMRK|INPCK|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IXOFF)
00106     }, oflags[] =
00107     {
00108        {OPOST, "OPOST"},
00109        {0, NULL}
00110 #define ALLOUT       (OPOST)
00111     }, cflags[] =
00112     {
00113        {CLOCAL, "CLOCAL"},
00114        {CREAD, "CREAD"},
00115        {CSTOPB, "CSTOPB"},
00116 #if !defined(CS5) || !defined(CS8)
00117        {CSIZE, "CSIZE"},
00118 #endif
00119        {HUPCL, "HUPCL"},
00120        {PARENB, "PARENB"},
00121        {PARODD | PARENB, "PARODD"},       /* concession to readability */
00122        {0, NULL}
00123 #define ALLCTRL      (CLOCAL|CREAD|CSIZE|CSTOPB|HUPCL|PARENB|PARODD)
00124     }, lflags[] =
00125     {
00126        {ECHO, "ECHO"},
00127        {ECHOE | ECHO, "ECHOE"},    /* concession to readability */
00128        {ECHOK | ECHO, "ECHOK"},    /* concession to readability */
00129        {ECHONL, "ECHONL"},
00130        {ICANON, "ICANON"},
00131        {ISIG, "ISIG"},
00132        {NOFLSH, "NOFLSH"},
00133        {TOSTOP, "TOSTOP"},
00134        {IEXTEN, "IEXTEN"},
00135        {0, NULL}
00136 #define ALLLOCAL     (ECHO|ECHONL|ICANON|ISIG|NOFLSH|TOSTOP|IEXTEN)
00137     };
00138 
00139     buf = _nc_trace_buf(0,
00140                      8 + sizeof(iflags) +
00141                      8 + sizeof(oflags) +
00142                      8 + sizeof(cflags) +
00143                      8 + sizeof(lflags) +
00144                      8);
00145 
00146     if (tty->c_iflag & ALLIN)
00147        lookup_bits(buf, iflags, "iflags", tty->c_iflag);
00148 
00149     if (tty->c_oflag & ALLOUT)
00150        lookup_bits(buf, oflags, "oflags", tty->c_oflag);
00151 
00152     if (tty->c_cflag & ALLCTRL)
00153        lookup_bits(buf, cflags, "cflags", tty->c_cflag);
00154 
00155 #if defined(CS5) && defined(CS8)
00156     {
00157        static struct {
00158            const char *name;
00159            int value;
00160        } csizes[] = {
00161            {
00162               "CS5 ", CS5
00163            },
00164 #ifdef CS6
00165            {
00166               "CS6 ", CS6
00167            },
00168 #endif
00169 #ifdef CS7
00170            {
00171               "CS7 ", CS7
00172            },
00173 #endif
00174            {
00175               "CS8 ", CS8
00176            },
00177        };
00178        const char *result = "CSIZE? ";
00179        int value = (tty->c_cflag & CSIZE);
00180        unsigned n;
00181 
00182        if (value != 0) {
00183            for (n = 0; n < SIZEOF(csizes); n++) {
00184               if (csizes[n].value == value) {
00185                   result = csizes[n].name;
00186                   break;
00187               }
00188            }
00189        }
00190        strcat(buf, result);
00191     }
00192 #endif
00193 
00194     if (tty->c_lflag & ALLLOCAL)
00195        lookup_bits(buf, lflags, "lflags", tty->c_lflag);
00196 
00197 #else
00198     /* reference: ttcompat(4M) on SunOS 4.1 */
00199 #ifndef EVENP
00200 #define EVENP 0
00201 #endif
00202 #ifndef LCASE
00203 #define LCASE 0
00204 #endif
00205 #ifndef LLITOUT
00206 #define LLITOUT 0
00207 #endif
00208 #ifndef ODDP
00209 #define ODDP 0
00210 #endif
00211 #ifndef TANDEM
00212 #define TANDEM 0
00213 #endif
00214 
00215     static const BITNAMES cflags[] =
00216     {
00217        {CBREAK, "CBREAK"},
00218        {CRMOD, "CRMOD"},
00219        {ECHO, "ECHO"},
00220        {EVENP, "EVENP"},
00221        {LCASE, "LCASE"},
00222        {LLITOUT, "LLITOUT"},
00223        {ODDP, "ODDP"},
00224        {RAW, "RAW"},
00225        {TANDEM, "TANDEM"},
00226        {XTABS, "XTABS"},
00227        {0, NULL}
00228 #define ALLCTRL      (CBREAK|CRMOD|ECHO|EVENP|LCASE|LLITOUT|ODDP|RAW|TANDEM|XTABS)
00229     };
00230 
00231     buf = _nc_trace_buf(0,
00232                      8 + sizeof(cflags));
00233 
00234     if (tty->sg_flags & ALLCTRL) {
00235        lookup_bits(buf, cflags, "cflags", tty->sg_flags);
00236     }
00237 #endif
00238     return (buf);
00239 }
00240 
00241 NCURSES_EXPORT(char *)
00242 _nc_tracebits(void)
00243 {
00244     return _nc_trace_ttymode(&(cur_term->Nttyb));
00245 }
00246 #else
00247 empty_module(_nc_tracebits)
00248 #endif /* TRACE */