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