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>
34 #include "closestream.h"
38 #include "pty-session.h"
39 #include "script-playutils.h"
40 #include "monotonic.h"
43 #define SCRIPT_MIN_DELAY 0.0001 /* from original sripreplay.pl */
47 struct replay_setup
*setup
;
48 struct replay_step
*step
;
51 static void __attribute__((__noreturn__
))
55 fputs(USAGE_HEADER
, out
);
58 program_invocation_short_name
);
60 _(" %s [-t] timingfile [-I|-B] typescript\n"),
61 program_invocation_short_name
);
63 fputs(USAGE_SEPARATOR
, out
);
64 fputs(_("Execute terminal typescript.\n"), out
);
66 fputs(USAGE_OPTIONS
, out
);
67 fputs(_(" -t, --timing <file> script timing log file\n"), out
);
68 fputs(_(" -T, --log-timing <file> alias to -t\n"), out
);
69 fputs(_(" -I, --log-in <file> script stdin log file\n"), out
);
70 fputs(_(" -B, --log-io <file> script stdin and stdout log file\n"), out
);
72 fputs(USAGE_SEPARATOR
, out
);
73 fputs(_(" -c, --command <command> run command rather than interactive shell\n"), 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
);
112 ul_pty_write_eof_to_child(ss
->pty
);
119 delay
= replay_step_get_delay(ss
->step
);
120 if (timerisset(delay
)) {
121 /* wait until now+delay in mainloop */
122 struct timeval now
, target
;
124 gettime_monotonic(&now
);
125 timeradd(&now
, delay
, &target
);
127 ul_pty_set_mainloop_time(ss
->pty
, &target
);
130 /* no delay -- immediately write */
131 rc
= replay_emit_step_data(ss
->setup
, ss
->step
, fd
);
139 static int mainloop_cb(void *data
)
141 struct scriptlive
*ss
= (struct scriptlive
*) data
;
144 /* emit previous waiting step */
145 if (ss
->step
&& !replay_step_is_empty(ss
->step
)) {
146 int fd
= ul_pty_get_childfd(ss
->pty
);;
148 rc
= replay_emit_step_data(ss
->setup
, ss
->step
, fd
);
154 return process_next_step(ss
);
158 main(int argc
, char *argv
[])
160 static const struct timeval mindelay
= { .tv_sec
= 0, .tv_usec
= 100 };
161 struct timeval maxdelay
;
163 const char *log_in
= NULL
, *log_io
= NULL
, *log_tm
= NULL
,
164 *shell
= NULL
, *command
= NULL
;
166 int diviopt
= FALSE
, idx
;
167 int ch
, caught_signal
= 0;
168 struct ul_pty_callbacks
*cb
;
169 struct scriptlive ss
= { .pty
= NULL
};
172 static const struct option longopts
[] = {
173 { "command", required_argument
, 0, 'c' },
174 { "timing", required_argument
, 0, 't' },
175 { "log-timing", required_argument
, 0, 'T' },
176 { "log-in", required_argument
, 0, 'I'},
177 { "log-io", required_argument
, 0, 'B'},
178 { "divisor", required_argument
, 0, 'd' },
179 { "maxdelay", required_argument
, 0, 'm' },
180 { "version", no_argument
, 0, 'V' },
181 { "help", no_argument
, 0, 'h' },
184 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
188 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
189 /* Because we use space as a separator, we can't afford to use any
190 * locale which tolerates a space in a number. In any case, script.c
191 * sets the LC_NUMERIC locale to C, anyway.
193 setlocale(LC_ALL
, "");
194 setlocale(LC_NUMERIC
, "C");
196 bindtextdomain(PACKAGE
, LOCALEDIR
);
198 close_stdout_atexit();
201 timerclear(&maxdelay
);
203 while ((ch
= getopt_long(argc
, argv
, "c:B:I:T:t:d:m:Vh", longopts
, NULL
)) != -1) {
205 err_exclusive_options(ch
, longopts
, excl
, excl_st
);
223 divi
= getnum(optarg
);
226 strtotimeval_or_err(optarg
, &maxdelay
, _("failed to parse maximal delay argument"));
229 print_version(EXIT_SUCCESS
);
233 errtryhelp(EXIT_FAILURE
);
240 if (!log_tm
&& idx
< argc
)
241 log_tm
= argv
[idx
++];
242 if (!log_in
&& !log_io
&& idx
< argc
)
243 log_in
= argv
[idx
++];
246 divi
= idx
< argc
? getnum(argv
[idx
]) : 1;
249 errx(EXIT_FAILURE
, _("timing file not specified"));
250 if (!(log_in
|| log_io
))
251 errx(EXIT_FAILURE
, _("stdin typescript file not specified"));
253 ss
.setup
= replay_new_setup();
255 if (replay_set_timing_file(ss
.setup
, log_tm
) != 0)
256 err(EXIT_FAILURE
, _("cannot open %s"), log_tm
);
258 if (log_in
&& replay_associate_log(ss
.setup
, "I", log_in
) != 0)
259 err(EXIT_FAILURE
, _("cannot open %s"), log_in
);
261 if (log_io
&& replay_associate_log(ss
.setup
, "IO", log_io
) != 0)
262 err(EXIT_FAILURE
, _("cannot open %s"), log_io
);
264 replay_set_default_type(ss
.setup
, 'I');
265 replay_set_crmode(ss
.setup
, REPLAY_CRMODE_NEVER
);
268 replay_set_delay_div(ss
.setup
, divi
);
269 if (timerisset(&maxdelay
))
270 replay_set_delay_max(ss
.setup
, &maxdelay
);
271 replay_set_delay_min(ss
.setup
, &mindelay
);
273 shell
= getenv("SHELL");
275 shell
= _PATH_BSHELL
;
277 fprintf(stdout
, _(">>> scriptlive: Starting your typescript execution by %s.\n"),
278 command
? command
: shell
);
280 ul_pty_init_debug(0);
282 ss
.pty
= ul_new_pty(isatty(STDIN_FILENO
));
284 err(EXIT_FAILURE
, _("failed to allocate PTY handler"));
286 ul_pty_set_callback_data(ss
.pty
, (void *) &ss
);
287 cb
= ul_pty_get_callbacks(ss
.pty
);
288 cb
->child_sigstop
= callback_child_sigstop
;
289 cb
->mainloop
= mainloop_cb
;
291 if (!isatty(STDIN_FILENO
))
292 /* We keep ECHO flag for compatibility with script(1) */
293 ul_pty_slave_echo(ss
.pty
, 1);
295 if (ul_pty_setup(ss
.pty
))
296 err(EXIT_FAILURE
, _("failed to create pseudo-terminal"));
298 fflush(stdout
); /* ??? */
300 switch ((int) (child
= fork())) {
302 ul_pty_cleanup(ss
.pty
);
303 err(EXIT_FAILURE
, _("cannot create child process"));
310 ul_pty_init_slave(ss
.pty
);
312 signal(SIGTERM
, SIG_DFL
); /* because /etc/csh.login */
314 shname
= strrchr(shell
, '/');
315 shname
= shname
? shname
+ 1 : shell
;
317 if (access(shell
, X_OK
) == 0) {
319 execl(shell
, shname
, "-c", command
, NULL
);
321 execl(shell
, shname
, "-i", NULL
);
324 execlp(shname
, "-c", command
, NULL
);
326 execlp(shname
, "-i", NULL
);
328 err(EXIT_FAILURE
, "failed to execute %s", shell
);
336 ul_pty_set_child(ss
.pty
, child
);
338 /* read the first step and set initial delay for pty main loop; the
339 * next steps will be processed by mainloop_cb() */
340 process_next_step(&ss
);
342 /* this is the main loop */
343 ul_pty_proxy_master(ss
.pty
);
345 /* all done; cleanup and kill */
346 caught_signal
= ul_pty_get_delivered_signal(ss
.pty
);
348 if (!caught_signal
&& ul_pty_get_child(ss
.pty
) != (pid_t
)-1)
349 ul_pty_wait_for_child(ss
.pty
); /* final wait */
351 if (caught_signal
&& ul_pty_get_child(ss
.pty
) != (pid_t
)-1) {
352 fprintf(stderr
, _("\nSession terminated, killing shell..."));
353 kill(child
, SIGTERM
);
355 kill(child
, SIGKILL
);
356 fprintf(stderr
, " ...killed.\n");
359 ul_pty_cleanup(ss
.pty
);
361 replay_free_setup(ss
.setup
);
363 fprintf(stdout
, _("\n>>> scriptlive: done.\n"));