ttyrecのfork. Original: http://0xcc.net/ttyrec/
修訂 | a23dffc1e8c1b95e34d2c6d5b78aa32b7458b38b (tree) |
---|---|
時間 | 2019-12-09 16:10:59 |
作者 | IWAMOTO Kouichi <sue@iwmt...> |
Commiter | IWAMOTO Kouichi |
update to ttyrec-1.0.1
@@ -1,6 +1,6 @@ | ||
1 | 1 | CC = gcc |
2 | 2 | CFLAGS = -O2 |
3 | -VERSION = 1.0 | |
3 | +VERSION = 1.0.1 | |
4 | 4 | |
5 | 5 | TARGET = ttyrec ttyplay |
6 | 6 |
@@ -5,7 +5,7 @@ Usage: | ||
5 | 5 | % ttyrec |
6 | 6 | (In the excuted shell, do whatever you want and exit) |
7 | 7 | |
8 | - % ttyplay < ttyrecord | |
8 | + % ttyplay ttyrecord | |
9 | 9 | |
10 | 10 | Have fun! |
11 | 11 |
@@ -36,14 +36,16 @@ | ||
36 | 36 | #include <assert.h> |
37 | 37 | #include <unistd.h> |
38 | 38 | #include <sys/time.h> |
39 | +#include <errno.h> | |
40 | +#include <string.h> | |
39 | 41 | |
40 | 42 | #include "ttyrec.h" |
41 | 43 | |
42 | -typedef void (*WaitFunc) (struct timeval prev, | |
43 | - struct timeval cur, | |
44 | - double speed); | |
45 | -typedef int (*ReadFunc) (Header *h, char **buf); | |
46 | -typedef void (*WriteFunc) (char *buf, int len); | |
44 | +typedef void (*WaitFunc) (struct timeval prev, | |
45 | + struct timeval cur, | |
46 | + double speed); | |
47 | +typedef int (*ReadFunc) (FILE *fp, Header *h, char **buf); | |
48 | +typedef void (*WriteFunc) (char *buf, int len); | |
47 | 49 | |
48 | 50 | struct timeval |
49 | 51 | timeval_diff (struct timeval tv1, struct timeval tv2) |
@@ -78,9 +80,9 @@ ttynowait (struct timeval prev, struct timeval cur, double speed) | ||
78 | 80 | } |
79 | 81 | |
80 | 82 | int |
81 | -ttyread (Header *h, char **buf) | |
83 | +ttyread (FILE *fp, Header *h, char **buf) | |
82 | 84 | { |
83 | - if (fread(h, sizeof(Header), 1, stdin) == 0) { | |
85 | + if (fread(h, sizeof(Header), 1, fp) == 0) { | |
84 | 86 | return 0; |
85 | 87 | } |
86 | 88 |
@@ -89,21 +91,21 @@ ttyread (Header *h, char **buf) | ||
89 | 91 | perror("malloc"); |
90 | 92 | } |
91 | 93 | |
92 | - if (fread(*buf, 1, h->len, stdin) == 0) { | |
94 | + if (fread(*buf, 1, h->len, fp) == 0) { | |
93 | 95 | perror("fread"); |
94 | 96 | } |
95 | 97 | return 1; |
96 | 98 | } |
97 | 99 | |
98 | 100 | int |
99 | -ttypread (Header *h, char **buf) | |
101 | +ttypread (FILE *fp, Header *h, char **buf) | |
100 | 102 | { |
101 | 103 | /* |
102 | 104 | * Read persistently just like tail -f. |
103 | 105 | */ |
104 | - while (ttyread(h, buf) == 0) { | |
106 | + while (ttyread(fp, h, buf) == 0) { | |
105 | 107 | usleep(250000); |
106 | - clearerr(stdin); | |
108 | + clearerr(fp); | |
107 | 109 | } |
108 | 110 | return 1; |
109 | 111 | } |
@@ -121,20 +123,20 @@ ttynowrite (char *buf, int len) | ||
121 | 123 | } |
122 | 124 | |
123 | 125 | void |
124 | -ttyplay (double speed, ReadFunc read_func, | |
126 | +ttyplay (FILE *fp, double speed, ReadFunc read_func, | |
125 | 127 | WriteFunc write_func, WaitFunc wait_func) |
126 | 128 | { |
127 | 129 | int first_time = 1; |
128 | 130 | struct timeval prev; |
129 | 131 | |
130 | 132 | setbuf(stdout, NULL); |
131 | - setbuf(stdin, NULL); | |
133 | + setbuf(fp, NULL); | |
132 | 134 | |
133 | 135 | while (1) { |
134 | 136 | char *buf; |
135 | 137 | Header h; |
136 | 138 | |
137 | - if (read_func(&h, &buf) == 0) { | |
139 | + if (read_func(fp, &h, &buf) == 0) { | |
138 | 140 | break; |
139 | 141 | } |
140 | 142 |
@@ -150,20 +152,59 @@ ttyplay (double speed, ReadFunc read_func, | ||
150 | 152 | } |
151 | 153 | |
152 | 154 | void |
153 | -ttyskipall (void) | |
155 | +ttyskipall (FILE *fp) | |
154 | 156 | { |
155 | 157 | /* |
156 | 158 | * Skip all records. |
157 | 159 | */ |
158 | - ttyplay(0, ttyread, ttynowrite, ttynowait); | |
160 | + ttyplay(fp, 0, ttyread, ttynowrite, ttynowait); | |
161 | +} | |
162 | + | |
163 | +typedef void (*ProcessFunc) (FILE *fp, double speed, | |
164 | + ReadFunc read_func, WaitFunc wait_func); | |
165 | + | |
166 | +void ttyplayback (FILE *fp, double speed, | |
167 | + ReadFunc read_func, WaitFunc wait_func) | |
168 | +{ | |
169 | + ttyplay(fp, speed, ttyread, ttywrite, wait_func); | |
170 | +} | |
171 | + | |
172 | +void ttypeek (FILE *fp, double speed, | |
173 | + ReadFunc read_func, WaitFunc wait_func) | |
174 | +{ | |
175 | + ttyskipall(fp); | |
176 | + ttyplay(fp, speed, ttypread, ttywrite, ttynowait); | |
177 | +} | |
178 | + | |
179 | + | |
180 | +void | |
181 | +usage (void) | |
182 | +{ | |
183 | + printf("Usage: ttyplay [OPTION] [FILE]\n"); | |
184 | + printf(" -s SPEED Set speed to SPEED [1.0]\n"); | |
185 | + printf(" -n No wait mode\n"); | |
186 | + printf(" -p Peek another person's ttyrecord\n"); | |
187 | + exit(EXIT_FAILURE); | |
188 | +} | |
189 | + | |
190 | +FILE * | |
191 | +efopen (const char *path, const char *mode) | |
192 | +{ | |
193 | + FILE *fp = fopen(path, mode); | |
194 | + if (fp == NULL) { | |
195 | + fprintf(stderr, "ttyplay: %s: %s\n", path, strerror(errno)); | |
196 | + exit(EXIT_FAILURE); | |
197 | + } | |
159 | 198 | } |
160 | 199 | |
161 | 200 | int |
162 | 201 | main (int argc, char **argv) |
163 | 202 | { |
164 | 203 | double speed = 1.0; |
165 | - ReadFunc read_func = ttyread; | |
166 | - WaitFunc wait_func = ttywait; | |
204 | + ReadFunc read_func = ttyread; | |
205 | + WaitFunc wait_func = ttywait; | |
206 | + ProcessFunc process = ttyplayback; | |
207 | + FILE *input = stdin; | |
167 | 208 | |
168 | 209 | while (1) { |
169 | 210 | int ch = getopt(argc, argv, "s:np"); |
@@ -174,7 +215,7 @@ main (int argc, char **argv) | ||
174 | 215 | case 's': |
175 | 216 | if (optarg == NULL) { |
176 | 217 | perror("-s option requires an argument"); |
177 | - exit(1); | |
218 | + exit(EXIT_FAILURE); | |
178 | 219 | } |
179 | 220 | sscanf(optarg, "%lf", &speed); |
180 | 221 | break; |
@@ -182,20 +223,18 @@ main (int argc, char **argv) | ||
182 | 223 | wait_func = ttynowait; |
183 | 224 | break; |
184 | 225 | case 'p': |
185 | - ttyskipall(); | |
186 | - read_func = ttypread; | |
187 | - wait_func = ttynowait; | |
226 | + process = ttypeek; | |
188 | 227 | break; |
189 | 228 | default: |
190 | - printf("Usage: ttyplay [OPTION] < ttyrecord\n"); | |
191 | - printf(" -s SPEED Set speed to SPEED [1.0]\n"); | |
192 | - printf(" -n No wait mode\n"); | |
193 | - printf(" -p Peek another person's ttyrecord\n"); | |
194 | - exit(1); | |
229 | + usage(); | |
195 | 230 | } |
196 | 231 | } |
197 | 232 | |
198 | - ttyplay(speed, read_func, ttywrite, wait_func); | |
233 | + if (optind < argc) { | |
234 | + input = efopen(argv[optind], "r"); | |
235 | + } | |
236 | + | |
237 | + process(input, speed, read_func, wait_func); | |
199 | 238 | |
200 | 239 | return 0; |
201 | 240 | } |