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