1 /****************************************************************************
2  * Copyright (c) 1998-2008,2009 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.32 2009/04/18 21:01:38 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     }
74 
75     for (n = 0; n <= win->_maxy; ++n) {
76 	char *ep = my_buffer;
77 	bool haveattrs, havecolors;
78 
79 	/*
80 	 * Dump A_CHARTEXT part.  It is more important to make the grid line up
81 	 * in the trace file than to represent control- and wide-characters, so
82 	 * we map those to '.' and '?' respectively.
83 	 */
84 	for (j = 0; j < width; ++j) {
85 	    chtype test = (chtype) CharOf(win->_line[n].text[j]);
86 	    ep[j] = (char) ((UChar(test) == test
87 #if USE_WIDEC_SUPPORT
88 			     && (win->_line[n].text[j].chars[1] == 0)
89 #endif
90 			    )
91 			    ? (iscntrl(UChar(test))
92 			       ? '.'
93 			       : UChar(test))
94 			    : '?');
95 	}
96 	ep[j] = '\0';
97 	_tracef("%s[%2d] %3ld%3ld ='%s'",
98 		name, n,
99 		(long) win->_line[n].firstchar,
100 		(long) win->_line[n].lastchar,
101 		ep);
102 
103 	/* if there are multi-column characters on the line, print them now */
104 	if_WIDEC({
105 	    bool multicolumn = FALSE;
106 	    for (j = 0; j < width; ++j)
107 		if (WidecExt(win->_line[n].text[j]) != 0) {
108 		    multicolumn = TRUE;
109 		    break;
110 		}
111 	    if (multicolumn) {
112 		ep = my_buffer;
113 		for (j = 0; j < width; ++j) {
114 		    chtype test = WidecExt(win->_line[n].text[j]);
115 		    if (test) {
116 			ep[j] = (char) (test + '0');
117 		    } else {
118 			ep[j] = ' ';
119 		    }
120 		}
121 		ep[j] = '\0';
122 		_tracef("%*s[%2d]%*s='%s'", (int) strlen(name),
123 			"widec", n, 8, " ", my_buffer);
124 	    }
125 	});
126 
127 	/* dump A_COLOR part, will screw up if there are more than 96 */
128 	havecolors = FALSE;
129 	for (j = 0; j < width; ++j)
130 	    if (GetPair(win->_line[n].text[j]) != 0) {
131 		havecolors = TRUE;
132 		break;
133 	    }
134 	if (havecolors) {
135 	    ep = my_buffer;
136 	    for (j = 0; j < width; ++j) {
137 		int pair = GetPair(win->_line[n].text[j]);
138 		if (pair >= 52)
139 		    ep[j] = '?';
140 		else if (pair >= 36)
141 		    ep[j] = (char) (pair + 'A');
142 		else if (pair >= 10)
143 		    ep[j] = (char) (pair + 'a');
144 		else if (pair >= 1)
145 		    ep[j] = (char) (pair + '0');
146 		else
147 		    ep[j] = ' ';
148 	    }
149 	    ep[j] = '\0';
150 	    _tracef("%*s[%2d]%*s='%s'", (int) strlen(name),
151 		    "colors", n, 8, " ", my_buffer);
152 	}
153 
154 	for (i = 0; i < 4; ++i) {
155 	    const char *hex = " 123456789ABCDEF";
156 	    attr_t mask = (attr_t) (0xf << ((i + 4) * 4));
157 
158 	    haveattrs = FALSE;
159 	    for (j = 0; j < width; ++j)
160 		if (AttrOf(win->_line[n].text[j]) & mask) {
161 		    haveattrs = TRUE;
162 		    break;
163 		}
164 	    if (haveattrs) {
165 		ep = my_buffer;
166 		for (j = 0; j < width; ++j)
167 		    ep[j] = hex[(AttrOf(win->_line[n].text[j]) & mask) >>
168 				((i + 4) * 4)];
169 		ep[j] = '\0';
170 		_tracef("%*s%d[%2d]%*s='%s'", (int) strlen(name) -
171 			1, "attrs", i, n, 8, " ", my_buffer);
172 	    }
173 	}
174     }
175 #if NO_LEAKS
176     free(my_buffer);
177     my_buffer = 0;
178     my_length = 0;
179 #endif
180 }
181 
182 #else
183 EMPTY_MODULE(_nc_lib_tracedmp)
184 #endif /* TRACE */
185