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(_(" -c, --command <command> run command rather than interactive shell\n"), out
);
75 fputs(_(" -d, --divisor <num> speed up or slow down execution with time divisor\n"), out
);
76 fputs(_(" -m, --maxdelay <num> wait at most this many seconds between updates\n"), out
);
77 printf(USAGE_HELP_OPTIONS(25));
79 printf(USAGE_MAN_TAIL("scriptlive(1)"));
86 const double d
= strtod_or_err(s
, _("failed to parse number"));
90 err(EXIT_FAILURE
, "%s: %s", _("failed to parse number"), s
);
95 static void callback_child_sigstop(
96 void *data
__attribute__((__unused__
)),
99 kill(getpid(), SIGSTOP
);
100 kill(child
, SIGCONT
);
103 static int process_next_step(struct scriptlive
*ss
)
105 int rc
= 0, fd
= ul_pty_get_childfd(ss
->pty
);
107 /* read next step(s) */
109 struct timeval
*delay
;
111 rc
= replay_get_next_step(ss
->setup
, "I", &ss
->step
);
113 ul_pty_write_eof_to_child(ss
->pty
);
120 delay
= replay_step_get_delay(ss
->step
);
121 if (timerisset(delay
)) {
122 /* wait until now+delay in mainloop */
123 struct timeval now
, target
;
125 gettime_monotonic(&now
);
126 timeradd(&now
, delay
, &target
);
128 ul_pty_set_mainloop_time(ss
->pty
, &target
);
131 /* no delay -- immediately write */
132 rc
= replay_emit_step_data(ss
->setup
, ss
->step
, fd
);
140 static int mainloop_cb(void *data
)
142 struct scriptlive
*ss
= (struct scriptlive
*) data
;
145 /* emit previous waiting step */
146 if (ss
->step
&& !replay_step_is_empty(ss
->step
)) {
147 int fd
= ul_pty_get_childfd(ss
->pty
);;
149 rc
= replay_emit_step_data(ss
->setup
, ss
->step
, fd
);
155 return process_next_step(ss
);
159 main(int argc
, char *argv
[])
161 static const struct timeval mindelay
= { .tv_sec
= 0, .tv_usec
= 100 };
162 struct timeval maxdelay
;
164 const char *log_in
= NULL
, *log_io
= NULL
, *log_tm
= NULL
,
165 *shell
= NULL
, *command
= NULL
;
167 int diviopt
= FALSE
, idx
;
168 int ch
, caught_signal
= 0;
169 struct ul_pty_callbacks
*cb
;
170 struct scriptlive ss
= { .pty
= NULL
};
173 static const struct option longopts
[] = {
174 { "command", required_argument
, 0, 'c' },
175 { "timing", required_argument
, 0, 't' },
176 { "log-timing", required_argument
, 0, 'T' },
177 { "log-in", required_argument
, 0, 'I'},
178 { "log-io", required_argument
, 0, 'B'},
179 { "divisor", required_argument
, 0, 'd' },
180 { "maxdelay", required_argument
, 0, 'm' },
181 { "version", no_argument
, 0, 'V' },
182 { "help", no_argument
, 0, 'h' },
185 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
189 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
190 /* Because we use space as a separator, we can't afford to use any
191 * locale which tolerates a space in a number. In any case, script.c
192 * sets the LC_NUMERIC locale to C, anyway.
194 setlocale(LC_ALL
, "");
195 setlocale(LC_NUMERIC
, "C");
197 bindtextdomain(PACKAGE
, LOCALEDIR
);
199 close_stdout_atexit();
202 timerclear(&maxdelay
);
204 while ((ch
= getopt_long(argc
, argv
, "c:B:I:T:t:d:m:Vh", longopts
, NULL
)) != -1) {
206 err_exclusive_options(ch
, longopts
, excl
, excl_st
);
224 divi
= getnum(optarg
);
227 strtotimeval_or_err(optarg
, &maxdelay
, _("failed to parse maximal delay argument"));
230 print_version(EXIT_SUCCESS
);
234 errtryhelp(EXIT_FAILURE
);
241 if (!log_tm
&& idx
< argc
)
242 log_tm
= argv
[idx
++];
243 if (!log_in
&& !log_io
&& idx
< argc
)
244 log_in
= argv
[idx
++];
247 divi
= idx
< argc
? getnum(argv
[idx
]) : 1;
250 errx(EXIT_FAILURE
, _("timing file not specified"));
251 if (!(log_in
|| log_io
))
252 errx(EXIT_FAILURE
, _("stdin typescript file not specified"));
254 ss
.setup
= replay_new_setup();
256 if (replay_set_timing_file(ss
.setup
, log_tm
) != 0)
257 err(EXIT_FAILURE
, _("cannot open %s"), log_tm
);
259 if (log_in
&& replay_associate_log(ss
.setup
, "I", log_in
) != 0)
260 err(EXIT_FAILURE
, _("cannot open %s"), log_in
);
262 if (log_io
&& replay_associate_log(ss
.setup
, "IO", log_io
) != 0)
263 err(EXIT_FAILURE
, _("cannot open %s"), log_io
);
265 replay_set_default_type(ss
.setup
, 'I');
266 replay_set_crmode(ss
.setup
, REPLAY_CRMODE_NEVER
);
269 replay_set_delay_div(ss
.setup
, divi
);
270 if (timerisset(&maxdelay
))
271 replay_set_delay_max(ss
.setup
, &maxdelay
);
272 replay_set_delay_min(ss
.setup
, &mindelay
);
274 shell
= getenv("SHELL");
276 shell
= _PATH_BSHELL
;
278 fprintf(stdout
, _(">>> scriptlive: Starting your typescript execution by %s.\n"),
279 command
? command
: shell
);
281 ul_pty_init_debug(0);
283 ss
.pty
= ul_new_pty(isatty(STDIN_FILENO
));
285 err(EXIT_FAILURE
, _("failed to allocate PTY handler"));
287 ul_pty_set_callback_data(ss
.pty
, (void *) &ss
);
288 cb
= ul_pty_get_callbacks(ss
.pty
);
289 cb
->child_sigstop
= callback_child_sigstop
;
290 cb
->mainloop
= mainloop_cb
;
292 if (!isatty(STDIN_FILENO
))
293 /* We keep ECHO flag for compatibility with script(1) */
294 ul_pty_slave_echo(ss
.pty
, 1);
296 if (ul_pty_setup(ss
.pty
))
297 err(EXIT_FAILURE
, _("failed to create pseudo-terminal"));
299 fflush(stdout
); /* ??? */
301 switch ((int) (child
= fork())) {
303 ul_pty_cleanup(ss
.pty
);
304 err(EXIT_FAILURE
, _("cannot create child process"));
311 ul_pty_init_slave(ss
.pty
);
313 signal(SIGTERM
, SIG_DFL
); /* because /etc/csh.login */
315 shname
= strrchr(shell
, '/');
316 shname
= shname
? shname
+ 1 : shell
;
318 if (access(shell
, X_OK
) == 0) {
320 execl(shell
, shname
, "-c", command
, NULL
);
322 execl(shell
, shname
, "-i", NULL
);
325 execlp(shname
, "-c", command
, NULL
);
327 execlp(shname
, "-i", NULL
);
329 err(EXIT_FAILURE
, "failed to execute %s", shell
);
337 ul_pty_set_child(ss
.pty
, child
);
339 /* read the first step and set initial delay for pty main loop; the
340 * next steps will be processed by mainloop_cb() */
341 process_next_step(&ss
);
343 /* this is the main loop */
344 ul_pty_proxy_master(ss
.pty
);
346 /* all done; cleanup and kill */
347 caught_signal
= ul_pty_get_delivered_signal(ss
.pty
);
349 if (!caught_signal
&& ul_pty_get_child(ss
.pty
) != (pid_t
)-1)
350 ul_pty_wait_for_child(ss
.pty
); /* final wait */
352 if (caught_signal
&& ul_pty_get_child(ss
.pty
) != (pid_t
)-1) {
353 fprintf(stderr
, _("\nSession terminated, killing shell..."));
354 kill(child
, SIGTERM
);
356 kill(child
, SIGKILL
);
357 fprintf(stderr
, " ...killed.\n");
360 ul_pty_cleanup(ss
.pty
);
362 replay_free_setup(ss
.setup
);
364 fprintf(stdout
, _("\n>>> scriptlive: done.\n"));