1 /****************************************************************************
2  * Copyright 2020 Thomas E. Dickey                                          *
3  * Copyright 1998-2012,2016 Free Software Foundation, Inc.                  *
4  *                                                                          *
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 E. Dickey 1996-on                                        *
32  *     and: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
33  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
34  ****************************************************************************/
35 
36 /*
37  *	lib_tracedmp.c - Tracing/Debugging routines
38  */
39 
40 #include <curses.priv.h>
41 #include <ctype.h>
42 
43 MODULE_ID("$Id: lib_tracedmp.c,v 1.36 2020/02/02 23:34:34 tom Exp $")
44 
45 #ifdef TRACE
46 
47 #define my_buffer _nc_globals.tracedmp_buf
48 #define my_length _nc_globals.tracedmp_used
49 
50 NCURSES_EXPORT(void)
51 _tracedump(const char *name, WINDOW *win)
52 {
53     int i, j, n, width;
54 
55     /* compute narrowest possible display width */
56     for (width = i = 0; i <= win->_maxy; ++i) {
57 	n = 0;
58 	for (j = 0; j <= win->_maxx; ++j) {
59 	    if (CharOf(win->_line[i].text[j]) != L(' ')
60 		|| AttrOf(win->_line[i].text[j]) != A_NORMAL
61 		|| GetPair(win->_line[i].text[j]) != 0) {
62 		n = j;
63 	    }
64 	}
65 
66 	if (n > width)
67 	    width = n;
68     }
69     if (width < win->_maxx)
70 	++width;
71     if (++width + 1 > (int) my_length) {
72 	my_length = (unsigned) (2 * (width + 1));
73 	my_buffer = typeRealloc(char, my_length, my_buffer);
74 	if (my_buffer == 0)
75 	    return;
76     }
77 
78     for (n = 0; n <= win->_maxy; ++n) {
79 	char *ep = my_buffer;
80 	bool havecolors;
81 
82 	/*
83 	 * Dump A_CHARTEXT part.  It is more important to make the grid line up
84 	 * in the trace file than to represent control- and wide-characters, so
85 	 * we map those to '.' and '?' respectively.
86 	 */
87 	for (j = 0; j < width; ++j) {
88 	    chtype test = (chtype) CharOf(win->_line[n].text[j]);
89 	    ep[j] = (char) ((UChar(test) == test
90 #if USE_WIDEC_SUPPORT
91 			     && (win->_line[n].text[j].chars[1] == 0)
92 #endif
93 			    )
94 			    ? (iscntrl(UChar(test))
95 			       ? '.'
96 			       : UChar(test))
97 			    : '?');
98 	}
99 	ep[j] = '\0';
100 	_tracef("%s[%2d] %3ld%3ld ='%s'",
101 		name, n,
102 		(long) win->_line[n].firstchar,
103 		(long) win->_line[n].lastchar,
104 		ep);
105 
106 	/* if there are multi-column characters on the line, print them now */
107 	if_WIDEC({
108 	    bool multicolumn = FALSE;
109 	    for (j = 0; j < width; ++j)
110 		if (WidecExt(win->_line[n].text[j]) != 0) {
111 		    multicolumn = TRUE;
112 		    break;
113 		}
114 	    if (multicolumn) {
115 		ep = my_buffer;
116 		for (j = 0; j < width; ++j) {
117 		    int test = WidecExt(win->_line[n].text[j]);
118 		    if (test) {
119 			ep[j] = (char) (test + '0');
120 		    } else {
121 			ep[j] = ' ';
122 		    }
123 		}
124 		ep[j] = '\0';
125 		_tracef("%*s[%2d]%*s='%s'", (int) strlen(name),
126 			"widec", n, 8, " ", my_buffer);
127 	    }
128 	});
129 
130 	/* dump A_COLOR part, will screw up if there are more than 96 */
131 	havecolors = FALSE;
132 	for (j = 0; j < width; ++j)
133 	    if (GetPair(win->_line[n].text[j]) != 0) {
134 		havecolors = TRUE;
135 		break;
136 	    }
137 	if (havecolors) {
138 	    ep = my_buffer;
139 	    for (j = 0; j < width; ++j) {
140 		int pair = GetPair(win->_line[n].text[j]);
141 		if (pair >= 52)
142 		    ep[j] = '?';
143 		else if (pair >= 36)
144 		    ep[j] = (char) (pair + 'A');
145 		else if (pair >= 10)
146 		    ep[j] = (char) (pair + 'a');
147 		else if (pair >= 1)
148 		    ep[j] = (char) (pair + '0');
149 		else
150 		    ep[j] = ' ';
151 	    }
152 	    ep[j] = '\0';
153 	    _tracef("%*s[%2d]%*s='%s'", (int) strlen(name),
154 		    "colors", n, 8, " ", my_buffer);
155 	}
156 
157 	for (i = 0; i < 4; ++i) {
158 	    const char *hex = " 123456789ABCDEF";
159 	    attr_t mask = (attr_t) (0xf << ((i + 4) * 4));
160 	    bool haveattrs = FALSE;
161 
162 	    for (j = 0; j < width; ++j)
163 		if (AttrOf(win->_line[n].text[j]) & mask) {
164 		    haveattrs = TRUE;
165 		    break;
166 		}
167 	    if (haveattrs) {
168 		ep = my_buffer;
169 		for (j = 0; j < width; ++j)
170 		    ep[j] = hex[(AttrOf(win->_line[n].text[j]) & mask) >>
171 				((i + 4) * 4)];
172 		ep[j] = '\0';
173 		_tracef("%*s%d[%2d]%*s='%s'", (int) strlen(name) -
174 			1, "attrs", i, n, 8, " ", my_buffer);
175 	    }
176 	}
177     }
178 #if NO_LEAKS
179     free(my_buffer);
180     my_buffer = 0;
181     my_length = 0;
182 #endif
183 }
184 
185 #else
186 EMPTY_MODULE(_nc_lib_tracedmp)
187 #endif /* TRACE */
188