2 * Copyright (C) 2019, Karel Zak <kzak@redhat.com>
4 * This file is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This file is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
23 #include <sys/select.h>
27 #include <sys/types.h>
35 #include "closestream.h"
39 #include "pty-session.h"
40 #include "script-playutils.h"
41 #include "monotonic.h"
44 #define SCRIPT_MIN_DELAY 0.0001 /* from original sripreplay.pl */
48 struct replay_setup
*setup
;
49 struct replay_step
*step
;
52 static void __attribute__((__noreturn__
))
56 fputs(USAGE_HEADER
, out
);
59 program_invocation_short_name
);
61 _(" %s [-t] timingfile [-I|-B] typescript\n"),
62 program_invocation_short_name
);
64 fputs(USAGE_SEPARATOR
, out
);
65 fputs(_("Execute terminal typescript.\n"), out
);
67 fputs(USAGE_OPTIONS
, out
);
68 fputs(_(" -t, --timing <file> script timing log file\n"), out
);
69 fputs(_(" -T, --log-timing <file> alias to -t\n"), out
);
70 fputs(_(" -I, --log-in <file> script stdin log file\n"), out
);
71 fputs(_(" -B, --log-io <file> script stdin and stdout log file\n"), out
);
73 fputs(USAGE_SEPARATOR
, out
);
74 fputs(_(" -d, --divisor <num> speed up or slow down execution with time divisor\n"), out
);
75 fputs(_(" -m, --maxdelay <num> wait at most this many seconds between updates\n"), out
);
76 printf(USAGE_HELP_OPTIONS(25));
78 printf(USAGE_MAN_TAIL("scriptlive(1)"));
85 const double d
= strtod_or_err(s
, _("failed to parse number"));
89 err(EXIT_FAILURE
, "%s: %s", _("failed to parse number"), s
);
94 static void callback_child_sigstop(
95 void *data
__attribute__((__unused__
)),
98 kill(getpid(), SIGSTOP
);
102 static int process_next_step(struct scriptlive
*ss
)
104 int rc
= 0, fd
= ul_pty_get_childfd(ss
->pty
);
106 /* read next step(s) */
108 struct timeval
*delay
;
110 rc
= replay_get_next_step(ss
->setup
, "I", &ss
->step
);
114 delay
= replay_step_get_delay(ss
->step
);
115 if (timerisset(delay
)) {
116 /* wait until now+delay in mainloop */
117 struct timeval now
, target
;
119 gettime_monotonic(&now
);
120 timeradd(&now
, delay
, &target
);
122 ul_pty_set_mainloop_time(ss
->pty
, &target
);
125 /* no delay -- immediately write */
126 rc
= replay_emit_step_data(ss
->setup
, ss
->step
, fd
);
134 static int mainloop_cb(void *data
)
136 struct scriptlive
*ss
= (struct scriptlive
*) data
;
139 /* emit previous waiting step */
140 if (ss
->step
&& !replay_step_is_empty(ss
->step
)) {
141 int fd
= ul_pty_get_childfd(ss
->pty
);;
143 rc
= replay_emit_step_data(ss
->setup
, ss
->step
, fd
);
149 return process_next_step(ss
);
153 main(int argc
, char *argv
[])
155 static const struct timeval mindelay
= { .tv_sec
= 0, .tv_usec
= 100 };
156 struct timeval maxdelay
;
158 const char *log_in
= NULL
, *log_io
= NULL
, *log_tm
= NULL
,
159 *shell
= NULL
, *command
= NULL
;
161 int diviopt
= FALSE
, idx
;
162 int ch
, caught_signal
= 0;
163 struct ul_pty_callbacks
*cb
;
164 struct scriptlive ss
= { .pty
= NULL
};
167 static const struct option longopts
[] = {
168 { "timing", required_argument
, 0, 't' },
169 { "log-timing", required_argument
, 0, 'T' },
170 { "log-in", required_argument
, 0, 'I'},
171 { "log-io", required_argument
, 0, 'B'},
172 { "divisor", required_argument
, 0, 'd' },
173 { "maxdelay", required_argument
, 0, 'm' },
174 { "version", no_argument
, 0, 'V' },
175 { "help", no_argument
, 0, 'h' },
178 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
182 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
183 /* Because we use space as a separator, we can't afford to use any
184 * locale which tolerates a space in a number. In any case, script.c
185 * sets the LC_NUMERIC locale to C, anyway.
187 setlocale(LC_ALL
, "");
188 setlocale(LC_NUMERIC
, "C");
190 bindtextdomain(PACKAGE
, LOCALEDIR
);
192 close_stdout_atexit();
195 timerclear(&maxdelay
);
197 while ((ch
= getopt_long(argc
, argv
, "B:I:T:t:d:m:Vh", longopts
, NULL
)) != -1) {
199 err_exclusive_options(ch
, longopts
, excl
, excl_st
);
214 divi
= getnum(optarg
);
217 strtotimeval_or_err(optarg
, &maxdelay
, _("failed to parse maximal delay argument"));
220 print_version(EXIT_SUCCESS
);
224 errtryhelp(EXIT_FAILURE
);
231 if (!isatty(STDIN_FILENO
))
232 errx(EXIT_FAILURE
, _("stdin is not terminal"));
234 if (!log_tm
&& idx
< argc
)
235 log_tm
= argv
[idx
++];
236 if (!log_in
&& !log_io
&& idx
< argc
)
237 log_in
= argv
[idx
++];
240 divi
= idx
< argc
? getnum(argv
[idx
]) : 1;
243 errx(EXIT_FAILURE
, _("timing file not specified"));
244 if (!(log_in
|| log_io
))
245 errx(EXIT_FAILURE
, _("stdin typescript file not specified"));
247 ss
.setup
= replay_new_setup();
249 if (replay_set_timing_file(ss
.setup
, log_tm
) != 0)
250 err(EXIT_FAILURE
, _("cannot open %s"), log_tm
);
252 if (log_in
&& replay_associate_log(ss
.setup
, "I", log_in
) != 0)
253 err(EXIT_FAILURE
, _("cannot open %s"), log_in
);
255 if (log_io
&& replay_associate_log(ss
.setup
, "IO", log_io
) != 0)
256 err(EXIT_FAILURE
, _("cannot open %s"), log_io
);
258 replay_set_default_type(ss
.setup
, 'I');
259 replay_set_crmode(ss
.setup
, REPLAY_CRMODE_NEVER
);
262 replay_set_delay_div(ss
.setup
, divi
);
263 if (timerisset(&maxdelay
))
264 replay_set_delay_max(ss
.setup
, &maxdelay
);
265 replay_set_delay_min(ss
.setup
, &mindelay
);
267 shell
= getenv("SHELL");
269 shell
= _PATH_BSHELL
;
271 fprintf(stdout
, _(">>> scriptlive: Starting your typescript execution by %s.\n"), shell
);
273 ul_pty_init_debug(0);
275 ss
.pty
= ul_new_pty(isatty(STDIN_FILENO
));
277 err(EXIT_FAILURE
, _("failed to allocate PTY handler"));
279 ul_pty_set_callback_data(ss
.pty
, (void *) &ss
);
280 cb
= ul_pty_get_callbacks(ss
.pty
);
281 cb
->child_sigstop
= callback_child_sigstop
;
282 cb
->mainloop
= mainloop_cb
;
284 if (ul_pty_setup(ss
.pty
))
285 err(EXIT_FAILURE
, _("failed to create pseudo-terminal"));
287 fflush(stdout
); /* ??? */
289 switch ((int) (child
= fork())) {
291 ul_pty_cleanup(ss
.pty
);
292 err(EXIT_FAILURE
, _("cannot create child process"));
299 ul_pty_init_slave(ss
.pty
);
301 signal(SIGTERM
, SIG_DFL
); /* because /etc/csh.login */
303 shname
= strrchr(shell
, '/');
304 shname
= shname
? shname
+ 1 : shell
;
307 execl(shell
, shname
, "-c", command
, NULL
);
309 execl(shell
, shname
, "-i", NULL
);
310 err(EXIT_FAILURE
, "failed to execute %s", shell
);
318 ul_pty_set_child(ss
.pty
, child
);
320 /* read the first step and set initial delay for pty main loop; the
321 * next steps will be processed by mainloop_cb() */
322 process_next_step(&ss
);
324 /* this is the main loop */
325 ul_pty_proxy_master(ss
.pty
);
327 /* all done; cleanup and kill */
328 caught_signal
= ul_pty_get_delivered_signal(ss
.pty
);
330 if (!caught_signal
&& ul_pty_get_child(ss
.pty
) != (pid_t
)-1)
331 ul_pty_wait_for_child(ss
.pty
); /* final wait */
333 if (caught_signal
&& ul_pty_get_child(ss
.pty
) != (pid_t
)-1) {
334 fprintf(stderr
, _("\nSession terminated, killing shell..."));
335 kill(child
, SIGTERM
);
337 kill(child
, SIGKILL
);
338 fprintf(stderr
, " ...killed.\n");
341 ul_pty_cleanup(ss
.pty
);
343 replay_free_setup(ss
.setup
);
345 fprintf(stdout
, _("\n>>> scriptlive: done.\n"));