1 /****************************************************************************
2  * Copyright 2019,2020 Thomas E. Dickey                                     *
3  * Copyright 2001-2016,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29 
30 /****************************************************************************
31  *  Author: Thomas E. Dickey                        1996-on                 *
32  *     and: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
33  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
34  ****************************************************************************/
35 
36 /*
37  *	visbuf.c - Tracing/Debugging support routines
38  */
39 
40 #define NEED_NCURSES_CH_T
41 #include <curses.priv.h>
42 
43 #include <tic.h>
44 #include <ctype.h>
45 
46 MODULE_ID("$Id: visbuf.c,v 1.52 2020/02/02 23:34:34 tom Exp $")
47 
48 #define NUM_VISBUFS 4
49 
50 #define NormalLen(len) (size_t) (((size_t)(len) + 1) * 4)
51 #define WideLen(len)   (size_t) (((size_t)(len) + 1) * 4 * (size_t) MB_CUR_MAX)
52 
53 #ifdef TRACE
54 static const char d_quote[] = StringOf(D_QUOTE);
55 static const char l_brace[] = StringOf(L_BRACE);
56 static const char r_brace[] = StringOf(R_BRACE);
57 #endif
58 
59 #if USE_STRING_HACKS && HAVE_SNPRINTF
60 #define VisChar(tp, chr, limit) _nc_vischar(tp, chr, limit)
61 #define LIMIT_ARG ,size_t limit
62 #else
63 #define VisChar(tp, chr, limit) _nc_vischar(tp, chr)
64 #define LIMIT_ARG		/* nothing */
65 #endif
66 
67 static char *
68 _nc_vischar(char *tp, unsigned c LIMIT_ARG)
69 {
70     if (c == '"' || c == '\\') {
71 	*tp++ = '\\';
72 	*tp++ = (char) c;
73     } else if (is7bits((int) c) && (isgraph((int) c) || c == ' ')) {
74 	*tp++ = (char) c;
75     } else if (c == '\n') {
76 	*tp++ = '\\';
77 	*tp++ = 'n';
78     } else if (c == '\r') {
79 	*tp++ = '\\';
80 	*tp++ = 'r';
81     } else if (c == '\b') {
82 	*tp++ = '\\';
83 	*tp++ = 'b';
84     } else if (c == '\t') {
85 	*tp++ = '\\';
86 	*tp++ = 't';
87     } else if (c == '\033') {
88 	*tp++ = '\\';
89 	*tp++ = 'e';
90     } else if (UChar(c) == 0x7f) {
91 	*tp++ = '\\';
92 	*tp++ = '^';
93 	*tp++ = '?';
94     } else if (is7bits(c) && iscntrl(UChar(c))) {
95 	*tp++ = '\\';
96 	*tp++ = '^';
97 	*tp++ = (char) ('@' + c);
98     } else {
99 	_nc_SPRINTF(tp, _nc_SLIMIT(limit)
100 		    "\\%03lo", (unsigned long) ChCharOf(c));
101 	tp += strlen(tp);
102     }
103     *tp = 0;
104     return tp;
105 }
106 
107 static const char *
108 _nc_visbuf2n(int bufnum, const char *buf, int len)
109 {
110     const char *vbuf = 0;
111     char *tp;
112     int count;
113 
114     if (buf == 0)
115 	return ("(null)");
116     if (buf == CANCELLED_STRING)
117 	return ("(cancelled)");
118 
119     if (len < 0)
120 	len = (int) strlen(buf);
121 
122     count = len;
123 #ifdef TRACE
124     vbuf = tp = _nc_trace_buf(bufnum, NormalLen(len));
125 #else
126     {
127 	static char *mybuf[NUM_VISBUFS];
128 	int c;
129 
130 	if (bufnum < 0) {
131 	    for (c = 0; c < NUM_VISBUFS; ++c) {
132 		FreeAndNull(mybuf[c]);
133 	    }
134 	    tp = 0;
135 	} else {
136 	    mybuf[bufnum] = typeRealloc(char, NormalLen(len), mybuf[bufnum]);
137 	    vbuf = tp = mybuf[bufnum];
138 	}
139     }
140 #endif
141     if (tp != 0) {
142 	int c;
143 
144 	*tp++ = D_QUOTE;
145 	while ((--count >= 0) && (c = *buf++) != '\0') {
146 	    tp = VisChar(tp, UChar(c), NormalLen(len));
147 	}
148 	*tp++ = D_QUOTE;
149 	*tp = '\0';
150     } else {
151 	vbuf = ("(_nc_visbuf2n failed)");
152     }
153     return (vbuf);
154 }
155 
156 NCURSES_EXPORT(const char *)
157 _nc_visbuf2(int bufnum, const char *buf)
158 {
159     return _nc_visbuf2n(bufnum, buf, -1);
160 }
161 
162 NCURSES_EXPORT(const char *)
163 _nc_visbuf(const char *buf)
164 {
165     return _nc_visbuf2(0, buf);
166 }
167 
168 NCURSES_EXPORT(const char *)
169 _nc_visbufn(const char *buf, int len)
170 {
171     return _nc_visbuf2n(0, buf, len);
172 }
173 
174 #ifdef TRACE
175 #if USE_WIDEC_SUPPORT
176 
177 #if defined(USE_TERMLIB)
178 #define _nc_wchstrlen _my_wchstrlen
179 static int
180 _nc_wchstrlen(const cchar_t *s)
181 {
182     int result = 0;
183     while (CharOf(s[result]) != L'\0') {
184 	result++;
185     }
186     return result;
187 }
188 #endif
189 
190 static const char *
191 _nc_viswbuf2n(int bufnum, const wchar_t *buf, int len)
192 {
193     const char *vbuf;
194     char *tp;
195     int count;
196 
197     if (buf == 0)
198 	return ("(null)");
199 
200     if (len < 0)
201 	len = (int) wcslen(buf);
202 
203     count = len;
204 #ifdef TRACE
205     vbuf = tp = _nc_trace_buf(bufnum, WideLen(len));
206 #else
207     {
208 	static char *mybuf[NUM_VISBUFS];
209 	mybuf[bufnum] = typeRealloc(char, WideLen(len), mybuf[bufnum]);
210 	vbuf = tp = mybuf[bufnum];
211     }
212 #endif
213     if (tp != 0) {
214 	wchar_t c;
215 
216 	*tp++ = D_QUOTE;
217 	while ((--count >= 0) && (c = *buf++) != '\0') {
218 	    char temp[CCHARW_MAX + 80];
219 	    int j = wctomb(temp, c), k;
220 	    if (j <= 0) {
221 		_nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
222 			    "\\u%08X", (unsigned) c);
223 		j = (int) strlen(temp);
224 	    }
225 	    for (k = 0; k < j; ++k) {
226 		tp = VisChar(tp, UChar(temp[k]), WideLen(len));
227 	    }
228 	}
229 	*tp++ = D_QUOTE;
230 	*tp = '\0';
231     } else {
232 	vbuf = ("(_nc_viswbuf2n failed)");
233     }
234     return (vbuf);
235 }
236 
237 NCURSES_EXPORT(const char *)
238 _nc_viswbuf2(int bufnum, const wchar_t *buf)
239 {
240     return _nc_viswbuf2n(bufnum, buf, -1);
241 }
242 
243 NCURSES_EXPORT(const char *)
244 _nc_viswbuf(const wchar_t *buf)
245 {
246     return _nc_viswbuf2(0, buf);
247 }
248 
249 NCURSES_EXPORT(const char *)
250 _nc_viswbufn(const wchar_t *buf, int len)
251 {
252     return _nc_viswbuf2n(0, buf, len);
253 }
254 
255 /* this special case is used for wget_wstr() */
256 NCURSES_EXPORT(const char *)
257 _nc_viswibuf(const wint_t *buf)
258 {
259     static wchar_t *mybuf;
260     static unsigned mylen;
261     unsigned n;
262 
263     for (n = 0; buf[n] != 0; ++n) {
264 	;			/* empty */
265     }
266     if (mylen < ++n) {
267 	mylen = n + 80;
268 	if (mybuf != 0)
269 	    mybuf = typeRealloc(wchar_t, mylen, mybuf);
270 	else
271 	    mybuf = typeMalloc(wchar_t, mylen);
272     }
273     if (mybuf != 0) {
274 	for (n = 0; buf[n] != 0; ++n) {
275 	    mybuf[n] = (wchar_t) buf[n];
276 	}
277 	mybuf[n] = L'\0';
278     }
279 
280     return _nc_viswbuf2(0, mybuf);
281 }
282 #endif /* USE_WIDEC_SUPPORT */
283 
284 /* use these functions for displaying parts of a line within a window */
285 NCURSES_EXPORT(const char *)
286 _nc_viscbuf2(int bufnum, const NCURSES_CH_T * buf, int len)
287 {
288     char *result = _nc_trace_buf(bufnum, (size_t) BUFSIZ);
289 
290     if (result != 0) {
291 	int first = 0;
292 
293 #if USE_WIDEC_SUPPORT
294 	if (len < 0)
295 	    len = _nc_wchstrlen(buf);
296 #endif /* USE_WIDEC_SUPPORT */
297 
298 	/*
299 	 * Display one or more strings followed by attributes.
300 	 */
301 	while (first < len) {
302 	    attr_t attr = AttrOf(buf[first]);
303 	    int last = len - 1;
304 	    int j;
305 
306 	    for (j = first + 1; j < len; ++j) {
307 		if (!SameAttrOf(buf[j], buf[first])) {
308 		    last = j - 1;
309 		    break;
310 		}
311 	    }
312 
313 	    (void) _nc_trace_bufcat(bufnum, l_brace);
314 	    (void) _nc_trace_bufcat(bufnum, d_quote);
315 	    for (j = first; j <= last; ++j) {
316 		const char *found = _nc_altcharset_name(attr, (chtype)
317 							CharOf(buf[j]));
318 		if (found != 0) {
319 		    (void) _nc_trace_bufcat(bufnum, found);
320 		    attr &= ~A_ALTCHARSET;
321 		} else
322 #if USE_WIDEC_SUPPORT
323 		if (!isWidecExt(buf[j])) {
324 		    PUTC_DATA;
325 
326 		    for (PUTC_i = 0; PUTC_i < CCHARW_MAX; ++PUTC_i) {
327 			int k;
328 			char temp[80];
329 
330 			PUTC_ch = buf[j].chars[PUTC_i];
331 			if (PUTC_ch == L'\0') {
332 			    if (PUTC_i == 0)
333 				(void) _nc_trace_bufcat(bufnum, "\\000");
334 			    break;
335 			}
336 			PUTC_INIT;
337 			PUTC_n = (int) wcrtomb(PUTC_buf,
338 					       buf[j].chars[PUTC_i], &PUT_st);
339 			if (PUTC_n <= 0 || buf[j].chars[PUTC_i] > 255) {
340 			    _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
341 					"{%d:\\u%lx}",
342 					_nc_wacs_width(buf[j].chars[PUTC_i]),
343 					(unsigned long) buf[j].chars[PUTC_i]);
344 			    (void) _nc_trace_bufcat(bufnum, temp);
345 			    break;
346 			}
347 			for (k = 0; k < PUTC_n; k++) {
348 			    VisChar(temp, UChar(PUTC_buf[k]), sizeof(temp));
349 			    (void) _nc_trace_bufcat(bufnum, temp);
350 			}
351 		    }
352 		}
353 #else
354 		{
355 		    char temp[80];
356 		    VisChar(temp, UChar(buf[j]), sizeof(temp));
357 		    (void) _nc_trace_bufcat(bufnum, temp);
358 		}
359 #endif /* USE_WIDEC_SUPPORT */
360 	    }
361 	    (void) _nc_trace_bufcat(bufnum, d_quote);
362 	    if (attr != A_NORMAL) {
363 		(void) _nc_trace_bufcat(bufnum, " | ");
364 		(void) _nc_trace_bufcat(bufnum, _traceattr2(bufnum + 20, attr));
365 	    }
366 	    result = _nc_trace_bufcat(bufnum, r_brace);
367 	    first = last + 1;
368 	}
369     }
370     return result;
371 }
372 
373 NCURSES_EXPORT(const char *)
374 _nc_viscbuf(const NCURSES_CH_T * buf, int len)
375 {
376     return _nc_viscbuf2(0, buf, len);
377 }
378 #endif /* TRACE */
379