blob: fec06439c35a06700b7af87da6719bff2fed80f2 [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
Steve Kondikae271bc2015-11-15 02:50:53 +01002 * Copyright (c) 2001-2012,2014 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05303 * *
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
Steve Kondikae271bc2015-11-15 02:50:53 +010045MODULE_ID("$Id: visbuf.c,v 1.44 2014/09/25 08:51:13 tom Exp $")
46
47#define NUM_VISBUFS 4
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053048
49#define NormalLen(len) (size_t) (((size_t)(len) + 1) * 4)
Steve Kondikae271bc2015-11-15 02:50:53 +010050#define WideLen(len) (size_t) (((size_t)(len) + 1) * 4 * (size_t) MB_CUR_MAX)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053051
52#ifdef TRACE
53static const char d_quote[] = StringOf(D_QUOTE);
54static const char l_brace[] = StringOf(L_BRACE);
55static const char r_brace[] = StringOf(R_BRACE);
56#endif
57
Steve Kondikae271bc2015-11-15 02:50:53 +010058#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
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053066static char *
Steve Kondikae271bc2015-11-15 02:50:53 +010067_nc_vischar(char *tp, unsigned c LIMIT_ARG)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053068{
69 if (c == '"' || c == '\\') {
70 *tp++ = '\\';
71 *tp++ = (char) c;
Steve Kondikae271bc2015-11-15 02:50:53 +010072 } else if (is7bits((int) c) && (isgraph((int) c) || c == ' ')) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053073 *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 {
Steve Kondikae271bc2015-11-15 02:50:53 +010095 _nc_SPRINTF(tp, _nc_SLIMIT(limit)
96 "\\%03lo", (unsigned long) ChCharOf(c));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053097 tp += strlen(tp);
98 }
99 *tp = 0;
100 return tp;
101}
102
103static const char *
104_nc_visbuf2n(int bufnum, const char *buf, int len)
105{
Steve Kondikae271bc2015-11-15 02:50:53 +0100106 const char *vbuf = 0;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530107 char *tp;
108 int c;
Steve Kondikae271bc2015-11-15 02:50:53 +0100109 int count;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530110
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
Steve Kondikae271bc2015-11-15 02:50:53 +0100119 count = len;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530120#ifdef TRACE
121 vbuf = tp = _nc_trace_buf(bufnum, NormalLen(len));
122#else
123 {
Steve Kondikae271bc2015-11-15 02:50:53 +0100124 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 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530134 }
135#endif
136 if (tp != 0) {
137 *tp++ = D_QUOTE;
Steve Kondikae271bc2015-11-15 02:50:53 +0100138 while ((--count >= 0) && (c = *buf++) != '\0') {
139 tp = VisChar(tp, UChar(c), NormalLen(len));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530140 }
141 *tp++ = D_QUOTE;
Steve Kondikae271bc2015-11-15 02:50:53 +0100142 *tp = '\0';
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530143 } else {
144 vbuf = ("(_nc_visbuf2n failed)");
145 }
146 return (vbuf);
147}
148
149NCURSES_EXPORT(const char *)
150_nc_visbuf2(int bufnum, const char *buf)
151{
152 return _nc_visbuf2n(bufnum, buf, -1);
153}
154
155NCURSES_EXPORT(const char *)
156_nc_visbuf(const char *buf)
157{
158 return _nc_visbuf2(0, buf);
159}
160
161NCURSES_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
172static 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
183static 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;
Steve Kondikae271bc2015-11-15 02:50:53 +0100189 int count;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530190
191 if (buf == 0)
192 return ("(null)");
193
194 if (len < 0)
195 len = (int) wcslen(buf);
196
Steve Kondikae271bc2015-11-15 02:50:53 +0100197 count = len;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530198#ifdef TRACE
199 vbuf = tp = _nc_trace_buf(bufnum, WideLen(len));
200#else
201 {
Steve Kondikae271bc2015-11-15 02:50:53 +0100202 static char *mybuf[NUM_VISBUFS];
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530203 mybuf[bufnum] = typeRealloc(char, WideLen(len), mybuf[bufnum]);
204 vbuf = tp = mybuf[bufnum];
205 }
206#endif
207 if (tp != 0) {
208 *tp++ = D_QUOTE;
Steve Kondikae271bc2015-11-15 02:50:53 +0100209 while ((--count >= 0) && (c = *buf++) != '\0') {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530210 char temp[CCHARW_MAX + 80];
211 int j = wctomb(temp, c), k;
212 if (j <= 0) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100213 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
214 "\\u%08X", (unsigned) c);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530215 j = (int) strlen(temp);
216 }
217 for (k = 0; k < j; ++k) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100218 tp = VisChar(tp, UChar(temp[k]), WideLen(len));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530219 }
220 }
221 *tp++ = D_QUOTE;
Steve Kondikae271bc2015-11-15 02:50:53 +0100222 *tp = '\0';
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530223 } else {
224 vbuf = ("(_nc_viswbuf2n failed)");
225 }
226 return (vbuf);
227}
228
229NCURSES_EXPORT(const char *)
230_nc_viswbuf2(int bufnum, const wchar_t *buf)
231{
232 return _nc_viswbuf2n(bufnum, buf, -1);
233}
234
235NCURSES_EXPORT(const char *)
236_nc_viswbuf(const wchar_t *buf)
237{
238 return _nc_viswbuf2(0, buf);
239}
240
241NCURSES_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() */
248NCURSES_EXPORT(const char *)
249_nc_viswibuf(const wint_t *buf)
250{
251 static wchar_t *mybuf;
252 static unsigned mylen;
253 unsigned n;
254
Steve Kondikae271bc2015-11-15 02:50:53 +0100255 for (n = 0; buf[n] != 0; ++n) {
256 ; /* empty */
257 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530258 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 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100265 if (mybuf != 0) {
266 for (n = 0; buf[n] != 0; ++n) {
267 mybuf[n] = (wchar_t) buf[n];
268 }
269 mybuf[n] = L'\0';
270 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530271
272 return _nc_viswbuf2(0, mybuf);
273}
274#endif /* USE_WIDEC_SUPPORT */
275
276/* use these functions for displaying parts of a line within a window */
277NCURSES_EXPORT(const char *)
278_nc_viscbuf2(int bufnum, const NCURSES_CH_T * buf, int len)
279{
Steve Kondikae271bc2015-11-15 02:50:53 +0100280 char *result = _nc_trace_buf(bufnum, (size_t) BUFSIZ);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530281 int first;
282 const char *found;
283
284 if (result != 0) {
285#if USE_WIDEC_SUPPORT
286 if (len < 0)
287 len = _nc_wchstrlen(buf);
288#endif /* USE_WIDEC_SUPPORT */
289
290 /*
291 * Display one or more strings followed by attributes.
292 */
293 first = 0;
294 while (first < len) {
295 attr_t attr = AttrOf(buf[first]);
296 int last = len - 1;
297 int j;
298
299 for (j = first + 1; j < len; ++j) {
300 if (!SameAttrOf(buf[j], buf[first])) {
301 last = j - 1;
302 break;
303 }
304 }
305
Steve Kondikae271bc2015-11-15 02:50:53 +0100306 (void) _nc_trace_bufcat(bufnum, l_brace);
307 (void) _nc_trace_bufcat(bufnum, d_quote);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530308 for (j = first; j <= last; ++j) {
309 found = _nc_altcharset_name(attr, (chtype) CharOf(buf[j]));
310 if (found != 0) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100311 (void) _nc_trace_bufcat(bufnum, found);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530312 attr &= ~A_ALTCHARSET;
313 } else
314#if USE_WIDEC_SUPPORT
315 if (!isWidecExt(buf[j])) {
316 PUTC_DATA;
317
318 PUTC_INIT;
319 for (PUTC_i = 0; PUTC_i < CCHARW_MAX; ++PUTC_i) {
320 int k;
321
322 PUTC_ch = buf[j].chars[PUTC_i];
Steve Kondikae271bc2015-11-15 02:50:53 +0100323 if (PUTC_ch == L'\0') {
324 if (PUTC_i == 0)
325 (void) _nc_trace_bufcat(bufnum, "\\000");
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530326 break;
Steve Kondikae271bc2015-11-15 02:50:53 +0100327 }
328 PUTC_n = (int) wcrtomb(PUTC_buf,
329 buf[j].chars[PUTC_i], &PUT_st);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530330 if (PUTC_n <= 0)
331 break;
332 for (k = 0; k < PUTC_n; k++) {
333 char temp[80];
Steve Kondikae271bc2015-11-15 02:50:53 +0100334 VisChar(temp, UChar(PUTC_buf[k]), sizeof(temp));
335 (void) _nc_trace_bufcat(bufnum, temp);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530336 }
337 }
338 }
339#else
340 {
341 char temp[80];
Steve Kondikae271bc2015-11-15 02:50:53 +0100342 VisChar(temp, UChar(buf[j]), sizeof(temp));
343 (void) _nc_trace_bufcat(bufnum, temp);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530344 }
345#endif /* USE_WIDEC_SUPPORT */
346 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100347 (void) _nc_trace_bufcat(bufnum, d_quote);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530348 if (attr != A_NORMAL) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100349 (void) _nc_trace_bufcat(bufnum, " | ");
350 (void) _nc_trace_bufcat(bufnum, _traceattr2(bufnum + 20, attr));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530351 }
352 result = _nc_trace_bufcat(bufnum, r_brace);
353 first = last + 1;
354 }
355 }
356 return result;
357}
358
359NCURSES_EXPORT(const char *)
360_nc_viscbuf(const NCURSES_CH_T * buf, int len)
361{
362 return _nc_viscbuf2(0, buf, len);
363}
364#endif /* TRACE */