]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/trace/visbuf.c
706dea0e450f1cab6d6580bf12651be24e1afcf5
[ncurses.git] / ncurses / trace / visbuf.c
1 /****************************************************************************
2  * Copyright (c) 2001-2006,2007 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-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
45 MODULE_ID("$Id: visbuf.c,v 1.23 2007/04/07 19:25:28 tom Exp $")
46
47 #ifdef TRACE
48 static const char d_quote[] = {D_QUOTE, 0};
49 static const char l_brace[] = {L_BRACE, 0};
50 static const char r_brace[] = {R_BRACE, 0};
51 #endif
52
53 static char *
54 _nc_vischar(char *tp, unsigned c)
55 {
56     if (c == '"' || c == '\\') {
57         *tp++ = '\\';
58         *tp++ = c;
59     } else if (is7bits(c) && (isgraph(c) || c == ' ')) {
60         *tp++ = c;
61     } else if (c == '\n') {
62         *tp++ = '\\';
63         *tp++ = 'n';
64     } else if (c == '\r') {
65         *tp++ = '\\';
66         *tp++ = 'r';
67     } else if (c == '\b') {
68         *tp++ = '\\';
69         *tp++ = 'b';
70     } else if (c == '\033') {
71         *tp++ = '\\';
72         *tp++ = 'e';
73     } else if (is7bits(c) && iscntrl(UChar(c))) {
74         *tp++ = '\\';
75         *tp++ = '^';
76         *tp++ = '@' + c;
77     } else {
78         sprintf(tp, "\\%03lo", (unsigned long) ChCharOf(c));
79         tp += strlen(tp);
80     }
81     *tp = 0;
82     return tp;
83 }
84
85 static const char *
86 _nc_visbuf2n(int bufnum, const char *buf, int len)
87 {
88     char *vbuf;
89     char *tp;
90     int c;
91
92     if (buf == 0)
93         return ("(null)");
94     if (buf == CANCELLED_STRING)
95         return ("(cancelled)");
96
97     if (len < 0)
98         len = strlen(buf);
99
100 #ifdef TRACE
101     tp = vbuf = _nc_trace_buf(bufnum, (unsigned) (len * 4) + 5);
102 #else
103     {
104         static char *mybuf[4];
105         mybuf[bufnum] = typeRealloc(char, (unsigned) (len * 4) + 5, mybuf[bufnum]);
106         tp = vbuf = mybuf[bufnum];
107     }
108 #endif
109     *tp++ = D_QUOTE;
110     while ((--len >= 0) && (c = *buf++) != '\0') {
111         tp = _nc_vischar(tp, UChar(c));
112     }
113     *tp++ = D_QUOTE;
114     *tp++ = '\0';
115     return (vbuf);
116 }
117
118 NCURSES_EXPORT(const char *)
119 _nc_visbuf2(int bufnum, const char *buf)
120 {
121     return _nc_visbuf2n(bufnum, buf, -1);
122 }
123
124 NCURSES_EXPORT(const char *)
125 _nc_visbuf(const char *buf)
126 {
127     return _nc_visbuf2(0, buf);
128 }
129
130 NCURSES_EXPORT(const char *)
131 _nc_visbufn(const char *buf, int len)
132 {
133     return _nc_visbuf2n(0, buf, len);
134 }
135
136 #ifdef TRACE
137 #if USE_WIDEC_SUPPORT
138
139 #if defined(USE_TERMLIB)
140 #define _nc_wchstrlen _my_wchstrlen
141 static int
142 _nc_wchstrlen(const cchar_t *s)
143 {
144     int result = 0;
145     while (CharOf(s[result]) != L'\0') {
146         result++;
147     }
148     return result;
149 }
150 #endif
151
152 static const char *
153 _nc_viswbuf2n(int bufnum, const wchar_t *buf, int len)
154 {
155     char *vbuf;
156     char *tp;
157     wchar_t c;
158
159     if (buf == 0)
160         return ("(null)");
161
162     if (len < 0)
163         len = wcslen(buf);
164
165 #ifdef TRACE
166     tp = vbuf = _nc_trace_buf(bufnum, (unsigned) (len * 4) + 5);
167 #else
168     {
169         static char *mybuf[2];
170         mybuf[bufnum] = typeRealloc(char, (unsigned) (len * 4) + 5, mybuf[bufnum]);
171         tp = vbuf = mybuf[bufnum];
172     }
173 #endif
174     *tp++ = D_QUOTE;
175     while ((--len >= 0) && (c = *buf++) != '\0') {
176         char temp[CCHARW_MAX + 80];
177         int j = wctomb(temp, c), k;
178         if (j <= 0) {
179             sprintf(temp, "\\u%08X", (wint_t) c);
180             j = strlen(temp);
181         }
182         for (k = 0; k < j; ++k) {
183             tp = _nc_vischar(tp, UChar(temp[k]));
184         }
185     }
186     *tp++ = D_QUOTE;
187     *tp++ = '\0';
188     return (vbuf);
189 }
190
191 NCURSES_EXPORT(const char *)
192 _nc_viswbuf2(int bufnum, const wchar_t *buf)
193 {
194     return _nc_viswbuf2n(bufnum, buf, -1);
195 }
196
197 NCURSES_EXPORT(const char *)
198 _nc_viswbuf(const wchar_t *buf)
199 {
200     return _nc_viswbuf2(0, buf);
201 }
202
203 NCURSES_EXPORT(const char *)
204 _nc_viswbufn(const wchar_t *buf, int len)
205 {
206     return _nc_viswbuf2n(0, buf, len);
207 }
208
209 /* this special case is used for wget_wstr() */
210 NCURSES_EXPORT(const char *)
211 _nc_viswibuf(const wint_t *buf)
212 {
213     static wchar_t *mybuf;
214     static unsigned mylen;
215     unsigned n;
216
217     for (n = 0; buf[n] != 0; ++n) ;
218     if (mylen < ++n) {
219         mylen = n + 80;
220         if (mybuf != 0)
221             mybuf = typeRealloc(wchar_t, mylen, mybuf);
222         else
223             mybuf = typeMalloc(wchar_t, mylen);
224     }
225     for (n = 0; buf[n] != 0; ++n)
226         mybuf[n] = (wchar_t) buf[n];
227
228     return _nc_viswbuf2(0, mybuf);
229 }
230 #endif /* USE_WIDEC_SUPPORT */
231
232 /* use these functions for displaying parts of a line within a window */
233 NCURSES_EXPORT(const char *)
234 _nc_viscbuf2(int bufnum, const NCURSES_CH_T * buf, int len)
235 {
236     char *result = _nc_trace_buf(bufnum, BUFSIZ);
237     int first;
238     const char *found;
239
240 #if USE_WIDEC_SUPPORT
241     if (len < 0)
242         len = _nc_wchstrlen(buf);
243 #endif /* USE_WIDEC_SUPPORT */
244
245     /*
246      * Display one or more strings followed by attributes.
247      */
248     first = 0;
249     while (first < len) {
250         attr_t attr = AttrOf(buf[first]);
251         int last = len - 1;
252         int j;
253
254         for (j = first + 1; j < len; ++j) {
255             if (!SameAttrOf(buf[j], buf[first])) {
256                 last = j - 1;
257                 break;
258             }
259         }
260
261         result = _nc_trace_bufcat(bufnum, l_brace);
262         result = _nc_trace_bufcat(bufnum, d_quote);
263         for (j = first; j <= last; ++j) {
264             if ((found = _nc_altcharset_name(attr, (chtype) CharOf(buf[j]))) != 0) {
265                 result = _nc_trace_bufcat(bufnum, found);
266                 attr &= ~A_ALTCHARSET;
267             } else
268 #if USE_WIDEC_SUPPORT
269             if (!isWidecExt(buf[j])) {
270                 PUTC_DATA;
271
272                 PUTC_INIT;
273                 for (PUTC_i = 0; PUTC_i < CCHARW_MAX; ++PUTC_i) {
274                     int k;
275
276                     PUTC_ch = buf[j].chars[PUTC_i];
277                     if (PUTC_ch == L'\0')
278                         break;
279                     PUTC_n = wcrtomb(PUTC_buf, buf[j].chars[PUTC_i], &PUT_st);
280                     if (PUTC_n <= 0)
281                         break;
282                     for (k = 0; k < PUTC_n; k++) {
283                         char temp[80];
284                         _nc_vischar(temp, UChar(PUTC_buf[k]));
285                         result = _nc_trace_bufcat(bufnum, temp);
286                     }
287                 }
288             }
289 #else
290             {
291                 char temp[80];
292                 _nc_vischar(temp, UChar(buf[j]));
293                 result = _nc_trace_bufcat(bufnum, temp);
294             }
295 #endif /* USE_WIDEC_SUPPORT */
296         }
297         result = _nc_trace_bufcat(bufnum, d_quote);
298         if (attr != A_NORMAL) {
299             result = _nc_trace_bufcat(bufnum, " | ");
300             result = _nc_trace_bufcat(bufnum, _traceattr2(bufnum + 20, attr));
301         }
302         result = _nc_trace_bufcat(bufnum, r_brace);
303         first = last + 1;
304     }
305     return result;
306 }
307
308 NCURSES_EXPORT(const char *)
309 _nc_viscbuf(const NCURSES_CH_T * buf, int len)
310 {
311     return _nc_viscbuf2(0, buf, len);
312 }
313 #endif /* TRACE */