]>
Commit | Line | Data |
---|---|---|
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" |
fec06a06 KZ |
38 | |
39 | static UL_DEBUG_DEFINE_MASK(scriptreplay); | |
40 | UL_DEBUG_DEFINE_MASKNAMES(scriptreplay) = UL_DEBUG_EMPTY_MASKNAMES; | |
41 | ||
42 | #define SCRIPTREPLAY_DEBUG_INIT (1 << 1) | |
43 | #define SCRIPTREPLAY_DEBUG_TIMING (1 << 2) | |
44 | #define SCRIPTREPLAY_DEBUG_LOG (1 << 3) | |
45 | #define SCRIPTREPLAY_DEBUG_MISC (1 << 4) | |
46 | #define SCRIPTREPLAY_DEBUG_ALL 0xFFFF | |
47 | ||
48 | #define DBG(m, x) __UL_DBG(scriptreplay, SCRIPTREPLAY_DEBUG_, m, x) | |
49 | #define ON_DBG(m, x) __UL_DBG_CALL(scriptreplay, SCRIPTREPLAY_DEBUG_, m, x) | |
18a706bd KZ |
50 | |
51 | #define SCRIPT_MIN_DELAY 0.0001 /* from original sripreplay.pl */ | |
52 | ||
fec06a06 KZ |
53 | /* |
54 | * The script replay is driven by timing file where each entry describes one | |
55 | * step in the replay. The timing step may refer input or output (or | |
56 | * signal, extra informations, etc.) | |
57 | * | |
58 | * The step data are stored in log files, the right log file for the step is | |
59 | * selected from replay_setup. | |
60 | * | |
61 | * TODO: move struct replay_{log,step,setup} to script-playutils.c to make it | |
62 | * usable for scriptlive(1) code. | |
63 | */ | |
64 | ||
65 | enum { | |
66 | REPLAY_TIMING_SIMPLE, /* timing info in classic "<delta> <offset>" format */ | |
67 | REPLAY_TIMING_MULTI /* multiple streams in format "<type> <delta> <offset|etc> */ | |
68 | }; | |
69 | ||
70 | struct replay_log { | |
71 | const char *streams; /* 'I'nput, 'O'utput or both */ | |
72 | const char *filename; | |
73 | FILE *fp; | |
74 | }; | |
75 | ||
76 | struct replay_step { | |
77 | char type; /* 'I'nput, 'O'utput, ... */ | |
78 | double delay; | |
79 | size_t size; | |
80 | ||
81 | struct replay_log *data; | |
82 | }; | |
83 | ||
84 | struct replay_setup { | |
85 | struct replay_log *logs; | |
86 | size_t nlogs; | |
87 | ||
88 | struct replay_step step; /* current step */ | |
89 | ||
90 | FILE *timing_fp; | |
91 | const char *timing_filename; | |
92 | int timing_format; | |
93 | int timing_line; | |
94 | }; | |
95 | ||
96 | static void scriptreplay_init_debug(void) | |
97 | { | |
98 | __UL_INIT_DEBUG_FROM_ENV(scriptreplay, SCRIPTREPLAY_DEBUG_, 0, SCRIPTREPLAY_DEBUG); | |
99 | } | |
100 | ||
101 | static int ignore_line(FILE *f) | |
102 | { | |
103 | int c; | |
104 | ||
105 | while((c = fgetc(f)) != EOF && c != '\n'); | |
106 | if (ferror(f)) | |
107 | return -errno; | |
108 | ||
109 | DBG(LOG, ul_debug(" ignore line")); | |
110 | return 0; | |
111 | } | |
112 | ||
113 | static int replay_set_timing_file(struct replay_setup *stp, const char *filename) | |
114 | { | |
115 | int c, rc = 0; | |
116 | ||
117 | assert(stp); | |
118 | assert(filename); | |
119 | ||
120 | stp->timing_filename = filename; | |
121 | stp->timing_line = 0; | |
122 | ||
123 | stp->timing_fp = fopen(filename, "r"); | |
124 | if (!stp->timing_fp) | |
125 | rc = -errno; | |
126 | else { | |
127 | /* detect timing file format */ | |
128 | c = fgetc(stp->timing_fp); | |
129 | if (c != EOF) { | |
130 | if (isdigit((unsigned int) c)) | |
131 | stp->timing_format = REPLAY_TIMING_SIMPLE; | |
132 | else | |
133 | stp->timing_format = REPLAY_TIMING_MULTI; | |
134 | ungetc(c, stp->timing_fp); | |
135 | } else if (ferror(stp->timing_fp)) | |
136 | rc = -errno; | |
137 | } | |
138 | ||
139 | if (rc) { | |
140 | fclose(stp->timing_fp); | |
141 | stp->timing_fp = NULL; | |
142 | } | |
143 | ||
144 | DBG(TIMING, ul_debug("timing file set to %s [rc=%d]", filename, rc)); | |
145 | return rc; | |
146 | } | |
147 | ||
148 | static int replay_associate_log(struct replay_setup *stp, | |
149 | const char *streams, const char *filename) | |
150 | { | |
151 | struct replay_log *log; | |
152 | int rc; | |
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 | ||
165 | /* open the file and skip the first line */ | |
166 | log->fp = fopen(filename, "r"); | |
167 | rc = log->fp == NULL ? -errno : ignore_line(log->fp); | |
168 | ||
169 | DBG(LOG, ul_debug("accociate log file %s with '%s' [rc=%d]", filename, streams, rc)); | |
170 | return rc; | |
171 | } | |
172 | ||
173 | static int is_wanted_stream(char type, const char *streams) | |
174 | { | |
175 | if (streams == NULL) | |
176 | return 1; | |
177 | if (strchr(streams, type)) | |
178 | return 1; | |
179 | return 0; | |
180 | } | |
181 | ||
182 | static int read_multistream_step(struct replay_step *step, FILE *f, char type) | |
183 | { | |
184 | int rc = 0; | |
185 | char nl; | |
186 | ||
187 | switch (type) { | |
188 | case 'O': /* output */ | |
189 | case 'I': /* input */ | |
190 | rc = fscanf(f, "%lf %zu%c\n", &step->delay, &step->size, &nl); | |
191 | if (rc != 3 || nl != '\n') | |
192 | rc = -EINVAL; | |
193 | else | |
194 | rc = 0; | |
195 | break; | |
196 | ||
197 | case 'S': /* signal */ | |
198 | rc = ignore_line(f); /* not implemnted yet */ | |
199 | break; | |
200 | ||
201 | case 'H': /* header */ | |
202 | rc = ignore_line(f); /* not implemnted yet */ | |
203 | break; | |
204 | default: | |
205 | break; | |
206 | } | |
207 | ||
208 | DBG(TIMING, ul_debug(" read step delay & size [rc=%d]", rc)); | |
209 | return rc; | |
210 | } | |
211 | ||
212 | /* returns next step with pointer to the right log file for specified streams (e.g. | |
213 | * "IOS" for in/out/signals) or all streams if stream is NULL. | |
214 | * | |
215 | * returns: 0 = success, <0 = error, 1 = done (EOF) | |
216 | */ | |
217 | static int replay_get_next_step(struct replay_setup *stp, char *streams, struct replay_step **xstep) | |
218 | { | |
219 | struct replay_step *step; | |
220 | int rc; | |
221 | ||
222 | assert(stp); | |
223 | assert(stp->timing_fp); | |
224 | assert(xstep && *xstep); | |
225 | ||
226 | /* old format supports only 'O'utput */ | |
227 | if (stp->timing_format == REPLAY_TIMING_SIMPLE && | |
228 | !is_wanted_stream('O', streams)) | |
229 | return 1; | |
230 | ||
231 | DBG(TIMING, ul_debug("reading next step")); | |
232 | ||
233 | step = &stp->step; | |
234 | memset(step, 0, sizeof(*step)); | |
235 | *xstep = NULL; | |
236 | ||
237 | do { | |
238 | size_t i; | |
239 | ||
240 | rc = 1; /* done */ | |
241 | if (feof(stp->timing_fp)) | |
242 | break; | |
243 | stp->timing_line++; | |
244 | ||
245 | switch (stp->timing_format) { | |
246 | case REPLAY_TIMING_SIMPLE: | |
247 | /* old format supports only output entries and format is the same | |
248 | * as new format, but without <type> prefix */ | |
249 | rc = read_multistream_step(step, stp->timing_fp, 'O'); | |
250 | if (rc == 0) | |
251 | step->type = 'O'; /* 'O'utput */ | |
252 | break; | |
253 | case REPLAY_TIMING_MULTI: | |
254 | rc = fscanf(stp->timing_fp, "%c ", &step->type); | |
255 | if (rc != 1) | |
256 | rc = -EINVAL; | |
257 | else if (is_wanted_stream(step->type, streams)) | |
258 | rc = read_multistream_step(step, | |
259 | stp->timing_fp, | |
260 | step->type); | |
261 | else { | |
262 | rc = ignore_line(stp->timing_fp); | |
263 | step->type = 0; | |
264 | } | |
265 | break; | |
266 | } | |
267 | ||
268 | if (rc) | |
269 | break;; /* error */ | |
270 | if (!step->type) | |
271 | continue; /* ignore this entry */ | |
272 | ||
273 | /* points step to the right log */ | |
274 | for (i = 0; i < stp->nlogs; i++) { | |
275 | struct replay_log *log = &stp->logs[i]; | |
276 | ||
277 | assert(log->streams); | |
278 | ||
279 | if (is_wanted_stream(step->type, log->streams)) { | |
280 | step->data = log; | |
281 | *xstep = step; | |
282 | break; | |
283 | } | |
284 | } | |
285 | } while (rc == 0 && *xstep == NULL); | |
286 | ||
287 | DBG(TIMING, ul_debug(" reading next step done [rc=%d delay=%g size=%zu]", | |
288 | rc, step->delay, step->size)); | |
289 | return rc; | |
290 | } | |
291 | ||
292 | /* return: 0 = success, <0 = error, 1 = done (EOF) */ | |
293 | static int replay_emit_step_data(struct replay_step *step, int fd) | |
294 | { | |
295 | size_t ct; | |
296 | int rc = 0; | |
297 | char buf[BUFSIZ]; | |
298 | ||
299 | assert(step); | |
300 | assert(step->size); | |
301 | assert(step->data); | |
302 | assert(step->data->fp); | |
303 | ||
304 | for (ct = step->size; ct > 0; ) { | |
305 | size_t len, cc; | |
306 | ||
307 | cc = ct > sizeof(buf) ? sizeof(buf): ct; | |
308 | len = fread(buf, 1, cc, step->data->fp); | |
309 | ||
310 | if (!len) | |
311 | break; | |
312 | ct -= len; | |
313 | cc = write(fd, buf, len); | |
314 | if (cc != len) { | |
315 | rc = -errno; | |
316 | break; | |
317 | } | |
318 | } | |
319 | ||
320 | if (ct && ferror(step->data->fp)) | |
321 | rc = -errno; | |
322 | if (ct && feof(step->data->fp)) | |
323 | rc = 1; | |
324 | ||
325 | DBG(LOG, ul_debug(" log data emited [rc=%d]", rc)); | |
326 | return rc; | |
327 | } | |
328 | ||
5769a938 | 329 | static void __attribute__((__noreturn__)) |
86be6a32 | 330 | usage(void) |
18a706bd | 331 | { |
86be6a32 | 332 | FILE *out = stdout; |
db433bf7 | 333 | fputs(USAGE_HEADER, out); |
14cc9dda KZ |
334 | fprintf(out, |
335 | _(" %s [-t] timingfile [typescript] [divisor]\n"), | |
336 | program_invocation_short_name); | |
337 | ||
451dbcfa BS |
338 | fputs(USAGE_SEPARATOR, out); |
339 | fputs(_("Play back terminal typescripts, using timing information.\n"), out); | |
340 | ||
db433bf7 | 341 | fputs(USAGE_OPTIONS, out); |
14cc9dda KZ |
342 | fputs(_(" -t, --timing <file> script timing output file\n" |
343 | " -s, --typescript <file> script terminal session output file\n" | |
344 | " -d, --divisor <num> speed up or slow down execution with time divisor\n" | |
7f1d4836 | 345 | " -m, --maxdelay <num> wait at most this many seconds between updates\n" |
b3054454 | 346 | ), out); |
f45f3ec3 | 347 | printf(USAGE_HELP_OPTIONS(25)); |
84adb5e6 | 348 | |
f45f3ec3 | 349 | printf(USAGE_MAN_TAIL("scriptreplay(1)")); |
86be6a32 | 350 | exit(EXIT_SUCCESS); |
18a706bd KZ |
351 | } |
352 | ||
353 | static double | |
354 | getnum(const char *s) | |
355 | { | |
f0b3b904 | 356 | const double d = strtod_or_err(s, _("failed to parse number")); |
18a706bd | 357 | |
f0b3b904 | 358 | if (isnan(d)) { |
18a706bd | 359 | errno = EINVAL; |
f0b3b904 | 360 | err(EXIT_FAILURE, "%s: %s", _("failed to parse number"), s); |
18a706bd KZ |
361 | } |
362 | return d; | |
363 | } | |
364 | ||
365 | static void | |
366 | delay_for(double delay) | |
367 | { | |
368 | #ifdef HAVE_NANOSLEEP | |
369 | struct timespec ts, remainder; | |
370 | ts.tv_sec = (time_t) delay; | |
371 | ts.tv_nsec = (delay - ts.tv_sec) * 1.0e9; | |
372 | ||
373 | while (-1 == nanosleep(&ts, &remainder)) { | |
374 | if (EINTR == errno) | |
375 | ts = remainder; | |
376 | else | |
377 | break; | |
378 | } | |
379 | #else | |
380 | struct timeval tv; | |
381 | tv.tv_sec = (long) delay; | |
382 | tv.tv_usec = (delay - tv.tv_sec) * 1.0e6; | |
383 | select(0, NULL, NULL, NULL, &tv); | |
384 | #endif | |
385 | } | |
386 | ||
18a706bd KZ |
387 | int |
388 | main(int argc, char *argv[]) | |
389 | { | |
fec06a06 KZ |
390 | struct replay_setup setup = { .nlogs = 0 }; |
391 | struct replay_step *step; | |
392 | int rc; | |
393 | ||
0da871b5 | 394 | const char *sname = NULL, *tname = NULL; |
7f1d4836 | 395 | double divi = 1, maxdelay = 0; |
fec06a06 | 396 | int diviopt = FALSE, maxdelayopt = FALSE, idx; |
94757ece | 397 | int ch; |
84adb5e6 SK |
398 | |
399 | static const struct option longopts[] = { | |
0da871b5 SK |
400 | { "timing", required_argument, 0, 't' }, |
401 | { "typescript", required_argument, 0, 's' }, | |
402 | { "divisor", required_argument, 0, 'd' }, | |
7f1d4836 | 403 | { "maxdelay", required_argument, 0, 'm' }, |
84adb5e6 SK |
404 | { "version", no_argument, 0, 'V' }, |
405 | { "help", no_argument, 0, 'h' }, | |
406 | { NULL, 0, 0, 0 } | |
407 | }; | |
18a706bd KZ |
408 | |
409 | /* Because we use space as a separator, we can't afford to use any | |
410 | * locale which tolerates a space in a number. In any case, script.c | |
411 | * sets the LC_NUMERIC locale to C, anyway. | |
412 | */ | |
413 | setlocale(LC_ALL, ""); | |
414 | setlocale(LC_NUMERIC, "C"); | |
415 | ||
416 | bindtextdomain(PACKAGE, LOCALEDIR); | |
417 | textdomain(PACKAGE); | |
2c308875 | 418 | close_stdout_atexit(); |
18a706bd | 419 | |
fec06a06 KZ |
420 | scriptreplay_init_debug(); |
421 | ||
7f1d4836 | 422 | while ((ch = getopt_long(argc, argv, "t:s:d:m:Vh", longopts, NULL)) != -1) |
84adb5e6 | 423 | switch(ch) { |
0da871b5 SK |
424 | case 't': |
425 | tname = optarg; | |
426 | break; | |
427 | case 's': | |
428 | sname = optarg; | |
429 | break; | |
430 | case 'd': | |
431 | diviopt = TRUE; | |
432 | divi = getnum(optarg); | |
433 | break; | |
7f1d4836 JDN |
434 | case 'm': |
435 | maxdelayopt = TRUE; | |
436 | maxdelay = getnum(optarg); | |
437 | break; | |
2c308875 | 438 | |
84adb5e6 | 439 | case 'V': |
2c308875 | 440 | print_version(EXIT_SUCCESS); |
84adb5e6 | 441 | case 'h': |
86be6a32 | 442 | usage(); |
84adb5e6 | 443 | default: |
677ec86c | 444 | errtryhelp(EXIT_FAILURE); |
84adb5e6 SK |
445 | } |
446 | argc -= optind; | |
447 | argv += optind; | |
0da871b5 | 448 | idx = 0; |
84adb5e6 | 449 | |
0da871b5 | 450 | if ((argc < 1 && !tname) || argc > 3) { |
84adb5e6 | 451 | warnx(_("wrong number of arguments")); |
677ec86c | 452 | errtryhelp(EXIT_FAILURE); |
84adb5e6 | 453 | } |
0da871b5 SK |
454 | if (!tname) |
455 | tname = argv[idx++]; | |
456 | if (!sname) | |
457 | sname = idx < argc ? argv[idx++] : "typescript"; | |
458 | if (!diviopt) | |
459 | divi = idx < argc ? getnum(argv[idx]) : 1; | |
7f1d4836 JDN |
460 | if (maxdelay < 0) |
461 | maxdelay = 0; | |
18a706bd | 462 | |
fec06a06 KZ |
463 | if (replay_set_timing_file(&setup, tname) != 0) |
464 | err(EXIT_FAILURE, _("cannot open %s"), tname); | |
7f1d4836 | 465 | |
fec06a06 KZ |
466 | if (replay_associate_log(&setup, "O", sname) != 0) |
467 | err(EXIT_FAILURE, _("cannot open %s"), sname); | |
18a706bd | 468 | |
fec06a06 KZ |
469 | do { |
470 | rc = replay_get_next_step(&setup, "O", &step); | |
471 | if (rc) | |
472 | break; | |
18a706bd | 473 | |
fec06a06 KZ |
474 | step->delay /= divi; |
475 | if (maxdelayopt && step->delay > maxdelay) | |
476 | step->delay = maxdelay; | |
477 | if (step->delay > SCRIPT_MIN_DELAY) | |
478 | delay_for(step->delay); | |
479 | ||
480 | rc = replay_emit_step_data(step, STDOUT_FILENO); | |
481 | } while (rc == 0); | |
482 | ||
483 | if (step && rc < 0) | |
484 | err(EXIT_FAILURE, _("%s: log file error"), step->data->filename); | |
485 | else if (rc < 0) | |
486 | err(EXIT_FAILURE, _("%s: line %d: timing file error"), | |
487 | setup.timing_filename, | |
488 | setup.timing_line); | |
f004a02d | 489 | printf("\n"); |
18a706bd KZ |
490 | exit(EXIT_SUCCESS); |
491 | } |