1 /****************************************************************************
2 * Copyright (c) 2001-2003,2004 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-2004 *
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 #include <curses.priv.h>
44 MODULE_ID("$Id: visbuf.c,v 1.9 2004/02/03 01:16:37 tom Exp $")
47 _nc_vischar(char *tp, unsigned c)
49 if (c == '"' || c == '\\') {
52 } else if (is7bits(c) && (isgraph(c) || c == ' ')) {
54 } else if (c == '\n') {
57 } else if (c == '\r') {
60 } else if (c == '\b') {
63 } else if (c == '\033') {
66 } else if (is7bits(c) && iscntrl(UChar(c))) {
71 sprintf(tp, "\\%03lo", ChCharOf(c));
79 _nc_visbuf2n(int bufnum, const char *buf, int len)
87 if (buf == CANCELLED_STRING)
88 return ("(cancelled)");
94 tp = vbuf = _nc_trace_buf(bufnum, (unsigned) (len * 4) + 5);
97 static char *mybuf[2];
98 mybuf[bufnum] = typeRealloc(char, (unsigned) (len * 4) + 5, mybuf[bufnum]);
99 tp = vbuf = mybuf[bufnum];
103 while ((--len >= 0) && (c = *buf++) != '\0') {
104 tp = _nc_vischar(tp, UChar(c));
111 NCURSES_EXPORT(const char *)
112 _nc_visbuf2(int bufnum, const char *buf)
114 return _nc_visbuf2n(bufnum, buf, -1);
117 NCURSES_EXPORT(const char *)
118 _nc_visbuf(const char *buf)
120 return _nc_visbuf2(0, buf);
123 NCURSES_EXPORT(const char *)
124 _nc_visbufn(const char *buf, int len)
126 return _nc_visbuf2n(0, buf, len);
129 #if USE_WIDEC_SUPPORT
132 _nc_viswbuf2n(int bufnum, const wchar_t * buf, int len)
145 tp = vbuf = _nc_trace_buf(bufnum, (unsigned) (len * 4) + 5);
148 static char *mybuf[2];
149 mybuf[bufnum] = typeRealloc(char, (unsigned) (len * 4) + 5, mybuf[bufnum]);
150 tp = vbuf = mybuf[bufnum];
154 while ((--len >= 0) && (c = *buf++) != '\0') {
155 char temp[CCHARW_MAX + 80];
156 int j = wctomb(temp, c), k;
158 sprintf(temp, "\\u%08X", (wint_t) c);
161 for (k = 0; k < j; ++k) {
162 tp = _nc_vischar(tp, temp[k]);
170 NCURSES_EXPORT(const char *)
171 _nc_viswbuf2(int bufnum, const wchar_t * buf)
173 return _nc_viswbuf2n(bufnum, buf, -1);
176 NCURSES_EXPORT(const char *)
177 _nc_viswbuf(const wchar_t * buf)
179 return _nc_viswbuf2(0, buf);
182 NCURSES_EXPORT(const char *)
183 _nc_viswbufn(const wchar_t * buf, int len)
185 return _nc_viswbuf2n(0, buf, len);
188 NCURSES_EXPORT(const char *)
189 _nc_viscbuf2(int bufnum, const cchar_t * buf, int len)
191 char *result = _nc_trace_buf(bufnum, BUFSIZ);
194 attr_t attr = A_NORMAL;
198 len = _nc_wchstrlen(buf);
200 for (n = 1; n < len; n++) {
201 if (AttrOf(buf[n]) != AttrOf(buf[0])) {
208 * If the rendition is the same for the whole string, display it as a
209 * quoted string, followed by the rendition. Otherwise, use the more
210 * detailed trace function that displays each character separately.
213 static const char d_quote[] =
216 result = _nc_trace_bufcat(bufnum, d_quote);
218 if ((found = _nc_altcharset_name(attr, CharOfD(buf))) != 0) {
219 result = _nc_trace_bufcat(bufnum, found);
220 attr &= ~A_ALTCHARSET;
221 } else if (!isnac(CHDEREF(buf))) {
226 PUTC_ch = PUTC_i < CCHARW_MAX ? buf->chars[PUTC_i] : L'\0';
227 PUTC_n = wcrtomb(PUTC_buf, buf->chars[PUTC_i], &PUT_st);
228 if (PUTC_ch == L'\0')
232 for (n = 0; n < PUTC_n; n++) {
234 _nc_vischar(temp, UChar(PUTC_buf[n]));
235 result = _nc_trace_bufcat(bufnum, temp);
238 } while (PUTC_ch != L'\0');
242 result = _nc_trace_bufcat(bufnum, d_quote);
243 if (attr != A_NORMAL) {
244 result = _nc_trace_bufcat(bufnum, " | ");
245 result = _nc_trace_bufcat(bufnum, _traceattr2(bufnum + 20, attr));
248 static const char l_brace[] =
250 static const char r_brace[] =
252 strcpy(result, l_brace);
254 result = _nc_trace_bufcat(bufnum,
255 _tracecchar_t2(bufnum + 20, buf++));
257 result = _nc_trace_bufcat(bufnum, r_brace);
262 NCURSES_EXPORT(const char *)
263 _nc_viscbuf(const cchar_t * buf, int len)
265 return _nc_viscbuf2(0, buf, len);
268 #endif /* USE_WIDEC_SUPPORT */