X-Git-Url: https://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=ncurses%2Ftrace%2Fvisbuf.c;h=13023aaa1a82426e9f2c45e1a730299d7ff623e3;hp=0540ee6d8184d3d0ecdb01636897fc9cd808bd57;hb=5d8dbcdd9423bf9821db414fd9ec792ccf1f1027;hpb=027ae42953e3186daed8f3882da73de48291b606 diff --git a/ncurses/trace/visbuf.c b/ncurses/trace/visbuf.c index 0540ee6d..13023aaa 100644 --- a/ncurses/trace/visbuf.c +++ b/ncurses/trace/visbuf.c @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright (c) 2001-2005,2006 Free Software Foundation, Inc. * + * Copyright (c) 2001-2014,2016 Free Software Foundation, Inc. * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the * @@ -42,20 +42,35 @@ #include #include -MODULE_ID("$Id: visbuf.c,v 1.21 2006/12/02 21:20:28 tom Exp $") +MODULE_ID("$Id: visbuf.c,v 1.48 2016/09/05 00:43:08 tom Exp $") -static const char d_quote[] = {D_QUOTE, 0}; -static const char l_brace[] = {L_BRACE, 0}; -static const char r_brace[] = {R_BRACE, 0}; +#define NUM_VISBUFS 4 + +#define NormalLen(len) (size_t) (((size_t)(len) + 1) * 4) +#define WideLen(len) (size_t) (((size_t)(len) + 1) * 4 * (size_t) MB_CUR_MAX) + +#ifdef TRACE +static const char d_quote[] = StringOf(D_QUOTE); +static const char l_brace[] = StringOf(L_BRACE); +static const char r_brace[] = StringOf(R_BRACE); +#endif + +#if USE_STRING_HACKS && HAVE_SNPRINTF +#define VisChar(tp, chr, limit) _nc_vischar(tp, chr, limit) +#define LIMIT_ARG ,size_t limit +#else +#define VisChar(tp, chr, limit) _nc_vischar(tp, chr) +#define LIMIT_ARG /* nothing */ +#endif static char * -_nc_vischar(char *tp, unsigned c) +_nc_vischar(char *tp, unsigned c LIMIT_ARG) { if (c == '"' || c == '\\') { *tp++ = '\\'; - *tp++ = c; - } else if (is7bits(c) && (isgraph(c) || c == ' ')) { - *tp++ = c; + *tp++ = (char) c; + } else if (is7bits((int) c) && (isgraph((int) c) || c == ' ')) { + *tp++ = (char) c; } else if (c == '\n') { *tp++ = '\\'; *tp++ = 'n'; @@ -68,12 +83,17 @@ _nc_vischar(char *tp, unsigned c) } else if (c == '\033') { *tp++ = '\\'; *tp++ = 'e'; + } else if (UChar(c) == 0x7f) { + *tp++ = '\\'; + *tp++ = '^'; + *tp++ = '?'; } else if (is7bits(c) && iscntrl(UChar(c))) { *tp++ = '\\'; *tp++ = '^'; - *tp++ = '@' + c; + *tp++ = (char) ('@' + c); } else { - sprintf(tp, "\\%03lo", (unsigned long) ChCharOf(c)); + _nc_SPRINTF(tp, _nc_SLIMIT(limit) + "\\%03lo", (unsigned long) ChCharOf(c)); tp += strlen(tp); } *tp = 0; @@ -83,9 +103,9 @@ _nc_vischar(char *tp, unsigned c) static const char * _nc_visbuf2n(int bufnum, const char *buf, int len) { - char *vbuf; + const char *vbuf = 0; char *tp; - int c; + int count; if (buf == 0) return ("(null)"); @@ -93,23 +113,39 @@ _nc_visbuf2n(int bufnum, const char *buf, int len) return ("(cancelled)"); if (len < 0) - len = strlen(buf); + len = (int) strlen(buf); + count = len; #ifdef TRACE - tp = vbuf = _nc_trace_buf(bufnum, (unsigned) (len * 4) + 5); + vbuf = tp = _nc_trace_buf(bufnum, NormalLen(len)); #else { - static char *mybuf[2]; - mybuf[bufnum] = typeRealloc(char, (unsigned) (len * 4) + 5, mybuf[bufnum]); - tp = vbuf = mybuf[bufnum]; + static char *mybuf[NUM_VISBUFS]; + int c; + + 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]; + } } #endif - *tp++ = D_QUOTE; - while ((--len >= 0) && (c = *buf++) != '\0') { - tp = _nc_vischar(tp, UChar(c)); + if (tp != 0) { + int c; + + *tp++ = D_QUOTE; + while ((--count >= 0) && (c = *buf++) != '\0') { + tp = VisChar(tp, UChar(c), NormalLen(len)); + } + *tp++ = D_QUOTE; + *tp = '\0'; + } else { + vbuf = ("(_nc_visbuf2n failed)"); } - *tp++ = D_QUOTE; - *tp++ = '\0'; return (vbuf); } @@ -150,39 +186,47 @@ _nc_wchstrlen(const cchar_t *s) static const char * _nc_viswbuf2n(int bufnum, const wchar_t *buf, int len) { - char *vbuf; + const char *vbuf; char *tp; - wchar_t c; + int count; if (buf == 0) return ("(null)"); if (len < 0) - len = wcslen(buf); + len = (int) wcslen(buf); + count = len; #ifdef TRACE - tp = vbuf = _nc_trace_buf(bufnum, (unsigned) (len * 4) + 5); + vbuf = tp = _nc_trace_buf(bufnum, WideLen(len)); #else { - static char *mybuf[2]; - mybuf[bufnum] = typeRealloc(char, (unsigned) (len * 4) + 5, mybuf[bufnum]); - tp = vbuf = mybuf[bufnum]; + static char *mybuf[NUM_VISBUFS]; + mybuf[bufnum] = typeRealloc(char, WideLen(len), mybuf[bufnum]); + vbuf = tp = 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, UChar(temp[k])); + if (tp != 0) { + wchar_t c; + + *tp++ = D_QUOTE; + while ((--count >= 0) && (c = *buf++) != '\0') { + char temp[CCHARW_MAX + 80]; + int j = wctomb(temp, c), k; + if (j <= 0) { + _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) + "\\u%08X", (unsigned) c); + j = (int) strlen(temp); + } + for (k = 0; k < j; ++k) { + tp = VisChar(tp, UChar(temp[k]), WideLen(len)); + } } + *tp++ = D_QUOTE; + *tp = '\0'; + } else { + vbuf = ("(_nc_viswbuf2n failed)"); } - *tp++ = D_QUOTE; - *tp++ = '\0'; return (vbuf); } @@ -212,7 +256,9 @@ _nc_viswibuf(const wint_t *buf) static unsigned mylen; unsigned n; - for (n = 0; buf[n] != 0; ++n) ; + for (n = 0; buf[n] != 0; ++n) { + ; /* empty */ + } if (mylen < ++n) { mylen = n + 80; if (mybuf != 0) @@ -220,8 +266,12 @@ _nc_viswibuf(const wint_t *buf) else mybuf = typeMalloc(wchar_t, mylen); } - for (n = 0; buf[n] != 0; ++n) - mybuf[n] = (wchar_t) buf[n]; + if (mybuf != 0) { + for (n = 0; buf[n] != 0; ++n) { + mybuf[n] = (wchar_t) buf[n]; + } + mybuf[n] = L'\0'; + } return _nc_viswbuf2(0, mybuf); } @@ -231,74 +281,87 @@ _nc_viswibuf(const wint_t *buf) NCURSES_EXPORT(const char *) _nc_viscbuf2(int bufnum, const NCURSES_CH_T * buf, int len) { - char *result = _nc_trace_buf(bufnum, BUFSIZ); - int first; - const char *found; + char *result = _nc_trace_buf(bufnum, (size_t) BUFSIZ); + + if (result != 0) { + int first = 0; #if USE_WIDEC_SUPPORT - if (len < 0) - len = _nc_wchstrlen(buf); + if (len < 0) + len = _nc_wchstrlen(buf); #endif /* USE_WIDEC_SUPPORT */ - /* - * 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; + /* + * Display one or more strings followed by attributes. + */ + 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; + } } - } - result = _nc_trace_bufcat(bufnum, l_brace); - result = _nc_trace_bufcat(bufnum, d_quote); - for (j = first; j <= last; ++j) { - if ((found = _nc_altcharset_name(attr, (chtype) CharOf(buf[j]))) != 0) { - result = _nc_trace_bufcat(bufnum, found); - attr &= ~A_ALTCHARSET; - } else + (void) _nc_trace_bufcat(bufnum, l_brace); + (void) _nc_trace_bufcat(bufnum, d_quote); + for (j = first; j <= last; ++j) { + const char *found = _nc_altcharset_name(attr, (chtype) + CharOf(buf[j])); + if (found != 0) { + (void) _nc_trace_bufcat(bufnum, found); + attr &= ~A_ALTCHARSET; + } else #if USE_WIDEC_SUPPORT - 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; - PUTC_n = wcrtomb(PUTC_buf, buf[j].chars[PUTC_i], &PUT_st); - if (PUTC_n <= 0) - break; - for (k = 0; k < PUTC_n; k++) { + if (!isWidecExt(buf[j])) { + PUTC_DATA; + + for (PUTC_i = 0; PUTC_i < CCHARW_MAX; ++PUTC_i) { + int k; char temp[80]; - _nc_vischar(temp, UChar(PUTC_buf[k])); - result = _nc_trace_bufcat(bufnum, temp); + + PUTC_ch = buf[j].chars[PUTC_i]; + if (PUTC_ch == L'\0') { + if (PUTC_i == 0) + (void) _nc_trace_bufcat(bufnum, "\\000"); + break; + } + PUTC_INIT; + PUTC_n = (int) wcrtomb(PUTC_buf, + buf[j].chars[PUTC_i], &PUT_st); + if (PUTC_n <= 0 || buf[j].chars[PUTC_i] > 255) { + _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) + "{%d:\\u%x}", + wcwidth(buf[j].chars[PUTC_i]), + buf[j].chars[PUTC_i]); + (void) _nc_trace_bufcat(bufnum, temp); + break; + } + for (k = 0; k < PUTC_n; k++) { + VisChar(temp, UChar(PUTC_buf[k]), sizeof(temp)); + (void) _nc_trace_bufcat(bufnum, temp); + } } } - } #else - { - char temp[80]; - _nc_vischar(temp, UChar(buf[j])); - result = _nc_trace_bufcat(bufnum, temp); - } + { + char temp[80]; + VisChar(temp, UChar(buf[j]), sizeof(temp)); + (void) _nc_trace_bufcat(bufnum, temp); + } #endif /* USE_WIDEC_SUPPORT */ + } + (void) _nc_trace_bufcat(bufnum, d_quote); + if (attr != A_NORMAL) { + (void) _nc_trace_bufcat(bufnum, " | "); + (void) _nc_trace_bufcat(bufnum, _traceattr2(bufnum + 20, attr)); + } + result = _nc_trace_bufcat(bufnum, r_brace); + first = last + 1; } - 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; } return result; }