1 /****************************************************************************
2 * Copyright 2018-2020,2022 Thomas E. Dickey *
3 * Copyright 1998-2017,2018 Free Software Foundation, Inc. *
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: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
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. *
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 *
28 ****************************************************************************/
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> *
34 * and: Juergen Pfeifer *
35 ****************************************************************************/
38 * lib_traceatr.c - Tracing/Debugging routines (attributes)
41 #include <curses.priv.h>
44 #define CUR SP_TERMTYPE
47 MODULE_ID("$Id: lib_traceatr.c,v 1.95 2022/06/11 22:40:56 tom Exp $")
49 #define COLOR_OF(c) ((c < 0) ? "default" : (c > 7 ? color_of(c) : colors[c].name))
51 #define TRACE_BUF_SIZE(num) (_nc_globals.tracebuf_ptr[num].size)
52 #define COLOR_BUF_SIZE(num) (sizeof(my_buffer[num]))
56 static const char l_brace[] = StringOf(L_BRACE);
57 static const char r_brace[] = StringOf(R_BRACE);
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
70 my_select = !my_select;
71 if (isDefaultColor(c))
72 _nc_STRCPY(my_buffer[my_select], "default",
73 COLOR_BUF_SIZE(my_select));
75 _nc_SPRINTF(my_buffer[my_select],
76 _nc_SLIMIT(COLOR_BUF_SIZE(my_select))
79 return my_buffer[my_select];
84 #endif /* !USE_TERMLIB */
86 NCURSES_EXPORT(char *)
87 _traceattr2(int bufnum, chtype newmode)
89 #define DATA(name) { name, { #name } }
124 #endif /* !USE_TERMLIB */
127 char *result = _nc_trace_buf(bufnum, (size_t) BUFSIZ);
131 unsigned save_nc_tracing = _nc_tracing;
135 _nc_STRCPY(result, l_brace, TRACE_BUF_SIZE(bufnum));
137 for (n = 0; n < SIZEOF(names); n++) {
139 if ((newmode & names[n].val) != 0) {
140 if (result[1] != '\0')
141 (void) _nc_trace_bufcat(bufnum, "|");
142 result = _nc_trace_bufcat(bufnum, names[n].name);
144 if (names[n].val == A_COLOR) {
146 short pairnum = (short) PairNumber(newmode);
148 /* pair_content lives in libncurses */
149 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
152 NCURSES_COLOR_T fg, bg;
154 if (pair_content(pairnum, &fg, &bg) == OK) {
155 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
161 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
165 result = _nc_trace_bufcat(bufnum, temp);
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");
175 _nc_tracing = save_nc_tracing;
176 result = _nc_trace_bufcat(bufnum, r_brace);
181 NCURSES_EXPORT(char *)
182 _traceattr(attr_t newmode)
184 return _traceattr2(0, newmode);
187 /* Trace 'int' return-values */
189 _nc_retrace_int_attr_t(attr_t code)
191 T((T_RETURN("%s"), _traceattr(code)));
195 /* Trace 'attr_t' return-values */
196 NCURSES_EXPORT(attr_t)
197 _nc_retrace_attr_t(attr_t code)
199 T((T_RETURN("%s"), _traceattr(code)));
204 _nc_altcharset_name(attr_t attr, chtype ch)
206 #define DATA(code, name) { code, { #name } }
212 SCREEN *sp = CURRENT_SCREEN;
214 static const ALT_NAMES names[] =
216 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 */
251 const char *result = 0;
256 if (SP_PARM != 0 && (attr & A_ALTCHARSET) && (acs_chars != 0)) {
260 for (cp = acs_chars; cp[0] && cp[1]; cp += 2) {
261 if (ChCharOf(UChar(cp[1])) == ChCharOf(ch)) {
263 /* don't exit from loop - there may be redefinitions */
270 ch = ChCharOf(UChar(*found));
271 for (n = 0; n < SIZEOF(names); ++n) {
272 if (names[n].val == ch) {
273 result = names[n].name;
282 NCURSES_EXPORT(char *)
283 _tracechtype2(int bufnum, chtype ch)
285 char *result = _nc_trace_buf(bufnum, (size_t) BUFSIZ);
289 attr_t attr = ChAttrOf(ch);
291 _nc_STRCPY(result, l_brace, TRACE_BUF_SIZE(bufnum));
292 if ((found = _nc_altcharset_name(attr, ch)) != 0) {
293 (void) _nc_trace_bufcat(bufnum, found);
294 attr &= ~A_ALTCHARSET;
296 (void) _nc_trace_bufcat(bufnum,
297 _nc_tracechar(CURRENT_SCREEN,
298 (int) ChCharOf(ch)));
300 if (attr != A_NORMAL) {
301 (void) _nc_trace_bufcat(bufnum, " | ");
302 (void) _nc_trace_bufcat(bufnum,
303 _traceattr2(bufnum + 20, attr));
306 result = _nc_trace_bufcat(bufnum, r_brace);
311 NCURSES_EXPORT(char *)
312 _tracechtype(chtype ch)
314 return _tracechtype2(0, ch);
317 /* Trace 'chtype' return-values */
318 NCURSES_EXPORT(chtype)
319 _nc_retrace_chtype(chtype code)
321 T((T_RETURN("%s"), _tracechtype(code)));
325 #if USE_WIDEC_SUPPORT
326 NCURSES_EXPORT(char *)
327 _tracecchar_t2(int bufnum, const cchar_t *ch)
329 char *result = _nc_trace_buf(bufnum, (size_t) BUFSIZ);
332 _nc_STRCPY(result, l_brace, TRACE_BUF_SIZE(bufnum));
335 attr_t attr = AttrOfD(ch);
337 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}");
347 (void) _nc_trace_bufcat(bufnum, "{ ");
348 for (PUTC_i = 0; PUTC_i < CCHARW_MAX; ++PUTC_i) {
349 PUTC_ch = ch->chars[PUTC_i];
350 if (PUTC_ch == L'\0') {
352 (void) _nc_trace_bufcat(bufnum, "\\000");
356 PUTC_n = (int) wcrtomb(PUTC_buf, ch->chars[PUTC_i], &PUT_st);
358 if (PUTC_ch != L'\0') {
359 /* it could not be a multibyte sequence */
360 (void) _nc_trace_bufcat(bufnum,
361 _nc_tracechar(CURRENT_SCREEN,
362 UChar(ch->chars[PUTC_i])));
365 } else if (ch->chars[PUTC_i] > 255) {
367 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
369 _nc_wacs_width(ch->chars[PUTC_i]),
370 (unsigned long) ch->chars[PUTC_i]);
371 (void) _nc_trace_bufcat(bufnum, temp);
372 attr &= ~A_CHARTEXT; /* ignore WidecExt(ch) */
374 for (n = 0; n < PUTC_n; n++) {
376 (void) _nc_trace_bufcat(bufnum, ", ");
377 (void) _nc_trace_bufcat(bufnum,
378 _nc_tracechar(CURRENT_SCREEN,
379 UChar(PUTC_buf[n])));
383 (void) _nc_trace_bufcat(bufnum, " }");
385 if (attr != A_NORMAL) {
386 (void) _nc_trace_bufcat(bufnum, " | ");
387 (void) _nc_trace_bufcat(bufnum, _traceattr2(bufnum + 20, attr));
389 #if NCURSES_EXT_COLORS
391 * Just in case the extended color is different from the chtype
394 if (ch->ext_color != PairNumber(attr)) {
396 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
397 " X_COLOR{%d:%d}", ch->ext_color, PairNumber(attr));
398 (void) _nc_trace_bufcat(bufnum, temp);
403 result = _nc_trace_bufcat(bufnum, r_brace);
408 NCURSES_EXPORT(char *)
409 _tracecchar_t(const cchar_t *ch)
411 return _tracecchar_t2(0, ch);
416 EMPTY_MODULE(_nc_lib_traceatr)