missing ncurses sources
[tomato.git] / release / src / router / libncurses / ncurses / trace / visbuf.c
blob66da4f4beca3cef79d9a9d5a9d47715d11ed031b
1 /****************************************************************************
2 * Copyright (c) 2001-2009,2010 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 ****************************************************************************/
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 ****************************************************************************/
36 * visbuf.c - Tracing/Debugging support routines
39 #define NEED_NCURSES_CH_T
40 #include <curses.priv.h>
42 #include <tic.h>
43 #include <ctype.h>
45 MODULE_ID("$Id: visbuf.c,v 1.37 2010/05/29 18:51:41 tom Exp $")
47 #define NUM_VISBUFS 4
49 #define NormalLen(len) (size_t) (((size_t)(len) + 1) * 4)
50 #define WideLen(len) (size_t) (((size_t)(len) + 1) * 4 * MB_CUR_MAX)
52 #ifdef TRACE
53 static const char d_quote[] = StringOf(D_QUOTE);
54 static const char l_brace[] = StringOf(L_BRACE);
55 static const char r_brace[] = StringOf(R_BRACE);
56 #endif
58 static char *
59 _nc_vischar(char *tp, unsigned c)
61 if (c == '"' || c == '\\') {
62 *tp++ = '\\';
63 *tp++ = (char) c;
64 } else if (is7bits(c) && (isgraph(c) || c == ' ')) {
65 *tp++ = (char) c;
66 } else if (c == '\n') {
67 *tp++ = '\\';
68 *tp++ = 'n';
69 } else if (c == '\r') {
70 *tp++ = '\\';
71 *tp++ = 'r';
72 } else if (c == '\b') {
73 *tp++ = '\\';
74 *tp++ = 'b';
75 } else if (c == '\033') {
76 *tp++ = '\\';
77 *tp++ = 'e';
78 } else if (UChar(c) == 0x7f) {
79 *tp++ = '\\';
80 *tp++ = '^';
81 *tp++ = '?';
82 } else if (is7bits(c) && iscntrl(UChar(c))) {
83 *tp++ = '\\';
84 *tp++ = '^';
85 *tp++ = (char) ('@' + c);
86 } else {
87 sprintf(tp, "\\%03lo", (unsigned long) ChCharOf(c));
88 tp += strlen(tp);
90 *tp = 0;
91 return tp;
94 static const char *
95 _nc_visbuf2n(int bufnum, const char *buf, int len)
97 const char *vbuf = 0;
98 char *tp;
99 int c;
101 if (buf == 0)
102 return ("(null)");
103 if (buf == CANCELLED_STRING)
104 return ("(cancelled)");
106 if (len < 0)
107 len = (int) strlen(buf);
109 #ifdef TRACE
110 vbuf = tp = _nc_trace_buf(bufnum, NormalLen(len));
111 #else
113 static char *mybuf[NUM_VISBUFS];
114 if (bufnum < 0) {
115 for (c = 0; c < NUM_VISBUFS; ++c) {
116 FreeAndNull(mybuf[c]);
118 tp = 0;
119 } else {
120 mybuf[bufnum] = typeRealloc(char, NormalLen(len), mybuf[bufnum]);
121 vbuf = tp = mybuf[bufnum];
124 #endif
125 if (tp != 0) {
126 *tp++ = D_QUOTE;
127 while ((--len >= 0) && (c = *buf++) != '\0') {
128 tp = _nc_vischar(tp, UChar(c));
130 *tp++ = D_QUOTE;
131 *tp = '\0';
132 } else {
133 vbuf = ("(_nc_visbuf2n failed)");
135 return (vbuf);
138 NCURSES_EXPORT(const char *)
139 _nc_visbuf2(int bufnum, const char *buf)
141 return _nc_visbuf2n(bufnum, buf, -1);
144 NCURSES_EXPORT(const char *)
145 _nc_visbuf(const char *buf)
147 return _nc_visbuf2(0, buf);
150 NCURSES_EXPORT(const char *)
151 _nc_visbufn(const char *buf, int len)
153 return _nc_visbuf2n(0, buf, len);
156 #ifdef TRACE
157 #if USE_WIDEC_SUPPORT
159 #if defined(USE_TERMLIB)
160 #define _nc_wchstrlen _my_wchstrlen
161 static int
162 _nc_wchstrlen(const cchar_t *s)
164 int result = 0;
165 while (CharOf(s[result]) != L'\0') {
166 result++;
168 return result;
170 #endif
172 static const char *
173 _nc_viswbuf2n(int bufnum, const wchar_t *buf, int len)
175 const char *vbuf;
176 char *tp;
177 wchar_t c;
179 if (buf == 0)
180 return ("(null)");
182 if (len < 0)
183 len = (int) wcslen(buf);
185 #ifdef TRACE
186 vbuf = tp = _nc_trace_buf(bufnum, WideLen(len));
187 #else
189 static char *mybuf[NUM_VISBUFS];
190 mybuf[bufnum] = typeRealloc(char, WideLen(len), mybuf[bufnum]);
191 vbuf = tp = mybuf[bufnum];
193 #endif
194 if (tp != 0) {
195 *tp++ = D_QUOTE;
196 while ((--len >= 0) && (c = *buf++) != '\0') {
197 char temp[CCHARW_MAX + 80];
198 int j = wctomb(temp, c), k;
199 if (j <= 0) {
200 sprintf(temp, "\\u%08X", (unsigned) c);
201 j = (int) strlen(temp);
203 for (k = 0; k < j; ++k) {
204 tp = _nc_vischar(tp, UChar(temp[k]));
207 *tp++ = D_QUOTE;
208 *tp = '\0';
209 } else {
210 vbuf = ("(_nc_viswbuf2n failed)");
212 return (vbuf);
215 NCURSES_EXPORT(const char *)
216 _nc_viswbuf2(int bufnum, const wchar_t *buf)
218 return _nc_viswbuf2n(bufnum, buf, -1);
221 NCURSES_EXPORT(const char *)
222 _nc_viswbuf(const wchar_t *buf)
224 return _nc_viswbuf2(0, buf);
227 NCURSES_EXPORT(const char *)
228 _nc_viswbufn(const wchar_t *buf, int len)
230 return _nc_viswbuf2n(0, buf, len);
233 /* this special case is used for wget_wstr() */
234 NCURSES_EXPORT(const char *)
235 _nc_viswibuf(const wint_t *buf)
237 static wchar_t *mybuf;
238 static unsigned mylen;
239 unsigned n;
241 for (n = 0; buf[n] != 0; ++n) {
242 ; /* empty */
244 if (mylen < ++n) {
245 mylen = n + 80;
246 if (mybuf != 0)
247 mybuf = typeRealloc(wchar_t, mylen, mybuf);
248 else
249 mybuf = typeMalloc(wchar_t, mylen);
251 for (n = 0; buf[n] != 0; ++n) {
252 mybuf[n] = (wchar_t) buf[n];
254 mybuf[n] = L'\0';
256 return _nc_viswbuf2(0, mybuf);
258 #endif /* USE_WIDEC_SUPPORT */
260 /* use these functions for displaying parts of a line within a window */
261 NCURSES_EXPORT(const char *)
262 _nc_viscbuf2(int bufnum, const NCURSES_CH_T * buf, int len)
264 char *result = _nc_trace_buf(bufnum, BUFSIZ);
265 int first;
266 const char *found;
268 if (result != 0) {
269 #if USE_WIDEC_SUPPORT
270 if (len < 0)
271 len = _nc_wchstrlen(buf);
272 #endif /* USE_WIDEC_SUPPORT */
275 * Display one or more strings followed by attributes.
277 first = 0;
278 while (first < len) {
279 attr_t attr = AttrOf(buf[first]);
280 int last = len - 1;
281 int j;
283 for (j = first + 1; j < len; ++j) {
284 if (!SameAttrOf(buf[j], buf[first])) {
285 last = j - 1;
286 break;
290 (void) _nc_trace_bufcat(bufnum, l_brace);
291 (void) _nc_trace_bufcat(bufnum, d_quote);
292 for (j = first; j <= last; ++j) {
293 found = _nc_altcharset_name(attr, (chtype) CharOf(buf[j]));
294 if (found != 0) {
295 (void) _nc_trace_bufcat(bufnum, found);
296 attr &= ~A_ALTCHARSET;
297 } else
298 #if USE_WIDEC_SUPPORT
299 if (!isWidecExt(buf[j])) {
300 PUTC_DATA;
302 PUTC_INIT;
303 for (PUTC_i = 0; PUTC_i < CCHARW_MAX; ++PUTC_i) {
304 int k;
306 PUTC_ch = buf[j].chars[PUTC_i];
307 if (PUTC_ch == L'\0') {
308 if (PUTC_i == 0)
309 (void) _nc_trace_bufcat(bufnum, "\\000");
310 break;
312 PUTC_n = (int) wcrtomb(PUTC_buf,
313 buf[j].chars[PUTC_i], &PUT_st);
314 if (PUTC_n <= 0)
315 break;
316 for (k = 0; k < PUTC_n; k++) {
317 char temp[80];
318 _nc_vischar(temp, UChar(PUTC_buf[k]));
319 (void) _nc_trace_bufcat(bufnum, temp);
323 #else
325 char temp[80];
326 _nc_vischar(temp, UChar(buf[j]));
327 result = _nc_trace_bufcat(bufnum, temp);
329 #endif /* USE_WIDEC_SUPPORT */
331 (void) _nc_trace_bufcat(bufnum, d_quote);
332 if (attr != A_NORMAL) {
333 (void) _nc_trace_bufcat(bufnum, " | ");
334 (void) _nc_trace_bufcat(bufnum, _traceattr2(bufnum + 20, attr));
336 result = _nc_trace_bufcat(bufnum, r_brace);
337 first = last + 1;
340 return result;
343 NCURSES_EXPORT(const char *)
344 _nc_viscbuf(const NCURSES_CH_T * buf, int len)
346 return _nc_viscbuf2(0, buf, len);
348 #endif /* TRACE */