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