Back to index

tetex-bin  3.0
visbuf.c
Go to the documentation of this file.
00001 /****************************************************************************
00002  * Copyright (c) 2001-2003,2004 Free Software Foundation, Inc.              *
00003  *                                                                          *
00004  * Permission is hereby granted, free of charge, to any person obtaining a  *
00005  * copy of this software and associated documentation files (the            *
00006  * "Software"), to deal in the Software without restriction, including      *
00007  * without limitation the rights to use, copy, modify, merge, publish,      *
00008  * distribute, distribute with modifications, sublicense, and/or sell       *
00009  * copies of the Software, and to permit persons to whom the Software is    *
00010  * furnished to do so, subject to the following conditions:                 *
00011  *                                                                          *
00012  * The above copyright notice and this permission notice shall be included  *
00013  * in all copies or substantial portions of the Software.                   *
00014  *                                                                          *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
00016  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
00017  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
00018  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
00019  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
00020  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
00021  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
00022  *                                                                          *
00023  * Except as contained in this notice, the name(s) of the above copyright   *
00024  * holders shall not be used in advertising or otherwise to promote the     *
00025  * sale, use or other dealings in this Software without prior written       *
00026  * authorization.                                                           *
00027  ****************************************************************************/
00028 
00029 /****************************************************************************
00030  *  Author: Thomas E. Dickey 1996-2004                                      *
00031  *     and: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
00032  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
00033  ****************************************************************************/
00034 
00035 /*
00036  *     visbuf.c - Tracing/Debugging support routines
00037  */
00038 
00039 #include <curses.priv.h>
00040 
00041 #include <tic.h>
00042 #include <ctype.h>
00043 
00044 MODULE_ID("$Id: visbuf.c,v 1.10 2004/09/25 20:56:22 tom Exp $")
00045 
00046 static char *
00047 _nc_vischar(char *tp, unsigned c)
00048 {
00049     if (c == '"' || c == '\\') {
00050        *tp++ = '\\';
00051        *tp++ = c;
00052     } else if (is7bits(c) && (isgraph(c) || c == ' ')) {
00053        *tp++ = c;
00054     } else if (c == '\n') {
00055        *tp++ = '\\';
00056        *tp++ = 'n';
00057     } else if (c == '\r') {
00058        *tp++ = '\\';
00059        *tp++ = 'r';
00060     } else if (c == '\b') {
00061        *tp++ = '\\';
00062        *tp++ = 'b';
00063     } else if (c == '\033') {
00064        *tp++ = '\\';
00065        *tp++ = 'e';
00066     } else if (is7bits(c) && iscntrl(UChar(c))) {
00067        *tp++ = '\\';
00068        *tp++ = '^';
00069        *tp++ = '@' + c;
00070     } else {
00071        sprintf(tp, "\\%03lo", ChCharOf(c));
00072        tp += strlen(tp);
00073     }
00074     *tp = 0;
00075     return tp;
00076 }
00077 
00078 static const char *
00079 _nc_visbuf2n(int bufnum, const char *buf, int len)
00080 {
00081     char *vbuf;
00082     char *tp;
00083     int c;
00084 
00085     if (buf == 0)
00086        return ("(null)");
00087     if (buf == CANCELLED_STRING)
00088        return ("(cancelled)");
00089 
00090     if (len < 0)
00091        len = strlen(buf);
00092 
00093 #ifdef TRACE
00094     tp = vbuf = _nc_trace_buf(bufnum, (unsigned) (len * 4) + 5);
00095 #else
00096     {
00097        static char *mybuf[2];
00098        mybuf[bufnum] = typeRealloc(char, (unsigned) (len * 4) + 5, mybuf[bufnum]);
00099        tp = vbuf = mybuf[bufnum];
00100     }
00101 #endif
00102     *tp++ = D_QUOTE;
00103     while ((--len >= 0) && (c = *buf++) != '\0') {
00104        tp = _nc_vischar(tp, UChar(c));
00105     }
00106     *tp++ = D_QUOTE;
00107     *tp++ = '\0';
00108     return (vbuf);
00109 }
00110 
00111 NCURSES_EXPORT(const char *)
00112 _nc_visbuf2(int bufnum, const char *buf)
00113 {
00114     return _nc_visbuf2n(bufnum, buf, -1);
00115 }
00116 
00117 NCURSES_EXPORT(const char *)
00118 _nc_visbuf(const char *buf)
00119 {
00120     return _nc_visbuf2(0, buf);
00121 }
00122 
00123 NCURSES_EXPORT(const char *)
00124 _nc_visbufn(const char *buf, int len)
00125 {
00126     return _nc_visbuf2n(0, buf, len);
00127 }
00128 
00129 #if USE_WIDEC_SUPPORT
00130 #ifdef TRACE
00131 static const char *
00132 _nc_viswbuf2n(int bufnum, const wchar_t * buf, int len)
00133 {
00134     char *vbuf;
00135     char *tp;
00136     wchar_t c;
00137 
00138     if (buf == 0)
00139        return ("(null)");
00140 
00141     if (len < 0)
00142        len = wcslen(buf);
00143 
00144 #ifdef TRACE
00145     tp = vbuf = _nc_trace_buf(bufnum, (unsigned) (len * 4) + 5);
00146 #else
00147     {
00148        static char *mybuf[2];
00149        mybuf[bufnum] = typeRealloc(char, (unsigned) (len * 4) + 5, mybuf[bufnum]);
00150        tp = vbuf = mybuf[bufnum];
00151     }
00152 #endif
00153     *tp++ = D_QUOTE;
00154     while ((--len >= 0) && (c = *buf++) != '\0') {
00155        char temp[CCHARW_MAX + 80];
00156        int j = wctomb(temp, c), k;
00157        if (j <= 0) {
00158            sprintf(temp, "\\u%08X", (wint_t) c);
00159            j = strlen(temp);
00160        }
00161        for (k = 0; k < j; ++k) {
00162            tp = _nc_vischar(tp, temp[k]);
00163        }
00164     }
00165     *tp++ = D_QUOTE;
00166     *tp++ = '\0';
00167     return (vbuf);
00168 }
00169 
00170 NCURSES_EXPORT(const char *)
00171 _nc_viswbuf2(int bufnum, const wchar_t * buf)
00172 {
00173     return _nc_viswbuf2n(bufnum, buf, -1);
00174 }
00175 
00176 NCURSES_EXPORT(const char *)
00177 _nc_viswbuf(const wchar_t * buf)
00178 {
00179     return _nc_viswbuf2(0, buf);
00180 }
00181 
00182 NCURSES_EXPORT(const char *)
00183 _nc_viswbufn(const wchar_t * buf, int len)
00184 {
00185     return _nc_viswbuf2n(0, buf, len);
00186 }
00187 
00188 /* this special case is used for wget_wstr() */
00189 NCURSES_EXPORT(const char *)
00190 _nc_viswibuf(const wint_t * buf)
00191 {
00192     static wchar_t *mybuf;
00193     static unsigned mylen;
00194     unsigned n;
00195 
00196     for (n = 0; buf[n] != 0; ++n) ;
00197     if (mylen < ++n) {
00198        mylen = n + 80;
00199        if (mybuf != 0)
00200            mybuf = typeRealloc(wchar_t, mylen, mybuf);
00201        else
00202            mybuf = typeMalloc(wchar_t, mylen);
00203     }
00204     for (n = 0; buf[n] != 0; ++n)
00205        mybuf[n] = (wchar_t) buf[n];
00206 
00207     return _nc_viswbuf2(0, mybuf);
00208 }
00209 
00210 NCURSES_EXPORT(const char *)
00211 _nc_viscbuf2(int bufnum, const cchar_t * buf, int len)
00212 {
00213     char *result = _nc_trace_buf(bufnum, BUFSIZ);
00214     int n;
00215     bool same = TRUE;
00216     attr_t attr = A_NORMAL;
00217     const char *found;
00218 
00219     if (len < 0)
00220        len = _nc_wchstrlen(buf);
00221 
00222     for (n = 1; n < len; n++) {
00223        if (AttrOf(buf[n]) != AttrOf(buf[0])) {
00224            same = FALSE;
00225            break;
00226        }
00227     }
00228 
00229     /*
00230      * If the rendition is the same for the whole string, display it as a
00231      * quoted string, followed by the rendition.  Otherwise, use the more
00232      * detailed trace function that displays each character separately.
00233      */
00234     if (same) {
00235        static const char d_quote[] =
00236        {D_QUOTE, 0};
00237 
00238        result = _nc_trace_bufcat(bufnum, d_quote);
00239        while (len-- > 0) {
00240            if ((found = _nc_altcharset_name(attr, CharOfD(buf))) != 0) {
00241               result = _nc_trace_bufcat(bufnum, found);
00242               attr &= ~A_ALTCHARSET;
00243            } else if (!isnac(CHDEREF(buf))) {
00244               PUTC_DATA;
00245 
00246               PUTC_INIT;
00247               do {
00248                   PUTC_ch = PUTC_i < CCHARW_MAX ? buf->chars[PUTC_i] : L'\0';
00249                   PUTC_n = wcrtomb(PUTC_buf, buf->chars[PUTC_i], &PUT_st);
00250                   if (PUTC_ch == L'\0')
00251                      --PUTC_n;
00252                   if (PUTC_n <= 0)
00253                      break;
00254                   for (n = 0; n < PUTC_n; n++) {
00255                      char temp[80];
00256                      _nc_vischar(temp, UChar(PUTC_buf[n]));
00257                      result = _nc_trace_bufcat(bufnum, temp);
00258                   }
00259                   ++PUTC_i;
00260               } while (PUTC_ch != L'\0');
00261            }
00262            buf++;
00263        }
00264        result = _nc_trace_bufcat(bufnum, d_quote);
00265        if (attr != A_NORMAL) {
00266            result = _nc_trace_bufcat(bufnum, " | ");
00267            result = _nc_trace_bufcat(bufnum, _traceattr2(bufnum + 20, attr));
00268        }
00269     } else {
00270        static const char l_brace[] =
00271        {L_BRACE, 0};
00272        static const char r_brace[] =
00273        {R_BRACE, 0};
00274        strcpy(result, l_brace);
00275        while (len-- > 0) {
00276            result = _nc_trace_bufcat(bufnum,
00277                                   _tracecchar_t2(bufnum + 20, buf++));
00278        }
00279        result = _nc_trace_bufcat(bufnum, r_brace);
00280     }
00281     return result;
00282 }
00283 
00284 NCURSES_EXPORT(const char *)
00285 _nc_viscbuf(const cchar_t * buf, int len)
00286 {
00287     return _nc_viscbuf2(0, buf, len);
00288 }
00289 #endif /* TRACE */
00290 #endif /* USE_WIDEC_SUPPORT */