a92e00a8624e4341e752616d12777f01aed3a24f
[ncurses.git] / ncurses / trace / lib_tracebits.c
1 /****************************************************************************
2  * Copyright (c) 1998 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
37 MODULE_ID("$Id: lib_tracebits.c,v 1.3 1999/08/21 21:43:48 tom Exp $")
38
39 #if defined(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 #include <fcntl.h>
50 #endif
51
52 /* may be undefined if we're using termio.h */
53 #ifndef TOSTOP
54 #define TOSTOP 0
55 #endif
56 #ifndef IEXTEN
57 #define IEXTEN 0
58 #endif
59
60 #ifdef TRACE
61
62 typedef struct {unsigned int val; const char *name;} BITNAMES;
63
64 static void lookup_bits(char *buf, const BITNAMES *table, const char *label, unsigned int val)
65 {
66         const BITNAMES *sp;
67
68         (void) strcat(buf, label);
69         (void) strcat(buf, ": {");
70         for (sp = table; sp->name; sp++)
71                 if (sp->val != 0
72                 && (val & sp->val) == sp->val)
73                 {
74                         (void) strcat(buf, sp->name);
75                         (void) strcat(buf, ", ");
76                 }
77         if (buf[strlen(buf) - 2] == ',')
78                 buf[strlen(buf) - 2] = '\0';
79         (void) strcat(buf,"} ");
80 }
81
82 char *_nc_tracebits(void)
83 /* describe the state of the terminal control bits exactly */
84 {
85 char    *buf;
86 static const    BITNAMES
87
88 #ifdef TERMIOS
89 iflags[] =
90     {
91         {BRKINT,        "BRKINT"},
92         {IGNBRK,        "IGNBRK"},
93         {IGNPAR,        "IGNPAR"},
94         {PARMRK,        "PARMRK"},
95         {INPCK,         "INPCK"},
96         {ISTRIP,        "ISTRIP"},
97         {INLCR,         "INLCR"},
98         {IGNCR,         "IGNC"},
99         {ICRNL,         "ICRNL"},
100         {IXON,          "IXON"},
101         {IXOFF,         "IXOFF"},
102         {0,             NULL}
103 #define ALLIN   (BRKINT|IGNBRK|IGNPAR|PARMRK|INPCK|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IXOFF)
104     },
105 oflags[] =
106     {
107         {OPOST,         "OPOST"},
108         {0,             NULL}
109 #define ALLOUT  (OPOST)
110     },
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     },
125 lflags[] =
126     {
127         {ECHO,          "ECHO"},
128         {ECHOE|ECHO,    "ECHOE"},       /* concession to readability */
129         {ECHOK|ECHO,    "ECHOK"},       /* concession to readability */
130         {ECHONL,        "ECHONL"},
131         {ICANON,        "ICANON"},
132         {ISIG,          "ISIG"},
133         {NOFLSH,        "NOFLSH"},
134         {TOSTOP,        "TOSTOP"},
135         {IEXTEN,        "IEXTEN"},
136         {0,             NULL}
137 #define ALLLOCAL        (ECHO|ECHONL|ICANON|ISIG|NOFLSH|TOSTOP|IEXTEN)
138     };
139
140
141     buf = _nc_trace_buf(0,
142         8 + sizeof(iflags) +
143         8 + sizeof(oflags) +
144         8 + sizeof(cflags) +
145         8 + sizeof(lflags) +
146         8);
147
148     if (cur_term->Nttyb.c_iflag & ALLIN)
149         lookup_bits(buf, iflags, "iflags", cur_term->Nttyb.c_iflag);
150
151     if (cur_term->Nttyb.c_oflag & ALLOUT)
152         lookup_bits(buf, oflags, "oflags", cur_term->Nttyb.c_oflag);
153
154     if (cur_term->Nttyb.c_cflag & ALLCTRL)
155         lookup_bits(buf, cflags, "cflags", cur_term->Nttyb.c_cflag);
156
157 #if defined(CS5) && defined(CS8)
158     switch (cur_term->Nttyb.c_cflag & CSIZE) {
159 #if defined(CS5) && (CS5 != 0)
160     case CS5:   strcat(buf, "CS5 ");    break;
161 #endif
162 #if defined(CS6) && (CS6 != 0)
163     case CS6:   strcat(buf, "CS6 ");    break;
164 #endif
165 #if defined(CS7) && (CS7 != 0)
166     case CS7:   strcat(buf, "CS7 ");    break;
167 #endif
168 #if defined(CS8) && (CS8 != 0)
169     case CS8:   strcat(buf, "CS8 ");    break;
170 #endif
171     default:    strcat(buf, "CSIZE? "); break;
172     }
173 #endif
174
175     if (cur_term->Nttyb.c_lflag & ALLLOCAL)
176         lookup_bits(buf, lflags, "lflags", cur_term->Nttyb.c_lflag);
177
178 #else
179     /* reference: ttcompat(4M) on SunOS 4.1 */
180 #ifndef EVENP
181 #define EVENP 0
182 #endif
183 #ifndef LCASE
184 #define LCASE 0
185 #endif
186 #ifndef LLITOUT
187 #define LLITOUT 0
188 #endif
189 #ifndef ODDP
190 #define ODDP 0
191 #endif
192 #ifndef TANDEM
193 #define TANDEM 0
194 #endif
195
196 cflags[] =
197     {
198         {CBREAK,        "CBREAK"},
199         {CRMOD,         "CRMOD"},
200         {ECHO,          "ECHO"},
201         {EVENP,         "EVENP"},
202         {LCASE,         "LCASE"},
203         {LLITOUT,       "LLITOUT"},
204         {ODDP,          "ODDP"},
205         {RAW,           "RAW"},
206         {TANDEM,        "TANDEM"},
207         {XTABS,         "XTABS"},
208         {0,             NULL}
209 #define ALLCTRL (CBREAK|CRMOD|ECHO|EVENP|LCASE|LLITOUT|ODDP|RAW|TANDEM|XTABS)
210     };
211
212     buf = _nc_trace_buf(0,
213         8 + sizeof(cflags));
214
215     if (cur_term->Nttyb.sg_flags & ALLCTRL)
216     {
217         lookup_bits(buf, cflags, "cflags", cur_term->Nttyb.sg_flags);
218     }
219
220 #endif
221     return(buf);
222 }
223 #else
224 char *_nc_tracebits(void) { static char tmp[] = ""; return tmp; }
225 #endif /* TRACE */