xref: /freebsd/usr.bin/script/script.c (revision e292a0e3f1426e75dca5476fb138f368b489a3f0)
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 
669b50d902SRodney W. Grimes FILE	*fscript;
679b50d902SRodney W. Grimes int	master, slave;
68e8eb82a8SPeter Wemm int	child;
69b139689cSDavid Malone const char *fname;
70e292a0e3SColin Percival int	qflg, ttyflg;
719b50d902SRodney W. Grimes 
729b50d902SRodney W. Grimes struct	termios tt;
739b50d902SRodney W. Grimes 
743f330d7dSWarner Losh void	done(int) __dead2;
753f330d7dSWarner Losh void	dooutput(void);
763f330d7dSWarner Losh void	doshell(char **);
773f330d7dSWarner Losh void	fail(void);
783f330d7dSWarner Losh void	finish(void);
793f330d7dSWarner Losh static void usage(void);
809b50d902SRodney W. Grimes 
819b50d902SRodney W. Grimes int
82f4ac32deSDavid Malone main(int argc, char *argv[])
839b50d902SRodney W. Grimes {
84d20f95e1SMark Murray 	int cc;
85e8eb82a8SPeter Wemm 	struct termios rtt, stt;
869b50d902SRodney W. Grimes 	struct winsize win;
87e8eb82a8SPeter Wemm 	int aflg, kflg, ch, n;
88e8eb82a8SPeter Wemm 	struct timeval tv, *tvp;
89e8eb82a8SPeter Wemm 	time_t tvec, start;
90e8eb82a8SPeter Wemm 	char obuf[BUFSIZ];
919b50d902SRodney W. Grimes 	char ibuf[BUFSIZ];
92e8eb82a8SPeter Wemm 	fd_set rfd;
93e8eb82a8SPeter Wemm 	int flushtime = 30;
949b50d902SRodney W. Grimes 
95e8eb82a8SPeter Wemm 	aflg = kflg = 0;
96e8eb82a8SPeter Wemm 	while ((ch = getopt(argc, argv, "aqkt:")) != -1)
979b50d902SRodney W. Grimes 		switch(ch) {
989b50d902SRodney W. Grimes 		case 'a':
999b50d902SRodney W. Grimes 			aflg = 1;
1009b50d902SRodney W. Grimes 			break;
10151afb8dfSPeter Wemm 		case 'q':
1020e604e98SPeter Wemm 			qflg = 1;
10351afb8dfSPeter Wemm 			break;
104e8eb82a8SPeter Wemm 		case 'k':
105e8eb82a8SPeter Wemm 			kflg = 1;
106e8eb82a8SPeter Wemm 			break;
107e8eb82a8SPeter Wemm 		case 't':
108e8eb82a8SPeter Wemm 			flushtime = atoi(optarg);
109e8eb82a8SPeter Wemm 			if (flushtime < 0)
110e8eb82a8SPeter Wemm 				err(1, "invalid flush time %d", flushtime);
111e8eb82a8SPeter Wemm 			break;
1129b50d902SRodney W. Grimes 		case '?':
1139b50d902SRodney W. Grimes 		default:
114236d2f55SPhilippe Charnier 			usage();
1159b50d902SRodney W. Grimes 		}
1169b50d902SRodney W. Grimes 	argc -= optind;
1179b50d902SRodney W. Grimes 	argv += optind;
1189b50d902SRodney W. Grimes 
11951afb8dfSPeter Wemm 	if (argc > 0) {
1209b50d902SRodney W. Grimes 		fname = argv[0];
12151afb8dfSPeter Wemm 		argv++;
12251afb8dfSPeter Wemm 		argc--;
12351afb8dfSPeter Wemm 	} else
1249b50d902SRodney W. Grimes 		fname = "typescript";
1259b50d902SRodney W. Grimes 
1269b50d902SRodney W. Grimes 	if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL)
127236d2f55SPhilippe Charnier 		err(1, "%s", fname);
1289b50d902SRodney W. Grimes 
129e292a0e3SColin Percival 	if (ttyflg = isatty(STDIN_FILENO)) {
130e292a0e3SColin Percival 		if (tcgetattr(STDIN_FILENO, &tt) == -1)
131e292a0e3SColin Percival 			err(1, "tcgetattr");
132e292a0e3SColin Percival 		if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1)
133e292a0e3SColin Percival 			err(1, "ioctl");
1349b50d902SRodney W. Grimes 		if (openpty(&master, &slave, NULL, &tt, &win) == -1)
135236d2f55SPhilippe Charnier 			err(1, "openpty");
136e292a0e3SColin Percival 	} else {
137e292a0e3SColin Percival 		if (openpty(&master, &slave, NULL, NULL, NULL) == -1)
138e292a0e3SColin Percival 			err(1, "openpty");
139e292a0e3SColin Percival 	}
1409b50d902SRodney W. Grimes 
141e8eb82a8SPeter Wemm 	if (!qflg) {
142e8eb82a8SPeter Wemm 		tvec = time(NULL);
1439b50d902SRodney W. Grimes 		(void)printf("Script started, output file is %s\n", fname);
144e8eb82a8SPeter Wemm 		(void)fprintf(fscript, "Script started on %s", ctime(&tvec));
145e8eb82a8SPeter Wemm 		fflush(fscript);
146e8eb82a8SPeter Wemm 	}
147e292a0e3SColin Percival 	if (ttyflg) {
1489b50d902SRodney W. Grimes 		rtt = tt;
1499b50d902SRodney W. Grimes 		cfmakeraw(&rtt);
1509b50d902SRodney W. Grimes 		rtt.c_lflag &= ~ECHO;
1519b50d902SRodney W. Grimes 		(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
152e292a0e3SColin Percival 	}
1539b50d902SRodney W. Grimes 
1549b50d902SRodney W. Grimes 	child = fork();
1559b50d902SRodney W. Grimes 	if (child < 0) {
156236d2f55SPhilippe Charnier 		warn("fork");
157b1c66970SDag-Erling Smørgrav 		done(1);
1589b50d902SRodney W. Grimes 	}
159e8eb82a8SPeter Wemm 	if (child == 0)
16051afb8dfSPeter Wemm 		doshell(argv);
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);
171e292a0e3SColin Percival 		if (argv[0] == NULL)
172e8eb82a8SPeter Wemm 			FD_SET(STDIN_FILENO, &rfd);
173e8eb82a8SPeter Wemm 		if (flushtime > 0) {
174e8eb82a8SPeter Wemm 			tv.tv_sec = flushtime;
175e8eb82a8SPeter Wemm 			tv.tv_usec = 0;
176e8eb82a8SPeter Wemm 		}
177e8eb82a8SPeter Wemm 		n = select(master + 1, &rfd, 0, 0, tvp);
178e8eb82a8SPeter Wemm 		if (n < 0 && errno != EINTR)
179e8eb82a8SPeter Wemm 			break;
180e8eb82a8SPeter Wemm 		if (n > 0 && FD_ISSET(STDIN_FILENO, &rfd)) {
181e8eb82a8SPeter Wemm 			cc = read(STDIN_FILENO, ibuf, BUFSIZ);
182e8eb82a8SPeter Wemm 			if (cc <= 0)
183e8eb82a8SPeter Wemm 				break;
184e8eb82a8SPeter Wemm 			if (cc > 0) {
1859b50d902SRodney W. Grimes 				(void)write(master, ibuf, cc);
186e8eb82a8SPeter Wemm 				if (kflg && tcgetattr(master, &stt) >= 0 &&
187e8eb82a8SPeter Wemm 				    ((stt.c_lflag & ECHO) == 0)) {
188e8eb82a8SPeter Wemm 					(void)fwrite(ibuf, 1, cc, fscript);
189e8eb82a8SPeter Wemm 				}
190e8eb82a8SPeter Wemm 			}
191e8eb82a8SPeter Wemm 		}
192e8eb82a8SPeter Wemm 		if (n > 0 && FD_ISSET(master, &rfd)) {
193e8eb82a8SPeter Wemm 			cc = read(master, obuf, sizeof (obuf));
194e8eb82a8SPeter Wemm 			if (cc <= 0)
195e8eb82a8SPeter Wemm 				break;
196e1b4d8d0SSheldon Hearn 			(void)write(STDOUT_FILENO, obuf, cc);
197e8eb82a8SPeter Wemm 			(void)fwrite(obuf, 1, cc, fscript);
198e8eb82a8SPeter Wemm 		}
199e8eb82a8SPeter Wemm 		tvec = time(0);
200e8eb82a8SPeter Wemm 		if (tvec - start >= flushtime) {
201e8eb82a8SPeter Wemm 			fflush(fscript);
202e8eb82a8SPeter Wemm 			start = tvec;
203e8eb82a8SPeter Wemm 		}
204e8eb82a8SPeter Wemm 	}
205b1c66970SDag-Erling Smørgrav 	finish();
206b1c66970SDag-Erling Smørgrav 	done(0);
2079b50d902SRodney W. Grimes }
2089b50d902SRodney W. Grimes 
209236d2f55SPhilippe Charnier static void
210f4ac32deSDavid Malone usage(void)
211236d2f55SPhilippe Charnier {
212e8eb82a8SPeter Wemm 	(void)fprintf(stderr,
213e8eb82a8SPeter Wemm 	    "usage: script [-a] [-q] [-k] [-t time] [file] [command]\n");
214236d2f55SPhilippe Charnier 	exit(1);
215236d2f55SPhilippe Charnier }
216236d2f55SPhilippe Charnier 
2179b50d902SRodney W. Grimes void
218f4ac32deSDavid Malone finish(void)
2199b50d902SRodney W. Grimes {
220cb263c35SMike Barcroft 	pid_t pid;
221cb263c35SMike Barcroft 	int die, e, status;
2229b50d902SRodney W. Grimes 
223b1c66970SDag-Erling Smørgrav 	die = e = 0;
224cb263c35SMike Barcroft 	while ((pid = wait3(&status, WNOHANG, 0)) > 0)
225b1c66970SDag-Erling Smørgrav 	        if (pid == child) {
2269b50d902SRodney W. Grimes 			die = 1;
227b1c66970SDag-Erling Smørgrav 			if (WIFEXITED(status))
228b1c66970SDag-Erling Smørgrav 				e = WEXITSTATUS(status);
229b1c66970SDag-Erling Smørgrav 			else if (WIFSIGNALED(status))
230b1c66970SDag-Erling Smørgrav 				e = WTERMSIG(status);
231b1c66970SDag-Erling Smørgrav 			else /* can't happen */
232b1c66970SDag-Erling Smørgrav 				e = 1;
233b1c66970SDag-Erling Smørgrav 		}
2349b50d902SRodney W. Grimes 
2359b50d902SRodney W. Grimes 	if (die)
236b1c66970SDag-Erling Smørgrav 		done(e);
2379b50d902SRodney W. Grimes }
2389b50d902SRodney W. Grimes 
2399b50d902SRodney W. Grimes void
240f4ac32deSDavid Malone doshell(char **av)
2419b50d902SRodney W. Grimes {
242b139689cSDavid Malone 	const char *shell;
2439b50d902SRodney W. Grimes 
2449b50d902SRodney W. Grimes 	shell = getenv("SHELL");
2459b50d902SRodney W. Grimes 	if (shell == NULL)
2469b50d902SRodney W. Grimes 		shell = _PATH_BSHELL;
2479b50d902SRodney W. Grimes 
2489b50d902SRodney W. Grimes 	(void)close(master);
2499b50d902SRodney W. Grimes 	(void)fclose(fscript);
2509b50d902SRodney W. Grimes 	login_tty(slave);
251b1c66970SDag-Erling Smørgrav 	if (av[0]) {
2520e604e98SPeter Wemm 		execvp(av[0], av);
253b7ffba17SKris Kennaway 		warn("%s", av[0]);
254b1c66970SDag-Erling Smørgrav 	} else {
2557bc6d015SBrian Somers 		execl(shell, shell, "-i", (char *)NULL);
256b7ffba17SKris Kennaway 		warn("%s", shell);
257b1c66970SDag-Erling Smørgrav 	}
2589b50d902SRodney W. Grimes 	fail();
2599b50d902SRodney W. Grimes }
2609b50d902SRodney W. Grimes 
2619b50d902SRodney W. Grimes void
262f4ac32deSDavid Malone fail(void)
2639b50d902SRodney W. Grimes {
2649b50d902SRodney W. Grimes 	(void)kill(0, SIGTERM);
265b1c66970SDag-Erling Smørgrav 	done(1);
2669b50d902SRodney W. Grimes }
2679b50d902SRodney W. Grimes 
2689b50d902SRodney W. Grimes void
269f4ac32deSDavid Malone done(int eno)
2709b50d902SRodney W. Grimes {
2719b50d902SRodney W. Grimes 	time_t tvec;
2729b50d902SRodney W. Grimes 
273e292a0e3SColin Percival 	if (ttyflg)
274e8eb82a8SPeter Wemm 		(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
2759b50d902SRodney W. Grimes 	tvec = time(NULL);
276e8eb82a8SPeter Wemm 	if (!qflg) {
2779b50d902SRodney W. Grimes 		(void)fprintf(fscript,"\nScript done on %s", ctime(&tvec));
278e8eb82a8SPeter Wemm 		(void)printf("\nScript done, output file is %s\n", fname);
279e8eb82a8SPeter Wemm 	}
2809b50d902SRodney W. Grimes 	(void)fclose(fscript);
2819b50d902SRodney W. Grimes 	(void)close(master);
282b1c66970SDag-Erling Smørgrav 	exit(eno);
2839b50d902SRodney W. Grimes }
284