xref: /freebsd/usr.bin/script/script.c (revision 6ac087cf8ff4de22dc0b1b7ce1248f0a1558ddc2)
19b50d902SRodney W. Grimes /*
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
46cff4e07SDavid E. O'Brien  * Copyright (c) 2010, 2012  David E. O'Brien
59b50d902SRodney W. Grimes  * Copyright (c) 1980, 1992, 1993
69b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
79b50d902SRodney W. Grimes  *
89b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
99b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
109b50d902SRodney W. Grimes  * are met:
119b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
129b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
139b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
149b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
159b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
16fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
179b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
189b50d902SRodney W. Grimes  *    without specific prior written permission.
199b50d902SRodney W. Grimes  *
209b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
219b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
229b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
239b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
249b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
259b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
269b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
279b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
289b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
299b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
309b50d902SRodney W. Grimes  * SUCH DAMAGE.
319b50d902SRodney W. Grimes  */
329b50d902SRodney W. Grimes 
3313f02f28SDavid E. O'Brien #include <sys/param.h>
34d20f95e1SMark Murray __FBSDID("$FreeBSD$");
359b50d902SRodney W. Grimes #ifndef lint
36236d2f55SPhilippe Charnier static const char copyright[] =
379b50d902SRodney W. Grimes "@(#) Copyright (c) 1980, 1992, 1993\n\
389b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
39d20f95e1SMark Murray #endif
409b50d902SRodney W. Grimes #ifndef lint
41d20f95e1SMark Murray static const char sccsid[] = "@(#)script.c	8.1 (Berkeley) 6/6/93";
42236d2f55SPhilippe Charnier #endif
439b50d902SRodney W. Grimes 
449b50d902SRodney W. Grimes #include <sys/wait.h>
459b50d902SRodney W. Grimes #include <sys/stat.h>
469b50d902SRodney W. Grimes #include <sys/ioctl.h>
479b50d902SRodney W. Grimes #include <sys/time.h>
48c0ba4c2eSKonstantin Belousov #include <sys/queue.h>
49df53360cSBrian Somers #include <sys/uio.h>
50df53360cSBrian Somers #include <sys/endian.h>
516cff4e07SDavid E. O'Brien #include <dev/filemon/filemon.h>
529b50d902SRodney W. Grimes 
53236d2f55SPhilippe Charnier #include <err.h>
54e8eb82a8SPeter Wemm #include <errno.h>
559b50d902SRodney W. Grimes #include <fcntl.h>
56236d2f55SPhilippe Charnier #include <libutil.h>
579b50d902SRodney W. Grimes #include <paths.h>
589b50d902SRodney W. Grimes #include <signal.h>
599b50d902SRodney W. Grimes #include <stdio.h>
609b50d902SRodney W. Grimes #include <stdlib.h>
619b50d902SRodney W. Grimes #include <string.h>
629b50d902SRodney W. Grimes #include <termios.h>
639b50d902SRodney W. Grimes #include <unistd.h>
649b50d902SRodney W. Grimes 
65df53360cSBrian Somers #define DEF_BUF 65536
66df53360cSBrian Somers 
67df53360cSBrian Somers struct stamp {
68df53360cSBrian Somers 	uint64_t scr_len;	/* amount of data */
69df53360cSBrian Somers 	uint64_t scr_sec;	/* time it arrived in seconds... */
70df53360cSBrian Somers 	uint32_t scr_usec;	/* ...and microseconds */
71df53360cSBrian Somers 	uint32_t scr_direction; /* 'i', 'o', etc (also indicates endianness) */
72df53360cSBrian Somers };
73df53360cSBrian Somers 
74c0ba4c2eSKonstantin Belousov struct buf_elm {
75c0ba4c2eSKonstantin Belousov 	TAILQ_ENTRY(buf_elm) link;
763276866fSDag-Erling Smørgrav 	size_t rpos;
773276866fSDag-Erling Smørgrav 	size_t len;
78c0ba4c2eSKonstantin Belousov 	char ibuf[];
79c0ba4c2eSKonstantin Belousov };
80c0ba4c2eSKonstantin Belousov 
81e8efeec6SEd Schouten static FILE *fscript;
82e8efeec6SEd Schouten static int master, slave;
83e8efeec6SEd Schouten static int child;
84e8efeec6SEd Schouten static const char *fname;
856cff4e07SDavid E. O'Brien static char *fmfname;
866cff4e07SDavid E. O'Brien static int fflg, qflg, ttyflg;
8766d93438SBryan Drewery static int usesleep, rawout, showexit;
88c0ba4c2eSKonstantin Belousov static TAILQ_HEAD(, buf_elm) obuf_list = TAILQ_HEAD_INITIALIZER(obuf_list);
899b50d902SRodney W. Grimes 
90e8efeec6SEd Schouten static struct termios tt;
919b50d902SRodney W. Grimes 
926c4afed5SSimon J. Gerraty #ifndef TSTAMP_FMT
936c4afed5SSimon J. Gerraty /* useful for tool and human reading */
9431fde973SSimon J. Gerraty # define TSTAMP_FMT "%n@ %s [%Y-%m-%d %T]%n"
956c4afed5SSimon J. Gerraty #endif
966c4afed5SSimon J. Gerraty static const char *tstamp_fmt = TSTAMP_FMT;
976c4afed5SSimon J. Gerraty static int tflg;
986c4afed5SSimon J. Gerraty 
99e8efeec6SEd Schouten static void done(int) __dead2;
100e8efeec6SEd Schouten static void doshell(char **);
101e8efeec6SEd Schouten static void finish(void);
102df53360cSBrian Somers static void record(FILE *, char *, size_t, int);
103df53360cSBrian Somers static void consume(FILE *, off_t, char *, int);
104df53360cSBrian Somers static void playback(FILE *) __dead2;
1053f330d7dSWarner Losh static void usage(void);
1069b50d902SRodney W. Grimes 
1079b50d902SRodney W. Grimes int
108f4ac32deSDavid Malone main(int argc, char *argv[])
1099b50d902SRodney W. Grimes {
110e8eb82a8SPeter Wemm 	struct termios rtt, stt;
1119b50d902SRodney W. Grimes 	struct winsize win;
112e8eb82a8SPeter Wemm 	struct timeval tv, *tvp;
113e8eb82a8SPeter Wemm 	time_t tvec, start;
114e8eb82a8SPeter Wemm 	char obuf[BUFSIZ];
1159b50d902SRodney W. Grimes 	char ibuf[BUFSIZ];
116c0ba4c2eSKonstantin Belousov 	fd_set rfd, wfd;
117c0ba4c2eSKonstantin Belousov 	struct buf_elm *be;
1183276866fSDag-Erling Smørgrav 	ssize_t cc;
119c0ba4c2eSKonstantin Belousov 	int aflg, Fflg, kflg, pflg, ch, k, n, fcm;
1206cff4e07SDavid E. O'Brien 	int flushtime, readstdin;
1216cff4e07SDavid E. O'Brien 	int fm_fd, fm_log;
1229b50d902SRodney W. Grimes 
1238d105abcSTom Rhodes 	aflg = Fflg = kflg = pflg = 0;
124df53360cSBrian Somers 	usesleep = 1;
125df53360cSBrian Somers 	rawout = 0;
1266cff4e07SDavid E. O'Brien 	flushtime = 30;
1276cff4e07SDavid E. O'Brien 	fm_fd = -1;	/* Shut up stupid "may be used uninitialized" GCC
1286cff4e07SDavid E. O'Brien 			   warning. (not needed w/clang) */
12966d93438SBryan Drewery 	showexit = 0;
130df53360cSBrian Somers 
1316c4afed5SSimon J. Gerraty 	while ((ch = getopt(argc, argv, "adeFfkpqrT:t:")) != -1)
1329b50d902SRodney W. Grimes 		switch(ch) {
1339b50d902SRodney W. Grimes 		case 'a':
1349b50d902SRodney W. Grimes 			aflg = 1;
1359b50d902SRodney W. Grimes 			break;
136df53360cSBrian Somers 		case 'd':
137df53360cSBrian Somers 			usesleep = 0;
13851afb8dfSPeter Wemm 			break;
13984961358SWarner Losh 		case 'e':	/* Default behavior, accepted for linux compat */
140f9177b6cSWarner Losh 			break;
1418d105abcSTom Rhodes 		case 'F':
1428d105abcSTom Rhodes 			Fflg = 1;
1438d105abcSTom Rhodes 			break;
1446cff4e07SDavid E. O'Brien 		case 'f':
1456cff4e07SDavid E. O'Brien 			fflg = 1;
1466cff4e07SDavid E. O'Brien 			break;
147e8eb82a8SPeter Wemm 		case 'k':
148e8eb82a8SPeter Wemm 			kflg = 1;
149e8eb82a8SPeter Wemm 			break;
150df53360cSBrian Somers 		case 'p':
151df53360cSBrian Somers 			pflg = 1;
152df53360cSBrian Somers 			break;
153df53360cSBrian Somers 		case 'q':
154df53360cSBrian Somers 			qflg = 1;
155df53360cSBrian Somers 			break;
156df53360cSBrian Somers 		case 'r':
157df53360cSBrian Somers 			rawout = 1;
158df53360cSBrian Somers 			break;
159e8eb82a8SPeter Wemm 		case 't':
160e8eb82a8SPeter Wemm 			flushtime = atoi(optarg);
161e8eb82a8SPeter Wemm 			if (flushtime < 0)
162e8eb82a8SPeter Wemm 				err(1, "invalid flush time %d", flushtime);
163e8eb82a8SPeter Wemm 			break;
1646c4afed5SSimon J. Gerraty 		case 'T':
1656c4afed5SSimon J. Gerraty 			tflg = pflg = 1;
1666c4afed5SSimon J. Gerraty 			if (strchr(optarg, '%'))
1676c4afed5SSimon J. Gerraty 				tstamp_fmt = optarg;
1686c4afed5SSimon J. Gerraty 			break;
1699b50d902SRodney W. Grimes 		case '?':
1709b50d902SRodney W. Grimes 		default:
171236d2f55SPhilippe Charnier 			usage();
1729b50d902SRodney W. Grimes 		}
1739b50d902SRodney W. Grimes 	argc -= optind;
1749b50d902SRodney W. Grimes 	argv += optind;
1759b50d902SRodney W. Grimes 
17651afb8dfSPeter Wemm 	if (argc > 0) {
1779b50d902SRodney W. Grimes 		fname = argv[0];
17851afb8dfSPeter Wemm 		argv++;
17951afb8dfSPeter Wemm 		argc--;
18051afb8dfSPeter Wemm 	} else
1819b50d902SRodney W. Grimes 		fname = "typescript";
1829b50d902SRodney W. Grimes 
183df53360cSBrian Somers 	if ((fscript = fopen(fname, pflg ? "r" : aflg ? "a" : "w")) == NULL)
184236d2f55SPhilippe Charnier 		err(1, "%s", fname);
1859b50d902SRodney W. Grimes 
1866cff4e07SDavid E. O'Brien 	if (fflg) {
1876cff4e07SDavid E. O'Brien 		asprintf(&fmfname, "%s.filemon", fname);
1886cff4e07SDavid E. O'Brien 		if (!fmfname)
1896cff4e07SDavid E. O'Brien 			err(1, "%s.filemon", fname);
19026e736b2SBaptiste Daroussin 		if ((fm_fd = open("/dev/filemon", O_RDWR | O_CLOEXEC)) == -1)
1916cff4e07SDavid E. O'Brien 			err(1, "open(\"/dev/filemon\", O_RDWR)");
19226e736b2SBaptiste Daroussin 		if ((fm_log = open(fmfname,
19326e736b2SBaptiste Daroussin 		    O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC,
1946cff4e07SDavid E. O'Brien 		    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
1956cff4e07SDavid E. O'Brien 			err(1, "open(%s)", fmfname);
1966cff4e07SDavid E. O'Brien 		if (ioctl(fm_fd, FILEMON_SET_FD, &fm_log) < 0)
1976cff4e07SDavid E. O'Brien 			err(1, "Cannot set filemon log file descriptor");
1986cff4e07SDavid E. O'Brien 	}
1996cff4e07SDavid E. O'Brien 
200df53360cSBrian Somers 	if (pflg)
201df53360cSBrian Somers 		playback(fscript);
202df53360cSBrian Somers 
2038bc30d2aSMark Johnston 	if (tcgetattr(STDIN_FILENO, &tt) == -1 ||
2048bc30d2aSMark Johnston 	    ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1) {
2058bc30d2aSMark Johnston 		if (errno != ENOTTY) /* For debugger. */
2068bc30d2aSMark Johnston 			err(1, "tcgetattr/ioctl");
207e292a0e3SColin Percival 		if (openpty(&master, &slave, NULL, NULL, NULL) == -1)
208e292a0e3SColin Percival 			err(1, "openpty");
2098bc30d2aSMark Johnston 	} else {
2108bc30d2aSMark Johnston 		if (openpty(&master, &slave, NULL, &tt, &win) == -1)
2118bc30d2aSMark Johnston 			err(1, "openpty");
2128bc30d2aSMark Johnston 		ttyflg = 1;
213e292a0e3SColin Percival 	}
214c0ba4c2eSKonstantin Belousov 	fcm = fcntl(master, F_GETFL);
215c0ba4c2eSKonstantin Belousov 	if (fcm == -1)
216c0ba4c2eSKonstantin Belousov 		err(1, "master F_GETFL");
217c0ba4c2eSKonstantin Belousov 	fcm |= O_NONBLOCK;
218c0ba4c2eSKonstantin Belousov 	if (fcntl(master, F_SETFL, fcm) == -1)
219c0ba4c2eSKonstantin Belousov 		err(1, "master F_SETFL");
2209b50d902SRodney W. Grimes 
221df53360cSBrian Somers 	if (rawout)
222df53360cSBrian Somers 		record(fscript, NULL, 0, 's');
223df53360cSBrian Somers 
224e8eb82a8SPeter Wemm 	if (!qflg) {
225e8eb82a8SPeter Wemm 		tvec = time(NULL);
2269b50d902SRodney W. Grimes 		(void)printf("Script started, output file is %s\n", fname);
227df53360cSBrian Somers 		if (!rawout) {
228df53360cSBrian Somers 			(void)fprintf(fscript, "Script started on %s",
229df53360cSBrian Somers 			    ctime(&tvec));
230df53360cSBrian Somers 			if (argv[0]) {
23166d93438SBryan Drewery 				showexit = 1;
23266d93438SBryan Drewery 				fprintf(fscript, "Command: ");
233df53360cSBrian Somers 				for (k = 0 ; argv[k] ; ++k)
234df53360cSBrian Somers 					fprintf(fscript, "%s%s", k ? " " : "",
235df53360cSBrian Somers 						argv[k]);
236df53360cSBrian Somers 				fprintf(fscript, "\n");
237df53360cSBrian Somers 			}
238df53360cSBrian Somers 		}
239e8eb82a8SPeter Wemm 		fflush(fscript);
2406cff4e07SDavid E. O'Brien 		if (fflg) {
2416cff4e07SDavid E. O'Brien 			(void)printf("Filemon started, output file is %s\n",
2426cff4e07SDavid E. O'Brien 			    fmfname);
2436cff4e07SDavid E. O'Brien 		}
244e8eb82a8SPeter Wemm 	}
245e292a0e3SColin Percival 	if (ttyflg) {
2469b50d902SRodney W. Grimes 		rtt = tt;
2479b50d902SRodney W. Grimes 		cfmakeraw(&rtt);
2489b50d902SRodney W. Grimes 		rtt.c_lflag &= ~ECHO;
2499b50d902SRodney W. Grimes 		(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
250e292a0e3SColin Percival 	}
2519b50d902SRodney W. Grimes 
2529b50d902SRodney W. Grimes 	child = fork();
2539b50d902SRodney W. Grimes 	if (child < 0) {
254236d2f55SPhilippe Charnier 		warn("fork");
255b1c66970SDag-Erling Smørgrav 		done(1);
2569b50d902SRodney W. Grimes 	}
2577b245cb8SBryan Drewery 	if (child == 0) {
2587b245cb8SBryan Drewery 		if (fflg) {
2597b245cb8SBryan Drewery 			int pid;
2609b50d902SRodney W. Grimes 
2617b245cb8SBryan Drewery 			pid = getpid();
2627b245cb8SBryan Drewery 			if (ioctl(fm_fd, FILEMON_SET_PID, &pid) < 0)
2636cff4e07SDavid E. O'Brien 				err(1, "Cannot set filemon PID");
2647b245cb8SBryan Drewery 		}
2657b245cb8SBryan Drewery 
2667b245cb8SBryan Drewery 		doshell(argv);
2677b245cb8SBryan Drewery 	}
2687b245cb8SBryan Drewery 	close(slave);
2696cff4e07SDavid E. O'Brien 
27029da7547SMikolaj Golub 	start = tvec = time(0);
27129da7547SMikolaj Golub 	readstdin = 1;
272e8eb82a8SPeter Wemm 	for (;;) {
27329da7547SMikolaj Golub 		FD_ZERO(&rfd);
274c0ba4c2eSKonstantin Belousov 		FD_ZERO(&wfd);
275e8eb82a8SPeter Wemm 		FD_SET(master, &rfd);
27629da7547SMikolaj Golub 		if (readstdin)
277e8eb82a8SPeter Wemm 			FD_SET(STDIN_FILENO, &rfd);
278c0ba4c2eSKonstantin Belousov 		if (!TAILQ_EMPTY(&obuf_list))
279c0ba4c2eSKonstantin Belousov 			FD_SET(master, &wfd);
28029f4384aSMikolaj Golub 		if (!readstdin && ttyflg) {
28129f4384aSMikolaj Golub 			tv.tv_sec = 1;
282e8eb82a8SPeter Wemm 			tv.tv_usec = 0;
28329da7547SMikolaj Golub 			tvp = &tv;
28429da7547SMikolaj Golub 			readstdin = 1;
28529f4384aSMikolaj Golub 		} else if (flushtime > 0) {
28629f4384aSMikolaj Golub 			tv.tv_sec = flushtime - (tvec - start);
28729f4384aSMikolaj Golub 			tv.tv_usec = 0;
28829f4384aSMikolaj Golub 			tvp = &tv;
28929da7547SMikolaj Golub 		} else {
29029da7547SMikolaj Golub 			tvp = NULL;
291e8eb82a8SPeter Wemm 		}
292c0ba4c2eSKonstantin Belousov 		n = select(master + 1, &rfd, &wfd, NULL, tvp);
293e8eb82a8SPeter Wemm 		if (n < 0 && errno != EINTR)
294e8eb82a8SPeter Wemm 			break;
295e8eb82a8SPeter Wemm 		if (n > 0 && FD_ISSET(STDIN_FILENO, &rfd)) {
296e8eb82a8SPeter Wemm 			cc = read(STDIN_FILENO, ibuf, BUFSIZ);
297d6a68195SColin Percival 			if (cc < 0)
298e8eb82a8SPeter Wemm 				break;
29929da7547SMikolaj Golub 			if (cc == 0) {
30029da7547SMikolaj Golub 				if (tcgetattr(master, &stt) == 0 &&
30129da7547SMikolaj Golub 				    (stt.c_lflag & ICANON) != 0) {
30229da7547SMikolaj Golub 					(void)write(master, &stt.c_cc[VEOF], 1);
30329da7547SMikolaj Golub 				}
30429da7547SMikolaj Golub 				readstdin = 0;
30529da7547SMikolaj Golub 			}
306e8eb82a8SPeter Wemm 			if (cc > 0) {
307df53360cSBrian Somers 				if (rawout)
308df53360cSBrian Somers 					record(fscript, ibuf, cc, 'i');
309c0ba4c2eSKonstantin Belousov 				be = malloc(sizeof(*be) + cc);
310c0ba4c2eSKonstantin Belousov 				be->rpos = 0;
311c0ba4c2eSKonstantin Belousov 				be->len = cc;
312c0ba4c2eSKonstantin Belousov 				memcpy(be->ibuf, ibuf, cc);
313c0ba4c2eSKonstantin Belousov 				TAILQ_INSERT_TAIL(&obuf_list, be, link);
314c0ba4c2eSKonstantin Belousov 			}
315c0ba4c2eSKonstantin Belousov 		}
316c0ba4c2eSKonstantin Belousov 		if (n > 0 && FD_ISSET(master, &wfd)) {
317c0ba4c2eSKonstantin Belousov 			while ((be = TAILQ_FIRST(&obuf_list)) != NULL) {
318c0ba4c2eSKonstantin Belousov 				cc = write(master, be->ibuf + be->rpos,
319c0ba4c2eSKonstantin Belousov 				    be->len);
320c0ba4c2eSKonstantin Belousov 				if (cc == -1) {
321c0ba4c2eSKonstantin Belousov 					if (errno == EWOULDBLOCK ||
322c0ba4c2eSKonstantin Belousov 					    errno == EINTR)
323c0ba4c2eSKonstantin Belousov 						break;
324c0ba4c2eSKonstantin Belousov 					warn("write master");
325c0ba4c2eSKonstantin Belousov 					done(1);
326c0ba4c2eSKonstantin Belousov 				}
327c0ba4c2eSKonstantin Belousov 				if (cc == 0)
328c0ba4c2eSKonstantin Belousov 					break;		/* retry later ? */
329e8eb82a8SPeter Wemm 				if (kflg && tcgetattr(master, &stt) >= 0 &&
330e8eb82a8SPeter Wemm 				    ((stt.c_lflag & ECHO) == 0)) {
331c0ba4c2eSKonstantin Belousov 					(void)fwrite(be->ibuf + be->rpos,
332c0ba4c2eSKonstantin Belousov 					    1, cc, fscript);
333c0ba4c2eSKonstantin Belousov 				}
334c0ba4c2eSKonstantin Belousov 				be->len -= cc;
335c0ba4c2eSKonstantin Belousov 				if (be->len == 0) {
336c0ba4c2eSKonstantin Belousov 					TAILQ_REMOVE(&obuf_list, be, link);
337c0ba4c2eSKonstantin Belousov 					free(be);
338c0ba4c2eSKonstantin Belousov 				} else {
339c0ba4c2eSKonstantin Belousov 					be->rpos += cc;
340e8eb82a8SPeter Wemm 				}
341e8eb82a8SPeter Wemm 			}
342e8eb82a8SPeter Wemm 		}
343e8eb82a8SPeter Wemm 		if (n > 0 && FD_ISSET(master, &rfd)) {
344e8eb82a8SPeter Wemm 			cc = read(master, obuf, sizeof (obuf));
345e8eb82a8SPeter Wemm 			if (cc <= 0)
346e8eb82a8SPeter Wemm 				break;
347e1b4d8d0SSheldon Hearn 			(void)write(STDOUT_FILENO, obuf, cc);
348df53360cSBrian Somers 			if (rawout)
349df53360cSBrian Somers 				record(fscript, obuf, cc, 'o');
350df53360cSBrian Somers 			else
351e8eb82a8SPeter Wemm 				(void)fwrite(obuf, 1, cc, fscript);
352e8eb82a8SPeter Wemm 		}
353e8eb82a8SPeter Wemm 		tvec = time(0);
354e8eb82a8SPeter Wemm 		if (tvec - start >= flushtime) {
355e8eb82a8SPeter Wemm 			fflush(fscript);
356e8eb82a8SPeter Wemm 			start = tvec;
357e8eb82a8SPeter Wemm 		}
3588d105abcSTom Rhodes 		if (Fflg)
3598d105abcSTom Rhodes 			fflush(fscript);
360e8eb82a8SPeter Wemm 	}
361b1c66970SDag-Erling Smørgrav 	finish();
362b1c66970SDag-Erling Smørgrav 	done(0);
3639b50d902SRodney W. Grimes }
3649b50d902SRodney W. Grimes 
365236d2f55SPhilippe Charnier static void
366f4ac32deSDavid Malone usage(void)
367236d2f55SPhilippe Charnier {
368e8eb82a8SPeter Wemm 	(void)fprintf(stderr,
369*6ac087cfSDag-Erling Smørgrav 	    "usage: script [-aeFfkpqr] [-t time] [file [command ...]]\n");
370e7c13cf4SDag-Erling Smørgrav 	(void)fprintf(stderr,
371*6ac087cfSDag-Erling Smørgrav 	    "       script -p [-deq] [-T fmt] [file]\n");
372236d2f55SPhilippe Charnier 	exit(1);
373236d2f55SPhilippe Charnier }
374236d2f55SPhilippe Charnier 
375e8efeec6SEd Schouten static void
376f4ac32deSDavid Malone finish(void)
3779b50d902SRodney W. Grimes {
378e1e9ba33SEd Schouten 	int e, status;
3799b50d902SRodney W. Grimes 
380e1e9ba33SEd Schouten 	if (waitpid(child, &status, 0) == child) {
381b1c66970SDag-Erling Smørgrav 		if (WIFEXITED(status))
382b1c66970SDag-Erling Smørgrav 			e = WEXITSTATUS(status);
383b1c66970SDag-Erling Smørgrav 		else if (WIFSIGNALED(status))
384b1c66970SDag-Erling Smørgrav 			e = WTERMSIG(status);
385b1c66970SDag-Erling Smørgrav 		else /* can't happen */
386b1c66970SDag-Erling Smørgrav 			e = 1;
387b1c66970SDag-Erling Smørgrav 		done(e);
3889b50d902SRodney W. Grimes 	}
389e1e9ba33SEd Schouten }
3909b50d902SRodney W. Grimes 
391e8efeec6SEd Schouten static void
392f4ac32deSDavid Malone doshell(char **av)
3939b50d902SRodney W. Grimes {
394b139689cSDavid Malone 	const char *shell;
3959b50d902SRodney W. Grimes 
3969b50d902SRodney W. Grimes 	shell = getenv("SHELL");
3979b50d902SRodney W. Grimes 	if (shell == NULL)
3989b50d902SRodney W. Grimes 		shell = _PATH_BSHELL;
3999b50d902SRodney W. Grimes 
4009b50d902SRodney W. Grimes 	(void)close(master);
4019b50d902SRodney W. Grimes 	(void)fclose(fscript);
4026cff4e07SDavid E. O'Brien 	free(fmfname);
4039b50d902SRodney W. Grimes 	login_tty(slave);
4049b91846cSDavid E. O'Brien 	setenv("SCRIPT", fname, 1);
405b1c66970SDag-Erling Smørgrav 	if (av[0]) {
4060e604e98SPeter Wemm 		execvp(av[0], av);
407b7ffba17SKris Kennaway 		warn("%s", av[0]);
408b1c66970SDag-Erling Smørgrav 	} else {
4097bc6d015SBrian Somers 		execl(shell, shell, "-i", (char *)NULL);
410b7ffba17SKris Kennaway 		warn("%s", shell);
411b1c66970SDag-Erling Smørgrav 	}
41295d13d1bSBryan Drewery 	exit(1);
4139b50d902SRodney W. Grimes }
4149b50d902SRodney W. Grimes 
415e8efeec6SEd Schouten static void
416f4ac32deSDavid Malone done(int eno)
4179b50d902SRodney W. Grimes {
4189b50d902SRodney W. Grimes 	time_t tvec;
4199b50d902SRodney W. Grimes 
420e292a0e3SColin Percival 	if (ttyflg)
421e8eb82a8SPeter Wemm 		(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
4229b50d902SRodney W. Grimes 	tvec = time(NULL);
423df53360cSBrian Somers 	if (rawout)
424df53360cSBrian Somers 		record(fscript, NULL, 0, 'e');
425e8eb82a8SPeter Wemm 	if (!qflg) {
42666d93438SBryan Drewery 		if (!rawout) {
42766d93438SBryan Drewery 			if (showexit)
42866d93438SBryan Drewery 				(void)fprintf(fscript, "\nCommand exit status:"
42966d93438SBryan Drewery 				    " %d", eno);
430df53360cSBrian Somers 			(void)fprintf(fscript,"\nScript done on %s",
431df53360cSBrian Somers 			    ctime(&tvec));
43266d93438SBryan Drewery 		}
433e8eb82a8SPeter Wemm 		(void)printf("\nScript done, output file is %s\n", fname);
4346cff4e07SDavid E. O'Brien 		if (fflg) {
4356cff4e07SDavid E. O'Brien 			(void)printf("Filemon done, output file is %s\n",
4366cff4e07SDavid E. O'Brien 			    fmfname);
4376cff4e07SDavid E. O'Brien 		}
438e8eb82a8SPeter Wemm 	}
4399b50d902SRodney W. Grimes 	(void)fclose(fscript);
4409b50d902SRodney W. Grimes 	(void)close(master);
441b1c66970SDag-Erling Smørgrav 	exit(eno);
4429b50d902SRodney W. Grimes }
443df53360cSBrian Somers 
444df53360cSBrian Somers static void
445df53360cSBrian Somers record(FILE *fp, char *buf, size_t cc, int direction)
446df53360cSBrian Somers {
447df53360cSBrian Somers 	struct iovec iov[2];
448df53360cSBrian Somers 	struct stamp stamp;
449df53360cSBrian Somers 	struct timeval tv;
450df53360cSBrian Somers 
451df53360cSBrian Somers 	(void)gettimeofday(&tv, NULL);
452df53360cSBrian Somers 	stamp.scr_len = cc;
453df53360cSBrian Somers 	stamp.scr_sec = tv.tv_sec;
454df53360cSBrian Somers 	stamp.scr_usec = tv.tv_usec;
455df53360cSBrian Somers 	stamp.scr_direction = direction;
456df53360cSBrian Somers 	iov[0].iov_len = sizeof(stamp);
457df53360cSBrian Somers 	iov[0].iov_base = &stamp;
458df53360cSBrian Somers 	iov[1].iov_len = cc;
459df53360cSBrian Somers 	iov[1].iov_base = buf;
460df53360cSBrian Somers 	if (writev(fileno(fp), &iov[0], 2) == -1)
461df53360cSBrian Somers 		err(1, "writev");
462df53360cSBrian Somers }
463df53360cSBrian Somers 
464df53360cSBrian Somers static void
465df53360cSBrian Somers consume(FILE *fp, off_t len, char *buf, int reg)
466df53360cSBrian Somers {
467df53360cSBrian Somers 	size_t l;
468df53360cSBrian Somers 
469df53360cSBrian Somers 	if (reg) {
470df53360cSBrian Somers 		if (fseeko(fp, len, SEEK_CUR) == -1)
471df53360cSBrian Somers 			err(1, NULL);
472df53360cSBrian Somers 	}
473df53360cSBrian Somers 	else {
474df53360cSBrian Somers 		while (len > 0) {
475df53360cSBrian Somers 			l = MIN(DEF_BUF, len);
476df53360cSBrian Somers 			if (fread(buf, sizeof(char), l, fp) != l)
477df53360cSBrian Somers 				err(1, "cannot read buffer");
478df53360cSBrian Somers 			len -= l;
479df53360cSBrian Somers 		}
480df53360cSBrian Somers 	}
481df53360cSBrian Somers }
482df53360cSBrian Somers 
483df53360cSBrian Somers #define swapstamp(stamp) do { \
484df53360cSBrian Somers 	if (stamp.scr_direction > 0xff) { \
485df53360cSBrian Somers 		stamp.scr_len = bswap64(stamp.scr_len); \
486df53360cSBrian Somers 		stamp.scr_sec = bswap64(stamp.scr_sec); \
487df53360cSBrian Somers 		stamp.scr_usec = bswap32(stamp.scr_usec); \
488df53360cSBrian Somers 		stamp.scr_direction = bswap32(stamp.scr_direction); \
489df53360cSBrian Somers 	} \
490df53360cSBrian Somers } while (0/*CONSTCOND*/)
491df53360cSBrian Somers 
492df53360cSBrian Somers static void
49369bc4fa9SMark Johnston termset(void)
49469bc4fa9SMark Johnston {
49569bc4fa9SMark Johnston 	struct termios traw;
49669bc4fa9SMark Johnston 
49769bc4fa9SMark Johnston 	if (tcgetattr(STDOUT_FILENO, &tt) == -1) {
4988bc30d2aSMark Johnston 		if (errno != ENOTTY) /* For debugger. */
4998bc30d2aSMark Johnston 			err(1, "tcgetattr");
50069bc4fa9SMark Johnston 		return;
50169bc4fa9SMark Johnston 	}
50269bc4fa9SMark Johnston 	ttyflg = 1;
50369bc4fa9SMark Johnston 	traw = tt;
50469bc4fa9SMark Johnston 	cfmakeraw(&traw);
50569bc4fa9SMark Johnston 	traw.c_lflag |= ISIG;
50669bc4fa9SMark Johnston 	(void)tcsetattr(STDOUT_FILENO, TCSANOW, &traw);
50769bc4fa9SMark Johnston }
50869bc4fa9SMark Johnston 
50969bc4fa9SMark Johnston static void
51069bc4fa9SMark Johnston termreset(void)
51169bc4fa9SMark Johnston {
51269bc4fa9SMark Johnston 	if (ttyflg) {
51369bc4fa9SMark Johnston 		tcsetattr(STDOUT_FILENO, TCSADRAIN, &tt);
51469bc4fa9SMark Johnston 		ttyflg = 0;
51569bc4fa9SMark Johnston 	}
51669bc4fa9SMark Johnston }
51769bc4fa9SMark Johnston 
51869bc4fa9SMark Johnston static void
519df53360cSBrian Somers playback(FILE *fp)
520df53360cSBrian Somers {
521df53360cSBrian Somers 	struct timespec tsi, tso;
522df53360cSBrian Somers 	struct stamp stamp;
523df53360cSBrian Somers 	struct stat pst;
524df53360cSBrian Somers 	char buf[DEF_BUF];
525df53360cSBrian Somers 	off_t nread, save_len;
526df53360cSBrian Somers 	size_t l;
527df53360cSBrian Somers 	time_t tclock;
5287b45ad3fSSimon J. Gerraty 	time_t lclock;
529df53360cSBrian Somers 	int reg;
530df53360cSBrian Somers 
531df53360cSBrian Somers 	if (fstat(fileno(fp), &pst) == -1)
532df53360cSBrian Somers 		err(1, "fstat failed");
533df53360cSBrian Somers 
534df53360cSBrian Somers 	reg = S_ISREG(pst.st_mode);
5357b45ad3fSSimon J. Gerraty 	lclock = 0;
536df53360cSBrian Somers 
537df53360cSBrian Somers 	for (nread = 0; !reg || nread < pst.st_size; nread += save_len) {
538df53360cSBrian Somers 		if (fread(&stamp, sizeof(stamp), 1, fp) != 1) {
539df53360cSBrian Somers 			if (reg)
540df53360cSBrian Somers 				err(1, "reading playback header");
541df53360cSBrian Somers 			else
542df53360cSBrian Somers 				break;
543df53360cSBrian Somers 		}
544df53360cSBrian Somers 		swapstamp(stamp);
545df53360cSBrian Somers 		save_len = sizeof(stamp);
546df53360cSBrian Somers 
547df53360cSBrian Somers 		if (reg && stamp.scr_len >
548df53360cSBrian Somers 		    (uint64_t)(pst.st_size - save_len) - nread)
549df53360cSBrian Somers 			errx(1, "invalid stamp");
550df53360cSBrian Somers 
551df53360cSBrian Somers 		save_len += stamp.scr_len;
552df53360cSBrian Somers 		tclock = stamp.scr_sec;
553df53360cSBrian Somers 		tso.tv_sec = stamp.scr_sec;
554df53360cSBrian Somers 		tso.tv_nsec = stamp.scr_usec * 1000;
555df53360cSBrian Somers 
556df53360cSBrian Somers 		switch (stamp.scr_direction) {
557df53360cSBrian Somers 		case 's':
558df53360cSBrian Somers 			if (!qflg)
559df53360cSBrian Somers 			    (void)printf("Script started on %s",
560df53360cSBrian Somers 				ctime(&tclock));
561df53360cSBrian Somers 			tsi = tso;
562df53360cSBrian Somers 			(void)consume(fp, stamp.scr_len, buf, reg);
56369bc4fa9SMark Johnston 			termset();
56469bc4fa9SMark Johnston 			atexit(termreset);
565df53360cSBrian Somers 			break;
566df53360cSBrian Somers 		case 'e':
56769bc4fa9SMark Johnston 			termreset();
568df53360cSBrian Somers 			if (!qflg)
569df53360cSBrian Somers 				(void)printf("\nScript done on %s",
570df53360cSBrian Somers 				    ctime(&tclock));
571df53360cSBrian Somers 			(void)consume(fp, stamp.scr_len, buf, reg);
572df53360cSBrian Somers 			break;
573df53360cSBrian Somers 		case 'i':
574df53360cSBrian Somers 			/* throw input away */
575df53360cSBrian Somers 			(void)consume(fp, stamp.scr_len, buf, reg);
576df53360cSBrian Somers 			break;
577df53360cSBrian Somers 		case 'o':
5786c4afed5SSimon J. Gerraty 			if (tflg) {
5796c4afed5SSimon J. Gerraty 				if (stamp.scr_len == 0)
5806c4afed5SSimon J. Gerraty 					continue;
5817b45ad3fSSimon J. Gerraty 				if (tclock - lclock > 0) {
5826c4afed5SSimon J. Gerraty 				    l = strftime(buf, sizeof buf, tstamp_fmt,
5836c4afed5SSimon J. Gerraty 					localtime(&tclock));
5846c4afed5SSimon J. Gerraty 				    (void)write(STDOUT_FILENO, buf, l);
5857b45ad3fSSimon J. Gerraty 				}
5867b45ad3fSSimon J. Gerraty 				lclock = tclock;
5876c4afed5SSimon J. Gerraty 			} else {
588df53360cSBrian Somers 				tsi.tv_sec = tso.tv_sec - tsi.tv_sec;
589df53360cSBrian Somers 				tsi.tv_nsec = tso.tv_nsec - tsi.tv_nsec;
590df53360cSBrian Somers 				if (tsi.tv_nsec < 0) {
591df53360cSBrian Somers 					tsi.tv_sec -= 1;
592df53360cSBrian Somers 					tsi.tv_nsec += 1000000000;
593df53360cSBrian Somers 				}
594df53360cSBrian Somers 				if (usesleep)
595df53360cSBrian Somers 					(void)nanosleep(&tsi, NULL);
596df53360cSBrian Somers 				tsi = tso;
5976c4afed5SSimon J. Gerraty 			}
598df53360cSBrian Somers 			while (stamp.scr_len > 0) {
599df53360cSBrian Somers 				l = MIN(DEF_BUF, stamp.scr_len);
600df53360cSBrian Somers 				if (fread(buf, sizeof(char), l, fp) != l)
601df53360cSBrian Somers 					err(1, "cannot read buffer");
602df53360cSBrian Somers 
603df53360cSBrian Somers 				(void)write(STDOUT_FILENO, buf, l);
604df53360cSBrian Somers 				stamp.scr_len -= l;
605df53360cSBrian Somers 			}
606df53360cSBrian Somers 			break;
607df53360cSBrian Somers 		default:
608df53360cSBrian Somers 			errx(1, "invalid direction");
609df53360cSBrian Somers 		}
610df53360cSBrian Somers 	}
611df53360cSBrian Somers 	(void)fclose(fp);
612df53360cSBrian Somers 	exit(0);
613df53360cSBrian Somers }
614