]>
Commit | Line | Data |
---|---|---|
18a706bd | 1 | /* |
bdd43357 | 2 | * Copyright (C) 2008-2019, Karel Zak <kzak@redhat.com> |
18a706bd KZ |
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> |
b639c2a3 | 30 | #include <sys/time.h> |
edb088ff | 31 | #include <termios.h> |
18a706bd | 32 | |
fec06a06 | 33 | #include "c.h" |
fec06a06 | 34 | #include "xalloc.h" |
cdd2a8c3 | 35 | #include "closestream.h" |
18a706bd | 36 | #include "nls.h" |
f0b3b904 | 37 | #include "strutils.h" |
0d955cd8 | 38 | #include "optutils.h" |
12352c96 | 39 | #include "script-playutils.h" |
18a706bd | 40 | |
5769a938 | 41 | static void __attribute__((__noreturn__)) |
86be6a32 | 42 | usage(void) |
18a706bd | 43 | { |
86be6a32 | 44 | FILE *out = stdout; |
db433bf7 | 45 | fputs(USAGE_HEADER, out); |
0d955cd8 KZ |
46 | fprintf(out, |
47 | _(" %s [options]\n"), | |
48 | program_invocation_short_name); | |
14cc9dda KZ |
49 | fprintf(out, |
50 | _(" %s [-t] timingfile [typescript] [divisor]\n"), | |
51 | program_invocation_short_name); | |
52 | ||
451dbcfa BS |
53 | fputs(USAGE_SEPARATOR, out); |
54 | fputs(_("Play back terminal typescripts, using timing information.\n"), out); | |
55 | ||
db433bf7 | 56 | fputs(USAGE_OPTIONS, out); |
02a51ce1 | 57 | fputs(_(" -t, --timing <file> script timing log file\n"), out); |
dc62fffc | 58 | fputs(_(" -T, --log-timing <file> alias to -t\n"), out); |
0d955cd8 KZ |
59 | fputs(_(" -I, --log-in <file> script stdin log file\n"), out); |
60 | fputs(_(" -O, --log-out <file> script stdout log file (default)\n"), out); | |
61 | fputs(_(" -B, --log-io <file> script stdin and stdout log file\n"), out); | |
a33f1fc4 | 62 | fputs(USAGE_SEPARATOR, out); |
eb781922 | 63 | fputs(_(" -s, --typescript <file> deprecated alias to -O\n"), out); |
0d955cd8 KZ |
64 | |
65 | fputs(USAGE_SEPARATOR, out); | |
4a4f4a62 | 66 | fputs(_(" --summary display overview about recorded session and exit\n"), out); |
02a51ce1 KZ |
67 | fputs(_(" -d, --divisor <num> speed up or slow down execution with time divisor\n"), out); |
68 | fputs(_(" -m, --maxdelay <num> wait at most this many seconds between updates\n"), out); | |
7c4a374f | 69 | fputs(_(" -x, --stream <name> stream type (out, in, signal or info)\n"), out); |
88b8e9bf | 70 | fputs(_(" -c, --cr-mode <type> CR char mode (auto, never, always)\n"), out); |
bad4c729 | 71 | fprintf(out, USAGE_HELP_OPTIONS(25)); |
84adb5e6 | 72 | |
bad4c729 | 73 | fprintf(out, USAGE_MAN_TAIL("scriptreplay(1)")); |
86be6a32 | 74 | exit(EXIT_SUCCESS); |
18a706bd KZ |
75 | } |
76 | ||
77 | static double | |
78 | getnum(const char *s) | |
79 | { | |
f0b3b904 | 80 | const double d = strtod_or_err(s, _("failed to parse number")); |
18a706bd | 81 | |
f0b3b904 | 82 | if (isnan(d)) { |
18a706bd | 83 | errno = EINVAL; |
f0b3b904 | 84 | err(EXIT_FAILURE, "%s: %s", _("failed to parse number"), s); |
18a706bd KZ |
85 | } |
86 | return d; | |
87 | } | |
88 | ||
89 | static void | |
b639c2a3 | 90 | delay_for(struct timeval *delay) |
18a706bd KZ |
91 | { |
92 | #ifdef HAVE_NANOSLEEP | |
93 | struct timespec ts, remainder; | |
b639c2a3 KZ |
94 | ts.tv_sec = (time_t) delay->tv_sec; |
95 | ts.tv_nsec = delay->tv_usec * 1000; | |
18a706bd | 96 | |
7b20bb1a KZ |
97 | DBG(TIMING, ul_debug("going to sleep for %"PRId64".%06"PRId64, |
98 | (int64_t) delay->tv_sec, (int64_t) delay->tv_usec)); | |
167a4851 | 99 | |
18a706bd KZ |
100 | while (-1 == nanosleep(&ts, &remainder)) { |
101 | if (EINTR == errno) | |
102 | ts = remainder; | |
103 | else | |
104 | break; | |
105 | } | |
106 | #else | |
b639c2a3 | 107 | select(0, NULL, NULL, NULL, delay); |
18a706bd KZ |
108 | #endif |
109 | } | |
110 | ||
cfc264d5 SG |
111 | static void |
112 | appendchr(char *buf, size_t bufsz, int c) | |
41ce6545 KZ |
113 | { |
114 | size_t sz; | |
115 | ||
116 | if (strchr(buf, c)) | |
117 | return; /* already in */ | |
118 | ||
119 | sz = strlen(buf); | |
120 | if (sz + 1 < bufsz) | |
121 | buf[sz] = c; | |
122 | } | |
123 | ||
cfc264d5 SG |
124 | static int |
125 | setterm(struct termios *backup) | |
edb088ff SG |
126 | { |
127 | struct termios tattr; | |
128 | ||
cfc264d5 | 129 | if (tcgetattr(STDOUT_FILENO, backup) != 0) { |
535e81eb SG |
130 | if (errno != ENOTTY) /* For debugger. */ |
131 | err(EXIT_FAILURE, _("unexpected tcgetattr failure")); | |
cfc264d5 SG |
132 | return 0; |
133 | } | |
edb088ff SG |
134 | tattr = *backup; |
135 | cfmakeraw(&tattr); | |
00a09d78 | 136 | tattr.c_lflag |= ISIG; |
584e5051 | 137 | tattr.c_iflag |= IXON; |
cfc264d5 SG |
138 | tcsetattr(STDOUT_FILENO, TCSANOW, &tattr); |
139 | return 1; | |
edb088ff SG |
140 | } |
141 | ||
18a706bd KZ |
142 | int |
143 | main(int argc, char *argv[]) | |
144 | { | |
b639c2a3 KZ |
145 | static const struct timeval mindelay = { .tv_sec = 0, .tv_usec = 100 }; |
146 | struct timeval maxdelay; | |
147 | ||
cfc264d5 | 148 | int isterm; |
edb088ff SG |
149 | struct termios saved; |
150 | ||
12352c96 | 151 | struct replay_setup *setup = NULL; |
4a4f4a62 | 152 | struct replay_step *step = NULL; |
41ce6545 | 153 | char streams[6] = {0}; /* IOSI - in, out, signal,info */ |
0d955cd8 KZ |
154 | const char *log_out = NULL, |
155 | *log_in = NULL, | |
156 | *log_io = NULL, | |
157 | *log_tm = NULL; | |
b639c2a3 KZ |
158 | double divi = 1; |
159 | int diviopt = FALSE, idx; | |
4a4f4a62 KZ |
160 | int ch, rc, crmode = REPLAY_CRMODE_AUTO, summary = 0; |
161 | enum { | |
162 | OPT_SUMMARY = CHAR_MAX + 1 | |
163 | }; | |
84adb5e6 SK |
164 | |
165 | static const struct option longopts[] = { | |
88b8e9bf | 166 | { "cr-mode", required_argument, 0, 'c' }, |
0da871b5 | 167 | { "timing", required_argument, 0, 't' }, |
a33f1fc4 KZ |
168 | { "log-timing", required_argument, 0, 'T' }, |
169 | { "log-in", required_argument, 0, 'I' }, | |
170 | { "log-out", required_argument, 0, 'O' }, | |
171 | { "log-io", required_argument, 0, 'B' }, | |
0da871b5 SK |
172 | { "typescript", required_argument, 0, 's' }, |
173 | { "divisor", required_argument, 0, 'd' }, | |
7f1d4836 | 174 | { "maxdelay", required_argument, 0, 'm' }, |
41ce6545 | 175 | { "stream", required_argument, 0, 'x' }, |
4a4f4a62 | 176 | { "summary", no_argument, 0, OPT_SUMMARY }, |
84adb5e6 SK |
177 | { "version", no_argument, 0, 'V' }, |
178 | { "help", no_argument, 0, 'h' }, | |
179 | { NULL, 0, 0, 0 } | |
180 | }; | |
0d955cd8 KZ |
181 | static const ul_excl_t excl[] = { /* rows and cols in ASCII order */ |
182 | { 'O', 's' }, | |
183 | { 0 } | |
184 | }; | |
185 | int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; | |
18a706bd KZ |
186 | /* Because we use space as a separator, we can't afford to use any |
187 | * locale which tolerates a space in a number. In any case, script.c | |
188 | * sets the LC_NUMERIC locale to C, anyway. | |
189 | */ | |
190 | setlocale(LC_ALL, ""); | |
191 | setlocale(LC_NUMERIC, "C"); | |
192 | ||
193 | bindtextdomain(PACKAGE, LOCALEDIR); | |
194 | textdomain(PACKAGE); | |
2c308875 | 195 | close_stdout_atexit(); |
18a706bd | 196 | |
12352c96 | 197 | replay_init_debug(); |
b639c2a3 | 198 | timerclear(&maxdelay); |
fec06a06 | 199 | |
a33f1fc4 | 200 | while ((ch = getopt_long(argc, argv, "B:c:I:O:T:t:s:d:m:x:Vh", longopts, NULL)) != -1) { |
0d955cd8 KZ |
201 | |
202 | err_exclusive_options(ch, longopts, excl, excl_st); | |
203 | ||
84adb5e6 | 204 | switch(ch) { |
88b8e9bf KZ |
205 | case 'c': |
206 | if (strcmp("auto", optarg) == 0) | |
207 | crmode = REPLAY_CRMODE_AUTO; | |
208 | else if (strcmp("never", optarg) == 0) | |
209 | crmode = REPLAY_CRMODE_NEVER; | |
210 | else if (strcmp("always", optarg) == 0) | |
211 | crmode = REPLAY_CRMODE_ALWAYS; | |
212 | else | |
213 | errx(EXIT_FAILURE, _("unsupported mode name: '%s'"), optarg); | |
214 | break; | |
0da871b5 | 215 | case 't': |
a33f1fc4 | 216 | case 'T': |
0d955cd8 | 217 | log_tm = optarg; |
0da871b5 | 218 | break; |
0d955cd8 | 219 | case 'O': |
0da871b5 | 220 | case 's': |
0d955cd8 KZ |
221 | log_out = optarg; |
222 | break; | |
223 | case 'I': | |
224 | log_in = optarg; | |
225 | break; | |
226 | case 'B': | |
227 | log_io = optarg; | |
0da871b5 SK |
228 | break; |
229 | case 'd': | |
230 | diviopt = TRUE; | |
231 | divi = getnum(optarg); | |
232 | break; | |
7f1d4836 | 233 | case 'm': |
b639c2a3 | 234 | strtotimeval_or_err(optarg, &maxdelay, _("failed to parse maximal delay argument")); |
7f1d4836 | 235 | break; |
41ce6545 KZ |
236 | case 'x': |
237 | if (strcmp("in", optarg) == 0) | |
238 | appendchr(streams, sizeof(streams), 'I'); | |
239 | else if (strcmp("out", optarg) == 0) | |
240 | appendchr(streams, sizeof(streams), 'O'); | |
241 | else if (strcmp("signal", optarg) == 0) | |
242 | appendchr(streams, sizeof(streams), 'S'); | |
7c4a374f KZ |
243 | else if (strcmp("info", optarg) == 0) |
244 | appendchr(streams, sizeof(streams), 'H'); | |
41ce6545 KZ |
245 | else |
246 | errx(EXIT_FAILURE, _("unsupported stream name: '%s'"), optarg); | |
247 | break; | |
4a4f4a62 KZ |
248 | case OPT_SUMMARY: |
249 | summary = 1; | |
250 | break; | |
84adb5e6 | 251 | case 'V': |
2c308875 | 252 | print_version(EXIT_SUCCESS); |
84adb5e6 | 253 | case 'h': |
86be6a32 | 254 | usage(); |
84adb5e6 | 255 | default: |
677ec86c | 256 | errtryhelp(EXIT_FAILURE); |
0d955cd8 KZ |
257 | } |
258 | } | |
84adb5e6 SK |
259 | argc -= optind; |
260 | argv += optind; | |
0da871b5 | 261 | idx = 0; |
84adb5e6 | 262 | |
4a4f4a62 KZ |
263 | if (summary) |
264 | streams[0] = 'H', streams[1] = '\0'; | |
265 | ||
7c4a374f | 266 | if (!log_tm && idx < argc) |
0d955cd8 | 267 | log_tm = argv[idx++]; |
d51f8ec1 | 268 | if (!log_out && !summary && !log_in && !log_io) |
0d955cd8 KZ |
269 | log_out = idx < argc ? argv[idx++] : "typescript"; |
270 | ||
0da871b5 SK |
271 | if (!diviopt) |
272 | divi = idx < argc ? getnum(argv[idx]) : 1; | |
18a706bd | 273 | |
6646f9f8 KZ |
274 | if (!log_tm) |
275 | errx(EXIT_FAILURE, _("timing file not specified")); | |
d51f8ec1 | 276 | if (!(log_out || log_in || log_io) && !summary) |
7c4a374f KZ |
277 | errx(EXIT_FAILURE, _("data log file not specified")); |
278 | ||
12352c96 KZ |
279 | setup = replay_new_setup(); |
280 | ||
281 | if (replay_set_timing_file(setup, log_tm) != 0) | |
0d955cd8 KZ |
282 | err(EXIT_FAILURE, _("cannot open %s"), log_tm); |
283 | ||
12352c96 | 284 | if (log_out && replay_associate_log(setup, "O", log_out) != 0) |
0d955cd8 KZ |
285 | err(EXIT_FAILURE, _("cannot open %s"), log_out); |
286 | ||
12352c96 | 287 | if (log_in && replay_associate_log(setup, "I", log_in) != 0) |
0d955cd8 | 288 | err(EXIT_FAILURE, _("cannot open %s"), log_in); |
7f1d4836 | 289 | |
12352c96 | 290 | if (log_io && replay_associate_log(setup, "IO", log_io) != 0) |
0d955cd8 | 291 | err(EXIT_FAILURE, _("cannot open %s"), log_io); |
18a706bd | 292 | |
41ce6545 | 293 | if (!*streams) { |
ac407b16 | 294 | /* output is preferred default */ |
41ce6545 KZ |
295 | if (log_out || log_io) |
296 | appendchr(streams, sizeof(streams), 'O'); | |
297 | else if (log_in) | |
298 | appendchr(streams, sizeof(streams), 'I'); | |
299 | } | |
300 | ||
12352c96 | 301 | replay_set_default_type(setup, |
41ce6545 | 302 | *streams && streams[1] == '\0' ? *streams : 'O'); |
12352c96 | 303 | replay_set_crmode(setup, crmode); |
41ce6545 | 304 | |
b639c2a3 KZ |
305 | if (divi != 1) |
306 | replay_set_delay_div(setup, divi); | |
307 | if (timerisset(&maxdelay)) | |
308 | replay_set_delay_max(setup, &maxdelay); | |
309 | replay_set_delay_min(setup, &mindelay); | |
310 | ||
cfc264d5 | 311 | isterm = setterm(&saved); |
edb088ff | 312 | |
fec06a06 | 313 | do { |
12352c96 | 314 | rc = replay_get_next_step(setup, streams, &step); |
fec06a06 KZ |
315 | if (rc) |
316 | break; | |
18a706bd | 317 | |
4a4f4a62 | 318 | if (!summary) { |
b639c2a3 | 319 | struct timeval *delay = replay_step_get_delay(step); |
12352c96 | 320 | |
b639c2a3 | 321 | if (delay && timerisset(delay)) |
12352c96 | 322 | delay_for(delay); |
4a4f4a62 | 323 | } |
12352c96 | 324 | rc = replay_emit_step_data(setup, step, STDOUT_FILENO); |
fec06a06 KZ |
325 | } while (rc == 0); |
326 | ||
edb088ff SG |
327 | if (isterm) |
328 | tcsetattr(STDOUT_FILENO, TCSADRAIN, &saved); | |
329 | ||
fec06a06 | 330 | if (step && rc < 0) |
12352c96 | 331 | err(EXIT_FAILURE, _("%s: log file error"), replay_step_get_filename(step)); |
fec06a06 KZ |
332 | else if (rc < 0) |
333 | err(EXIT_FAILURE, _("%s: line %d: timing file error"), | |
12352c96 KZ |
334 | replay_get_timing_file(setup), |
335 | replay_get_timing_line(setup)); | |
f004a02d | 336 | printf("\n"); |
263835e8 KZ |
337 | replay_free_setup(setup); |
338 | ||
18a706bd KZ |
339 | exit(EXIT_SUCCESS); |
340 | } |