lib_tracedmp.c revision 166124
1/****************************************************************************
2 * Copyright (c) 1998-2005,2006 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
42MODULE_ID("$Id: lib_tracedmp.c,v 1.27 2006/10/14 20:43:31 tom Exp $")
43
44#ifdef TRACE
45NCURSES_EXPORT(void)
46_tracedump(const char *name, WINDOW *win)
47{
48    static char *buf = 0;
49    static size_t used = 0;
50
51    int i, j, n, width;
52
53    /* compute narrowest possible display width */
54    for (width = i = 0; i <= win->_maxy; ++i) {
55	n = 0;
56	for (j = 0; j <= win->_maxx; ++j) {
57	    if (CharOf(win->_line[i].text[j]) != L(' ')
58		|| AttrOf(win->_line[i].text[j]) != A_NORMAL
59		|| GetPair(win->_line[i].text[j]) != 0) {
60		n = j;
61	    }
62	}
63
64	if (n > width)
65	    width = n;
66    }
67    if (width < win->_maxx)
68	++width;
69    if (++width + 1 > (int) used) {
70	used = 2 * (width + 1);
71	buf = typeRealloc(char, used, buf);
72    }
73
74    for (n = 0; n <= win->_maxy; ++n) {
75	char *ep = buf;
76	bool haveattrs, havecolors;
77
78	/*
79	 * Dump A_CHARTEXT part.  It is more important to make the grid line up
80	 * in the trace file than to represent control- and wide-characters, so
81	 * we map those to '.' and '?' respectively.
82	 */
83	for (j = 0; j < width; ++j) {
84	    chtype test = CharOf(win->_line[n].text[j]);
85	    ep[j] = (UChar(test) == test
86#if USE_WIDEC_SUPPORT
87		     && (win->_line[n].text[j].chars[1] == 0)
88#endif
89		)
90		? (iscntrl(UChar(test))
91		   ? '.'
92		   : UChar(test))
93		: '?';
94	}
95	ep[j] = '\0';
96	_tracef("%s[%2d] %3ld%3ld ='%s'",
97		name, n,
98		(long) win->_line[n].firstchar,
99		(long) win->_line[n].lastchar,
100		ep);
101
102	/* dump A_COLOR part, will screw up if there are more than 96 */
103	havecolors = FALSE;
104	for (j = 0; j < width; ++j)
105	    if (GetPair(win->_line[n].text[j]) != 0) {
106		havecolors = TRUE;
107		break;
108	    }
109	if (havecolors) {
110	    ep = buf;
111	    for (j = 0; j < width; ++j) {
112		int pair = GetPair(win->_line[n].text[j]);
113		if (pair >= 52)
114		    ep[j] = '?';
115		else if (pair >= 36)
116		    ep[j] = pair + 'A';
117		else if (pair >= 10)
118		    ep[j] = pair + 'a';
119		else if (pair >= 1)
120		    ep[j] = pair + '0';
121		else
122		    ep[j] = ' ';
123	    }
124	    ep[j] = '\0';
125	    _tracef("%*s[%2d]%*s='%s'", (int) strlen(name),
126		    "colors", n, 8, " ", buf);
127	}
128
129	for (i = 0; i < 4; ++i) {
130	    const char *hex = " 123456789ABCDEF";
131	    attr_t mask = (0xf << ((i + 4) * 4));
132
133	    haveattrs = FALSE;
134	    for (j = 0; j < width; ++j)
135		if (AttrOf(win->_line[n].text[j]) & mask) {
136		    haveattrs = TRUE;
137		    break;
138		}
139	    if (haveattrs) {
140		ep = buf;
141		for (j = 0; j < width; ++j)
142		    ep[j] = hex[(AttrOf(win->_line[n].text[j]) & mask) >>
143				((i + 4) * 4)];
144		ep[j] = '\0';
145		_tracef("%*s%d[%2d]%*s='%s'", (int) strlen(name) -
146			1, "attrs", i, n, 8, " ", buf);
147	    }
148	}
149    }
150#if NO_LEAKS
151    free(buf);
152    buf = 0;
153    used = 0;
154#endif
155}
156
157#else
158empty_module(_nc_lib_tracedmp)
159#endif /* TRACE */
160