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"
35 #define SCRIPT_MIN_DELAY 0.0001 /* from original sripreplay.pl */
37 void __attribute__((__noreturn__
))
40 fputs(_("\nUsage:\n"), out
);
42 _(" %s [-t] timingfile [typescript] [divisor]\n"),
43 program_invocation_short_name
);
45 fputs(_("\nOptions:\n"), out
);
46 fputs(_(" -t, --timing <file> script timing output file\n"
47 " -s, --typescript <file> script terminal session output file\n"
48 " -d, --divisor <num> speed up or slow down execution with time divisor\n"
49 " -V, --version output version information and exit\n"
50 " -h, --help display this help and exit\n\n"), out
);
52 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
64 if (end
&& *end
!= '\0')
65 errx(EXIT_FAILURE
, _("expected a number, but got '%s'"), s
);
67 if ((d
== HUGE_VAL
|| d
== -HUGE_VAL
) && ERANGE
== errno
)
68 err(EXIT_FAILURE
, _("divisor '%s'"), s
);
70 if (!(d
==d
)) { /* did they specify "nan"? */
72 err(EXIT_FAILURE
, _("divisor '%s'"), s
);
78 delay_for(double delay
)
81 struct timespec ts
, remainder
;
82 ts
.tv_sec
= (time_t) delay
;
83 ts
.tv_nsec
= (delay
- ts
.tv_sec
) * 1.0e9
;
85 while (-1 == nanosleep(&ts
, &remainder
)) {
93 tv
.tv_sec
= (long) delay
;
94 tv
.tv_usec
= (delay
- tv
.tv_sec
) * 1.0e6
;
95 select(0, NULL
, NULL
, NULL
, &tv
);
100 emit(FILE *fd
, const char *filename
, size_t ct
)
107 cc
= ct
> sizeof(buf
) ? sizeof(buf
) : ct
;
108 len
= fread(buf
, 1, cc
, fd
);
114 cc
= write(STDOUT_FILENO
, buf
, len
);
116 err(EXIT_FAILURE
, _("write to stdout failed"));
122 errx(EXIT_FAILURE
, _("unexpected end of file on %s"), filename
);
124 err(EXIT_FAILURE
, _("failed to read typescript file %s"), filename
);
129 main(int argc
, char *argv
[])
132 const char *sname
= NULL
, *tname
= NULL
;
134 int c
, diviopt
= FALSE
, idx
;
139 static const struct option longopts
[] = {
140 { "timing", required_argument
, 0, 't' },
141 { "typescript", required_argument
, 0, 's' },
142 { "divisor", required_argument
, 0, 'd' },
143 { "version", no_argument
, 0, 'V' },
144 { "help", no_argument
, 0, 'h' },
148 /* Because we use space as a separator, we can't afford to use any
149 * locale which tolerates a space in a number. In any case, script.c
150 * sets the LC_NUMERIC locale to C, anyway.
152 setlocale(LC_ALL
, "");
153 setlocale(LC_NUMERIC
, "C");
155 bindtextdomain(PACKAGE
, LOCALEDIR
);
157 atexit(close_stdout
);
159 while ((ch
= getopt_long(argc
, argv
, "t:s:d:Vh", longopts
, NULL
)) != -1)
169 divi
= getnum(optarg
);
172 printf(_("%s from %s\n"), program_invocation_short_name
,
184 if ((argc
< 1 && !tname
) || argc
> 3) {
185 warnx(_("wrong number of arguments"));
191 sname
= idx
< argc
? argv
[idx
++] : "typescript";
193 divi
= idx
< argc
? getnum(argv
[idx
]) : 1;
195 tfile
= fopen(tname
, "r");
197 err(EXIT_FAILURE
, _("cannot open timing file %s"), tname
);
198 sfile
= fopen(sname
, "r");
200 err(EXIT_FAILURE
, _("cannot open typescript file %s"), sname
);
202 /* ignore the first typescript line */
203 while((c
= fgetc(sfile
)) != EOF
&& c
!= '\n');
205 for(line
= 0; ; line
++) {
209 if (fscanf(tfile
, "%lf %zu%c\n", &delay
, &blk
, &nl
) != 3 ||
215 _("failed to read timing file %s"), tname
);
217 _("timings file %s: %lu: unexpected format"),
222 if (delay
> SCRIPT_MIN_DELAY
)
226 emit(sfile
, sname
, oldblk
);