lib_trace.c revision 1.4
1/* $OpenBSD: lib_trace.c,v 1.4 2000/03/10 01:35:05 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 45MODULE_ID("$From: lib_trace.c,v 1.33 2000/02/13 01:01:55 tom Exp $") 46 47unsigned _nc_tracing = 0; /* always define this */ 48 49#ifdef TRACE 50const char *_nc_tputs_trace = ""; 51long _nc_outchars = 0; 52 53static FILE *tracefp; /* default to writing to stderr */ 54#endif 55 56void 57trace(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 87const 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 tp = vbuf = _nc_trace_buf(bufnum, (strlen(buf) * 4) + 5); 101 *tp++ = '"'; 102 while ((c = *buf++) != '\0') { 103 if (c == '"') { 104 *tp++ = '\\'; 105 *tp++ = '"'; 106 } else if (is7bits(c) && (isgraph(c) || c == ' ')) { 107 *tp++ = c; 108 } else if (c == '\n') { 109 *tp++ = '\\'; 110 *tp++ = 'n'; 111 } else if (c == '\r') { 112 *tp++ = '\\'; 113 *tp++ = 'r'; 114 } else if (c == '\b') { 115 *tp++ = '\\'; 116 *tp++ = 'b'; 117 } else if (c == '\033') { 118 *tp++ = '\\'; 119 *tp++ = 'e'; 120 } else if (is7bits(c) && iscntrl(c)) { 121 *tp++ = '\\'; 122 *tp++ = '^'; 123 *tp++ = '@' + c; 124 } else { 125 sprintf(tp, "\\%03o", c & 0xff); 126 tp += strlen(tp); 127 } 128 } 129 *tp++ = '"'; 130 *tp++ = '\0'; 131 return (vbuf); 132} 133 134const char * 135_nc_visbuf(const char *buf) 136{ 137 return _nc_visbuf2(0, buf); 138} 139 140#ifdef TRACE 141void 142_tracef(const char *fmt,...) 143{ 144 static const char Called[] = T_CALLED(""); 145 static const char Return[] = T_RETURN(""); 146 static int level; 147 va_list ap; 148 bool before = FALSE; 149 bool after = FALSE; 150 int doit = _nc_tracing; 151 int save_err = errno; 152 153 if (strlen(fmt) >= sizeof(Called) - 1) { 154 if (!strncmp(fmt, Called, sizeof(Called) - 1)) { 155 before = TRUE; 156 level++; 157 } else if (!strncmp(fmt, Return, sizeof(Return) - 1)) { 158 after = TRUE; 159 } 160 if (before || after) { 161 if ((level <= 1) 162 || (doit & TRACE_ICALLS) != 0) 163 doit &= (TRACE_CALLS | TRACE_CCALLS); 164 else 165 doit = 0; 166 } 167 } 168 169 if (doit != 0) { 170 if (tracefp == 0) 171 tracefp = stderr; 172 if (before || after) { 173 int n; 174 for (n = 1; n < level; n++) 175 fputs("+ ", tracefp); 176 } 177 va_start(ap, fmt); 178 vfprintf(tracefp, fmt, ap); 179 fputc('\n', tracefp); 180 va_end(ap); 181 fflush(tracefp); 182 } 183 184 if (after && level) 185 level--; 186 errno = save_err; 187} 188 189/* Trace 'int' return-values */ 190int 191_nc_retrace_int(int code) 192{ 193 T((T_RETURN("%d"), code)); 194 return code; 195} 196 197/* Trace 'char*' return-values */ 198char * 199_nc_retrace_ptr(char *code) 200{ 201 T((T_RETURN("%s"), _nc_visbuf(code))); 202 return code; 203} 204 205/* Trace 'WINDOW *' return-values */ 206WINDOW * 207_nc_retrace_win(WINDOW *code) 208{ 209 T((T_RETURN("%p"), code)); 210 return code; 211} 212#endif /* TRACE */ 213