]> git.ipfire.org Git - thirdparty/util-linux.git/blame - term-utils/scriptreplay.c
scriptreplay: add --log-{in,out,io} options
[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
71struct replay_log {
72 const char *streams; /* 'I'nput, 'O'utput or both */
73 const char *filename;
74 FILE *fp;
75};
76
77struct replay_step {
78 char type; /* 'I'nput, 'O'utput, ... */
79 double delay;
80 size_t size;
81
82 struct replay_log *data;
83};
84
85struct replay_setup {
86 struct replay_log *logs;
87 size_t nlogs;
88
89 struct replay_step step; /* current step */
90
91 FILE *timing_fp;
92 const char *timing_filename;
93 int timing_format;
94 int timing_line;
95};
96
97static void scriptreplay_init_debug(void)
98{
99 __UL_INIT_DEBUG_FROM_ENV(scriptreplay, SCRIPTREPLAY_DEBUG_, 0, SCRIPTREPLAY_DEBUG);
100}
101
102static int ignore_line(FILE *f)
103{
104 int c;
105
106 while((c = fgetc(f)) != EOF && c != '\n');
107 if (ferror(f))
108 return -errno;
109
110 DBG(LOG, ul_debug(" ignore line"));
111 return 0;
112}
113
114static int replay_set_timing_file(struct replay_setup *stp, const char *filename)
115{
116 int c, rc = 0;
117
118 assert(stp);
119 assert(filename);
120
121 stp->timing_filename = filename;
122 stp->timing_line = 0;
123
124 stp->timing_fp = fopen(filename, "r");
125 if (!stp->timing_fp)
126 rc = -errno;
127 else {
128 /* detect timing file format */
129 c = fgetc(stp->timing_fp);
130 if (c != EOF) {
131 if (isdigit((unsigned int) c))
132 stp->timing_format = REPLAY_TIMING_SIMPLE;
133 else
134 stp->timing_format = REPLAY_TIMING_MULTI;
135 ungetc(c, stp->timing_fp);
136 } else if (ferror(stp->timing_fp))
137 rc = -errno;
138 }
139
140 if (rc) {
141 fclose(stp->timing_fp);
142 stp->timing_fp = NULL;
143 }
144
145 DBG(TIMING, ul_debug("timing file set to %s [rc=%d]", filename, rc));
146 return rc;
147}
148
149static int replay_associate_log(struct replay_setup *stp,
150 const char *streams, const char *filename)
151{
152 struct replay_log *log;
153 int rc;
154
155 assert(stp);
156 assert(streams);
157 assert(filename);
158
159 stp->logs = xrealloc(stp->logs, (stp->nlogs + 1) * sizeof(*log));
160 log = &stp->logs[stp->nlogs];
161 stp->nlogs++;
162
163 log->filename = filename;
164 log->streams = streams;
165
166 /* open the file and skip the first line */
167 log->fp = fopen(filename, "r");
168 rc = log->fp == NULL ? -errno : ignore_line(log->fp);
169
170 DBG(LOG, ul_debug("accociate log file %s with '%s' [rc=%d]", filename, streams, rc));
171 return rc;
172}
173
174static int is_wanted_stream(char type, const char *streams)
175{
176 if (streams == NULL)
177 return 1;
178 if (strchr(streams, type))
179 return 1;
180 return 0;
181}
182
183static int read_multistream_step(struct replay_step *step, FILE *f, char type)
184{
185 int rc = 0;
186 char nl;
187
188 switch (type) {
189 case 'O': /* output */
190 case 'I': /* input */
191 rc = fscanf(f, "%lf %zu%c\n", &step->delay, &step->size, &nl);
192 if (rc != 3 || nl != '\n')
193 rc = -EINVAL;
194 else
195 rc = 0;
196 break;
197
198 case 'S': /* signal */
199 rc = ignore_line(f); /* not implemnted yet */
200 break;
201
202 case 'H': /* header */
203 rc = ignore_line(f); /* not implemnted yet */
204 break;
205 default:
206 break;
207 }
208
167a4851 209 DBG(TIMING, ul_debug(" read step delay & size [rc=%d]", rc));
fec06a06
KZ
210 return rc;
211}
212
167a4851
KZ
213static struct replay_log *replay_get_stream_log(struct replay_setup *stp, char stream)
214{
215 size_t i;
216
217 for (i = 0; i < stp->nlogs; i++) {
218 struct replay_log *log = &stp->logs[i];
219
220 if (is_wanted_stream(stream, log->streams))
221 return log;
222 }
223 return NULL;
224}
225
226static int replay_seek_log(struct replay_log *log, size_t move)
227{
228 DBG(LOG, ul_debug(" %s: seek ++ %zu", log->filename, move));
229 return fseek(log->fp, move, SEEK_CUR) == (off_t) -1 ? -errno : 0;
230}
231
fec06a06
KZ
232/* returns next step with pointer to the right log file for specified streams (e.g.
233 * "IOS" for in/out/signals) or all streams if stream is NULL.
234 *
235 * returns: 0 = success, <0 = error, 1 = done (EOF)
236 */
237static int replay_get_next_step(struct replay_setup *stp, char *streams, struct replay_step **xstep)
238{
239 struct replay_step *step;
240 int rc;
167a4851 241 double ignored_delay = 0;
fec06a06
KZ
242
243 assert(stp);
244 assert(stp->timing_fp);
245 assert(xstep && *xstep);
246
247 /* old format supports only 'O'utput */
248 if (stp->timing_format == REPLAY_TIMING_SIMPLE &&
249 !is_wanted_stream('O', streams))
250 return 1;
251
fec06a06
KZ
252
253 step = &stp->step;
fec06a06
KZ
254 *xstep = NULL;
255
256 do {
167a4851 257 struct replay_log *log = NULL;
fec06a06
KZ
258
259 rc = 1; /* done */
260 if (feof(stp->timing_fp))
261 break;
167a4851
KZ
262
263 DBG(TIMING, ul_debug("reading next step"));
264
265 memset(step, 0, sizeof(*step));
fec06a06
KZ
266 stp->timing_line++;
267
268 switch (stp->timing_format) {
269 case REPLAY_TIMING_SIMPLE:
270 /* old format supports only output entries and format is the same
271 * as new format, but without <type> prefix */
272 rc = read_multistream_step(step, stp->timing_fp, 'O');
273 if (rc == 0)
274 step->type = 'O'; /* 'O'utput */
275 break;
276 case REPLAY_TIMING_MULTI:
277 rc = fscanf(stp->timing_fp, "%c ", &step->type);
278 if (rc != 1)
279 rc = -EINVAL;
167a4851 280 else
fec06a06
KZ
281 rc = read_multistream_step(step,
282 stp->timing_fp,
283 step->type);
fec06a06
KZ
284 break;
285 }
286
287 if (rc)
288 break;; /* error */
fec06a06 289
167a4851 290 DBG(TIMING, ul_debug(" step entry is '%c'", step->type));
fec06a06 291
167a4851
KZ
292 log = replay_get_stream_log(stp, step->type);
293 if (log) {
294 if (is_wanted_stream(step->type, streams)) {
fec06a06
KZ
295 step->data = log;
296 *xstep = step;
167a4851
KZ
297 DBG(LOG, ul_debug(" use %s as data source", log->filename));
298 goto done;
fec06a06 299 }
167a4851
KZ
300 /* The step entry is unwanted, but we keep the right
301 * position in the log file although the data are ignored.
302 */
303 replay_seek_log(log, step->size);
304 } else
305 DBG(TIMING, ul_debug(" not found log for '%c' stream", step->type));
306
307 DBG(TIMING, ul_debug(" ignore step '%c' [delay=%f]",
308 step->type, step->delay));
309 ignored_delay += step->delay;
310 } while (rc == 0);
311
312done:
313 if (ignored_delay)
314 step->delay += ignored_delay;
315
316 DBG(TIMING, ul_debug("reading next step done [rc=%d delay=%f (ignored=%f) size=%zu]",
317 rc, step->delay, ignored_delay, step->size));
fec06a06
KZ
318 return rc;
319}
320
321/* return: 0 = success, <0 = error, 1 = done (EOF) */
322static int replay_emit_step_data(struct replay_step *step, int fd)
323{
324 size_t ct;
325 int rc = 0;
326 char buf[BUFSIZ];
327
328 assert(step);
329 assert(step->size);
330 assert(step->data);
331 assert(step->data->fp);
332
333 for (ct = step->size; ct > 0; ) {
334 size_t len, cc;
335
336 cc = ct > sizeof(buf) ? sizeof(buf): ct;
337 len = fread(buf, 1, cc, step->data->fp);
338
167a4851
KZ
339 if (!len) {
340 DBG(LOG, ul_debug("log data emit: failed to read log %m"));
fec06a06 341 break;
167a4851
KZ
342 }
343
fec06a06
KZ
344 ct -= len;
345 cc = write(fd, buf, len);
346 if (cc != len) {
347 rc = -errno;
167a4851 348 DBG(LOG, ul_debug("log data emit: failed write data %m"));
fec06a06
KZ
349 break;
350 }
351 }
352
353 if (ct && ferror(step->data->fp))
354 rc = -errno;
355 if (ct && feof(step->data->fp))
356 rc = 1;
357
167a4851 358 DBG(LOG, ul_debug("log data emited [rc=%d size=%zu]", rc, step->size));
fec06a06
KZ
359 return rc;
360}
361
5769a938 362static void __attribute__((__noreturn__))
86be6a32 363usage(void)
18a706bd 364{
86be6a32 365 FILE *out = stdout;
db433bf7 366 fputs(USAGE_HEADER, out);
0d955cd8
KZ
367 fprintf(out,
368 _(" %s [options]\n"),
369 program_invocation_short_name);
14cc9dda
KZ
370 fprintf(out,
371 _(" %s [-t] timingfile [typescript] [divisor]\n"),
372 program_invocation_short_name);
373
451dbcfa
BS
374 fputs(USAGE_SEPARATOR, out);
375 fputs(_("Play back terminal typescripts, using timing information.\n"), out);
376
db433bf7 377 fputs(USAGE_OPTIONS, out);
02a51ce1 378 fputs(_(" -t, --timing <file> script timing log file\n"), out);
0d955cd8
KZ
379 fputs(_(" -I, --log-in <file> script stdin log file\n"), out);
380 fputs(_(" -O, --log-out <file> script stdout log file (default)\n"), out);
381 fputs(_(" -B, --log-io <file> script stdin and stdout log file\n"), out);
382 fputs(_(" -s, --typescript <file> deprecated alist to -O\n"), out);
383
384 fputs(USAGE_SEPARATOR, out);
02a51ce1
KZ
385 fputs(_(" -d, --divisor <num> speed up or slow down execution with time divisor\n"), out);
386 fputs(_(" -m, --maxdelay <num> wait at most this many seconds between updates\n"), out);
f45f3ec3 387 printf(USAGE_HELP_OPTIONS(25));
84adb5e6 388
f45f3ec3 389 printf(USAGE_MAN_TAIL("scriptreplay(1)"));
86be6a32 390 exit(EXIT_SUCCESS);
18a706bd
KZ
391}
392
393static double
394getnum(const char *s)
395{
f0b3b904 396 const double d = strtod_or_err(s, _("failed to parse number"));
18a706bd 397
f0b3b904 398 if (isnan(d)) {
18a706bd 399 errno = EINVAL;
f0b3b904 400 err(EXIT_FAILURE, "%s: %s", _("failed to parse number"), s);
18a706bd
KZ
401 }
402 return d;
403}
404
405static void
406delay_for(double delay)
407{
408#ifdef HAVE_NANOSLEEP
409 struct timespec ts, remainder;
410 ts.tv_sec = (time_t) delay;
411 ts.tv_nsec = (delay - ts.tv_sec) * 1.0e9;
412
167a4851
KZ
413 DBG(TIMING, ul_debug("going to sleep for %fs", delay));
414
18a706bd
KZ
415 while (-1 == nanosleep(&ts, &remainder)) {
416 if (EINTR == errno)
417 ts = remainder;
418 else
419 break;
420 }
421#else
422 struct timeval tv;
423 tv.tv_sec = (long) delay;
424 tv.tv_usec = (delay - tv.tv_sec) * 1.0e6;
425 select(0, NULL, NULL, NULL, &tv);
426#endif
427}
428
18a706bd
KZ
429int
430main(int argc, char *argv[])
431{
fec06a06
KZ
432 struct replay_setup setup = { .nlogs = 0 };
433 struct replay_step *step;
0d955cd8
KZ
434 const char *log_out = NULL,
435 *log_in = NULL,
436 *log_io = NULL,
437 *log_tm = NULL;
7f1d4836 438 double divi = 1, maxdelay = 0;
fec06a06 439 int diviopt = FALSE, maxdelayopt = FALSE, idx;
0d955cd8 440 int ch, rc;
84adb5e6
SK
441
442 static const struct option longopts[] = {
0da871b5 443 { "timing", required_argument, 0, 't' },
0d955cd8
KZ
444 { "log-in", required_argument, 0, 'I'},
445 { "log-out", required_argument, 0, 'O'},
446 { "log-io", required_argument, 0, 'B'},
0da871b5
SK
447 { "typescript", required_argument, 0, 's' },
448 { "divisor", required_argument, 0, 'd' },
7f1d4836 449 { "maxdelay", required_argument, 0, 'm' },
84adb5e6
SK
450 { "version", no_argument, 0, 'V' },
451 { "help", no_argument, 0, 'h' },
452 { NULL, 0, 0, 0 }
453 };
0d955cd8
KZ
454 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
455 { 'O', 's' },
456 { 0 }
457 };
458 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
18a706bd
KZ
459 /* Because we use space as a separator, we can't afford to use any
460 * locale which tolerates a space in a number. In any case, script.c
461 * sets the LC_NUMERIC locale to C, anyway.
462 */
463 setlocale(LC_ALL, "");
464 setlocale(LC_NUMERIC, "C");
465
466 bindtextdomain(PACKAGE, LOCALEDIR);
467 textdomain(PACKAGE);
2c308875 468 close_stdout_atexit();
18a706bd 469
fec06a06
KZ
470 scriptreplay_init_debug();
471
0d955cd8
KZ
472 while ((ch = getopt_long(argc, argv, "B:I:O:t:s:d:m:Vh", longopts, NULL)) != -1) {
473
474 err_exclusive_options(ch, longopts, excl, excl_st);
475
84adb5e6 476 switch(ch) {
0da871b5 477 case 't':
0d955cd8 478 log_tm = optarg;
0da871b5 479 break;
0d955cd8 480 case 'O':
0da871b5 481 case 's':
0d955cd8
KZ
482 log_out = optarg;
483 break;
484 case 'I':
485 log_in = optarg;
486 break;
487 case 'B':
488 log_io = optarg;
0da871b5
SK
489 break;
490 case 'd':
491 diviopt = TRUE;
492 divi = getnum(optarg);
493 break;
7f1d4836
JDN
494 case 'm':
495 maxdelayopt = TRUE;
496 maxdelay = getnum(optarg);
497 break;
2c308875 498
84adb5e6 499 case 'V':
2c308875 500 print_version(EXIT_SUCCESS);
84adb5e6 501 case 'h':
86be6a32 502 usage();
84adb5e6 503 default:
677ec86c 504 errtryhelp(EXIT_FAILURE);
0d955cd8
KZ
505 }
506 }
84adb5e6
SK
507 argc -= optind;
508 argv += optind;
0da871b5 509 idx = 0;
84adb5e6 510
0d955cd8 511 if ((argc < 1 && !(log_out || log_in || log_io)) || argc > 3) {
84adb5e6 512 warnx(_("wrong number of arguments"));
677ec86c 513 errtryhelp(EXIT_FAILURE);
84adb5e6 514 }
0d955cd8
KZ
515 if (!log_tm)
516 log_tm = argv[idx++];
517 if (!log_out && !log_in && !log_io)
518 log_out = idx < argc ? argv[idx++] : "typescript";
519
0da871b5
SK
520 if (!diviopt)
521 divi = idx < argc ? getnum(argv[idx]) : 1;
7f1d4836
JDN
522 if (maxdelay < 0)
523 maxdelay = 0;
18a706bd 524
0d955cd8
KZ
525 if (replay_set_timing_file(&setup, log_tm) != 0)
526 err(EXIT_FAILURE, _("cannot open %s"), log_tm);
527
528 if (log_out && replay_associate_log(&setup, "O", log_out) != 0)
529 err(EXIT_FAILURE, _("cannot open %s"), log_out);
530
531 if (log_in && replay_associate_log(&setup, "I", log_in) != 0)
532 err(EXIT_FAILURE, _("cannot open %s"), log_in);
7f1d4836 533
0d955cd8
KZ
534 if (log_io && replay_associate_log(&setup, "IO", log_io) != 0)
535 err(EXIT_FAILURE, _("cannot open %s"), log_io);
18a706bd 536
fec06a06
KZ
537 do {
538 rc = replay_get_next_step(&setup, "O", &step);
539 if (rc)
540 break;
18a706bd 541
fec06a06
KZ
542 step->delay /= divi;
543 if (maxdelayopt && step->delay > maxdelay)
544 step->delay = maxdelay;
545 if (step->delay > SCRIPT_MIN_DELAY)
546 delay_for(step->delay);
547
548 rc = replay_emit_step_data(step, STDOUT_FILENO);
549 } while (rc == 0);
550
551 if (step && rc < 0)
552 err(EXIT_FAILURE, _("%s: log file error"), step->data->filename);
553 else if (rc < 0)
554 err(EXIT_FAILURE, _("%s: line %d: timing file error"),
555 setup.timing_filename,
556 setup.timing_line);
f004a02d 557 printf("\n");
18a706bd
KZ
558 exit(EXIT_SUCCESS);
559}