]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/trace/lib_tracedmp.c
ncurses 5.6 - patch 20070609
[ncurses.git] / ncurses / trace / lib_tracedmp.c
1 /****************************************************************************
2  * Copyright (c) 1998-2006,2007 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.28 2007/04/21 21:10:54 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 = 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 = CharOf(win->_line[n].text[j]);
86             ep[j] = (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         /* dump A_COLOR part, will screw up if there are more than 96 */
104         havecolors = FALSE;
105         for (j = 0; j < width; ++j)
106             if (GetPair(win->_line[n].text[j]) != 0) {
107                 havecolors = TRUE;
108                 break;
109             }
110         if (havecolors) {
111             ep = my_buffer;
112             for (j = 0; j < width; ++j) {
113                 int pair = GetPair(win->_line[n].text[j]);
114                 if (pair >= 52)
115                     ep[j] = '?';
116                 else if (pair >= 36)
117                     ep[j] = pair + 'A';
118                 else if (pair >= 10)
119                     ep[j] = pair + 'a';
120                 else if (pair >= 1)
121                     ep[j] = pair + '0';
122                 else
123                     ep[j] = ' ';
124             }
125             ep[j] = '\0';
126             _tracef("%*s[%2d]%*s='%s'", (int) strlen(name),
127                     "colors", n, 8, " ", my_buffer);
128         }
129
130         for (i = 0; i < 4; ++i) {
131             const char *hex = " 123456789ABCDEF";
132             attr_t mask = (0xf << ((i + 4) * 4));
133
134             haveattrs = FALSE;
135             for (j = 0; j < width; ++j)
136                 if (AttrOf(win->_line[n].text[j]) & mask) {
137                     haveattrs = TRUE;
138                     break;
139                 }
140             if (haveattrs) {
141                 ep = my_buffer;
142                 for (j = 0; j < width; ++j)
143                     ep[j] = hex[(AttrOf(win->_line[n].text[j]) & mask) >>
144                                 ((i + 4) * 4)];
145                 ep[j] = '\0';
146                 _tracef("%*s%d[%2d]%*s='%s'", (int) strlen(name) -
147                         1, "attrs", i, n, 8, " ", my_buffer);
148             }
149         }
150     }
151 #if NO_LEAKS
152     free(my_buffer);
153     my_buffer = 0;
154     my_length = 0;
155 #endif
156 }
157
158 #else
159 empty_module(_nc_lib_tracedmp)
160 #endif /* TRACE */