19b50d902SRodney W. Grimes /* 29b50d902SRodney W. Grimes * Copyright (c) 1980, 1992, 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 139b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 149b50d902SRodney W. Grimes * must display the following acknowledgement: 159b50d902SRodney W. Grimes * This product includes software developed by the University of 169b50d902SRodney W. Grimes * California, Berkeley and its contributors. 179b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 189b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 199b50d902SRodney W. Grimes * without specific prior written permission. 209b50d902SRodney W. Grimes * 219b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 229b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 259b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319b50d902SRodney W. Grimes * SUCH DAMAGE. 329b50d902SRodney W. Grimes */ 339b50d902SRodney W. Grimes 34d20f95e1SMark Murray #include <sys/cdefs.h> 35d20f95e1SMark Murray 36d20f95e1SMark Murray __FBSDID("$FreeBSD$"); 37d20f95e1SMark Murray 389b50d902SRodney W. Grimes #ifndef lint 39236d2f55SPhilippe Charnier static const char copyright[] = 409b50d902SRodney W. Grimes "@(#) Copyright (c) 1980, 1992, 1993\n\ 419b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 42d20f95e1SMark Murray #endif 439b50d902SRodney W. Grimes 449b50d902SRodney W. Grimes #ifndef lint 45d20f95e1SMark Murray static const char sccsid[] = "@(#)script.c 8.1 (Berkeley) 6/6/93"; 46236d2f55SPhilippe Charnier #endif 479b50d902SRodney W. Grimes 489b50d902SRodney W. Grimes #include <sys/types.h> 499b50d902SRodney W. Grimes #include <sys/wait.h> 509b50d902SRodney W. Grimes #include <sys/stat.h> 519b50d902SRodney W. Grimes #include <sys/ioctl.h> 529b50d902SRodney W. Grimes #include <sys/time.h> 539b50d902SRodney W. Grimes 54236d2f55SPhilippe Charnier #include <err.h> 55e8eb82a8SPeter Wemm #include <errno.h> 569b50d902SRodney W. Grimes #include <fcntl.h> 57236d2f55SPhilippe Charnier #include <libutil.h> 589b50d902SRodney W. Grimes #include <paths.h> 599b50d902SRodney W. Grimes #include <signal.h> 609b50d902SRodney W. Grimes #include <stdio.h> 619b50d902SRodney W. Grimes #include <stdlib.h> 629b50d902SRodney W. Grimes #include <string.h> 639b50d902SRodney W. Grimes #include <termios.h> 649b50d902SRodney W. Grimes #include <unistd.h> 659b50d902SRodney W. Grimes 66e8efeec6SEd Schouten static FILE *fscript; 67e8efeec6SEd Schouten static int master, slave; 68e8efeec6SEd Schouten static int child; 69e8efeec6SEd Schouten static const char *fname; 70e8efeec6SEd Schouten static int qflg, ttyflg; 719b50d902SRodney W. Grimes 72e8efeec6SEd Schouten static struct termios tt; 739b50d902SRodney W. Grimes 74e8efeec6SEd Schouten static void done(int) __dead2; 75e8efeec6SEd Schouten static void doshell(char **); 76e8efeec6SEd Schouten static void fail(void); 77e8efeec6SEd Schouten static void finish(void); 783f330d7dSWarner Losh static void usage(void); 799b50d902SRodney W. Grimes 809b50d902SRodney W. Grimes int 81f4ac32deSDavid Malone main(int argc, char *argv[]) 829b50d902SRodney W. Grimes { 83d20f95e1SMark Murray int cc; 84e8eb82a8SPeter Wemm struct termios rtt, stt; 859b50d902SRodney W. Grimes struct winsize win; 86e8eb82a8SPeter Wemm int aflg, kflg, ch, n; 87e8eb82a8SPeter Wemm struct timeval tv, *tvp; 88e8eb82a8SPeter Wemm time_t tvec, start; 89e8eb82a8SPeter Wemm char obuf[BUFSIZ]; 909b50d902SRodney W. Grimes char ibuf[BUFSIZ]; 91e8eb82a8SPeter Wemm fd_set rfd; 92e8eb82a8SPeter Wemm int flushtime = 30; 939b50d902SRodney W. Grimes 94e8eb82a8SPeter Wemm aflg = kflg = 0; 95e8eb82a8SPeter Wemm while ((ch = getopt(argc, argv, "aqkt:")) != -1) 969b50d902SRodney W. Grimes switch(ch) { 979b50d902SRodney W. Grimes case 'a': 989b50d902SRodney W. Grimes aflg = 1; 999b50d902SRodney W. Grimes break; 10051afb8dfSPeter Wemm case 'q': 1010e604e98SPeter Wemm qflg = 1; 10251afb8dfSPeter Wemm break; 103e8eb82a8SPeter Wemm case 'k': 104e8eb82a8SPeter Wemm kflg = 1; 105e8eb82a8SPeter Wemm break; 106e8eb82a8SPeter Wemm case 't': 107e8eb82a8SPeter Wemm flushtime = atoi(optarg); 108e8eb82a8SPeter Wemm if (flushtime < 0) 109e8eb82a8SPeter Wemm err(1, "invalid flush time %d", flushtime); 110e8eb82a8SPeter Wemm break; 1119b50d902SRodney W. Grimes case '?': 1129b50d902SRodney W. Grimes default: 113236d2f55SPhilippe Charnier usage(); 1149b50d902SRodney W. Grimes } 1159b50d902SRodney W. Grimes argc -= optind; 1169b50d902SRodney W. Grimes argv += optind; 1179b50d902SRodney W. Grimes 11851afb8dfSPeter Wemm if (argc > 0) { 1199b50d902SRodney W. Grimes fname = argv[0]; 12051afb8dfSPeter Wemm argv++; 12151afb8dfSPeter Wemm argc--; 12251afb8dfSPeter Wemm } else 1239b50d902SRodney W. Grimes fname = "typescript"; 1249b50d902SRodney W. Grimes 1259b50d902SRodney W. Grimes if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL) 126236d2f55SPhilippe Charnier err(1, "%s", fname); 1279b50d902SRodney W. Grimes 128d93708c3SEd Schouten if ((ttyflg = isatty(STDIN_FILENO)) != 0) { 129e292a0e3SColin Percival if (tcgetattr(STDIN_FILENO, &tt) == -1) 130e292a0e3SColin Percival err(1, "tcgetattr"); 131e292a0e3SColin Percival if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1) 132e292a0e3SColin Percival err(1, "ioctl"); 1339b50d902SRodney W. Grimes if (openpty(&master, &slave, NULL, &tt, &win) == -1) 134236d2f55SPhilippe Charnier err(1, "openpty"); 135e292a0e3SColin Percival } else { 136e292a0e3SColin Percival if (openpty(&master, &slave, NULL, NULL, NULL) == -1) 137e292a0e3SColin Percival err(1, "openpty"); 138e292a0e3SColin Percival } 1399b50d902SRodney W. Grimes 140e8eb82a8SPeter Wemm if (!qflg) { 141e8eb82a8SPeter Wemm tvec = time(NULL); 1429b50d902SRodney W. Grimes (void)printf("Script started, output file is %s\n", fname); 143e8eb82a8SPeter Wemm (void)fprintf(fscript, "Script started on %s", ctime(&tvec)); 144e8eb82a8SPeter Wemm fflush(fscript); 145e8eb82a8SPeter Wemm } 146e292a0e3SColin Percival if (ttyflg) { 1479b50d902SRodney W. Grimes rtt = tt; 1489b50d902SRodney W. Grimes cfmakeraw(&rtt); 1499b50d902SRodney W. Grimes rtt.c_lflag &= ~ECHO; 1509b50d902SRodney W. Grimes (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt); 151e292a0e3SColin Percival } 1529b50d902SRodney W. Grimes 1539b50d902SRodney W. Grimes child = fork(); 1549b50d902SRodney W. Grimes if (child < 0) { 155236d2f55SPhilippe Charnier warn("fork"); 156b1c66970SDag-Erling Smørgrav done(1); 1579b50d902SRodney W. Grimes } 158e8eb82a8SPeter Wemm if (child == 0) 15951afb8dfSPeter Wemm doshell(argv); 1608d5e4a14SEd Schouten close(slave); 1619b50d902SRodney W. Grimes 162e8eb82a8SPeter Wemm if (flushtime > 0) 163e8eb82a8SPeter Wemm tvp = &tv; 164e8eb82a8SPeter Wemm else 165e8eb82a8SPeter Wemm tvp = NULL; 166e8eb82a8SPeter Wemm 167e8eb82a8SPeter Wemm start = time(0); 168e8eb82a8SPeter Wemm FD_ZERO(&rfd); 169e8eb82a8SPeter Wemm for (;;) { 170e8eb82a8SPeter Wemm FD_SET(master, &rfd); 171e8eb82a8SPeter Wemm FD_SET(STDIN_FILENO, &rfd); 172e8eb82a8SPeter Wemm if (flushtime > 0) { 173e8eb82a8SPeter Wemm tv.tv_sec = flushtime; 174e8eb82a8SPeter Wemm tv.tv_usec = 0; 175e8eb82a8SPeter Wemm } 176e8eb82a8SPeter Wemm n = select(master + 1, &rfd, 0, 0, tvp); 177e8eb82a8SPeter Wemm if (n < 0 && errno != EINTR) 178e8eb82a8SPeter Wemm break; 179e8eb82a8SPeter Wemm if (n > 0 && FD_ISSET(STDIN_FILENO, &rfd)) { 180e8eb82a8SPeter Wemm cc = read(STDIN_FILENO, ibuf, BUFSIZ); 181d6a68195SColin Percival if (cc < 0) 182e8eb82a8SPeter Wemm break; 183d6a68195SColin Percival if (cc == 0) 184d6a68195SColin Percival (void)write(master, ibuf, 0); 185e8eb82a8SPeter Wemm if (cc > 0) { 1869b50d902SRodney W. Grimes (void)write(master, ibuf, cc); 187e8eb82a8SPeter Wemm if (kflg && tcgetattr(master, &stt) >= 0 && 188e8eb82a8SPeter Wemm ((stt.c_lflag & ECHO) == 0)) { 189e8eb82a8SPeter Wemm (void)fwrite(ibuf, 1, cc, fscript); 190e8eb82a8SPeter Wemm } 191e8eb82a8SPeter Wemm } 192e8eb82a8SPeter Wemm } 193e8eb82a8SPeter Wemm if (n > 0 && FD_ISSET(master, &rfd)) { 194e8eb82a8SPeter Wemm cc = read(master, obuf, sizeof (obuf)); 195e8eb82a8SPeter Wemm if (cc <= 0) 196e8eb82a8SPeter Wemm break; 197e1b4d8d0SSheldon Hearn (void)write(STDOUT_FILENO, obuf, cc); 198e8eb82a8SPeter Wemm (void)fwrite(obuf, 1, cc, fscript); 199e8eb82a8SPeter Wemm } 200e8eb82a8SPeter Wemm tvec = time(0); 201e8eb82a8SPeter Wemm if (tvec - start >= flushtime) { 202e8eb82a8SPeter Wemm fflush(fscript); 203e8eb82a8SPeter Wemm start = tvec; 204e8eb82a8SPeter Wemm } 205e8eb82a8SPeter Wemm } 206b1c66970SDag-Erling Smørgrav finish(); 207b1c66970SDag-Erling Smørgrav done(0); 2089b50d902SRodney W. Grimes } 2099b50d902SRodney W. Grimes 210236d2f55SPhilippe Charnier static void 211f4ac32deSDavid Malone usage(void) 212236d2f55SPhilippe Charnier { 213e8eb82a8SPeter Wemm (void)fprintf(stderr, 2144294ff16SSheldon Hearn "usage: script [-akq] [-t time] [file [command ...]]\n"); 215236d2f55SPhilippe Charnier exit(1); 216236d2f55SPhilippe Charnier } 217236d2f55SPhilippe Charnier 218e8efeec6SEd Schouten static void 219f4ac32deSDavid Malone finish(void) 2209b50d902SRodney W. Grimes { 221e1e9ba33SEd Schouten int e, status; 2229b50d902SRodney W. Grimes 223e1e9ba33SEd Schouten if (waitpid(child, &status, 0) == child) { 224b1c66970SDag-Erling Smørgrav if (WIFEXITED(status)) 225b1c66970SDag-Erling Smørgrav e = WEXITSTATUS(status); 226b1c66970SDag-Erling Smørgrav else if (WIFSIGNALED(status)) 227b1c66970SDag-Erling Smørgrav e = WTERMSIG(status); 228b1c66970SDag-Erling Smørgrav else /* can't happen */ 229b1c66970SDag-Erling Smørgrav e = 1; 230b1c66970SDag-Erling Smørgrav done(e); 2319b50d902SRodney W. Grimes } 232e1e9ba33SEd Schouten } 2339b50d902SRodney W. Grimes 234e8efeec6SEd Schouten static void 235f4ac32deSDavid Malone doshell(char **av) 2369b50d902SRodney W. Grimes { 237b139689cSDavid Malone const char *shell; 238*9b91846cSDavid E. O'Brien int k; 2399b50d902SRodney W. Grimes 2409b50d902SRodney W. Grimes shell = getenv("SHELL"); 2419b50d902SRodney W. Grimes if (shell == NULL) 2429b50d902SRodney W. Grimes shell = _PATH_BSHELL; 2439b50d902SRodney W. Grimes 244*9b91846cSDavid E. O'Brien if (av[0]) 245*9b91846cSDavid E. O'Brien for (k = 0 ; av[k] ; ++k) 246*9b91846cSDavid E. O'Brien fprintf(fscript, "%s%s", k ? " " : "", av[k]); 247*9b91846cSDavid E. O'Brien fprintf(fscript, "\r\n"); 248*9b91846cSDavid E. O'Brien 2499b50d902SRodney W. Grimes (void)close(master); 2509b50d902SRodney W. Grimes (void)fclose(fscript); 2519b50d902SRodney W. Grimes login_tty(slave); 252*9b91846cSDavid E. O'Brien setenv("SCRIPT", fname, 1); 253b1c66970SDag-Erling Smørgrav if (av[0]) { 2540e604e98SPeter Wemm execvp(av[0], av); 255b7ffba17SKris Kennaway warn("%s", av[0]); 256b1c66970SDag-Erling Smørgrav } else { 2577bc6d015SBrian Somers execl(shell, shell, "-i", (char *)NULL); 258b7ffba17SKris Kennaway warn("%s", shell); 259b1c66970SDag-Erling Smørgrav } 2609b50d902SRodney W. Grimes fail(); 2619b50d902SRodney W. Grimes } 2629b50d902SRodney W. Grimes 263e8efeec6SEd Schouten static void 264f4ac32deSDavid Malone fail(void) 2659b50d902SRodney W. Grimes { 2669b50d902SRodney W. Grimes (void)kill(0, SIGTERM); 267b1c66970SDag-Erling Smørgrav done(1); 2689b50d902SRodney W. Grimes } 2699b50d902SRodney W. Grimes 270e8efeec6SEd Schouten static void 271f4ac32deSDavid Malone done(int eno) 2729b50d902SRodney W. Grimes { 2739b50d902SRodney W. Grimes time_t tvec; 2749b50d902SRodney W. Grimes 275e292a0e3SColin Percival if (ttyflg) 276e8eb82a8SPeter Wemm (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt); 2779b50d902SRodney W. Grimes tvec = time(NULL); 278e8eb82a8SPeter Wemm if (!qflg) { 2799b50d902SRodney W. Grimes (void)fprintf(fscript,"\nScript done on %s", ctime(&tvec)); 280e8eb82a8SPeter Wemm (void)printf("\nScript done, output file is %s\n", fname); 281e8eb82a8SPeter Wemm } 2829b50d902SRodney W. Grimes (void)fclose(fscript); 2839b50d902SRodney W. Grimes (void)close(master); 284b1c66970SDag-Erling Smørgrav exit(eno); 2859b50d902SRodney W. Grimes } 286