183cbb37nwhitehorn/*
2f8604a5bapt *  $Id: trace.c,v 1.26 2018/06/13 00:06:48 tom Exp $
383cbb37nwhitehorn *
483cbb37nwhitehorn *  trace.c -- implements screen-dump and keystroke-logging
583cbb37nwhitehorn *
6f8604a5bapt *  Copyright 2007-2017,2018	Thomas E. Dickey
783cbb37nwhitehorn *
883cbb37nwhitehorn *  This program is free software; you can redistribute it and/or modify
983cbb37nwhitehorn *  it under the terms of the GNU Lesser General Public License, version 2.1
1083cbb37nwhitehorn *
1183cbb37nwhitehorn *  This program is distributed in the hope that it will be useful, but
1283cbb37nwhitehorn *  WITHOUT ANY WARRANTY; without even the implied warranty of
1383cbb37nwhitehorn *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1483cbb37nwhitehorn *  Lesser General Public License for more details.
1583cbb37nwhitehorn *
1683cbb37nwhitehorn *  You should have received a copy of the GNU Lesser General Public
1783cbb37nwhitehorn *  License along with this program; if not, write to
1883cbb37nwhitehorn *	Free Software Foundation, Inc.
1983cbb37nwhitehorn *	51 Franklin St., Fifth Floor
2083cbb37nwhitehorn *	Boston, MA 02110, USA.
2183cbb37nwhitehorn */
2283cbb37nwhitehorn
2383cbb37nwhitehorn#include <dialog.h>
2483cbb37nwhitehorn
2583cbb37nwhitehorn#ifdef HAVE_DLG_TRACE
2683cbb37nwhitehorn
27a94245bnwhitehorn#ifdef NEED_WCHAR_H
28a94245bnwhitehorn#include <wchar.h>
29a94245bnwhitehorn#endif
30a94245bnwhitehorn
3183cbb37nwhitehorn#include <dlg_keys.h>
3283cbb37nwhitehorn#include <time.h>
3383cbb37nwhitehorn
3483cbb37nwhitehorn#define myFP dialog_state.trace_output
3583cbb37nwhitehorn
369904759nwhitehornstatic void
379904759nwhitehorndlg_trace_time(const char *tag)
389904759nwhitehorn{
399904759nwhitehorn    time_t now = time((time_t *) 0);
409904759nwhitehorn    fprintf(myFP, "%s %s", tag, ctime(&now));
419904759nwhitehorn}
429904759nwhitehorn
4383cbb37nwhitehornvoid
4483cbb37nwhitehorndlg_trace_msg(const char *fmt,...)
4583cbb37nwhitehorn{
4683cbb37nwhitehorn    if (myFP != 0) {
4783cbb37nwhitehorn	va_list ap;
4883cbb37nwhitehorn	va_start(ap, fmt);
4983cbb37nwhitehorn	vfprintf(myFP, fmt, ap);
5083cbb37nwhitehorn	va_end(ap);
5183cbb37nwhitehorn	fflush(myFP);
5283cbb37nwhitehorn    }
5383cbb37nwhitehorn}
5483cbb37nwhitehorn
5583cbb37nwhitehornvoid
56f8604a5baptdlg_trace_2s(const char *name, const char *value)
57f8604a5bapt{
58f8604a5bapt    bool first = TRUE;
59f8604a5bapt    const char *next;
60f8604a5bapt    int left, right = 0;
61f8604a5bapt
62f8604a5bapt    if (value == 0)
63f8604a5bapt	value = "<NULL>";
64f8604a5bapt
65f8604a5bapt    while (value[right] != '\0') {
66f8604a5bapt	value += right;
67f8604a5bapt	if ((next = strchr(value, '\n')) != 0) {
68f8604a5bapt	    left = (int) (next - value);
69f8604a5bapt	    right = left + 1;
70f8604a5bapt	} else {
71f8604a5bapt	    left = (int) strlen(value);
72f8604a5bapt	    right = left;
73f8604a5bapt	}
74f8604a5bapt	if (first) {
75f8604a5bapt	    first = FALSE;
76f8604a5bapt	    dlg_trace_msg("#%14s=%.*s\n", name, left, value);
77f8604a5bapt	} else {
78f8604a5bapt	    dlg_trace_msg("#+\t\t%.*s\n", left, value);
79f8604a5bapt	}
80f8604a5bapt    }
81f8604a5bapt}
82f8604a5bapt
83f8604a5baptvoid
84f8604a5baptdlg_trace_2n(const char *name, int value)
85f8604a5bapt{
86f8604a5bapt    dlg_trace_msg("#\t%7s=%d\n", name, value);
87f8604a5bapt}
88f8604a5bapt
89f8604a5baptvoid
9083cbb37nwhitehorndlg_trace_win(WINDOW *win)
9183cbb37nwhitehorn{
9283cbb37nwhitehorn    if (myFP != 0) {
9383cbb37nwhitehorn	int y, x;
9483cbb37nwhitehorn	int j, k;
95a94245bnwhitehorn	WINDOW *top = wgetparent(win);
96a94245bnwhitehorn
97a94245bnwhitehorn	while (top != 0 && top != stdscr) {
98a94245bnwhitehorn	    win = top;
99a94245bnwhitehorn	    top = wgetparent(win);
100a94245bnwhitehorn	}
101a94245bnwhitehorn
102a94245bnwhitehorn	if (win != 0) {
103a94245bnwhitehorn	    int rc = getmaxy(win);
104a94245bnwhitehorn	    int cc = getmaxx(win);
105a94245bnwhitehorn	    chtype ch, c2;
106a94245bnwhitehorn
107a94245bnwhitehorn	    fprintf(myFP, "window %dx%d at %d,%d\n",
108a94245bnwhitehorn		    rc, cc, getbegy(win), getbegx(win));
109a94245bnwhitehorn
110a94245bnwhitehorn	    getyx(win, y, x);
111a94245bnwhitehorn	    for (j = 0; j < rc; ++j) {
112a94245bnwhitehorn		fprintf(myFP, "%3d:", j);
113a94245bnwhitehorn		for (k = 0; k < cc; ++k) {
114a94245bnwhitehorn#ifdef USE_WIDE_CURSES
115a94245bnwhitehorn		    char buffer[80];
116a94245bnwhitehorn
117a94245bnwhitehorn		    ch = mvwinch(win, j, k) & (A_CHARTEXT | A_ALTCHARSET);
118a94245bnwhitehorn		    if (ch & A_ALTCHARSET) {
119a94245bnwhitehorn			c2 = dlg_asciibox(ch);
120a94245bnwhitehorn			if (c2 != 0) {
121a94245bnwhitehorn			    ch = c2;
122a94245bnwhitehorn			}
123a94245bnwhitehorn			buffer[0] = (char) ch;
124a94245bnwhitehorn			buffer[1] = '\0';
125a94245bnwhitehorn		    } else {
126a94245bnwhitehorn			cchar_t cch;
127a94245bnwhitehorn			wchar_t *uc;
128a94245bnwhitehorn
129a94245bnwhitehorn			if (win_wch(win, &cch) == ERR
130f8604a5bapt			    || (uc = wunctrl((&cch))) == 0
131a94245bnwhitehorn			    || uc[1] != 0
132a94245bnwhitehorn			    || wcwidth(uc[0]) <= 0) {
133a94245bnwhitehorn			    buffer[0] = '.';
134a94245bnwhitehorn			    buffer[1] = '\0';
135a94245bnwhitehorn			} else {
136a94245bnwhitehorn			    mbstate_t state;
137a94245bnwhitehorn			    const wchar_t *ucp = uc;
138a94245bnwhitehorn
139a94245bnwhitehorn			    memset(&state, 0, sizeof(state));
140a94245bnwhitehorn			    wcsrtombs(buffer, &ucp, sizeof(buffer), &state);
141a94245bnwhitehorn			    k += wcwidth(uc[0]) - 1;
142a94245bnwhitehorn			}
143a94245bnwhitehorn		    }
144a94245bnwhitehorn		    fputs(buffer, myFP);
145a94245bnwhitehorn#else
146a94245bnwhitehorn		    ch = mvwinch(win, j, k) & (A_CHARTEXT | A_ALTCHARSET);
147a94245bnwhitehorn		    c2 = dlg_asciibox(ch);
148a94245bnwhitehorn		    if (c2 != 0) {
149a94245bnwhitehorn			ch = c2;
150a94245bnwhitehorn		    } else if (unctrl(ch) == 0 || strlen(unctrl(ch)) > 1) {
151a94245bnwhitehorn			ch = '.';
152a94245bnwhitehorn		    }
153a94245bnwhitehorn		    fputc((int) (ch & 0xff), myFP);
154a94245bnwhitehorn#endif
15583cbb37nwhitehorn		}
156a94245bnwhitehorn		fputc('\n', myFP);
15783cbb37nwhitehorn	    }
158a94245bnwhitehorn	    wmove(win, y, x);
159a94245bnwhitehorn	    fflush(myFP);
16083cbb37nwhitehorn	}
16183cbb37nwhitehorn    }
16283cbb37nwhitehorn}
16383cbb37nwhitehorn
16483cbb37nwhitehornvoid
16583cbb37nwhitehorndlg_trace_chr(int ch, int fkey)
16683cbb37nwhitehorn{
167a94245bnwhitehorn    static int last_err = 0;
168a94245bnwhitehorn
169a94245bnwhitehorn    /*
170a94245bnwhitehorn     * Do not bother to trace ERR's indefinitely, since those are usually due
171a94245bnwhitehorn     * to relatively short polling timeouts.
172a94245bnwhitehorn     */
173a94245bnwhitehorn    if (last_err && !fkey && ch == ERR) {
174a94245bnwhitehorn	++last_err;
175a94245bnwhitehorn    } else if (myFP != 0) {
17683cbb37nwhitehorn	const char *fkey_name = "?";
177a94245bnwhitehorn
178a94245bnwhitehorn	if (last_err) {
179a94245bnwhitehorn	    fprintf(myFP, "skipped %d ERR's\n", last_err);
180a94245bnwhitehorn	    last_err = 0;
181a94245bnwhitehorn	}
182a94245bnwhitehorn
18383cbb37nwhitehorn	if (fkey) {
18483cbb37nwhitehorn	    if (fkey > KEY_MAX || (fkey_name = keyname(fkey)) == 0) {
18583cbb37nwhitehorn#define CASE(name) case name: fkey_name = #name; break
18683cbb37nwhitehorn		switch ((DLG_KEYS_ENUM) fkey) {
18783cbb37nwhitehorn		    CASE(DLGK_MIN);
18883cbb37nwhitehorn		    CASE(DLGK_OK);
18983cbb37nwhitehorn		    CASE(DLGK_CANCEL);
19083cbb37nwhitehorn		    CASE(DLGK_EXTRA);
19183cbb37nwhitehorn		    CASE(DLGK_HELP);
19283cbb37nwhitehorn		    CASE(DLGK_ESC);
19383cbb37nwhitehorn		    CASE(DLGK_PAGE_FIRST);
19483cbb37nwhitehorn		    CASE(DLGK_PAGE_LAST);
19583cbb37nwhitehorn		    CASE(DLGK_PAGE_NEXT);
19683cbb37nwhitehorn		    CASE(DLGK_PAGE_PREV);
19783cbb37nwhitehorn		    CASE(DLGK_ITEM_FIRST);
19883cbb37nwhitehorn		    CASE(DLGK_ITEM_LAST);
19983cbb37nwhitehorn		    CASE(DLGK_ITEM_NEXT);
20083cbb37nwhitehorn		    CASE(DLGK_ITEM_PREV);
20183cbb37nwhitehorn		    CASE(DLGK_FIELD_FIRST);
20283cbb37nwhitehorn		    CASE(DLGK_FIELD_LAST);
20383cbb37nwhitehorn		    CASE(DLGK_FIELD_NEXT);
20483cbb37nwhitehorn		    CASE(DLGK_FIELD_PREV);
205a94245bnwhitehorn		    CASE(DLGK_FORM_FIRST);
206a94245bnwhitehorn		    CASE(DLGK_FORM_LAST);
207a94245bnwhitehorn		    CASE(DLGK_FORM_NEXT);
208a94245bnwhitehorn		    CASE(DLGK_FORM_PREV);
20983cbb37nwhitehorn		    CASE(DLGK_GRID_UP);
21083cbb37nwhitehorn		    CASE(DLGK_GRID_DOWN);
21183cbb37nwhitehorn		    CASE(DLGK_GRID_LEFT);
21283cbb37nwhitehorn		    CASE(DLGK_GRID_RIGHT);
21383cbb37nwhitehorn		    CASE(DLGK_DELETE_LEFT);
21483cbb37nwhitehorn		    CASE(DLGK_DELETE_RIGHT);
21583cbb37nwhitehorn		    CASE(DLGK_DELETE_ALL);
21683cbb37nwhitehorn		    CASE(DLGK_ENTER);
21783cbb37nwhitehorn		    CASE(DLGK_BEGIN);
21883cbb37nwhitehorn		    CASE(DLGK_FINAL);
21983cbb37nwhitehorn		    CASE(DLGK_SELECT);
22094f19e0nwhitehorn		    CASE(DLGK_HELPFILE);
22183cbb37nwhitehorn		    CASE(DLGK_TRACE);
222f8604a5bapt		    CASE(DLGK_TOGGLE);
22383cbb37nwhitehorn		}
22483cbb37nwhitehorn	    }
2259904759nwhitehorn	} else if (ch == ERR) {
2269904759nwhitehorn	    fkey_name = "ERR";
227a94245bnwhitehorn	    last_err = 1;
22883cbb37nwhitehorn	} else {
22983cbb37nwhitehorn	    fkey_name = unctrl((chtype) ch);
23083cbb37nwhitehorn	    if (fkey_name == 0)
23183cbb37nwhitehorn		fkey_name = "UNKNOWN";
23283cbb37nwhitehorn	}
233f8604a5bapt	if (ch >= 0) {
234f8604a5bapt	    fprintf(myFP, "chr %s (ch=%#x, fkey=%d)\n", fkey_name, ch, fkey);
235f8604a5bapt	} else {
236f8604a5bapt	    fprintf(myFP, "chr %s (ch=%d, fkey=%d)\n", fkey_name, ch, fkey);
237f8604a5bapt	}
23883cbb37nwhitehorn	fflush(myFP);
23983cbb37nwhitehorn    }
24083cbb37nwhitehorn}
24183cbb37nwhitehorn
24283cbb37nwhitehornvoid
24383cbb37nwhitehorndlg_trace(const char *fname)
24483cbb37nwhitehorn{
24583cbb37nwhitehorn    if (fname != 0) {
24683cbb37nwhitehorn	if (myFP == 0) {
24783cbb37nwhitehorn	    myFP = fopen(fname, "a");
24883cbb37nwhitehorn	    if (myFP != 0) {
249f8604a5bapt		dlg_trace_time("## opened at");
250f8604a5bapt		DLG_TRACE(("## dialog %s\n", dialog_version()));
251f8604a5bapt		DLG_TRACE(("## vile: confmode\n"));
25283cbb37nwhitehorn	    }
25383cbb37nwhitehorn	}
25483cbb37nwhitehorn    } else if (myFP != 0) {
255f8604a5bapt	dlg_trace_time("## closed at");
25683cbb37nwhitehorn	fclose(myFP);
25783cbb37nwhitehorn	myFP = 0;
25883cbb37nwhitehorn    }
25983cbb37nwhitehorn}
26083cbb37nwhitehorn#else
26183cbb37nwhitehorn#undef dlg_trace
26283cbb37nwhitehornextern void dlg_trace(const char *);
26383cbb37nwhitehornvoid
26483cbb37nwhitehorndlg_trace(const char *fname)
26583cbb37nwhitehorn{
26683cbb37nwhitehorn    (void) fname;
26783cbb37nwhitehorn}
26883cbb37nwhitehorn#endif
269