X-Git-Url: https://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=ncurses%2Ftrace%2Fvisbuf.c;h=1093073f49be32e756b8ccfe4fa182a48abaece0;hp=afbb94298f453cd5bdf51ab1807374fa9cdb53d1;hb=ba78eb501a2ed01647123dbe7279ca0f38782e04;hpb=3996fe0bf797f113d6abc4329cc869951735a4d8 diff --git a/ncurses/trace/visbuf.c b/ncurses/trace/visbuf.c index afbb9429..1093073f 100644 --- a/ncurses/trace/visbuf.c +++ b/ncurses/trace/visbuf.c @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright (c) 2001-2008,2009 Free Software Foundation, Inc. * + * Copyright (c) 2001-2011,2012 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,7 +42,7 @@ #include #include -MODULE_ID("$Id: visbuf.c,v 1.35 2009/12/12 21:34:23 tom Exp $") +MODULE_ID("$Id: visbuf.c,v 1.42 2012/10/27 20:58:50 tom Exp $") #define NUM_VISBUFS 4 @@ -55,8 +55,16 @@ 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++ = '\\'; @@ -84,7 +92,8 @@ _nc_vischar(char *tp, unsigned c) *tp++ = '^'; *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; @@ -94,9 +103,10 @@ _nc_vischar(char *tp, unsigned c) static const char * _nc_visbuf2n(int bufnum, const char *buf, int len) { - const char *vbuf; + const char *vbuf = 0; char *tp; int c; + int count; if (buf == 0) return ("(null)"); @@ -106,6 +116,7 @@ _nc_visbuf2n(int bufnum, const char *buf, int len) if (len < 0) len = (int) strlen(buf); + count = len; #ifdef TRACE vbuf = tp = _nc_trace_buf(bufnum, NormalLen(len)); #else @@ -124,11 +135,11 @@ _nc_visbuf2n(int bufnum, const char *buf, int len) #endif if (tp != 0) { *tp++ = D_QUOTE; - while ((--len >= 0) && (c = *buf++) != '\0') { - tp = _nc_vischar(tp, UChar(c)); + while ((--count >= 0) && (c = *buf++) != '\0') { + tp = VisChar(tp, UChar(c), NormalLen(len)); } *tp++ = D_QUOTE; - *tp++ = '\0'; + *tp = '\0'; } else { vbuf = ("(_nc_visbuf2n failed)"); } @@ -175,6 +186,7 @@ _nc_viswbuf2n(int bufnum, const wchar_t *buf, int len) const char *vbuf; char *tp; wchar_t c; + int count; if (buf == 0) return ("(null)"); @@ -182,6 +194,7 @@ _nc_viswbuf2n(int bufnum, const wchar_t *buf, int len) if (len < 0) len = (int) wcslen(buf); + count = len; #ifdef TRACE vbuf = tp = _nc_trace_buf(bufnum, WideLen(len)); #else @@ -193,19 +206,20 @@ _nc_viswbuf2n(int bufnum, const wchar_t *buf, int len) #endif if (tp != 0) { *tp++ = D_QUOTE; - while ((--len >= 0) && (c = *buf++) != '\0') { + while ((--count >= 0) && (c = *buf++) != '\0') { char temp[CCHARW_MAX + 80]; int j = wctomb(temp, c), k; if (j <= 0) { - sprintf(temp, "\\u%08X", (unsigned) c); + _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) + "\\u%08X", (unsigned) c); j = (int) strlen(temp); } for (k = 0; k < j; ++k) { - tp = _nc_vischar(tp, UChar(temp[k])); + tp = VisChar(tp, UChar(temp[k]), WideLen(len)); } } *tp++ = D_QUOTE; - *tp++ = '\0'; + *tp = '\0'; } else { vbuf = ("(_nc_viswbuf2n failed)"); } @@ -248,10 +262,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'; } - mybuf[n] = L'\0'; return _nc_viswbuf2(0, mybuf); } @@ -261,7 +277,7 @@ _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); + char *result = _nc_trace_buf(bufnum, (size_t) BUFSIZ); int first; const char *found; @@ -287,12 +303,12 @@ _nc_viscbuf2(int bufnum, const NCURSES_CH_T * buf, int len) } } - result = _nc_trace_bufcat(bufnum, l_brace); - result = _nc_trace_bufcat(bufnum, d_quote); + (void) _nc_trace_bufcat(bufnum, l_brace); + (void) _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); + (void) _nc_trace_bufcat(bufnum, found); attr &= ~A_ALTCHARSET; } else #if USE_WIDEC_SUPPORT @@ -306,7 +322,7 @@ _nc_viscbuf2(int bufnum, const NCURSES_CH_T * buf, int len) PUTC_ch = buf[j].chars[PUTC_i]; if (PUTC_ch == L'\0') { if (PUTC_i == 0) - result = _nc_trace_bufcat(bufnum, "\\000"); + (void) _nc_trace_bufcat(bufnum, "\\000"); break; } PUTC_n = (int) wcrtomb(PUTC_buf, @@ -315,23 +331,23 @@ _nc_viscbuf2(int bufnum, const NCURSES_CH_T * buf, int len) break; for (k = 0; k < PUTC_n; k++) { char temp[80]; - _nc_vischar(temp, UChar(PUTC_buf[k])); - result = _nc_trace_bufcat(bufnum, temp); + 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); + VisChar(temp, UChar(buf[j]), sizeof(temp)); + (void) _nc_trace_bufcat(bufnum, temp); } #endif /* USE_WIDEC_SUPPORT */ } - result = _nc_trace_bufcat(bufnum, d_quote); + (void) _nc_trace_bufcat(bufnum, d_quote); if (attr != A_NORMAL) { - result = _nc_trace_bufcat(bufnum, " | "); - result = _nc_trace_bufcat(bufnum, _traceattr2(bufnum + 20, attr)); + (void) _nc_trace_bufcat(bufnum, " | "); + (void) _nc_trace_bufcat(bufnum, _traceattr2(bufnum + 20, attr)); } result = _nc_trace_bufcat(bufnum, r_brace); first = last + 1;