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.28 2020/02/02 23:34:34 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 typedef struct {
76 unsigned int val;
77 const char name[8];
78 } BITNAMES;
79
80 #define TRACE_BUF_SIZE(num) (_nc_globals.tracebuf_ptr[num].size)
81
82 static void
lookup_bits(char * buf,const BITNAMES * table,const char * label,unsigned int val)83 lookup_bits(char *buf, const BITNAMES * table, const char *label, unsigned int val)
84 {
85 const BITNAMES *sp;
86
87 _nc_STRCAT(buf, label, TRACE_BUF_SIZE(0));
88 _nc_STRCAT(buf, ": {", TRACE_BUF_SIZE(0));
89 for (sp = table; sp->name[0]; sp++)
90 if (sp->val != 0
91 && (val & sp->val) == sp->val) {
92 _nc_STRCAT(buf, sp->name, TRACE_BUF_SIZE(0));
93 _nc_STRCAT(buf, ", ", TRACE_BUF_SIZE(0));
94 }
95 if (buf[strlen(buf) - 2] == ',')
96 buf[strlen(buf) - 2] = '\0';
97 _nc_STRCAT(buf, "} ", TRACE_BUF_SIZE(0));
98 }
99
100 NCURSES_EXPORT(char *)
_nc_trace_ttymode(TTY * tty)101 _nc_trace_ttymode(TTY * tty)
102 /* describe the state of the terminal control bits exactly */
103 {
104 char *buf;
105
106 #ifdef TERMIOS
107 #define DATA(name) { name, { #name } }
108 #define DATA2(name,name2) { name, { #name2 } }
109 #define DATAX() { 0, { "" } }
110 static const BITNAMES iflags[] =
111 {
112 DATA(BRKINT),
113 DATA(IGNBRK),
114 DATA(IGNPAR),
115 DATA(PARMRK),
116 DATA(INPCK),
117 DATA(ISTRIP),
118 DATA(INLCR),
119 DATA(IGNCR),
120 DATA(ICRNL),
121 DATA(IXON),
122 DATA(IXOFF),
123 DATAX()
124 #define ALLIN (BRKINT|IGNBRK|IGNPAR|PARMRK|INPCK|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IXOFF)
125 }, oflags[] =
126 {
127 DATA(OPOST),
128 DATA2(OFLAGS_TABS, XTABS),
129 DATA(ONLCR),
130 DATA(OCRNL),
131 DATA(ONOCR),
132 DATA(ONLRET),
133 DATAX()
134 #define ALLOUT (OPOST|OFLAGS_TABS|ONLCR|OCRNL|ONOCR|ONLRET|OFLAGS_TABS)
135 }, cflags[] =
136 {
137 DATA(CLOCAL),
138 DATA(CREAD),
139 DATA(CSTOPB),
140 #if !defined(CS5) || !defined(CS8)
141 DATA(CSIZE),
142 #endif
143 DATA(HUPCL),
144 DATA(PARENB),
145 DATA2(PARODD | PARENB, PARODD),
146 DATAX()
147 #define ALLCTRL (CLOCAL|CREAD|CSIZE|CSTOPB|HUPCL|PARENB|PARODD)
148 }, lflags[] =
149 {
150 DATA(ECHO),
151 DATA2(ECHOE | ECHO, ECHOE),
152 DATA2(ECHOK | ECHO, ECHOK),
153 DATA(ECHONL),
154 DATA(ICANON),
155 DATA(ISIG),
156 DATA(NOFLSH),
157 DATA(TOSTOP),
158 DATA(IEXTEN),
159 DATAX()
160 #define ALLLOCAL (ECHO|ECHONL|ICANON|ISIG|NOFLSH|TOSTOP|IEXTEN)
161 };
162
163 buf = _nc_trace_buf(0,
164 8 + sizeof(iflags) +
165 8 + sizeof(oflags) +
166 8 + sizeof(cflags) +
167 8 + sizeof(lflags) +
168 8);
169 if (buf != 0) {
170
171 if (tty->c_iflag & ALLIN)
172 lookup_bits(buf, iflags, "iflags", tty->c_iflag);
173
174 if (tty->c_oflag & ALLOUT)
175 lookup_bits(buf, oflags, "oflags", tty->c_oflag);
176
177 if (tty->c_cflag & ALLCTRL)
178 lookup_bits(buf, cflags, "cflags", tty->c_cflag);
179
180 #if defined(CS5) && defined(CS8)
181 {
182 static const struct {
183 int value;
184 const char name[5];
185 } csizes[] = {
186 #define CS_DATA(name) { name, { #name " " } }
187 CS_DATA(CS5),
188 #ifdef CS6
189 CS_DATA(CS6),
190 #endif
191 #ifdef CS7
192 CS_DATA(CS7),
193 #endif
194 CS_DATA(CS8),
195 };
196 const char *result = "CSIZE? ";
197 int value = (int) (tty->c_cflag & CSIZE);
198 unsigned n;
199
200 if (value != 0) {
201 for (n = 0; n < SIZEOF(csizes); n++) {
202 if (csizes[n].value == value) {
203 result = csizes[n].name;
204 break;
205 }
206 }
207 }
208 _nc_STRCAT(buf, result, TRACE_BUF_SIZE(0));
209 }
210 #endif
211
212 if (tty->c_lflag & ALLLOCAL)
213 lookup_bits(buf, lflags, "lflags", tty->c_lflag);
214 }
215 #else
216 /* reference: ttcompat(4M) on SunOS 4.1 */
217 #ifndef EVENP
218 #define EVENP 0
219 #endif
220 #ifndef LCASE
221 #define LCASE 0
222 #endif
223 #ifndef LLITOUT
224 #define LLITOUT 0
225 #endif
226 #ifndef ODDP
227 #define ODDP 0
228 #endif
229 #ifndef TANDEM
230 #define TANDEM 0
231 #endif
232
233 static const BITNAMES cflags[] =
234 {
235 DATA(CBREAK),
236 DATA(CRMOD),
237 DATA(ECHO),
238 DATA(EVENP),
239 DATA(LCASE),
240 DATA(LLITOUT),
241 DATA(ODDP),
242 DATA(RAW),
243 DATA(TANDEM),
244 DATA(XTABS),
245 DATAX()
246 #define ALLCTRL (CBREAK|CRMOD|ECHO|EVENP|LCASE|LLITOUT|ODDP|RAW|TANDEM|XTABS)
247 };
248
249 buf = _nc_trace_buf(0,
250 8 + sizeof(cflags));
251 if (buf != 0) {
252 if (tty->sg_flags & ALLCTRL) {
253 lookup_bits(buf, cflags, "cflags", tty->sg_flags);
254 }
255 }
256 #endif
257 return (buf);
258 }
259
260 NCURSES_EXPORT(char *)
_nc_tracebits(void)261 _nc_tracebits(void)
262 {
263 return _nc_trace_ttymode(&(cur_term->Nttyb));
264 }
265 #else
266 EMPTY_MODULE(_nc_empty_lib_tracebits)
267 #endif /* TRACE */
268