]> git.ipfire.org Git - thirdparty/util-linux.git/blame - term-utils/scriptreplay.c
Merge branch 'master' of https://github.com/pali/util-linux
[thirdparty/util-linux.git] / term-utils / scriptreplay.c
CommitLineData
18a706bd
KZ
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>
84adb5e6 29#include <getopt.h>
18a706bd 30
cdd2a8c3 31#include "closestream.h"
18a706bd 32#include "nls.h"
eb76ca98 33#include "c.h"
18a706bd
KZ
34
35#define SCRIPT_MIN_DELAY 0.0001 /* from original sripreplay.pl */
36
5769a938 37static void __attribute__((__noreturn__))
84adb5e6 38usage(FILE *out)
18a706bd 39{
db433bf7 40 fputs(USAGE_HEADER, out);
14cc9dda
KZ
41 fprintf(out,
42 _(" %s [-t] timingfile [typescript] [divisor]\n"),
43 program_invocation_short_name);
44
451dbcfa
BS
45 fputs(USAGE_SEPARATOR, out);
46 fputs(_("Play back terminal typescripts, using timing information.\n"), out);
47
db433bf7 48 fputs(USAGE_OPTIONS, out);
14cc9dda
KZ
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"
7f1d4836 52 " -m, --maxdelay <num> wait at most this many seconds between updates\n"
84adb5e6 53 " -V, --version output version information and exit\n"
14cc9dda 54 " -h, --help display this help and exit\n\n"), out);
84adb5e6 55
a587cc55 56 fprintf(out, USAGE_MAN_TAIL("scriptreplay(1)"));
84adb5e6 57 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
18a706bd
KZ
58}
59
60static double
61getnum(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
82static void
83delay_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
104static void
105emit(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)
3e6e4bf6 121 err(EXIT_FAILURE, _("write to stdout failed"));
18a706bd
KZ
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
133int
134main(int argc, char *argv[])
135{
136 FILE *tfile, *sfile;
0da871b5 137 const char *sname = NULL, *tname = NULL;
7f1d4836
JDN
138 double divi = 1, maxdelay = 0;
139 int c, diviopt = FALSE, maxdelayopt = FALSE, idx;
18a706bd 140 unsigned long line;
84adb5e6
SK
141 char ch;
142
143 static const struct option longopts[] = {
0da871b5
SK
144 { "timing", required_argument, 0, 't' },
145 { "typescript", required_argument, 0, 's' },
146 { "divisor", required_argument, 0, 'd' },
7f1d4836 147 { "maxdelay", required_argument, 0, 'm' },
84adb5e6
SK
148 { "version", no_argument, 0, 'V' },
149 { "help", no_argument, 0, 'h' },
150 { NULL, 0, 0, 0 }
151 };
18a706bd
KZ
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);
cdd2a8c3 162 atexit(close_stdout);
18a706bd 163
7f1d4836 164 while ((ch = getopt_long(argc, argv, "t:s:d:m:Vh", longopts, NULL)) != -1)
84adb5e6 165 switch(ch) {
0da871b5
SK
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;
7f1d4836
JDN
176 case 'm':
177 maxdelayopt = TRUE;
178 maxdelay = getnum(optarg);
179 break;
84adb5e6 180 case 'V':
f6277500 181 printf(UTIL_LINUX_VERSION);
84adb5e6
SK
182 exit(EXIT_SUCCESS);
183 case 'h':
184 usage(stdout);
185 default:
186 usage(stderr);
187 }
188 argc -= optind;
189 argv += optind;
0da871b5 190 idx = 0;
84adb5e6 191
0da871b5 192 if ((argc < 1 && !tname) || argc > 3) {
84adb5e6
SK
193 warnx(_("wrong number of arguments"));
194 usage(stderr);
195 }
0da871b5
SK
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;
7f1d4836
JDN
202 if (maxdelay < 0)
203 maxdelay = 0;
18a706bd
KZ
204 tfile = fopen(tname, "r");
205 if (!tfile)
289dcc90 206 err(EXIT_FAILURE, _("cannot open %s"), tname);
18a706bd
KZ
207 sfile = fopen(sname, "r");
208 if (!sfile)
289dcc90 209 err(EXIT_FAILURE, _("cannot open %s"), sname);
18a706bd
KZ
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;
66fc0665 218 if (fscanf(tfile, "%lf %zu%c\n", &delay, &blk, &nl) != 3 ||
739de076 219 nl != '\n') {
18a706bd
KZ
220 if (feof(tfile))
221 break;
222 if (ferror(tfile))
223 err(EXIT_FAILURE,
3e6e4bf6 224 _("failed to read timing file %s"), tname);
18a706bd 225 errx(EXIT_FAILURE,
91b72e09 226 _("timings file %s: %lu: unexpected format"),
18a706bd
KZ
227 tname, line);
228 }
229 delay /= divi;
230
7f1d4836
JDN
231 if (maxdelayopt && delay > maxdelay)
232 delay = maxdelay;
233
18a706bd
KZ
234 if (delay > SCRIPT_MIN_DELAY)
235 delay_for(delay);
236
a21f7ec9 237 emit(sfile, sname, blk);
18a706bd
KZ
238 }
239
240 fclose(sfile);
241 fclose(tfile);
f004a02d 242 printf("\n");
18a706bd
KZ
243 exit(EXIT_SUCCESS);
244}