visbuf.c revision 97049
1/**************************************************************************** 2 * Copyright (c) 2001 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-2001 * 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.3 2001/11/10 23:47:51 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", ChCharOf(c)); 72 tp += strlen(tp); 73 } 74 return tp; 75} 76 77NCURSES_EXPORT(const char *) 78_nc_visbuf2(int bufnum, const char *buf) 79{ 80 char *vbuf; 81 char *tp; 82 int c; 83 84 if (buf == 0) 85 return ("(null)"); 86 if (buf == CANCELLED_STRING) 87 return ("(cancelled)"); 88 89#ifdef TRACE 90 tp = vbuf = _nc_trace_buf(bufnum, (strlen(buf) * 4) + 5); 91#else 92 { 93 static char *mybuf[2]; 94 mybuf[bufnum] = _nc_doalloc(mybuf[bufnum], (strlen(buf) * 4) + 5); 95 tp = vbuf = mybuf[bufnum]; 96 } 97#endif 98 *tp++ = D_QUOTE; 99 while ((c = *buf++) != '\0') { 100 tp = _nc_vischar(tp, UChar(c)); 101 } 102 *tp++ = D_QUOTE; 103 *tp++ = '\0'; 104 return (vbuf); 105} 106 107NCURSES_EXPORT(const char *) 108_nc_visbuf(const char *buf) 109{ 110 return _nc_visbuf2(0, buf); 111} 112 113#if USE_WIDEC_SUPPORT 114#ifdef TRACE 115NCURSES_EXPORT(const char *) 116_nc_viswbuf2(int bufnum, const wchar_t * buf) 117{ 118 char *vbuf; 119 char *tp; 120 int c; 121 122 if (buf == 0) 123 return ("(null)"); 124 125#ifdef TRACE 126 tp = vbuf = _nc_trace_buf(bufnum, (wcslen(buf) * 4) + 5); 127#else 128 { 129 static char *mybuf[2]; 130 mybuf[bufnum] = _nc_doalloc(mybuf[bufnum], (wcslen(buf) * 4) + 5); 131 tp = vbuf = mybuf[bufnum]; 132 } 133#endif 134 *tp++ = D_QUOTE; 135 while ((c = *buf++) != '\0') { 136 tp = _nc_vischar(tp, ChCharOf(c)); 137 } 138 *tp++ = D_QUOTE; 139 *tp++ = '\0'; 140 return (vbuf); 141} 142 143NCURSES_EXPORT(const char *) 144_nc_viswbuf(const wchar_t * buf) 145{ 146 return _nc_viswbuf2(0, buf); 147} 148 149NCURSES_EXPORT(const char *) 150_nc_viscbuf2(int bufnum, const cchar_t * buf, int len) 151{ 152 size_t have = BUFSIZ; 153 char *result = _nc_trace_buf(bufnum, have); 154 char *tp = result; 155 int n; 156 bool same = TRUE; 157 attr_t attr = A_NORMAL; 158 const char *found; 159 160 if (len < 0) 161 len = _nc_wchstrlen(buf); 162 163 for (n = 1; n < len; n++) { 164 if (AttrOf(buf[n]) != AttrOf(buf[0])) { 165 same = FALSE; 166 break; 167 } 168 } 169 170 /* 171 * If the rendition is the same for the whole string, display it as a 172 * quoted string, followed by the rendition. Otherwise, use the more 173 * detailed trace function that displays each character separately. 174 */ 175 if (same) { 176 *tp++ = D_QUOTE; 177 while (len-- > 0) { 178 if ((found = _nc_altcharset_name(attr, CharOfD(buf))) != 0) { 179 (void) strcpy(tp, found); 180 tp += strlen(tp); 181 attr &= ~A_ALTCHARSET; 182 } else if (!isnac(CHDEREF(buf))) { 183 PUTC_DATA; 184 185 memset(&PUT_st, '\0', sizeof(PUT_st)); 186 PUTC_i = 0; 187 do { 188 PUTC_ch = PUTC_i < CCHARW_MAX ? buf->chars[PUTC_i] : L'\0'; 189 PUTC_n = wcrtomb(PUTC_buf, buf->chars[PUTC_i], &PUT_st); 190 if (PUTC_ch == L'\0') 191 --PUTC_n; 192 if (PUTC_n <= 0) 193 break; 194 for (n = 0; n < PUTC_n; n++) { 195 tp = _nc_vischar(tp, UChar(PUTC_buf[n])); 196 } 197 ++PUTC_i; 198 } while (PUTC_ch != L'\0'); 199 } 200 buf++; 201 } 202 *tp++ = D_QUOTE; 203 *tp++ = '\0'; 204 if (attr != A_NORMAL) 205 (void) sprintf(tp, " | %s", 206 _traceattr2(bufnum + 20, attr)); 207 } else { 208 *tp++ = L_BRACE; 209 while (len-- > 0) { 210 char *temp = _tracecchar_t2(bufnum + 20, buf++); 211 size_t used = (tp - result); 212 size_t want = strlen(temp) + 5 + used; 213 if (want > have) { 214 result = _nc_trace_buf(bufnum, have = want); 215 tp = result + used; 216 } 217 (void) strcpy(tp, temp); 218 tp += strlen(tp); 219 } 220 *tp++ = R_BRACE; 221 *tp++ = '\0'; 222 } 223 return result; 224} 225 226NCURSES_EXPORT(const char *) 227_nc_viscbuf(const cchar_t * buf, int len) 228{ 229 return _nc_viscbuf2(0, buf, len); 230} 231#endif /* TRACE */ 232#endif /* USE_WIDEC_SUPPORT */ 233