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 349b50d902SRodney W. Grimes #ifndef lint 35236d2f55SPhilippe Charnier static const char copyright[] = 369b50d902SRodney W. Grimes "@(#) Copyright (c) 1980, 1992, 1993\n\ 379b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 389b50d902SRodney W. Grimes #endif /* not lint */ 399b50d902SRodney W. Grimes 409b50d902SRodney W. Grimes #ifndef lint 41236d2f55SPhilippe Charnier #if 0 429b50d902SRodney W. Grimes static char sccsid[] = "@(#)script.c 8.1 (Berkeley) 6/6/93"; 43236d2f55SPhilippe Charnier #endif 44236d2f55SPhilippe Charnier static const char rcsid[] = 45c3aac50fSPeter Wemm "$FreeBSD$"; 469b50d902SRodney W. Grimes #endif /* not lint */ 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 669b50d902SRodney W. Grimes FILE *fscript; 679b50d902SRodney W. Grimes int master, slave; 68e8eb82a8SPeter Wemm int child; 699b50d902SRodney W. Grimes char *fname; 7051afb8dfSPeter Wemm int qflg; 719b50d902SRodney W. Grimes 729b50d902SRodney W. Grimes struct termios tt; 739b50d902SRodney W. Grimes 74b1c66970SDag-Erling Smørgrav void done __P((int)) __dead2; 759b50d902SRodney W. Grimes void dooutput __P((void)); 7651afb8dfSPeter Wemm void doshell __P((char **)); 779b50d902SRodney W. Grimes void fail __P((void)); 78b1c66970SDag-Erling Smørgrav void finish __P((void)); 79236d2f55SPhilippe Charnier static void usage __P((void)); 809b50d902SRodney W. Grimes 819b50d902SRodney W. Grimes int 829b50d902SRodney W. Grimes main(argc, argv) 839b50d902SRodney W. Grimes int argc; 849b50d902SRodney W. Grimes char *argv[]; 859b50d902SRodney W. Grimes { 869b50d902SRodney W. Grimes register int cc; 87e8eb82a8SPeter Wemm struct termios rtt, stt; 889b50d902SRodney W. Grimes struct winsize win; 89e8eb82a8SPeter Wemm int aflg, kflg, ch, n; 90e8eb82a8SPeter Wemm struct timeval tv, *tvp; 91e8eb82a8SPeter Wemm time_t tvec, start; 92e8eb82a8SPeter Wemm char obuf[BUFSIZ]; 939b50d902SRodney W. Grimes char ibuf[BUFSIZ]; 94e8eb82a8SPeter Wemm fd_set rfd; 95e8eb82a8SPeter Wemm int flushtime = 30; 969b50d902SRodney W. Grimes 97e8eb82a8SPeter Wemm aflg = kflg = 0; 98e8eb82a8SPeter Wemm while ((ch = getopt(argc, argv, "aqkt:")) != -1) 999b50d902SRodney W. Grimes switch(ch) { 1009b50d902SRodney W. Grimes case 'a': 1019b50d902SRodney W. Grimes aflg = 1; 1029b50d902SRodney W. Grimes break; 10351afb8dfSPeter Wemm case 'q': 1040e604e98SPeter Wemm qflg = 1; 10551afb8dfSPeter Wemm break; 106e8eb82a8SPeter Wemm case 'k': 107e8eb82a8SPeter Wemm kflg = 1; 108e8eb82a8SPeter Wemm break; 109e8eb82a8SPeter Wemm case 't': 110e8eb82a8SPeter Wemm flushtime = atoi(optarg); 111e8eb82a8SPeter Wemm if (flushtime < 0) 112e8eb82a8SPeter Wemm err(1, "invalid flush time %d", flushtime); 113e8eb82a8SPeter Wemm break; 1149b50d902SRodney W. Grimes case '?': 1159b50d902SRodney W. Grimes default: 116236d2f55SPhilippe Charnier usage(); 1179b50d902SRodney W. Grimes } 1189b50d902SRodney W. Grimes argc -= optind; 1199b50d902SRodney W. Grimes argv += optind; 1209b50d902SRodney W. Grimes 12151afb8dfSPeter Wemm if (argc > 0) { 1229b50d902SRodney W. Grimes fname = argv[0]; 12351afb8dfSPeter Wemm argv++; 12451afb8dfSPeter Wemm argc--; 12551afb8dfSPeter Wemm } else 1269b50d902SRodney W. Grimes fname = "typescript"; 1279b50d902SRodney W. Grimes 1289b50d902SRodney W. Grimes if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL) 129236d2f55SPhilippe Charnier err(1, "%s", fname); 1309b50d902SRodney W. Grimes 1319b50d902SRodney W. Grimes (void)tcgetattr(STDIN_FILENO, &tt); 1329b50d902SRodney W. Grimes (void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win); 1339b50d902SRodney W. Grimes if (openpty(&master, &slave, NULL, &tt, &win) == -1) 134236d2f55SPhilippe Charnier err(1, "openpty"); 1359b50d902SRodney W. Grimes 136e8eb82a8SPeter Wemm if (!qflg) { 137e8eb82a8SPeter Wemm tvec = time(NULL); 1389b50d902SRodney W. Grimes (void)printf("Script started, output file is %s\n", fname); 139e8eb82a8SPeter Wemm (void)fprintf(fscript, "Script started on %s", ctime(&tvec)); 140e8eb82a8SPeter Wemm fflush(fscript); 141e8eb82a8SPeter Wemm } 1429b50d902SRodney W. Grimes rtt = tt; 1439b50d902SRodney W. Grimes cfmakeraw(&rtt); 1449b50d902SRodney W. Grimes rtt.c_lflag &= ~ECHO; 1459b50d902SRodney W. Grimes (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt); 1469b50d902SRodney W. Grimes 1479b50d902SRodney W. Grimes child = fork(); 1489b50d902SRodney W. Grimes if (child < 0) { 149236d2f55SPhilippe Charnier warn("fork"); 150b1c66970SDag-Erling Smørgrav done(1); 1519b50d902SRodney W. Grimes } 152e8eb82a8SPeter Wemm if (child == 0) 15351afb8dfSPeter Wemm doshell(argv); 1549b50d902SRodney W. Grimes 155e8eb82a8SPeter Wemm if (flushtime > 0) 156e8eb82a8SPeter Wemm tvp = &tv; 157e8eb82a8SPeter Wemm else 158e8eb82a8SPeter Wemm tvp = NULL; 159e8eb82a8SPeter Wemm 160e8eb82a8SPeter Wemm start = time(0); 161e8eb82a8SPeter Wemm FD_ZERO(&rfd); 162e8eb82a8SPeter Wemm for (;;) { 163e8eb82a8SPeter Wemm FD_SET(master, &rfd); 164e8eb82a8SPeter Wemm FD_SET(STDIN_FILENO, &rfd); 165e8eb82a8SPeter Wemm if (flushtime > 0) { 166e8eb82a8SPeter Wemm tv.tv_sec = flushtime; 167e8eb82a8SPeter Wemm tv.tv_usec = 0; 168e8eb82a8SPeter Wemm } 169e8eb82a8SPeter Wemm n = select(master + 1, &rfd, 0, 0, tvp); 170e8eb82a8SPeter Wemm if (n < 0 && errno != EINTR) 171e8eb82a8SPeter Wemm break; 172e8eb82a8SPeter Wemm if (n > 0 && FD_ISSET(STDIN_FILENO, &rfd)) { 173e8eb82a8SPeter Wemm cc = read(STDIN_FILENO, ibuf, BUFSIZ); 174e8eb82a8SPeter Wemm if (cc <= 0) 175e8eb82a8SPeter Wemm break; 176e8eb82a8SPeter Wemm if (cc > 0) { 1779b50d902SRodney W. Grimes (void)write(master, ibuf, cc); 178e8eb82a8SPeter Wemm if (kflg && tcgetattr(master, &stt) >= 0 && 179e8eb82a8SPeter Wemm ((stt.c_lflag & ECHO) == 0)) { 180e8eb82a8SPeter Wemm (void)fwrite(ibuf, 1, cc, fscript); 181e8eb82a8SPeter Wemm } 182e8eb82a8SPeter Wemm } 183e8eb82a8SPeter Wemm } 184e8eb82a8SPeter Wemm if (n > 0 && FD_ISSET(master, &rfd)) { 185e8eb82a8SPeter Wemm cc = read(master, obuf, sizeof (obuf)); 186e8eb82a8SPeter Wemm if (cc <= 0) 187e8eb82a8SPeter Wemm break; 188e8eb82a8SPeter Wemm (void)write(1, obuf, cc); 189e8eb82a8SPeter Wemm (void)fwrite(obuf, 1, cc, fscript); 190e8eb82a8SPeter Wemm } 191e8eb82a8SPeter Wemm tvec = time(0); 192e8eb82a8SPeter Wemm if (tvec - start >= flushtime) { 193e8eb82a8SPeter Wemm fflush(fscript); 194e8eb82a8SPeter Wemm start = tvec; 195e8eb82a8SPeter Wemm } 196e8eb82a8SPeter Wemm } 197b1c66970SDag-Erling Smørgrav finish(); 198b1c66970SDag-Erling Smørgrav done(0); 1999b50d902SRodney W. Grimes } 2009b50d902SRodney W. Grimes 201236d2f55SPhilippe Charnier static void 202236d2f55SPhilippe Charnier usage() 203236d2f55SPhilippe Charnier { 204e8eb82a8SPeter Wemm (void)fprintf(stderr, 205e8eb82a8SPeter Wemm "usage: script [-a] [-q] [-k] [-t time] [file] [command]\n"); 206236d2f55SPhilippe Charnier exit(1); 207236d2f55SPhilippe Charnier } 208236d2f55SPhilippe Charnier 2099b50d902SRodney W. Grimes void 210b1c66970SDag-Erling Smørgrav finish() 2119b50d902SRodney W. Grimes { 212b1c66970SDag-Erling Smørgrav int die, e, pid; 2139b50d902SRodney W. Grimes union wait status; 2149b50d902SRodney W. Grimes 215b1c66970SDag-Erling Smørgrav die = e = 0; 2169b50d902SRodney W. Grimes while ((pid = wait3((int *)&status, WNOHANG, 0)) > 0) 217b1c66970SDag-Erling Smørgrav if (pid == child) { 2189b50d902SRodney W. Grimes die = 1; 219b1c66970SDag-Erling Smørgrav if (WIFEXITED(status)) 220b1c66970SDag-Erling Smørgrav e = WEXITSTATUS(status); 221b1c66970SDag-Erling Smørgrav else if (WIFSIGNALED(status)) 222b1c66970SDag-Erling Smørgrav e = WTERMSIG(status); 223b1c66970SDag-Erling Smørgrav else /* can't happen */ 224b1c66970SDag-Erling Smørgrav e = 1; 225b1c66970SDag-Erling Smørgrav } 2269b50d902SRodney W. Grimes 2279b50d902SRodney W. Grimes if (die) 228b1c66970SDag-Erling Smørgrav done(e); 2299b50d902SRodney W. Grimes } 2309b50d902SRodney W. Grimes 2319b50d902SRodney W. Grimes void 23251afb8dfSPeter Wemm doshell(av) 23351afb8dfSPeter Wemm char **av; 2349b50d902SRodney W. Grimes { 2359b50d902SRodney W. Grimes char *shell; 2369b50d902SRodney W. Grimes 2379b50d902SRodney W. Grimes shell = getenv("SHELL"); 2389b50d902SRodney W. Grimes if (shell == NULL) 2399b50d902SRodney W. Grimes shell = _PATH_BSHELL; 2409b50d902SRodney W. Grimes 2419b50d902SRodney W. Grimes (void)close(master); 2429b50d902SRodney W. Grimes (void)fclose(fscript); 2439b50d902SRodney W. Grimes login_tty(slave); 244b1c66970SDag-Erling Smørgrav if (av[0]) { 2450e604e98SPeter Wemm execvp(av[0], av); 246b7ffba17SKris Kennaway warn("%s", av[0]); 247b1c66970SDag-Erling Smørgrav } else { 2487bc6d015SBrian Somers execl(shell, shell, "-i", (char *)NULL); 249b7ffba17SKris Kennaway warn("%s", shell); 250b1c66970SDag-Erling Smørgrav } 2519b50d902SRodney W. Grimes fail(); 2529b50d902SRodney W. Grimes } 2539b50d902SRodney W. Grimes 2549b50d902SRodney W. Grimes void 2559b50d902SRodney W. Grimes fail() 2569b50d902SRodney W. Grimes { 2579b50d902SRodney W. Grimes (void)kill(0, SIGTERM); 258b1c66970SDag-Erling Smørgrav done(1); 2599b50d902SRodney W. Grimes } 2609b50d902SRodney W. Grimes 2619b50d902SRodney W. Grimes void 262b1c66970SDag-Erling Smørgrav done(eno) 263b1c66970SDag-Erling Smørgrav int eno; 2649b50d902SRodney W. Grimes { 2659b50d902SRodney W. Grimes time_t tvec; 2669b50d902SRodney W. Grimes 267e8eb82a8SPeter Wemm (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt); 2689b50d902SRodney W. Grimes tvec = time(NULL); 269e8eb82a8SPeter Wemm if (!qflg) { 2709b50d902SRodney W. Grimes (void)fprintf(fscript,"\nScript done on %s", ctime(&tvec)); 271e8eb82a8SPeter Wemm (void)printf("\nScript done, output file is %s\n", fname); 272e8eb82a8SPeter Wemm } 2739b50d902SRodney W. Grimes (void)fclose(fscript); 2749b50d902SRodney W. Grimes (void)close(master); 275b1c66970SDag-Erling Smørgrav exit(eno); 2769b50d902SRodney W. Grimes } 277