1 /**************************************************************************** 2 * Copyright (c) 2001-2007,2008 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 #define NEED_NCURSES_CH_T 40 #include <curses.priv.h> 41 42 #include <tic.h> 43 #include <ctype.h> 44 45 MODULE_ID("$Id: visbuf.c,v 1.32 2008/08/04 23:07:39 tom Exp $") 46 47 #define NormalLen(len) (size_t) (((size_t)(len) + 1) * 4) 48 #define WideLen(len) (size_t) (((size_t)(len) + 1) * 4 * MB_CUR_MAX) 49 50 #ifdef TRACE 51 static const char d_quote[] = StringOf(D_QUOTE); 52 static const char l_brace[] = StringOf(L_BRACE); 53 static const char r_brace[] = StringOf(R_BRACE); 54 #endif 55 56 static char * 57 _nc_vischar(char *tp, unsigned c) 58 { 59 if (c == '"' || c == '\\') { 60 *tp++ = '\\'; 61 *tp++ = (char) c; 62 } else if (is7bits(c) && (isgraph(c) || c == ' ')) { 63 *tp++ = (char) c; 64 } else if (c == '\n') { 65 *tp++ = '\\'; 66 *tp++ = 'n'; 67 } else if (c == '\r') { 68 *tp++ = '\\'; 69 *tp++ = 'r'; 70 } else if (c == '\b') { 71 *tp++ = '\\'; 72 *tp++ = 'b'; 73 } else if (c == '\033') { 74 *tp++ = '\\'; 75 *tp++ = 'e'; 76 } else if (UChar(c) == 0x7f) { 77 *tp++ = '\\'; 78 *tp++ = '^'; 79 *tp++ = '?'; 80 } else if (is7bits(c) && iscntrl(UChar(c))) { 81 *tp++ = '\\'; 82 *tp++ = '^'; 83 *tp++ = (char) ('@' + c); 84 } else { 85 sprintf(tp, "\\%03lo", (unsigned long) ChCharOf(c)); 86 tp += strlen(tp); 87 } 88 *tp = 0; 89 return tp; 90 } 91 92 static const char * 93 _nc_visbuf2n(int bufnum, const char *buf, int len) 94 { 95 const char *vbuf; 96 char *tp; 97 int c; 98 99 if (buf == 0) 100 return ("(null)"); 101 if (buf == CANCELLED_STRING) 102 return ("(cancelled)"); 103 104 if (len < 0) 105 len = (int) strlen(buf); 106 107 #ifdef TRACE 108 vbuf = tp = _nc_trace_buf(bufnum, NormalLen(len)); 109 #else 110 { 111 static char *mybuf[4]; 112 mybuf[bufnum] = typeRealloc(char, NormalLen(len), mybuf[bufnum]); 113 vbuf = tp = mybuf[bufnum]; 114 } 115 #endif 116 if (tp != 0) { 117 *tp++ = D_QUOTE; 118 while ((--len >= 0) && (c = *buf++) != '\0') { 119 tp = _nc_vischar(tp, UChar(c)); 120 } 121 *tp++ = D_QUOTE; 122 *tp++ = '\0'; 123 } else { 124 vbuf = ("(_nc_visbuf2n failed)"); 125 } 126 return (vbuf); 127 } 128 129 NCURSES_EXPORT(const char *) 130 _nc_visbuf2(int bufnum, const char *buf) 131 { 132 return _nc_visbuf2n(bufnum, buf, -1); 133 } 134 135 NCURSES_EXPORT(const char *) 136 _nc_visbuf(const char *buf) 137 { 138 return _nc_visbuf2(0, buf); 139 } 140 141 NCURSES_EXPORT(const char *) 142 _nc_visbufn(const char *buf, int len) 143 { 144 return _nc_visbuf2n(0, buf, len); 145 } 146 147 #ifdef TRACE 148 #if USE_WIDEC_SUPPORT 149 150 #if defined(USE_TERMLIB) 151 #define _nc_wchstrlen _my_wchstrlen 152 static int 153 _nc_wchstrlen(const cchar_t *s) 154 { 155 int result = 0; 156 while (CharOf(s[result]) != L'\0') { 157 result++; 158 } 159 return result; 160 } 161 #endif 162 163 static const char * 164 _nc_viswbuf2n(int bufnum, const wchar_t *buf, int len) 165 { 166 const char *vbuf; 167 char *tp; 168 wchar_t c; 169 170 if (buf == 0) 171 return ("(null)"); 172 173 if (len < 0) 174 len = (int) wcslen(buf); 175 176 #ifdef TRACE 177 vbuf = tp = _nc_trace_buf(bufnum, WideLen(len)); 178 #else 179 { 180 static char *mybuf[2]; 181 mybuf[bufnum] = typeRealloc(char, WideLen(len), mybuf[bufnum]); 182 vbuf = tp = mybuf[bufnum]; 183 } 184 #endif 185 if (tp != 0) { 186 *tp++ = D_QUOTE; 187 while ((--len >= 0) && (c = *buf++) != '\0') { 188 char temp[CCHARW_MAX + 80]; 189 int j = wctomb(temp, c), k; 190 if (j <= 0) { 191 sprintf(temp, "\\u%08X", (unsigned) c); 192 j = (int) strlen(temp); 193 } 194 for (k = 0; k < j; ++k) { 195 tp = _nc_vischar(tp, UChar(temp[k])); 196 } 197 } 198 *tp++ = D_QUOTE; 199 *tp++ = '\0'; 200 } else { 201 vbuf = ("(_nc_viswbuf2n failed)"); 202 } 203 return (vbuf); 204 } 205 206 NCURSES_EXPORT(const char *) 207 _nc_viswbuf2(int bufnum, const wchar_t *buf) 208 { 209 return _nc_viswbuf2n(bufnum, buf, -1); 210 } 211 212 NCURSES_EXPORT(const char *) 213 _nc_viswbuf(const wchar_t *buf) 214 { 215 return _nc_viswbuf2(0, buf); 216 } 217 218 NCURSES_EXPORT(const char *) 219 _nc_viswbufn(const wchar_t *buf, int len) 220 { 221 return _nc_viswbuf2n(0, buf, len); 222 } 223 224 /* this special case is used for wget_wstr() */ 225 NCURSES_EXPORT(const char *) 226 _nc_viswibuf(const wint_t *buf) 227 { 228 static wchar_t *mybuf; 229 static unsigned mylen; 230 unsigned n; 231 232 for (n = 0; buf[n] != 0; ++n) ; 233 if (mylen < ++n) { 234 mylen = n + 80; 235 if (mybuf != 0) 236 mybuf = typeRealloc(wchar_t, mylen, mybuf); 237 else 238 mybuf = typeMalloc(wchar_t, mylen); 239 } 240 for (n = 0; buf[n] != 0; ++n) 241 mybuf[n] = (wchar_t) buf[n]; 242 243 return _nc_viswbuf2(0, mybuf); 244 } 245 #endif /* USE_WIDEC_SUPPORT */ 246 247 /* use these functions for displaying parts of a line within a window */ 248 NCURSES_EXPORT(const char *) 249 _nc_viscbuf2(int bufnum, const NCURSES_CH_T * buf, int len) 250 { 251 char *result = _nc_trace_buf(bufnum, BUFSIZ); 252 int first; 253 const char *found; 254 255 if (result != 0) { 256 #if USE_WIDEC_SUPPORT 257 if (len < 0) 258 len = _nc_wchstrlen(buf); 259 #endif /* USE_WIDEC_SUPPORT */ 260 261 /* 262 * Display one or more strings followed by attributes. 263 */ 264 first = 0; 265 while (first < len) { 266 attr_t attr = AttrOf(buf[first]); 267 int last = len - 1; 268 int j; 269 270 for (j = first + 1; j < len; ++j) { 271 if (!SameAttrOf(buf[j], buf[first])) { 272 last = j - 1; 273 break; 274 } 275 } 276 277 result = _nc_trace_bufcat(bufnum, l_brace); 278 result = _nc_trace_bufcat(bufnum, d_quote); 279 for (j = first; j <= last; ++j) { 280 found = _nc_altcharset_name(attr, (chtype) CharOf(buf[j])); 281 if (found != 0) { 282 result = _nc_trace_bufcat(bufnum, found); 283 attr &= ~A_ALTCHARSET; 284 } else 285 #if USE_WIDEC_SUPPORT 286 if (!isWidecExt(buf[j])) { 287 PUTC_DATA; 288 289 PUTC_INIT; 290 for (PUTC_i = 0; PUTC_i < CCHARW_MAX; ++PUTC_i) { 291 int k; 292 293 PUTC_ch = buf[j].chars[PUTC_i]; 294 if (PUTC_ch == L'\0') 295 break; 296 PUTC_n = (int) wcrtomb(PUTC_buf, buf[j].chars[PUTC_i], &PUT_st); 297 if (PUTC_n <= 0) 298 break; 299 for (k = 0; k < PUTC_n; k++) { 300 char temp[80]; 301 _nc_vischar(temp, UChar(PUTC_buf[k])); 302 result = _nc_trace_bufcat(bufnum, temp); 303 } 304 } 305 } 306 #else 307 { 308 char temp[80]; 309 _nc_vischar(temp, UChar(buf[j])); 310 result = _nc_trace_bufcat(bufnum, temp); 311 } 312 #endif /* USE_WIDEC_SUPPORT */ 313 } 314 result = _nc_trace_bufcat(bufnum, d_quote); 315 if (attr != A_NORMAL) { 316 result = _nc_trace_bufcat(bufnum, " | "); 317 result = _nc_trace_bufcat(bufnum, _traceattr2(bufnum + 20, attr)); 318 } 319 result = _nc_trace_bufcat(bufnum, r_brace); 320 first = last + 1; 321 } 322 } 323 return result; 324 } 325 326 NCURSES_EXPORT(const char *) 327 _nc_viscbuf(const NCURSES_CH_T * buf, int len) 328 { 329 return _nc_viscbuf2(0, buf, len); 330 } 331 #endif /* TRACE */ 332