168349Sobrien/****************************************************************************
2133359Sobrien * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc.              *
3133359Sobrien *                                                                          *
4133359Sobrien * Permission is hereby granted, free of charge, to any person obtaining a  *
5133359Sobrien * copy of this software and associated documentation files (the            *
6133359Sobrien * "Software"), to deal in the Software without restriction, including      *
7133359Sobrien * without limitation the rights to use, copy, modify, merge, publish,      *
8133359Sobrien * distribute, distribute with modifications, sublicense, and/or sell       *
9133359Sobrien * copies of the Software, and to permit persons to whom the Software is    *
10133359Sobrien * furnished to do so, subject to the following conditions:                 *
11133359Sobrien *                                                                          *
12133359Sobrien * The above copyright notice and this permission notice shall be included  *
13133359Sobrien * in all copies or substantial portions of the Software.                   *
14133359Sobrien *                                                                          *
15133359Sobrien * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16133359Sobrien * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17133359Sobrien * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18133359Sobrien * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19133359Sobrien * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20133359Sobrien * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21133359Sobrien * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22133359Sobrien *                                                                          *
23133359Sobrien * Except as contained in this notice, the name(s) of the above copyright   *
24133359Sobrien * holders shall not be used in advertising or otherwise to promote the     *
25133359Sobrien * sale, use or other dealings in this Software without prior written       *
26133359Sobrien * authorization.                                                           *
27133359Sobrien ****************************************************************************/
28133359Sobrien
2968349Sobrien/****************************************************************************
3068349Sobrien *  Author: Thomas E. Dickey 1996-on                                        *
3168349Sobrien *     and: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
3268349Sobrien *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
3368349Sobrien ****************************************************************************/
3468349Sobrien
3568349Sobrien/*
36191736Sobrien *	lib_tracedmp.c - Tracing/Debugging routines
37191736Sobrien */
38226048Sobrien
39191736Sobrien#include <curses.priv.h>
40191736Sobrien#include <ctype.h>
41133359Sobrien
4268349SobrienMODULE_ID("$Id: lib_tracedmp.c,v 1.31 2008/08/16 19:30:56 tom Exp $")
4368349Sobrien
4468349Sobrien#ifdef TRACE
4568349Sobrien
4668349Sobrien#define my_buffer _nc_globals.tracedmp_buf
47133359Sobrien#define my_length _nc_globals.tracedmp_used
48226048Sobrien
49169942SobrienNCURSES_EXPORT(void)
50226048Sobrien_tracedump(const char *name, WINDOW *win)
5168349Sobrien{
5268349Sobrien    int i, j, n, width;
5368349Sobrien
54169962Sobrien    /* compute narrowest possible display width */
55169962Sobrien    for (width = i = 0; i <= win->_maxy; ++i) {
56169962Sobrien	n = 0;
57175296Sobrien	for (j = 0; j <= win->_maxx; ++j) {
58175296Sobrien	    if (CharOf(win->_line[i].text[j]) != L(' ')
59103373Sobrien		|| AttrOf(win->_line[i].text[j]) != A_NORMAL
60103373Sobrien		|| GetPair(win->_line[i].text[j]) != 0) {
61103373Sobrien		n = j;
62186690Sobrien	    }
63186690Sobrien	}
64133359Sobrien
65186690Sobrien	if (n > width)
66133359Sobrien	    width = n;
6768349Sobrien    }
6868349Sobrien    if (width < win->_maxx)
6975937Sobrien	++width;
7075937Sobrien    if (++width + 1 > (int) my_length) {
7175937Sobrien	my_length = 2 * (width + 1);
7268349Sobrien	my_buffer = typeRealloc(char, my_length, my_buffer);
7368349Sobrien    }
7468349Sobrien
7568349Sobrien    for (n = 0; n <= win->_maxy; ++n) {
7668349Sobrien	char *ep = my_buffer;
77159764Sobrien	bool haveattrs, havecolors;
78159764Sobrien
7980588Sobrien	/*
80191736Sobrien	 * Dump A_CHARTEXT part.  It is more important to make the grid line up
81192348Sdelphij	 * in the trace file than to represent control- and wide-characters, so
82226048Sobrien	 * we map those to '.' and '?' respectively.
8368349Sobrien	 */
8468349Sobrien	for (j = 0; j < width; ++j) {
85169942Sobrien	    chtype test = CharOf(win->_line[n].text[j]);
8668349Sobrien	    ep[j] = (char) ((UChar(test) == test
87133359Sobrien#if USE_WIDEC_SUPPORT
88226048Sobrien			     && (win->_line[n].text[j].chars[1] == 0)
89226048Sobrien#endif
90159764Sobrien			    )
91159764Sobrien			    ? (iscntrl(UChar(test))
92175296Sobrien			       ? '.'
93133359Sobrien			       : UChar(test))
94133359Sobrien			    : '?');
95103373Sobrien	}
9668349Sobrien	ep[j] = '\0';
97133359Sobrien	_tracef("%s[%2d] %3ld%3ld ='%s'",
98169962Sobrien		name, n,
99169962Sobrien		(long) win->_line[n].firstchar,
10068349Sobrien		(long) win->_line[n].lastchar,
101133359Sobrien		ep);
102133359Sobrien
103133359Sobrien	/* if there are multi-column characters on the line, print them now */
104175296Sobrien	if_WIDEC({
10568349Sobrien	    bool multicolumn = FALSE;
106133359Sobrien	    for (j = 0; j < width; ++j)
107133359Sobrien		if (WidecExt(win->_line[n].text[j]) != 0) {
108133359Sobrien		    multicolumn = TRUE;
10968349Sobrien		    break;
11068349Sobrien		}
11168349Sobrien	    if (multicolumn) {
11268349Sobrien		ep = my_buffer;
113159764Sobrien		for (j = 0; j < width; ++j) {
114169942Sobrien		    int test = WidecExt(win->_line[n].text[j]);
115133359Sobrien		    if (test) {
116133359Sobrien			ep[j] = (char) (test + '0');
117169962Sobrien		    } else {
118133359Sobrien			ep[j] = ' ';
119175296Sobrien		    }
120175296Sobrien		}
121175296Sobrien		ep[j] = '\0';
122175296Sobrien		_tracef("%*s[%2d]%*s='%s'", (int) strlen(name),
123175296Sobrien			"widec", n, 8, " ", my_buffer);
124175296Sobrien	    }
125175296Sobrien	});
126175296Sobrien
127175296Sobrien	/* dump A_COLOR part, will screw up if there are more than 96 */
128133359Sobrien	havecolors = FALSE;
129175296Sobrien	for (j = 0; j < width; ++j)
130175296Sobrien	    if (GetPair(win->_line[n].text[j]) != 0) {
131133359Sobrien		havecolors = TRUE;
132133359Sobrien		break;
133133359Sobrien	    }
13468349Sobrien	if (havecolors) {
13568349Sobrien	    ep = my_buffer;
136133359Sobrien	    for (j = 0; j < width; ++j) {
137133359Sobrien		int pair = GetPair(win->_line[n].text[j]);
138133359Sobrien		if (pair >= 52)
139133359Sobrien		    ep[j] = '?';
140133359Sobrien		else if (pair >= 36)
14168349Sobrien		    ep[j] = (char) (pair + 'A');
142226048Sobrien		else if (pair >= 10)
14375937Sobrien		    ep[j] = (char) (pair + 'a');
14475937Sobrien		else if (pair >= 1)
14575937Sobrien		    ep[j] = (char) (pair + '0');
146133359Sobrien		else
147103373Sobrien		    ep[j] = ' ';
14875937Sobrien	    }
149226048Sobrien	    ep[j] = '\0';
15075937Sobrien	    _tracef("%*s[%2d]%*s='%s'", (int) strlen(name),
15168349Sobrien		    "colors", n, 8, " ", my_buffer);
15275937Sobrien	}
15375937Sobrien
15475937Sobrien	for (i = 0; i < 4; ++i) {
15575937Sobrien	    const char *hex = " 123456789ABCDEF";
15675937Sobrien	    attr_t mask = (0xf << ((i + 4) * 4));
15775937Sobrien
15875937Sobrien	    haveattrs = FALSE;
15975937Sobrien	    for (j = 0; j < width; ++j)
160226048Sobrien		if (AttrOf(win->_line[n].text[j]) & mask) {
16175937Sobrien		    haveattrs = TRUE;
16275937Sobrien		    break;
16375937Sobrien		}
16475937Sobrien	    if (haveattrs) {
16575937Sobrien		ep = my_buffer;
16675937Sobrien		for (j = 0; j < width; ++j)
16775937Sobrien		    ep[j] = hex[(AttrOf(win->_line[n].text[j]) & mask) >>
16875937Sobrien				((i + 4) * 4)];
16975937Sobrien		ep[j] = '\0';
17075937Sobrien		_tracef("%*s%d[%2d]%*s='%s'", (int) strlen(name) -
171169942Sobrien			1, "attrs", i, n, 8, " ", my_buffer);
172226048Sobrien	    }
17375937Sobrien	}
174226048Sobrien    }
175226048Sobrien#if NO_LEAKS
176226048Sobrien    free(my_buffer);
177226048Sobrien    my_buffer = 0;
178169942Sobrien    my_length = 0;
179169942Sobrien#endif
180169942Sobrien}
18175937Sobrien
18275937Sobrien#else
183169942SobrienEMPTY_MODULE(_nc_lib_tracedmp)
184169942Sobrien#endif /* TRACE */
185169942Sobrien