forked from dmitry-j-mikhin/ttyrec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttyplay.c
341 lines (300 loc) · 8.02 KB
/
ttyplay.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
* Copyright (c) 2000 Satoru Takabayashi <satoru@namazu.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <termios.h>
#include <sys/time.h>
#include <string.h>
#include "ttyrec.h"
#include "io.h"
typedef double (*WaitFunc) (struct timeval prev,
struct timeval cur,
double speed);
typedef int (*ReadFunc) (FILE *fp, Header *h, char **buf);
typedef void (*WriteFunc) (char *buf, int len);
typedef void (*ProcessFunc) (FILE *fp, double speed,
ReadFunc read_func, WaitFunc wait_func);
struct timeval
timeval_diff (struct timeval tv1, struct timeval tv2)
{
struct timeval diff;
diff.tv_sec = tv2.tv_sec - tv1.tv_sec;
diff.tv_usec = tv2.tv_usec - tv1.tv_usec;
if (diff.tv_usec < 0) {
diff.tv_sec--;
diff.tv_usec += 1000000;
}
return diff;
}
struct timeval
timeval_div (struct timeval tv1, double n)
{
double x = ((double)tv1.tv_sec + (double)tv1.tv_usec / 1000000.0) / n;
struct timeval div;
div.tv_sec = (int)x;
div.tv_usec = (x - (int)x) * 1000000;
return div;
}
double
ttywait (struct timeval prev, struct timeval cur, double speed)
{
static struct timeval drift = {0, 0};
struct timeval start;
struct timeval diff = timeval_diff(prev, cur);
struct timeval *diffp = &diff;
fd_set readfs;
gettimeofday(&start, NULL);
if (speed == 0.0)
diffp = NULL;
else
diff = timeval_diff(drift, timeval_div(diff, speed));
if (diff.tv_sec < 0) {
diff.tv_sec = diff.tv_usec = 0;
}
FD_ZERO(&readfs);
FD_SET(STDIN_FILENO, &readfs);
/*
* We use select() for sleeping with subsecond precision.
* select() is also used to wait user's input from a keyboard.
*
* Save "diff" since select(2) may overwrite it to {0, 0}.
*/
struct timeval orig_diff = diff;
int r;
r = select(1, &readfs, NULL, NULL, diffp); /* skip if a user hits any key */
diff = orig_diff; /* Restore the original diff value. */
if (r > 0 && FD_ISSET(0, &readfs)) { /* a user hits a character? */
char c;
read(STDIN_FILENO, &c, 1); /* drain the character */
switch (c) {
case '+':
case 'f':
speed *= 2;
break;
case '-':
case 's':
speed /= 2;
break;
case '1':
speed = 1.0;
break;
case '0':
speed = 0.0;
break;
}
drift.tv_sec = drift.tv_usec = 0;
} else {
struct timeval stop;
gettimeofday(&stop, NULL);
/* Hack to accumulate the drift */
if (diff.tv_sec == 0 && diff.tv_usec == 0) {
diff = timeval_diff(drift, diff); // diff = 0 - drift.
}
drift = timeval_diff(diff, timeval_diff(start, stop));
}
return speed;
}
double
ttynowait (struct timeval prev, struct timeval cur, double speed)
{
/* do nothing */
return 1.0; /* Speed isn't important. */
}
int
ttyread (FILE *fp, Header *h, char **buf)
{
fpos_t pos;
int can_seek=0;
if (fgetpos(fp, &pos) == 0) {
can_seek=1;
}
clearerr(fp);
if (read_header(fp, h) == 0) {
goto err;
}
*buf = malloc(h->len);
if (*buf == NULL) {
perror("malloc");
}
if (fread(*buf, 1, h->len, fp) == 0) {
goto err;
}
return 1;
err:
if (ferror(fp)) {
perror("fread");
}
else {
/* Short read. Seek back to before header, to set up for retry. */
if (can_seek) {
fsetpos(fp, &pos);
}
}
return 0;
}
int
ttypread (FILE *fp, Header *h, char **buf)
{
/*
* Read persistently just like tail -f.
*/
while (ttyread(fp, h, buf) == 0) {
struct timeval w = {0, 250000};
select(0, NULL, NULL, NULL, &w);
clearerr(fp);
}
return 1;
}
void
ttywrite (char *buf, int len)
{
fwrite(buf, 1, len, stdout);
}
void
ttynowrite (char *buf, int len)
{
/* do nothing */
}
void
ttyplay (FILE *fp, double speed, ReadFunc read_func,
WriteFunc write_func, WaitFunc wait_func)
{
int first_time = 1;
struct timeval prev;
setbuf(stdout, NULL);
setbuf(fp, NULL);
while (1) {
char *buf;
Header h;
if (read_func(fp, &h, &buf) == 0) {
break;
}
if (!first_time) {
do {
speed = wait_func(prev, h.tv, speed);
} while (speed == 0.0);
}
first_time = 0;
write_func(buf, h.len);
prev = h.tv;
free(buf);
}
}
void
ttyskipall (FILE *fp)
{
/*
* Skip all records.
*/
ttyplay(fp, 0, ttyread, ttynowrite, ttynowait);
}
void ttyplayback (FILE *fp, double speed,
ReadFunc read_func, WaitFunc wait_func)
{
ttyplay(fp, speed, ttyread, ttywrite, wait_func);
}
void ttypeek (FILE *fp, double speed,
ReadFunc read_func, WaitFunc wait_func)
{
ttyskipall(fp);
ttyplay(fp, speed, ttypread, ttywrite, ttynowait);
}
void
usage (void)
{
printf("Usage: ttyplay [OPTION] [FILE]\n");
printf(" -s SPEED Set speed to SPEED [1.0]\n");
printf(" -n No wait mode\n");
printf(" -p Peek another person's ttyrecord\n");
exit(EXIT_FAILURE);
}
/*
* We do some tricks so that select(2) properly works on
* STDIN_FILENO in ttywait().
*/
FILE *
input_from_stdin (void)
{
int fd = edup(STDIN_FILENO);
edup2(STDOUT_FILENO, STDIN_FILENO);
return efdopen(fd, "r");
}
int
main (int argc, char **argv)
{
double speed = 1.0;
ReadFunc read_func = ttyread;
WaitFunc wait_func = ttywait;
ProcessFunc process = ttyplayback;
FILE *input = NULL;
struct termios old, new;
set_progname(argv[0]);
while (1) {
int ch = getopt(argc, argv, "s:np");
if (ch == EOF) {
break;
}
switch (ch) {
case 's':
if (optarg == NULL) {
perror("-s option requires an argument");
exit(EXIT_FAILURE);
}
sscanf(optarg, "%lf", &speed);
break;
case 'n':
wait_func = ttynowait;
break;
case 'p':
process = ttypeek;
break;
default:
usage();
}
}
if (optind < argc) {
input = efopen(argv[optind], "r");
} else {
input = input_from_stdin();
}
assert(input != NULL);
tcgetattr(0, &old); /* Get current terminal state */
new = old; /* Make a copy */
new.c_lflag &= ~(ICANON | ECHO | ECHONL); /* unbuffered, no echo */
tcsetattr(0, TCSANOW, &new); /* Make it current */
process(input, speed, read_func, wait_func);
tcsetattr(0, TCSANOW, &old); /* Return terminal state */
return 0;
}