• R/O
  • HTTP
  • SSH
  • HTTPS

提交

標籤
無標籤

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

ttyrecのfork. Original: http://0xcc.net/ttyrec/


Commit MetaInfo

修訂a23dffc1e8c1b95e34d2c6d5b78aa32b7458b38b (tree)
時間2019-12-09 16:10:59
作者IWAMOTO Kouichi <sue@iwmt...>
CommiterIWAMOTO Kouichi

Log Message

update to ttyrec-1.0.1

Change Summary

差異

--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
11 CC = gcc
22 CFLAGS = -O2
3-VERSION = 1.0
3+VERSION = 1.0.1
44
55 TARGET = ttyrec ttyplay
66
--- a/README
+++ b/README
@@ -5,7 +5,7 @@ Usage:
55 % ttyrec
66 (In the excuted shell, do whatever you want and exit)
77
8- % ttyplay < ttyrecord
8+ % ttyplay ttyrecord
99
1010 Have fun!
1111
--- a/ttyplay.c
+++ b/ttyplay.c
@@ -36,14 +36,16 @@
3636 #include <assert.h>
3737 #include <unistd.h>
3838 #include <sys/time.h>
39+#include <errno.h>
40+#include <string.h>
3941
4042 #include "ttyrec.h"
4143
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);
4749
4850 struct timeval
4951 timeval_diff (struct timeval tv1, struct timeval tv2)
@@ -78,9 +80,9 @@ ttynowait (struct timeval prev, struct timeval cur, double speed)
7880 }
7981
8082 int
81-ttyread (Header *h, char **buf)
83+ttyread (FILE *fp, Header *h, char **buf)
8284 {
83- if (fread(h, sizeof(Header), 1, stdin) == 0) {
85+ if (fread(h, sizeof(Header), 1, fp) == 0) {
8486 return 0;
8587 }
8688
@@ -89,21 +91,21 @@ ttyread (Header *h, char **buf)
8991 perror("malloc");
9092 }
9193
92- if (fread(*buf, 1, h->len, stdin) == 0) {
94+ if (fread(*buf, 1, h->len, fp) == 0) {
9395 perror("fread");
9496 }
9597 return 1;
9698 }
9799
98100 int
99-ttypread (Header *h, char **buf)
101+ttypread (FILE *fp, Header *h, char **buf)
100102 {
101103 /*
102104 * Read persistently just like tail -f.
103105 */
104- while (ttyread(h, buf) == 0) {
106+ while (ttyread(fp, h, buf) == 0) {
105107 usleep(250000);
106- clearerr(stdin);
108+ clearerr(fp);
107109 }
108110 return 1;
109111 }
@@ -121,20 +123,20 @@ ttynowrite (char *buf, int len)
121123 }
122124
123125 void
124-ttyplay (double speed, ReadFunc read_func,
126+ttyplay (FILE *fp, double speed, ReadFunc read_func,
125127 WriteFunc write_func, WaitFunc wait_func)
126128 {
127129 int first_time = 1;
128130 struct timeval prev;
129131
130132 setbuf(stdout, NULL);
131- setbuf(stdin, NULL);
133+ setbuf(fp, NULL);
132134
133135 while (1) {
134136 char *buf;
135137 Header h;
136138
137- if (read_func(&h, &buf) == 0) {
139+ if (read_func(fp, &h, &buf) == 0) {
138140 break;
139141 }
140142
@@ -150,20 +152,59 @@ ttyplay (double speed, ReadFunc read_func,
150152 }
151153
152154 void
153-ttyskipall (void)
155+ttyskipall (FILE *fp)
154156 {
155157 /*
156158 * Skip all records.
157159 */
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+ }
159198 }
160199
161200 int
162201 main (int argc, char **argv)
163202 {
164203 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;
167208
168209 while (1) {
169210 int ch = getopt(argc, argv, "s:np");
@@ -174,7 +215,7 @@ main (int argc, char **argv)
174215 case 's':
175216 if (optarg == NULL) {
176217 perror("-s option requires an argument");
177- exit(1);
218+ exit(EXIT_FAILURE);
178219 }
179220 sscanf(optarg, "%lf", &speed);
180221 break;
@@ -182,20 +223,18 @@ main (int argc, char **argv)
182223 wait_func = ttynowait;
183224 break;
184225 case 'p':
185- ttyskipall();
186- read_func = ttypread;
187- wait_func = ttynowait;
226+ process = ttypeek;
188227 break;
189228 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();
195230 }
196231 }
197232
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);
199238
200239 return 0;
201240 }