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