]> git.ipfire.org Git - thirdparty/util-linux.git/blame - term-utils/scriptreplay.c
scriptreplay: add --summary
[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
fec06a06
KZ
31
32#include "c.h"
33#include "debug.h"
34#include "xalloc.h"
cdd2a8c3 35#include "closestream.h"
18a706bd 36#include "nls.h"
f0b3b904 37#include "strutils.h"
0d955cd8 38#include "optutils.h"
fec06a06
KZ
39
40static UL_DEBUG_DEFINE_MASK(scriptreplay);
41UL_DEBUG_DEFINE_MASKNAMES(scriptreplay) = UL_DEBUG_EMPTY_MASKNAMES;
42
43#define SCRIPTREPLAY_DEBUG_INIT (1 << 1)
44#define SCRIPTREPLAY_DEBUG_TIMING (1 << 2)
45#define SCRIPTREPLAY_DEBUG_LOG (1 << 3)
46#define SCRIPTREPLAY_DEBUG_MISC (1 << 4)
47#define SCRIPTREPLAY_DEBUG_ALL 0xFFFF
48
49#define DBG(m, x) __UL_DBG(scriptreplay, SCRIPTREPLAY_DEBUG_, m, x)
50#define ON_DBG(m, x) __UL_DBG_CALL(scriptreplay, SCRIPTREPLAY_DEBUG_, m, x)
18a706bd
KZ
51
52#define SCRIPT_MIN_DELAY 0.0001 /* from original sripreplay.pl */
53
fec06a06
KZ
54/*
55 * The script replay is driven by timing file where each entry describes one
56 * step in the replay. The timing step may refer input or output (or
57 * signal, extra informations, etc.)
58 *
59 * The step data are stored in log files, the right log file for the step is
60 * selected from replay_setup.
61 *
62 * TODO: move struct replay_{log,step,setup} to script-playutils.c to make it
63 * usable for scriptlive(1) code.
64 */
65
66enum {
67 REPLAY_TIMING_SIMPLE, /* timing info in classic "<delta> <offset>" format */
68 REPLAY_TIMING_MULTI /* multiple streams in format "<type> <delta> <offset|etc> */
69};
70
88b8e9bf
KZ
71/* CR to '\n' mode */
72enum {
73 REPLAY_CRMODE_AUTO = 0,
74 REPLAY_CRMODE_NEVER,
75 REPLAY_CRMODE_ALWAYS
76};
77
fec06a06 78struct replay_log {
7c4a374f 79 const char *streams; /* 'I'nput, 'O'utput or both */
fec06a06
KZ
80 const char *filename;
81 FILE *fp;
7c4a374f
KZ
82
83 unsigned int noseek; /* do not seek in this log */
fec06a06
KZ
84};
85
86struct replay_step {
87 char type; /* 'I'nput, 'O'utput, ... */
88 double delay;
89 size_t size;
90
7c4a374f
KZ
91 char *name; /* signals / heders */
92 char *value;
93
fec06a06
KZ
94 struct replay_log *data;
95};
96
97struct replay_setup {
98 struct replay_log *logs;
99 size_t nlogs;
100
101 struct replay_step step; /* current step */
102
103 FILE *timing_fp;
104 const char *timing_filename;
105 int timing_format;
106 int timing_line;
41ce6545
KZ
107
108 char default_type; /* type for REPLAY_TIMING_SIMPLE */
88b8e9bf 109 int crmode;
fec06a06
KZ
110};
111
112static void scriptreplay_init_debug(void)
113{
114 __UL_INIT_DEBUG_FROM_ENV(scriptreplay, SCRIPTREPLAY_DEBUG_, 0, SCRIPTREPLAY_DEBUG);
115}
116
117static int ignore_line(FILE *f)
118{
119 int c;
120
121 while((c = fgetc(f)) != EOF && c != '\n');
122 if (ferror(f))
123 return -errno;
124
125 DBG(LOG, ul_debug(" ignore line"));
126 return 0;
127}
128
41ce6545
KZ
129/* if timing file does not contains types of entries (old format) than use this
130 * type as the default */
131static int replay_set_default_type(struct replay_setup *stp, char type)
132{
133 assert(stp);
134 stp->default_type = type;
135
136 return 0;
137}
138
88b8e9bf
KZ
139static int replay_set_crmode(struct replay_setup *stp, int mode)
140{
141 assert(stp);
142 stp->crmode = mode;
143
144 return 0;
145}
146
7c4a374f
KZ
147static struct replay_log *replay_new_log(struct replay_setup *stp,
148 const char *streams,
149 const char *filename,
150 FILE *f)
151{
152 struct replay_log *log;
153
154 assert(stp);
155 assert(streams);
156 assert(filename);
157
158 stp->logs = xrealloc(stp->logs, (stp->nlogs + 1) * sizeof(*log));
159 log = &stp->logs[stp->nlogs];
160 stp->nlogs++;
161
162 log->filename = filename;
163 log->streams = streams;
164 log->fp = f;
165
166 return log;
167}
168
fec06a06
KZ
169static int replay_set_timing_file(struct replay_setup *stp, const char *filename)
170{
171 int c, rc = 0;
172
173 assert(stp);
174 assert(filename);
175
176 stp->timing_filename = filename;
177 stp->timing_line = 0;
178
179 stp->timing_fp = fopen(filename, "r");
180 if (!stp->timing_fp)
181 rc = -errno;
182 else {
183 /* detect timing file format */
184 c = fgetc(stp->timing_fp);
185 if (c != EOF) {
186 if (isdigit((unsigned int) c))
187 stp->timing_format = REPLAY_TIMING_SIMPLE;
188 else
189 stp->timing_format = REPLAY_TIMING_MULTI;
190 ungetc(c, stp->timing_fp);
191 } else if (ferror(stp->timing_fp))
192 rc = -errno;
193 }
194
4f4bac42 195 if (rc && stp->timing_fp) {
fec06a06
KZ
196 fclose(stp->timing_fp);
197 stp->timing_fp = NULL;
198 }
199
7c4a374f
KZ
200 /* create quasi-log for signals, headers, etc. */
201 if (rc == 0 && stp->timing_format == REPLAY_TIMING_MULTI) {
202 struct replay_log *log = replay_new_log(stp, "SH",
203 filename, stp->timing_fp);
204 if (!log)
205 rc = -ENOMEM;
206 else {
207 log->noseek = 1;
208 DBG(LOG, ul_debug("accociate log file '%s' with 'SH'", filename));
209 }
210 }
211
41ce6545 212 DBG(TIMING, ul_debug("timing file set to '%s' [rc=%d]", filename, rc));
fec06a06
KZ
213 return rc;
214}
215
7c4a374f 216
fec06a06
KZ
217static int replay_associate_log(struct replay_setup *stp,
218 const char *streams, const char *filename)
219{
7c4a374f 220 FILE *f;
fec06a06
KZ
221 int rc;
222
223 assert(stp);
224 assert(streams);
225 assert(filename);
226
fec06a06 227 /* open the file and skip the first line */
7c4a374f
KZ
228 f = fopen(filename, "r");
229 rc = f == NULL ? -errno : ignore_line(f);
230
231 if (rc == 0)
232 replay_new_log(stp, streams, filename, f);
fec06a06 233
41ce6545 234 DBG(LOG, ul_debug("accociate log file '%s' with '%s' [rc=%d]", filename, streams, rc));
fec06a06
KZ
235 return rc;
236}
237
238static int is_wanted_stream(char type, const char *streams)
239{
240 if (streams == NULL)
241 return 1;
242 if (strchr(streams, type))
243 return 1;
244 return 0;
245}
246
7c4a374f
KZ
247static void replay_reset_step(struct replay_step *step)
248{
249 assert(step);
250
251 step->size = 0;
252 step->delay = 0;
253 step->data = NULL;
254 step->type = 0;
255}
256
fec06a06
KZ
257static int read_multistream_step(struct replay_step *step, FILE *f, char type)
258{
259 int rc = 0;
260 char nl;
261
7c4a374f 262
fec06a06
KZ
263 switch (type) {
264 case 'O': /* output */
265 case 'I': /* input */
266 rc = fscanf(f, "%lf %zu%c\n", &step->delay, &step->size, &nl);
267 if (rc != 3 || nl != '\n')
268 rc = -EINVAL;
269 else
270 rc = 0;
271 break;
272
273 case 'S': /* signal */
fec06a06 274 case 'H': /* header */
7c4a374f
KZ
275 {
276 char buf[BUFSIZ];
277
278 rc = fscanf(f, "%lf ", &step->delay); /* delay */
279 if (rc != 1)
280 break;
281
282 rc = fscanf(f, "%s", buf); /* name */
283 if (rc != 1)
284 break;
285 step->name = strrealloc(step->name, buf);
286 if (!step->name)
287 err_oom();
288
289 if (!fgets(buf, sizeof(buf), f)) { /* value */
290 rc = -errno;
291 break;
292 }
293 if (*buf) {
294 strrem(buf, '\n');
295 step->value = strrealloc(step->value, buf);
296 if (!step->value)
297 err_oom();
298 }
299 rc = 0;
fec06a06 300 break;
7c4a374f 301 }
fec06a06
KZ
302 default:
303 break;
304 }
305
167a4851 306 DBG(TIMING, ul_debug(" read step delay & size [rc=%d]", rc));
fec06a06
KZ
307 return rc;
308}
309
167a4851
KZ
310static struct replay_log *replay_get_stream_log(struct replay_setup *stp, char stream)
311{
312 size_t i;
313
314 for (i = 0; i < stp->nlogs; i++) {
315 struct replay_log *log = &stp->logs[i];
316
317 if (is_wanted_stream(stream, log->streams))
318 return log;
319 }
320 return NULL;
321}
322
323static int replay_seek_log(struct replay_log *log, size_t move)
324{
7c4a374f
KZ
325 if (log->noseek)
326 return 0;
327
167a4851
KZ
328 DBG(LOG, ul_debug(" %s: seek ++ %zu", log->filename, move));
329 return fseek(log->fp, move, SEEK_CUR) == (off_t) -1 ? -errno : 0;
330}
331
fec06a06
KZ
332/* returns next step with pointer to the right log file for specified streams (e.g.
333 * "IOS" for in/out/signals) or all streams if stream is NULL.
334 *
335 * returns: 0 = success, <0 = error, 1 = done (EOF)
336 */
337static int replay_get_next_step(struct replay_setup *stp, char *streams, struct replay_step **xstep)
338{
339 struct replay_step *step;
340 int rc;
167a4851 341 double ignored_delay = 0;
fec06a06
KZ
342
343 assert(stp);
344 assert(stp->timing_fp);
4a4f4a62 345 assert(xstep);
fec06a06 346
fec06a06 347 step = &stp->step;
fec06a06
KZ
348 *xstep = NULL;
349
350 do {
167a4851 351 struct replay_log *log = NULL;
fec06a06
KZ
352
353 rc = 1; /* done */
354 if (feof(stp->timing_fp))
355 break;
167a4851
KZ
356
357 DBG(TIMING, ul_debug("reading next step"));
358
7c4a374f 359 replay_reset_step(step);
fec06a06
KZ
360 stp->timing_line++;
361
362 switch (stp->timing_format) {
363 case REPLAY_TIMING_SIMPLE:
41ce6545
KZ
364 /* old format is the same as new format, but without <type> prefix */
365 rc = read_multistream_step(step, stp->timing_fp, stp->default_type);
fec06a06 366 if (rc == 0)
41ce6545 367 step->type = stp->default_type;
fec06a06
KZ
368 break;
369 case REPLAY_TIMING_MULTI:
370 rc = fscanf(stp->timing_fp, "%c ", &step->type);
371 if (rc != 1)
372 rc = -EINVAL;
167a4851 373 else
fec06a06
KZ
374 rc = read_multistream_step(step,
375 stp->timing_fp,
376 step->type);
fec06a06
KZ
377 break;
378 }
379
f923a595
KZ
380 if (rc) {
381 if (rc < 0 && feof(stp->timing_fp))
382 rc = 1;
383 break; /* error or EOF */
384 }
fec06a06 385
167a4851 386 DBG(TIMING, ul_debug(" step entry is '%c'", step->type));
fec06a06 387
167a4851
KZ
388 log = replay_get_stream_log(stp, step->type);
389 if (log) {
390 if (is_wanted_stream(step->type, streams)) {
fec06a06
KZ
391 step->data = log;
392 *xstep = step;
167a4851
KZ
393 DBG(LOG, ul_debug(" use %s as data source", log->filename));
394 goto done;
fec06a06 395 }
167a4851
KZ
396 /* The step entry is unwanted, but we keep the right
397 * position in the log file although the data are ignored.
398 */
399 replay_seek_log(log, step->size);
400 } else
401 DBG(TIMING, ul_debug(" not found log for '%c' stream", step->type));
402
403 DBG(TIMING, ul_debug(" ignore step '%c' [delay=%f]",
404 step->type, step->delay));
405 ignored_delay += step->delay;
406 } while (rc == 0);
407
408done:
409 if (ignored_delay)
410 step->delay += ignored_delay;
411
412 DBG(TIMING, ul_debug("reading next step done [rc=%d delay=%f (ignored=%f) size=%zu]",
413 rc, step->delay, ignored_delay, step->size));
fec06a06
KZ
414 return rc;
415}
416
417/* return: 0 = success, <0 = error, 1 = done (EOF) */
88b8e9bf 418static int replay_emit_step_data(struct replay_setup *stp, struct replay_step *step, int fd)
fec06a06
KZ
419{
420 size_t ct;
88b8e9bf 421 int rc = 0, cr2nl = 0;
fec06a06
KZ
422 char buf[BUFSIZ];
423
88b8e9bf 424 assert(stp);
fec06a06 425 assert(step);
7c4a374f
KZ
426 switch (step->type) {
427 case 'S':
428 assert(step->name);
429 assert(step->value);
430 dprintf(fd, "%s %s\n", step->name, step->value);
431 DBG(LOG, ul_debug("log signal emited"));
432 return 0;
433 case 'H':
434 assert(step->name);
435 assert(step->value);
436 dprintf(fd, "%10s: %s\n", step->name, step->value);
4a4f4a62 437 DBG(LOG, ul_debug("log header emited"));
7c4a374f
KZ
438 return 0;
439 default:
440 break; /* continue with real data */
441 }
442
fec06a06
KZ
443 assert(step->size);
444 assert(step->data);
445 assert(step->data->fp);
446
88b8e9bf
KZ
447 switch (stp->crmode) {
448 case REPLAY_CRMODE_AUTO:
449 if (step->type == 'I')
450 cr2nl = 1;
451 break;
452 case REPLAY_CRMODE_NEVER:
453 cr2nl = 0;
454 break;
455 case REPLAY_CRMODE_ALWAYS:
456 cr2nl = 1;
457 break;
458 }
459
fec06a06
KZ
460 for (ct = step->size; ct > 0; ) {
461 size_t len, cc;
462
463 cc = ct > sizeof(buf) ? sizeof(buf): ct;
464 len = fread(buf, 1, cc, step->data->fp);
465
167a4851
KZ
466 if (!len) {
467 DBG(LOG, ul_debug("log data emit: failed to read log %m"));
fec06a06 468 break;
167a4851
KZ
469 }
470
88b8e9bf
KZ
471 if (cr2nl) {
472 size_t i;
473
474 for (i = 0; i < len; i++) {
475 if (buf[i] == 0x0D)
476 buf[i] = '\n';
477 }
478 }
479
fec06a06
KZ
480 ct -= len;
481 cc = write(fd, buf, len);
482 if (cc != len) {
483 rc = -errno;
167a4851 484 DBG(LOG, ul_debug("log data emit: failed write data %m"));
fec06a06
KZ
485 break;
486 }
487 }
488
489 if (ct && ferror(step->data->fp))
490 rc = -errno;
491 if (ct && feof(step->data->fp))
492 rc = 1;
493
167a4851 494 DBG(LOG, ul_debug("log data emited [rc=%d size=%zu]", rc, step->size));
fec06a06
KZ
495 return rc;
496}
497
5769a938 498static void __attribute__((__noreturn__))
86be6a32 499usage(void)
18a706bd 500{
86be6a32 501 FILE *out = stdout;
db433bf7 502 fputs(USAGE_HEADER, out);
0d955cd8
KZ
503 fprintf(out,
504 _(" %s [options]\n"),
505 program_invocation_short_name);
14cc9dda
KZ
506 fprintf(out,
507 _(" %s [-t] timingfile [typescript] [divisor]\n"),
508 program_invocation_short_name);
509
451dbcfa
BS
510 fputs(USAGE_SEPARATOR, out);
511 fputs(_("Play back terminal typescripts, using timing information.\n"), out);
512
db433bf7 513 fputs(USAGE_OPTIONS, out);
02a51ce1 514 fputs(_(" -t, --timing <file> script timing log file\n"), out);
0d955cd8
KZ
515 fputs(_(" -I, --log-in <file> script stdin log file\n"), out);
516 fputs(_(" -O, --log-out <file> script stdout log file (default)\n"), out);
517 fputs(_(" -B, --log-io <file> script stdin and stdout log file\n"), out);
518 fputs(_(" -s, --typescript <file> deprecated alist to -O\n"), out);
519
520 fputs(USAGE_SEPARATOR, out);
4a4f4a62 521 fputs(_(" --summary display overview about recorded session and exit\n"), out);
02a51ce1
KZ
522 fputs(_(" -d, --divisor <num> speed up or slow down execution with time divisor\n"), out);
523 fputs(_(" -m, --maxdelay <num> wait at most this many seconds between updates\n"), out);
7c4a374f 524 fputs(_(" -x, --stream <name> stream type (out, in, signal or info)\n"), out);
88b8e9bf 525 fputs(_(" -c, --cr-mode <type> CR char mode (auto, never, always)\n"), out);
f45f3ec3 526 printf(USAGE_HELP_OPTIONS(25));
84adb5e6 527
f45f3ec3 528 printf(USAGE_MAN_TAIL("scriptreplay(1)"));
86be6a32 529 exit(EXIT_SUCCESS);
18a706bd
KZ
530}
531
532static double
533getnum(const char *s)
534{
f0b3b904 535 const double d = strtod_or_err(s, _("failed to parse number"));
18a706bd 536
f0b3b904 537 if (isnan(d)) {
18a706bd 538 errno = EINVAL;
f0b3b904 539 err(EXIT_FAILURE, "%s: %s", _("failed to parse number"), s);
18a706bd
KZ
540 }
541 return d;
542}
543
544static void
545delay_for(double delay)
546{
547#ifdef HAVE_NANOSLEEP
548 struct timespec ts, remainder;
549 ts.tv_sec = (time_t) delay;
550 ts.tv_nsec = (delay - ts.tv_sec) * 1.0e9;
551
167a4851
KZ
552 DBG(TIMING, ul_debug("going to sleep for %fs", delay));
553
18a706bd
KZ
554 while (-1 == nanosleep(&ts, &remainder)) {
555 if (EINTR == errno)
556 ts = remainder;
557 else
558 break;
559 }
560#else
561 struct timeval tv;
562 tv.tv_sec = (long) delay;
563 tv.tv_usec = (delay - tv.tv_sec) * 1.0e6;
564 select(0, NULL, NULL, NULL, &tv);
565#endif
566}
567
41ce6545
KZ
568static void appendchr(char *buf, size_t bufsz, int c)
569{
570 size_t sz;
571
572 if (strchr(buf, c))
573 return; /* already in */
574
575 sz = strlen(buf);
576 if (sz + 1 < bufsz)
577 buf[sz] = c;
578}
579
18a706bd
KZ
580int
581main(int argc, char *argv[])
582{
fec06a06 583 struct replay_setup setup = { .nlogs = 0 };
4a4f4a62 584 struct replay_step *step = NULL;
41ce6545 585 char streams[6] = {0}; /* IOSI - in, out, signal,info */
0d955cd8
KZ
586 const char *log_out = NULL,
587 *log_in = NULL,
588 *log_io = NULL,
589 *log_tm = NULL;
7f1d4836 590 double divi = 1, maxdelay = 0;
fec06a06 591 int diviopt = FALSE, maxdelayopt = FALSE, idx;
4a4f4a62
KZ
592 int ch, rc, crmode = REPLAY_CRMODE_AUTO, summary = 0;
593 enum {
594 OPT_SUMMARY = CHAR_MAX + 1
595 };
84adb5e6
SK
596
597 static const struct option longopts[] = {
88b8e9bf 598 { "cr-mode", required_argument, 0, 'c' },
0da871b5 599 { "timing", required_argument, 0, 't' },
0d955cd8
KZ
600 { "log-in", required_argument, 0, 'I'},
601 { "log-out", required_argument, 0, 'O'},
602 { "log-io", required_argument, 0, 'B'},
0da871b5
SK
603 { "typescript", required_argument, 0, 's' },
604 { "divisor", required_argument, 0, 'd' },
7f1d4836 605 { "maxdelay", required_argument, 0, 'm' },
41ce6545 606 { "stream", required_argument, 0, 'x' },
4a4f4a62 607 { "summary", no_argument, 0, OPT_SUMMARY },
84adb5e6
SK
608 { "version", no_argument, 0, 'V' },
609 { "help", no_argument, 0, 'h' },
610 { NULL, 0, 0, 0 }
611 };
0d955cd8
KZ
612 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
613 { 'O', 's' },
614 { 0 }
615 };
616 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
18a706bd
KZ
617 /* Because we use space as a separator, we can't afford to use any
618 * locale which tolerates a space in a number. In any case, script.c
619 * sets the LC_NUMERIC locale to C, anyway.
620 */
621 setlocale(LC_ALL, "");
622 setlocale(LC_NUMERIC, "C");
623
624 bindtextdomain(PACKAGE, LOCALEDIR);
625 textdomain(PACKAGE);
2c308875 626 close_stdout_atexit();
18a706bd 627
fec06a06
KZ
628 scriptreplay_init_debug();
629
88b8e9bf 630 while ((ch = getopt_long(argc, argv, "B:c:I:O:t:s:d:m:x:Vh", longopts, NULL)) != -1) {
0d955cd8
KZ
631
632 err_exclusive_options(ch, longopts, excl, excl_st);
633
84adb5e6 634 switch(ch) {
88b8e9bf
KZ
635 case 'c':
636 if (strcmp("auto", optarg) == 0)
637 crmode = REPLAY_CRMODE_AUTO;
638 else if (strcmp("never", optarg) == 0)
639 crmode = REPLAY_CRMODE_NEVER;
640 else if (strcmp("always", optarg) == 0)
641 crmode = REPLAY_CRMODE_ALWAYS;
642 else
643 errx(EXIT_FAILURE, _("unsupported mode name: '%s'"), optarg);
644 break;
0da871b5 645 case 't':
0d955cd8 646 log_tm = optarg;
0da871b5 647 break;
0d955cd8 648 case 'O':
0da871b5 649 case 's':
0d955cd8
KZ
650 log_out = optarg;
651 break;
652 case 'I':
653 log_in = optarg;
654 break;
655 case 'B':
656 log_io = optarg;
0da871b5
SK
657 break;
658 case 'd':
659 diviopt = TRUE;
660 divi = getnum(optarg);
661 break;
7f1d4836
JDN
662 case 'm':
663 maxdelayopt = TRUE;
664 maxdelay = getnum(optarg);
665 break;
41ce6545
KZ
666 case 'x':
667 if (strcmp("in", optarg) == 0)
668 appendchr(streams, sizeof(streams), 'I');
669 else if (strcmp("out", optarg) == 0)
670 appendchr(streams, sizeof(streams), 'O');
671 else if (strcmp("signal", optarg) == 0)
672 appendchr(streams, sizeof(streams), 'S');
7c4a374f
KZ
673 else if (strcmp("info", optarg) == 0)
674 appendchr(streams, sizeof(streams), 'H');
41ce6545
KZ
675 else
676 errx(EXIT_FAILURE, _("unsupported stream name: '%s'"), optarg);
677 break;
4a4f4a62
KZ
678 case OPT_SUMMARY:
679 summary = 1;
680 break;
84adb5e6 681 case 'V':
2c308875 682 print_version(EXIT_SUCCESS);
84adb5e6 683 case 'h':
86be6a32 684 usage();
84adb5e6 685 default:
677ec86c 686 errtryhelp(EXIT_FAILURE);
0d955cd8
KZ
687 }
688 }
84adb5e6
SK
689 argc -= optind;
690 argv += optind;
0da871b5 691 idx = 0;
84adb5e6 692
4a4f4a62
KZ
693 if (summary)
694 streams[0] = 'H', streams[1] = '\0';
695
7c4a374f 696 if (!log_tm && idx < argc)
0d955cd8
KZ
697 log_tm = argv[idx++];
698 if (!log_out && !log_in && !log_io)
699 log_out = idx < argc ? argv[idx++] : "typescript";
700
0da871b5
SK
701 if (!diviopt)
702 divi = idx < argc ? getnum(argv[idx]) : 1;
7f1d4836
JDN
703 if (maxdelay < 0)
704 maxdelay = 0;
18a706bd 705
6646f9f8
KZ
706 if (!log_tm)
707 errx(EXIT_FAILURE, _("timing file not specified"));
7c4a374f
KZ
708 if (!(log_out || log_in || log_io))
709 errx(EXIT_FAILURE, _("data log file not specified"));
710
711 if (replay_set_timing_file(&setup, log_tm) != 0)
0d955cd8
KZ
712 err(EXIT_FAILURE, _("cannot open %s"), log_tm);
713
714 if (log_out && replay_associate_log(&setup, "O", log_out) != 0)
715 err(EXIT_FAILURE, _("cannot open %s"), log_out);
716
717 if (log_in && replay_associate_log(&setup, "I", log_in) != 0)
718 err(EXIT_FAILURE, _("cannot open %s"), log_in);
7f1d4836 719
0d955cd8
KZ
720 if (log_io && replay_associate_log(&setup, "IO", log_io) != 0)
721 err(EXIT_FAILURE, _("cannot open %s"), log_io);
18a706bd 722
41ce6545
KZ
723 if (!*streams) {
724 /* output is prefered default */
725 if (log_out || log_io)
726 appendchr(streams, sizeof(streams), 'O');
727 else if (log_in)
728 appendchr(streams, sizeof(streams), 'I');
729 }
730
731 replay_set_default_type(&setup,
732 *streams && streams[1] == '\0' ? *streams : 'O');
88b8e9bf 733 replay_set_crmode(&setup, crmode);
41ce6545 734
fec06a06 735 do {
41ce6545 736 rc = replay_get_next_step(&setup, streams, &step);
fec06a06
KZ
737 if (rc)
738 break;
18a706bd 739
4a4f4a62
KZ
740 if (!summary) {
741 step->delay /= divi;
742 if (maxdelayopt && step->delay > maxdelay)
743 step->delay = maxdelay;
744 if (step->delay > SCRIPT_MIN_DELAY)
745 delay_for(step->delay);
746 }
fec06a06 747
88b8e9bf 748 rc = replay_emit_step_data(&setup, step, STDOUT_FILENO);
fec06a06
KZ
749 } while (rc == 0);
750
751 if (step && rc < 0)
752 err(EXIT_FAILURE, _("%s: log file error"), step->data->filename);
753 else if (rc < 0)
754 err(EXIT_FAILURE, _("%s: line %d: timing file error"),
755 setup.timing_filename,
756 setup.timing_line);
f004a02d 757 printf("\n");
18a706bd
KZ
758 exit(EXIT_SUCCESS);
759}