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