xref: /freebsd/usr.bin/script/script.c (revision b7ffba17f8c0492d8eb466f4537729bab2231286)
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;
96e8eb82a8SPeter Wemm 	int lastc = 1000;
979b50d902SRodney W. Grimes 
98e8eb82a8SPeter Wemm 	aflg = kflg = 0;
99e8eb82a8SPeter Wemm 	while ((ch = getopt(argc, argv, "aqkt:")) != -1)
1009b50d902SRodney W. Grimes 		switch(ch) {
1019b50d902SRodney W. Grimes 		case 'a':
1029b50d902SRodney W. Grimes 			aflg = 1;
1039b50d902SRodney W. Grimes 			break;
10451afb8dfSPeter Wemm 		case 'q':
1050e604e98SPeter Wemm 			qflg = 1;
10651afb8dfSPeter Wemm 			break;
107e8eb82a8SPeter Wemm 		case 'k':
108e8eb82a8SPeter Wemm 			kflg = 1;
109e8eb82a8SPeter Wemm 			break;
110e8eb82a8SPeter Wemm 		case 't':
111e8eb82a8SPeter Wemm 			flushtime = atoi(optarg);
112e8eb82a8SPeter Wemm 			if (flushtime < 0)
113e8eb82a8SPeter Wemm 				err(1, "invalid flush time %d", flushtime);
114e8eb82a8SPeter Wemm 			break;
1159b50d902SRodney W. Grimes 		case '?':
1169b50d902SRodney W. Grimes 		default:
117236d2f55SPhilippe Charnier 			usage();
1189b50d902SRodney W. Grimes 		}
1199b50d902SRodney W. Grimes 	argc -= optind;
1209b50d902SRodney W. Grimes 	argv += optind;
1219b50d902SRodney W. Grimes 
12251afb8dfSPeter Wemm 	if (argc > 0) {
1239b50d902SRodney W. Grimes 		fname = argv[0];
12451afb8dfSPeter Wemm 		argv++;
12551afb8dfSPeter Wemm 		argc--;
12651afb8dfSPeter Wemm 	} else
1279b50d902SRodney W. Grimes 		fname = "typescript";
1289b50d902SRodney W. Grimes 
1299b50d902SRodney W. Grimes 	if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL)
130236d2f55SPhilippe Charnier 		err(1, "%s", fname);
1319b50d902SRodney W. Grimes 
1329b50d902SRodney W. Grimes 	(void)tcgetattr(STDIN_FILENO, &tt);
1339b50d902SRodney W. Grimes 	(void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win);
1349b50d902SRodney W. Grimes 	if (openpty(&master, &slave, NULL, &tt, &win) == -1)
135236d2f55SPhilippe Charnier 		err(1, "openpty");
1369b50d902SRodney W. Grimes 
137e8eb82a8SPeter Wemm 	if (!qflg) {
138e8eb82a8SPeter Wemm 		tvec = time(NULL);
1399b50d902SRodney W. Grimes 		(void)printf("Script started, output file is %s\n", fname);
140e8eb82a8SPeter Wemm 		(void)fprintf(fscript, "Script started on %s", ctime(&tvec));
141e8eb82a8SPeter Wemm 		fflush(fscript);
142e8eb82a8SPeter Wemm 	}
1439b50d902SRodney W. Grimes 	rtt = tt;
1449b50d902SRodney W. Grimes 	cfmakeraw(&rtt);
1459b50d902SRodney W. Grimes 	rtt.c_lflag &= ~ECHO;
1469b50d902SRodney W. Grimes 	(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
1479b50d902SRodney W. Grimes 
1489b50d902SRodney W. Grimes 	child = fork();
1499b50d902SRodney W. Grimes 	if (child < 0) {
150236d2f55SPhilippe Charnier 		warn("fork");
151b1c66970SDag-Erling Smørgrav 		done(1);
1529b50d902SRodney W. Grimes 	}
153e8eb82a8SPeter Wemm 	if (child == 0)
15451afb8dfSPeter Wemm 		doshell(argv);
1559b50d902SRodney W. Grimes 
156e8eb82a8SPeter Wemm 	if (flushtime > 0)
157e8eb82a8SPeter Wemm 		tvp = &tv;
158e8eb82a8SPeter Wemm 	else
159e8eb82a8SPeter Wemm 		tvp = NULL;
160e8eb82a8SPeter Wemm 
161e8eb82a8SPeter Wemm 	start = time(0);
162e8eb82a8SPeter Wemm 	FD_ZERO(&rfd);
163e8eb82a8SPeter Wemm 	for (;;) {
164e8eb82a8SPeter Wemm 		FD_SET(master, &rfd);
165e8eb82a8SPeter Wemm 		FD_SET(STDIN_FILENO, &rfd);
166e8eb82a8SPeter Wemm 		if (flushtime > 0) {
167e8eb82a8SPeter Wemm 			tv.tv_sec = flushtime;
168e8eb82a8SPeter Wemm 			tv.tv_usec = 0;
169e8eb82a8SPeter Wemm 		}
170e8eb82a8SPeter Wemm 		n = select(master + 1, &rfd, 0, 0, tvp);
171e8eb82a8SPeter Wemm 		if (n < 0 && errno != EINTR)
172e8eb82a8SPeter Wemm 			break;
173e8eb82a8SPeter Wemm 		if (n > 0 && FD_ISSET(STDIN_FILENO, &rfd)) {
174e8eb82a8SPeter Wemm 			cc = read(STDIN_FILENO, ibuf, BUFSIZ);
175e8eb82a8SPeter Wemm 			if (cc <= 0)
176e8eb82a8SPeter Wemm 				break;
177e8eb82a8SPeter Wemm 			if (cc > 0) {
1789b50d902SRodney W. Grimes 				(void)write(master, ibuf, cc);
179e8eb82a8SPeter Wemm 				if (kflg && tcgetattr(master, &stt) >= 0 &&
180e8eb82a8SPeter Wemm 				    ((stt.c_lflag & ECHO) == 0)) {
181e8eb82a8SPeter Wemm 					(void)fwrite(ibuf, 1, cc, fscript);
182e8eb82a8SPeter Wemm 				}
183e8eb82a8SPeter Wemm 			}
184e8eb82a8SPeter Wemm 		}
185e8eb82a8SPeter Wemm 		if (n > 0 && FD_ISSET(master, &rfd)) {
186e8eb82a8SPeter Wemm 			cc = read(master, obuf, sizeof (obuf));
187e8eb82a8SPeter Wemm 			if (cc <= 0)
188e8eb82a8SPeter Wemm 				break;
189e8eb82a8SPeter Wemm 			(void)write(1, obuf, cc);
190e8eb82a8SPeter Wemm 			(void)fwrite(obuf, 1, cc, fscript);
191e8eb82a8SPeter Wemm 		}
192e8eb82a8SPeter Wemm 		tvec = time(0);
193e8eb82a8SPeter Wemm 		if (tvec - start >= flushtime) {
194e8eb82a8SPeter Wemm 			fflush(fscript);
195e8eb82a8SPeter Wemm 			start = tvec;
196e8eb82a8SPeter Wemm 		}
197e8eb82a8SPeter Wemm 	}
198b1c66970SDag-Erling Smørgrav 	finish();
199b1c66970SDag-Erling Smørgrav 	done(0);
2009b50d902SRodney W. Grimes }
2019b50d902SRodney W. Grimes 
202236d2f55SPhilippe Charnier static void
203236d2f55SPhilippe Charnier usage()
204236d2f55SPhilippe Charnier {
205e8eb82a8SPeter Wemm 	(void)fprintf(stderr,
206e8eb82a8SPeter Wemm 	    "usage: script [-a] [-q] [-k] [-t time] [file] [command]\n");
207236d2f55SPhilippe Charnier 	exit(1);
208236d2f55SPhilippe Charnier }
209236d2f55SPhilippe Charnier 
2109b50d902SRodney W. Grimes void
211b1c66970SDag-Erling Smørgrav finish()
2129b50d902SRodney W. Grimes {
213b1c66970SDag-Erling Smørgrav 	int die, e, pid;
2149b50d902SRodney W. Grimes 	union wait status;
2159b50d902SRodney W. Grimes 
216b1c66970SDag-Erling Smørgrav 	die = e = 0;
2179b50d902SRodney W. Grimes 	while ((pid = wait3((int *)&status, WNOHANG, 0)) > 0)
218b1c66970SDag-Erling Smørgrav 	        if (pid == child) {
2199b50d902SRodney W. Grimes 			die = 1;
220b1c66970SDag-Erling Smørgrav 			if (WIFEXITED(status))
221b1c66970SDag-Erling Smørgrav 				e = WEXITSTATUS(status);
222b1c66970SDag-Erling Smørgrav 			else if (WIFSIGNALED(status))
223b1c66970SDag-Erling Smørgrav 				e = WTERMSIG(status);
224b1c66970SDag-Erling Smørgrav 			else /* can't happen */
225b1c66970SDag-Erling Smørgrav 				e = 1;
226b1c66970SDag-Erling Smørgrav 		}
2279b50d902SRodney W. Grimes 
2289b50d902SRodney W. Grimes 	if (die)
229b1c66970SDag-Erling Smørgrav 		done(e);
2309b50d902SRodney W. Grimes }
2319b50d902SRodney W. Grimes 
2329b50d902SRodney W. Grimes void
23351afb8dfSPeter Wemm doshell(av)
23451afb8dfSPeter Wemm 	char **av;
2359b50d902SRodney W. Grimes {
2369b50d902SRodney W. Grimes 	char *shell;
2379b50d902SRodney W. Grimes 
2389b50d902SRodney W. Grimes 	shell = getenv("SHELL");
2399b50d902SRodney W. Grimes 	if (shell == NULL)
2409b50d902SRodney W. Grimes 		shell = _PATH_BSHELL;
2419b50d902SRodney W. Grimes 
2429b50d902SRodney W. Grimes 	(void)close(master);
2439b50d902SRodney W. Grimes 	(void)fclose(fscript);
2449b50d902SRodney W. Grimes 	login_tty(slave);
245b1c66970SDag-Erling Smørgrav 	if (av[0]) {
2460e604e98SPeter Wemm 		execvp(av[0], av);
247b7ffba17SKris Kennaway 		warn("%s", av[0]);
248b1c66970SDag-Erling Smørgrav 	} else {
2495cba8ccaSSheldon Hearn 		execl(shell, shell, "-i", NULL);
250b7ffba17SKris Kennaway 		warn("%s", shell);
251b1c66970SDag-Erling Smørgrav 	}
2529b50d902SRodney W. Grimes 	fail();
2539b50d902SRodney W. Grimes }
2549b50d902SRodney W. Grimes 
2559b50d902SRodney W. Grimes void
2569b50d902SRodney W. Grimes fail()
2579b50d902SRodney W. Grimes {
2589b50d902SRodney W. Grimes 	(void)kill(0, SIGTERM);
259b1c66970SDag-Erling Smørgrav 	done(1);
2609b50d902SRodney W. Grimes }
2619b50d902SRodney W. Grimes 
2629b50d902SRodney W. Grimes void
263b1c66970SDag-Erling Smørgrav done(eno)
264b1c66970SDag-Erling Smørgrav 	int eno;
2659b50d902SRodney W. Grimes {
2669b50d902SRodney W. Grimes 	time_t tvec;
2679b50d902SRodney W. Grimes 
268e8eb82a8SPeter Wemm 	(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
2699b50d902SRodney W. Grimes 	tvec = time(NULL);
270e8eb82a8SPeter Wemm 	if (!qflg) {
2719b50d902SRodney W. Grimes 		(void)fprintf(fscript,"\nScript done on %s", ctime(&tvec));
272e8eb82a8SPeter Wemm 		(void)printf("\nScript done, output file is %s\n", fname);
273e8eb82a8SPeter Wemm 	}
2749b50d902SRodney W. Grimes 	(void)fclose(fscript);
2759b50d902SRodney W. Grimes 	(void)close(master);
276b1c66970SDag-Erling Smørgrav 	exit(eno);
2779b50d902SRodney W. Grimes }
278