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