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