150276Speter/****************************************************************************
2184989Srafan * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc.              *
350276Speter *                                                                          *
450276Speter * Permission is hereby granted, free of charge, to any person obtaining a  *
550276Speter * copy of this software and associated documentation files (the            *
650276Speter * "Software"), to deal in the Software without restriction, including      *
750276Speter * without limitation the rights to use, copy, modify, merge, publish,      *
850276Speter * distribute, distribute with modifications, sublicense, and/or sell       *
950276Speter * copies of the Software, and to permit persons to whom the Software is    *
1050276Speter * furnished to do so, subject to the following conditions:                 *
1150276Speter *                                                                          *
1250276Speter * The above copyright notice and this permission notice shall be included  *
1350276Speter * in all copies or substantial portions of the Software.                   *
1450276Speter *                                                                          *
1550276Speter * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
1650276Speter * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
1750276Speter * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
1850276Speter * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
1950276Speter * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
2050276Speter * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
2150276Speter * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
2250276Speter *                                                                          *
2350276Speter * Except as contained in this notice, the name(s) of the above copyright   *
2450276Speter * holders shall not be used in advertising or otherwise to promote the     *
2550276Speter * sale, use or other dealings in this Software without prior written       *
2650276Speter * authorization.                                                           *
2750276Speter ****************************************************************************/
2850276Speter
2950276Speter/****************************************************************************
30166124Srafan *  Author: Thomas E. Dickey 1996-on                                        *
3197049Speter *     and: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
3250276Speter *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
3350276Speter ****************************************************************************/
3450276Speter
3550276Speter/*
3650276Speter *	lib_tracedmp.c - Tracing/Debugging routines
3750276Speter */
3850276Speter
3950276Speter#include <curses.priv.h>
4097049Speter#include <ctype.h>
4150276Speter
42184989SrafanMODULE_ID("$Id: lib_tracedmp.c,v 1.31 2008/08/16 19:30:56 tom Exp $")
4350276Speter
4450276Speter#ifdef TRACE
45174993Srafan
46174993Srafan#define my_buffer _nc_globals.tracedmp_buf
47174993Srafan#define my_length _nc_globals.tracedmp_used
48174993Srafan
4976726SpeterNCURSES_EXPORT(void)
5076726Speter_tracedump(const char *name, WINDOW *win)
5150276Speter{
5276726Speter    int i, j, n, width;
5350276Speter
5450276Speter    /* compute narrowest possible display width */
5597049Speter    for (width = i = 0; i <= win->_maxy; ++i) {
5650276Speter	n = 0;
57166124Srafan	for (j = 0; j <= win->_maxx; ++j) {
58166124Srafan	    if (CharOf(win->_line[i].text[j]) != L(' ')
59166124Srafan		|| AttrOf(win->_line[i].text[j]) != A_NORMAL
60166124Srafan		|| GetPair(win->_line[i].text[j]) != 0) {
6176726Speter		n = j;
62166124Srafan	    }
63166124Srafan	}
6450276Speter
6550276Speter	if (n > width)
6676726Speter	    width = n;
6750276Speter    }
6850276Speter    if (width < win->_maxx)
6976726Speter	++width;
70174993Srafan    if (++width + 1 > (int) my_length) {
71174993Srafan	my_length = 2 * (width + 1);
72174993Srafan	my_buffer = typeRealloc(char, my_length, my_buffer);
7397049Speter    }
7450276Speter
7597049Speter    for (n = 0; n <= win->_maxy; ++n) {
76174993Srafan	char *ep = my_buffer;
7750276Speter	bool haveattrs, havecolors;
7850276Speter
7997049Speter	/*
8097049Speter	 * Dump A_CHARTEXT part.  It is more important to make the grid line up
8197049Speter	 * in the trace file than to represent control- and wide-characters, so
8297049Speter	 * we map those to '.' and '?' respectively.
8397049Speter	 */
8497049Speter	for (j = 0; j < width; ++j) {
8597049Speter	    chtype test = CharOf(win->_line[n].text[j]);
86184989Srafan	    ep[j] = (char) ((UChar(test) == test
8797049Speter#if USE_WIDEC_SUPPORT
88184989Srafan			     && (win->_line[n].text[j].chars[1] == 0)
8997049Speter#endif
90184989Srafan			    )
91184989Srafan			    ? (iscntrl(UChar(test))
92184989Srafan			       ? '.'
93184989Srafan			       : UChar(test))
94184989Srafan			    : '?');
9550276Speter	}
9697049Speter	ep[j] = '\0';
97166124Srafan	_tracef("%s[%2d] %3ld%3ld ='%s'",
9897049Speter		name, n,
99166124Srafan		(long) win->_line[n].firstchar,
100166124Srafan		(long) win->_line[n].lastchar,
10197049Speter		ep);
10250276Speter
103174993Srafan	/* if there are multi-column characters on the line, print them now */
104174993Srafan	if_WIDEC({
105174993Srafan	    bool multicolumn = FALSE;
106174993Srafan	    for (j = 0; j < width; ++j)
107174993Srafan		if (WidecExt(win->_line[n].text[j]) != 0) {
108174993Srafan		    multicolumn = TRUE;
109174993Srafan		    break;
110174993Srafan		}
111174993Srafan	    if (multicolumn) {
112174993Srafan		ep = my_buffer;
113174993Srafan		for (j = 0; j < width; ++j) {
114174993Srafan		    int test = WidecExt(win->_line[n].text[j]);
115174993Srafan		    if (test) {
116184989Srafan			ep[j] = (char) (test + '0');
117174993Srafan		    } else {
118174993Srafan			ep[j] = ' ';
119174993Srafan		    }
120174993Srafan		}
121174993Srafan		ep[j] = '\0';
122174993Srafan		_tracef("%*s[%2d]%*s='%s'", (int) strlen(name),
123174993Srafan			"widec", n, 8, " ", my_buffer);
124174993Srafan	    }
125174993Srafan	});
126174993Srafan
12750276Speter	/* dump A_COLOR part, will screw up if there are more than 96 */
12850276Speter	havecolors = FALSE;
12997049Speter	for (j = 0; j < width; ++j)
130166124Srafan	    if (GetPair(win->_line[n].text[j]) != 0) {
13150276Speter		havecolors = TRUE;
13250276Speter		break;
13350276Speter	    }
13476726Speter	if (havecolors) {
135174993Srafan	    ep = my_buffer;
136166124Srafan	    for (j = 0; j < width; ++j) {
137166124Srafan		int pair = GetPair(win->_line[n].text[j]);
138166124Srafan		if (pair >= 52)
139166124Srafan		    ep[j] = '?';
140166124Srafan		else if (pair >= 36)
141184989Srafan		    ep[j] = (char) (pair + 'A');
142166124Srafan		else if (pair >= 10)
143184989Srafan		    ep[j] = (char) (pair + 'a');
144166124Srafan		else if (pair >= 1)
145184989Srafan		    ep[j] = (char) (pair + '0');
146166124Srafan		else
147166124Srafan		    ep[j] = ' ';
148166124Srafan	    }
14997049Speter	    ep[j] = '\0';
15097049Speter	    _tracef("%*s[%2d]%*s='%s'", (int) strlen(name),
151174993Srafan		    "colors", n, 8, " ", my_buffer);
15250276Speter	}
15350276Speter
15497049Speter	for (i = 0; i < 4; ++i) {
15576726Speter	    const char *hex = " 123456789ABCDEF";
15697049Speter	    attr_t mask = (0xf << ((i + 4) * 4));
15750276Speter
15850276Speter	    haveattrs = FALSE;
15997049Speter	    for (j = 0; j < width; ++j)
16097049Speter		if (AttrOf(win->_line[n].text[j]) & mask) {
16150276Speter		    haveattrs = TRUE;
16250276Speter		    break;
16350276Speter		}
16476726Speter	    if (haveattrs) {
165174993Srafan		ep = my_buffer;
16697049Speter		for (j = 0; j < width; ++j)
16797049Speter		    ep[j] = hex[(AttrOf(win->_line[n].text[j]) & mask) >>
16897049Speter				((i + 4) * 4)];
16997049Speter		ep[j] = '\0';
17097049Speter		_tracef("%*s%d[%2d]%*s='%s'", (int) strlen(name) -
171174993Srafan			1, "attrs", i, n, 8, " ", my_buffer);
17250276Speter	    }
17350276Speter	}
17450276Speter    }
17597049Speter#if NO_LEAKS
176174993Srafan    free(my_buffer);
177174993Srafan    my_buffer = 0;
178174993Srafan    my_length = 0;
17997049Speter#endif
18050276Speter}
18197049Speter
18250276Speter#else
183184989SrafanEMPTY_MODULE(_nc_lib_tracedmp)
18450276Speter#endif /* TRACE */
185