xref: /freebsd/usr.bin/script/script.c (revision 8bc30d2afda0dae4b708b298c6b1e9d8ec2b8a7d)
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>
48df53360cSBrian Somers #include <sys/uio.h>
49df53360cSBrian Somers #include <sys/endian.h>
506cff4e07SDavid E. O'Brien #include <dev/filemon/filemon.h>
519b50d902SRodney W. Grimes 
52236d2f55SPhilippe Charnier #include <err.h>
53e8eb82a8SPeter Wemm #include <errno.h>
549b50d902SRodney W. Grimes #include <fcntl.h>
55236d2f55SPhilippe Charnier #include <libutil.h>
569b50d902SRodney W. Grimes #include <paths.h>
579b50d902SRodney W. Grimes #include <signal.h>
589b50d902SRodney W. Grimes #include <stdio.h>
599b50d902SRodney W. Grimes #include <stdlib.h>
609b50d902SRodney W. Grimes #include <string.h>
619b50d902SRodney W. Grimes #include <termios.h>
629b50d902SRodney W. Grimes #include <unistd.h>
639b50d902SRodney W. Grimes 
64df53360cSBrian Somers #define DEF_BUF 65536
65df53360cSBrian Somers 
66df53360cSBrian Somers struct stamp {
67df53360cSBrian Somers 	uint64_t scr_len;	/* amount of data */
68df53360cSBrian Somers 	uint64_t scr_sec;	/* time it arrived in seconds... */
69df53360cSBrian Somers 	uint32_t scr_usec;	/* ...and microseconds */
70df53360cSBrian Somers 	uint32_t scr_direction; /* 'i', 'o', etc (also indicates endianness) */
71df53360cSBrian Somers };
72df53360cSBrian Somers 
73e8efeec6SEd Schouten static FILE *fscript;
74e8efeec6SEd Schouten static int master, slave;
75e8efeec6SEd Schouten static int child;
76e8efeec6SEd Schouten static const char *fname;
776cff4e07SDavid E. O'Brien static char *fmfname;
786cff4e07SDavid E. O'Brien static int fflg, qflg, ttyflg;
7966d93438SBryan Drewery static int usesleep, rawout, showexit;
809b50d902SRodney W. Grimes 
81e8efeec6SEd Schouten static struct termios tt;
829b50d902SRodney W. Grimes 
83e8efeec6SEd Schouten static void done(int) __dead2;
84e8efeec6SEd Schouten static void doshell(char **);
85e8efeec6SEd Schouten static void finish(void);
86df53360cSBrian Somers static void record(FILE *, char *, size_t, int);
87df53360cSBrian Somers static void consume(FILE *, off_t, char *, int);
88df53360cSBrian Somers static void playback(FILE *) __dead2;
893f330d7dSWarner Losh static void usage(void);
909b50d902SRodney W. Grimes 
919b50d902SRodney W. Grimes int
92f4ac32deSDavid Malone main(int argc, char *argv[])
939b50d902SRodney W. Grimes {
94d20f95e1SMark Murray 	int cc;
95e8eb82a8SPeter Wemm 	struct termios rtt, stt;
969b50d902SRodney W. Grimes 	struct winsize win;
97e8eb82a8SPeter Wemm 	struct timeval tv, *tvp;
98e8eb82a8SPeter Wemm 	time_t tvec, start;
99e8eb82a8SPeter Wemm 	char obuf[BUFSIZ];
1009b50d902SRodney W. Grimes 	char ibuf[BUFSIZ];
101e8eb82a8SPeter Wemm 	fd_set rfd;
1028d105abcSTom Rhodes 	int aflg, Fflg, kflg, pflg, ch, k, n;
1036cff4e07SDavid E. O'Brien 	int flushtime, readstdin;
1046cff4e07SDavid E. O'Brien 	int fm_fd, fm_log;
1059b50d902SRodney W. Grimes 
1068d105abcSTom Rhodes 	aflg = Fflg = kflg = pflg = 0;
107df53360cSBrian Somers 	usesleep = 1;
108df53360cSBrian Somers 	rawout = 0;
1096cff4e07SDavid E. O'Brien 	flushtime = 30;
1106cff4e07SDavid E. O'Brien 	fm_fd = -1;	/* Shut up stupid "may be used uninitialized" GCC
1116cff4e07SDavid E. O'Brien 			   warning. (not needed w/clang) */
11266d93438SBryan Drewery 	showexit = 0;
113df53360cSBrian Somers 
1148d105abcSTom Rhodes 	while ((ch = getopt(argc, argv, "adFfkpqrt:")) != -1)
1159b50d902SRodney W. Grimes 		switch(ch) {
1169b50d902SRodney W. Grimes 		case 'a':
1179b50d902SRodney W. Grimes 			aflg = 1;
1189b50d902SRodney W. Grimes 			break;
119df53360cSBrian Somers 		case 'd':
120df53360cSBrian Somers 			usesleep = 0;
12151afb8dfSPeter Wemm 			break;
1228d105abcSTom Rhodes 		case 'F':
1238d105abcSTom Rhodes 			Fflg = 1;
1248d105abcSTom Rhodes 			break;
1256cff4e07SDavid E. O'Brien 		case 'f':
1266cff4e07SDavid E. O'Brien 			fflg = 1;
1276cff4e07SDavid E. O'Brien 			break;
128e8eb82a8SPeter Wemm 		case 'k':
129e8eb82a8SPeter Wemm 			kflg = 1;
130e8eb82a8SPeter Wemm 			break;
131df53360cSBrian Somers 		case 'p':
132df53360cSBrian Somers 			pflg = 1;
133df53360cSBrian Somers 			break;
134df53360cSBrian Somers 		case 'q':
135df53360cSBrian Somers 			qflg = 1;
136df53360cSBrian Somers 			break;
137df53360cSBrian Somers 		case 'r':
138df53360cSBrian Somers 			rawout = 1;
139df53360cSBrian Somers 			break;
140e8eb82a8SPeter Wemm 		case 't':
141e8eb82a8SPeter Wemm 			flushtime = atoi(optarg);
142e8eb82a8SPeter Wemm 			if (flushtime < 0)
143e8eb82a8SPeter Wemm 				err(1, "invalid flush time %d", flushtime);
144e8eb82a8SPeter Wemm 			break;
1459b50d902SRodney W. Grimes 		case '?':
1469b50d902SRodney W. Grimes 		default:
147236d2f55SPhilippe Charnier 			usage();
1489b50d902SRodney W. Grimes 		}
1499b50d902SRodney W. Grimes 	argc -= optind;
1509b50d902SRodney W. Grimes 	argv += optind;
1519b50d902SRodney W. Grimes 
15251afb8dfSPeter Wemm 	if (argc > 0) {
1539b50d902SRodney W. Grimes 		fname = argv[0];
15451afb8dfSPeter Wemm 		argv++;
15551afb8dfSPeter Wemm 		argc--;
15651afb8dfSPeter Wemm 	} else
1579b50d902SRodney W. Grimes 		fname = "typescript";
1589b50d902SRodney W. Grimes 
159df53360cSBrian Somers 	if ((fscript = fopen(fname, pflg ? "r" : aflg ? "a" : "w")) == NULL)
160236d2f55SPhilippe Charnier 		err(1, "%s", fname);
1619b50d902SRodney W. Grimes 
1626cff4e07SDavid E. O'Brien 	if (fflg) {
1636cff4e07SDavid E. O'Brien 		asprintf(&fmfname, "%s.filemon", fname);
1646cff4e07SDavid E. O'Brien 		if (!fmfname)
1656cff4e07SDavid E. O'Brien 			err(1, "%s.filemon", fname);
16626e736b2SBaptiste Daroussin 		if ((fm_fd = open("/dev/filemon", O_RDWR | O_CLOEXEC)) == -1)
1676cff4e07SDavid E. O'Brien 			err(1, "open(\"/dev/filemon\", O_RDWR)");
16826e736b2SBaptiste Daroussin 		if ((fm_log = open(fmfname,
16926e736b2SBaptiste Daroussin 		    O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC,
1706cff4e07SDavid E. O'Brien 		    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
1716cff4e07SDavid E. O'Brien 			err(1, "open(%s)", fmfname);
1726cff4e07SDavid E. O'Brien 		if (ioctl(fm_fd, FILEMON_SET_FD, &fm_log) < 0)
1736cff4e07SDavid E. O'Brien 			err(1, "Cannot set filemon log file descriptor");
1746cff4e07SDavid E. O'Brien 	}
1756cff4e07SDavid E. O'Brien 
176df53360cSBrian Somers 	if (pflg)
177df53360cSBrian Somers 		playback(fscript);
178df53360cSBrian Somers 
179*8bc30d2aSMark Johnston 	if (tcgetattr(STDIN_FILENO, &tt) == -1 ||
180*8bc30d2aSMark Johnston 	    ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1) {
181*8bc30d2aSMark Johnston 		if (errno != ENOTTY) /* For debugger. */
182*8bc30d2aSMark Johnston 			err(1, "tcgetattr/ioctl");
183e292a0e3SColin Percival 		if (openpty(&master, &slave, NULL, NULL, NULL) == -1)
184e292a0e3SColin Percival 			err(1, "openpty");
185*8bc30d2aSMark Johnston 	} else {
186*8bc30d2aSMark Johnston 		if (openpty(&master, &slave, NULL, &tt, &win) == -1)
187*8bc30d2aSMark Johnston 			err(1, "openpty");
188*8bc30d2aSMark Johnston 		ttyflg = 1;
189e292a0e3SColin Percival 	}
1909b50d902SRodney W. Grimes 
191df53360cSBrian Somers 	if (rawout)
192df53360cSBrian Somers 		record(fscript, NULL, 0, 's');
193df53360cSBrian Somers 
194e8eb82a8SPeter Wemm 	if (!qflg) {
195e8eb82a8SPeter Wemm 		tvec = time(NULL);
1969b50d902SRodney W. Grimes 		(void)printf("Script started, output file is %s\n", fname);
197df53360cSBrian Somers 		if (!rawout) {
198df53360cSBrian Somers 			(void)fprintf(fscript, "Script started on %s",
199df53360cSBrian Somers 			    ctime(&tvec));
200df53360cSBrian Somers 			if (argv[0]) {
20166d93438SBryan Drewery 				showexit = 1;
20266d93438SBryan Drewery 				fprintf(fscript, "Command: ");
203df53360cSBrian Somers 				for (k = 0 ; argv[k] ; ++k)
204df53360cSBrian Somers 					fprintf(fscript, "%s%s", k ? " " : "",
205df53360cSBrian Somers 						argv[k]);
206df53360cSBrian Somers 				fprintf(fscript, "\n");
207df53360cSBrian Somers 			}
208df53360cSBrian Somers 		}
209e8eb82a8SPeter Wemm 		fflush(fscript);
2106cff4e07SDavid E. O'Brien 		if (fflg) {
2116cff4e07SDavid E. O'Brien 			(void)printf("Filemon started, output file is %s\n",
2126cff4e07SDavid E. O'Brien 			    fmfname);
2136cff4e07SDavid E. O'Brien 		}
214e8eb82a8SPeter Wemm 	}
215e292a0e3SColin Percival 	if (ttyflg) {
2169b50d902SRodney W. Grimes 		rtt = tt;
2179b50d902SRodney W. Grimes 		cfmakeraw(&rtt);
2189b50d902SRodney W. Grimes 		rtt.c_lflag &= ~ECHO;
2199b50d902SRodney W. Grimes 		(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
220e292a0e3SColin Percival 	}
2219b50d902SRodney W. Grimes 
2229b50d902SRodney W. Grimes 	child = fork();
2239b50d902SRodney W. Grimes 	if (child < 0) {
224236d2f55SPhilippe Charnier 		warn("fork");
225b1c66970SDag-Erling Smørgrav 		done(1);
2269b50d902SRodney W. Grimes 	}
2277b245cb8SBryan Drewery 	if (child == 0) {
2287b245cb8SBryan Drewery 		if (fflg) {
2297b245cb8SBryan Drewery 			int pid;
2309b50d902SRodney W. Grimes 
2317b245cb8SBryan Drewery 			pid = getpid();
2327b245cb8SBryan Drewery 			if (ioctl(fm_fd, FILEMON_SET_PID, &pid) < 0)
2336cff4e07SDavid E. O'Brien 				err(1, "Cannot set filemon PID");
2347b245cb8SBryan Drewery 		}
2357b245cb8SBryan Drewery 
2367b245cb8SBryan Drewery 		doshell(argv);
2377b245cb8SBryan Drewery 	}
2387b245cb8SBryan Drewery 	close(slave);
2396cff4e07SDavid E. O'Brien 
24029da7547SMikolaj Golub 	start = tvec = time(0);
24129da7547SMikolaj Golub 	readstdin = 1;
242e8eb82a8SPeter Wemm 	for (;;) {
24329da7547SMikolaj Golub 		FD_ZERO(&rfd);
244e8eb82a8SPeter Wemm 		FD_SET(master, &rfd);
24529da7547SMikolaj Golub 		if (readstdin)
246e8eb82a8SPeter Wemm 			FD_SET(STDIN_FILENO, &rfd);
24729f4384aSMikolaj Golub 		if (!readstdin && ttyflg) {
24829f4384aSMikolaj Golub 			tv.tv_sec = 1;
249e8eb82a8SPeter Wemm 			tv.tv_usec = 0;
25029da7547SMikolaj Golub 			tvp = &tv;
25129da7547SMikolaj Golub 			readstdin = 1;
25229f4384aSMikolaj Golub 		} else if (flushtime > 0) {
25329f4384aSMikolaj Golub 			tv.tv_sec = flushtime - (tvec - start);
25429f4384aSMikolaj Golub 			tv.tv_usec = 0;
25529f4384aSMikolaj Golub 			tvp = &tv;
25629da7547SMikolaj Golub 		} else {
25729da7547SMikolaj Golub 			tvp = NULL;
258e8eb82a8SPeter Wemm 		}
259e8eb82a8SPeter Wemm 		n = select(master + 1, &rfd, 0, 0, tvp);
260e8eb82a8SPeter Wemm 		if (n < 0 && errno != EINTR)
261e8eb82a8SPeter Wemm 			break;
262e8eb82a8SPeter Wemm 		if (n > 0 && FD_ISSET(STDIN_FILENO, &rfd)) {
263e8eb82a8SPeter Wemm 			cc = read(STDIN_FILENO, ibuf, BUFSIZ);
264d6a68195SColin Percival 			if (cc < 0)
265e8eb82a8SPeter Wemm 				break;
26629da7547SMikolaj Golub 			if (cc == 0) {
26729da7547SMikolaj Golub 				if (tcgetattr(master, &stt) == 0 &&
26829da7547SMikolaj Golub 				    (stt.c_lflag & ICANON) != 0) {
26929da7547SMikolaj Golub 					(void)write(master, &stt.c_cc[VEOF], 1);
27029da7547SMikolaj Golub 				}
27129da7547SMikolaj Golub 				readstdin = 0;
27229da7547SMikolaj Golub 			}
273e8eb82a8SPeter Wemm 			if (cc > 0) {
274df53360cSBrian Somers 				if (rawout)
275df53360cSBrian Somers 					record(fscript, ibuf, cc, 'i');
2769b50d902SRodney W. Grimes 				(void)write(master, ibuf, cc);
277e8eb82a8SPeter Wemm 				if (kflg && tcgetattr(master, &stt) >= 0 &&
278e8eb82a8SPeter Wemm 				    ((stt.c_lflag & ECHO) == 0)) {
279e8eb82a8SPeter Wemm 					(void)fwrite(ibuf, 1, cc, fscript);
280e8eb82a8SPeter Wemm 				}
281e8eb82a8SPeter Wemm 			}
282e8eb82a8SPeter Wemm 		}
283e8eb82a8SPeter Wemm 		if (n > 0 && FD_ISSET(master, &rfd)) {
284e8eb82a8SPeter Wemm 			cc = read(master, obuf, sizeof (obuf));
285e8eb82a8SPeter Wemm 			if (cc <= 0)
286e8eb82a8SPeter Wemm 				break;
287e1b4d8d0SSheldon Hearn 			(void)write(STDOUT_FILENO, obuf, cc);
288df53360cSBrian Somers 			if (rawout)
289df53360cSBrian Somers 				record(fscript, obuf, cc, 'o');
290df53360cSBrian Somers 			else
291e8eb82a8SPeter Wemm 				(void)fwrite(obuf, 1, cc, fscript);
292e8eb82a8SPeter Wemm 		}
293e8eb82a8SPeter Wemm 		tvec = time(0);
294e8eb82a8SPeter Wemm 		if (tvec - start >= flushtime) {
295e8eb82a8SPeter Wemm 			fflush(fscript);
296e8eb82a8SPeter Wemm 			start = tvec;
297e8eb82a8SPeter Wemm 		}
2988d105abcSTom Rhodes 		if (Fflg)
2998d105abcSTom Rhodes 			fflush(fscript);
300e8eb82a8SPeter Wemm 	}
301b1c66970SDag-Erling Smørgrav 	finish();
302b1c66970SDag-Erling Smørgrav 	done(0);
3039b50d902SRodney W. Grimes }
3049b50d902SRodney W. Grimes 
305236d2f55SPhilippe Charnier static void
306f4ac32deSDavid Malone usage(void)
307236d2f55SPhilippe Charnier {
308e8eb82a8SPeter Wemm 	(void)fprintf(stderr,
3096cff4e07SDavid E. O'Brien 	    "usage: script [-adfkpqr] [-t time] [file [command ...]]\n");
310236d2f55SPhilippe Charnier 	exit(1);
311236d2f55SPhilippe Charnier }
312236d2f55SPhilippe Charnier 
313e8efeec6SEd Schouten static void
314f4ac32deSDavid Malone finish(void)
3159b50d902SRodney W. Grimes {
316e1e9ba33SEd Schouten 	int e, status;
3179b50d902SRodney W. Grimes 
318e1e9ba33SEd Schouten 	if (waitpid(child, &status, 0) == child) {
319b1c66970SDag-Erling Smørgrav 		if (WIFEXITED(status))
320b1c66970SDag-Erling Smørgrav 			e = WEXITSTATUS(status);
321b1c66970SDag-Erling Smørgrav 		else if (WIFSIGNALED(status))
322b1c66970SDag-Erling Smørgrav 			e = WTERMSIG(status);
323b1c66970SDag-Erling Smørgrav 		else /* can't happen */
324b1c66970SDag-Erling Smørgrav 			e = 1;
325b1c66970SDag-Erling Smørgrav 		done(e);
3269b50d902SRodney W. Grimes 	}
327e1e9ba33SEd Schouten }
3289b50d902SRodney W. Grimes 
329e8efeec6SEd Schouten static void
330f4ac32deSDavid Malone doshell(char **av)
3319b50d902SRodney W. Grimes {
332b139689cSDavid Malone 	const char *shell;
3339b50d902SRodney W. Grimes 
3349b50d902SRodney W. Grimes 	shell = getenv("SHELL");
3359b50d902SRodney W. Grimes 	if (shell == NULL)
3369b50d902SRodney W. Grimes 		shell = _PATH_BSHELL;
3379b50d902SRodney W. Grimes 
3389b50d902SRodney W. Grimes 	(void)close(master);
3399b50d902SRodney W. Grimes 	(void)fclose(fscript);
3406cff4e07SDavid E. O'Brien 	free(fmfname);
3419b50d902SRodney W. Grimes 	login_tty(slave);
3429b91846cSDavid E. O'Brien 	setenv("SCRIPT", fname, 1);
343b1c66970SDag-Erling Smørgrav 	if (av[0]) {
3440e604e98SPeter Wemm 		execvp(av[0], av);
345b7ffba17SKris Kennaway 		warn("%s", av[0]);
346b1c66970SDag-Erling Smørgrav 	} else {
3477bc6d015SBrian Somers 		execl(shell, shell, "-i", (char *)NULL);
348b7ffba17SKris Kennaway 		warn("%s", shell);
349b1c66970SDag-Erling Smørgrav 	}
35095d13d1bSBryan Drewery 	exit(1);
3519b50d902SRodney W. Grimes }
3529b50d902SRodney W. Grimes 
353e8efeec6SEd Schouten static void
354f4ac32deSDavid Malone done(int eno)
3559b50d902SRodney W. Grimes {
3569b50d902SRodney W. Grimes 	time_t tvec;
3579b50d902SRodney W. Grimes 
358e292a0e3SColin Percival 	if (ttyflg)
359e8eb82a8SPeter Wemm 		(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
3609b50d902SRodney W. Grimes 	tvec = time(NULL);
361df53360cSBrian Somers 	if (rawout)
362df53360cSBrian Somers 		record(fscript, NULL, 0, 'e');
363e8eb82a8SPeter Wemm 	if (!qflg) {
36466d93438SBryan Drewery 		if (!rawout) {
36566d93438SBryan Drewery 			if (showexit)
36666d93438SBryan Drewery 				(void)fprintf(fscript, "\nCommand exit status:"
36766d93438SBryan Drewery 				    " %d", eno);
368df53360cSBrian Somers 			(void)fprintf(fscript,"\nScript done on %s",
369df53360cSBrian Somers 			    ctime(&tvec));
37066d93438SBryan Drewery 		}
371e8eb82a8SPeter Wemm 		(void)printf("\nScript done, output file is %s\n", fname);
3726cff4e07SDavid E. O'Brien 		if (fflg) {
3736cff4e07SDavid E. O'Brien 			(void)printf("Filemon done, output file is %s\n",
3746cff4e07SDavid E. O'Brien 			    fmfname);
3756cff4e07SDavid E. O'Brien 		}
376e8eb82a8SPeter Wemm 	}
3779b50d902SRodney W. Grimes 	(void)fclose(fscript);
3789b50d902SRodney W. Grimes 	(void)close(master);
379b1c66970SDag-Erling Smørgrav 	exit(eno);
3809b50d902SRodney W. Grimes }
381df53360cSBrian Somers 
382df53360cSBrian Somers static void
383df53360cSBrian Somers record(FILE *fp, char *buf, size_t cc, int direction)
384df53360cSBrian Somers {
385df53360cSBrian Somers 	struct iovec iov[2];
386df53360cSBrian Somers 	struct stamp stamp;
387df53360cSBrian Somers 	struct timeval tv;
388df53360cSBrian Somers 
389df53360cSBrian Somers 	(void)gettimeofday(&tv, NULL);
390df53360cSBrian Somers 	stamp.scr_len = cc;
391df53360cSBrian Somers 	stamp.scr_sec = tv.tv_sec;
392df53360cSBrian Somers 	stamp.scr_usec = tv.tv_usec;
393df53360cSBrian Somers 	stamp.scr_direction = direction;
394df53360cSBrian Somers 	iov[0].iov_len = sizeof(stamp);
395df53360cSBrian Somers 	iov[0].iov_base = &stamp;
396df53360cSBrian Somers 	iov[1].iov_len = cc;
397df53360cSBrian Somers 	iov[1].iov_base = buf;
398df53360cSBrian Somers 	if (writev(fileno(fp), &iov[0], 2) == -1)
399df53360cSBrian Somers 		err(1, "writev");
400df53360cSBrian Somers }
401df53360cSBrian Somers 
402df53360cSBrian Somers static void
403df53360cSBrian Somers consume(FILE *fp, off_t len, char *buf, int reg)
404df53360cSBrian Somers {
405df53360cSBrian Somers 	size_t l;
406df53360cSBrian Somers 
407df53360cSBrian Somers 	if (reg) {
408df53360cSBrian Somers 		if (fseeko(fp, len, SEEK_CUR) == -1)
409df53360cSBrian Somers 			err(1, NULL);
410df53360cSBrian Somers 	}
411df53360cSBrian Somers 	else {
412df53360cSBrian Somers 		while (len > 0) {
413df53360cSBrian Somers 			l = MIN(DEF_BUF, len);
414df53360cSBrian Somers 			if (fread(buf, sizeof(char), l, fp) != l)
415df53360cSBrian Somers 				err(1, "cannot read buffer");
416df53360cSBrian Somers 			len -= l;
417df53360cSBrian Somers 		}
418df53360cSBrian Somers 	}
419df53360cSBrian Somers }
420df53360cSBrian Somers 
421df53360cSBrian Somers #define swapstamp(stamp) do { \
422df53360cSBrian Somers 	if (stamp.scr_direction > 0xff) { \
423df53360cSBrian Somers 		stamp.scr_len = bswap64(stamp.scr_len); \
424df53360cSBrian Somers 		stamp.scr_sec = bswap64(stamp.scr_sec); \
425df53360cSBrian Somers 		stamp.scr_usec = bswap32(stamp.scr_usec); \
426df53360cSBrian Somers 		stamp.scr_direction = bswap32(stamp.scr_direction); \
427df53360cSBrian Somers 	} \
428df53360cSBrian Somers } while (0/*CONSTCOND*/)
429df53360cSBrian Somers 
430df53360cSBrian Somers static void
43169bc4fa9SMark Johnston termset(void)
43269bc4fa9SMark Johnston {
43369bc4fa9SMark Johnston 	struct termios traw;
43469bc4fa9SMark Johnston 
43569bc4fa9SMark Johnston 	if (tcgetattr(STDOUT_FILENO, &tt) == -1) {
436*8bc30d2aSMark Johnston 		if (errno != ENOTTY) /* For debugger. */
437*8bc30d2aSMark Johnston 			err(1, "tcgetattr");
43869bc4fa9SMark Johnston 		return;
43969bc4fa9SMark Johnston 	}
44069bc4fa9SMark Johnston 	ttyflg = 1;
44169bc4fa9SMark Johnston 	traw = tt;
44269bc4fa9SMark Johnston 	cfmakeraw(&traw);
44369bc4fa9SMark Johnston 	traw.c_lflag |= ISIG;
44469bc4fa9SMark Johnston 	(void)tcsetattr(STDOUT_FILENO, TCSANOW, &traw);
44569bc4fa9SMark Johnston }
44669bc4fa9SMark Johnston 
44769bc4fa9SMark Johnston static void
44869bc4fa9SMark Johnston termreset(void)
44969bc4fa9SMark Johnston {
45069bc4fa9SMark Johnston 	if (ttyflg) {
45169bc4fa9SMark Johnston 		tcsetattr(STDOUT_FILENO, TCSADRAIN, &tt);
45269bc4fa9SMark Johnston 		ttyflg = 0;
45369bc4fa9SMark Johnston 	}
45469bc4fa9SMark Johnston }
45569bc4fa9SMark Johnston 
45669bc4fa9SMark Johnston static void
457df53360cSBrian Somers playback(FILE *fp)
458df53360cSBrian Somers {
459df53360cSBrian Somers 	struct timespec tsi, tso;
460df53360cSBrian Somers 	struct stamp stamp;
461df53360cSBrian Somers 	struct stat pst;
462df53360cSBrian Somers 	char buf[DEF_BUF];
463df53360cSBrian Somers 	off_t nread, save_len;
464df53360cSBrian Somers 	size_t l;
465df53360cSBrian Somers 	time_t tclock;
466df53360cSBrian Somers 	int reg;
467df53360cSBrian Somers 
468df53360cSBrian Somers 	if (fstat(fileno(fp), &pst) == -1)
469df53360cSBrian Somers 		err(1, "fstat failed");
470df53360cSBrian Somers 
471df53360cSBrian Somers 	reg = S_ISREG(pst.st_mode);
472df53360cSBrian Somers 
473df53360cSBrian Somers 	for (nread = 0; !reg || nread < pst.st_size; nread += save_len) {
474df53360cSBrian Somers 		if (fread(&stamp, sizeof(stamp), 1, fp) != 1) {
475df53360cSBrian Somers 			if (reg)
476df53360cSBrian Somers 				err(1, "reading playback header");
477df53360cSBrian Somers 			else
478df53360cSBrian Somers 				break;
479df53360cSBrian Somers 		}
480df53360cSBrian Somers 		swapstamp(stamp);
481df53360cSBrian Somers 		save_len = sizeof(stamp);
482df53360cSBrian Somers 
483df53360cSBrian Somers 		if (reg && stamp.scr_len >
484df53360cSBrian Somers 		    (uint64_t)(pst.st_size - save_len) - nread)
485df53360cSBrian Somers 			errx(1, "invalid stamp");
486df53360cSBrian Somers 
487df53360cSBrian Somers 		save_len += stamp.scr_len;
488df53360cSBrian Somers 		tclock = stamp.scr_sec;
489df53360cSBrian Somers 		tso.tv_sec = stamp.scr_sec;
490df53360cSBrian Somers 		tso.tv_nsec = stamp.scr_usec * 1000;
491df53360cSBrian Somers 
492df53360cSBrian Somers 		switch (stamp.scr_direction) {
493df53360cSBrian Somers 		case 's':
494df53360cSBrian Somers 			if (!qflg)
495df53360cSBrian Somers 			    (void)printf("Script started on %s",
496df53360cSBrian Somers 				ctime(&tclock));
497df53360cSBrian Somers 			tsi = tso;
498df53360cSBrian Somers 			(void)consume(fp, stamp.scr_len, buf, reg);
49969bc4fa9SMark Johnston 			termset();
50069bc4fa9SMark Johnston 			atexit(termreset);
501df53360cSBrian Somers 			break;
502df53360cSBrian Somers 		case 'e':
50369bc4fa9SMark Johnston 			termreset();
504df53360cSBrian Somers 			if (!qflg)
505df53360cSBrian Somers 				(void)printf("\nScript done on %s",
506df53360cSBrian Somers 				    ctime(&tclock));
507df53360cSBrian Somers 			(void)consume(fp, stamp.scr_len, buf, reg);
508df53360cSBrian Somers 			break;
509df53360cSBrian Somers 		case 'i':
510df53360cSBrian Somers 			/* throw input away */
511df53360cSBrian Somers 			(void)consume(fp, stamp.scr_len, buf, reg);
512df53360cSBrian Somers 			break;
513df53360cSBrian Somers 		case 'o':
514df53360cSBrian Somers 			tsi.tv_sec = tso.tv_sec - tsi.tv_sec;
515df53360cSBrian Somers 			tsi.tv_nsec = tso.tv_nsec - tsi.tv_nsec;
516df53360cSBrian Somers 			if (tsi.tv_nsec < 0) {
517df53360cSBrian Somers 				tsi.tv_sec -= 1;
518df53360cSBrian Somers 				tsi.tv_nsec += 1000000000;
519df53360cSBrian Somers 			}
520df53360cSBrian Somers 			if (usesleep)
521df53360cSBrian Somers 				(void)nanosleep(&tsi, NULL);
522df53360cSBrian Somers 			tsi = tso;
523df53360cSBrian Somers 			while (stamp.scr_len > 0) {
524df53360cSBrian Somers 				l = MIN(DEF_BUF, stamp.scr_len);
525df53360cSBrian Somers 				if (fread(buf, sizeof(char), l, fp) != l)
526df53360cSBrian Somers 					err(1, "cannot read buffer");
527df53360cSBrian Somers 
528df53360cSBrian Somers 				(void)write(STDOUT_FILENO, buf, l);
529df53360cSBrian Somers 				stamp.scr_len -= l;
530df53360cSBrian Somers 			}
531df53360cSBrian Somers 			break;
532df53360cSBrian Somers 		default:
533df53360cSBrian Somers 			errx(1, "invalid direction");
534df53360cSBrian Somers 		}
535df53360cSBrian Somers 	}
536df53360cSBrian Somers 	(void)fclose(fp);
537df53360cSBrian Somers 	exit(0);
538df53360cSBrian Somers }
539