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