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