From eaea20be3fc0bee6acb49bea73b8728ab885b74d Mon Sep 17 00:00:00 2001 From: christos Date: Sun, 16 Jan 2022 19:04:00 +0000 Subject: [PATCH] If -e is specified exit with the exit status of the child process in a shell-like format (nabijaczleweli) --- usr.bin/script/script.1 | 10 +++++--- usr.bin/script/script.c | 54 ++++++++++++++++++++--------------------- 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/usr.bin/script/script.1 b/usr.bin/script/script.1 index 51c76d817391..76503d5300f0 100644 --- a/usr.bin/script/script.1 +++ b/usr.bin/script/script.1 @@ -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 @@ associated with a tty. 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 --git a/usr.bin/script/script.c b/usr.bin/script/script.c index 0c60922d0d8f..62536155886a 100644 --- a/usr.bin/script/script.c +++ b/usr.bin/script/script.c @@ -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 @@ __COPYRIGHT("@(#) Copyright (c) 1980, 1992, 1993\ #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 @@ -81,10 +81,11 @@ static int usesleep, rawout; 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 finish(int); 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 @@ main(int argc, char *argv[]) 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 @@ main(int argc, char *argv[]) case 'd': usesleep = 0; break; + case 'e': + eflag = 1; + break; case 'f': flush = 1; break; @@ -139,7 +142,7 @@ main(int argc, char *argv[]) case '?': default: (void)fprintf(stderr, - "Usage: %s [-c ][-adfpqr] [file]\n", + "Usage: %s [-c ][-adefpqr] [file]\n", getprogname()); exit(EXIT_FAILURE); } @@ -205,31 +208,29 @@ main(int argc, char *argv[]) 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 @@ dooutput(void) if (flush) (void)fflush(fscript); } - childwait(); - exit(EXIT_SUCCESS); + finish(-1); } static void @@ -307,11 +307,11 @@ fail(void) { (void)kill(0, SIGTERM); - done(); + done(EXIT_FAILURE); } static void -done(void) +done(int status) { time_t tvec; @@ -330,7 +330,7 @@ done(void) if (!quiet) (void)printf("Script done, output file is %s\n", fname); } - exit(EXIT_SUCCESS); + exit(status); } static void