]> git.ipfire.org Git - thirdparty/util-linux.git/blob - term-utils/scriptreplay.c
scriptreplay: move all utils to script-playutils.{c,h}
[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 "c.h"
32 #include "xalloc.h"
33 #include "closestream.h"
34 #include "nls.h"
35 #include "strutils.h"
36 #include "optutils.h"
37 #include "script-playutils.h"
38
39 #define SCRIPT_MIN_DELAY 0.0001 /* from original sripreplay.pl */
40
41 static void __attribute__((__noreturn__))
42 usage(void)
43 {
44 FILE *out = stdout;
45 fputs(USAGE_HEADER, out);
46 fprintf(out,
47 _(" %s [options]\n"),
48 program_invocation_short_name);
49 fprintf(out,
50 _(" %s [-t] timingfile [typescript] [divisor]\n"),
51 program_invocation_short_name);
52
53 fputs(USAGE_SEPARATOR, out);
54 fputs(_("Play back terminal typescripts, using timing information.\n"), out);
55
56 fputs(USAGE_OPTIONS, out);
57 fputs(_(" -t, --timing <file> script timing log file\n"), out);
58 fputs(_(" -I, --log-in <file> script stdin log file\n"), out);
59 fputs(_(" -O, --log-out <file> script stdout log file (default)\n"), out);
60 fputs(_(" -B, --log-io <file> script stdin and stdout log file\n"), out);
61 fputs(_(" -s, --typescript <file> deprecated alist to -O\n"), out);
62
63 fputs(USAGE_SEPARATOR, out);
64 fputs(_(" --summary display overview about recorded session and exit\n"), out);
65 fputs(_(" -d, --divisor <num> speed up or slow down execution with time divisor\n"), out);
66 fputs(_(" -m, --maxdelay <num> wait at most this many seconds between updates\n"), out);
67 fputs(_(" -x, --stream <name> stream type (out, in, signal or info)\n"), out);
68 fputs(_(" -c, --cr-mode <type> CR char mode (auto, never, always)\n"), out);
69 printf(USAGE_HELP_OPTIONS(25));
70
71 printf(USAGE_MAN_TAIL("scriptreplay(1)"));
72 exit(EXIT_SUCCESS);
73 }
74
75 static double
76 getnum(const char *s)
77 {
78 const double d = strtod_or_err(s, _("failed to parse number"));
79
80 if (isnan(d)) {
81 errno = EINVAL;
82 err(EXIT_FAILURE, "%s: %s", _("failed to parse number"), s);
83 }
84 return d;
85 }
86
87 static void
88 delay_for(double delay)
89 {
90 #ifdef HAVE_NANOSLEEP
91 struct timespec ts, remainder;
92 ts.tv_sec = (time_t) delay;
93 ts.tv_nsec = (delay - ts.tv_sec) * 1.0e9;
94
95 DBG(TIMING, ul_debug("going to sleep for %fs", delay));
96
97 while (-1 == nanosleep(&ts, &remainder)) {
98 if (EINTR == errno)
99 ts = remainder;
100 else
101 break;
102 }
103 #else
104 struct timeval tv;
105 tv.tv_sec = (long) delay;
106 tv.tv_usec = (delay - tv.tv_sec) * 1.0e6;
107 select(0, NULL, NULL, NULL, &tv);
108 #endif
109 }
110
111 static void appendchr(char *buf, size_t bufsz, int c)
112 {
113 size_t sz;
114
115 if (strchr(buf, c))
116 return; /* already in */
117
118 sz = strlen(buf);
119 if (sz + 1 < bufsz)
120 buf[sz] = c;
121 }
122
123 int
124 main(int argc, char *argv[])
125 {
126 struct replay_setup *setup = NULL;
127 struct replay_step *step = NULL;
128 char streams[6] = {0}; /* IOSI - in, out, signal,info */
129 const char *log_out = NULL,
130 *log_in = NULL,
131 *log_io = NULL,
132 *log_tm = NULL;
133 double divi = 1, maxdelay = 0;
134 int diviopt = FALSE, maxdelayopt = FALSE, idx;
135 int ch, rc, crmode = REPLAY_CRMODE_AUTO, summary = 0;
136 enum {
137 OPT_SUMMARY = CHAR_MAX + 1
138 };
139
140 static const struct option longopts[] = {
141 { "cr-mode", required_argument, 0, 'c' },
142 { "timing", required_argument, 0, 't' },
143 { "log-in", required_argument, 0, 'I'},
144 { "log-out", required_argument, 0, 'O'},
145 { "log-io", required_argument, 0, 'B'},
146 { "typescript", required_argument, 0, 's' },
147 { "divisor", required_argument, 0, 'd' },
148 { "maxdelay", required_argument, 0, 'm' },
149 { "stream", required_argument, 0, 'x' },
150 { "summary", no_argument, 0, OPT_SUMMARY },
151 { "version", no_argument, 0, 'V' },
152 { "help", no_argument, 0, 'h' },
153 { NULL, 0, 0, 0 }
154 };
155 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
156 { 'O', 's' },
157 { 0 }
158 };
159 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
160 /* Because we use space as a separator, we can't afford to use any
161 * locale which tolerates a space in a number. In any case, script.c
162 * sets the LC_NUMERIC locale to C, anyway.
163 */
164 setlocale(LC_ALL, "");
165 setlocale(LC_NUMERIC, "C");
166
167 bindtextdomain(PACKAGE, LOCALEDIR);
168 textdomain(PACKAGE);
169 close_stdout_atexit();
170
171 replay_init_debug();
172
173 while ((ch = getopt_long(argc, argv, "B:c:I:O:t:s:d:m:x:Vh", longopts, NULL)) != -1) {
174
175 err_exclusive_options(ch, longopts, excl, excl_st);
176
177 switch(ch) {
178 case 'c':
179 if (strcmp("auto", optarg) == 0)
180 crmode = REPLAY_CRMODE_AUTO;
181 else if (strcmp("never", optarg) == 0)
182 crmode = REPLAY_CRMODE_NEVER;
183 else if (strcmp("always", optarg) == 0)
184 crmode = REPLAY_CRMODE_ALWAYS;
185 else
186 errx(EXIT_FAILURE, _("unsupported mode name: '%s'"), optarg);
187 break;
188 case 't':
189 log_tm = optarg;
190 break;
191 case 'O':
192 case 's':
193 log_out = optarg;
194 break;
195 case 'I':
196 log_in = optarg;
197 break;
198 case 'B':
199 log_io = optarg;
200 break;
201 case 'd':
202 diviopt = TRUE;
203 divi = getnum(optarg);
204 break;
205 case 'm':
206 maxdelayopt = TRUE;
207 maxdelay = getnum(optarg);
208 break;
209 case 'x':
210 if (strcmp("in", optarg) == 0)
211 appendchr(streams, sizeof(streams), 'I');
212 else if (strcmp("out", optarg) == 0)
213 appendchr(streams, sizeof(streams), 'O');
214 else if (strcmp("signal", optarg) == 0)
215 appendchr(streams, sizeof(streams), 'S');
216 else if (strcmp("info", optarg) == 0)
217 appendchr(streams, sizeof(streams), 'H');
218 else
219 errx(EXIT_FAILURE, _("unsupported stream name: '%s'"), optarg);
220 break;
221 case OPT_SUMMARY:
222 summary = 1;
223 break;
224 case 'V':
225 print_version(EXIT_SUCCESS);
226 case 'h':
227 usage();
228 default:
229 errtryhelp(EXIT_FAILURE);
230 }
231 }
232 argc -= optind;
233 argv += optind;
234 idx = 0;
235
236 if (summary)
237 streams[0] = 'H', streams[1] = '\0';
238
239 if (!log_tm && idx < argc)
240 log_tm = argv[idx++];
241 if (!log_out && !log_in && !log_io)
242 log_out = idx < argc ? argv[idx++] : "typescript";
243
244 if (!diviopt)
245 divi = idx < argc ? getnum(argv[idx]) : 1;
246 if (maxdelay < 0)
247 maxdelay = 0;
248
249 if (!log_tm)
250 errx(EXIT_FAILURE, _("timing file not specified"));
251 if (!(log_out || log_in || log_io))
252 errx(EXIT_FAILURE, _("data log file not specified"));
253
254 setup = replay_new_setup();
255
256 if (replay_set_timing_file(setup, log_tm) != 0)
257 err(EXIT_FAILURE, _("cannot open %s"), log_tm);
258
259 if (log_out && replay_associate_log(setup, "O", log_out) != 0)
260 err(EXIT_FAILURE, _("cannot open %s"), log_out);
261
262 if (log_in && replay_associate_log(setup, "I", log_in) != 0)
263 err(EXIT_FAILURE, _("cannot open %s"), log_in);
264
265 if (log_io && replay_associate_log(setup, "IO", log_io) != 0)
266 err(EXIT_FAILURE, _("cannot open %s"), log_io);
267
268 if (!*streams) {
269 /* output is prefered default */
270 if (log_out || log_io)
271 appendchr(streams, sizeof(streams), 'O');
272 else if (log_in)
273 appendchr(streams, sizeof(streams), 'I');
274 }
275
276 replay_set_default_type(setup,
277 *streams && streams[1] == '\0' ? *streams : 'O');
278 replay_set_crmode(setup, crmode);
279
280 do {
281 rc = replay_get_next_step(setup, streams, &step);
282 if (rc)
283 break;
284
285 if (!summary) {
286 double delay = replay_step_get_delay(step);
287
288 delay /= divi;
289 if (maxdelayopt && delay > maxdelay)
290 delay = maxdelay;
291 if (delay > SCRIPT_MIN_DELAY)
292 delay_for(delay);
293 }
294
295 rc = replay_emit_step_data(setup, step, STDOUT_FILENO);
296 } while (rc == 0);
297
298 if (step && rc < 0)
299 err(EXIT_FAILURE, _("%s: log file error"), replay_step_get_filename(step));
300 else if (rc < 0)
301 err(EXIT_FAILURE, _("%s: line %d: timing file error"),
302 replay_get_timing_file(setup),
303 replay_get_timing_line(setup));
304 printf("\n");
305 exit(EXIT_SUCCESS);
306 }