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