]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/trace/lib_tracebits.c
6dbb2f793ad0d1bbea81ace51c09edc9d110c0a7
[ncurses.git] / ncurses / trace / lib_tracebits.c
1 /****************************************************************************
2  * Copyright (c) 1998,1999,2000 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 #include <curses.priv.h>
35 #include <term.h>               /* cur_term */
36
37 MODULE_ID("$Id: lib_tracebits.c,v 1.5 2000/02/13 01:01:55 tom Exp $")
38
39 #if defined(SVR4_TERMIO) && !defined(_POSIX_SOURCE)
40 #define _POSIX_SOURCE
41 #endif
42
43 #if HAVE_SYS_TERMIO_H
44 #include <sys/termio.h>         /* needed for ISC */
45 #endif
46
47 #ifdef __EMX__
48 #include <io.h>
49 #endif
50
51 /* may be undefined if we're using termio.h */
52 #ifndef TOSTOP
53 #define TOSTOP 0
54 #endif
55 #ifndef IEXTEN
56 #define IEXTEN 0
57 #endif
58
59 #ifdef TRACE
60
61 typedef struct {
62     unsigned int val;
63     const char *name;
64 } BITNAMES;
65
66 static void
67 lookup_bits(char *buf, const BITNAMES * table, const char *label, unsigned int val)
68 {
69     const BITNAMES *sp;
70
71     (void) strcat(buf, label);
72     (void) strcat(buf, ": {");
73     for (sp = table; sp->name; sp++)
74         if (sp->val != 0
75             && (val & sp->val) == sp->val) {
76             (void) strcat(buf, sp->name);
77             (void) strcat(buf, ", ");
78         }
79     if (buf[strlen(buf) - 2] == ',')
80         buf[strlen(buf) - 2] = '\0';
81     (void) strcat(buf, "} ");
82 }
83
84 char *
85 _nc_tracebits(void)
86 /* describe the state of the terminal control bits exactly */
87 {
88     char *buf;
89
90 #ifdef TERMIOS
91     static const BITNAMES iflags[] =
92     {
93         {BRKINT, "BRKINT"},
94         {IGNBRK, "IGNBRK"},
95         {IGNPAR, "IGNPAR"},
96         {PARMRK, "PARMRK"},
97         {INPCK, "INPCK"},
98         {ISTRIP, "ISTRIP"},
99         {INLCR, "INLCR"},
100         {IGNCR, "IGNC"},
101         {ICRNL, "ICRNL"},
102         {IXON, "IXON"},
103         {IXOFF, "IXOFF"},
104         {0, NULL}
105 #define ALLIN   (BRKINT|IGNBRK|IGNPAR|PARMRK|INPCK|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IXOFF)
106     }, oflags[] =
107     {
108         {OPOST, "OPOST"},
109         {0, NULL}
110 #define ALLOUT  (OPOST)
111     }, cflags[] =
112     {
113         {CLOCAL, "CLOCAL"},
114         {CREAD, "CREAD"},
115         {CSTOPB, "CSTOPB"},
116 #if !defined(CS5) || !defined(CS8)
117         {CSIZE, "CSIZE"},
118 #endif
119         {HUPCL, "HUPCL"},
120         {PARENB, "PARENB"},
121         {PARODD | PARENB, "PARODD"},    /* concession to readability */
122         {0, NULL}
123 #define ALLCTRL (CLOCAL|CREAD|CSIZE|CSTOPB|HUPCL|PARENB|PARODD)
124     }, lflags[] =
125     {
126         {ECHO, "ECHO"},
127         {ECHOE | ECHO, "ECHOE"},        /* concession to readability */
128         {ECHOK | ECHO, "ECHOK"},        /* concession to readability */
129         {ECHONL, "ECHONL"},
130         {ICANON, "ICANON"},
131         {ISIG, "ISIG"},
132         {NOFLSH, "NOFLSH"},
133         {TOSTOP, "TOSTOP"},
134         {IEXTEN, "IEXTEN"},
135         {0, NULL}
136 #define ALLLOCAL        (ECHO|ECHONL|ICANON|ISIG|NOFLSH|TOSTOP|IEXTEN)
137     };
138
139     buf = _nc_trace_buf(0,
140         8 + sizeof(iflags) +
141         8 + sizeof(oflags) +
142         8 + sizeof(cflags) +
143         8 + sizeof(lflags) +
144         8);
145
146     if (cur_term->Nttyb.c_iflag & ALLIN)
147         lookup_bits(buf, iflags, "iflags", cur_term->Nttyb.c_iflag);
148
149     if (cur_term->Nttyb.c_oflag & ALLOUT)
150         lookup_bits(buf, oflags, "oflags", cur_term->Nttyb.c_oflag);
151
152     if (cur_term->Nttyb.c_cflag & ALLCTRL)
153         lookup_bits(buf, cflags, "cflags", cur_term->Nttyb.c_cflag);
154
155 #if defined(CS5) && defined(CS8)
156     switch (cur_term->Nttyb.c_cflag & CSIZE) {
157 #if defined(CS5) && (CS5 != 0)
158     case CS5:
159         strcat(buf, "CS5 ");
160         break;
161 #endif
162 #if defined(CS6) && (CS6 != 0)
163     case CS6:
164         strcat(buf, "CS6 ");
165         break;
166 #endif
167 #if defined(CS7) && (CS7 != 0)
168     case CS7:
169         strcat(buf, "CS7 ");
170         break;
171 #endif
172 #if defined(CS8) && (CS8 != 0)
173     case CS8:
174         strcat(buf, "CS8 ");
175         break;
176 #endif
177     default:
178         strcat(buf, "CSIZE? ");
179         break;
180     }
181 #endif
182
183     if (cur_term->Nttyb.c_lflag & ALLLOCAL)
184         lookup_bits(buf, lflags, "lflags", cur_term->Nttyb.c_lflag);
185
186 #else
187     /* reference: ttcompat(4M) on SunOS 4.1 */
188 #ifndef EVENP
189 #define EVENP 0
190 #endif
191 #ifndef LCASE
192 #define LCASE 0
193 #endif
194 #ifndef LLITOUT
195 #define LLITOUT 0
196 #endif
197 #ifndef ODDP
198 #define ODDP 0
199 #endif
200 #ifndef TANDEM
201 #define TANDEM 0
202 #endif
203
204     static const BITNAMES cflags[] =
205     {
206         {CBREAK, "CBREAK"},
207         {CRMOD, "CRMOD"},
208         {ECHO, "ECHO"},
209         {EVENP, "EVENP"},
210         {LCASE, "LCASE"},
211         {LLITOUT, "LLITOUT"},
212         {ODDP, "ODDP"},
213         {RAW, "RAW"},
214         {TANDEM, "TANDEM"},
215         {XTABS, "XTABS"},
216         {0, NULL}
217 #define ALLCTRL (CBREAK|CRMOD|ECHO|EVENP|LCASE|LLITOUT|ODDP|RAW|TANDEM|XTABS)
218     };
219
220     buf = _nc_trace_buf(0,
221         8 + sizeof(cflags));
222
223     if (cur_term->Nttyb.sg_flags & ALLCTRL) {
224         lookup_bits(buf, cflags, "cflags", cur_term->Nttyb.sg_flags);
225     }
226 #endif
227     return (buf);
228 }
229 #else
230 char *
231 _nc_tracebits(void)
232 {
233     static char tmp[] = "";
234     return tmp;
235 }
236 #endif /* TRACE */