2 * Copyright (C) 2008, Karel Zak <kzak@redhat.com>
3 * Copyright (C) 2008, James Youngman <jay@gnu.org>
5 * This file is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This file is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 * Based on scriptreplay.pl by Joey Hess <joey@kitenet.net>
27 #include <sys/select.h>
31 #include "closestream.h"
36 #define SCRIPT_MIN_DELAY 0.0001 /* from original sripreplay.pl */
38 static void __attribute__((__noreturn__
))
42 fputs(USAGE_HEADER
, out
);
44 _(" %s [-t] timingfile [typescript] [divisor]\n"),
45 program_invocation_short_name
);
47 fputs(USAGE_SEPARATOR
, out
);
48 fputs(_("Play back terminal typescripts, using timing information.\n"), out
);
50 fputs(USAGE_OPTIONS
, out
);
51 fputs(_(" -t, --timing <file> script timing output file\n"
52 " -s, --typescript <file> script terminal session output file\n"
53 " -d, --divisor <num> speed up or slow down execution with time divisor\n"
54 " -m, --maxdelay <num> wait at most this many seconds between updates\n"
56 printf(USAGE_HELP_OPTIONS(25));
58 printf(USAGE_MAN_TAIL("scriptreplay(1)"));
65 const double d
= strtod_or_err(s
, _("failed to parse number"));
69 err(EXIT_FAILURE
, "%s: %s", _("failed to parse number"), s
);
75 delay_for(double delay
)
78 struct timespec ts
, remainder
;
79 ts
.tv_sec
= (time_t) delay
;
80 ts
.tv_nsec
= (delay
- ts
.tv_sec
) * 1.0e9
;
82 while (-1 == nanosleep(&ts
, &remainder
)) {
90 tv
.tv_sec
= (long) delay
;
91 tv
.tv_usec
= (delay
- tv
.tv_sec
) * 1.0e6
;
92 select(0, NULL
, NULL
, NULL
, &tv
);
97 emit(FILE *fd
, const char *filename
, size_t ct
)
104 cc
= ct
> sizeof(buf
) ? sizeof(buf
) : ct
;
105 len
= fread(buf
, 1, cc
, fd
);
111 cc
= write(STDOUT_FILENO
, buf
, len
);
113 err(EXIT_FAILURE
, _("write to stdout failed"));
119 errx(EXIT_FAILURE
, _("unexpected end of file on %s"), filename
);
121 err(EXIT_FAILURE
, _("failed to read typescript file %s"), filename
);
126 main(int argc
, char *argv
[])
129 const char *sname
= NULL
, *tname
= NULL
;
130 double divi
= 1, maxdelay
= 0;
131 int c
, diviopt
= FALSE
, maxdelayopt
= FALSE
, idx
;
135 static const struct option longopts
[] = {
136 { "timing", required_argument
, 0, 't' },
137 { "typescript", required_argument
, 0, 's' },
138 { "divisor", required_argument
, 0, 'd' },
139 { "maxdelay", required_argument
, 0, 'm' },
140 { "version", no_argument
, 0, 'V' },
141 { "help", no_argument
, 0, 'h' },
145 /* Because we use space as a separator, we can't afford to use any
146 * locale which tolerates a space in a number. In any case, script.c
147 * sets the LC_NUMERIC locale to C, anyway.
149 setlocale(LC_ALL
, "");
150 setlocale(LC_NUMERIC
, "C");
152 bindtextdomain(PACKAGE
, LOCALEDIR
);
154 close_stdout_atexit();
156 while ((ch
= getopt_long(argc
, argv
, "t:s:d:m:Vh", longopts
, NULL
)) != -1)
166 divi
= getnum(optarg
);
170 maxdelay
= getnum(optarg
);
174 print_version(EXIT_SUCCESS
);
178 errtryhelp(EXIT_FAILURE
);
184 if ((argc
< 1 && !tname
) || argc
> 3) {
185 warnx(_("wrong number of arguments"));
186 errtryhelp(EXIT_FAILURE
);
191 sname
= idx
< argc
? argv
[idx
++] : "typescript";
193 divi
= idx
< argc
? getnum(argv
[idx
]) : 1;
196 tfile
= fopen(tname
, "r");
198 err(EXIT_FAILURE
, _("cannot open %s"), tname
);
199 sfile
= fopen(sname
, "r");
201 err(EXIT_FAILURE
, _("cannot open %s"), sname
);
203 /* ignore the first typescript line */
204 while((c
= fgetc(sfile
)) != EOF
&& c
!= '\n');
206 for(line
= 1; ; line
++) {
210 if (fscanf(tfile
, "%lf %zu%c\n", &delay
, &blk
, &nl
) != 3 ||
216 _("failed to read timing file %s"), tname
);
218 _("timing file %s: line %lu: unexpected format"),
223 if (maxdelayopt
&& delay
> maxdelay
)
226 if (delay
> SCRIPT_MIN_DELAY
)
229 emit(sfile
, sname
, blk
);