1 /****************************************************************************
2  * Copyright (c) 1998,1999,2000 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: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  ****************************************************************************/
33 
34 /*
35  *	lib_trace.c - Tracing/Debugging routines
36  */
37 
38 #include <curses.priv.h>
39 #include <tic.h>
40 
41 #include <ctype.h>
42 
43 MODULE_ID("$Id: lib_trace.c,v 1.34 2000/04/01 20:25:47 tom Exp $")
44 
45 unsigned _nc_tracing = 0;	/* always define this */
46 
47 #ifdef TRACE
48 const char *_nc_tputs_trace = "";
49 long _nc_outchars = 0;
50 
51 static FILE *tracefp;		/* default to writing to stderr */
52 
53 void
54 trace(const unsigned int tracelevel GCC_UNUSED)
55 {
56     static bool been_here = FALSE;
57     static char my_name[] = "trace";
58 
59     _nc_tracing = tracelevel;
60     if (!been_here && tracelevel) {
61 	been_here = TRUE;
62 
63 	if (_nc_access(my_name, W_OK) < 0
64 	    || (tracefp = fopen(my_name, "w")) == 0) {
65 	    perror("curses: Can't open 'trace' file: ");
66 	    exit(EXIT_FAILURE);
67 	}
68 	/* Try to set line-buffered mode, or (failing that) unbuffered,
69 	 * so that the trace-output gets flushed automatically at the
70 	 * end of each line.  This is useful in case the program dies.
71 	 */
72 #if HAVE_SETVBUF		/* ANSI */
73 	(void) setvbuf(tracefp, (char *) 0, _IOLBF, 0);
74 #elif HAVE_SETBUF		/* POSIX */
75 	(void) setbuffer(tracefp, (char *) 0);
76 #endif
77 	_tracef("TRACING NCURSES version %s (%d)",
78 	    NCURSES_VERSION, NCURSES_VERSION_PATCH);
79     }
80 }
81 #endif
82 
83 const char *
84 _nc_visbuf2(int bufnum, const char *buf)
85 /* visibilize a given string */
86 {
87     char *vbuf;
88     char *tp;
89     int c;
90 
91     if (buf == 0)
92 	return ("(null)");
93     if (buf == CANCELLED_STRING)
94 	return ("(cancelled)");
95 
96 #ifdef TRACE
97     tp = vbuf = _nc_trace_buf(bufnum, (strlen(buf) * 4) + 5);
98 #else
99     {
100     static char *mybuf[2];
101     mybuf[bufnum] = _nc_doalloc(mybuf[bufnum], (strlen(buf) * 4) + 5);
102     tp = vbuf = mybuf[bufnum];
103     }
104 #endif
105     *tp++ = '"';
106     while ((c = *buf++) != '\0') {
107 	if (c == '"') {
108 	    *tp++ = '\\';
109 	    *tp++ = '"';
110 	} else if (is7bits(c) && (isgraph(c) || c == ' ')) {
111 	    *tp++ = c;
112 	} else if (c == '\n') {
113 	    *tp++ = '\\';
114 	    *tp++ = 'n';
115 	} else if (c == '\r') {
116 	    *tp++ = '\\';
117 	    *tp++ = 'r';
118 	} else if (c == '\b') {
119 	    *tp++ = '\\';
120 	    *tp++ = 'b';
121 	} else if (c == '\033') {
122 	    *tp++ = '\\';
123 	    *tp++ = 'e';
124 	} else if (is7bits(c) && iscntrl(c)) {
125 	    *tp++ = '\\';
126 	    *tp++ = '^';
127 	    *tp++ = '@' + c;
128 	} else {
129 	    sprintf(tp, "\\%03o", c & 0xff);
130 	    tp += strlen(tp);
131 	}
132     }
133     *tp++ = '"';
134     *tp++ = '\0';
135     return (vbuf);
136 }
137 
138 const char *
139 _nc_visbuf(const char *buf)
140 {
141     return _nc_visbuf2(0, buf);
142 }
143 
144 #ifdef TRACE
145 void
146 _tracef(const char *fmt,...)
147 {
148     static const char Called[] = T_CALLED("");
149     static const char Return[] = T_RETURN("");
150     static int level;
151     va_list ap;
152     bool before = FALSE;
153     bool after = FALSE;
154     int doit = _nc_tracing;
155     int save_err = errno;
156 
157     if (strlen(fmt) >= sizeof(Called) - 1) {
158 	if (!strncmp(fmt, Called, sizeof(Called) - 1)) {
159 	    before = TRUE;
160 	    level++;
161 	} else if (!strncmp(fmt, Return, sizeof(Return) - 1)) {
162 	    after = TRUE;
163 	}
164 	if (before || after) {
165 	    if ((level <= 1)
166 		|| (doit & TRACE_ICALLS) != 0)
167 		doit &= (TRACE_CALLS | TRACE_CCALLS);
168 	    else
169 		doit = 0;
170 	}
171     }
172 
173     if (doit != 0) {
174 	if (tracefp == 0)
175 	    tracefp = stderr;
176 	if (before || after) {
177 	    int n;
178 	    for (n = 1; n < level; n++)
179 		fputs("+ ", tracefp);
180 	}
181 	va_start(ap, fmt);
182 	vfprintf(tracefp, fmt, ap);
183 	fputc('\n', tracefp);
184 	va_end(ap);
185 	fflush(tracefp);
186     }
187 
188     if (after && level)
189 	level--;
190     errno = save_err;
191 }
192 
193 /* Trace 'int' return-values */
194 int
195 _nc_retrace_int(int code)
196 {
197     T((T_RETURN("%d"), code));
198     return code;
199 }
200 
201 /* Trace 'char*' return-values */
202 char *
203 _nc_retrace_ptr(char *code)
204 {
205     T((T_RETURN("%s"), _nc_visbuf(code)));
206     return code;
207 }
208 
209 /* Trace 'WINDOW *' return-values */
210 WINDOW *
211 _nc_retrace_win(WINDOW *code)
212 {
213     T((T_RETURN("%p"), code));
214     return code;
215 }
216 #endif /* TRACE */
217