Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/script If -e is specified exit with the exit status ...



details:   https://anonhg.NetBSD.org/src/rev/483c1d6b2658
branches:  trunk
changeset: 359678:483c1d6b2658
user:      christos <christos%NetBSD.org@localhost>
date:      Sun Jan 16 19:04:00 2022 +0000

description:
If -e is specified exit with the exit status of the child process in
a shell-like format (nabijaczleweli)

diffstat:

 usr.bin/script/script.1 |  10 ++++++--
 usr.bin/script/script.c |  54 ++++++++++++++++++++++++------------------------
 2 files changed, 34 insertions(+), 30 deletions(-)

diffs (184 lines):

diff -r 6de0a03c4780 -r 483c1d6b2658 usr.bin/script/script.1
--- a/usr.bin/script/script.1   Sun Jan 16 18:16:06 2022 +0000
+++ b/usr.bin/script/script.1   Sun Jan 16 19:04:00 2022 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: script.1,v 1.13 2011/06/08 13:54:16 yamt Exp $
+.\"    $NetBSD: script.1,v 1.14 2022/01/16 19:04:00 christos Exp $
 .\"
 .\" Copyright (c) 1980, 1990, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -29,7 +29,7 @@
 .\"
 .\"    @(#)script.1    8.1 (Berkeley) 6/6/93
 .\"
-.Dd October 17, 2009
+.Dd January 16, 2022
 .Dt SCRIPT 1
 .Os
 .Sh NAME
@@ -37,7 +37,7 @@
 .Nd make typescript of terminal session
 .Sh SYNOPSIS
 .Nm
-.Op Fl adfpqr
+.Op Fl adefpqr
 .Op Fl c Ar command
 .Op Ar file
 .Sh DESCRIPTION
@@ -75,6 +75,10 @@
 When playing back a session with the
 .Fl p
 flag, don't sleep between records when playing back a timestamped session.
+.It Fl e
+Exit with the shell-style exit status of the shell or
+.Ar command ,
+instead of always exiting successfully.
 .It Fl f
 Flush output after each write.
 This is useful for watching the script output in real time.
diff -r 6de0a03c4780 -r 483c1d6b2658 usr.bin/script/script.c
--- a/usr.bin/script/script.c   Sun Jan 16 18:16:06 2022 +0000
+++ b/usr.bin/script/script.c   Sun Jan 16 19:04:00 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: script.c,v 1.28 2020/08/31 15:32:15 christos Exp $     */
+/*     $NetBSD: script.c,v 1.29 2022/01/16 19:04:00 christos Exp $     */
 
 /*
  * Copyright (c) 1980, 1992, 1993
@@ -39,7 +39,7 @@
 #if 0
 static char sccsid[] = "@(#)script.c   8.1 (Berkeley) 6/6/93";
 #endif
-__RCSID("$NetBSD: script.c,v 1.28 2020/08/31 15:32:15 christos Exp $");
+__RCSID("$NetBSD: script.c,v 1.29 2022/01/16 19:04:00 christos Exp $");
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -81,10 +81,11 @@
 static int     quiet, flush;
 static const char *fname;
 
+static int     eflag;
 static int     isterm;
 static struct  termios tt;
 
-__dead static void     done(void);
+__dead static void     done(int);
 __dead static void     dooutput(void);
 __dead static void     doshell(const char *);
 __dead static void     fail(void);
@@ -92,7 +93,6 @@
 static void    scriptflush(int);
 static void    record(FILE *, char *, size_t, int);
 static void    consume(FILE *, off_t, char *, int);
-static void    childwait(void);
 __dead static void     playback(FILE *);
 
 int
@@ -113,7 +113,7 @@
        quiet = 0;
        flush = 0;
        command = NULL;
-       while ((ch = getopt(argc, argv, "ac:dfpqr")) != -1)
+       while ((ch = getopt(argc, argv, "ac:defpqr")) != -1)
                switch(ch) {
                case 'a':
                        aflg = 1;
@@ -124,6 +124,9 @@
                case 'd':
                        usesleep = 0;
                        break;
+               case 'e':
+                       eflag = 1;
+                       break;
                case 'f':
                        flush = 1;
                        break;
@@ -139,7 +142,7 @@
                case '?':
                default:
                        (void)fprintf(stderr,
-                           "Usage: %s [-c <command>][-adfpqr] [file]\n",
+                           "Usage: %s [-c <command>][-adefpqr] [file]\n",
                            getprogname());
                        exit(EXIT_FAILURE);
                }
@@ -205,31 +208,29 @@
                        record(fscript, ibuf, cc, 'i');
                (void)write(master, ibuf, cc);
        }
-       childwait();
-       return EXIT_SUCCESS;
-}
-
-static void
-childwait(void)
-{
-       sigset_t set;
-
-       sigemptyset(&set);
-       sigsuspend(&set);
+       finish(-1);
 }
 
 static void
 finish(int signo)
 {
-       int die, pid, status;
+       int die, pid, status, cstat;
 
        die = 0;
-       while ((pid = wait3(&status, WNOHANG, 0)) > 0)
-               if (pid == child)
+       while ((pid = wait(&status)) > 0)
+               if (pid == child) {
+                       cstat = status;
                        die = 1;
+               }
 
-       if (die)
-               done();
+       if (!die)
+               return;
+       if (!eflag)
+               done(EXIT_SUCCESS);
+       else if (WIFEXITED(cstat))
+               done(WEXITSTATUS(cstat));
+       else
+               done(128 + WTERMSIG(cstat));
 }
 
 static void
@@ -267,8 +268,7 @@
                if (flush)
                        (void)fflush(fscript);
        }
-       childwait();
-       exit(EXIT_SUCCESS);
+       finish(-1);
 }
 
 static void
@@ -307,11 +307,11 @@
 {
 
        (void)kill(0, SIGTERM);
-       done();
+       done(EXIT_FAILURE);
 }
 
 static void
-done(void)
+done(int status)
 {
        time_t tvec;
 
@@ -330,7 +330,7 @@
                if (!quiet)
                        (void)printf("Script done, output file is %s\n", fname);
        }
-       exit(EXIT_SUCCESS);
+       exit(status);
 }
 
 static void



Home | Main Index | Thread Index | Old Index