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