1 /****************************************************************************
2 * Copyright (c) 2001-2008,2009 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.35 2009/12/12 21:34:23 tom Exp $")
49 #define NormalLen(len) (size_t) (((size_t)(len) + 1) * 4)
50 #define WideLen(len) (size_t) (((size_t)(len) + 1) * 4 * 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);
59 _nc_vischar(char *tp, unsigned c)
61 if (c == '"' || c == '\\') {
64 } else if (is7bits(c) && (isgraph(c) || c == ' ')) {
66 } else if (c == '\n') {
69 } else if (c == '\r') {
72 } else if (c == '\b') {
75 } else if (c == '\033') {
78 } else if (UChar(c) == 0x7f) {
82 } else if (is7bits(c) && iscntrl(UChar(c))) {
85 *tp++ = (char) ('@' + c);
87 sprintf(tp, "\\%03lo", (unsigned long) ChCharOf(c));
95 _nc_visbuf2n(int bufnum, const char *buf, int len)
103 if (buf == CANCELLED_STRING)
104 return ("(cancelled)");
107 len = (int) strlen(buf);
110 vbuf = tp = _nc_trace_buf(bufnum, NormalLen(len));
113 static char *mybuf[NUM_VISBUFS];
115 for (c = 0; c < NUM_VISBUFS; ++c) {
116 FreeAndNull(mybuf[c]);
120 mybuf[bufnum] = typeRealloc(char, NormalLen(len), mybuf[bufnum]);
121 vbuf = tp = mybuf[bufnum];
127 while ((--len >= 0) && (c = *buf++) != '\0') {
128 tp = _nc_vischar(tp, UChar(c));
133 vbuf = ("(_nc_visbuf2n failed)");
138 NCURSES_EXPORT(const char *)
139 _nc_visbuf2(int bufnum, const char *buf)
141 return _nc_visbuf2n(bufnum, buf, -1);
144 NCURSES_EXPORT(const char *)
145 _nc_visbuf(const char *buf)
147 return _nc_visbuf2(0, buf);
150 NCURSES_EXPORT(const char *)
151 _nc_visbufn(const char *buf, int len)
153 return _nc_visbuf2n(0, buf, len);
157 #if USE_WIDEC_SUPPORT
159 #if defined(USE_TERMLIB)
160 #define _nc_wchstrlen _my_wchstrlen
162 _nc_wchstrlen(const cchar_t *s)
165 while (CharOf(s[result]) != L'\0') {
173 _nc_viswbuf2n(int bufnum, const wchar_t *buf, int len)
183 len = (int) wcslen(buf);
186 vbuf = tp = _nc_trace_buf(bufnum, WideLen(len));
189 static char *mybuf[NUM_VISBUFS];
190 mybuf[bufnum] = typeRealloc(char, WideLen(len), mybuf[bufnum]);
191 vbuf = tp = mybuf[bufnum];
196 while ((--len >= 0) && (c = *buf++) != '\0') {
197 char temp[CCHARW_MAX + 80];
198 int j = wctomb(temp, c), k;
200 sprintf(temp, "\\u%08X", (unsigned) c);
201 j = (int) strlen(temp);
203 for (k = 0; k < j; ++k) {
204 tp = _nc_vischar(tp, UChar(temp[k]));
210 vbuf = ("(_nc_viswbuf2n failed)");
215 NCURSES_EXPORT(const char *)
216 _nc_viswbuf2(int bufnum, const wchar_t *buf)
218 return _nc_viswbuf2n(bufnum, buf, -1);
221 NCURSES_EXPORT(const char *)
222 _nc_viswbuf(const wchar_t *buf)
224 return _nc_viswbuf2(0, buf);
227 NCURSES_EXPORT(const char *)
228 _nc_viswbufn(const wchar_t *buf, int len)
230 return _nc_viswbuf2n(0, buf, len);
233 /* this special case is used for wget_wstr() */
234 NCURSES_EXPORT(const char *)
235 _nc_viswibuf(const wint_t *buf)
237 static wchar_t *mybuf;
238 static unsigned mylen;
241 for (n = 0; buf[n] != 0; ++n) {
247 mybuf = typeRealloc(wchar_t, mylen, mybuf);
249 mybuf = typeMalloc(wchar_t, mylen);
251 for (n = 0; buf[n] != 0; ++n) {
252 mybuf[n] = (wchar_t) buf[n];
256 return _nc_viswbuf2(0, mybuf);
258 #endif /* USE_WIDEC_SUPPORT */
260 /* use these functions for displaying parts of a line within a window */
261 NCURSES_EXPORT(const char *)
262 _nc_viscbuf2(int bufnum, const NCURSES_CH_T * buf, int len)
264 char *result = _nc_trace_buf(bufnum, BUFSIZ);
269 #if USE_WIDEC_SUPPORT
271 len = _nc_wchstrlen(buf);
272 #endif /* USE_WIDEC_SUPPORT */
275 * Display one or more strings followed by attributes.
278 while (first < len) {
279 attr_t attr = AttrOf(buf[first]);
283 for (j = first + 1; j < len; ++j) {
284 if (!SameAttrOf(buf[j], buf[first])) {
290 result = _nc_trace_bufcat(bufnum, l_brace);
291 result = _nc_trace_bufcat(bufnum, d_quote);
292 for (j = first; j <= last; ++j) {
293 found = _nc_altcharset_name(attr, (chtype) CharOf(buf[j]));
295 result = _nc_trace_bufcat(bufnum, found);
296 attr &= ~A_ALTCHARSET;
298 #if USE_WIDEC_SUPPORT
299 if (!isWidecExt(buf[j])) {
303 for (PUTC_i = 0; PUTC_i < CCHARW_MAX; ++PUTC_i) {
306 PUTC_ch = buf[j].chars[PUTC_i];
307 if (PUTC_ch == L'\0') {
309 result = _nc_trace_bufcat(bufnum, "\\000");
312 PUTC_n = (int) wcrtomb(PUTC_buf,
313 buf[j].chars[PUTC_i], &PUT_st);
316 for (k = 0; k < PUTC_n; k++) {
318 _nc_vischar(temp, UChar(PUTC_buf[k]));
319 result = _nc_trace_bufcat(bufnum, temp);
326 _nc_vischar(temp, UChar(buf[j]));
327 result = _nc_trace_bufcat(bufnum, temp);
329 #endif /* USE_WIDEC_SUPPORT */
331 result = _nc_trace_bufcat(bufnum, d_quote);
332 if (attr != A_NORMAL) {
333 result = _nc_trace_bufcat(bufnum, " | ");
334 result = _nc_trace_bufcat(bufnum, _traceattr2(bufnum + 20, attr));
336 result = _nc_trace_bufcat(bufnum, r_brace);
343 NCURSES_EXPORT(const char *)
344 _nc_viscbuf(const NCURSES_CH_T * buf, int len)
346 return _nc_viscbuf2(0, buf, len);