ncurses 4.1
[ncurses.git] / ncurses / lib_traceatr.c
1
2 /***************************************************************************
3 *                            COPYRIGHT NOTICE                              *
4 ****************************************************************************
5 *                ncurses is copyright (C) 1992-1995                        *
6 *                          Zeyd M. Ben-Halim                               *
7 *                          zmbenhal@netcom.com                             *
8 *                          Eric S. Raymond                                 *
9 *                          esr@snark.thyrsus.com                           *
10 *                                                                          *
11 *        Permission is hereby granted to reproduce and distribute ncurses  *
12 *        by any means and for any fee, whether alone or as part of a       *
13 *        larger distribution, in source or in binary form, PROVIDED        *
14 *        this notice is included with any such distribution, and is not    *
15 *        removed from any of its header files. Mention of ncurses in any   *
16 *        applications linked with it is highly appreciated.                *
17 *                                                                          *
18 *        ncurses comes AS IS with no warranty, implied or expressed.       *
19 *                                                                          *
20 ***************************************************************************/
21
22
23
24 /*
25  *      lib_traceatr.c - Tracing/Debugging routines (attributes)
26  */
27
28 #ifndef TRACE
29 #define TRACE                   /* turn on internal defs for this module */
30 #endif
31
32 #include <curses.priv.h>
33 #include <term.h>       /* acs_chars */
34
35 MODULE_ID("$Id: lib_traceatr.c,v 1.20 1997/05/06 11:07:27 tom Exp $")
36
37 #define COLOR_OF(c) (c < 0 || c > 7 ? "default" : colors[c].name)
38
39 char * _nc_trace_buf(int bufnum, size_t want)
40 {
41         static struct {
42                 char *text;
43                 size_t size;
44         } *list;
45         static size_t have;
46
47         if (bufnum < 0)
48                 bufnum = 0;
49
50         if ((size_t)(bufnum+1) > have) {
51                 size_t need = (bufnum + 1) * 2;
52                 size_t used = sizeof(*list) * need;
53                 list = (list == 0) ? malloc(used) : realloc(list, used);
54                 while (need > have)
55                         list[have++].text = 0;
56         }
57
58         if (list[bufnum].text == 0)
59         {
60                 list[bufnum].text = malloc(want);
61                 list[bufnum].size = want;
62         }
63         else if (want > list[bufnum].size) {
64                 list[bufnum].text = realloc(list[bufnum].text, want);
65                 list[bufnum].size = want;
66         }
67         *(list[bufnum].text) = '\0';
68         return list[bufnum].text;
69 }
70
71 char *_traceattr2(int bufnum, attr_t newmode)
72 {
73 char    *buf = _nc_trace_buf(bufnum, BUFSIZ);
74 char    *tmp = buf;
75 static const    struct {unsigned int val; const char *name;}
76 names[] =
77     {
78         { A_STANDOUT,           "A_STANDOUT" },
79         { A_UNDERLINE,          "A_UNDERLINE" },
80         { A_REVERSE,            "A_REVERSE" },
81         { A_BLINK,              "A_BLINK" },
82         { A_DIM,                "A_DIM" },
83         { A_BOLD,               "A_BOLD" },
84         { A_ALTCHARSET,         "A_ALTCHARSET" },
85         { A_INVIS,              "A_INVIS" },
86         { A_PROTECT,            "A_PROTECT" },
87         { A_CHARTEXT,           "A_CHARTEXT" },
88         { A_NORMAL,             "A_NORMAL" },
89         { A_COLOR,              "A_COLOR" },
90     },
91 colors[] =
92     {
93         { COLOR_BLACK,          "COLOR_BLACK" },
94         { COLOR_RED,            "COLOR_RED" },
95         { COLOR_GREEN,          "COLOR_GREEN" },
96         { COLOR_YELLOW,         "COLOR_YELLOW" },
97         { COLOR_BLUE,           "COLOR_BLUE" },
98         { COLOR_MAGENTA,        "COLOR_MAGENTA" },
99         { COLOR_CYAN,           "COLOR_CYAN" },
100         { COLOR_WHITE,          "COLOR_WHITE" },
101     };
102 size_t n;
103 unsigned save_nc_tracing = _nc_tracing;
104         _nc_tracing = 0;
105
106         strcpy(tmp++, "{");
107
108         for (n = 0; n < sizeof(names)/sizeof(names[0]); n++) {
109                 if ((newmode & names[n].val) != 0) {
110                         if (buf[1] != '\0')
111                                 strcat(tmp, "|");
112                         strcat(tmp, names[n].name);
113                         tmp += strlen(tmp);
114
115                         if (names[n].val == A_COLOR)
116                         {
117                                 short pairnum = PAIR_NUMBER(newmode);
118                                 short fg, bg;
119         
120                                 if (pair_content(pairnum, &fg, &bg) == OK)
121                                         (void) sprintf(tmp,
122                                                 "{%d = {%s, %s}}",
123                                                 pairnum,
124                                                 COLOR_OF(fg),
125                                                 COLOR_OF(bg)
126                                                 );
127                                 else
128                                         (void) sprintf(tmp, "{%d}", pairnum);
129                         }
130                 }
131         }
132         if (AttrOf(newmode) == A_NORMAL) {
133                 if (buf[1] != '\0')
134                         strcat(tmp, "|");
135                 strcat(tmp, "A_NORMAL");
136         }
137
138         _nc_tracing = save_nc_tracing;
139         return (strcat(buf,"}"));
140 }
141
142 char *_traceattr(attr_t newmode)
143 {
144         return _traceattr2(0, newmode);
145 }
146
147 char *_tracechtype2(int bufnum, chtype ch)
148 {
149 char    *buf = _nc_trace_buf(bufnum, BUFSIZ);
150 char    *found = 0;
151
152     strcpy(buf, "{");
153     if (ch & A_ALTCHARSET)
154     {
155         char    *cp;
156         static const    struct {unsigned int val; const char *name;}
157         names[] =
158         {
159             {'l', "ACS_ULCORNER"},      /* upper left corner */
160             {'m', "ACS_LLCORNER"},      /* lower left corner */
161             {'k', "ACS_URCORNER"},      /* upper right corner */
162             {'j', "ACS_LRCORNER"},      /* lower right corner */
163             {'t', "ACS_LTEE"},          /* tee pointing right */
164             {'u', "ACS_RTEE"},          /* tee pointing left */
165             {'v', "ACS_BTEE"},          /* tee pointing up */
166             {'w', "ACS_TTEE"},          /* tee pointing down */
167             {'q', "ACS_HLINE"},         /* horizontal line */
168             {'x', "ACS_VLINE"},         /* vertical line */
169             {'n', "ACS_PLUS"},          /* large plus or crossover */
170             {'o', "ACS_S1"},            /* scan line 1 */
171             {'s', "ACS_S9"},            /* scan line 9 */
172             {'`', "ACS_DIAMOND"},       /* diamond */
173             {'a', "ACS_CKBOARD"},       /* checker board (stipple) */
174             {'f', "ACS_DEGREE"},        /* degree symbol */
175             {'g', "ACS_PLMINUS"},       /* plus/minus */
176             {'~', "ACS_BULLET"},        /* bullet */
177             {',', "ACS_LARROW"},        /* arrow pointing left */
178             {'+', "ACS_RARROW"},        /* arrow pointing right */
179             {'.', "ACS_DARROW"},        /* arrow pointing down */
180             {'-', "ACS_UARROW"},        /* arrow pointing up */
181             {'h', "ACS_BOARD"},         /* board of squares */
182             {'I', "ACS_LANTERN"},       /* lantern symbol */
183             {'0', "ACS_BLOCK"},         /* solid square block */
184             {'p', "ACS_S3"},            /* scan line 3 */
185             {'r', "ACS_S7"},            /* scan line 7 */
186             {'y', "ACS_LEQUAL"},        /* less/equal */
187             {'z', "ACS_GEQUAL"},        /* greater/equal */
188             {'{', "ACS_PI"},            /* Pi */
189             {'|', "ACS_NEQUAL"},        /* not equal */
190             {'}', "ACS_STERLING"},      /* UK pound sign */
191             {'\0',(char *)0}
192         },
193         *sp;
194
195         for (cp = acs_chars; cp[0] && cp[1]; cp += 2)
196         {
197             if (TextOf(cp[1]) == TextOf(ch))
198             {
199                 found = cp;
200                 /* don't exit from loop - there may be redefinitions */
201             }
202         }
203
204         if (found != 0)
205         {
206             ch = TextOf(*found);
207             for (sp = names; sp->val; sp++)
208                 if (sp->val == ch)
209                 {
210                     (void) strcat(buf, sp->name);
211                     ch &= ~A_ALTCHARSET;
212                     break;
213                 }
214         }
215     }
216
217     if (found == 0)
218         (void) strcat(buf, _tracechar(TextOf(ch)));
219
220     if (AttrOf(ch) != A_NORMAL)
221         (void) sprintf(buf + strlen(buf), " | %s", _traceattr2(bufnum+20,AttrOf(ch)));
222
223     strcat(buf, "}");
224     return(buf);
225 }
226
227 char *_tracechtype(chtype ch)
228 {
229         return _tracechtype2(0, ch);
230 }