ncursesw-morphos/ncurses/trace/visbuf.c

269 lines
7.3 KiB
C
Raw Normal View History

2002-10-13 11:35:53 +08:00
/****************************************************************************
2004-02-09 10:15:26 +08:00
* Copyright (c) 2001-2003,2004 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. *
****************************************************************************/
/****************************************************************************
2004-02-09 10:15:26 +08:00
* Author: Thomas E. Dickey 1996-2004 *
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
*/
#include <curses.priv.h>
#include <tic.h>
#include <ctype.h>
2004-02-09 10:15:26 +08:00
MODULE_ID("$Id: visbuf.c,v 1.9 2004/02/03 01:16:37 tom Exp $")
2002-10-13 11:35:53 +08:00
static char *
_nc_vischar(char *tp, unsigned c)
{
if (c == '"' || c == '\\') {
*tp++ = '\\';
*tp++ = c;
} else if (is7bits(c) && (isgraph(c) || c == ' ')) {
*tp++ = c;
} 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';
} else if (is7bits(c) && iscntrl(UChar(c))) {
*tp++ = '\\';
*tp++ = '^';
*tp++ = '@' + c;
} else {
sprintf(tp, "\\%03lo", ChCharOf(c));
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)
{
char *vbuf;
char *tp;
int c;
if (buf == 0)
return ("(null)");
if (buf == CANCELLED_STRING)
return ("(cancelled)");
if (len < 0)
len = strlen(buf);
#ifdef TRACE
tp = vbuf = _nc_trace_buf(bufnum, (unsigned) (len * 4) + 5);
#else
{
static char *mybuf[2];
mybuf[bufnum] = typeRealloc(char, (unsigned) (len * 4) + 5, mybuf[bufnum]);
tp = vbuf = mybuf[bufnum];
}
#endif
*tp++ = D_QUOTE;
while ((--len >= 0) && (c = *buf++) != '\0') {
tp = _nc_vischar(tp, UChar(c));
}
*tp++ = D_QUOTE;
*tp++ = '\0';
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);
}
#if USE_WIDEC_SUPPORT
#ifdef TRACE
static const char *
_nc_viswbuf2n(int bufnum, const wchar_t * buf, int len)
{
char *vbuf;
char *tp;
wchar_t c;
if (buf == 0)
return ("(null)");
if (len < 0)
len = wcslen(buf);
#ifdef TRACE
tp = vbuf = _nc_trace_buf(bufnum, (unsigned) (len * 4) + 5);
#else
{
static char *mybuf[2];
mybuf[bufnum] = typeRealloc(char, (unsigned) (len * 4) + 5, mybuf[bufnum]);
tp = vbuf = mybuf[bufnum];
}
#endif
*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", (wint_t) c);
j = strlen(temp);
}
for (k = 0; k < j; ++k) {
tp = _nc_vischar(tp, temp[k]);
}
}
*tp++ = D_QUOTE;
*tp++ = '\0';
return (vbuf);
}
NCURSES_EXPORT(const char *)
_nc_viswbuf2(int bufnum, const wchar_t * buf)
{
return _nc_viswbuf2n(bufnum, buf, -1);
}
NCURSES_EXPORT(const char *)
_nc_viswbuf(const wchar_t * buf)
{
return _nc_viswbuf2(0, buf);
}
NCURSES_EXPORT(const char *)
_nc_viswbufn(const wchar_t * buf, int len)
{
return _nc_viswbuf2n(0, buf, len);
}
NCURSES_EXPORT(const char *)
_nc_viscbuf2(int bufnum, const cchar_t * buf, int len)
{
2004-02-09 10:15:26 +08:00
char *result = _nc_trace_buf(bufnum, BUFSIZ);
2002-10-13 11:35:53 +08:00
int n;
bool same = TRUE;
attr_t attr = A_NORMAL;
const char *found;
if (len < 0)
len = _nc_wchstrlen(buf);
for (n = 1; n < len; n++) {
if (AttrOf(buf[n]) != AttrOf(buf[0])) {
same = FALSE;
break;
}
}
/*
* If the rendition is the same for the whole string, display it as a
* quoted string, followed by the rendition. Otherwise, use the more
* detailed trace function that displays each character separately.
*/
if (same) {
2004-02-09 10:15:26 +08:00
static const char d_quote[] =
{D_QUOTE, 0};
result = _nc_trace_bufcat(bufnum, d_quote);
2002-10-13 11:35:53 +08:00
while (len-- > 0) {
if ((found = _nc_altcharset_name(attr, CharOfD(buf))) != 0) {
2004-02-09 10:15:26 +08:00
result = _nc_trace_bufcat(bufnum, found);
2002-10-13 11:35:53 +08:00
attr &= ~A_ALTCHARSET;
} else if (!isnac(CHDEREF(buf))) {
PUTC_DATA;
2004-02-09 10:15:26 +08:00
PUTC_INIT;
2002-10-13 11:35:53 +08:00
do {
PUTC_ch = PUTC_i < CCHARW_MAX ? buf->chars[PUTC_i] : L'\0';
PUTC_n = wcrtomb(PUTC_buf, buf->chars[PUTC_i], &PUT_st);
if (PUTC_ch == L'\0')
--PUTC_n;
if (PUTC_n <= 0)
break;
for (n = 0; n < PUTC_n; n++) {
2004-02-09 10:15:26 +08:00
char temp[80];
_nc_vischar(temp, UChar(PUTC_buf[n]));
result = _nc_trace_bufcat(bufnum, temp);
2002-10-13 11:35:53 +08:00
}
++PUTC_i;
} while (PUTC_ch != L'\0');
}
buf++;
}
2004-02-09 10:15:26 +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));
}
2002-10-13 11:35:53 +08:00
} else {
2004-02-09 10:15:26 +08:00
static const char l_brace[] =
{L_BRACE, 0};
static const char r_brace[] =
{R_BRACE, 0};
strcpy(result, l_brace);
2002-10-13 11:35:53 +08:00
while (len-- > 0) {
2004-02-09 10:15:26 +08:00
result = _nc_trace_bufcat(bufnum,
_tracecchar_t2(bufnum + 20, buf++));
2002-10-13 11:35:53 +08:00
}
2004-02-09 10:15:26 +08:00
result = _nc_trace_bufcat(bufnum, r_brace);
2002-10-13 11:35:53 +08:00
}
return result;
}
NCURSES_EXPORT(const char *)
_nc_viscbuf(const cchar_t * buf, int len)
{
return _nc_viscbuf2(0, buf, len);
}
#endif /* TRACE */
#endif /* USE_WIDEC_SUPPORT */