1 /****************************************************************************
2 * Copyright (c) 2001-2014,2016 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
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 ****************************************************************************/
36 * visbuf.c - Tracing/Debugging support routines
39 #define NEED_NCURSES_CH_T
40 #include <curses.priv.h>
45 MODULE_ID("$Id: visbuf.c,v 1.48 2016/09/05 00:43:08 tom Exp $")
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)
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);
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
62 #define VisChar(tp, chr, limit) _nc_vischar(tp, chr)
63 #define LIMIT_ARG /* nothing */
67 _nc_vischar(char *tp, unsigned c LIMIT_ARG)
69 if (c == '"' || c == '\\') {
72 } else if (is7bits((int) c) && (isgraph((int) c) || c == ' ')) {
74 } else if (c == '\n') {
77 } else if (c == '\r') {
80 } else if (c == '\b') {
83 } else if (c == '\033') {
86 } else if (UChar(c) == 0x7f) {
90 } else if (is7bits(c) && iscntrl(UChar(c))) {
93 *tp++ = (char) ('@' + c);
95 _nc_SPRINTF(tp, _nc_SLIMIT(limit)
96 "\\%03lo", (unsigned long) ChCharOf(c));
104 _nc_visbuf2n(int bufnum, const char *buf, int len)
106 const char *vbuf = 0;
112 if (buf == CANCELLED_STRING)
113 return ("(cancelled)");
116 len = (int) strlen(buf);
120 vbuf = tp = _nc_trace_buf(bufnum, NormalLen(len));
123 static char *mybuf[NUM_VISBUFS];
127 for (c = 0; c < NUM_VISBUFS; ++c) {
128 FreeAndNull(mybuf[c]);
132 mybuf[bufnum] = typeRealloc(char, NormalLen(len), mybuf[bufnum]);
133 vbuf = tp = mybuf[bufnum];
141 while ((--count >= 0) && (c = *buf++) != '\0') {
142 tp = VisChar(tp, UChar(c), NormalLen(len));
147 vbuf = ("(_nc_visbuf2n failed)");
152 NCURSES_EXPORT(const char *)
153 _nc_visbuf2(int bufnum, const char *buf)
155 return _nc_visbuf2n(bufnum, buf, -1);
158 NCURSES_EXPORT(const char *)
159 _nc_visbuf(const char *buf)
161 return _nc_visbuf2(0, buf);
164 NCURSES_EXPORT(const char *)
165 _nc_visbufn(const char *buf, int len)
167 return _nc_visbuf2n(0, buf, len);
171 #if USE_WIDEC_SUPPORT
173 #if defined(USE_TERMLIB)
174 #define _nc_wchstrlen _my_wchstrlen
176 _nc_wchstrlen(const cchar_t *s)
179 while (CharOf(s[result]) != L'\0') {
187 _nc_viswbuf2n(int bufnum, const wchar_t *buf, int len)
197 len = (int) wcslen(buf);
201 vbuf = tp = _nc_trace_buf(bufnum, WideLen(len));
204 static char *mybuf[NUM_VISBUFS];
205 mybuf[bufnum] = typeRealloc(char, WideLen(len), mybuf[bufnum]);
206 vbuf = tp = mybuf[bufnum];
213 while ((--count >= 0) && (c = *buf++) != '\0') {
214 char temp[CCHARW_MAX + 80];
215 int j = wctomb(temp, c), k;
217 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
218 "\\u%08X", (unsigned) c);
219 j = (int) strlen(temp);
221 for (k = 0; k < j; ++k) {
222 tp = VisChar(tp, UChar(temp[k]), WideLen(len));
228 vbuf = ("(_nc_viswbuf2n failed)");
233 NCURSES_EXPORT(const char *)
234 _nc_viswbuf2(int bufnum, const wchar_t *buf)
236 return _nc_viswbuf2n(bufnum, buf, -1);
239 NCURSES_EXPORT(const char *)
240 _nc_viswbuf(const wchar_t *buf)
242 return _nc_viswbuf2(0, buf);
245 NCURSES_EXPORT(const char *)
246 _nc_viswbufn(const wchar_t *buf, int len)
248 return _nc_viswbuf2n(0, buf, len);
251 /* this special case is used for wget_wstr() */
252 NCURSES_EXPORT(const char *)
253 _nc_viswibuf(const wint_t *buf)
255 static wchar_t *mybuf;
256 static unsigned mylen;
259 for (n = 0; buf[n] != 0; ++n) {
265 mybuf = typeRealloc(wchar_t, mylen, mybuf);
267 mybuf = typeMalloc(wchar_t, mylen);
270 for (n = 0; buf[n] != 0; ++n) {
271 mybuf[n] = (wchar_t) buf[n];
276 return _nc_viswbuf2(0, mybuf);
278 #endif /* USE_WIDEC_SUPPORT */
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)
284 char *result = _nc_trace_buf(bufnum, (size_t) BUFSIZ);
289 #if USE_WIDEC_SUPPORT
291 len = _nc_wchstrlen(buf);
292 #endif /* USE_WIDEC_SUPPORT */
295 * Display one or more strings followed by attributes.
297 while (first < len) {
298 attr_t attr = AttrOf(buf[first]);
302 for (j = first + 1; j < len; ++j) {
303 if (!SameAttrOf(buf[j], buf[first])) {
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)
315 (void) _nc_trace_bufcat(bufnum, found);
316 attr &= ~A_ALTCHARSET;
318 #if USE_WIDEC_SUPPORT
319 if (!isWidecExt(buf[j])) {
322 for (PUTC_i = 0; PUTC_i < CCHARW_MAX; ++PUTC_i) {
326 PUTC_ch = buf[j].chars[PUTC_i];
327 if (PUTC_ch == L'\0') {
329 (void) _nc_trace_bufcat(bufnum, "\\000");
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))
338 wcwidth(buf[j].chars[PUTC_i]),
339 buf[j].chars[PUTC_i]);
340 (void) _nc_trace_bufcat(bufnum, temp);
343 for (k = 0; k < PUTC_n; k++) {
344 VisChar(temp, UChar(PUTC_buf[k]), sizeof(temp));
345 (void) _nc_trace_bufcat(bufnum, temp);
352 VisChar(temp, UChar(buf[j]), sizeof(temp));
353 (void) _nc_trace_bufcat(bufnum, temp);
355 #endif /* USE_WIDEC_SUPPORT */
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));
362 result = _nc_trace_bufcat(bufnum, r_brace);
369 NCURSES_EXPORT(const char *)
370 _nc_viscbuf(const NCURSES_CH_T * buf, int len)
372 return _nc_viscbuf2(0, buf, len);