]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/trace/visbuf.c
ncurses 5.7 - patch 20090927
[ncurses.git] / ncurses / trace / visbuf.c
1 /****************************************************************************
2  * Copyright (c) 2001-2008,2009 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  *      visbuf.c - Tracing/Debugging support routines
37  */
38
39 #define NEED_NCURSES_CH_T
40 #include <curses.priv.h>
41
42 #include <tic.h>
43 #include <ctype.h>
44
45 MODULE_ID("$Id: visbuf.c,v 1.33 2009/07/11 14:44:20 tom Exp $")
46
47 #define NUM_VISBUFS 4
48
49 #define NormalLen(len) (size_t) (((size_t)(len) + 1) * 4)
50 #define WideLen(len)   (size_t) (((size_t)(len) + 1) * 4 * MB_CUR_MAX)
51
52 #ifdef TRACE
53 static const char d_quote[] = StringOf(D_QUOTE);
54 static const char l_brace[] = StringOf(L_BRACE);
55 static const char r_brace[] = StringOf(R_BRACE);
56 #endif
57
58 static char *
59 _nc_vischar(char *tp, unsigned c)
60 {
61     if (c == '"' || c == '\\') {
62         *tp++ = '\\';
63         *tp++ = (char) c;
64     } else if (is7bits(c) && (isgraph(c) || c == ' ')) {
65         *tp++ = (char) c;
66     } else if (c == '\n') {
67         *tp++ = '\\';
68         *tp++ = 'n';
69     } else if (c == '\r') {
70         *tp++ = '\\';
71         *tp++ = 'r';
72     } else if (c == '\b') {
73         *tp++ = '\\';
74         *tp++ = 'b';
75     } else if (c == '\033') {
76         *tp++ = '\\';
77         *tp++ = 'e';
78     } else if (UChar(c) == 0x7f) {
79         *tp++ = '\\';
80         *tp++ = '^';
81         *tp++ = '?';
82     } else if (is7bits(c) && iscntrl(UChar(c))) {
83         *tp++ = '\\';
84         *tp++ = '^';
85         *tp++ = (char) ('@' + c);
86     } else {
87         sprintf(tp, "\\%03lo", (unsigned long) ChCharOf(c));
88         tp += strlen(tp);
89     }
90     *tp = 0;
91     return tp;
92 }
93
94 static const char *
95 _nc_visbuf2n(int bufnum, const char *buf, int len)
96 {
97     const char *vbuf;
98     char *tp;
99     int c;
100
101     if (buf == 0)
102         return ("(null)");
103     if (buf == CANCELLED_STRING)
104         return ("(cancelled)");
105
106     if (len < 0)
107         len = (int) strlen(buf);
108
109 #ifdef TRACE
110     vbuf = tp = _nc_trace_buf(bufnum, NormalLen(len));
111 #else
112     {
113         static char *mybuf[NUM_VISBUFS];
114         if (bufnum < 0) {
115             for (c = 0; c < NUM_VISBUFS; ++c) {
116                 FreeAndNull(mybuf[c]);
117             }
118             tp = 0;
119         } else {
120             mybuf[bufnum] = typeRealloc(char, NormalLen(len), mybuf[bufnum]);
121             vbuf = tp = mybuf[bufnum];
122         }
123     }
124 #endif
125     if (tp != 0) {
126         *tp++ = D_QUOTE;
127         while ((--len >= 0) && (c = *buf++) != '\0') {
128             tp = _nc_vischar(tp, UChar(c));
129         }
130         *tp++ = D_QUOTE;
131         *tp++ = '\0';
132     } else {
133         vbuf = ("(_nc_visbuf2n failed)");
134     }
135     return (vbuf);
136 }
137
138 NCURSES_EXPORT(const char *)
139 _nc_visbuf2(int bufnum, const char *buf)
140 {
141     return _nc_visbuf2n(bufnum, buf, -1);
142 }
143
144 NCURSES_EXPORT(const char *)
145 _nc_visbuf(const char *buf)
146 {
147     return _nc_visbuf2(0, buf);
148 }
149
150 NCURSES_EXPORT(const char *)
151 _nc_visbufn(const char *buf, int len)
152 {
153     return _nc_visbuf2n(0, buf, len);
154 }
155
156 #ifdef TRACE
157 #if USE_WIDEC_SUPPORT
158
159 #if defined(USE_TERMLIB)
160 #define _nc_wchstrlen _my_wchstrlen
161 static int
162 _nc_wchstrlen(const cchar_t *s)
163 {
164     int result = 0;
165     while (CharOf(s[result]) != L'\0') {
166         result++;
167     }
168     return result;
169 }
170 #endif
171
172 static const char *
173 _nc_viswbuf2n(int bufnum, const wchar_t *buf, int len)
174 {
175     const char *vbuf;
176     char *tp;
177     wchar_t c;
178
179     if (buf == 0)
180         return ("(null)");
181
182     if (len < 0)
183         len = (int) wcslen(buf);
184
185 #ifdef TRACE
186     vbuf = tp = _nc_trace_buf(bufnum, WideLen(len));
187 #else
188     {
189         static char *mybuf[NUM_VISBUFS];
190         mybuf[bufnum] = typeRealloc(char, WideLen(len), mybuf[bufnum]);
191         vbuf = tp = mybuf[bufnum];
192     }
193 #endif
194     if (tp != 0) {
195         *tp++ = D_QUOTE;
196         while ((--len >= 0) && (c = *buf++) != '\0') {
197             char temp[CCHARW_MAX + 80];
198             int j = wctomb(temp, c), k;
199             if (j <= 0) {
200                 sprintf(temp, "\\u%08X", (unsigned) c);
201                 j = (int) strlen(temp);
202             }
203             for (k = 0; k < j; ++k) {
204                 tp = _nc_vischar(tp, UChar(temp[k]));
205             }
206         }
207         *tp++ = D_QUOTE;
208         *tp++ = '\0';
209     } else {
210         vbuf = ("(_nc_viswbuf2n failed)");
211     }
212     return (vbuf);
213 }
214
215 NCURSES_EXPORT(const char *)
216 _nc_viswbuf2(int bufnum, const wchar_t *buf)
217 {
218     return _nc_viswbuf2n(bufnum, buf, -1);
219 }
220
221 NCURSES_EXPORT(const char *)
222 _nc_viswbuf(const wchar_t *buf)
223 {
224     return _nc_viswbuf2(0, buf);
225 }
226
227 NCURSES_EXPORT(const char *)
228 _nc_viswbufn(const wchar_t *buf, int len)
229 {
230     return _nc_viswbuf2n(0, buf, len);
231 }
232
233 /* this special case is used for wget_wstr() */
234 NCURSES_EXPORT(const char *)
235 _nc_viswibuf(const wint_t *buf)
236 {
237     static wchar_t *mybuf;
238     static unsigned mylen;
239     unsigned n;
240
241     for (n = 0; buf[n] != 0; ++n) ;
242     if (mylen < ++n) {
243         mylen = n + 80;
244         if (mybuf != 0)
245             mybuf = typeRealloc(wchar_t, mylen, mybuf);
246         else
247             mybuf = typeMalloc(wchar_t, mylen);
248     }
249     for (n = 0; buf[n] != 0; ++n)
250         mybuf[n] = (wchar_t) buf[n];
251
252     return _nc_viswbuf2(0, mybuf);
253 }
254 #endif /* USE_WIDEC_SUPPORT */
255
256 /* use these functions for displaying parts of a line within a window */
257 NCURSES_EXPORT(const char *)
258 _nc_viscbuf2(int bufnum, const NCURSES_CH_T * buf, int len)
259 {
260     char *result = _nc_trace_buf(bufnum, BUFSIZ);
261     int first;
262     const char *found;
263
264     if (result != 0) {
265 #if USE_WIDEC_SUPPORT
266         if (len < 0)
267             len = _nc_wchstrlen(buf);
268 #endif /* USE_WIDEC_SUPPORT */
269
270         /*
271          * Display one or more strings followed by attributes.
272          */
273         first = 0;
274         while (first < len) {
275             attr_t attr = AttrOf(buf[first]);
276             int last = len - 1;
277             int j;
278
279             for (j = first + 1; j < len; ++j) {
280                 if (!SameAttrOf(buf[j], buf[first])) {
281                     last = j - 1;
282                     break;
283                 }
284             }
285
286             result = _nc_trace_bufcat(bufnum, l_brace);
287             result = _nc_trace_bufcat(bufnum, d_quote);
288             for (j = first; j <= last; ++j) {
289                 found = _nc_altcharset_name(attr, (chtype) CharOf(buf[j]));
290                 if (found != 0) {
291                     result = _nc_trace_bufcat(bufnum, found);
292                     attr &= ~A_ALTCHARSET;
293                 } else
294 #if USE_WIDEC_SUPPORT
295                 if (!isWidecExt(buf[j])) {
296                     PUTC_DATA;
297
298                     PUTC_INIT;
299                     for (PUTC_i = 0; PUTC_i < CCHARW_MAX; ++PUTC_i) {
300                         int k;
301
302                         PUTC_ch = buf[j].chars[PUTC_i];
303                         if (PUTC_ch == L'\0')
304                             break;
305                         PUTC_n = (int) wcrtomb(PUTC_buf,
306                                                buf[j].chars[PUTC_i], &PUT_st);
307                         if (PUTC_n <= 0)
308                             break;
309                         for (k = 0; k < PUTC_n; k++) {
310                             char temp[80];
311                             _nc_vischar(temp, UChar(PUTC_buf[k]));
312                             result = _nc_trace_bufcat(bufnum, temp);
313                         }
314                     }
315                 }
316 #else
317                 {
318                     char temp[80];
319                     _nc_vischar(temp, UChar(buf[j]));
320                     result = _nc_trace_bufcat(bufnum, temp);
321                 }
322 #endif /* USE_WIDEC_SUPPORT */
323             }
324             result = _nc_trace_bufcat(bufnum, d_quote);
325             if (attr != A_NORMAL) {
326                 result = _nc_trace_bufcat(bufnum, " | ");
327                 result = _nc_trace_bufcat(bufnum, _traceattr2(bufnum + 20, attr));
328             }
329             result = _nc_trace_bufcat(bufnum, r_brace);
330             first = last + 1;
331         }
332     }
333     return result;
334 }
335
336 NCURSES_EXPORT(const char *)
337 _nc_viscbuf(const NCURSES_CH_T * buf, int len)
338 {
339     return _nc_viscbuf2(0, buf, len);
340 }
341 #endif /* TRACE */