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