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