]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/lib_trace.c
ncurses 4.2
[ncurses.git] / ncurses / lib_trace.c
1 /****************************************************************************
2  * Copyright (c) 1998 Free Software Foundation, Inc.                        *
3  *                                                                          *
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:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
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.                               *
22  *                                                                          *
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       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  ****************************************************************************/
33
34 /*
35  *      lib_trace.c - Tracing/Debugging routines
36  */
37
38 #ifndef TRACE
39 #define TRACE                   /* turn on internal defs for this module */
40 #endif
41
42 #include <curses.priv.h>
43
44 MODULE_ID("$Id: lib_trace.c,v 1.25 1998/02/11 12:13:55 tom Exp $")
45
46 #include <ctype.h>
47 #if HAVE_FCNTL_H
48 #include <fcntl.h>
49 #endif
50
51 unsigned _nc_tracing = 0;
52 const char *_nc_tputs_trace = "";
53 long _nc_outchars;
54 int _nc_optimize_enable = OPTIMIZE_ALL;
55
56 static FILE *   tracefp;        /* default to writing to stderr */
57
58 void trace(const unsigned int tracelevel)
59 {
60 static bool     been_here = FALSE;
61
62         _nc_tracing = tracelevel;
63         if (! been_here && tracelevel) {
64                 been_here = TRUE;
65
66                 if ((tracefp = fopen("trace", "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
84 const char *_nc_visbuf2(int bufnum, const char *buf)
85 /* visibilize a given string */
86 {
87 char *vbuf;
88 char *tp;
89 int c;
90
91         if (buf == 0)
92             return("(null)");
93
94         tp = vbuf = _nc_trace_buf(bufnum, (strlen(buf) * 4) + 5);
95         *tp++ = '"';
96         while ((c = *buf++) != '\0') {
97                 if (c == '"') {
98                         *tp++ = '\\'; *tp++ = '"';
99                 } else if (is7bits(c) && (isgraph(c) || c == ' ')) {
100                         *tp++ = c;
101                 } else if (c == '\n') {
102                         *tp++ = '\\'; *tp++ = 'n';
103                 } else if (c == '\r') {
104                         *tp++ = '\\'; *tp++ = 'r';
105                 } else if (c == '\b') {
106                         *tp++ = '\\'; *tp++ = 'b';
107                 } else if (c == '\033') {
108                         *tp++ = '\\'; *tp++ = 'e';
109                 } else if (is7bits(c) && iscntrl(c)) {
110                         *tp++ = '\\'; *tp++ = '^'; *tp++ = '@' + c;
111                 } else {
112                         sprintf(tp, "\\%03o", c & 0xff);
113                         tp += strlen(tp);
114                 }
115         }
116         *tp++ = '"';
117         *tp++ = '\0';
118         return(vbuf);
119 }
120
121 const char *_nc_visbuf(const char *buf)
122 {
123         return _nc_visbuf2(0, buf);
124 }
125
126 void
127 _tracef(const char *fmt, ...)
128 {
129 static const char Called[] = T_CALLED("");
130 static const char Return[] = T_RETURN("");
131 static int level;
132 va_list ap;
133 bool    before = FALSE;
134 bool    after = FALSE;
135 int     doit = _nc_tracing;
136 int     save_err = errno;
137
138         if (strlen(fmt) >= sizeof(Called) - 1) {
139                 if (!strncmp(fmt, Called, sizeof(Called)-1)) {
140                         before = TRUE;
141                         level++;
142                 } else if (!strncmp(fmt, Return, sizeof(Return)-1)) {
143                         after = TRUE;
144                 }
145                 if (before || after) {
146                         if ((level <= 1)
147                          || (doit & TRACE_ICALLS) != 0)
148                                 doit &= (TRACE_CALLS|TRACE_CCALLS);
149                         else
150                                 doit = 0;
151                 }
152         }
153
154         if (doit != 0) {
155                 if (tracefp == 0)
156                         tracefp = stderr;
157                 if (before || after) {
158                         int n;
159                         for (n = 1; n < level; n++)
160                                 fputs("+ ", tracefp);
161                 }
162                 va_start(ap, fmt);
163                 vfprintf(tracefp, fmt, ap);
164                 fputc('\n', tracefp);
165                 va_end(ap);
166                 fflush(tracefp);
167         }
168
169         if (after && level)
170                 level--;
171         errno = save_err;
172 }
173
174 /* Trace 'int' return-values */
175 int _nc_retrace_int(int code)
176 {
177         T((T_RETURN("%d"), code));
178         return code;
179 }
180
181 /* Trace 'char*' return-values */
182 char * _nc_retrace_ptr(char * code)
183 {
184         T((T_RETURN("%s"), _nc_visbuf(code)));
185         return code;
186 }
187
188 /* Trace 'WINDOW *' return-values */
189 WINDOW *_nc_retrace_win(WINDOW *code)
190 {
191         T((T_RETURN("%p"), code));
192         return code;
193 }