]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/trace/lib_tracedmp.c
ncurses 5.3
[ncurses.git] / ncurses / trace / lib_tracedmp.c
1 /****************************************************************************
2  * Copyright (c) 1998-2001,2002 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-2001                                      *
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.25 2002/09/22 22:21:38 tom Exp $")
43
44 #ifdef TRACE
45 NCURSES_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                 n = j;
60
61         if (n > width)
62             width = n;
63     }
64     if (width < win->_maxx)
65         ++width;
66     if (++width + 1 > (int) used) {
67         used = 2 * (width + 1);
68         buf = typeRealloc(char, used, buf);
69     }
70
71     for (n = 0; n <= win->_maxy; ++n) {
72         char *ep = buf;
73         bool haveattrs, havecolors;
74
75         /*
76          * Dump A_CHARTEXT part.  It is more important to make the grid line up
77          * in the trace file than to represent control- and wide-characters, so
78          * we map those to '.' and '?' respectively.
79          */
80         for (j = 0; j < width; ++j) {
81             chtype test = CharOf(win->_line[n].text[j]);
82             ep[j] = (UChar(test) == test
83 #if USE_WIDEC_SUPPORT
84                      && (win->_line[n].text[j].chars[1] == 0)
85 #endif
86                 )
87                 ? (iscntrl(UChar(test))
88                    ? '.'
89                    : UChar(test))
90                 : '?';
91         }
92         ep[j] = '\0';
93         _tracef("%s[%2d] %3d%3d ='%s'",
94                 name, n,
95                 win->_line[n].firstchar,
96                 win->_line[n].lastchar,
97                 ep);
98
99         /* dump A_COLOR part, will screw up if there are more than 96 */
100         havecolors = FALSE;
101         for (j = 0; j < width; ++j)
102             if (AttrOf(win->_line[n].text[j]) & A_COLOR) {
103                 havecolors = TRUE;
104                 break;
105             }
106         if (havecolors) {
107             ep = buf;
108             for (j = 0; j < width; ++j)
109                 ep[j] = UChar(AttrOf(win->_line[n].text[j]) >>
110                               NCURSES_ATTR_SHIFT) + ' ';
111             ep[j] = '\0';
112             _tracef("%*s[%2d]%*s='%s'", (int) strlen(name),
113                     "colors", n, 8, " ", buf);
114         }
115
116         for (i = 0; i < 4; ++i) {
117             const char *hex = " 123456789ABCDEF";
118             attr_t mask = (0xf << ((i + 4) * 4));
119
120             haveattrs = FALSE;
121             for (j = 0; j < width; ++j)
122                 if (AttrOf(win->_line[n].text[j]) & mask) {
123                     haveattrs = TRUE;
124                     break;
125                 }
126             if (haveattrs) {
127                 ep = buf;
128                 for (j = 0; j < width; ++j)
129                     ep[j] = hex[(AttrOf(win->_line[n].text[j]) & mask) >>
130                                 ((i + 4) * 4)];
131                 ep[j] = '\0';
132                 _tracef("%*s%d[%2d]%*s='%s'", (int) strlen(name) -
133                         1, "attrs", i, n, 8, " ", buf);
134             }
135         }
136     }
137 #if NO_LEAKS
138     free(buf);
139     buf = 0;
140     used = 0;
141 #endif
142 }
143
144 #else
145 empty_module(_nc_lib_tracedmp)
146 #endif /* TRACE */