xref: /openbsd-src/lib/libcurses/trace/lib_trace.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: lib_trace.c,v 1.6 2001/01/22 18:01:58 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     char *vbuf;
92     char *tp;
93     int c;
94 
95     if (buf == 0)
96 	return ("(null)");
97     if (buf == CANCELLED_STRING)
98 	return ("(cancelled)");
99 
100 #ifdef TRACE
101     tp = vbuf = _nc_trace_buf(bufnum, (strlen(buf) * 4) + 5);
102 #else
103     {
104 	static char *mybuf[2];
105 	mybuf[bufnum] = _nc_doalloc(mybuf[bufnum], (strlen(buf) * 4) + 5);
106 	tp = vbuf = mybuf[bufnum];
107     }
108 #endif
109     *tp++ = '"';
110     while ((c = *buf++) != '\0') {
111 	if (c == '"') {
112 	    *tp++ = '\\';
113 	    *tp++ = '"';
114 	} else if (is7bits(c) && (isgraph(c) || c == ' ')) {
115 	    *tp++ = c;
116 	} else if (c == '\n') {
117 	    *tp++ = '\\';
118 	    *tp++ = 'n';
119 	} else if (c == '\r') {
120 	    *tp++ = '\\';
121 	    *tp++ = 'r';
122 	} else if (c == '\b') {
123 	    *tp++ = '\\';
124 	    *tp++ = 'b';
125 	} else if (c == '\033') {
126 	    *tp++ = '\\';
127 	    *tp++ = 'e';
128 	} else if (is7bits(c) && iscntrl(c)) {
129 	    *tp++ = '\\';
130 	    *tp++ = '^';
131 	    *tp++ = '@' + c;
132 	} else {
133 	    sprintf(tp, "\\%03o", CharOf(c));
134 	    tp += strlen(tp);
135 	}
136     }
137     *tp++ = '"';
138     *tp++ = '\0';
139     return (vbuf);
140 }
141 
142 NCURSES_EXPORT(const char *)
143 _nc_visbuf(const char *buf)
144 {
145     return _nc_visbuf2(0, buf);
146 }
147 
148 #ifdef TRACE
149 NCURSES_EXPORT(void)
150 _tracef(const char *fmt,...)
151 {
152     static const char Called[] = T_CALLED("");
153     static const char Return[] = T_RETURN("");
154     static int level;
155     va_list ap;
156     bool before = FALSE;
157     bool after = FALSE;
158     int doit = _nc_tracing;
159     int save_err = errno;
160 
161     if (strlen(fmt) >= sizeof(Called) - 1) {
162 	if (!strncmp(fmt, Called, sizeof(Called) - 1)) {
163 	    before = TRUE;
164 	    level++;
165 	} else if (!strncmp(fmt, Return, sizeof(Return) - 1)) {
166 	    after = TRUE;
167 	}
168 	if (before || after) {
169 	    if ((level <= 1)
170 		|| (doit & TRACE_ICALLS) != 0)
171 		doit &= (TRACE_CALLS | TRACE_CCALLS);
172 	    else
173 		doit = 0;
174 	}
175     }
176 
177     if (doit != 0) {
178 	if (tracefp == 0)
179 	    tracefp = stderr;
180 	if (before || after) {
181 	    int n;
182 	    for (n = 1; n < level; n++)
183 		fputs("+ ", tracefp);
184 	}
185 	va_start(ap, fmt);
186 	vfprintf(tracefp, fmt, ap);
187 	fputc('\n', tracefp);
188 	va_end(ap);
189 	fflush(tracefp);
190     }
191 
192     if (after && level)
193 	level--;
194     errno = save_err;
195 }
196 
197 /* Trace 'int' return-values */
198 NCURSES_EXPORT(int)
199 _nc_retrace_int(int code)
200 {
201     T((T_RETURN("%d"), code));
202     return code;
203 }
204 
205 /* Trace 'char*' return-values */
206 NCURSES_EXPORT(char *)
207 _nc_retrace_ptr(char *code)
208 {
209     T((T_RETURN("%s"), _nc_visbuf(code)));
210     return code;
211 }
212 
213 /* Trace 'WINDOW *' return-values */
214 NCURSES_EXPORT(WINDOW *)
215 _nc_retrace_win(WINDOW *code)
216 {
217     T((T_RETURN("%p"), code));
218     return code;
219 }
220 #endif /* TRACE */
221