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