]>
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" |
0d955cd8 | 38 | #include "optutils.h" |
fec06a06 KZ |
39 | |
40 | static UL_DEBUG_DEFINE_MASK(scriptreplay); | |
41 | UL_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 | ||
66 | enum { | |
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 */ |
72 | enum { | |
73 | REPLAY_CRMODE_AUTO = 0, | |
74 | REPLAY_CRMODE_NEVER, | |
75 | REPLAY_CRMODE_ALWAYS | |
76 | }; | |
77 | ||
fec06a06 | 78 | struct 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 | ||
86 | struct 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 | ||
97 | struct 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 | ||
112 | static void scriptreplay_init_debug(void) | |
113 | { | |
114 | __UL_INIT_DEBUG_FROM_ENV(scriptreplay, SCRIPTREPLAY_DEBUG_, 0, SCRIPTREPLAY_DEBUG); | |
115 | } | |
116 | ||
117 | static 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 */ | |
131 | static 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 |
139 | static 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 |
147 | static 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 |
169 | static 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 |
217 | static 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 | ||
238 | static 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 |
247 | static 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 |
257 | static 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 |
310 | static 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 | ||
323 | static 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 | */ | |
337 | static 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); | |
345 | assert(xstep && *xstep); | |
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 | ||
408 | done: | |
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 | 418 | static 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); | |
437 | DBG(LOG, ul_debug("log signal emited")); | |
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 | 498 | static void __attribute__((__noreturn__)) |
86be6a32 | 499 | usage(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); | |
02a51ce1 KZ |
521 | fputs(_(" -d, --divisor <num> speed up or slow down execution with time divisor\n"), out); |
522 | fputs(_(" -m, --maxdelay <num> wait at most this many seconds between updates\n"), out); | |
7c4a374f | 523 | fputs(_(" -x, --stream <name> stream type (out, in, signal or info)\n"), out); |
88b8e9bf | 524 | fputs(_(" -c, --cr-mode <type> CR char mode (auto, never, always)\n"), out); |
f45f3ec3 | 525 | printf(USAGE_HELP_OPTIONS(25)); |
84adb5e6 | 526 | |
f45f3ec3 | 527 | printf(USAGE_MAN_TAIL("scriptreplay(1)")); |
86be6a32 | 528 | exit(EXIT_SUCCESS); |
18a706bd KZ |
529 | } |
530 | ||
531 | static double | |
532 | getnum(const char *s) | |
533 | { | |
f0b3b904 | 534 | const double d = strtod_or_err(s, _("failed to parse number")); |
18a706bd | 535 | |
f0b3b904 | 536 | if (isnan(d)) { |
18a706bd | 537 | errno = EINVAL; |
f0b3b904 | 538 | err(EXIT_FAILURE, "%s: %s", _("failed to parse number"), s); |
18a706bd KZ |
539 | } |
540 | return d; | |
541 | } | |
542 | ||
543 | static void | |
544 | delay_for(double delay) | |
545 | { | |
546 | #ifdef HAVE_NANOSLEEP | |
547 | struct timespec ts, remainder; | |
548 | ts.tv_sec = (time_t) delay; | |
549 | ts.tv_nsec = (delay - ts.tv_sec) * 1.0e9; | |
550 | ||
167a4851 KZ |
551 | DBG(TIMING, ul_debug("going to sleep for %fs", delay)); |
552 | ||
18a706bd KZ |
553 | while (-1 == nanosleep(&ts, &remainder)) { |
554 | if (EINTR == errno) | |
555 | ts = remainder; | |
556 | else | |
557 | break; | |
558 | } | |
559 | #else | |
560 | struct timeval tv; | |
561 | tv.tv_sec = (long) delay; | |
562 | tv.tv_usec = (delay - tv.tv_sec) * 1.0e6; | |
563 | select(0, NULL, NULL, NULL, &tv); | |
564 | #endif | |
565 | } | |
566 | ||
41ce6545 KZ |
567 | static void appendchr(char *buf, size_t bufsz, int c) |
568 | { | |
569 | size_t sz; | |
570 | ||
571 | if (strchr(buf, c)) | |
572 | return; /* already in */ | |
573 | ||
574 | sz = strlen(buf); | |
575 | if (sz + 1 < bufsz) | |
576 | buf[sz] = c; | |
577 | } | |
578 | ||
18a706bd KZ |
579 | int |
580 | main(int argc, char *argv[]) | |
581 | { | |
fec06a06 KZ |
582 | struct replay_setup setup = { .nlogs = 0 }; |
583 | struct replay_step *step; | |
41ce6545 | 584 | char streams[6] = {0}; /* IOSI - in, out, signal,info */ |
0d955cd8 KZ |
585 | const char *log_out = NULL, |
586 | *log_in = NULL, | |
587 | *log_io = NULL, | |
588 | *log_tm = NULL; | |
7f1d4836 | 589 | double divi = 1, maxdelay = 0; |
fec06a06 | 590 | int diviopt = FALSE, maxdelayopt = FALSE, idx; |
88b8e9bf | 591 | int ch, rc, crmode = REPLAY_CRMODE_AUTO; |
84adb5e6 SK |
592 | |
593 | static const struct option longopts[] = { | |
88b8e9bf | 594 | { "cr-mode", required_argument, 0, 'c' }, |
0da871b5 | 595 | { "timing", required_argument, 0, 't' }, |
0d955cd8 KZ |
596 | { "log-in", required_argument, 0, 'I'}, |
597 | { "log-out", required_argument, 0, 'O'}, | |
598 | { "log-io", required_argument, 0, 'B'}, | |
0da871b5 SK |
599 | { "typescript", required_argument, 0, 's' }, |
600 | { "divisor", required_argument, 0, 'd' }, | |
7f1d4836 | 601 | { "maxdelay", required_argument, 0, 'm' }, |
41ce6545 | 602 | { "stream", required_argument, 0, 'x' }, |
84adb5e6 SK |
603 | { "version", no_argument, 0, 'V' }, |
604 | { "help", no_argument, 0, 'h' }, | |
605 | { NULL, 0, 0, 0 } | |
606 | }; | |
0d955cd8 KZ |
607 | static const ul_excl_t excl[] = { /* rows and cols in ASCII order */ |
608 | { 'O', 's' }, | |
609 | { 0 } | |
610 | }; | |
611 | int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; | |
18a706bd KZ |
612 | /* Because we use space as a separator, we can't afford to use any |
613 | * locale which tolerates a space in a number. In any case, script.c | |
614 | * sets the LC_NUMERIC locale to C, anyway. | |
615 | */ | |
616 | setlocale(LC_ALL, ""); | |
617 | setlocale(LC_NUMERIC, "C"); | |
618 | ||
619 | bindtextdomain(PACKAGE, LOCALEDIR); | |
620 | textdomain(PACKAGE); | |
2c308875 | 621 | close_stdout_atexit(); |
18a706bd | 622 | |
fec06a06 KZ |
623 | scriptreplay_init_debug(); |
624 | ||
88b8e9bf | 625 | while ((ch = getopt_long(argc, argv, "B:c:I:O:t:s:d:m:x:Vh", longopts, NULL)) != -1) { |
0d955cd8 KZ |
626 | |
627 | err_exclusive_options(ch, longopts, excl, excl_st); | |
628 | ||
84adb5e6 | 629 | switch(ch) { |
88b8e9bf KZ |
630 | case 'c': |
631 | if (strcmp("auto", optarg) == 0) | |
632 | crmode = REPLAY_CRMODE_AUTO; | |
633 | else if (strcmp("never", optarg) == 0) | |
634 | crmode = REPLAY_CRMODE_NEVER; | |
635 | else if (strcmp("always", optarg) == 0) | |
636 | crmode = REPLAY_CRMODE_ALWAYS; | |
637 | else | |
638 | errx(EXIT_FAILURE, _("unsupported mode name: '%s'"), optarg); | |
639 | break; | |
0da871b5 | 640 | case 't': |
0d955cd8 | 641 | log_tm = optarg; |
0da871b5 | 642 | break; |
0d955cd8 | 643 | case 'O': |
0da871b5 | 644 | case 's': |
0d955cd8 KZ |
645 | log_out = optarg; |
646 | break; | |
647 | case 'I': | |
648 | log_in = optarg; | |
649 | break; | |
650 | case 'B': | |
651 | log_io = optarg; | |
0da871b5 SK |
652 | break; |
653 | case 'd': | |
654 | diviopt = TRUE; | |
655 | divi = getnum(optarg); | |
656 | break; | |
7f1d4836 JDN |
657 | case 'm': |
658 | maxdelayopt = TRUE; | |
659 | maxdelay = getnum(optarg); | |
660 | break; | |
41ce6545 KZ |
661 | case 'x': |
662 | if (strcmp("in", optarg) == 0) | |
663 | appendchr(streams, sizeof(streams), 'I'); | |
664 | else if (strcmp("out", optarg) == 0) | |
665 | appendchr(streams, sizeof(streams), 'O'); | |
666 | else if (strcmp("signal", optarg) == 0) | |
667 | appendchr(streams, sizeof(streams), 'S'); | |
7c4a374f KZ |
668 | else if (strcmp("info", optarg) == 0) |
669 | appendchr(streams, sizeof(streams), 'H'); | |
41ce6545 KZ |
670 | else |
671 | errx(EXIT_FAILURE, _("unsupported stream name: '%s'"), optarg); | |
672 | break; | |
84adb5e6 | 673 | case 'V': |
2c308875 | 674 | print_version(EXIT_SUCCESS); |
84adb5e6 | 675 | case 'h': |
86be6a32 | 676 | usage(); |
84adb5e6 | 677 | default: |
677ec86c | 678 | errtryhelp(EXIT_FAILURE); |
0d955cd8 KZ |
679 | } |
680 | } | |
84adb5e6 SK |
681 | argc -= optind; |
682 | argv += optind; | |
0da871b5 | 683 | idx = 0; |
84adb5e6 | 684 | |
7c4a374f | 685 | if (!log_tm && idx < argc) |
0d955cd8 KZ |
686 | log_tm = argv[idx++]; |
687 | if (!log_out && !log_in && !log_io) | |
688 | log_out = idx < argc ? argv[idx++] : "typescript"; | |
689 | ||
0da871b5 SK |
690 | if (!diviopt) |
691 | divi = idx < argc ? getnum(argv[idx]) : 1; | |
7f1d4836 JDN |
692 | if (maxdelay < 0) |
693 | maxdelay = 0; | |
18a706bd | 694 | |
6646f9f8 KZ |
695 | if (!log_tm) |
696 | errx(EXIT_FAILURE, _("timing file not specified")); | |
7c4a374f KZ |
697 | if (!(log_out || log_in || log_io)) |
698 | errx(EXIT_FAILURE, _("data log file not specified")); | |
699 | ||
700 | if (replay_set_timing_file(&setup, log_tm) != 0) | |
0d955cd8 KZ |
701 | err(EXIT_FAILURE, _("cannot open %s"), log_tm); |
702 | ||
703 | if (log_out && replay_associate_log(&setup, "O", log_out) != 0) | |
704 | err(EXIT_FAILURE, _("cannot open %s"), log_out); | |
705 | ||
706 | if (log_in && replay_associate_log(&setup, "I", log_in) != 0) | |
707 | err(EXIT_FAILURE, _("cannot open %s"), log_in); | |
7f1d4836 | 708 | |
0d955cd8 KZ |
709 | if (log_io && replay_associate_log(&setup, "IO", log_io) != 0) |
710 | err(EXIT_FAILURE, _("cannot open %s"), log_io); | |
18a706bd | 711 | |
41ce6545 KZ |
712 | if (!*streams) { |
713 | /* output is prefered default */ | |
714 | if (log_out || log_io) | |
715 | appendchr(streams, sizeof(streams), 'O'); | |
716 | else if (log_in) | |
717 | appendchr(streams, sizeof(streams), 'I'); | |
718 | } | |
719 | ||
720 | replay_set_default_type(&setup, | |
721 | *streams && streams[1] == '\0' ? *streams : 'O'); | |
88b8e9bf | 722 | replay_set_crmode(&setup, crmode); |
41ce6545 | 723 | |
fec06a06 | 724 | do { |
41ce6545 | 725 | rc = replay_get_next_step(&setup, streams, &step); |
fec06a06 KZ |
726 | if (rc) |
727 | break; | |
18a706bd | 728 | |
fec06a06 KZ |
729 | step->delay /= divi; |
730 | if (maxdelayopt && step->delay > maxdelay) | |
731 | step->delay = maxdelay; | |
732 | if (step->delay > SCRIPT_MIN_DELAY) | |
733 | delay_for(step->delay); | |
734 | ||
88b8e9bf | 735 | rc = replay_emit_step_data(&setup, step, STDOUT_FILENO); |
fec06a06 KZ |
736 | } while (rc == 0); |
737 | ||
738 | if (step && rc < 0) | |
739 | err(EXIT_FAILURE, _("%s: log file error"), step->data->filename); | |
740 | else if (rc < 0) | |
741 | err(EXIT_FAILURE, _("%s: line %d: timing file error"), | |
742 | setup.timing_filename, | |
743 | setup.timing_line); | |
f004a02d | 744 | printf("\n"); |
18a706bd KZ |
745 | exit(EXIT_SUCCESS); |
746 | } |