1 /****************************************************************************
2 * Copyright (c) 1998 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 ****************************************************************************/
35 * lib_trace.c - Tracing/Debugging routines
38 #include <curses.priv.h>
41 MODULE_ID("$Id: lib_trace.c,v 1.30 1998/10/03 23:41:42 tom Exp $")
48 unsigned _nc_tracing = 0; /* always define this */
51 const char *_nc_tputs_trace = "";
54 static FILE * tracefp; /* default to writing to stderr */
57 void trace(const unsigned int tracelevel GCC_UNUSED)
60 static bool been_here = FALSE;
61 static char my_name[] = "trace";
63 _nc_tracing = tracelevel;
64 if (! been_here && tracelevel) {
67 if (_nc_access(my_name, W_OK) < 0
68 || (tracefp = fopen(my_name, "w")) == 0) {
69 perror("curses: Can't open 'trace' file: ");
72 /* Try to set line-buffered mode, or (failing that) unbuffered,
73 * so that the trace-output gets flushed automatically at the
74 * end of each line. This is useful in case the program dies.
76 #if HAVE_SETVBUF /* ANSI */
77 (void) setvbuf(tracefp, (char *)0, _IOLBF, 0);
78 #elif HAVE_SETBUF /* POSIX */
79 (void) setbuffer(tracefp, (char *)0);
81 _tracef("TRACING NCURSES version %s (%d)",
82 NCURSES_VERSION, NCURSES_VERSION_PATCH);
87 const char *_nc_visbuf2(int bufnum, const char *buf)
88 /* visibilize a given string */
96 if (buf == CANCELLED_STRING)
97 return("(cancelled)");
99 tp = vbuf = _nc_trace_buf(bufnum, (strlen(buf) * 4) + 5);
101 while ((c = *buf++) != '\0') {
103 *tp++ = '\\'; *tp++ = '"';
104 } else if (is7bits(c) && (isgraph(c) || c == ' ')) {
106 } else if (c == '\n') {
107 *tp++ = '\\'; *tp++ = 'n';
108 } else if (c == '\r') {
109 *tp++ = '\\'; *tp++ = 'r';
110 } else if (c == '\b') {
111 *tp++ = '\\'; *tp++ = 'b';
112 } else if (c == '\033') {
113 *tp++ = '\\'; *tp++ = 'e';
114 } else if (is7bits(c) && iscntrl(c)) {
115 *tp++ = '\\'; *tp++ = '^'; *tp++ = '@' + c;
117 sprintf(tp, "\\%03o", c & 0xff);
126 const char *_nc_visbuf(const char *buf)
128 return _nc_visbuf2(0, buf);
133 _tracef(const char *fmt, ...)
135 static const char Called[] = T_CALLED("");
136 static const char Return[] = T_RETURN("");
141 int doit = _nc_tracing;
142 int save_err = errno;
144 if (strlen(fmt) >= sizeof(Called) - 1) {
145 if (!strncmp(fmt, Called, sizeof(Called)-1)) {
148 } else if (!strncmp(fmt, Return, sizeof(Return)-1)) {
151 if (before || after) {
153 || (doit & TRACE_ICALLS) != 0)
154 doit &= (TRACE_CALLS|TRACE_CCALLS);
163 if (before || after) {
165 for (n = 1; n < level; n++)
166 fputs("+ ", tracefp);
169 vfprintf(tracefp, fmt, ap);
170 fputc('\n', tracefp);
180 /* Trace 'int' return-values */
181 int _nc_retrace_int(int code)
183 T((T_RETURN("%d"), code));
187 /* Trace 'char*' return-values */
188 char * _nc_retrace_ptr(char * code)
190 T((T_RETURN("%s"), _nc_visbuf(code)));
194 /* Trace 'WINDOW *' return-values */
195 WINDOW *_nc_retrace_win(WINDOW *code)
197 T((T_RETURN("%p"), code));