]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/dialog/trace.c
Merge gdtoa-20110304.
[FreeBSD/FreeBSD.git] / contrib / dialog / trace.c
1 /*
2  *  $Id: trace.c,v 1.11 2010/01/17 15:36:26 tom Exp $
3  *
4  *  trace.c -- implements screen-dump and keystroke-logging
5  *
6  *  Copyright 2007-2008,2010    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 #include <dlg_keys.h>
28 #include <time.h>
29
30 #define myFP dialog_state.trace_output
31
32 void
33 dlg_trace_msg(const char *fmt,...)
34 {
35     if (myFP != 0) {
36         va_list ap;
37         va_start(ap, fmt);
38         vfprintf(myFP, fmt, ap);
39         va_end(ap);
40         fflush(myFP);
41     }
42 }
43
44 void
45 dlg_trace_win(WINDOW *win)
46 {
47     if (myFP != 0) {
48         int y, x;
49         int j, k;
50         int rc = getmaxy(win);
51         int cc = getmaxx(win);
52         chtype ch, c2;
53
54         fprintf(myFP, "window %dx%d at %d,%d\n",
55                 rc, cc, getbegy(win), getbegx(win));
56
57         getyx(win, y, x);
58         for (j = 0; j < rc; ++j) {
59             fprintf(myFP, "%3d:", j);
60             for (k = 0; k < cc; ++k) {
61                 ch = mvwinch(win, j, k) & (A_CHARTEXT | A_ALTCHARSET);
62                 c2 = dlg_asciibox(ch);
63                 if (c2 != 0) {
64                     ch = c2;
65                 } else if (unctrl(ch) == 0 || strlen(unctrl(ch)) > 1) {
66                     ch = '.';
67                 }
68                 fputc((int) (ch & 0xff), myFP);
69             }
70             fputc('\n', myFP);
71         }
72         wmove(win, y, x);
73         fflush(myFP);
74     }
75 }
76
77 void
78 dlg_trace_chr(int ch, int fkey)
79 {
80     if (myFP != 0) {
81         const char *fkey_name = "?";
82         if (fkey) {
83             if (fkey > KEY_MAX || (fkey_name = keyname(fkey)) == 0) {
84 #define CASE(name) case name: fkey_name = #name; break
85                 switch ((DLG_KEYS_ENUM) fkey) {
86                     CASE(DLGK_MIN);
87                     CASE(DLGK_OK);
88                     CASE(DLGK_CANCEL);
89                     CASE(DLGK_EXTRA);
90                     CASE(DLGK_HELP);
91                     CASE(DLGK_ESC);
92                     CASE(DLGK_PAGE_FIRST);
93                     CASE(DLGK_PAGE_LAST);
94                     CASE(DLGK_PAGE_NEXT);
95                     CASE(DLGK_PAGE_PREV);
96                     CASE(DLGK_ITEM_FIRST);
97                     CASE(DLGK_ITEM_LAST);
98                     CASE(DLGK_ITEM_NEXT);
99                     CASE(DLGK_ITEM_PREV);
100                     CASE(DLGK_FIELD_FIRST);
101                     CASE(DLGK_FIELD_LAST);
102                     CASE(DLGK_FIELD_NEXT);
103                     CASE(DLGK_FIELD_PREV);
104                     CASE(DLGK_GRID_UP);
105                     CASE(DLGK_GRID_DOWN);
106                     CASE(DLGK_GRID_LEFT);
107                     CASE(DLGK_GRID_RIGHT);
108                     CASE(DLGK_DELETE_LEFT);
109                     CASE(DLGK_DELETE_RIGHT);
110                     CASE(DLGK_DELETE_ALL);
111                     CASE(DLGK_ENTER);
112                     CASE(DLGK_BEGIN);
113                     CASE(DLGK_FINAL);
114                     CASE(DLGK_SELECT);
115                     CASE(DLGK_TRACE);
116                 }
117             }
118         } else {
119             fkey_name = unctrl((chtype) ch);
120             if (fkey_name == 0)
121                 fkey_name = "UNKNOWN";
122         }
123         fprintf(myFP, "chr %s (ch=%#x, fkey=%d)\n",
124                 fkey_name,
125                 ch, fkey);
126         fflush(myFP);
127     }
128 }
129
130 void
131 dlg_trace(const char *fname)
132 {
133     if (fname != 0) {
134         if (myFP == 0) {
135             myFP = fopen(fname, "a");
136             if (myFP != 0) {
137                 time_t now = time((time_t *) 0);
138                 fprintf(myFP, "** opened at %s", ctime(&now));
139             }
140         }
141     } else if (myFP != 0) {
142         time_t now = time((time_t *) 0);
143         fprintf(myFP, "** closed at %s", ctime(&now));
144         fclose(myFP);
145         myFP = 0;
146     }
147 }
148 #else
149 #undef dlg_trace
150 extern void dlg_trace(const char *);
151 void
152 dlg_trace(const char *fname)
153 {
154     (void) fname;
155 }
156 #endif