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