GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libcurses/trace/lib_traceatr.c Lines: 0 1 0.0 %
Date: 2017-11-07 Branches: 0 0 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: lib_traceatr.c,v 1.6 2010/01/12 23:22:07 nicm Exp $ */
2
3
/****************************************************************************
4
 * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc.              *
5
 *                                                                          *
6
 * Permission is hereby granted, free of charge, to any person obtaining a  *
7
 * copy of this software and associated documentation files (the            *
8
 * "Software"), to deal in the Software without restriction, including      *
9
 * without limitation the rights to use, copy, modify, merge, publish,      *
10
 * distribute, distribute with modifications, sublicense, and/or sell       *
11
 * copies of the Software, and to permit persons to whom the Software is    *
12
 * furnished to do so, subject to the following conditions:                 *
13
 *                                                                          *
14
 * The above copyright notice and this permission notice shall be included  *
15
 * in all copies or substantial portions of the Software.                   *
16
 *                                                                          *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20
 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24
 *                                                                          *
25
 * Except as contained in this notice, the name(s) of the above copyright   *
26
 * holders shall not be used in advertising or otherwise to promote the     *
27
 * sale, use or other dealings in this Software without prior written       *
28
 * authorization.                                                           *
29
 ****************************************************************************/
30
31
/****************************************************************************
32
 *  Author: Thomas Dickey                           1996-on                 *
33
 *     and: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
34
 *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
35
 ****************************************************************************/
36
37
/*
38
 *	lib_traceatr.c - Tracing/Debugging routines (attributes)
39
 */
