CVS commit: src/usr.bin/script
Module Name:src Committed By: hgutch Date: Tue May 9 15:43:39 UTC 2023 Modified Files: src/usr.bin/script: script.c Log Message: Reset SIGCHLD handler to SIG_DFL. If run with "-c", both child and parent script processes will handle SIGCHLD resulting in a possible duplicate "Script done" line in the output. This fixes bin/54514. To generate a diff of this commit: cvs rdiff -u -r1.33 -r1.34 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.c diff -u src/usr.bin/script/script.c:1.33 src/usr.bin/script/script.c:1.34 --- src/usr.bin/script/script.c:1.33 Sun Feb 13 19:40:14 2022 +++ src/usr.bin/script/script.c Tue May 9 15:43:39 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: script.c,v 1.33 2022/02/13 19:40:14 christos Exp $ */ +/* $NetBSD: script.c,v 1.34 2023/05/09 15:43:39 hgutch 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.33 2022/02/13 19:40:14 christos Exp $"); +__RCSID("$NetBSD: script.c,v 1.34 2023/05/09 15:43:39 hgutch Exp $"); #endif /* not lint */ #include @@ -192,6 +192,7 @@ main(int argc, char *argv[]) fail(); } if (child == 0) { + (void)xsignal(SIGCHLD, SIG_DFL); subchild = child = fork(); if (child == -1) { warn("fork");
CVS commit: src/usr.bin/script
Module Name:src Committed By: hgutch Date: Tue May 9 15:43:39 UTC 2023 Modified Files: src/usr.bin/script: script.c Log Message: Reset SIGCHLD handler to SIG_DFL. If run with "-c", both child and parent script processes will handle SIGCHLD resulting in a possible duplicate "Script done" line in the output. This fixes bin/54514. To generate a diff of this commit: cvs rdiff -u -r1.33 -r1.34 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.
CVS commit: src/usr.bin/script
Module Name:src Committed By: christos Date: Sun Feb 13 19:40:14 UTC 2022 Modified Files: src/usr.bin/script: script.c Log Message: 1. restore the previous finish() logic to make: script -e -c /usr/bin/true script -e -c /usr/bin/false exit with the proper exit code. 2. handle system return value correctly (nabijaczleweli) 3. factor out the conversion of wait status -> shell return code. To generate a diff of this commit: cvs rdiff -u -r1.32 -r1.33 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.c diff -u src/usr.bin/script/script.c:1.32 src/usr.bin/script/script.c:1.33 --- src/usr.bin/script/script.c:1.32 Sat Feb 12 18:03:52 2022 +++ src/usr.bin/script/script.c Sun Feb 13 14:40:14 2022 @@ -1,4 +1,4 @@ -/* $NetBSD: script.c,v 1.32 2022/02/12 23:03:52 rillig Exp $ */ +/* $NetBSD: script.c,v 1.33 2022/02/13 19:40:14 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.32 2022/02/12 23:03:52 rillig Exp $"); +__RCSID("$NetBSD: script.c,v 1.33 2022/02/13 19:40:14 christos Exp $"); #endif /* not lint */ #include @@ -231,23 +231,30 @@ xsignal(int signo, sig_t handler) return osa.sa_handler; } +static int +getshellstatus(int status) +{ + if (WIFEXITED(status)) + return WEXITSTATUS(status); + if (WIFSIGNALED(status)) + return 128 + WTERMSIG(status); + return EXIT_FAILURE; +} + static void finish(int signo) { int pid, status; + die = 0; while ((pid = wait()) > 0) if (pid == child) { - cstat = status; die = 1; } - if (!eflag) - cstat = EXIT_SUCCESS; - else if (WIFEXITED(cstat)) - cstat = WEXITSTATUS(cstat); - else - cstat = 128 + WTERMSIG(cstat); + if (!die) + return; + done(eflag ? getshellstatus(status) : EXIT_SUCCESS); } static void @@ -312,8 +319,11 @@ doshell(const char *command) execl(shell, shell, "-i", NULL); warn("execl `%s'", shell); } else { - if (system(command) == -1) + int ret = system(command); + if (ret == -1) warn("system `%s'", command); + else + exit(eflag ? getshellstatus(ret) : EXIT_FAILURE); } fail();
CVS commit: src/usr.bin/script
Module Name:src Committed By: christos Date: Sun Feb 13 19:40:14 UTC 2022 Modified Files: src/usr.bin/script: script.c Log Message: 1. restore the previous finish() logic to make: script -e -c /usr/bin/true script -e -c /usr/bin/false exit with the proper exit code. 2. handle system return value correctly (nabijaczleweli) 3. factor out the conversion of wait status -> shell return code. To generate a diff of this commit: cvs rdiff -u -r1.32 -r1.33 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.
CVS commit: src/usr.bin/script
Module Name:src Committed By: rillig Date: Sat Feb 12 23:03:52 UTC 2022 Modified Files: src/usr.bin/script: script.c Log Message: script: declare dooutput as __dead This fixes the Clang build, which failed with: usr.bin/script/script.c:255:1: error: function 'dooutput' could be declared with attribute 'noreturn' [-Werror,-Wmissing-noreturn] To generate a diff of this commit: cvs rdiff -u -r1.31 -r1.32 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.
CVS commit: src/usr.bin/script
Module Name:src Committed By: rillig Date: Sat Feb 12 23:03:52 UTC 2022 Modified Files: src/usr.bin/script: script.c Log Message: script: declare dooutput as __dead This fixes the Clang build, which failed with: usr.bin/script/script.c:255:1: error: function 'dooutput' could be declared with attribute 'noreturn' [-Werror,-Wmissing-noreturn] To generate a diff of this commit: cvs rdiff -u -r1.31 -r1.32 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.c diff -u src/usr.bin/script/script.c:1.31 src/usr.bin/script/script.c:1.32 --- src/usr.bin/script/script.c:1.31 Fri Feb 11 21:15:25 2022 +++ src/usr.bin/script/script.c Sat Feb 12 23:03:52 2022 @@ -1,4 +1,4 @@ -/* $NetBSD: script.c,v 1.31 2022/02/11 21:15:25 christos Exp $ */ +/* $NetBSD: script.c,v 1.32 2022/02/12 23:03:52 rillig 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.31 2022/02/11 21:15:25 christos Exp $"); +__RCSID("$NetBSD: script.c,v 1.32 2022/02/12 23:03:52 rillig Exp $"); #endif /* not lint */ #include @@ -91,7 +91,7 @@ __dead static void done(int); __dead static void doshell(const char *); __dead static void fail(void); static sig_t xsignal(int, sig_t); -static void dooutput(void); +__dead static void dooutput(void); static void finish(int); static void scriptflush(int); static void record(FILE *, char *, size_t, int);
CVS commit: src/usr.bin/script
Module Name:src Committed By: christos Date: Fri Feb 11 21:15:25 UTC 2022 Modified Files: src/usr.bin/script: script.c Log Message: PR/56254: RVP: Don't call non-async-signal-safe functions from signal handlers. Establish a non-restart signal handler to avoid blocking in long I/Os. To generate a diff of this commit: cvs rdiff -u -r1.30 -r1.31 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.c diff -u src/usr.bin/script/script.c:1.30 src/usr.bin/script/script.c:1.31 --- src/usr.bin/script/script.c:1.30 Thu Jan 20 14:49:51 2022 +++ src/usr.bin/script/script.c Fri Feb 11 16:15:25 2022 @@ -1,4 +1,4 @@ -/* $NetBSD: script.c,v 1.30 2022/01/20 19:49:51 christos Exp $ */ +/* $NetBSD: script.c,v 1.31 2022/02/11 21:15:25 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.30 2022/01/20 19:49:51 christos Exp $"); +__RCSID("$NetBSD: script.c,v 1.31 2022/02/11 21:15:25 christos Exp $"); #endif /* not lint */ #include @@ -81,6 +81,8 @@ static int usesleep, rawout; static int quiet, flush; static const char *fname; +static volatile sig_atomic_t die = 0; /* exit if 1 */ +static int cstat = EXIT_SUCCESS; /* cmd. exit status */ static int eflag; static int isterm; static struct termios tt; @@ -88,6 +90,7 @@ static struct termios tt; __dead static void done(int); __dead static void doshell(const char *); __dead static void fail(void); +static sig_t xsignal(int, sig_t); static void dooutput(void); static void finish(int); static void scriptflush(int); @@ -182,7 +185,7 @@ main(int argc, char *argv[]) (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, ); } - (void)signal(SIGCHLD, finish); + (void)xsignal(SIGCHLD, finish); child = fork(); if (child == -1) { warn("fork"); @@ -202,35 +205,49 @@ main(int argc, char *argv[]) if (!rawout) (void)fclose(fscript); - while ((scc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0) { + while (!die && (scc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0) { cc = (size_t)scc; if (rawout) record(fscript, ibuf, cc, 'i'); (void)write(master, ibuf, cc); } - finish(-1); + done(cstat); +} + +/** + * wrapper around sigaction() because we want POSIX semantics: + * no auto-restarting of interrupted slow syscalls. + */ +static sig_t +xsignal(int signo, sig_t handler) +{ + struct sigaction sa, osa; + + sa.sa_handler = handler; + sa.sa_flags = 0; + sigemptyset(_mask); + if (sigaction(signo, , ) == -1) + return SIG_ERR; + return osa.sa_handler; } static void finish(int signo) { - int die, pid, status, cstat; + int pid, status; - die = 0; while ((pid = wait()) > 0) if (pid == child) { cstat = status; die = 1; } - if (!die) - return; if (!eflag) - done(EXIT_SUCCESS); + cstat = EXIT_SUCCESS; else if (WIFEXITED(cstat)) - done(WEXITSTATUS(cstat)); + cstat = WEXITSTATUS(cstat); else - done(128 + WTERMSIG(cstat)); + cstat = 128 + WTERMSIG(cstat); } static void @@ -268,7 +285,7 @@ dooutput(void) if (flush) (void)fflush(fscript); } - finish(-1); + done(cstat); } static void
CVS commit: src/usr.bin/script
Module Name:src Committed By: christos Date: Fri Feb 11 21:15:25 UTC 2022 Modified Files: src/usr.bin/script: script.c Log Message: PR/56254: RVP: Don't call non-async-signal-safe functions from signal handlers. Establish a non-restart signal handler to avoid blocking in long I/Os. To generate a diff of this commit: cvs rdiff -u -r1.30 -r1.31 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.
CVS commit: src/usr.bin/script
Module Name:src Committed By: christos Date: Thu Jan 20 19:49:51 UTC 2022 Modified Files: src/usr.bin/script: script.c Log Message: dooutput can return if finish() does not find a chile process. To generate a diff of this commit: cvs rdiff -u -r1.29 -r1.30 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.c diff -u src/usr.bin/script/script.c:1.29 src/usr.bin/script/script.c:1.30 --- src/usr.bin/script/script.c:1.29 Sun Jan 16 14:04:00 2022 +++ src/usr.bin/script/script.c Thu Jan 20 14:49:51 2022 @@ -1,4 +1,4 @@ -/* $NetBSD: script.c,v 1.29 2022/01/16 19:04:00 christos Exp $ */ +/* $NetBSD: script.c,v 1.30 2022/01/20 19:49:51 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.29 2022/01/16 19:04:00 christos Exp $"); +__RCSID("$NetBSD: script.c,v 1.30 2022/01/20 19:49:51 christos Exp $"); #endif /* not lint */ #include @@ -86,9 +86,9 @@ static int isterm; static struct termios tt; __dead static void done(int); -__dead static void dooutput(void); __dead static void doshell(const char *); __dead static void fail(void); +static void dooutput(void); static void finish(int); static void scriptflush(int); static void record(FILE *, char *, size_t, int);
CVS commit: src/usr.bin/script
Module Name:src Committed By: christos Date: Thu Jan 20 19:49:51 UTC 2022 Modified Files: src/usr.bin/script: script.c Log Message: dooutput can return if finish() does not find a chile process. To generate a diff of this commit: cvs rdiff -u -r1.29 -r1.30 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.
CVS commit: src/usr.bin/script
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 @@ -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(); - sigsuspend(); + finish(-1); } static void finish(int signo) { - int die, pid, status; + int die, pid, status, cstat; die = 0; - while ((pid = wait3(, WNOHANG, 0)) > 0) - if (pid == child) + while ((pid = wait()) > 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
CVS commit: src/usr.bin/script
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.
CVS commit: src/usr.bin/script
Module Name:src Committed By: christos Date: Mon Aug 31 15:32:16 UTC 2020 Modified Files: src/usr.bin/script: script.c Log Message: Unlike done() childwait() returns, found by clang. To generate a diff of this commit: cvs rdiff -u -r1.27 -r1.28 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.c diff -u src/usr.bin/script/script.c:1.27 src/usr.bin/script/script.c:1.28 --- src/usr.bin/script/script.c:1.27 Fri Aug 28 13:10:15 2020 +++ src/usr.bin/script/script.c Mon Aug 31 11:32:15 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: script.c,v 1.27 2020/08/28 17:10:15 christos Exp $ */ +/* $NetBSD: script.c,v 1.28 2020/08/31 15:32:15 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.27 2020/08/28 17:10:15 christos Exp $"); +__RCSID("$NetBSD: script.c,v 1.28 2020/08/31 15:32:15 christos Exp $"); #endif /* not lint */ #include @@ -206,7 +206,6 @@ main(int argc, char *argv[]) (void)write(master, ibuf, cc); } childwait(); - /* NOTREACHED */ return EXIT_SUCCESS; } @@ -269,6 +268,7 @@ dooutput(void) (void)fflush(fscript); } childwait(); + exit(EXIT_SUCCESS); } static void
CVS commit: src/usr.bin/script
Module Name:src Committed By: christos Date: Mon Aug 31 15:32:16 UTC 2020 Modified Files: src/usr.bin/script: script.c Log Message: Unlike done() childwait() returns, found by clang. To generate a diff of this commit: cvs rdiff -u -r1.27 -r1.28 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.
CVS commit: src/usr.bin/script
Module Name:src Committed By: christos Date: Fri Aug 28 17:10:15 UTC 2020 Modified Files: src/usr.bin/script: script.c Log Message: PR/55605: Soumendra Ganguly: Wait for child to finish when stdin is not a tty To generate a diff of this commit: cvs rdiff -u -r1.26 -r1.27 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.
CVS commit: src/usr.bin/script
Module Name:src Committed By: christos Date: Fri Aug 28 17:10:15 UTC 2020 Modified Files: src/usr.bin/script: script.c Log Message: PR/55605: Soumendra Ganguly: Wait for child to finish when stdin is not a tty To generate a diff of this commit: cvs rdiff -u -r1.26 -r1.27 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.c diff -u src/usr.bin/script/script.c:1.26 src/usr.bin/script/script.c:1.27 --- src/usr.bin/script/script.c:1.26 Sat Aug 8 12:01:35 2020 +++ src/usr.bin/script/script.c Fri Aug 28 13:10:15 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: script.c,v 1.26 2020/08/08 16:01:35 christos Exp $ */ +/* $NetBSD: script.c,v 1.27 2020/08/28 17:10:15 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.26 2020/08/08 16:01:35 christos Exp $"); +__RCSID("$NetBSD: script.c,v 1.27 2020/08/28 17:10:15 christos Exp $"); #endif /* not lint */ #include @@ -92,6 +92,7 @@ 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 @@ -204,12 +205,21 @@ main(int argc, char *argv[]) record(fscript, ibuf, cc, 'i'); (void)write(master, ibuf, cc); } - done(); + childwait(); /* NOTREACHED */ return EXIT_SUCCESS; } static void +childwait(void) +{ + sigset_t set; + + sigemptyset(); + sigsuspend(); +} + +static void finish(int signo) { int die, pid, status; @@ -249,7 +259,7 @@ dooutput(void) if (scc <= 0) break; cc = (size_t)scc; - (void)write(1, obuf, cc); + (void)write(STDOUT_FILENO, obuf, cc); if (rawout) record(fscript, obuf, cc, 'o'); else @@ -258,7 +268,7 @@ dooutput(void) if (flush) (void)fflush(fscript); } - done(); + childwait(); } static void
CVS commit: src/usr.bin/script
Module Name:src Committed By: christos Date: Sat Aug 8 16:01:35 UTC 2020 Modified Files: src/usr.bin/script: script.c Log Message: PR/1 Soumendra Ganguly: Simplify tcgetattr error handling. To generate a diff of this commit: cvs rdiff -u -r1.25 -r1.26 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.c diff -u src/usr.bin/script/script.c:1.25 src/usr.bin/script/script.c:1.26 --- src/usr.bin/script/script.c:1.25 Fri Aug 7 09:36:28 2020 +++ src/usr.bin/script/script.c Sat Aug 8 12:01:35 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: script.c,v 1.25 2020/08/07 13:36:28 christos Exp $ */ +/* $NetBSD: script.c,v 1.26 2020/08/08 16:01:35 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.25 2020/08/07 13:36:28 christos Exp $"); +__RCSID("$NetBSD: script.c,v 1.26 2020/08/08 16:01:35 christos Exp $"); #endif /* not lint */ #include @@ -158,16 +158,10 @@ main(int argc, char *argv[]) if (tcgetattr(STDIN_FILENO, ) == -1 || ioctl(STDIN_FILENO, TIOCGWINSZ, ) == -1) { - switch (errno) { - case ENOTTY: - if (openpty(, , NULL, NULL, NULL) == -1) -err(EXIT_FAILURE, "openpty"); - break; - case EBADF: - err(EXIT_FAILURE, "%d not valid fd", STDIN_FILENO); - default: /* errno == EFAULT or EINVAL for ioctl. Not reached in practice. */ - err(EXIT_FAILURE, "ioctl"); - } + if (errno != ENOTTY) /* For debugger. */ + err(EXIT_FAILURE, "tcgetattr/ioctl"); + if (openpty(, , NULL, NULL, NULL) == -1) + err(EXIT_FAILURE, "openpty"); } else { if (openpty(, , NULL, , ) == -1) err(EXIT_FAILURE, "openpty"); @@ -383,9 +377,8 @@ termset(void) struct termios traw; if (tcgetattr(STDOUT_FILENO, ) == -1) { - if (errno == EBADF) - err(EXIT_FAILURE, "%d not valid fd", STDOUT_FILENO); - /* errno == ENOTTY */ + if (errno != ENOTTY) /* For debugger. */ + err(EXIT_FAILURE, "tcgetattr"); return; } isterm = 1;
CVS commit: src/usr.bin/script
Module Name:src Committed By: christos Date: Sat Aug 8 16:01:35 UTC 2020 Modified Files: src/usr.bin/script: script.c Log Message: PR/1 Soumendra Ganguly: Simplify tcgetattr error handling. To generate a diff of this commit: cvs rdiff -u -r1.25 -r1.26 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.
CVS commit: src/usr.bin/script
Module Name:src Committed By: christos Date: Fri Aug 7 13:36:28 UTC 2020 Modified Files: src/usr.bin/script: script.c Log Message: PR/55548: Soumendra Ganguly: Since isatty(3) is implemented using tcgetattr(3), call it directly to avoid calling it twice. This makes error handling more precise. Also don't call err(3) when tcsetattr(3) fails. To generate a diff of this commit: cvs rdiff -u -r1.24 -r1.25 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.
CVS commit: src/usr.bin/script
Module Name:src Committed By: christos Date: Fri Aug 7 13:36:28 UTC 2020 Modified Files: src/usr.bin/script: script.c Log Message: PR/55548: Soumendra Ganguly: Since isatty(3) is implemented using tcgetattr(3), call it directly to avoid calling it twice. This makes error handling more precise. Also don't call err(3) when tcsetattr(3) fails. To generate a diff of this commit: cvs rdiff -u -r1.24 -r1.25 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.c diff -u src/usr.bin/script/script.c:1.24 src/usr.bin/script/script.c:1.25 --- src/usr.bin/script/script.c:1.24 Sun Aug 2 23:34:43 2020 +++ src/usr.bin/script/script.c Fri Aug 7 09:36:28 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: script.c,v 1.24 2020/08/03 03:34:43 christos Exp $ */ +/* $NetBSD: script.c,v 1.25 2020/08/07 13:36:28 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.24 2020/08/03 03:34:43 christos Exp $"); +__RCSID("$NetBSD: script.c,v 1.25 2020/08/07 13:36:28 christos Exp $"); #endif /* not lint */ #include @@ -156,17 +156,22 @@ main(int argc, char *argv[]) if (pflg) playback(fscript); - isterm = isatty(STDIN_FILENO); - if (isterm) { - if (tcgetattr(STDIN_FILENO, ) == -1) - err(EXIT_FAILURE, "tcgetattr"); - if (ioctl(STDIN_FILENO, TIOCGWINSZ, ) == -1) + if (tcgetattr(STDIN_FILENO, ) == -1 || + ioctl(STDIN_FILENO, TIOCGWINSZ, ) == -1) { + switch (errno) { + case ENOTTY: + if (openpty(, , NULL, NULL, NULL) == -1) +err(EXIT_FAILURE, "openpty"); + break; + case EBADF: + err(EXIT_FAILURE, "%d not valid fd", STDIN_FILENO); + default: /* errno == EFAULT or EINVAL for ioctl. Not reached in practice. */ err(EXIT_FAILURE, "ioctl"); - if (openpty(, , NULL, , ) == -1) - err(EXIT_FAILURE, "openpty"); + } } else { - if (openpty(, , NULL, NULL, NULL) == -1) + if (openpty(, , NULL, , ) == -1) err(EXIT_FAILURE, "openpty"); + isterm = 1; } if (!quiet) @@ -377,18 +382,17 @@ termset(void) { struct termios traw; - isterm = isatty(STDOUT_FILENO); - if (!isterm) + if (tcgetattr(STDOUT_FILENO, ) == -1) { + if (errno == EBADF) + err(EXIT_FAILURE, "%d not valid fd", STDOUT_FILENO); + /* errno == ENOTTY */ return; - - if (tcgetattr(STDOUT_FILENO, ) == -1) - err(EXIT_FAILURE, "tcgetattr"); - + } + isterm = 1; traw = tt; cfmakeraw(); traw.c_lflag |= ISIG; - if (tcsetattr(STDOUT_FILENO, TCSANOW, ) == -1) - err(EXIT_FAILURE, "tcsetattr"); +(void)tcsetattr(STDOUT_FILENO, TCSANOW, ); } static void
CVS commit: src/usr.bin/script
Module Name:src Committed By: christos Date: Mon Aug 3 03:34:43 UTC 2020 Modified Files: src/usr.bin/script: script.c Log Message: remove trailing whitespace To generate a diff of this commit: cvs rdiff -u -r1.23 -r1.24 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.c diff -u src/usr.bin/script/script.c:1.23 src/usr.bin/script/script.c:1.24 --- src/usr.bin/script/script.c:1.23 Sun Aug 2 12:23:33 2020 +++ src/usr.bin/script/script.c Sun Aug 2 23:34:43 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: script.c,v 1.23 2020/08/02 16:23:33 christos Exp $ */ +/* $NetBSD: script.c,v 1.24 2020/08/03 03:34:43 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.23 2020/08/02 16:23:33 christos Exp $"); +__RCSID("$NetBSD: script.c,v 1.24 2020/08/03 03:34:43 christos Exp $"); #endif /* not lint */ #include @@ -166,7 +166,7 @@ main(int argc, char *argv[]) err(EXIT_FAILURE, "openpty"); } else { if (openpty(, , NULL, NULL, NULL) == -1) - err(EXIT_FAILURE, "openpty"); + err(EXIT_FAILURE, "openpty"); } if (!quiet) @@ -200,7 +200,7 @@ main(int argc, char *argv[]) if (!rawout) (void)fclose(fscript); while ((scc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0) { - cc = (size_t)scc; + cc = (size_t)scc; if (rawout) record(fscript, ibuf, cc, 'i'); (void)write(master, ibuf, cc); @@ -413,7 +413,7 @@ playback(FILE *fp) int reg; if (fstat(fileno(fp), ) == -1) - err(EXIT_FAILURE, "fstat failed"); + err(EXIT_FAILURE, "fstat failed"); reg = S_ISREG(pst.st_mode);
CVS commit: src/usr.bin/script
Module Name:src Committed By: christos Date: Mon Aug 3 03:34:43 UTC 2020 Modified Files: src/usr.bin/script: script.c Log Message: remove trailing whitespace To generate a diff of this commit: cvs rdiff -u -r1.23 -r1.24 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.
CVS commit: src/usr.bin/script
Module Name:src Committed By: christos Date: Sun Aug 2 16:23:33 UTC 2020 Modified Files: src/usr.bin/script: script.c Log Message: PR/55531: Soumendra Ganguly: - add more error handling - handle -p when not a terminal - call termreset() before printing script done, so that it is printed correctly Also: - use ssize_t/size_t instead of int - use EXIT_SUCCESS/EXIT_FAILURE - check result of fork() against -1 To generate a diff of this commit: cvs rdiff -u -r1.22 -r1.23 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.c diff -u src/usr.bin/script/script.c:1.22 src/usr.bin/script/script.c:1.23 --- src/usr.bin/script/script.c:1.22 Sat Aug 1 13:31:06 2020 +++ src/usr.bin/script/script.c Sun Aug 2 12:23:33 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: script.c,v 1.22 2020/08/01 17:31:06 christos Exp $ */ +/* $NetBSD: script.c,v 1.23 2020/08/02 16:23:33 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.22 2020/08/01 17:31:06 christos Exp $"); +__RCSID("$NetBSD: script.c,v 1.23 2020/08/02 16:23:33 christos Exp $"); #endif /* not lint */ #include @@ -76,7 +76,7 @@ struct stamp { static FILE *fscript; static int master, slave; static int child, subchild; -static int outcc; +static size_t outcc; static int usesleep, rawout; static int quiet, flush; static const char *fname; @@ -97,7 +97,8 @@ __dead static void playback(FILE *); int main(int argc, char *argv[]) { - int cc; + ssize_t scc; + size_t cc; struct termios rtt; struct winsize win; int aflg, pflg, ch; @@ -139,7 +140,7 @@ main(int argc, char *argv[]) (void)fprintf(stderr, "Usage: %s [-c ][-adfpqr] [file]\n", getprogname()); - exit(1); + exit(EXIT_FAILURE); } argc -= optind; argv += optind; @@ -150,32 +151,43 @@ main(int argc, char *argv[]) fname = "typescript"; if ((fscript = fopen(fname, pflg ? "r" : aflg ? "a" : "w")) == NULL) - err(1, "fopen %s", fname); + err(EXIT_FAILURE, "fopen %s", fname); if (pflg) playback(fscript); - (void)tcgetattr(STDIN_FILENO, ); - (void)ioctl(STDIN_FILENO, TIOCGWINSZ, ); - if (openpty(, , NULL, , ) == -1) - err(1, "openpty"); + isterm = isatty(STDIN_FILENO); + if (isterm) { + if (tcgetattr(STDIN_FILENO, ) == -1) + err(EXIT_FAILURE, "tcgetattr"); + if (ioctl(STDIN_FILENO, TIOCGWINSZ, ) == -1) + err(EXIT_FAILURE, "ioctl"); + if (openpty(, , NULL, , ) == -1) + err(EXIT_FAILURE, "openpty"); + } else { + if (openpty(, , NULL, NULL, NULL) == -1) + err(EXIT_FAILURE, "openpty"); + } if (!quiet) (void)printf("Script started, output file is %s\n", fname); - rtt = tt; - cfmakeraw(); - rtt.c_lflag &= ~ECHO; - (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, ); + + if (isterm) { + rtt = tt; + cfmakeraw(); + rtt.c_lflag &= ~ECHO; + (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, ); + } (void)signal(SIGCHLD, finish); child = fork(); - if (child < 0) { + if (child == -1) { warn("fork"); fail(); } if (child == 0) { subchild = child = fork(); - if (child < 0) { + if (child == -1) { warn("fork"); fail(); } @@ -187,14 +199,15 @@ main(int argc, char *argv[]) if (!rawout) (void)fclose(fscript); - while ((cc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0) { + while ((scc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0) { + cc = (size_t)scc; if (rawout) record(fscript, ibuf, cc, 'i'); (void)write(master, ibuf, cc); } done(); /* NOTREACHED */ - return (0); + return EXIT_SUCCESS; } static void @@ -215,7 +228,8 @@ static void dooutput(void) { struct itimerval value; - int cc; + ssize_t scc; + size_t cc; time_t tvec; char obuf[BUFSIZ]; @@ -232,9 +246,10 @@ dooutput(void) value.it_value = value.it_interval; (void)setitimer(ITIMER_REAL, , NULL); for (;;) { - cc = read(master, obuf, sizeof (obuf)); - if (cc <= 0) + scc = read(master, obuf, sizeof(obuf)); + if (scc <= 0) break; + cc = (size_t)scc; (void)write(1, obuf, cc); if (rawout) record(fscript, obuf, cc, 'o'); @@ -301,11 +316,12 @@ done(void) (void)fclose(fscript); (void)close(master); } else { - (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, ); + if (isterm) + (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, ); if (!quiet) (void)printf("Script done, output file is %s\n", fname); } - exit(0); + exit(EXIT_SUCCESS); } static void @@ -325,7 +341,7 @@ record(FILE *fp, char *buf, size_t cc, i iov[1].iov_len = cc; iov[1].iov_base = buf; if (writev(fileno(fp), [0], 2) == -1) - err(1, "writev"); + err(EXIT_FAILURE, "writev"); } static void @@ -335,13 +351,13 @@ consume(FILE *fp, off_t len, char *buf, if (reg) { if (fseeko(fp, len, SEEK_CUR) == -1) - err(1, NULL); + err(EXIT_FAILURE, NULL);
CVS commit: src/usr.bin/script
Module Name:src Committed By: christos Date: Sun Aug 2 16:23:33 UTC 2020 Modified Files: src/usr.bin/script: script.c Log Message: PR/55531: Soumendra Ganguly: - add more error handling - handle -p when not a terminal - call termreset() before printing script done, so that it is printed correctly Also: - use ssize_t/size_t instead of int - use EXIT_SUCCESS/EXIT_FAILURE - check result of fork() against -1 To generate a diff of this commit: cvs rdiff -u -r1.22 -r1.23 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.
CVS commit: src/usr.bin/script
Module Name:src Committed By: christos Date: Sat Aug 1 17:31:06 UTC 2020 Modified Files: src/usr.bin/script: script.c Log Message: PR/55529: Soumendra Ganguly: configure the terminal in raw mode during playback so that output postprocessing is not done and playback of programs using curses does not appear corrupted. To generate a diff of this commit: cvs rdiff -u -r1.21 -r1.22 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.c diff -u src/usr.bin/script/script.c:1.21 src/usr.bin/script/script.c:1.22 --- src/usr.bin/script/script.c:1.21 Tue Sep 6 14:29:56 2011 +++ src/usr.bin/script/script.c Sat Aug 1 13:31:06 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: script.c,v 1.21 2011/09/06 18:29:56 joerg Exp $ */ +/* $NetBSD: script.c,v 1.22 2020/08/01 17:31:06 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.21 2011/09/06 18:29:56 joerg Exp $"); +__RCSID("$NetBSD: script.c,v 1.22 2020/08/01 17:31:06 christos Exp $"); #endif /* not lint */ #include @@ -81,6 +81,7 @@ static int usesleep, rawout; static int quiet, flush; static const char *fname; +static int isterm; static struct termios tt; __dead static void done(void); @@ -356,6 +357,34 @@ consume(FILE *fp, off_t len, char *buf, } while (0/*CONSTCOND*/) static void +termset(void) +{ + struct termios traw; + + isterm = isatty(STDOUT_FILENO); + if (!isterm) + return; + + if (tcgetattr(STDOUT_FILENO, ) != 0) + err(1, "tcgetattr"); + + traw = tt; + cfmakeraw(); + traw.c_lflag |= ISIG; + if (tcsetattr(STDOUT_FILENO, TCSANOW, ) != 0) + err(1, "tcsetattr"); +} + +static void +termreset(void) +{ + if (isterm) + tcsetattr(STDOUT_FILENO, TCSADRAIN, ); + + isterm = 0; +} + +static void playback(FILE *fp) { struct timespec tsi, tso; @@ -398,6 +427,8 @@ playback(FILE *fp) ctime()); tsi = tso; (void)consume(fp, stamp.scr_len, buf, reg); + termset(); + atexit(termreset); break; case 'e': if (!quiet)
CVS commit: src/usr.bin/script
Module Name:src Committed By: christos Date: Sat Aug 1 17:31:06 UTC 2020 Modified Files: src/usr.bin/script: script.c Log Message: PR/55529: Soumendra Ganguly: configure the terminal in raw mode during playback so that output postprocessing is not done and playback of programs using curses does not appear corrupted. To generate a diff of this commit: cvs rdiff -u -r1.21 -r1.22 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.
CVS commit: src/usr.bin/script
Module Name:src Committed By: joerg Date: Tue Sep 6 18:29:56 UTC 2011 Modified Files: src/usr.bin/script: script.c Log Message: static + __dead To generate a diff of this commit: cvs rdiff -u -r1.20 -r1.21 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.c diff -u src/usr.bin/script/script.c:1.20 src/usr.bin/script/script.c:1.21 --- src/usr.bin/script/script.c:1.20 Wed Jun 8 13:51:13 2011 +++ src/usr.bin/script/script.c Tue Sep 6 18:29:56 2011 @@ -1,4 +1,4 @@ -/* $NetBSD: script.c,v 1.20 2011/06/08 13:51:13 yamt Exp $ */ +/* $NetBSD: script.c,v 1.21 2011/09/06 18:29:56 joerg 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.20 2011/06/08 13:51:13 yamt Exp $); +__RCSID($NetBSD: script.c,v 1.21 2011/09/06 18:29:56 joerg Exp $); #endif /* not lint */ #include sys/types.h @@ -73,26 +73,25 @@ uint32_t scr_direction; /* 'i', 'o', etc (also indicates endianness) */ }; -FILE *fscript; -int master, slave; -int child, subchild; -int outcc; -int usesleep, rawout; -int quiet, flush; -const char *fname; - -struct termios tt; - -void done(void); -void dooutput(void); -void doshell(const char *); -void fail(void); -void finish(int); -int main(int, char **); -void scriptflush(int); -void record(FILE *, char *, size_t, int); -void consume(FILE *, off_t, char *, int); -void playback(FILE *); +static FILE *fscript; +static int master, slave; +static int child, subchild; +static int outcc; +static int usesleep, rawout; +static int quiet, flush; +static const char *fname; + +static struct termios tt; + +__dead static void done(void); +__dead static void dooutput(void); +__dead static void doshell(const char *); +__dead static void fail(void); +static void finish(int); +static void scriptflush(int); +static void record(FILE *, char *, size_t, int); +static void consume(FILE *, off_t, char *, int); +__dead static void playback(FILE *); int main(int argc, char *argv[]) @@ -197,7 +196,7 @@ return (0); } -void +static void finish(int signo) { int die, pid, status; @@ -211,7 +210,7 @@ done(); } -void +static void dooutput(void) { struct itimerval value; @@ -247,7 +246,7 @@ done(); } -void +static void scriptflush(int signo) { if (outcc) { @@ -256,7 +255,7 @@ } } -void +static void doshell(const char *command) { const char *shell; @@ -278,16 +277,16 @@ fail(); } -void -fail() +static void +fail(void) { (void)kill(0, SIGTERM); done(); } -void -done() +static void +done(void) { time_t tvec; @@ -308,7 +307,7 @@ exit(0); } -void +static void record(FILE *fp, char *buf, size_t cc, int direction) { struct iovec iov[2]; @@ -328,7 +327,7 @@ err(1, writev); } -void +static void consume(FILE *fp, off_t len, char *buf, int reg) { size_t l; @@ -356,7 +355,7 @@ } \ } while (0/*CONSTCOND*/) -void +static void playback(FILE *fp) { struct timespec tsi, tso;
CVS commit: src/usr.bin/script
Module Name:src Committed By: joerg Date: Tue Sep 6 18:29:56 UTC 2011 Modified Files: src/usr.bin/script: script.c Log Message: static + __dead To generate a diff of this commit: cvs rdiff -u -r1.20 -r1.21 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.
CVS commit: src/usr.bin/script
Module Name:src Committed By: yamt Date: Wed Jun 8 13:51:13 UTC 2011 Modified Files: src/usr.bin/script: script.c Log Message: err - errx where appropriate To generate a diff of this commit: cvs rdiff -u -r1.19 -r1.20 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.c diff -u src/usr.bin/script/script.c:1.19 src/usr.bin/script/script.c:1.20 --- src/usr.bin/script/script.c:1.19 Sat Oct 17 22:36:23 2009 +++ src/usr.bin/script/script.c Wed Jun 8 13:51:13 2011 @@ -1,4 +1,4 @@ -/* $NetBSD: script.c,v 1.19 2009/10/17 22:36:23 christos Exp $ */ +/* $NetBSD: script.c,v 1.20 2011/06/08 13:51:13 yamt 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.19 2009/10/17 22:36:23 christos Exp $); +__RCSID($NetBSD: script.c,v 1.20 2011/06/08 13:51:13 yamt Exp $); #endif /* not lint */ #include sys/types.h @@ -385,7 +385,7 @@ if (reg stamp.scr_len (uint64_t)(pst.st_size - save_len) - nread) - err(1, invalid stamp); + errx(1, invalid stamp); save_len += stamp.scr_len; tclock = stamp.scr_sec; @@ -430,7 +430,7 @@ } break; default: - err(1, invalid direction); + errx(1, invalid direction); } } (void)fclose(fp);
CVS commit: src/usr.bin/script
Module Name:src Committed By: yamt Date: Wed Jun 8 13:54:16 UTC 2011 Modified Files: src/usr.bin/script: script.1 Log Message: mention combinations of flags To generate a diff of this commit: cvs rdiff -u -r1.12 -r1.13 src/usr.bin/script/script.1 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.12 src/usr.bin/script/script.1:1.13 --- src/usr.bin/script/script.1:1.12 Sat Oct 17 22:28:33 2009 +++ src/usr.bin/script/script.1 Wed Jun 8 13:54:16 2011 @@ -1,4 +1,4 @@ -.\ $NetBSD: script.1,v 1.12 2009/10/17 22:28:33 wiz Exp $ +.\ $NetBSD: script.1,v 1.13 2011/06/08 13:54:16 yamt Exp $ .\ .\ Copyright (c) 1980, 1990, 1993 .\ The Regents of the University of California. All rights reserved. @@ -72,12 +72,16 @@ Useful for capturing the output of a program that behaves differently when associated with a tty. .It Fl d -Don't sleep between records when playing back a timestamped session. +When playing back a session with the +.Fl p +flag, don't sleep between records when playing back a timestamped session. .It Fl f Flush output after each write. This is useful for watching the script output in real time. .It Fl p -Play back a recorded session in real time. +Play back a session recorded with the +.Fl r +flag in real time. .It Fl q Be quiet, and don't output started and ended lines. .It Fl r
CVS commit: src/usr.bin/script
Module Name:src Committed By: yamt Date: Wed Jun 8 13:51:13 UTC 2011 Modified Files: src/usr.bin/script: script.c Log Message: err - errx where appropriate To generate a diff of this commit: cvs rdiff -u -r1.19 -r1.20 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.
CVS commit: src/usr.bin/script
Module Name:src Committed By: yamt Date: Wed Jun 8 13:54:16 UTC 2011 Modified Files: src/usr.bin/script: script.1 Log Message: mention combinations of flags To generate a diff of this commit: cvs rdiff -u -r1.12 -r1.13 src/usr.bin/script/script.1 Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
CVS commit: src/usr.bin/script
Module Name:src Committed By: christos Date: Sat Oct 17 19:05:55 UTC 2009 Modified Files: src/usr.bin/script: script.1 script.c Log Message: add more features found in other unices To generate a diff of this commit: cvs rdiff -u -r1.10 -r1.11 src/usr.bin/script/script.1 cvs rdiff -u -r1.17 -r1.18 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.10 src/usr.bin/script/script.1:1.11 --- src/usr.bin/script/script.1:1.10 Thu Aug 7 07:15:48 2003 +++ src/usr.bin/script/script.1 Sat Oct 17 15:05:54 2009 @@ -1,4 +1,4 @@ -.\ $NetBSD: script.1,v 1.10 2003/08/07 11:15:48 agc Exp $ +.\ $NetBSD: script.1,v 1.11 2009/10/17 19:05:54 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 June 6, 1993 +.Dd October 17, 2009 .Dt SCRIPT 1 .Os .Sh NAME @@ -37,7 +37,8 @@ .Nd make typescript of terminal session .Sh SYNOPSIS .Nm -.Op Fl adpr +.Op Fl adfpqr +.Op Fl c Ar command .Op Ar file .Sh DESCRIPTION .Nm @@ -64,10 +65,21 @@ or .Pa typescript , retaining the prior contents. +.It Fl c Ar command +Run the named +.Ar command +instead of the shell. +Useful for capturing the output of a program that behaves differently when +associated with a tty. .It Fl d Don't sleep between records when playing back a timestamped session. +.It Fl f +Flush output after each write. This is useful for watching the script +output in real time. .It Fl p Play back a recorded session in real time. +.It Fl q +Be quiet, and don't output started and ended lines. .It Fl r Record a session with input, output, and timestamping. .El Index: src/usr.bin/script/script.c diff -u src/usr.bin/script/script.c:1.17 src/usr.bin/script/script.c:1.18 --- src/usr.bin/script/script.c:1.17 Mon Apr 13 03:15:32 2009 +++ src/usr.bin/script/script.c Sat Oct 17 15:05:54 2009 @@ -1,4 +1,4 @@ -/* $NetBSD: script.c,v 1.17 2009/04/13 07:15:32 lukem Exp $ */ +/* $NetBSD: script.c,v 1.18 2009/10/17 19:05:54 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.17 2009/04/13 07:15:32 lukem Exp $); +__RCSID($NetBSD: script.c,v 1.18 2009/10/17 19:05:54 christos Exp $); #endif /* not lint */ #include sys/types.h @@ -78,13 +78,14 @@ int child, subchild; int outcc; int usesleep, rawout; +int quiet, flush; const char *fname; struct termios tt; void done(void); void dooutput(void); -void doshell(void); +void doshell(const char *); void fail(void); void finish(int); int main(int, char **); @@ -101,28 +102,42 @@ struct winsize win; int aflg, pflg, ch; char ibuf[BUFSIZ]; + const char *command; aflg = 0; pflg = 0; usesleep = 1; rawout = 0; - while ((ch = getopt(argc, argv, adpr)) != -1) + quiet = 0; + flush = 0; + command = NULL; + while ((ch = getopt(argc, argv, ac:dfpqr)) != -1) switch(ch) { case 'a': aflg = 1; break; + case 'c': + command = optarg; + break; case 'd': usesleep = 0; break; + case 'f': + flush = 1; + break; case 'p': pflg = 1; break; + case 'q': + quiet = 1; + break; case 'r': rawout = 1; break; case '?': default: - (void)fprintf(stderr, usage: %s [-adpr] [file]\n, + (void)fprintf(stderr, + Usage: %s [-c command][-adfpqr] [file]\n, getprogname()); exit(1); } @@ -145,7 +160,8 @@ if (openpty(master, slave, NULL, tt, win) == -1) err(1, openpty); - (void)printf(Script started, output file is %s\n, fname); + if (!quiet) + (void)printf(Script started, output file is %s\n, fname); rtt = tt; cfmakeraw(rtt); rtt.c_lflag = ~ECHO; @@ -166,7 +182,7 @@ if (child) dooutput(); else - doshell(); + doshell(command); } if (!rawout) @@ -196,7 +212,7 @@ } void -dooutput() +dooutput(void) { struct itimerval value; int cc; @@ -207,7 +223,7 @@ tvec = time(NULL); if (rawout) record(fscript, NULL, 0, 's'); - else + else if (!quiet) (void)fprintf(fscript, Script started on %s, ctime(tvec)); (void)signal(SIGALRM, scriptflush); @@ -225,6 +241,8 @@ else (void)fwrite(obuf, 1, cc, fscript); outcc += cc; + if (flush) + (void)fflush(fscript); } done(); } @@ -239,19 +257,23 @@ } void -doshell() +doshell(const char *command) { const char *shell; - shell = getenv(SHELL); - if (shell == NULL) - shell = _PATH_BSHELL; - (void)close(master); (void)fclose(fscript); login_tty(slave); - execl(shell, shell, -i, NULL); - warn(execl %s, shell); + if (command == NULL) { + shell = getenv(SHELL); + if (shell == NULL) + shell = _PATH_BSHELL; + execl(shell, shell, -i, NULL); + command = shell; + } else +
CVS commit: src/usr.bin/script
Module Name:src Committed By: wiz Date: Sat Oct 17 22:28:33 UTC 2009 Modified Files: src/usr.bin/script: script.1 Log Message: New sentence, new line. Remove trailing whitespace. To generate a diff of this commit: cvs rdiff -u -r1.11 -r1.12 src/usr.bin/script/script.1 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.11 src/usr.bin/script/script.1:1.12 --- src/usr.bin/script/script.1:1.11 Sat Oct 17 19:05:54 2009 +++ src/usr.bin/script/script.1 Sat Oct 17 22:28:33 2009 @@ -1,4 +1,4 @@ -.\ $NetBSD: script.1,v 1.11 2009/10/17 19:05:54 christos Exp $ +.\ $NetBSD: script.1,v 1.12 2009/10/17 22:28:33 wiz Exp $ .\ .\ Copyright (c) 1980, 1990, 1993 .\ The Regents of the University of California. All rights reserved. @@ -66,7 +66,7 @@ .Pa typescript , retaining the prior contents. .It Fl c Ar command -Run the named +Run the named .Ar command instead of the shell. Useful for capturing the output of a program that behaves differently when @@ -74,8 +74,8 @@ .It Fl d Don't sleep between records when playing back a timestamped session. .It Fl f -Flush output after each write. This is useful for watching the script -output in real time. +Flush output after each write. +This is useful for watching the script output in real time. .It Fl p Play back a recorded session in real time. .It Fl q @@ -116,10 +116,12 @@ .Ev SHELL exists, the shell forked by .Nm -will be that shell. If +will be that shell. +If .Ev SHELL is not set, the Bourne shell -is assumed. (Most shells set this variable automatically). +is assumed. +(Most shells set this variable automatically). .El .Sh SEE ALSO .Xr csh 1
CVS commit: src/usr.bin/script
Module Name:src Committed By: christos Date: Sat Oct 17 22:36:23 UTC 2009 Modified Files: src/usr.bin/script: script.c Log Message: use system to parse args To generate a diff of this commit: cvs rdiff -u -r1.18 -r1.19 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.c diff -u src/usr.bin/script/script.c:1.18 src/usr.bin/script/script.c:1.19 --- src/usr.bin/script/script.c:1.18 Sat Oct 17 15:05:54 2009 +++ src/usr.bin/script/script.c Sat Oct 17 18:36:23 2009 @@ -1,4 +1,4 @@ -/* $NetBSD: script.c,v 1.18 2009/10/17 19:05:54 christos Exp $ */ +/* $NetBSD: script.c,v 1.19 2009/10/17 22:36:23 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.18 2009/10/17 19:05:54 christos Exp $); +__RCSID($NetBSD: script.c,v 1.19 2009/10/17 22:36:23 christos Exp $); #endif /* not lint */ #include sys/types.h @@ -269,11 +269,12 @@ if (shell == NULL) shell = _PATH_BSHELL; execl(shell, shell, -i, NULL); - command = shell; - } else - execlp(command, command, NULL); + warn(execl `%s', shell); + } else { + if (system(command) == -1) + warn(system `%s', command); + } - warn(execl %s, command); fail(); }
CVS commit: src/usr.bin/script
Module Name:src Committed By: lukem Date: Mon Apr 13 07:15:32 UTC 2009 Modified Files: src/usr.bin/script: script.c Log Message: Fix WARNS=4 issues (-Wcast-qual -Wshadow) To generate a diff of this commit: cvs rdiff -u -r1.16 -r1.17 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.c diff -u src/usr.bin/script/script.c:1.16 src/usr.bin/script/script.c:1.17 --- src/usr.bin/script/script.c:1.16 Mon Jul 21 14:19:25 2008 +++ src/usr.bin/script/script.c Mon Apr 13 07:15:32 2009 @@ -1,4 +1,4 @@ -/* $NetBSD: script.c,v 1.16 2008/07/21 14:19:25 lukem Exp $ */ +/* $NetBSD: script.c,v 1.17 2009/04/13 07:15:32 lukem 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.16 2008/07/21 14:19:25 lukem Exp $); +__RCSID($NetBSD: script.c,v 1.17 2009/04/13 07:15:32 lukem Exp $); #endif /* not lint */ #include sys/types.h @@ -78,7 +78,7 @@ int child, subchild; int outcc; int usesleep, rawout; -char *fname; +const char *fname; struct termios tt; @@ -241,7 +241,7 @@ void doshell() { - char *shell; + const char *shell; shell = getenv(SHELL); if (shell == NULL) @@ -285,7 +285,7 @@ } void -record(FILE *fscript, char *buf, size_t cc, int direction) +record(FILE *fp, char *buf, size_t cc, int direction) { struct iovec iov[2]; struct stamp stamp; @@ -300,23 +300,23 @@ iov[0].iov_base = stamp; iov[1].iov_len = cc; iov[1].iov_base = buf; - if (writev(fileno(fscript), iov[0], 2) == -1) + if (writev(fileno(fp), iov[0], 2) == -1) err(1, writev); } void -consume(FILE *fscript, off_t len, char *buf, int reg) +consume(FILE *fp, off_t len, char *buf, int reg) { size_t l; if (reg) { - if (fseeko(fscript, len, SEEK_CUR) == -1) + if (fseeko(fp, len, SEEK_CUR) == -1) err(1, NULL); } else { while (len 0) { l = MIN(DEF_BUF, len); - if (fread(buf, sizeof(char), l, fscript) != l) + if (fread(buf, sizeof(char), l, fp) != l) err(1, cannot read buffer); len -= l; } @@ -333,7 +333,7 @@ } while (0/*CONSTCOND*/) void -playback(FILE *fscript) +playback(FILE *fp) { struct timespec tsi, tso; struct stamp stamp; @@ -341,16 +341,16 @@ char buf[DEF_BUF]; off_t nread, save_len; size_t l; - time_t clock; + time_t tclock; int reg; - if (fstat(fileno(fscript), pst) == -1) + if (fstat(fileno(fp), pst) == -1) err(1, fstat failed); reg = S_ISREG(pst.st_mode); for (nread = 0; !reg || nread pst.st_size; nread += save_len) { - if (fread(stamp, sizeof(stamp), 1, fscript) != 1) { + if (fread(stamp, sizeof(stamp), 1, fp) != 1) { if (reg) err(1, reading playback header); else @@ -364,23 +364,23 @@ err(1, invalid stamp); save_len += stamp.scr_len; - clock = stamp.scr_sec; + tclock = stamp.scr_sec; tso.tv_sec = stamp.scr_sec; tso.tv_nsec = stamp.scr_usec * 1000; switch (stamp.scr_direction) { case 's': - (void)printf(Script started on %s, ctime(clock)); + (void)printf(Script started on %s, ctime(tclock)); tsi = tso; - (void)consume(fscript, stamp.scr_len, buf, reg); + (void)consume(fp, stamp.scr_len, buf, reg); break; case 'e': - (void)printf(\nScript done on %s, ctime(clock)); - (void)consume(fscript, stamp.scr_len, buf, reg); + (void)printf(\nScript done on %s, ctime(tclock)); + (void)consume(fp, stamp.scr_len, buf, reg); break; case 'i': /* throw input away */ - (void)consume(fscript, stamp.scr_len, buf, reg); + (void)consume(fp, stamp.scr_len, buf, reg); break; case 'o': tsi.tv_sec = tso.tv_sec - tsi.tv_sec; @@ -394,7 +394,7 @@ tsi = tso; while (stamp.scr_len 0) { l = MIN(DEF_BUF, stamp.scr_len); -if (fread(buf, sizeof(char), l, fscript) != l) +if (fread(buf, sizeof(char), l, fp) != l) err(1, cannot read buffer); (void)write(STDOUT_FILENO, buf, l); @@ -405,6 +405,6 @@ err(1, invalid direction); } } - (void)fclose(fscript); + (void)fclose(fp); exit(0); }