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