1/**************************************************************************** 2 * Copyright (c) 2001-2004,2005 Free Software Foundation, Inc. * 3 * * 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: * 11 * * 12 * The above copyright notice and this permission notice shall be included * 13 * in all copies or substantial portions of the Software. * 14 * * 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. * 22 * * 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 * 26 * authorization. * 27 ****************************************************************************/ 28 29/**************************************************************************** 30 * Author: Thomas E. Dickey 1996-on * 31 * and: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * 32 * and: Eric S. Raymond <esr@snark.thyrsus.com> * 33 ****************************************************************************/ 34 35/* 36 * visbuf.c - Tracing/Debugging support routines 37 */ 38 39#include <curses.priv.h> 40 41#include <tic.h> 42#include <ctype.h> 43 44MODULE_ID("$Id: visbuf.c,v 1.14 2005/08/20 20:01:20 tom Exp $") 45 46static char * 47_nc_vischar(char *tp, unsigned c) 48{ 49 if (c == '"' || c == '\\') { 50 *tp++ = '\\'; 51 *tp++ = c; 52 } else if (is7bits(c) && (isgraph(c) || c == ' ')) { 53 *tp++ = c; 54 } else if (c == '\n') { 55 *tp++ = '\\'; 56 *tp++ = 'n'; 57 } else if (c == '\r') { 58 *tp++ = '\\'; 59 *tp++ = 'r'; 60 } else if (c == '\b') { 61 *tp++ = '\\'; 62 *tp++ = 'b'; 63 } else if (c == '\033') { 64 *tp++ = '\\'; 65 *tp++ = 'e'; 66 } else if (is7bits(c) && iscntrl(UChar(c))) { 67 *tp++ = '\\'; 68 *tp++ = '^'; 69 *tp++ = '@' + c; 70 } else { 71 sprintf(tp, "\\%03lo", (unsigned long) ChCharOf(c)); 72 tp += strlen(tp); 73 } 74 *tp = 0; 75 return tp; 76} 77 78static const char * 79_nc_visbuf2n(int bufnum, const char *buf, int len) 80{ 81 char *vbuf; 82 char *tp; 83 int c; 84 85 if (buf == 0) 86 return ("(null)"); 87 if (buf == CANCELLED_STRING) 88 return ("(cancelled)"); 89 90 if (len < 0) 91 len = strlen(buf); 92 93#ifdef TRACE 94 tp = vbuf = _nc_trace_buf(bufnum, (unsigned) (len * 4) + 5); 95#else 96 { 97 static char *mybuf[2]; 98 mybuf[bufnum] = typeRealloc(char, (unsigned) (len * 4) + 5, mybuf[bufnum]); 99 tp = vbuf = mybuf[bufnum]; 100 } 101#endif 102 *tp++ = D_QUOTE; 103 while ((--len >= 0) && (c = *buf++) != '\0') { 104 tp = _nc_vischar(tp, UChar(c)); 105 } 106 *tp++ = D_QUOTE; 107 *tp++ = '\0'; 108 return (vbuf); 109} 110 111NCURSES_EXPORT(const char *) 112_nc_visbuf2(int bufnum, const char *buf) 113{ 114 return _nc_visbuf2n(bufnum, buf, -1); 115} 116 117NCURSES_EXPORT(const char *) 118_nc_visbuf(const char *buf) 119{ 120 return _nc_visbuf2(0, buf); 121} 122 123NCURSES_EXPORT(const char *) 124_nc_visbufn(const char *buf, int len) 125{ 126 return _nc_visbuf2n(0, buf, len); 127} 128 129#if USE_WIDEC_SUPPORT 130#ifdef TRACE 131 132#if defined(USE_TERMLIB) 133#define _nc_wchstrlen _my_wchstrlen 134static int 135_nc_wchstrlen(const cchar_t *s) 136{ 137 int result = 0; 138 while (CharOf(s[result]) != L'\0') { 139 result++; 140 } 141 return result; 142} 143#endif 144 145static const char * 146_nc_viswbuf2n(int bufnum, const wchar_t *buf, int len) 147{ 148 char *vbuf; 149 char *tp; 150 wchar_t c; 151 152 if (buf == 0) 153 return ("(null)"); 154 155 if (len < 0) 156 len = wcslen(buf); 157 158#ifdef TRACE 159 tp = vbuf = _nc_trace_buf(bufnum, (unsigned) (len * 4) + 5); 160#else 161 { 162 static char *mybuf[2]; 163 mybuf[bufnum] = typeRealloc(char, (unsigned) (len * 4) + 5, mybuf[bufnum]); 164 tp = vbuf = mybuf[bufnum]; 165 } 166#endif 167 *tp++ = D_QUOTE; 168 while ((--len >= 0) && (c = *buf++) != '\0') { 169 char temp[CCHARW_MAX + 80]; 170 int j = wctomb(temp, c), k; 171 if (j <= 0) { 172 sprintf(temp, "\\u%08X", (wint_t) c); 173 j = strlen(temp); 174 } 175 for (k = 0; k < j; ++k) { 176 tp = _nc_vischar(tp, temp[k]); 177 } 178 } 179 *tp++ = D_QUOTE; 180 *tp++ = '\0'; 181 return (vbuf); 182} 183 184NCURSES_EXPORT(const char *) 185_nc_viswbuf2(int bufnum, const wchar_t *buf) 186{ 187 return _nc_viswbuf2n(bufnum, buf, -1); 188} 189 190NCURSES_EXPORT(const char *) 191_nc_viswbuf(const wchar_t *buf) 192{ 193 return _nc_viswbuf2(0, buf); 194} 195 196NCURSES_EXPORT(const char *) 197_nc_viswbufn(const wchar_t *buf, int len) 198{ 199 return _nc_viswbuf2n(0, buf, len); 200} 201 202/* this special case is used for wget_wstr() */ 203NCURSES_EXPORT(const char *) 204_nc_viswibuf(const wint_t *buf) 205{ 206 static wchar_t *mybuf; 207 static unsigned mylen; 208 unsigned n; 209 210 for (n = 0; buf[n] != 0; ++n) ; 211 if (mylen < ++n) { 212 mylen = n + 80; 213 if (mybuf != 0) 214 mybuf = typeRealloc(wchar_t, mylen, mybuf); 215 else 216 mybuf = typeMalloc(wchar_t, mylen); 217 } 218 for (n = 0; buf[n] != 0; ++n) 219 mybuf[n] = (wchar_t) buf[n]; 220 221 return _nc_viswbuf2(0, mybuf); 222} 223 224NCURSES_EXPORT(const char *) 225_nc_viscbuf2(int bufnum, const cchar_t *buf, int len) 226{ 227 char *result = _nc_trace_buf(bufnum, BUFSIZ); 228 int n; 229 bool same = TRUE; 230 attr_t attr = A_NORMAL; 231 const char *found; 232 233 if (len < 0) 234 len = _nc_wchstrlen(buf); 235 236 for (n = 1; n < len; n++) { 237 if (!SameAttrOf(buf[n], buf[0])) { 238 same = FALSE; 239 break; 240 } 241 } 242 243 /* 244 * If the rendition is the same for the whole string, display it as a 245 * quoted string, followed by the rendition. Otherwise, use the more 246 * detailed trace function that displays each character separately. 247 */ 248 if (same) { 249 static const char d_quote[] = 250 {D_QUOTE, 0}; 251 252 result = _nc_trace_bufcat(bufnum, d_quote); 253 while (len-- > 0) { 254 if ((found = _nc_altcharset_name(attr, CharOfD(buf))) != 0) { 255 result = _nc_trace_bufcat(bufnum, found); 256 attr &= ~A_ALTCHARSET; 257 } else if (!isWidecExt(CHDEREF(buf))) { 258 PUTC_DATA; 259 260 PUTC_INIT; 261 do { 262 PUTC_ch = PUTC_i < CCHARW_MAX ? buf->chars[PUTC_i] : L'\0'; 263 PUTC_n = wcrtomb(PUTC_buf, buf->chars[PUTC_i], &PUT_st); 264 if (PUTC_ch == L'\0') 265 --PUTC_n; 266 if (PUTC_n <= 0) 267 break; 268 for (n = 0; n < PUTC_n; n++) { 269 char temp[80]; 270 _nc_vischar(temp, UChar(PUTC_buf[n])); 271 result = _nc_trace_bufcat(bufnum, temp); 272 } 273 ++PUTC_i; 274 } while (PUTC_ch != L'\0'); 275 } 276 buf++; 277 } 278 result = _nc_trace_bufcat(bufnum, d_quote); 279 if (attr != A_NORMAL) { 280 result = _nc_trace_bufcat(bufnum, " | "); 281 result = _nc_trace_bufcat(bufnum, _traceattr2(bufnum + 20, attr)); 282 } 283 } else { 284 static const char l_brace[] = 285 {L_BRACE, 0}; 286 static const char r_brace[] = 287 {R_BRACE, 0}; 288 strcpy(result, l_brace); 289 while (len-- > 0) { 290 result = _nc_trace_bufcat(bufnum, 291 _tracecchar_t2(bufnum + 20, buf++)); 292 } 293 result = _nc_trace_bufcat(bufnum, r_brace); 294 } 295 return result; 296} 297 298NCURSES_EXPORT(const char *) 299_nc_viscbuf(const cchar_t *buf, int len) 300{ 301 return _nc_viscbuf2(0, buf, len); 302} 303#endif /* TRACE */ 304#endif /* USE_WIDEC_SUPPORT */ 305