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