blob: b36286253d6d7fd6c8ac04e51fe6b2406dc448ce [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2018-2022,2024 Thomas E. Dickey *
3 * Copyright 1998-2017,2018 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05304 * *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
12 * *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
15 * *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
27 * authorization. *
28 ****************************************************************************/
29
30/****************************************************************************
31 * Author: Thomas Dickey 1996-on *
32 * and: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
33 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
Steve Kondikae271bc2015-11-15 02:50:53 +010034 * and: Juergen Pfeifer *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053035 ****************************************************************************/
36
37/*
38 * lib_traceatr.c - Tracing/Debugging routines (attributes)
39 */
40
41#include <curses.priv.h>
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053042
Steve Kondikae271bc2015-11-15 02:50:53 +010043#ifndef CUR
44#define CUR SP_TERMTYPE
45#endif
46
micky3879b9f5e72025-07-08 18:04:53 -040047MODULE_ID("$Id: lib_traceatr.c,v 1.96 2024/02/04 00:11:35 tom Exp $")
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053048
49#define COLOR_OF(c) ((c < 0) ? "default" : (c > 7 ? color_of(c) : colors[c].name))
50
Steve Kondikae271bc2015-11-15 02:50:53 +010051#define TRACE_BUF_SIZE(num) (_nc_globals.tracebuf_ptr[num].size)
52#define COLOR_BUF_SIZE(num) (sizeof(my_buffer[num]))
53
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053054#ifdef TRACE
55
56static const char l_brace[] = StringOf(L_BRACE);
57static const char r_brace[] = StringOf(R_BRACE);
58
59#ifndef USE_TERMLIB
60
61#define my_buffer _nc_globals.traceatr_color_buf
62#define my_select _nc_globals.traceatr_color_sel
63#define my_cached _nc_globals.traceatr_color_last
64
65static char *
66color_of(int c)
67{
68 if (c != my_cached) {
69 my_cached = c;
70 my_select = !my_select;
micky3879b9f5e72025-07-08 18:04:53 -040071 if (isDefaultColor(c))
Steve Kondikae271bc2015-11-15 02:50:53 +010072 _nc_STRCPY(my_buffer[my_select], "default",
73 COLOR_BUF_SIZE(my_select));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053074 else
Steve Kondikae271bc2015-11-15 02:50:53 +010075 _nc_SPRINTF(my_buffer[my_select],
76 _nc_SLIMIT(COLOR_BUF_SIZE(my_select))
77 "color%d", c);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053078 }
79 return my_buffer[my_select];
80}
81
82#undef my_buffer
83#undef my_select
84#endif /* !USE_TERMLIB */
85
86NCURSES_EXPORT(char *)
87_traceattr2(int bufnum, chtype newmode)
88{
Steve Kondikae271bc2015-11-15 02:50:53 +010089#define DATA(name) { name, { #name } }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053090 static const struct {
91 unsigned int val;
Steve Kondikae271bc2015-11-15 02:50:53 +010092 const char name[14];
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053093 } names[] =
94 {
Steve Kondikae271bc2015-11-15 02:50:53 +010095 DATA(A_STANDOUT),
96 DATA(A_UNDERLINE),
97 DATA(A_REVERSE),
98 DATA(A_BLINK),
99 DATA(A_DIM),
100 DATA(A_BOLD),
101 DATA(A_ALTCHARSET),
102 DATA(A_INVIS),
103 DATA(A_PROTECT),
104 DATA(A_CHARTEXT),
105 DATA(A_NORMAL),
106 DATA(A_COLOR),
107#if USE_ITALIC
108 DATA(A_ITALIC),
109#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530110 }
111#ifndef USE_TERMLIB
112 ,
113 colors[] =
114 {
Steve Kondikae271bc2015-11-15 02:50:53 +0100115 DATA(COLOR_BLACK),
116 DATA(COLOR_RED),
117 DATA(COLOR_GREEN),
118 DATA(COLOR_YELLOW),
119 DATA(COLOR_BLUE),
120 DATA(COLOR_MAGENTA),
121 DATA(COLOR_CYAN),
122 DATA(COLOR_WHITE),
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530123 }
124#endif /* !USE_TERMLIB */
125 ;
Steve Kondikae271bc2015-11-15 02:50:53 +0100126#undef DATA
Steve Kondikae271bc2015-11-15 02:50:53 +0100127 char *result = _nc_trace_buf(bufnum, (size_t) BUFSIZ);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530128
129 if (result != 0) {
micky3879b9f5e72025-07-08 18:04:53 -0400130 size_t n;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530131 unsigned save_nc_tracing = _nc_tracing;
132
133 _nc_tracing = 0;
134
Steve Kondikae271bc2015-11-15 02:50:53 +0100135 _nc_STRCPY(result, l_brace, TRACE_BUF_SIZE(bufnum));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530136
137 for (n = 0; n < SIZEOF(names); n++) {
micky3879b9f5e72025-07-08 18:04:53 -0400138
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530139 if ((newmode & names[n].val) != 0) {
140 if (result[1] != '\0')
Steve Kondikae271bc2015-11-15 02:50:53 +0100141 (void) _nc_trace_bufcat(bufnum, "|");
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530142 result = _nc_trace_bufcat(bufnum, names[n].name);
143
144 if (names[n].val == A_COLOR) {
micky3879b9f5e72025-07-08 18:04:53 -0400145 char temp[80];
Steve Kondikae271bc2015-11-15 02:50:53 +0100146 short pairnum = (short) PairNumber(newmode);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530147#ifdef USE_TERMLIB
148 /* pair_content lives in libncurses */
Steve Kondikae271bc2015-11-15 02:50:53 +0100149 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
150 "{%d}", pairnum);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530151#else
Steve Kondikae271bc2015-11-15 02:50:53 +0100152 NCURSES_COLOR_T fg, bg;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530153
154 if (pair_content(pairnum, &fg, &bg) == OK) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100155 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
156 "{%d = {%s, %s}}",
157 pairnum,
158 COLOR_OF(fg),
159 COLOR_OF(bg));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530160 } else {
Steve Kondikae271bc2015-11-15 02:50:53 +0100161 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
162 "{%d}", pairnum);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530163 }
164#endif
165 result = _nc_trace_bufcat(bufnum, temp);
166 }
167 }
168 }
169 if (ChAttrOf(newmode) == A_NORMAL) {
170 if (result != 0 && result[1] != '\0')
171 (void) _nc_trace_bufcat(bufnum, "|");
172 (void) _nc_trace_bufcat(bufnum, "A_NORMAL");
173 }
174
175 _nc_tracing = save_nc_tracing;
176 result = _nc_trace_bufcat(bufnum, r_brace);
177 }
178 return result;
179}
180
181NCURSES_EXPORT(char *)
182_traceattr(attr_t newmode)
183{
184 return _traceattr2(0, newmode);
185}
186
187/* Trace 'int' return-values */
Steve Kondikae271bc2015-11-15 02:50:53 +0100188NCURSES_EXPORT(int)
189_nc_retrace_int_attr_t(attr_t code)
190{
191 T((T_RETURN("%s"), _traceattr(code)));
192 return (int) code;
193}
194
195/* Trace 'attr_t' return-values */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530196NCURSES_EXPORT(attr_t)
197_nc_retrace_attr_t(attr_t code)
198{
199 T((T_RETURN("%s"), _traceattr(code)));
200 return code;
201}
202
203const char *
204_nc_altcharset_name(attr_t attr, chtype ch)
205{
Steve Kondikae271bc2015-11-15 02:50:53 +0100206#define DATA(code, name) { code, { #name } }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530207 typedef struct {
208 unsigned int val;
Steve Kondikae271bc2015-11-15 02:50:53 +0100209 const char name[13];
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530210 } ALT_NAMES;
Steve Kondikae271bc2015-11-15 02:50:53 +0100211#if NCURSES_SP_FUNCS
212 SCREEN *sp = CURRENT_SCREEN;
213#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530214 static const ALT_NAMES names[] =
215 {
Steve Kondikae271bc2015-11-15 02:50:53 +0100216 DATA('l', ACS_ULCORNER), /* upper left corner */
217 DATA('m', ACS_LLCORNER), /* lower left corner */
218 DATA('k', ACS_URCORNER), /* upper right corner */
219 DATA('j', ACS_LRCORNER), /* lower right corner */
220 DATA('t', ACS_LTEE), /* tee pointing right */
221 DATA('u', ACS_RTEE), /* tee pointing left */
222 DATA('v', ACS_BTEE), /* tee pointing up */
223 DATA('w', ACS_TTEE), /* tee pointing down */
224 DATA('q', ACS_HLINE), /* horizontal line */
225 DATA('x', ACS_VLINE), /* vertical line */
226 DATA('n', ACS_PLUS), /* large plus or crossover */
227 DATA('o', ACS_S1), /* scan line 1 */
228 DATA('s', ACS_S9), /* scan line 9 */
229 DATA('`', ACS_DIAMOND), /* diamond */
230 DATA('a', ACS_CKBOARD), /* checker board (stipple) */
231 DATA('f', ACS_DEGREE), /* degree symbol */
232 DATA('g', ACS_PLMINUS), /* plus/minus */
233 DATA('~', ACS_BULLET), /* bullet */
234 DATA(',', ACS_LARROW), /* arrow pointing left */
235 DATA('+', ACS_RARROW), /* arrow pointing right */
236 DATA('.', ACS_DARROW), /* arrow pointing down */
237 DATA('-', ACS_UARROW), /* arrow pointing up */
238 DATA('h', ACS_BOARD), /* board of squares */
239 DATA('i', ACS_LANTERN), /* lantern symbol */
240 DATA('0', ACS_BLOCK), /* solid square block */
241 DATA('p', ACS_S3), /* scan line 3 */
242 DATA('r', ACS_S7), /* scan line 7 */
243 DATA('y', ACS_LEQUAL), /* less/equal */
244 DATA('z', ACS_GEQUAL), /* greater/equal */
245 DATA('{', ACS_PI), /* Pi */
246 DATA('|', ACS_NEQUAL), /* not equal */
247 DATA('}', ACS_STERLING), /* UK pound sign */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530248 };
Steve Kondikae271bc2015-11-15 02:50:53 +0100249#undef DATA
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530250
251 const char *result = 0;
252
Steve Kondikae271bc2015-11-15 02:50:53 +0100253#if NCURSES_SP_FUNCS
254 (void) sp;
255#endif
256 if (SP_PARM != 0 && (attr & A_ALTCHARSET) && (acs_chars != 0)) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530257 char *cp;
258 char *found = 0;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530259
260 for (cp = acs_chars; cp[0] && cp[1]; cp += 2) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100261 if (ChCharOf(UChar(cp[1])) == ChCharOf(ch)) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530262 found = cp;
263 /* don't exit from loop - there may be redefinitions */
264 }
265 }
266
267 if (found != 0) {
micky3879b9f5e72025-07-08 18:04:53 -0400268 size_t n;
269
Steve Kondikae271bc2015-11-15 02:50:53 +0100270 ch = ChCharOf(UChar(*found));
271 for (n = 0; n < SIZEOF(names); ++n) {
272 if (names[n].val == ch) {
273 result = names[n].name;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530274 break;
275 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100276 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530277 }
278 }
279 return result;
280}
281
282NCURSES_EXPORT(char *)
283_tracechtype2(int bufnum, chtype ch)
284{
Steve Kondikae271bc2015-11-15 02:50:53 +0100285 char *result = _nc_trace_buf(bufnum, (size_t) BUFSIZ);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530286
287 if (result != 0) {
micky3879b9f5e72025-07-08 18:04:53 -0400288 const char *found;
289 attr_t attr = ChAttrOf(ch);
290
Steve Kondikae271bc2015-11-15 02:50:53 +0100291 _nc_STRCPY(result, l_brace, TRACE_BUF_SIZE(bufnum));
micky3879b9f5e72025-07-08 18:04:53 -0400292 if ((found = _nc_altcharset_name(attr, ch)) != 0) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530293 (void) _nc_trace_bufcat(bufnum, found);
micky3879b9f5e72025-07-08 18:04:53 -0400294 attr &= ~A_ALTCHARSET;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530295 } else
Steve Kondikae271bc2015-11-15 02:50:53 +0100296 (void) _nc_trace_bufcat(bufnum,
297 _nc_tracechar(CURRENT_SCREEN,
298 (int) ChCharOf(ch)));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530299
micky3879b9f5e72025-07-08 18:04:53 -0400300 if (attr != A_NORMAL) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530301 (void) _nc_trace_bufcat(bufnum, " | ");
302 (void) _nc_trace_bufcat(bufnum,
micky3879b9f5e72025-07-08 18:04:53 -0400303 _traceattr2(bufnum + 20, attr));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530304 }
305
306 result = _nc_trace_bufcat(bufnum, r_brace);
307 }
308 return result;
309}
310
311NCURSES_EXPORT(char *)
312_tracechtype(chtype ch)
313{
314 return _tracechtype2(0, ch);
315}
316
317/* Trace 'chtype' return-values */
318NCURSES_EXPORT(chtype)
319_nc_retrace_chtype(chtype code)
320{
321 T((T_RETURN("%s"), _tracechtype(code)));
322 return code;
323}
324
325#if USE_WIDEC_SUPPORT
326NCURSES_EXPORT(char *)
327_tracecchar_t2(int bufnum, const cchar_t *ch)
328{
Steve Kondikae271bc2015-11-15 02:50:53 +0100329 char *result = _nc_trace_buf(bufnum, (size_t) BUFSIZ);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530330
331 if (result != 0) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100332 _nc_STRCPY(result, l_brace, TRACE_BUF_SIZE(bufnum));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530333 if (ch != 0) {
micky3879b9f5e72025-07-08 18:04:53 -0400334 const char *found;
335 attr_t attr = AttrOfD(ch);
336
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530337 if ((found = _nc_altcharset_name(attr, (chtype) CharOfD(ch))) != 0) {
338 (void) _nc_trace_bufcat(bufnum, found);
339 attr &= ~A_ALTCHARSET;
340 } else if (isWidecExt(CHDEREF(ch))) {
341 (void) _nc_trace_bufcat(bufnum, "{NAC}");
342 attr &= ~A_CHARTEXT;
343 } else {
344 PUTC_DATA;
345 int n;
micky3879b9f5e72025-07-08 18:04:53 -0400346 int assume_unicode = _nc_unicode_locale()? 128 : 255;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530347
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530348 (void) _nc_trace_bufcat(bufnum, "{ ");
349 for (PUTC_i = 0; PUTC_i < CCHARW_MAX; ++PUTC_i) {
350 PUTC_ch = ch->chars[PUTC_i];
Steve Kondikae271bc2015-11-15 02:50:53 +0100351 if (PUTC_ch == L'\0') {
352 if (PUTC_i == 0)
353 (void) _nc_trace_bufcat(bufnum, "\\000");
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530354 break;
Steve Kondikae271bc2015-11-15 02:50:53 +0100355 }
micky3879b9f5e72025-07-08 18:04:53 -0400356 PUTC_INIT;
Steve Kondikae271bc2015-11-15 02:50:53 +0100357 PUTC_n = (int) wcrtomb(PUTC_buf, ch->chars[PUTC_i], &PUT_st);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530358 if (PUTC_n <= 0) {
359 if (PUTC_ch != L'\0') {
360 /* it could not be a multibyte sequence */
361 (void) _nc_trace_bufcat(bufnum,
Steve Kondikae271bc2015-11-15 02:50:53 +0100362 _nc_tracechar(CURRENT_SCREEN,
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530363 UChar(ch->chars[PUTC_i])));
364 }
365 break;
micky3879b9f5e72025-07-08 18:04:53 -0400366 } else if (ch->chars[PUTC_i] > assume_unicode) {
367 char temp[80];
368 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
369 "{%d:\\u%04lx}",
370 _nc_wacs_width(ch->chars[PUTC_i]),
371 (unsigned long) ch->chars[PUTC_i]);
372 (void) _nc_trace_bufcat(bufnum, temp);
373 attr &= ~A_CHARTEXT; /* ignore WidecExt(ch) */
374 } else {
375 for (n = 0; n < PUTC_n; n++) {
376 if (n)
377 (void) _nc_trace_bufcat(bufnum, ", ");
378 (void) _nc_trace_bufcat(bufnum,
379 _nc_tracechar(CURRENT_SCREEN,
380 UChar(PUTC_buf[n])));
381 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530382 }
383 }
384 (void) _nc_trace_bufcat(bufnum, " }");
385 }
386 if (attr != A_NORMAL) {
387 (void) _nc_trace_bufcat(bufnum, " | ");
388 (void) _nc_trace_bufcat(bufnum, _traceattr2(bufnum + 20, attr));
389 }
micky3879b9f5e72025-07-08 18:04:53 -0400390#if NCURSES_EXT_COLORS
391 /*
392 * Just in case the extended color is different from the chtype
393 * value, trace both.
394 */
395 if (ch->ext_color != PairNumber(attr)) {
396 char temp[80];
397 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
398 " X_COLOR{%d:%d}", ch->ext_color, PairNumber(attr));
399 (void) _nc_trace_bufcat(bufnum, temp);
400 }
401#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530402 }
403
404 result = _nc_trace_bufcat(bufnum, r_brace);
405 }
406 return result;
407}
408
409NCURSES_EXPORT(char *)
410_tracecchar_t(const cchar_t *ch)
411{
412 return _tracecchar_t2(0, ch);
413}
414#endif
415
416#else
417EMPTY_MODULE(_nc_lib_traceatr)
418#endif /* TRACE */