40
41
#include <curses.priv.h>
42
#include <term.h>		/* acs_chars */
43
44
MODULE_ID("$Id: lib_traceatr.c,v 1.6 2010/01/12 23:22:07 nicm Exp $")
45
46
#define COLOR_OF(c) ((c < 0) ? "default" : (c > 7 ? color_of(c) : colors[c].name))
47
48
#ifdef TRACE
49
50
static const char l_brace[] = StringOf(L_BRACE);
51
static const char r_brace[] = StringOf(R_BRACE);
52
53
#ifndef USE_TERMLIB
54
55
#define my_buffer _nc_globals.traceatr_color_buf
56
#define my_select _nc_globals.traceatr_color_sel
57
#define my_cached _nc_globals.traceatr_color_last
58
59
static char *
60
color_of(int c)
61
{
62
    if (c != my_cached) {
63
	my_cached = c;
64
	my_select = !my_select;
65
	if (c == COLOR_DEFAULT)
66
	    strlcpy(my_buffer[my_select], "default", _nc_globals_traceatr_color_buf_size);
67
	else
68
	    snprintf(my_buffer[my_select], _nc_globals_traceatr_color_buf_size, "color%d", c);
69
    }
70
    return my_buffer[my_select];
71
}
72
73
#undef my_buffer
74
#undef my_select
75
#endif /* !USE_TERMLIB */
76
77
NCURSES_EXPORT(char *)
78
_traceattr2(int bufnum, chtype newmode)
79
{
80
    static const struct {
81
	unsigned int val;
82
	const char *name;
83
    } names[] =
84
    {
85
	/* *INDENT-OFF* */
86
	{ A_STANDOUT,		"A_STANDOUT" },
87
	{ A_UNDERLINE,		"A_UNDERLINE" },
88
	{ A_REVERSE,		"A_REVERSE" },
89
	{ A_BLINK,		"A_BLINK" },
90
	{ A_DIM,		"A_DIM" },
91
	{ A_BOLD,		"A_BOLD" },
92
	{ A_ALTCHARSET,		"A_ALTCHARSET" },
93
	{ A_INVIS,		"A_INVIS" },
94
	{ A_PROTECT,		"A_PROTECT" },
95
	{ A_CHARTEXT,		"A_CHARTEXT" },
96
	{ A_NORMAL,		"A_NORMAL" },
97
	{ A_COLOR,		"A_COLOR" },
98
	/* *INDENT-ON* */
99
100
    }
101
#ifndef USE_TERMLIB
102
    ,
103
	colors[] =
104
    {
105
	/* *INDENT-OFF* */
106
	{ COLOR_BLACK,		"COLOR_BLACK" },
107
	{ COLOR_RED,		"COLOR_RED" },
108
	{ COLOR_GREEN,		"COLOR_GREEN" },
109
	{ COLOR_YELLOW,		"COLOR_YELLOW" },
110
	{ COLOR_BLUE,		"COLOR_BLUE" },
111
	{ COLOR_MAGENTA,	"COLOR_MAGENTA" },
112
	{ COLOR_CYAN,		"COLOR_CYAN" },
113
	{ COLOR_WHITE,		"COLOR_WHITE" },
114
	/* *INDENT-ON* */
115
116
    }
117
#endif /* !USE_TERMLIB */
118
    ;
119
    size_t n;
120
    char temp[80];
121
    char *result = _nc_trace_buf(bufnum, BUFSIZ);
122
123
    if (result != 0) {
124
	unsigned save_nc_tracing = _nc_tracing;
125
126
	_nc_tracing = 0;
127
128
	strlcpy(result, l_brace, BUFSIZ);
129
130
	for (n = 0; n < SIZEOF(names); n++) {
131
	    if ((newmode & names[n].val) != 0) {
132
		if (result[1] != '\0')
133
		    result = _nc_trace_bufcat(bufnum, "|");
134
		result = _nc_trace_bufcat(bufnum, names[n].name);
135
136
		if (names[n].val == A_COLOR) {
137
		    short pairnum = PAIR_NUMBER(newmode);
138
#ifdef USE_TERMLIB
139
		    /* pair_content lives in libncurses */
140
		    (void) snprintf(temp, sizeof(temp), "{%d}", pairnum);
141
#else
142
		    short fg, bg;
143
144
		    if (pair_content(pairnum, &fg, &bg) == OK) {
145
			(void) snprintf(temp, sizeof(temp),
146
				       "{%d = {%s, %s}}",
147
				       pairnum,
148
				       COLOR_OF(fg),
149
				       COLOR_OF(bg));
150
		    } else {
151
			(void) snprintf(temp, sizeof(temp), "{%d}", pairnum);
152
		    }
153
#endif
154
		    result = _nc_trace_bufcat(bufnum, temp);
155
		}
156
	    }
157
	}
158
	if (ChAttrOf(newmode) == A_NORMAL) {
159
	    if (result != 0 && result[1] != '\0')
160
		(void) _nc_trace_bufcat(bufnum, "|");
161
	    (void) _nc_trace_bufcat(bufnum, "A_NORMAL");
162
	}
163
164
	_nc_tracing = save_nc_tracing;
165
	result = _nc_trace_bufcat(bufnum, r_brace);
166
    }
167
    return result;
168
}
169
170
NCURSES_EXPORT(char *)
171
_traceattr(attr_t newmode)
172
{
173
    return _traceattr2(0, newmode);
174
}
175
176
/* Trace 'int' return-values */
177
NCURSES_EXPORT(attr_t)
178
_nc_retrace_attr_t(attr_t code)
179
{
180
    T((T_RETURN("%s"), _traceattr(code)));
181
    return code;
182
}
183
184
const char *
185
_nc_altcharset_name(attr_t attr, chtype ch)
186
{
187
    typedef struct {
188
	unsigned int val;
189
	const char *name;
190
    } ALT_NAMES;
191
    static const ALT_NAMES names[] =
192
    {
193
	{'l', "ACS_ULCORNER"},	/* upper left corner */
194
	{'m', "ACS_LLCORNER"},	/* lower left corner */
195
	{'k', "ACS_URCORNER"},	/* upper right corner */
196
	{'j', "ACS_LRCORNER"},	/* lower right corner */
197
	{'t', "ACS_LTEE"},	/* tee pointing right */
198
	{'u', "ACS_RTEE"},	/* tee pointing left */
199
	{'v', "ACS_BTEE"},	/* tee pointing up */
200
	{'w', "ACS_TTEE"},	/* tee pointing down */
201
	{'q', "ACS_HLINE"},	/* horizontal line */
202
	{'x', "ACS_VLINE"},	/* vertical line */
203
	{'n', "ACS_PLUS"},	/* large plus or crossover */
204
	{'o', "ACS_S1"},	/* scan line 1 */
205
	{'s', "ACS_S9"},	/* scan line 9 */
206
	{'`', "ACS_DIAMOND"},	/* diamond */
207
	{'a', "ACS_CKBOARD"},	/* checker board (stipple) */
208
	{'f', "ACS_DEGREE"},	/* degree symbol */
209
	{'g', "ACS_PLMINUS"},	/* plus/minus */
210
	{'~', "ACS_BULLET"},	/* bullet */
211
	{',', "ACS_LARROW"},	/* arrow pointing left */
212
	{'+', "ACS_RARROW"},	/* arrow pointing right */
213
	{'.', "ACS_DARROW"},	/* arrow pointing down */
214
	{'-', "ACS_UARROW"},	/* arrow pointing up */
215
	{'h', "ACS_BOARD"},	/* board of squares */
216
	{'i', "ACS_LANTERN"},	/* lantern symbol */
217
	{'0', "ACS_BLOCK"},	/* solid square block */
218
	{'p', "ACS_S3"},	/* scan line 3 */
219
	{'r', "ACS_S7"},	/* scan line 7 */
220
	{'y', "ACS_LEQUAL"},	/* less/equal */
221
	{'z', "ACS_GEQUAL"},	/* greater/equal */
222
	{'{', "ACS_PI"},	/* Pi */
223
	{'|', "ACS_NEQUAL"},	/* not equal */
224
	{'}', "ACS_STERLING"},	/* UK pound sign */
225
	{'\0', (char *) 0}
226
    };
227
228
    const char *result = 0;
229
230
    if ((attr & A_ALTCHARSET) && (acs_chars != 0)) {
231
	char *cp;
232
	char *found = 0;
233
	const ALT_NAMES *sp;
234
235
	for (cp = acs_chars; cp[0] && cp[1]; cp += 2) {
236
	    if (ChCharOf(cp[1]) == ChCharOf(ch)) {
237
		found = cp;
238
		/* don't exit from loop - there may be redefinitions */
239
	    }
240
	}
241
242
	if (found != 0) {
243
	    ch = ChCharOf(*found);
244
	    for (sp = names; sp->val; sp++)
245
		if (sp->val == ch) {
246
		    result = sp->name;
247
		    break;
248
		}
249
	}
250
    }
251
    return result;
252
}
253
254
NCURSES_EXPORT(char *)
255
_tracechtype2(int bufnum, chtype ch)
256
{
257
    const char *found;
258
    char *result = _nc_trace_buf(bufnum, BUFSIZ);
259
260
    if (result != 0) {
261
	strlcpy(result, l_brace, BUFSIZ);
262
	if ((found = _nc_altcharset_name(ChAttrOf(ch), ch)) != 0) {
263
	    (void) _nc_trace_bufcat(bufnum, found);
264
	} else
265
	    (void) _nc_trace_bufcat(bufnum, _nc_tracechar(SP, (int) ChCharOf(ch)));
266
267
	if (ChAttrOf(ch) != A_NORMAL) {
268
	    (void) _nc_trace_bufcat(bufnum, " | ");
269
	    (void) _nc_trace_bufcat(bufnum,
270
				    _traceattr2(bufnum + 20, ChAttrOf(ch)));
271
	}
272
273
	result = _nc_trace_bufcat(bufnum, r_brace);
274
    }
275
    return result;
276
}
277
278
NCURSES_EXPORT(char *)
279
_tracechtype(chtype ch)
280
{
281
    return _tracechtype2(0, ch);
282
}
283
284
/* Trace 'chtype' return-values */
285
NCURSES_EXPORT(chtype)
286
_nc_retrace_chtype(chtype code)
287
{
288
    T((T_RETURN("%s"), _tracechtype(code)));
289
    return code;
290
}
291
292
#if USE_WIDEC_SUPPORT
293
NCURSES_EXPORT(char *)
294
_tracecchar_t2(int bufnum, const cchar_t *ch)
295
{
296
    char *result = _nc_trace_buf(bufnum, BUFSIZ);
297
    attr_t attr;
298
    const char *found;
299
300
    if (result != 0) {
301
	    strlcpy(result, l_brace, BUFSIZ);
302
	if (ch != 0) {
303
	    attr = AttrOfD(ch);
304
	    if ((found = _nc_altcharset_name(attr, (chtype) CharOfD(ch))) != 0) {
305
		(void) _nc_trace_bufcat(bufnum, found);
306
		attr &= ~A_ALTCHARSET;
307
	    } else if (isWidecExt(CHDEREF(ch))) {
308
		(void) _nc_trace_bufcat(bufnum, "{NAC}");
309
		attr &= ~A_CHARTEXT;
310
	    } else {
311
		PUTC_DATA;
312
		int n;
313
314
		PUTC_INIT;
315
		(void) _nc_trace_bufcat(bufnum, "{ ");
316
		for (PUTC_i = 0; PUTC_i < CCHARW_MAX; ++PUTC_i) {
317
		    PUTC_ch = ch->chars[PUTC_i];
318
		    if (PUTC_ch == L'\0')
319
			break;
320
		    PUTC_n = wcrtomb(PUTC_buf, ch->chars[PUTC_i], &PUT_st);
321
		    if (PUTC_n <= 0) {
322
			if (PUTC_ch != L'\0') {
323
			    /* it could not be a multibyte sequence */
324
			    (void) _nc_trace_bufcat(bufnum,
325
						    _nc_tracechar(SP,
326
								  UChar(ch->chars[PUTC_i])));
327
			}
328
			break;
329
		    }
330
		    for (n = 0; n < PUTC_n; n++) {
331
			if (n)
332
			    (void) _nc_trace_bufcat(bufnum, ", ");
333
			(void) _nc_trace_bufcat(bufnum,
334
						_nc_tracechar(SP,
335
							      UChar(PUTC_buf[n])));
336
		    }
337
		}
338
		(void) _nc_trace_bufcat(bufnum, " }");
339
	    }
340
	    if (attr != A_NORMAL) {
341
		(void) _nc_trace_bufcat(bufnum, " | ");
342
		(void) _nc_trace_bufcat(bufnum, _traceattr2(bufnum + 20, attr));
343
	    }
344
	}
345
346
	result = _nc_trace_bufcat(bufnum, r_brace);
347
    }
348
    return result;
349
}
350
351
NCURSES_EXPORT(char *)
352
_tracecchar_t(const cchar_t *ch)
353
{
354
    return _tracecchar_t2(0, ch);
355
}
356
#endif
357
358
#else
359
EMPTY_MODULE(_nc_lib_traceatr)
360
#endif /* TRACE */