1 /****************************************************************************
2 * Copyright (c) 2001-2006,2007 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.26 2007/06/09 17:21:53 tom Exp $")
47 #define NormalLen(len) (unsigned) ((len + 1) * 4)
48 #define WideLen(len) (unsigned) ((len + 1) * 4 * MB_CUR_MAX)
51 static const char d_quote[] = StringOf(D_QUOTE);
52 static const char l_brace[] = StringOf(L_BRACE);
53 static const char r_brace[] = StringOf(R_BRACE);
57 _nc_vischar(char *tp, unsigned c)
59 if (c == '"' || c == '\\') {
62 } else if (is7bits(c) && (isgraph(c) || c == ' ')) {
64 } else if (c == '\n') {
67 } else if (c == '\r') {
70 } else if (c == '\b') {
73 } else if (c == '\033') {
76 } else if (UChar(c) == 0x7f) {
80 } else if (is7bits(c) && iscntrl(UChar(c))) {
85 sprintf(tp, "\\%03lo", (unsigned long) ChCharOf(c));
93 _nc_visbuf2n(int bufnum, const char *buf, int len)
101 if (buf == CANCELLED_STRING)
102 return ("(cancelled)");
108 tp = vbuf = _nc_trace_buf(bufnum, NormalLen(len));
111 static char *mybuf[4];
112 mybuf[bufnum] = typeRealloc(char, NormalLen(len), mybuf[bufnum]);
113 tp = vbuf = mybuf[bufnum];
117 while ((--len >= 0) && (c = *buf++) != '\0') {
118 tp = _nc_vischar(tp, UChar(c));
125 NCURSES_EXPORT(const char *)
126 _nc_visbuf2(int bufnum, const char *buf)
128 return _nc_visbuf2n(bufnum, buf, -1);
131 NCURSES_EXPORT(const char *)
132 _nc_visbuf(const char *buf)
134 return _nc_visbuf2(0, buf);
137 NCURSES_EXPORT(const char *)
138 _nc_visbufn(const char *buf, int len)
140 return _nc_visbuf2n(0, buf, len);
144 #if USE_WIDEC_SUPPORT
146 #if defined(USE_TERMLIB)
147 #define _nc_wchstrlen _my_wchstrlen
149 _nc_wchstrlen(const cchar_t *s)
152 while (CharOf(s[result]) != L'\0') {
160 _nc_viswbuf2n(int bufnum, const wchar_t *buf, int len)
173 tp = vbuf = _nc_trace_buf(bufnum, WideLen(len));
176 static char *mybuf[2];
177 mybuf[bufnum] = typeRealloc(char, WideLen(len), mybuf[bufnum]);
178 tp = vbuf = mybuf[bufnum];
182 while ((--len >= 0) && (c = *buf++) != '\0') {
183 char temp[CCHARW_MAX + 80];
184 int j = wctomb(temp, c), k;
186 sprintf(temp, "\\u%08X", (wint_t) c);
189 for (k = 0; k < j; ++k) {
190 tp = _nc_vischar(tp, UChar(temp[k]));
198 NCURSES_EXPORT(const char *)
199 _nc_viswbuf2(int bufnum, const wchar_t *buf)
201 return _nc_viswbuf2n(bufnum, buf, -1);
204 NCURSES_EXPORT(const char *)
205 _nc_viswbuf(const wchar_t *buf)
207 return _nc_viswbuf2(0, buf);
210 NCURSES_EXPORT(const char *)
211 _nc_viswbufn(const wchar_t *buf, int len)
213 return _nc_viswbuf2n(0, buf, len);
216 /* this special case is used for wget_wstr() */
217 NCURSES_EXPORT(const char *)
218 _nc_viswibuf(const wint_t *buf)
220 static wchar_t *mybuf;
221 static unsigned mylen;
224 for (n = 0; buf[n] != 0; ++n) ;
228 mybuf = typeRealloc(wchar_t, mylen, mybuf);
230 mybuf = typeMalloc(wchar_t, mylen);
232 for (n = 0; buf[n] != 0; ++n)
233 mybuf[n] = (wchar_t) buf[n];
235 return _nc_viswbuf2(0, mybuf);
237 #endif /* USE_WIDEC_SUPPORT */
239 /* use these functions for displaying parts of a line within a window */
240 NCURSES_EXPORT(const char *)
241 _nc_viscbuf2(int bufnum, const NCURSES_CH_T * buf, int len)
243 char *result = _nc_trace_buf(bufnum, BUFSIZ);
247 #if USE_WIDEC_SUPPORT
249 len = _nc_wchstrlen(buf);
250 #endif /* USE_WIDEC_SUPPORT */
253 * Display one or more strings followed by attributes.
256 while (first < len) {
257 attr_t attr = AttrOf(buf[first]);
261 for (j = first + 1; j < len; ++j) {
262 if (!SameAttrOf(buf[j], buf[first])) {
268 result = _nc_trace_bufcat(bufnum, l_brace);
269 result = _nc_trace_bufcat(bufnum, d_quote);
270 for (j = first; j <= last; ++j) {
271 found = _nc_altcharset_name(attr, (chtype) CharOf(buf[j]));
273 result = _nc_trace_bufcat(bufnum, found);
274 attr &= ~A_ALTCHARSET;
276 #if USE_WIDEC_SUPPORT
277 if (!isWidecExt(buf[j])) {
281 for (PUTC_i = 0; PUTC_i < CCHARW_MAX; ++PUTC_i) {
284 PUTC_ch = buf[j].chars[PUTC_i];
285 if (PUTC_ch == L'\0')
287 PUTC_n = wcrtomb(PUTC_buf, buf[j].chars[PUTC_i], &PUT_st);
290 for (k = 0; k < PUTC_n; k++) {
292 _nc_vischar(temp, UChar(PUTC_buf[k]));
293 result = _nc_trace_bufcat(bufnum, temp);
300 _nc_vischar(temp, UChar(buf[j]));
301 result = _nc_trace_bufcat(bufnum, temp);
303 #endif /* USE_WIDEC_SUPPORT */
305 result = _nc_trace_bufcat(bufnum, d_quote);
306 if (attr != A_NORMAL) {
307 result = _nc_trace_bufcat(bufnum, " | ");
308 result = _nc_trace_bufcat(bufnum, _traceattr2(bufnum + 20, attr));
310 result = _nc_trace_bufcat(bufnum, r_brace);
316 NCURSES_EXPORT(const char *)
317 _nc_viscbuf(const NCURSES_CH_T * buf, int len)
319 return _nc_viscbuf2(0, buf, len);