]> git.ipfire.org Git - thirdparty/util-linux.git/blob - term-utils/scriptreplay.c
Merge branch 'fix-script-timing' of https://github.com/theonewolf/util-linux
[thirdparty/util-linux.git] / term-utils / scriptreplay.c
1 /*
2 * Copyright (C) 2008, Karel Zak <kzak@redhat.com>
3 * Copyright (C) 2008, James Youngman <jay@gnu.org>
4 *
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.
9 *
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.
14 *
15 *
16 * Based on scriptreplay.pl by Joey Hess <joey@kitenet.net>
17 */
18
19 #include <stdio.h>
20 #include <stdarg.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <time.h>
25 #include <limits.h>
26 #include <math.h>
27 #include <sys/select.h>
28 #include <unistd.h>
29 #include <getopt.h>
30
31 #include "closestream.h"
32 #include "nls.h"
33 #include "c.h"
34
35 #define SCRIPT_MIN_DELAY 0.0001 /* from original sripreplay.pl */
36
37 static void __attribute__((__noreturn__))
38 usage(FILE *out)
39 {
40 fputs(_("\nUsage:\n"), out);
41 fprintf(out,
42 _(" %s [-t] timingfile [typescript] [divisor]\n"),
43 program_invocation_short_name);
44
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 " -m, --maxdelay <num> wait at most this many seconds between updates\n"
50 " -V, --version output version information and exit\n"
51 " -h, --help display this help and exit\n\n"), out);
52
53 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
54 }
55
56 static double
57 getnum(const char *s)
58 {
59 double d;
60 char *end;
61
62 errno = 0;
63 d = strtod(s, &end);
64
65 if (end && *end != '\0')
66 errx(EXIT_FAILURE, _("expected a number, but got '%s'"), s);
67
68 if ((d == HUGE_VAL || d == -HUGE_VAL) && ERANGE == errno)
69 err(EXIT_FAILURE, _("divisor '%s'"), s);
70
71 if (!(d==d)) { /* did they specify "nan"? */
72 errno = EINVAL;
73 err(EXIT_FAILURE, _("divisor '%s'"), s);
74 }
75 return d;
76 }
77
78 static void
79 delay_for(double delay)
80 {
81 #ifdef HAVE_NANOSLEEP
82 struct timespec ts, remainder;
83 ts.tv_sec = (time_t) delay;
84 ts.tv_nsec = (delay - ts.tv_sec) * 1.0e9;
85
86 while (-1 == nanosleep(&ts, &remainder)) {
87 if (EINTR == errno)
88 ts = remainder;
89 else
90 break;
91 }
92 #else
93 struct timeval tv;
94 tv.tv_sec = (long) delay;
95 tv.tv_usec = (delay - tv.tv_sec) * 1.0e6;
96 select(0, NULL, NULL, NULL, &tv);
97 #endif
98 }
99
100 static void
101 emit(FILE *fd, const char *filename, size_t ct)
102 {
103 char buf[BUFSIZ];
104
105 while(ct) {
106 size_t len, cc;
107
108 cc = ct > sizeof(buf) ? sizeof(buf) : ct;
109 len = fread(buf, 1, cc, fd);
110
111 if (!len)
112 break;
113
114 ct -= len;
115 cc = write(STDOUT_FILENO, buf, len);
116 if (cc != len)
117 err(EXIT_FAILURE, _("write to stdout failed"));
118 }
119
120 if (!ct)
121 return;
122 if (feof(fd))
123 errx(EXIT_FAILURE, _("unexpected end of file on %s"), filename);
124
125 err(EXIT_FAILURE, _("failed to read typescript file %s"), filename);
126 }
127
128
129 int
130 main(int argc, char *argv[])
131 {
132 FILE *tfile, *sfile;
133 const char *sname = NULL, *tname = NULL;
134 double divi = 1, maxdelay = 0;
135 int c, diviopt = FALSE, maxdelayopt = FALSE, idx;
136 unsigned long line;
137 char ch;
138
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 { "maxdelay", required_argument, 0, 'm' },
144 { "version", no_argument, 0, 'V' },
145 { "help", no_argument, 0, 'h' },
146 { NULL, 0, 0, 0 }
147 };
148
149 /* Because we use space as a separator, we can't afford to use any
150 * locale which tolerates a space in a number. In any case, script.c
151 * sets the LC_NUMERIC locale to C, anyway.
152 */
153 setlocale(LC_ALL, "");
154 setlocale(LC_NUMERIC, "C");
155
156 bindtextdomain(PACKAGE, LOCALEDIR);
157 textdomain(PACKAGE);
158 atexit(close_stdout);
159
160 while ((ch = getopt_long(argc, argv, "t:s:d:m:Vh", longopts, NULL)) != -1)
161 switch(ch) {
162 case 't':
163 tname = optarg;
164 break;
165 case 's':
166 sname = optarg;
167 break;
168 case 'd':
169 diviopt = TRUE;
170 divi = getnum(optarg);
171 break;
172 case 'm':
173 maxdelayopt = TRUE;
174 maxdelay = getnum(optarg);
175 break;
176 case 'V':
177 printf(_("%s from %s\n"), program_invocation_short_name,
178 PACKAGE_STRING);
179 exit(EXIT_SUCCESS);
180 case 'h':
181 usage(stdout);
182 default:
183 usage(stderr);
184 }
185 argc -= optind;
186 argv += optind;
187 idx = 0;
188
189 if ((argc < 1 && !tname) || argc > 3) {
190 warnx(_("wrong number of arguments"));
191 usage(stderr);
192 }
193 if (!tname)
194 tname = argv[idx++];
195 if (!sname)
196 sname = idx < argc ? argv[idx++] : "typescript";
197 if (!diviopt)
198 divi = idx < argc ? getnum(argv[idx]) : 1;
199 if (maxdelay < 0)
200 maxdelay = 0;
201 tfile = fopen(tname, "r");
202 if (!tfile)
203 err(EXIT_FAILURE, _("cannot open %s"), tname);
204 sfile = fopen(sname, "r");
205 if (!sfile)
206 err(EXIT_FAILURE, _("cannot open %s"), sname);
207
208 /* ignore the first typescript line */
209 while((c = fgetc(sfile)) != EOF && c != '\n');
210
211 for(line = 0; ; line++) {
212 double delay;
213 size_t blk;
214 char nl;
215 if (fscanf(tfile, "%lf %zu%c\n", &delay, &blk, &nl) != 3 ||
216 nl != '\n') {
217 if (feof(tfile))
218 break;
219 if (ferror(tfile))
220 err(EXIT_FAILURE,
221 _("failed to read timing file %s"), tname);
222 errx(EXIT_FAILURE,
223 _("timings file %s: %lu: unexpected format"),
224 tname, line);
225 }
226 delay /= divi;
227
228 if (maxdelayopt && delay > maxdelay)
229 delay = maxdelay;
230
231 if (delay > SCRIPT_MIN_DELAY)
232 delay_for(delay);
233
234 emit(sfile, sname, blk);
235 }
236
237 fclose(sfile);
238 fclose(tfile);
239 printf("\n");
240 exit(EXIT_SUCCESS);
241 }