Fix PROCDURE -> PROCEDURE.
[dragonfly.git] / contrib / ncurses-5.4 / ncurses / trace / visbuf.c
1 /****************************************************************************
2  * Copyright (c) 2001-2003,2004 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-2004                                      *
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 #include <curses.priv.h>
40
41 #include <tic.h>
42 #include <ctype.h>
43
44 MODULE_ID("$Id: visbuf.c,v 1.9 2004/02/03 01:16:37 tom Exp $")
45
46 static char *
47 _nc_vischar(char *tp, unsigned c)
48 {
49     if (c == '"' || c == '\\') {
50         *tp++ = '\\';
51         *tp++ = c;
52     } else if (is7bits(c) && (isgraph(c) || c == ' ')) {
53         *tp++ = c;
54     } else if (c == '\n') {
55         *tp++ = '\\';
56         *tp++ = 'n';
57     } else if (c == '\r') {
58         *tp++ = '\\';
59         *tp++ = 'r';
60     } else if (c == '\b') {
61         *tp++ = '\\';
62         *tp++ = 'b';
63     } else if (c == '\033') {
64         *tp++ = '\\';
65         *tp++ = 'e';
66     } else if (is7bits(c) && iscntrl(UChar(c))) {
67         *tp++ = '\\';
68         *tp++ = '^';
69         *tp++ = '@' + c;
70     } else {
71         sprintf(tp, "\\%03lo", ChCharOf(c));
72         tp += strlen(tp);
73     }
74     *tp = 0;
75     return tp;
76 }
77
78 static const char *
79 _nc_visbuf2n(int bufnum, const char *buf, int len)
80 {
81     char *vbuf;
82     char *tp;
83     int c;
84
85     if (buf == 0)
86         return ("(null)");
87     if (buf == CANCELLED_STRING)
88         return ("(cancelled)");
89
90     if (len < 0)
91         len = strlen(buf);
92
93 #ifdef TRACE
94     tp = vbuf = _nc_trace_buf(bufnum, (unsigned) (len * 4) + 5);
95 #else
96     {
97         static char *mybuf[2];
98         mybuf[bufnum] = typeRealloc(char, (unsigned) (len * 4) + 5, mybuf[bufnum]);
99         tp = vbuf = mybuf[bufnum];
100     }
101 #endif
102     *tp++ = D_QUOTE;
103     while ((--len >= 0) && (c = *buf++) != '\0') {
104         tp = _nc_vischar(tp, UChar(c));
105     }
106     *tp++ = D_QUOTE;
107     *tp++ = '\0';
108     return (vbuf);
109 }
110
111 NCURSES_EXPORT(const char *)
112 _nc_visbuf2(int bufnum, const char *buf)
113 {
114     return _nc_visbuf2n(bufnum, buf, -1);
115 }
116
117 NCURSES_EXPORT(const char *)
118 _nc_visbuf(const char *buf)
119 {
120     return _nc_visbuf2(0, buf);
121 }
122
123 NCURSES_EXPORT(const char *)
124 _nc_visbufn(const char *buf, int len)
125 {
126     return _nc_visbuf2n(0, buf, len);
127 }
128
129 #if USE_WIDEC_SUPPORT
130 #ifdef TRACE
131 static const char *
132 _nc_viswbuf2n(int bufnum, const wchar_t * buf, int len)
133 {
134     char *vbuf;
135     char *tp;
136     wchar_t c;
137
138     if (buf == 0)
139         return ("(null)");
140
141     if (len < 0)
142         len = wcslen(buf);
143
144 #ifdef TRACE
145     tp = vbuf = _nc_trace_buf(bufnum, (unsigned) (len * 4) + 5);
146 #else
147     {
148         static char *mybuf[2];
149         mybuf[bufnum] = typeRealloc(char, (unsigned) (len * 4) + 5, mybuf[bufnum]);
150         tp = vbuf = mybuf[bufnum];
151     }
152 #endif
153     *tp++ = D_QUOTE;
154     while ((--len >= 0) && (c = *buf++) != '\0') {
155         char temp[CCHARW_MAX + 80];
156         int j = wctomb(temp, c), k;
157         if (j <= 0) {
158             sprintf(temp, "\\u%08X", (wint_t) c);
159             j = strlen(temp);
160         }
161         for (k = 0; k < j; ++k) {
162             tp = _nc_vischar(tp, temp[k]);
163         }
164     }
165     *tp++ = D_QUOTE;
166     *tp++ = '\0';
167     return (vbuf);
168 }
169
170 NCURSES_EXPORT(const char *)
171 _nc_viswbuf2(int bufnum, const wchar_t * buf)
172 {
173     return _nc_viswbuf2n(bufnum, buf, -1);
174 }
175
176 NCURSES_EXPORT(const char *)
177 _nc_viswbuf(const wchar_t * buf)
178 {
179     return _nc_viswbuf2(0, buf);
180 }
181
182 NCURSES_EXPORT(const char *)
183 _nc_viswbufn(const wchar_t * buf, int len)
184 {
185     return _nc_viswbuf2n(0, buf, len);
186 }
187
188 NCURSES_EXPORT(const char *)
189 _nc_viscbuf2(int bufnum, const cchar_t * buf, int len)
190 {
191     char *result = _nc_trace_buf(bufnum, BUFSIZ);
192     int n;
193     bool same = TRUE;
194     attr_t attr = A_NORMAL;
195     const char *found;
196
197     if (len < 0)
198         len = _nc_wchstrlen(buf);
199
200     for (n = 1; n < len; n++) {
201         if (AttrOf(buf[n]) != AttrOf(buf[0])) {
202             same = FALSE;
203             break;
204         }
205     }
206
207     /*
208      * If the rendition is the same for the whole string, display it as a
209      * quoted string, followed by the rendition.  Otherwise, use the more
210      * detailed trace function that displays each character separately.
211      */
212     if (same) {
213         static const char d_quote[] =
214         {D_QUOTE, 0};
215
216         result = _nc_trace_bufcat(bufnum, d_quote);
217         while (len-- > 0) {
218             if ((found = _nc_altcharset_name(attr, CharOfD(buf))) != 0) {
219                 result = _nc_trace_bufcat(bufnum, found);
220                 attr &= ~A_ALTCHARSET;
221             } else if (!isnac(CHDEREF(buf))) {
222                 PUTC_DATA;
223
224                 PUTC_INIT;
225                 do {
226                     PUTC_ch = PUTC_i < CCHARW_MAX ? buf->chars[PUTC_i] : L'\0';
227                     PUTC_n = wcrtomb(PUTC_buf, buf->chars[PUTC_i], &PUT_st);
228                     if (PUTC_ch == L'\0')
229                         --PUTC_n;
230                     if (PUTC_n <= 0)
231                         break;
232                     for (n = 0; n < PUTC_n; n++) {
233                         char temp[80];
234                         _nc_vischar(temp, UChar(PUTC_buf[n]));
235                         result = _nc_trace_bufcat(bufnum, temp);
236                     }
237                     ++PUTC_i;
238                 } while (PUTC_ch != L'\0');
239             }
240             buf++;
241         }
242         result = _nc_trace_bufcat(bufnum, d_quote);
243         if (attr != A_NORMAL) {
244             result = _nc_trace_bufcat(bufnum, " | ");
245             result = _nc_trace_bufcat(bufnum, _traceattr2(bufnum + 20, attr));
246         }
247     } else {
248         static const char l_brace[] =
249         {L_BRACE, 0};
250         static const char r_brace[] =
251         {R_BRACE, 0};
252         strcpy(result, l_brace);
253         while (len-- > 0) {
254             result = _nc_trace_bufcat(bufnum,
255                                       _tracecchar_t2(bufnum + 20, buf++));
256         }
257         result = _nc_trace_bufcat(bufnum, r_brace);
258     }
259     return result;
260 }
261
262 NCURSES_EXPORT(const char *)
263 _nc_viscbuf(const cchar_t * buf, int len)
264 {
265     return _nc_viscbuf2(0, buf, len);
266 }
267 #endif /* TRACE */
268 #endif /* USE_WIDEC_SUPPORT */