aa4b887111a7cd7ac2c3b94d4acb270c19e55a51
[ncurses.git] / ncurses / trace / 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 #include <curses.priv.h>
39 #include <tic.h>
40
41 MODULE_ID("$Id: lib_trace.c,v 1.30 1998/10/03 23:41:42 tom Exp $")
42
43 #include <ctype.h>
44 #if HAVE_FCNTL_H
45 #include <fcntl.h>
46 #endif
47
48 unsigned _nc_tracing = 0;       /* always define this */
49
50 #ifdef TRACE
51 const char *_nc_tputs_trace = "";
52 long _nc_outchars;
53
54 static FILE *   tracefp;        /* default to writing to stderr */
55 #endif
56
57 void trace(const unsigned int tracelevel GCC_UNUSED)
58 {
59 #ifdef TRACE
60 static bool     been_here = FALSE;
61 static char     my_name[] = "trace";
62
63         _nc_tracing = tracelevel;
64         if (! been_here && tracelevel) {
65                 been_here = TRUE;
66
67                 if (_nc_access(my_name, W_OK) < 0
68                  || (tracefp = fopen(my_name, "w")) == 0) {
69                         perror("curses: Can't open 'trace' file: ");
70                         exit(EXIT_FAILURE);
71                 }
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. 
75                  */
76 #if HAVE_SETVBUF        /* ANSI */
77                 (void) setvbuf(tracefp, (char *)0, _IOLBF, 0);
78 #elif HAVE_SETBUF       /* POSIX */
79                 (void) setbuffer(tracefp, (char *)0);
80 #endif
81                 _tracef("TRACING NCURSES version %s (%d)",
82                         NCURSES_VERSION, NCURSES_VERSION_PATCH);
83         }
84 #endif
85 }
86
87 const char *_nc_visbuf2(int bufnum, const char *buf)
88 /* visibilize a given string */
89 {
90 char *vbuf;
91 char *tp;
92 int c;
93
94         if (buf == 0)
95             return("(null)");
96         if (buf == CANCELLED_STRING)
97             return("(cancelled)");
98
99         tp = vbuf = _nc_trace_buf(bufnum, (strlen(buf) * 4) + 5);
100         *tp++ = '"';
101         while ((c = *buf++) != '\0') {
102                 if (c == '"') {
103                         *tp++ = '\\'; *tp++ = '"';
104                 } else if (is7bits(c) && (isgraph(c) || c == ' ')) {
105                         *tp++ = 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;
116                 } else {
117                         sprintf(tp, "\\%03o", c & 0xff);
118                         tp += strlen(tp);
119                 }
120         }
121         *tp++ = '"';
122         *tp++ = '\0';
123         return(vbuf);
124 }
125
126 const char *_nc_visbuf(const char *buf)
127 {
128         return _nc_visbuf2(0, buf);
129 }
130
131 #ifdef TRACE
132 void
133 _tracef(const char *fmt, ...)
134 {
135 static const char Called[] = T_CALLED("");
136 static const char Return[] = T_RETURN("");
137 static int level;
138 va_list ap;
139 bool    before = FALSE;
140 bool    after = FALSE;
141 int     doit = _nc_tracing;
142 int     save_err = errno;
143
144         if (strlen(fmt) >= sizeof(Called) - 1) {
145                 if (!strncmp(fmt, Called, sizeof(Called)-1)) {
146                         before = TRUE;
147                         level++;
148                 } else if (!strncmp(fmt, Return, sizeof(Return)-1)) {
149                         after = TRUE;
150                 }
151                 if (before || after) {
152                         if ((level <= 1)
153                          || (doit & TRACE_ICALLS) != 0)
154                                 doit &= (TRACE_CALLS|TRACE_CCALLS);
155                         else
156                                 doit = 0;
157                 }
158         }
159
160         if (doit != 0) {
161                 if (tracefp == 0)
162                         tracefp = stderr;
163                 if (before || after) {
164                         int n;
165                         for (n = 1; n < level; n++)
166                                 fputs("+ ", tracefp);
167                 }
168                 va_start(ap, fmt);
169                 vfprintf(tracefp, fmt, ap);
170                 fputc('\n', tracefp);
171                 va_end(ap);
172                 fflush(tracefp);
173         }
174
175         if (after && level)
176                 level--;
177         errno = save_err;
178 }
179
180 /* Trace 'int' return-values */
181 int _nc_retrace_int(int code)
182 {
183         T((T_RETURN("%d"), code));
184         return code;
185 }
186
187 /* Trace 'char*' return-values */
188 char * _nc_retrace_ptr(char * code)
189 {
190         T((T_RETURN("%s"), _nc_visbuf(code)));
191         return code;
192 }
193
194 /* Trace 'WINDOW *' return-values */
195 WINDOW *_nc_retrace_win(WINDOW *code)
196 {
197         T((T_RETURN("%p"), code));
198         return code;
199 }
200 #endif /* TRACE */