Module Name:    src
Committed By:   christos
Date:           Sun Jan 16 19:04:00 UTC 2022

Modified Files:
        src/usr.bin/script: script.1 script.c

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


To generate a diff of this commit:
cvs rdiff -u -r1.13 -r1.14 src/usr.bin/script/script.1
cvs rdiff -u -r1.28 -r1.29 src/usr.bin/script/script.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/script/script.1
diff -u src/usr.bin/script/script.1:1.13 src/usr.bin/script/script.1:1.14
--- src/usr.bin/script/script.1:1.13	Wed Jun  8 09:54:16 2011
+++ src/usr.bin/script/script.1	Sun Jan 16 14:04:00 2022
@@ -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.

Index: src/usr.bin/script/script.c
diff -u src/usr.bin/script/script.c:1.28 src/usr.bin/script/script.c:1.29
--- src/usr.bin/script/script.c:1.28	Mon Aug 31 11:32:15 2020
+++ src/usr.bin/script/script.c	Sun Jan 16 14:04:00 2022
@@ -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, 19
 #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	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 <command>][-adfpqr] [file]\n",
+			    "Usage: %s [-c <command>][-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

Reply via email to