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