]> ncurses.scripts.mit.edu Git - ncurses.git/blob - lib_traceatr.c
ee6cf5f1f3d37f175d4cc67f68944d8f4831856a
[ncurses.git] / lib_traceatr.c
1 /****************************************************************************
2  * Copyright (c) 1998-2005,2006 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: Thomas Dickey                           1996-on                 *
31  *     and: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
32  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
33  ****************************************************************************/
34
35 /*
36  *      lib_traceatr.c - Tracing/Debugging routines (attributes)
37  */
38
39 #include <curses.priv.h>
40 #include <term.h>               /* acs_chars */
41
42 MODULE_ID("$Id: lib_traceatr.c,v 1.56 2006/12/02 21:18:28 tom Exp $")
43
44 #define COLOR_OF(c) ((c < 0) ? "default" : (c > 7 ? color_of(c) : colors[c].name))
45
46 #ifdef TRACE
47
48 static const char l_brace[] = {L_BRACE, 0};
49 static const char r_brace[] = {R_BRACE, 0};
50
51 #ifndef USE_TERMLIB
52 static char *
53 color_of(int c)
54 {
55     static char buffer[2][80];
56     static int sel;
57     static int last = -1;
58
59     if (c != last) {
60         last = c;
61         sel = !sel;
62         if (c == COLOR_DEFAULT)
63             strcpy(buffer[sel], "default");
64         else
65             sprintf(buffer[sel], "color%d", c);
66     }
67     return buffer[sel];
68 }
69 #endif /* !USE_TERMLIB */
70
71 NCURSES_EXPORT(char *)
72 _traceattr2(int bufnum, chtype newmode)
73 {
74     char *buf = _nc_trace_buf(bufnum, BUFSIZ);
75     char temp[80];
76     static const struct {
77         unsigned int val;
78         const char *name;
79     } names[] =
80     {
81         /* *INDENT-OFF* */
82         { A_STANDOUT,           "A_STANDOUT" },
83         { A_UNDERLINE,          "A_UNDERLINE" },
84         { A_REVERSE,            "A_REVERSE" },
85         { A_BLINK,              "A_BLINK" },
86         { A_DIM,                "A_DIM" },
87         { A_BOLD,               "A_BOLD" },
88         { A_ALTCHARSET,         "A_ALTCHARSET" },
89         { A_INVIS,              "A_INVIS" },
90         { A_PROTECT,            "A_PROTECT" },
91         { A_CHARTEXT,           "A_CHARTEXT" },
92         { A_NORMAL,             "A_NORMAL" },
93         { A_COLOR,              "A_COLOR" },
94         /* *INDENT-ON* */
95
96     }
97 #ifndef USE_TERMLIB
98     ,
99         colors[] =
100     {
101         /* *INDENT-OFF* */
102         { COLOR_BLACK,          "COLOR_BLACK" },
103         { COLOR_RED,            "COLOR_RED" },
104         { COLOR_GREEN,          "COLOR_GREEN" },
105         { COLOR_YELLOW,         "COLOR_YELLOW" },
106         { COLOR_BLUE,           "COLOR_BLUE" },
107         { COLOR_MAGENTA,        "COLOR_MAGENTA" },
108         { COLOR_CYAN,           "COLOR_CYAN" },
109         { COLOR_WHITE,          "COLOR_WHITE" },
110         /* *INDENT-ON* */
111
112     }
113 #endif /* !USE_TERMLIB */
114     ;
115     size_t n;
116     unsigned save_nc_tracing = _nc_tracing;
117     _nc_tracing = 0;
118
119     strcpy(buf, l_brace);
120
121     for (n = 0; n < SIZEOF(names); n++) {
122         if ((newmode & names[n].val) != 0) {
123             if (buf[1] != '\0')
124                 buf = _nc_trace_bufcat(bufnum, "|");
125             buf = _nc_trace_bufcat(bufnum, names[n].name);
126
127             if (names[n].val == A_COLOR) {
128                 short pairnum = PAIR_NUMBER(newmode);
129 #ifdef USE_TERMLIB
130                 /* pair_content lives in libncurses */
131                 (void) sprintf(temp, "{%d}", pairnum);
132 #else
133                 short fg, bg;
134
135                 if (pair_content(pairnum, &fg, &bg) == OK) {
136                     (void) sprintf(temp,
137                                    "{%d = {%s, %s}}",
138                                    pairnum,
139                                    COLOR_OF(fg),
140                                    COLOR_OF(bg));
141                 } else {
142                     (void) sprintf(temp, "{%d}", pairnum);
143                 }
144 #endif
145                 buf = _nc_trace_bufcat(bufnum, temp);
146             }
147         }
148     }
149     if (ChAttrOf(newmode) == A_NORMAL) {
150         if (buf[1] != '\0')
151             (void) _nc_trace_bufcat(bufnum, "|");
152         (void) _nc_trace_bufcat(bufnum, "A_NORMAL");
153     }
154
155     _nc_tracing = save_nc_tracing;
156     return (_nc_trace_bufcat(bufnum, r_brace));
157 }
158
159 NCURSES_EXPORT(char *)
160 _traceattr(attr_t newmode)
161 {
162     return _traceattr2(0, newmode);
163 }
164
165 /* Trace 'int' return-values */
166 NCURSES_EXPORT(attr_t)
167 _nc_retrace_attr_t(attr_t code)
168 {
169     T((T_RETURN("%s"), _traceattr(code)));
170     return code;
171 }
172
173 const char *
174 _nc_altcharset_name(attr_t attr, chtype ch)
175 {
176     const char *result = 0;
177
178     if ((attr & A_ALTCHARSET) && (acs_chars != 0)) {
179         char *cp;
180         char *found = 0;
181         static const struct {
182             unsigned int val;
183             const char *name;
184         } names[] =
185         {
186             /* *INDENT-OFF* */
187             { 'l', "ACS_ULCORNER" },    /* upper left corner */
188             { 'm', "ACS_LLCORNER" },    /* lower left corner */
189             { 'k', "ACS_URCORNER" },    /* upper right corner */
190             { 'j', "ACS_LRCORNER" },    /* lower right corner */
191             { 't', "ACS_LTEE" },        /* tee pointing right */
192             { 'u', "ACS_RTEE" },        /* tee pointing left */
193             { 'v', "ACS_BTEE" },        /* tee pointing up */
194             { 'w', "ACS_TTEE" },        /* tee pointing down */
195             { 'q', "ACS_HLINE" },       /* horizontal line */
196             { 'x', "ACS_VLINE" },       /* vertical line */
197             { 'n', "ACS_PLUS" },        /* large plus or crossover */
198             { 'o', "ACS_S1" },          /* scan line 1 */
199             { 's', "ACS_S9" },          /* scan line 9 */
200             { '`', "ACS_DIAMOND" },     /* diamond */
201             { 'a', "ACS_CKBOARD" },     /* checker board (stipple) */
202             { 'f', "ACS_DEGREE" },      /* degree symbol */
203             { 'g', "ACS_PLMINUS" },     /* plus/minus */
204             { '~', "ACS_BULLET" },      /* bullet */
205             { ',', "ACS_LARROW" },      /* arrow pointing left */
206             { '+', "ACS_RARROW" },      /* arrow pointing right */
207             { '.', "ACS_DARROW" },      /* arrow pointing down */
208             { '-', "ACS_UARROW" },      /* arrow pointing up */
209             { 'h', "ACS_BOARD" },       /* board of squares */
210             { 'i', "ACS_LANTERN" },     /* lantern symbol */
211             { '0', "ACS_BLOCK" },       /* solid square block */
212             { 'p', "ACS_S3" },          /* scan line 3 */
213             { 'r', "ACS_S7" },          /* scan line 7 */
214             { 'y', "ACS_LEQUAL" },      /* less/equal */
215             { 'z', "ACS_GEQUAL" },      /* greater/equal */
216             { '{', "ACS_PI" },          /* Pi */
217             { '|', "ACS_NEQUAL" },      /* not equal */
218             { '}', "ACS_STERLING" },    /* UK pound sign */
219             { '\0', (char *) 0 }
220                 /* *INDENT-OFF* */
221         },
222             *sp;
223
224         for (cp = acs_chars; cp[0] && cp[1]; cp += 2) {
225             if (ChCharOf(cp[1]) == ChCharOf(ch)) {
226                 found = cp;
227                 /* don't exit from loop - there may be redefinitions */
228             }
229         }
230
231         if (found != 0) {
232             ch = ChCharOf(*found);
233             for (sp = names; sp->val; sp++)
234                 if (sp->val == ch) {
235                     result = sp->name;
236                     break;
237                 }
238         }
239     }
240     return result;
241 }
242
243 NCURSES_EXPORT(char *)
244 _tracechtype2(int bufnum, chtype ch)
245 {
246     const char *found;
247
248     strcpy(_nc_trace_buf(bufnum, BUFSIZ), l_brace);
249     if ((found = _nc_altcharset_name(ChAttrOf(ch), ch)) != 0) {
250         (void) _nc_trace_bufcat(bufnum, found);
251     } else
252         (void) _nc_trace_bufcat(bufnum, _tracechar((int)ChCharOf(ch)));
253
254     if (ChAttrOf(ch) != A_NORMAL) {
255         (void) _nc_trace_bufcat(bufnum, " | ");
256         (void) _nc_trace_bufcat(bufnum,
257                 _traceattr2(bufnum + 20, ChAttrOf(ch)));
258     }
259
260     return (_nc_trace_bufcat(bufnum, r_brace));
261 }
262
263 NCURSES_EXPORT(char *)
264 _tracechtype (chtype ch)
265 {
266     return _tracechtype2(0, ch);
267 }
268
269 /* Trace 'chtype' return-values */
270 NCURSES_EXPORT(chtype)
271 _nc_retrace_chtype (chtype code)
272 {
273     T((T_RETURN("%s"), _tracechtype(code)));
274     return code;
275 }
276
277 #if USE_WIDEC_SUPPORT
278 NCURSES_EXPORT(char *)
279 _tracecchar_t2 (int bufnum, const cchar_t *ch)
280 {
281     char *buf = _nc_trace_buf(bufnum, BUFSIZ);
282     attr_t attr;
283     const char *found;
284
285     strcpy(buf, l_brace);
286     if (ch != 0) {
287         attr = AttrOfD(ch);
288         if ((found = _nc_altcharset_name(attr, (chtype) CharOfD(ch))) != 0) {
289             (void) _nc_trace_bufcat(bufnum, found);
290             attr &= ~A_ALTCHARSET;
291         } else if (isWidecExt(CHDEREF(ch))) {
292             (void) _nc_trace_bufcat(bufnum, "{NAC}");
293             attr &= ~A_CHARTEXT;
294         } else {
295             PUTC_DATA;
296             int n;
297
298             PUTC_INIT;
299             (void) _nc_trace_bufcat(bufnum, "{ ");
300             for (PUTC_i = 0; PUTC_i < CCHARW_MAX; ++PUTC_i) {
301                 PUTC_ch = ch->chars[PUTC_i];
302                 if (PUTC_ch == L'\0')
303                     break;
304                 PUTC_n = wcrtomb(PUTC_buf, ch->chars[PUTC_i], &PUT_st);
305                 if (PUTC_n <= 0) {
306                     if (PUTC_ch != L'\0') {
307                         /* it could not be a multibyte sequence */
308                         (void) _nc_trace_bufcat(bufnum, _tracechar(UChar(ch->chars[PUTC_i])));
309                     }
310                     break;
311                 }
312                 for (n = 0; n < PUTC_n; n++) {
313                     if (n)
314                         (void) _nc_trace_bufcat(bufnum, ", ");
315                     (void) _nc_trace_bufcat(bufnum, _tracechar(UChar(PUTC_buf[n])));
316                 }
317             }
318             (void) _nc_trace_bufcat(bufnum, " }");
319         }
320         if (attr != A_NORMAL) {
321             (void) _nc_trace_bufcat(bufnum, " | ");
322             (void) _nc_trace_bufcat(bufnum, _traceattr2(bufnum + 20, attr));
323         }
324     }
325
326     return (_nc_trace_bufcat(bufnum, r_brace));
327 }
328
329 NCURSES_EXPORT(char *)
330 _tracecchar_t (const cchar_t *ch)
331 {
332     return _tracecchar_t2(0, ch);
333 }
334 #endif
335
336 #else
337 empty_module(_nc_lib_traceatr)
338 #endif /* TRACE */