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