xref: /openbsd-src/lib/libcurses/trace/lib_trace.c (revision 40ecf94db7fa4502a9387680c2502ce8909185e3)
1 /*	$OpenBSD: lib_trace.c,v 1.5 2000/04/04 16:49:59 millert Exp $	*/
2 
3 /****************************************************************************
4  * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc.              *
5  *                                                                          *
6  * Permission is hereby granted, free of charge, to any person obtaining a  *
7  * copy of this software and associated documentation files (the            *
8  * "Software"), to deal in the Software without restriction, including      *
9  * without limitation the rights to use, copy, modify, merge, publish,      *
10  * distribute, distribute with modifications, sublicense, and/or sell       *
11  * copies of the Software, and to permit persons to whom the Software is    *
12  * furnished to do so, subject to the following conditions:                 *
13  *                                                                          *
14  * The above copyright notice and this permission notice shall be included  *
15  * in all copies or substantial portions of the Software.                   *
16  *                                                                          *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24  *                                                                          *
25  * Except as contained in this notice, the name(s) of the above copyright   *
26  * holders shall not be used in advertising or otherwise to promote the     *
27  * sale, use or other dealings in this Software without prior written       *
28  * authorization.                                                           *
29  ****************************************************************************/
30 
31 /****************************************************************************
32  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
33  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
34  ****************************************************************************/
35 
36 /*
37  *	lib_trace.c - Tracing/Debugging routines
38  */
39 
40 #include <curses.priv.h>
41 #include <tic.h>
42 
43 #include <ctype.h>
44 
45 MODULE_ID("$From: lib_trace.c,v 1.34 2000/04/01 20:25:47 tom Exp $")
46 
47 unsigned _nc_tracing = 0;	/* always define this */
48 
49 #ifdef TRACE
50 const char *_nc_tputs_trace = "";
51 long _nc_outchars = 0;
52 
53 static FILE *tracefp;		/* default to writing to stderr */
54 
55 void
56 trace(const unsigned int tracelevel GCC_UNUSED)
57 {
58     static bool been_here = FALSE;
59     static char my_name[] = "trace";
60 
61     _nc_tracing = tracelevel;
62     if (!been_here && tracelevel) {
63 	been_here = TRUE;
64 
65 	if (_nc_access(my_name, W_OK) < 0
66 	    || (tracefp = fopen(my_name, "w")) == 0) {
67 	    perror("curses: Can't open 'trace' file: ");
68 	    exit(EXIT_FAILURE);
69 	}
70 	/* Try to set line-buffered mode, or (failing that) unbuffered,
71 	 * so that the trace-output gets flushed automatically at the
72 	 * end of each line.  This is useful in case the program dies.
73 	 */
74 #if HAVE_SETVBUF		/* ANSI */
75 	(void) setvbuf(tracefp, (char *) 0, _IOLBF, 0);
76 #elif HAVE_SETBUF		/* POSIX */
77 	(void) setbuffer(tracefp, (char *) 0);
78 #endif
79 	_tracef("TRACING NCURSES version %s (%d)",
80 	    NCURSES_VERSION, NCURSES_VERSION_PATCH);
81     }
82 }
83 #endif
84 
85 const char *
86 _nc_visbuf2(int bufnum, const char *buf)
87 /* visibilize a given string */
88 {
89     char *vbuf;
90     char *tp;
91     int c;
92 
93     if (buf == 0)
94 	return ("(null)");
95     if (buf == CANCELLED_STRING)
96 	return ("(cancelled)");
97 
98 #ifdef TRACE
99     tp = vbuf = _nc_trace_buf(bufnum, (strlen(buf) * 4) + 5);
100 #else
101     {
102     static char *mybuf[2];
103     mybuf[bufnum] = _nc_doalloc(mybuf[bufnum], (strlen(buf) * 4) + 5);
104     tp = vbuf = mybuf[bufnum];
105     }
106 #endif
107     *tp++ = '"';
108     while ((c = *buf++) != '\0') {
109 	if (c == '"') {
110 	    *tp++ = '\\';
111 	    *tp++ = '"';
112 	} else if (is7bits(c) && (isgraph(c) || c == ' ')) {
113 	    *tp++ = c;
114 	} else if (c == '\n') {
115 	    *tp++ = '\\';
116 	    *tp++ = 'n';
117 	} else if (c == '\r') {
118 	    *tp++ = '\\';
119 	    *tp++ = 'r';
120 	} else if (c == '\b') {
121 	    *tp++ = '\\';
122 	    *tp++ = 'b';
123 	} else if (c == '\033') {
124 	    *tp++ = '\\';
125 	    *tp++ = 'e';
126 	} else if (is7bits(c) && iscntrl(c)) {
127 	    *tp++ = '\\';
128 	    *tp++ = '^';
129 	    *tp++ = '@' + c;
130 	} else {
131 	    sprintf(tp, "\\%03o", c & 0xff);
132 	    tp += strlen(tp);
133 	}
134     }
135     *tp++ = '"';
136     *tp++ = '\0';
137     return (vbuf);
138 }
139 
140 const char *
141 _nc_visbuf(const char *buf)
142 {
143     return _nc_visbuf2(0, buf);
144 }
145 
146 #ifdef TRACE
147 void
148 _tracef(const char *fmt,...)
149 {
150     static const char Called[] = T_CALLED("");
151     static const char Return[] = T_RETURN("");
152     static int level;
153     va_list ap;
154     bool before = FALSE;
155     bool after = FALSE;
156     int doit = _nc_tracing;
157     int save_err = errno;
158 
159     if (strlen(fmt) >= sizeof(Called) - 1) {
160 	if (!strncmp(fmt, Called, sizeof(Called) - 1)) {
161 	    before = TRUE;
162 	    level++;
163 	} else if (!strncmp(fmt, Return, sizeof(Return) - 1)) {
164 	    after = TRUE;
165 	}
166 	if (before || after) {
167 	    if ((level <= 1)
168 		|| (doit & TRACE_ICALLS) != 0)
169 		doit &= (TRACE_CALLS | TRACE_CCALLS);
170 	    else
171 		doit = 0;
172 	}
173     }
174 
175     if (doit != 0) {
176 	if (tracefp == 0)
177 	    tracefp = stderr;
178 	if (before || after) {
179 	    int n;
180 	    for (n = 1; n < level; n++)
181 		fputs("+ ", tracefp);
182 	}
183 	va_start(ap, fmt);
184 	vfprintf(tracefp, fmt, ap);
185 	fputc('\n', tracefp);
186 	va_end(ap);
187 	fflush(tracefp);
188     }
189 
190     if (after && level)
191 	level--;
192     errno = save_err;
193 }
194 
195 /* Trace 'int' return-values */
196 int
197 _nc_retrace_int(int code)
198 {
199     T((T_RETURN("%d"), code));
200     return code;
201 }
202 
203 /* Trace 'char*' return-values */
204 char *
205 _nc_retrace_ptr(char *code)
206 {
207     T((T_RETURN("%s"), _nc_visbuf(code)));
208     return code;
209 }
210 
211 /* Trace 'WINDOW *' return-values */
212 WINDOW *
213 _nc_retrace_win(WINDOW *code)
214 {
215     T((T_RETURN("%p"), code));
216     return code;
217 }
218 #endif /* TRACE */
219