2002-10-13 11:35:53 +08:00
|
|
|
/****************************************************************************
|
2009-07-12 07:12:47 +08:00
|
|
|
* Copyright (c) 2001-2008,2009 Free Software Foundation, Inc. *
|
2002-10-13 11:35:53 +08:00
|
|
|
* *
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a *
|
|
|
|
* copy of this software and associated documentation files (the *
|
|
|
|
* "Software"), to deal in the Software without restriction, including *
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish, *
|
|
|
|
* distribute, distribute with modifications, sublicense, and/or sell *
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is *
|
|
|
|
* furnished to do so, subject to the following conditions: *
|
|
|
|
* *
|
|
|
|
* The above copyright notice and this permission notice shall be included *
|
|
|
|
* in all copies or substantial portions of the Software. *
|
|
|
|
* *
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
|
|
|
|
* IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
|
|
|
|
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
|
|
|
|
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
|
|
|
|
* THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
|
|
|
|
* *
|
|
|
|
* Except as contained in this notice, the name(s) of the above copyright *
|
|
|
|
* holders shall not be used in advertising or otherwise to promote the *
|
|
|
|
* sale, use or other dealings in this Software without prior written *
|
|
|
|
* authorization. *
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2005-10-10 02:41:57 +08:00
|
|
|
* Author: Thomas E. Dickey 1996-on *
|
2002-10-13 11:35:53 +08:00
|
|
|
* and: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
|
|
|
|
* and: Eric S. Raymond <esr@snark.thyrsus.com> *
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* visbuf.c - Tracing/Debugging support routines
|
|
|
|
*/
|
|
|
|
|
2006-12-18 12:32:42 +08:00
|
|
|
#define NEED_NCURSES_CH_T
|
2002-10-13 11:35:53 +08:00
|
|
|
#include <curses.priv.h>
|
|
|
|
|
|
|
|
#include <tic.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2009-10-11 08:32:49 +08:00
|
|
|
MODULE_ID("$Id: visbuf.c,v 1.34 2009/10/10 20:41:55 tom Exp $")
|
2009-07-12 07:12:47 +08:00
|
|
|
|
|
|
|
#define NUM_VISBUFS 4
|
2007-06-03 08:04:38 +08:00
|
|
|
|
2008-08-05 09:06:16 +08:00
|
|
|
#define NormalLen(len) (size_t) (((size_t)(len) + 1) * 4)
|
|
|
|
#define WideLen(len) (size_t) (((size_t)(len) + 1) * 4 * MB_CUR_MAX)
|
2006-12-18 12:32:42 +08:00
|
|
|
|
2007-04-08 09:10:28 +08:00
|
|
|
#ifdef TRACE
|
2007-06-03 08:04:38 +08:00
|
|
|
static const char d_quote[] = StringOf(D_QUOTE);
|
|
|
|
static const char l_brace[] = StringOf(L_BRACE);
|
|
|
|
static const char r_brace[] = StringOf(R_BRACE);
|
2007-04-08 09:10:28 +08:00
|
|
|
#endif
|
2002-10-13 11:35:53 +08:00
|
|
|
|
|
|
|
static char *
|
|
|
|
_nc_vischar(char *tp, unsigned c)
|
|
|
|
{
|
|
|
|
if (c == '"' || c == '\\') {
|
|
|
|
*tp++ = '\\';
|
2008-08-05 09:06:16 +08:00
|
|
|
*tp++ = (char) c;
|
2002-10-13 11:35:53 +08:00
|
|
|
} else if (is7bits(c) && (isgraph(c) || c == ' ')) {
|
2008-08-05 09:06:16 +08:00
|
|
|
*tp++ = (char) c;
|
2002-10-13 11:35:53 +08:00
|
|
|
} else if (c == '\n') {
|
|
|
|
*tp++ = '\\';
|
|
|
|
*tp++ = 'n';
|
|
|
|
} else if (c == '\r') {
|
|
|
|
*tp++ = '\\';
|
|
|
|
*tp++ = 'r';
|
|
|
|
} else if (c == '\b') {
|
|
|
|
*tp++ = '\\';
|
|
|
|
*tp++ = 'b';
|
|
|
|
} else if (c == '\033') {
|
|
|
|
*tp++ = '\\';
|
|
|
|
*tp++ = 'e';
|
2007-06-10 08:41:32 +08:00
|
|
|
} else if (UChar(c) == 0x7f) {
|
|
|
|
*tp++ = '\\';
|
|
|
|
*tp++ = '^';
|
|
|
|
*tp++ = '?';
|
2002-10-13 11:35:53 +08:00
|
|
|
} else if (is7bits(c) && iscntrl(UChar(c))) {
|
|
|
|
*tp++ = '\\';
|
|
|
|
*tp++ = '^';
|
2008-08-05 09:06:16 +08:00
|
|
|
*tp++ = (char) ('@' + c);
|
2002-10-13 11:35:53 +08:00
|
|
|
} else {
|
2005-10-10 02:41:57 +08:00
|
|
|
sprintf(tp, "\\%03lo", (unsigned long) ChCharOf(c));
|
2002-10-13 11:35:53 +08:00
|
|
|
tp += strlen(tp);
|
|
|
|
}
|
2004-02-09 10:15:26 +08:00
|
|
|
*tp = 0;
|
2002-10-13 11:35:53 +08:00
|
|
|
return tp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
_nc_visbuf2n(int bufnum, const char *buf, int len)
|
|
|
|
{
|
2008-08-05 09:06:16 +08:00
|
|
|
const char *vbuf;
|
2002-10-13 11:35:53 +08:00
|
|
|
char *tp;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
if (buf == 0)
|
|
|
|
return ("(null)");
|
|
|
|
if (buf == CANCELLED_STRING)
|
|
|
|
return ("(cancelled)");
|
|
|
|
|
|
|
|
if (len < 0)
|
2008-08-05 09:06:16 +08:00
|
|
|
len = (int) strlen(buf);
|
2002-10-13 11:35:53 +08:00
|
|
|
|
|
|
|
#ifdef TRACE
|
2008-08-05 09:06:16 +08:00
|
|
|
vbuf = tp = _nc_trace_buf(bufnum, NormalLen(len));
|
2002-10-13 11:35:53 +08:00
|
|
|
#else
|
|
|
|
{
|
2009-07-12 07:12:47 +08:00
|
|
|
static char *mybuf[NUM_VISBUFS];
|
|
|
|
if (bufnum < 0) {
|
|
|
|
for (c = 0; c < NUM_VISBUFS; ++c) {
|
|
|
|
FreeAndNull(mybuf[c]);
|
|
|
|
}
|
|
|
|
tp = 0;
|
|
|
|
} else {
|
|
|
|
mybuf[bufnum] = typeRealloc(char, NormalLen(len), mybuf[bufnum]);
|
|
|
|
vbuf = tp = mybuf[bufnum];
|
|
|
|
}
|
2002-10-13 11:35:53 +08:00
|
|
|
}
|
|
|
|
#endif
|
2008-08-05 09:06:16 +08:00
|
|
|
if (tp != 0) {
|
|
|
|
*tp++ = D_QUOTE;
|
|
|
|
while ((--len >= 0) && (c = *buf++) != '\0') {
|
|
|
|
tp = _nc_vischar(tp, UChar(c));
|
|
|
|
}
|
|
|
|
*tp++ = D_QUOTE;
|
|
|
|
*tp++ = '\0';
|
|
|
|
} else {
|
|
|
|
vbuf = ("(_nc_visbuf2n failed)");
|
2002-10-13 11:35:53 +08:00
|
|
|
}
|
|
|
|
return (vbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
NCURSES_EXPORT(const char *)
|
|
|
|
_nc_visbuf2(int bufnum, const char *buf)
|
|
|
|
{
|
|
|
|
return _nc_visbuf2n(bufnum, buf, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
NCURSES_EXPORT(const char *)
|
|
|
|
_nc_visbuf(const char *buf)
|
|
|
|
{
|
|
|
|
return _nc_visbuf2(0, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
NCURSES_EXPORT(const char *)
|
|
|
|
_nc_visbufn(const char *buf, int len)
|
|
|
|
{
|
|
|
|
return _nc_visbuf2n(0, buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TRACE
|
2006-12-18 12:32:42 +08:00
|
|
|
#if USE_WIDEC_SUPPORT
|
2005-10-10 02:41:57 +08:00
|
|
|
|
|
|
|
#if defined(USE_TERMLIB)
|
|
|
|
#define _nc_wchstrlen _my_wchstrlen
|
|
|
|
static int
|
|
|
|
_nc_wchstrlen(const cchar_t *s)
|
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
while (CharOf(s[result]) != L'\0') {
|
|
|
|
result++;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-10-13 11:35:53 +08:00
|
|
|
static const char *
|
2005-10-10 02:41:57 +08:00
|
|
|
_nc_viswbuf2n(int bufnum, const wchar_t *buf, int len)
|
2002-10-13 11:35:53 +08:00
|
|
|
{
|
2008-08-05 09:06:16 +08:00
|
|
|
const char *vbuf;
|
2002-10-13 11:35:53 +08:00
|
|
|
char *tp;
|
|
|
|
wchar_t c;
|
|
|
|
|
|
|
|
if (buf == 0)
|
|
|
|
return ("(null)");
|
|
|
|
|
|
|
|
if (len < 0)
|
2008-08-05 09:06:16 +08:00
|
|
|
len = (int) wcslen(buf);
|
2002-10-13 11:35:53 +08:00
|
|
|
|
|
|
|
#ifdef TRACE
|
2008-08-05 09:06:16 +08:00
|
|
|
vbuf = tp = _nc_trace_buf(bufnum, WideLen(len));
|
2002-10-13 11:35:53 +08:00
|
|
|
#else
|
|
|
|
{
|
2009-07-12 07:12:47 +08:00
|
|
|
static char *mybuf[NUM_VISBUFS];
|
2007-06-03 08:04:38 +08:00
|
|
|
mybuf[bufnum] = typeRealloc(char, WideLen(len), mybuf[bufnum]);
|
2008-08-05 09:06:16 +08:00
|
|
|
vbuf = tp = mybuf[bufnum];
|
2002-10-13 11:35:53 +08:00
|
|
|
}
|
|
|
|
#endif
|
2008-08-05 09:06:16 +08:00
|
|
|
if (tp != 0) {
|
|
|
|
*tp++ = D_QUOTE;
|
|
|
|
while ((--len >= 0) && (c = *buf++) != '\0') {
|
|
|
|
char temp[CCHARW_MAX + 80];
|
|
|
|
int j = wctomb(temp, c), k;
|
|
|
|
if (j <= 0) {
|
|
|
|
sprintf(temp, "\\u%08X", (unsigned) c);
|
|
|
|
j = (int) strlen(temp);
|
|
|
|
}
|
|
|
|
for (k = 0; k < j; ++k) {
|
|
|
|
tp = _nc_vischar(tp, UChar(temp[k]));
|
|
|
|
}
|
2002-10-13 11:35:53 +08:00
|
|
|
}
|
2008-08-05 09:06:16 +08:00
|
|
|
*tp++ = D_QUOTE;
|
|
|
|
*tp++ = '\0';
|
|
|
|
} else {
|
|
|
|
vbuf = ("(_nc_viswbuf2n failed)");
|
2002-10-13 11:35:53 +08:00
|
|
|
}
|
|
|
|
return (vbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
NCURSES_EXPORT(const char *)
|
2005-10-10 02:41:57 +08:00
|
|
|
_nc_viswbuf2(int bufnum, const wchar_t *buf)
|
2002-10-13 11:35:53 +08:00
|
|
|
{
|
|
|
|
return _nc_viswbuf2n(bufnum, buf, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
NCURSES_EXPORT(const char *)
|
2005-10-10 02:41:57 +08:00
|
|
|
_nc_viswbuf(const wchar_t *buf)
|
2002-10-13 11:35:53 +08:00
|
|
|
{
|
|
|
|
return _nc_viswbuf2(0, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
NCURSES_EXPORT(const char *)
|
2005-10-10 02:41:57 +08:00
|
|
|
_nc_viswbufn(const wchar_t *buf, int len)
|
2002-10-13 11:35:53 +08:00
|
|
|
{
|
|
|
|
return _nc_viswbuf2n(0, buf, len);
|
|
|
|
}
|
|
|
|
|
2005-10-10 02:41:57 +08:00
|
|
|
/* this special case is used for wget_wstr() */
|
|
|
|
NCURSES_EXPORT(const char *)
|
|
|
|
_nc_viswibuf(const wint_t *buf)
|
|
|
|
{
|
|
|
|
static wchar_t *mybuf;
|
|
|
|
static unsigned mylen;
|
|
|
|
unsigned n;
|
|
|
|
|
2009-10-11 08:32:49 +08:00
|
|
|
for (n = 0; buf[n] != 0; ++n) {
|
|
|
|
; /* empty */
|
|
|
|
}
|
2005-10-10 02:41:57 +08:00
|
|
|
if (mylen < ++n) {
|
|
|
|
mylen = n + 80;
|
|
|
|
if (mybuf != 0)
|
|
|
|
mybuf = typeRealloc(wchar_t, mylen, mybuf);
|
|
|
|
else
|
|
|
|
mybuf = typeMalloc(wchar_t, mylen);
|
|
|
|
}
|
2009-10-11 08:32:49 +08:00
|
|
|
for (n = 0; buf[n] != 0; ++n) {
|
2005-10-10 02:41:57 +08:00
|
|
|
mybuf[n] = (wchar_t) buf[n];
|
2009-10-11 08:32:49 +08:00
|
|
|
}
|
|
|
|
mybuf[n] = L'\0';
|
2005-10-10 02:41:57 +08:00
|
|
|
|
|
|
|
return _nc_viswbuf2(0, mybuf);
|
|
|
|
}
|
2006-12-18 12:32:42 +08:00
|
|
|
#endif /* USE_WIDEC_SUPPORT */
|
2005-10-10 02:41:57 +08:00
|
|
|
|
2006-12-18 12:32:42 +08:00
|
|
|
/* use these functions for displaying parts of a line within a window */
|
2002-10-13 11:35:53 +08:00
|
|
|
NCURSES_EXPORT(const char *)
|
2006-12-18 12:32:42 +08:00
|
|
|
_nc_viscbuf2(int bufnum, const NCURSES_CH_T * buf, int len)
|
2002-10-13 11:35:53 +08:00
|
|
|
{
|
2004-02-09 10:15:26 +08:00
|
|
|
char *result = _nc_trace_buf(bufnum, BUFSIZ);
|
2006-12-18 12:32:42 +08:00
|
|
|
int first;
|
2002-10-13 11:35:53 +08:00
|
|
|
const char *found;
|
|
|
|
|
2008-08-05 09:06:16 +08:00
|
|
|
if (result != 0) {
|
2006-12-18 12:32:42 +08:00
|
|
|
#if USE_WIDEC_SUPPORT
|
2008-08-05 09:06:16 +08:00
|
|
|
if (len < 0)
|
|
|
|
len = _nc_wchstrlen(buf);
|
2006-12-18 12:32:42 +08:00
|
|
|
#endif /* USE_WIDEC_SUPPORT */
|
2002-10-13 11:35:53 +08:00
|
|
|
|
2008-08-05 09:06:16 +08:00
|
|
|
/*
|
|
|
|
* Display one or more strings followed by attributes.
|
|
|
|
*/
|
|
|
|
first = 0;
|
|
|
|
while (first < len) {
|
|
|
|
attr_t attr = AttrOf(buf[first]);
|
|
|
|
int last = len - 1;
|
|
|
|
int j;
|
|
|
|
|
|
|
|
for (j = first + 1; j < len; ++j) {
|
|
|
|
if (!SameAttrOf(buf[j], buf[first])) {
|
|
|
|
last = j - 1;
|
|
|
|
break;
|
|
|
|
}
|
2006-12-18 12:32:42 +08:00
|
|
|
}
|
2004-02-09 10:15:26 +08:00
|
|
|
|
2008-08-05 09:06:16 +08:00
|
|
|
result = _nc_trace_bufcat(bufnum, l_brace);
|
|
|
|
result = _nc_trace_bufcat(bufnum, d_quote);
|
|
|
|
for (j = first; j <= last; ++j) {
|
|
|
|
found = _nc_altcharset_name(attr, (chtype) CharOf(buf[j]));
|
|
|
|
if (found != 0) {
|
|
|
|
result = _nc_trace_bufcat(bufnum, found);
|
|
|
|
attr &= ~A_ALTCHARSET;
|
|
|
|
} else
|
2006-12-18 12:32:42 +08:00
|
|
|
#if USE_WIDEC_SUPPORT
|
2008-08-05 09:06:16 +08:00
|
|
|
if (!isWidecExt(buf[j])) {
|
|
|
|
PUTC_DATA;
|
|
|
|
|
|
|
|
PUTC_INIT;
|
|
|
|
for (PUTC_i = 0; PUTC_i < CCHARW_MAX; ++PUTC_i) {
|
|
|
|
int k;
|
|
|
|
|
|
|
|
PUTC_ch = buf[j].chars[PUTC_i];
|
|
|
|
if (PUTC_ch == L'\0')
|
|
|
|
break;
|
2009-07-12 07:12:47 +08:00
|
|
|
PUTC_n = (int) wcrtomb(PUTC_buf,
|
|
|
|
buf[j].chars[PUTC_i], &PUT_st);
|
2008-08-05 09:06:16 +08:00
|
|
|
if (PUTC_n <= 0)
|
|
|
|
break;
|
|
|
|
for (k = 0; k < PUTC_n; k++) {
|
|
|
|
char temp[80];
|
|
|
|
_nc_vischar(temp, UChar(PUTC_buf[k]));
|
|
|
|
result = _nc_trace_bufcat(bufnum, temp);
|
|
|
|
}
|
2002-10-13 11:35:53 +08:00
|
|
|
}
|
2006-12-18 12:32:42 +08:00
|
|
|
}
|
|
|
|
#else
|
2008-08-05 09:06:16 +08:00
|
|
|
{
|
|
|
|
char temp[80];
|
|
|
|
_nc_vischar(temp, UChar(buf[j]));
|
|
|
|
result = _nc_trace_bufcat(bufnum, temp);
|
|
|
|
}
|
2006-12-18 12:32:42 +08:00
|
|
|
#endif /* USE_WIDEC_SUPPORT */
|
2008-08-05 09:06:16 +08:00
|
|
|
}
|
|
|
|
result = _nc_trace_bufcat(bufnum, d_quote);
|
|
|
|
if (attr != A_NORMAL) {
|
|
|
|
result = _nc_trace_bufcat(bufnum, " | ");
|
|
|
|
result = _nc_trace_bufcat(bufnum, _traceattr2(bufnum + 20, attr));
|
|
|
|
}
|
|
|
|
result = _nc_trace_bufcat(bufnum, r_brace);
|
|
|
|
first = last + 1;
|
2002-10-13 11:35:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NCURSES_EXPORT(const char *)
|
2006-12-18 12:32:42 +08:00
|
|
|
_nc_viscbuf(const NCURSES_CH_T * buf, int len)
|
2002-10-13 11:35:53 +08:00
|
|
|
{
|
|
|
|
return _nc_viscbuf2(0, buf, len);
|
|
|
|
}
|
|
|
|
#endif /* TRACE */
|