]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - term-utils/scriptlive.c
build-sys: remove duplicate includes
[thirdparty/util-linux.git] / term-utils / scriptlive.c
index b7abe361064a2b7f635b84567c1d2a5995bfb8c7..b17dc9ef304e0f8f7dc72a752fede00dd64feb29 100644 (file)
@@ -27,7 +27,6 @@
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <termios.h>
-#include <unistd.h>
 #include <paths.h>
 
 #include "c.h"
@@ -71,6 +70,7 @@ usage(void)
        fputs(_(" -B, --log-io <file>     script stdin and stdout log file\n"), out);
 
        fputs(USAGE_SEPARATOR, out);
+       fputs(_(" -c, --command <command> run command rather than interactive shell\n"), out);
        fputs(_(" -d, --divisor <num>     speed up or slow down execution with time divisor\n"), out);
        fputs(_(" -m, --maxdelay <num>    wait at most this many seconds between updates\n"), out);
        printf(USAGE_HELP_OPTIONS(25));
@@ -108,6 +108,11 @@ static int process_next_step(struct scriptlive *ss)
                struct timeval *delay;
 
                rc = replay_get_next_step(ss->setup, "I", &ss->step);
+               if (rc == 1) {
+                       ul_pty_write_eof_to_child(ss->pty);
+                       rc = 0;
+                       break;
+               }
                if (rc)
                        break;
 
@@ -165,6 +170,7 @@ main(int argc, char *argv[])
        pid_t child;
 
        static const struct option longopts[] = {
+               { "command",    required_argument,      0, 'c' },
                { "timing",     required_argument,      0, 't' },
                { "log-timing", required_argument,      0, 'T' },
                { "log-in",     required_argument,      0, 'I'},
@@ -194,11 +200,14 @@ main(int argc, char *argv[])
        replay_init_debug();
        timerclear(&maxdelay);
 
-       while ((ch = getopt_long(argc, argv, "B:I:T:t:d:m:Vh", longopts, NULL)) != -1) {
+       while ((ch = getopt_long(argc, argv, "c:B:I:T:t:d:m:Vh", longopts, NULL)) != -1) {
 
                err_exclusive_options(ch, longopts, excl, excl_st);
 
                switch(ch) {
+               case 'c':
+                       command = optarg;
+                       break;
                case 't':
                case 'T':
                        log_tm = optarg;
@@ -228,9 +237,6 @@ main(int argc, char *argv[])
        argv += optind;
        idx = 0;
 
-       if (!isatty(STDIN_FILENO))
-               errx(EXIT_FAILURE, _("stdin is not terminal"));
-
        if (!log_tm && idx < argc)
                log_tm = argv[idx++];
        if (!log_in && !log_io && idx < argc)
@@ -268,7 +274,8 @@ main(int argc, char *argv[])
        if (shell == NULL)
                shell = _PATH_BSHELL;
 
-       fprintf(stdout, _(">>> scriptlive: Starting your typescript execution by %s.\n"), shell);
+       fprintf(stdout, _(">>> scriptlive: Starting your typescript execution by %s.\n"),
+                       command ? command : shell);
 
        ul_pty_init_debug(0);
 
@@ -281,6 +288,10 @@ main(int argc, char *argv[])
        cb->child_sigstop = callback_child_sigstop;
        cb->mainloop = mainloop_cb;
 
+       if (!isatty(STDIN_FILENO))
+               /* We keep ECHO flag for compatibility with script(1) */
+               ul_pty_slave_echo(ss.pty, 1);
+
        if (ul_pty_setup(ss.pty))
                err(EXIT_FAILURE, _("failed to create pseudo-terminal"));
 
@@ -303,10 +314,17 @@ main(int argc, char *argv[])
                shname = strrchr(shell, '/');
                shname = shname ? shname + 1 : shell;
 
-               if (command)
-                       execl(shell, shname, "-c", command, NULL);
-               else
-                       execl(shell, shname, "-i", NULL);
+               if (access(shell, X_OK) == 0) {
+                       if (command)
+                               execl(shell, shname, "-c", command, NULL);
+                       else
+                               execl(shell, shname, "-i", NULL);
+               } else {
+                       if (command)
+                               execlp(shname, "-c", command, NULL);
+                       else
+                               execlp(shname, "-i", NULL);
+               }
                err(EXIT_FAILURE, "failed to execute %s", shell);
                break;
        }