xref: /freebsd/contrib/dialog/trace.c (revision 36712a94975f5bd0d26c85377283b49a2369c82f)
1 /*
2  *  $Id: trace.c,v 1.26 2018/06/13 00:06:48 tom Exp $
3  *
4  *  trace.c -- implements screen-dump and keystroke-logging
5  *
6  *  Copyright 2007-2017,2018	Thomas E. Dickey
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU Lesser General Public License, version 2.1
10  *
11  *  This program is distributed in the hope that it will be useful, but
12  *  WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this program; if not, write to
18  *	Free Software Foundation, Inc.
19  *	51 Franklin St., Fifth Floor
20  *	Boston, MA 02110, USA.
21  */
22 
23 #include <dialog.h>
24 
25 #ifdef HAVE_DLG_TRACE
26 
27 #ifdef NEED_WCHAR_H
28 #include <wchar.h>
29 #endif
30 
31 #include <dlg_keys.h>
32 #include <time.h>
33 
34 #define myFP dialog_state.trace_output
35 
36 static void
37 dlg_trace_time(const char *tag)
38 {
39     time_t now = time((time_t *) 0);
40     fprintf(myFP, "%s %s", tag, ctime(&now));
41 }
42 
43 void
44 dlg_trace_msg(const char *fmt,...)
45 {
46     if (myFP != 0) {
47 	va_list ap;
48 	va_start(ap, fmt);
49 	vfprintf(myFP, fmt, ap);
50 	va_end(ap);
51 	fflush(myFP);
52     }
53 }
54 
55 void
56 dlg_trace_2s(const char *name, const char *value)
57 {
58     bool first = TRUE;
59     const char *next;
60     int left, right = 0;
61 
62     if (value == 0)
63 	value = "<NULL>";
64 
65     while (value[right] != '\0') {
66 	value += right;
67 	if ((next = strchr(value, '\n')) != 0) {
68 	    left = (int) (next - value);
69 	    right = left + 1;
70 	} else {
71 	    left = (int) strlen(value);
72 	    right = left;
73 	}
74 	if (first) {
75 	    first = FALSE;
76 	    dlg_trace_msg("#%14s=%.*s\n", name, left, value);
77 	} else {
78 	    dlg_trace_msg("#+\t\t%.*s\n", left, value);
79 	}
80     }
81 }
82 
83 void
84 dlg_trace_2n(const char *name, int value)
85 {
86     dlg_trace_msg("#\t%7s=%d\n", name, value);
87 }
88 
89 void
90 dlg_trace_win(WINDOW *win)
91 {
92     if (myFP != 0) {
93 	int y, x;
94 	int j, k;
95 	WINDOW *top = wgetparent(win);
96 
97 	while (top != 0 && top != stdscr) {
98 	    win = top;
99 	    top = wgetparent(win);
100 	}
101 
102 	if (win != 0) {
103 	    int rc = getmaxy(win);
104 	    int cc = getmaxx(win);
105 	    chtype ch, c2;
106 
107 	    fprintf(myFP, "window %dx%d at %d,%d\n",
108 		    rc, cc, getbegy(win), getbegx(win));
109 
110 	    getyx(win, y, x);
111 	    for (j = 0; j < rc; ++j) {
112 		fprintf(myFP, "%3d:", j);
113 		for (k = 0; k < cc; ++k) {
114 #ifdef USE_WIDE_CURSES
115 		    char buffer[80];
116 
117 		    ch = mvwinch(win, j, k) & (A_CHARTEXT | A_ALTCHARSET);
118 		    if (ch & A_ALTCHARSET) {
119 			c2 = dlg_asciibox(ch);
120 			if (c2 != 0) {
121 			    ch = c2;
122 			}
123 			buffer[0] = (char) ch;
124 			buffer[1] = '\0';
125 		    } else {
126 			cchar_t cch;
127 			wchar_t *uc;
128 
129 			if (win_wch(win, &cch) == ERR
130 			    || (uc = wunctrl((&cch))) == 0
131 			    || uc[1] != 0
132 			    || wcwidth(uc[0]) <= 0) {
133 			    buffer[0] = '.';
134 			    buffer[1] = '\0';
135 			} else {
136 			    mbstate_t state;
137 			    const wchar_t *ucp = uc;
138 
139 			    memset(&state, 0, sizeof(state));
140 			    wcsrtombs(buffer, &ucp, sizeof(buffer), &state);
141 			    k += wcwidth(uc[0]) - 1;
142 			}
143 		    }
144 		    fputs(buffer, myFP);
145 #else
146 		    ch = mvwinch(win, j, k) & (A_CHARTEXT | A_ALTCHARSET);
147 		    c2 = dlg_asciibox(ch);
148 		    if (c2 != 0) {
149 			ch = c2;
150 		    } else if (unctrl(ch) == 0 || strlen(unctrl(ch)) > 1) {
151 			ch = '.';
152 		    }
153 		    fputc((int) (ch & 0xff), myFP);
154 #endif
155 		}
156 		fputc('\n', myFP);
157 	    }
158 	    wmove(win, y, x);
159 	    fflush(myFP);
160 	}
161     }
162 }
163 
164 void
165 dlg_trace_chr(int ch, int fkey)
166 {
167     static int last_err = 0;
168 
169     /*
170      * Do not bother to trace ERR's indefinitely, since those are usually due
171      * to relatively short polling timeouts.
172      */
173     if (last_err && !fkey && ch == ERR) {
174 	++last_err;
175     } else if (myFP != 0) {
176 	const char *fkey_name = "?";
177 
178 	if (last_err) {
179 	    fprintf(myFP, "skipped %d ERR's\n", last_err);
180 	    last_err = 0;
181 	}
182 
183 	if (fkey) {
184 	    if (fkey > KEY_MAX || (fkey_name = keyname(fkey)) == 0) {
185 #define CASE(name) case name: fkey_name = #name; break
186 		switch ((DLG_KEYS_ENUM) fkey) {
187 		    CASE(DLGK_MIN);
188 		    CASE(DLGK_OK);
189 		    CASE(DLGK_CANCEL);
190 		    CASE(DLGK_EXTRA);
191 		    CASE(DLGK_HELP);
192 		    CASE(DLGK_ESC);
193 		    CASE(DLGK_PAGE_FIRST);
194 		    CASE(DLGK_PAGE_LAST);
195 		    CASE(DLGK_PAGE_NEXT);
196 		    CASE(DLGK_PAGE_PREV);
197 		    CASE(DLGK_ITEM_FIRST);
198 		    CASE(DLGK_ITEM_LAST);
199 		    CASE(DLGK_ITEM_NEXT);
200 		    CASE(DLGK_ITEM_PREV);
201 		    CASE(DLGK_FIELD_FIRST);
202 		    CASE(DLGK_FIELD_LAST);
203 		    CASE(DLGK_FIELD_NEXT);
204 		    CASE(DLGK_FIELD_PREV);
205 		    CASE(DLGK_FORM_FIRST);
206 		    CASE(DLGK_FORM_LAST);
207 		    CASE(DLGK_FORM_NEXT);
208 		    CASE(DLGK_FORM_PREV);
209 		    CASE(DLGK_GRID_UP);
210 		    CASE(DLGK_GRID_DOWN);
211 		    CASE(DLGK_GRID_LEFT);
212 		    CASE(DLGK_GRID_RIGHT);
213 		    CASE(DLGK_DELETE_LEFT);
214 		    CASE(DLGK_DELETE_RIGHT);
215 		    CASE(DLGK_DELETE_ALL);
216 		    CASE(DLGK_ENTER);
217 		    CASE(DLGK_BEGIN);
218 		    CASE(DLGK_FINAL);
219 		    CASE(DLGK_SELECT);
220 		    CASE(DLGK_HELPFILE);
221 		    CASE(DLGK_TRACE);
222 		    CASE(DLGK_TOGGLE);
223 		}
224 	    }
225 	} else if (ch == ERR) {
226 	    fkey_name = "ERR";
227 	    last_err = 1;
228 	} else {
229 	    fkey_name = unctrl((chtype) ch);
230 	    if (fkey_name == 0)
231 		fkey_name = "UNKNOWN";
232 	}
233 	if (ch >= 0) {
234 	    fprintf(myFP, "chr %s (ch=%#x, fkey=%d)\n", fkey_name, ch, fkey);
235 	} else {
236 	    fprintf(myFP, "chr %s (ch=%d, fkey=%d)\n", fkey_name, ch, fkey);
237 	}
238 	fflush(myFP);
239     }
240 }
241 
242 void
243 dlg_trace(const char *fname)
244 {
245     if (fname != 0) {
246 	if (myFP == 0) {
247 	    myFP = fopen(fname, "a");
248 	    if (myFP != 0) {
249 		dlg_trace_time("## opened at");
250 		DLG_TRACE(("## dialog %s\n", dialog_version()));
251 		DLG_TRACE(("## vile: confmode\n"));
252 	    }
253 	}
254     } else if (myFP != 0) {
255 	dlg_trace_time("## closed at");
256 	fclose(myFP);
257 	myFP = 0;
258     }
259 }
260 #else
261 #undef dlg_trace
262 extern void dlg_trace(const char *);
263 void
264 dlg_trace(const char *fname)
265 {
266     (void) fname;
267 }
268 #endif
269