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