xref: /freebsd/contrib/ncurses/ncurses/trace/lib_tracebits.c (revision 7a65641922f404b84e9a249d48593de84d8e8d17)
1 /****************************************************************************
2  * Copyright 2019,2020 Thomas E. Dickey                                     *
3  * Copyright 1998-2012,2015 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29 
30 /****************************************************************************
31  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
32  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
33  *     and: Thomas E. Dickey                        1996-on                 *
34  ****************************************************************************/
35 
36 #include <curses.priv.h>
37 
38 MODULE_ID("$Id: lib_tracebits.c,v 1.31 2020/11/14 23:38:11 tom Exp $")
39 
40 #if HAVE_SYS_TERMIO_H
41 #include <sys/termio.h>		/* needed for ISC */
42 #endif
43 
44 #ifdef __EMX__
45 #include <io.h>
46 #endif
47 
48 /* may be undefined if we're using termio.h */
49 #ifndef TOSTOP
50 #define TOSTOP 0
51 #endif
52 
53 #ifndef IEXTEN
54 #define IEXTEN 0
55 #endif
56 
57 #ifndef ONLCR
58 #define ONLCR 0
59 #endif
60 
61 #ifndef OCRNL
62 #define OCRNL 0
63 #endif
64 
65 #ifndef ONOCR
66 #define ONOCR 0
67 #endif
68 
69 #ifndef ONLRET
70 #define ONLRET 0
71 #endif
72 
73 #ifdef TRACE
74 
75 #if defined(EXP_WIN32_DRIVER)
76 #define BITNAMELEN 36
77 #else
78 #define BITNAMELEN 8
79 #endif
80 
81 typedef struct {
82     unsigned int val;
83     const char name[BITNAMELEN];
84 } BITNAMES;
85 
86 #define TRACE_BUF_SIZE(num) (_nc_globals.tracebuf_ptr[num].size)
87 
88 static void
lookup_bits(char * buf,const BITNAMES * table,const char * label,unsigned int val)89 lookup_bits(char *buf, const BITNAMES * table, const char *label, unsigned int val)
90 {
91     const BITNAMES *sp;
92 
93     _nc_STRCAT(buf, label, TRACE_BUF_SIZE(0));
94     _nc_STRCAT(buf, ": {", TRACE_BUF_SIZE(0));
95     for (sp = table; sp->name[0]; sp++)
96 	if (sp->val != 0
97 	    && (val & sp->val) == sp->val) {
98 	    _nc_STRCAT(buf, sp->name, TRACE_BUF_SIZE(0));
99 	    _nc_STRCAT(buf, ", ", TRACE_BUF_SIZE(0));
100 	}
101     if (buf[strlen(buf) - 2] == ',')
102 	buf[strlen(buf) - 2] = '\0';
103     _nc_STRCAT(buf, "} ", TRACE_BUF_SIZE(0));
104 }
105 
106 NCURSES_EXPORT(char *)
_nc_trace_ttymode(const TTY * tty)107 _nc_trace_ttymode(const TTY * tty)
108 /* describe the state of the terminal control bits exactly */
109 {
110     char *buf;
111 
112 #ifdef TERMIOS
113 #define DATA(name)        { name, { #name } }
114 #define DATA2(name,name2) { name, { #name2 } }
115 #define DATAX()           { 0,    { "" } }
116     static const BITNAMES iflags[] =
117     {
118 	DATA(BRKINT),
119 	DATA(IGNBRK),
120 	DATA(IGNPAR),
121 	DATA(PARMRK),
122 	DATA(INPCK),
123 	DATA(ISTRIP),
124 	DATA(INLCR),
125 	DATA(IGNCR),
126 	DATA(ICRNL),
127 	DATA(IXON),
128 	DATA(IXOFF),
129 	DATAX()
130 #define ALLIN	(BRKINT|IGNBRK|IGNPAR|PARMRK|INPCK|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IXOFF)
131     }, oflags[] =
132     {
133 	DATA(OPOST),
134 	DATA2(OFLAGS_TABS, XTABS),
135 	DATA(ONLCR),
136 	DATA(OCRNL),
137 	DATA(ONOCR),
138 	DATA(ONLRET),
139 	DATAX()
140 #define ALLOUT	(OPOST|OFLAGS_TABS|ONLCR|OCRNL|ONOCR|ONLRET|OFLAGS_TABS)
141     }, cflags[] =
142     {
143 	DATA(CLOCAL),
144 	DATA(CREAD),
145 	DATA(CSTOPB),
146 #if !defined(CS5) || !defined(CS8)
147 	DATA(CSIZE),
148 #endif
149 	DATA(HUPCL),
150 	DATA(PARENB),
151 	DATA2(PARODD | PARENB, PARODD),
152 	DATAX()
153 #define ALLCTRL	(CLOCAL|CREAD|CSIZE|CSTOPB|HUPCL|PARENB|PARODD)
154     }, lflags[] =
155     {
156 	DATA(ECHO),
157 	DATA2(ECHOE | ECHO, ECHOE),
158 	DATA2(ECHOK | ECHO, ECHOK),
159 	DATA(ECHONL),
160 	DATA(ICANON),
161 	DATA(ISIG),
162 	DATA(NOFLSH),
163 	DATA(TOSTOP),
164 	DATA(IEXTEN),
165 	DATAX()
166 #define ALLLOCAL	(ECHO|ECHONL|ICANON|ISIG|NOFLSH|TOSTOP|IEXTEN)
167     };
168 
169     buf = _nc_trace_buf(0,
170 			8 + sizeof(iflags) +
171 			8 + sizeof(oflags) +
172 			8 + sizeof(cflags) +
173 			8 + sizeof(lflags) +
174 			8);
175     if (buf != 0) {
176 
177 	if (tty->c_iflag & ALLIN)
178 	    lookup_bits(buf, iflags, "iflags", tty->c_iflag);
179 
180 	if (tty->c_oflag & ALLOUT)
181 	    lookup_bits(buf, oflags, "oflags", tty->c_oflag);
182 
183 	if (tty->c_cflag & ALLCTRL)
184 	    lookup_bits(buf, cflags, "cflags", tty->c_cflag);
185 
186 #if defined(CS5) && defined(CS8)
187 	{
188 	    static const struct {
189 		int value;
190 		const char name[5];
191 	    } csizes[] = {
192 #define CS_DATA(name) { name, { #name " " } }
193 		CS_DATA(CS5),
194 #ifdef CS6
195 		    CS_DATA(CS6),
196 #endif
197 #ifdef CS7
198 		    CS_DATA(CS7),
199 #endif
200 		    CS_DATA(CS8),
201 	    };
202 	    const char *result = "CSIZE? ";
203 	    int value = (int) (tty->c_cflag & CSIZE);
204 	    unsigned n;
205 
206 	    if (value != 0) {
207 		for (n = 0; n < SIZEOF(csizes); n++) {
208 		    if (csizes[n].value == value) {
209 			result = csizes[n].name;
210 			break;
211 		    }
212 		}
213 	    }
214 	    _nc_STRCAT(buf, result, TRACE_BUF_SIZE(0));
215 	}
216 #endif
217 
218 	if (tty->c_lflag & ALLLOCAL)
219 	    lookup_bits(buf, lflags, "lflags", tty->c_lflag);
220     }
221 #elif defined(EXP_WIN32_DRIVER)
222 #define DATA(name)        { name, { #name } }
223     static const BITNAMES dwFlagsOut[] =
224     {
225 	DATA(ENABLE_PROCESSED_OUTPUT),
226 	DATA(ENABLE_WRAP_AT_EOL_OUTPUT),
227 	DATA(ENABLE_VIRTUAL_TERMINAL_PROCESSING),
228 	DATA(DISABLE_NEWLINE_AUTO_RETURN),
229 	DATA(ENABLE_LVB_GRID_WORLDWIDE)
230     };
231     static const BITNAMES dwFlagsIn[] =
232     {
233 	DATA(ENABLE_PROCESSED_INPUT),
234 	DATA(ENABLE_LINE_INPUT),
235 	DATA(ENABLE_ECHO_INPUT),
236 	DATA(ENABLE_MOUSE_INPUT),
237 	DATA(ENABLE_INSERT_MODE),
238 	DATA(ENABLE_QUICK_EDIT_MODE),
239 	DATA(ENABLE_EXTENDED_FLAGS),
240 	DATA(ENABLE_AUTO_POSITION),
241 	DATA(ENABLE_VIRTUAL_TERMINAL_INPUT)
242     };
243 
244     buf = _nc_trace_buf(0,
245 			8 + sizeof(dwFlagsOut) +
246 			8 + sizeof(dwFlagsIn));
247     if (buf != 0) {
248 	lookup_bits(buf, dwFlagsIn, "dwIn", tty->dwFlagIn);
249 	lookup_bits(buf, dwFlagsOut, "dwOut", tty->dwFlagOut);
250     }
251 #else
252     /* reference: ttcompat(4M) on SunOS 4.1 */
253 #ifndef EVENP
254 #define EVENP 0
255 #endif
256 #ifndef LCASE
257 #define LCASE 0
258 #endif
259 #ifndef LLITOUT
260 #define LLITOUT 0
261 #endif
262 #ifndef ODDP
263 #define ODDP 0
264 #endif
265 #ifndef TANDEM
266 #define TANDEM 0
267 #endif
268 
269     static const BITNAMES cflags[] =
270     {
271 	DATA(CBREAK),
272 	DATA(CRMOD),
273 	DATA(ECHO),
274 	DATA(EVENP),
275 	DATA(LCASE),
276 	DATA(LLITOUT),
277 	DATA(ODDP),
278 	DATA(RAW),
279 	DATA(TANDEM),
280 	DATA(XTABS),
281 	DATAX()
282 #define ALLCTRL	(CBREAK|CRMOD|ECHO|EVENP|LCASE|LLITOUT|ODDP|RAW|TANDEM|XTABS)
283     };
284 
285     buf = _nc_trace_buf(0,
286 			8 + sizeof(cflags));
287     if (buf != 0) {
288 	if (tty->sg_flags & ALLCTRL) {
289 	    lookup_bits(buf, cflags, "cflags", tty->sg_flags);
290 	}
291     }
292 #endif
293     return (buf);
294 }
295 
296 NCURSES_EXPORT(char *)
_nc_tracebits(void)297 _nc_tracebits(void)
298 {
299     return _nc_trace_ttymode(&(cur_term->Nttyb));
300 }
301 #else
302 EMPTY_MODULE(_nc_empty_lib_tracebits)
303 #endif /* TRACE */
304