Back to index

tetex-bin  3.0
lib_traceatr.c
Go to the documentation of this file.
00001 /****************************************************************************
00002  * Copyright (c) 1998-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 Dickey                           1996-on                 *
00031  *     and: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
00032  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
00033  ****************************************************************************/
00034 
00035 /*
00036  *     lib_traceatr.c - Tracing/Debugging routines (attributes)
00037  */
00038 
00039 #include <curses.priv.h>
00040 #include <term.h>           /* acs_chars */
00041 
00042 MODULE_ID("$Id: lib_traceatr.c,v 1.49 2004/10/16 21:43:13 tom Exp $")
00043 
00044 #define COLOR_OF(c) (c < 0 || c > 7 ? "default" : colors[c].name)
00045 
00046 #ifdef TRACE
00047 
00048 static const char l_brace[] = {L_BRACE, 0};
00049 static const char r_brace[] = {R_BRACE, 0};
00050 
00051 NCURSES_EXPORT(char *)
00052 _traceattr2(int bufnum, attr_t newmode)
00053 {
00054     char *buf = _nc_trace_buf(bufnum, BUFSIZ);
00055     char temp[80];
00056     static const struct {
00057        unsigned int val;
00058        const char *name;
00059     } names[] =
00060     {
00061        /* *INDENT-OFF* */
00062        { A_STANDOUT,        "A_STANDOUT" },
00063        { A_UNDERLINE,              "A_UNDERLINE" },
00064        { A_REVERSE,         "A_REVERSE" },
00065        { A_BLINK,           "A_BLINK" },
00066        { A_DIM,             "A_DIM" },
00067        { A_BOLD,            "A_BOLD" },
00068        { A_ALTCHARSET,             "A_ALTCHARSET" },
00069        { A_INVIS,           "A_INVIS" },
00070        { A_PROTECT,         "A_PROTECT" },
00071        { A_CHARTEXT,        "A_CHARTEXT" },
00072        { A_NORMAL,          "A_NORMAL" },
00073        { A_COLOR,           "A_COLOR" },
00074        /* *INDENT-ON* */
00075 
00076     },
00077        colors[] =
00078     {
00079        /* *INDENT-OFF* */
00080        { COLOR_BLACK,              "COLOR_BLACK" },
00081        { COLOR_RED,         "COLOR_RED" },
00082        { COLOR_GREEN,              "COLOR_GREEN" },
00083        { COLOR_YELLOW,             "COLOR_YELLOW" },
00084        { COLOR_BLUE,        "COLOR_BLUE" },
00085        { COLOR_MAGENTA,     "COLOR_MAGENTA" },
00086        { COLOR_CYAN,        "COLOR_CYAN" },
00087        { COLOR_WHITE,              "COLOR_WHITE" },
00088        /* *INDENT-ON* */
00089 
00090     };
00091     size_t n;
00092     unsigned save_nc_tracing = _nc_tracing;
00093     _nc_tracing = 0;
00094 
00095     strcpy(buf, l_brace);
00096 
00097     for (n = 0; n < SIZEOF(names); n++) {
00098        if ((newmode & names[n].val) != 0) {
00099            if (buf[1] != '\0')
00100               buf = _nc_trace_bufcat(bufnum, "|");
00101            buf = _nc_trace_bufcat(bufnum, names[n].name);
00102 
00103            if (names[n].val == A_COLOR) {
00104               short pairnum = PAIR_NUMBER(newmode);
00105               short fg, bg;
00106 
00107               if (pair_content(pairnum, &fg, &bg) == OK) {
00108                   (void) sprintf(temp,
00109                                "{%d = {%s, %s}}",
00110                                pairnum,
00111                                COLOR_OF(fg),
00112                                COLOR_OF(bg));
00113               } else {
00114                   (void) sprintf(temp, "{%d}", pairnum);
00115               }
00116               buf = _nc_trace_bufcat(bufnum, temp);
00117            }
00118        }
00119     }
00120     if (ChAttrOf(newmode) == A_NORMAL) {
00121        if (buf[1] != '\0')
00122            (void) _nc_trace_bufcat(bufnum, "|");
00123        (void) _nc_trace_bufcat(bufnum, "A_NORMAL");
00124     }
00125 
00126     _nc_tracing = save_nc_tracing;
00127     return (_nc_trace_bufcat(bufnum, r_brace));
00128 }
00129 
00130 NCURSES_EXPORT(char *)
00131 _traceattr(attr_t newmode)
00132 {
00133     return _traceattr2(0, newmode);
00134 }
00135 
00136 /* Trace 'int' return-values */
00137 NCURSES_EXPORT(attr_t)
00138 _nc_retrace_attr_t(attr_t code)
00139 {
00140     T((T_RETURN("%s"), _traceattr(code)));
00141     return code;
00142 }
00143 
00144 const char *
00145 _nc_altcharset_name(attr_t attr, chtype ch)
00146 {
00147     const char *result = 0;
00148 
00149     if ((attr & A_ALTCHARSET) && (acs_chars != 0)) {
00150        char *cp;
00151        char *found = 0;
00152        static const struct {
00153            unsigned int val;
00154            const char *name;
00155        } names[] =
00156        {
00157            /* *INDENT-OFF* */
00158            { 'l', "ACS_ULCORNER" },       /* upper left corner */
00159            { 'm', "ACS_LLCORNER" },       /* lower left corner */
00160            { 'k', "ACS_URCORNER" },       /* upper right corner */
00161            { 'j', "ACS_LRCORNER" },       /* lower right corner */
00162            { 't', "ACS_LTEE" },    /* tee pointing right */
00163            { 'u', "ACS_RTEE" },    /* tee pointing left */
00164            { 'v', "ACS_BTEE" },    /* tee pointing up */
00165            { 'w', "ACS_TTEE" },    /* tee pointing down */
00166            { 'q', "ACS_HLINE" },   /* horizontal line */
00167            { 'x', "ACS_VLINE" },   /* vertical line */
00168            { 'n', "ACS_PLUS" },    /* large plus or crossover */
00169            { 'o', "ACS_S1" },             /* scan line 1 */
00170            { 's', "ACS_S9" },             /* scan line 9 */
00171            { '`', "ACS_DIAMOND" }, /* diamond */
00172            { 'a', "ACS_CKBOARD" }, /* checker board (stipple) */
00173            { 'f', "ACS_DEGREE" },  /* degree symbol */
00174            { 'g', "ACS_PLMINUS" }, /* plus/minus */
00175            { '~', "ACS_BULLET" },  /* bullet */
00176            { ',', "ACS_LARROW" },  /* arrow pointing left */
00177            { '+', "ACS_RARROW" },  /* arrow pointing right */
00178            { '.', "ACS_DARROW" },  /* arrow pointing down */
00179            { '-', "ACS_UARROW" },  /* arrow pointing up */
00180            { 'h', "ACS_BOARD" },   /* board of squares */
00181            { 'i', "ACS_LANTERN" }, /* lantern symbol */
00182            { '0', "ACS_BLOCK" },   /* solid square block */
00183            { 'p', "ACS_S3" },             /* scan line 3 */
00184            { 'r', "ACS_S7" },             /* scan line 7 */
00185            { 'y', "ACS_LEQUAL" },  /* less/equal */
00186            { 'z', "ACS_GEQUAL" },  /* greater/equal */
00187            { '{', "ACS_PI" },             /* Pi */
00188            { '|', "ACS_NEQUAL" },  /* not equal */
00189            { '}', "ACS_STERLING" },       /* UK pound sign */
00190            { '\0', (char *) 0 }
00191               /* *INDENT-OFF* */
00192        },
00193            *sp;
00194 
00195        for (cp = acs_chars; cp[0] && cp[1]; cp += 2) {
00196            if (ChCharOf(cp[1]) == ChCharOf(ch)) {
00197               found = cp;
00198               /* don't exit from loop - there may be redefinitions */
00199            }
00200        }
00201 
00202        if (found != 0) {
00203            ch = ChCharOf(*found);
00204            for (sp = names; sp->val; sp++)
00205               if (sp->val == ch) {
00206                   result = sp->name;
00207                   break;
00208               }
00209        }
00210     }
00211     return result;
00212 }
00213 
00214 NCURSES_EXPORT(char *)
00215 _tracechtype2(int bufnum, chtype ch)
00216 {
00217     const char *found;
00218 
00219     strcpy(_nc_trace_buf(bufnum, BUFSIZ), l_brace);
00220     if ((found = _nc_altcharset_name(ChAttrOf(ch), ch)) != 0) {
00221        (void) _nc_trace_bufcat(bufnum, found);
00222     } else
00223        (void) _nc_trace_bufcat(bufnum, _tracechar(ChCharOf(ch)));
00224 
00225     if (ChAttrOf(ch) != A_NORMAL) {
00226        (void) _nc_trace_bufcat(bufnum, " | ");
00227        (void) _nc_trace_bufcat(bufnum,
00228               _traceattr2(bufnum + 20, ChAttrOf(ch)));
00229     }
00230 
00231     return (_nc_trace_bufcat(bufnum, r_brace));
00232 }
00233 
00234 NCURSES_EXPORT(char *)
00235 _tracechtype (chtype ch)
00236 {
00237     return _tracechtype2(0, ch);
00238 }
00239 
00240 /* Trace 'chtype' return-values */
00241 NCURSES_EXPORT(chtype)
00242 _nc_retrace_chtype (chtype code)
00243 {
00244     T((T_RETURN("%s"), _tracechtype(code)));
00245     return code;
00246 }
00247 
00248 #if USE_WIDEC_SUPPORT
00249 NCURSES_EXPORT(char *)
00250 _tracecchar_t2 (int bufnum, const cchar_t *ch)
00251 {
00252     char *buf = _nc_trace_buf(bufnum, BUFSIZ);
00253     attr_t attr;
00254     const char *found;
00255 
00256     strcpy(buf, l_brace);
00257     if (ch != 0) {
00258        attr = AttrOfD(ch);
00259        if ((found = _nc_altcharset_name(attr, CharOfD(ch))) != 0) {
00260            (void) _nc_trace_bufcat(bufnum, found);
00261            attr &= ~A_ALTCHARSET;
00262        } else if (isnac(CHDEREF(ch))) {
00263            (void) _nc_trace_bufcat(bufnum, "{NAC}");
00264            attr &= ~WA_NAC;
00265        } else {
00266            PUTC_DATA;
00267            int n;
00268 
00269            PUTC_INIT;
00270            (void) _nc_trace_bufcat(bufnum, "{ ");
00271            do {
00272               PUTC_ch = PUTC_i < CCHARW_MAX ? ch->chars[PUTC_i] : L'\0';
00273               PUTC_n = wcrtomb(PUTC_buf, ch->chars[PUTC_i], &PUT_st);
00274               if (PUTC_ch == L'\0')
00275                   --PUTC_n;
00276               if (PUTC_n <= 0) {
00277                   if (PUTC_ch != L'\0') {
00278                      /* it could not be a multibyte sequence */
00279                      (void) _nc_trace_bufcat(bufnum, _tracechar(UChar(ch->chars[PUTC_i])));
00280                   }
00281                   break;
00282               }
00283               for (n = 0; n < PUTC_n; n++) {
00284                   if (n)
00285                      (void) _nc_trace_bufcat(bufnum, ", ");
00286                   (void) _nc_trace_bufcat(bufnum, _tracechar(UChar(PUTC_buf[n])));
00287               }
00288               ++PUTC_i;
00289            } while (PUTC_ch != L'\0');
00290            (void) _nc_trace_bufcat(bufnum, " }");
00291        }
00292        if (attr != A_NORMAL) {
00293            (void) _nc_trace_bufcat(bufnum, " | ");
00294            (void) _nc_trace_bufcat(bufnum, _traceattr2(bufnum + 20, attr));
00295        }
00296     }
00297 
00298     return (_nc_trace_bufcat(bufnum, r_brace));
00299 }
00300 
00301 NCURSES_EXPORT(char *)
00302 _tracecchar_t (const cchar_t *ch)
00303 {
00304     return _tracecchar_t2(0, ch);
00305 }
00306 #endif
00307 
00308 #else
00309 empty_module(_nc_lib_traceatr)
00310 #endif /* TRACE */