ttyrecのfork. Original: http://0xcc.net/ttyrec/
Revisão | c64b05514c797d98abb2c186386cc54b7c491168 (tree) |
---|---|
Hora | 2019-12-09 16:11:47 |
Autor | IWAMOTO Kouichi <sue@iwmt...> |
Commiter | IWAMOTO Kouichi |
update to ttyrec-1.0.2
@@ -1,6 +1,6 @@ | ||
1 | 1 | CC = gcc |
2 | 2 | CFLAGS = -O2 |
3 | -VERSION = 1.0.1 | |
3 | +VERSION = 1.0.2 | |
4 | 4 | |
5 | 5 | TARGET = ttyrec ttyplay |
6 | 6 |
@@ -1,5 +1,13 @@ | ||
1 | 1 | ttyrec is a tty recorder. ttyplay is a tty player. |
2 | 2 | |
3 | +Installation: | |
4 | + | |
5 | + % make | |
6 | + | |
7 | +or if your system is SVR4 series (Solaris etc.), | |
8 | + | |
9 | + % make CFLAGS=-DSVR4 | |
10 | + | |
3 | 11 | Usage: |
4 | 12 | |
5 | 13 | % ttyrec |
@@ -46,6 +46,8 @@ typedef void (*WaitFunc) (struct timeval prev, | ||
46 | 46 | double speed); |
47 | 47 | typedef int (*ReadFunc) (FILE *fp, Header *h, char **buf); |
48 | 48 | typedef void (*WriteFunc) (char *buf, int len); |
49 | +typedef void (*ProcessFunc) (FILE *fp, double speed, | |
50 | + ReadFunc read_func, WaitFunc wait_func); | |
49 | 51 | |
50 | 52 | struct timeval |
51 | 53 | timeval_diff (struct timeval tv1, struct timeval tv2) |
@@ -69,8 +71,13 @@ ttywait (struct timeval prev, struct timeval cur, double speed) | ||
69 | 71 | |
70 | 72 | assert(speed != 0); |
71 | 73 | |
72 | - sleep(diff.tv_sec / speed); | |
73 | - usleep(diff.tv_usec / speed); | |
74 | + /* | |
75 | + * Use select instead of sleep and usleep because some | |
76 | + * systems don't support usleep. | |
77 | + */ | |
78 | + diff.tv_sec /= speed; | |
79 | + diff.tv_usec /= speed; | |
80 | + select(0, NULL, NULL, NULL, &diff); | |
74 | 81 | } |
75 | 82 | |
76 | 83 | void |
@@ -100,11 +107,12 @@ ttyread (FILE *fp, Header *h, char **buf) | ||
100 | 107 | int |
101 | 108 | ttypread (FILE *fp, Header *h, char **buf) |
102 | 109 | { |
110 | + struct timeval w = {0, 250000}; | |
103 | 111 | /* |
104 | 112 | * Read persistently just like tail -f. |
105 | 113 | */ |
106 | 114 | while (ttyread(fp, h, buf) == 0) { |
107 | - usleep(250000); | |
115 | + select(0, NULL, NULL, NULL, &w); | |
108 | 116 | clearerr(fp); |
109 | 117 | } |
110 | 118 | return 1; |
@@ -160,9 +168,6 @@ ttyskipall (FILE *fp) | ||
160 | 168 | ttyplay(fp, 0, ttyread, ttynowrite, ttynowait); |
161 | 169 | } |
162 | 170 | |
163 | -typedef void (*ProcessFunc) (FILE *fp, double speed, | |
164 | - ReadFunc read_func, WaitFunc wait_func); | |
165 | - | |
166 | 171 | void ttyplayback (FILE *fp, double speed, |
167 | 172 | ReadFunc read_func, WaitFunc wait_func) |
168 | 173 | { |
@@ -195,6 +200,7 @@ efopen (const char *path, const char *mode) | ||
195 | 200 | fprintf(stderr, "ttyplay: %s: %s\n", path, strerror(errno)); |
196 | 201 | exit(EXIT_FAILURE); |
197 | 202 | } |
203 | + return fp; | |
198 | 204 | } |
199 | 205 | |
200 | 206 | int |
@@ -51,6 +51,13 @@ | ||
51 | 51 | #include <sys/signal.h> |
52 | 52 | #include <stdio.h> |
53 | 53 | |
54 | +#if defined(SVR4) | |
55 | +#include <stdlib.h> | |
56 | +#include <unistd.h> | |
57 | +#include <fcntl.h> | |
58 | +#include <stropts.h> | |
59 | +#endif /* SVR4 */ | |
60 | + | |
54 | 61 | #ifdef __linux__ |
55 | 62 | #include <unistd.h> |
56 | 63 | #include <string.h> |
@@ -90,9 +97,11 @@ struct termios tt; | ||
90 | 97 | struct winsize win; |
91 | 98 | int lb; |
92 | 99 | int l; |
100 | +#if !defined(SVR4) | |
93 | 101 | #ifndef HAVE_openpty |
94 | 102 | char line[] = "/dev/ptyXX"; |
95 | 103 | #endif |
104 | +#endif /* !SVR4 */ | |
96 | 105 | int aflg; |
97 | 106 | |
98 | 107 | int |
@@ -177,7 +186,11 @@ doinput() | ||
177 | 186 | void |
178 | 187 | finish() |
179 | 188 | { |
189 | +#if defined(SVR4) | |
190 | + int status; | |
191 | +#else /* !SVR4 */ | |
180 | 192 | union wait status; |
193 | +#endif /* !SVR4 */ | |
181 | 194 | register int pid; |
182 | 195 | register int die = 0; |
183 | 196 |
@@ -236,11 +249,9 @@ doshell() | ||
236 | 249 | (void) dup2(slave, 1); |
237 | 250 | (void) dup2(slave, 2); |
238 | 251 | (void) close(slave); |
239 | -#ifdef __linux__ | |
252 | + | |
240 | 253 | execl(shell, strrchr(shell, '/') + 1, "-i", 0); |
241 | -#else | |
242 | - execl(shell, "sh", "-i", 0); | |
243 | -#endif | |
254 | + | |
244 | 255 | perror(shell); |
245 | 256 | fail(); |
246 | 257 | } |
@@ -251,8 +262,20 @@ fixtty() | ||
251 | 262 | struct termios rtt; |
252 | 263 | |
253 | 264 | rtt = tt; |
265 | +#if defined(SVR4) | |
266 | + rtt.c_iflag = 0; | |
267 | + rtt.c_lflag &= ~(ISIG|ICANON|XCASE|ECHO|ECHOE|ECHOK|ECHONL); | |
268 | + rtt.c_oflag = OPOST; | |
269 | + rtt.c_cc[VINTR] = CDEL; | |
270 | + rtt.c_cc[VQUIT] = CDEL; | |
271 | + rtt.c_cc[VERASE] = CDEL; | |
272 | + rtt.c_cc[VKILL] = CDEL; | |
273 | + rtt.c_cc[VEOF] = 1; | |
274 | + rtt.c_cc[VEOL] = 0; | |
275 | +#else /* !SVR4 */ | |
254 | 276 | cfmakeraw(&rtt); |
255 | 277 | rtt.c_lflag &= ~ECHO; |
278 | +#endif /* !SVR4 */ | |
256 | 279 | (void) tcsetattr(0, TCSAFLUSH, &rtt); |
257 | 280 | } |
258 | 281 |
@@ -283,6 +306,14 @@ done() | ||
283 | 306 | void |
284 | 307 | getmaster() |
285 | 308 | { |
309 | +#if defined(SVR4) | |
310 | + (void) tcgetattr(0, &tt); | |
311 | + (void) ioctl(0, TIOCGWINSZ, (char *)&win); | |
312 | + if ((master = open("/dev/ptmx", O_RDWR)) < 0) { | |
313 | + perror("open(\"/dev/ptmx\", O_RDWR)"); | |
314 | + fail(); | |
315 | + } | |
316 | +#else /* !SVR4 */ | |
286 | 317 | #ifdef HAVE_openpty |
287 | 318 | (void) tcgetattr(0, &tt); |
288 | 319 | (void) ioctl(0, TIOCGWINSZ, (char *)&win); |
@@ -324,11 +355,34 @@ getmaster() | ||
324 | 355 | fprintf(stderr, _("Out of pty's\n")); |
325 | 356 | fail(); |
326 | 357 | #endif /* not HAVE_openpty */ |
358 | +#endif /* !SVR4 */ | |
327 | 359 | } |
328 | 360 | |
329 | 361 | void |
330 | 362 | getslave() |
331 | 363 | { |
364 | +#if defined(SVR4) | |
365 | + (void) setsid(); | |
366 | + grantpt( master); | |
367 | + unlockpt(master); | |
368 | + if ((slave = open(ptsname(master), O_RDWR)) < 0) { | |
369 | + perror("open(fd, O_RDWR)"); | |
370 | + fail(); | |
371 | + } | |
372 | + if (ioctl(slave, I_PUSH, "ptem") < 0) { | |
373 | + perror("ioctl(fd, I_PUSH, ptem)"); | |
374 | + fail(); | |
375 | + } | |
376 | + if (ioctl(slave, I_PUSH, "ldterm") < 0) { | |
377 | + perror("ioctl(fd, I_PUSH, ldterm)"); | |
378 | + fail(); | |
379 | + } | |
380 | + if (ioctl(slave, I_PUSH, "ttcompat") < 0) { | |
381 | + perror("ioctl(fd, I_PUSH, ttcompat)"); | |
382 | + fail(); | |
383 | + } | |
384 | + (void) ioctl(0, TIOCGWINSZ, (char *)&win); | |
385 | +#else /* !SVR4 */ | |
332 | 386 | #ifndef HAVE_openpty |
333 | 387 | line[strlen("/dev/")] = 't'; |
334 | 388 | slave = open(line, O_RDWR); |
@@ -341,4 +395,5 @@ getslave() | ||
341 | 395 | #endif |
342 | 396 | (void) setsid(); |
343 | 397 | (void) ioctl(slave, TIOCSCTTY, 0); |
398 | +#endif /* SVR4 */ | |
344 | 399 | } |