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