xref: /freebsd/usr.bin/script/script.c (revision 4294ff16a71fdc03ab7b1dc1df70caa25488d853)
1 /*
2  * Copyright (c) 1980, 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 
36 __FBSDID("$FreeBSD$");
37 
38 #ifndef lint
39 static const char copyright[] =
40 "@(#) Copyright (c) 1980, 1992, 1993\n\
41 	The Regents of the University of California.  All rights reserved.\n";
42 #endif
43 
44 #ifndef lint
45 static const char sccsid[] = "@(#)script.c	8.1 (Berkeley) 6/6/93";
46 #endif
47 
48 #include <sys/types.h>
49 #include <sys/wait.h>
50 #include <sys/stat.h>
51 #include <sys/ioctl.h>
52 #include <sys/time.h>
53 
54 #include <err.h>
55 #include <errno.h>
56 #include <fcntl.h>
57 #include <libutil.h>
58 #include <paths.h>
59 #include <signal.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <termios.h>
64 #include <unistd.h>
65 
66 FILE	*fscript;
67 int	master, slave;
68 int	child;
69 const char *fname;
70 int	qflg, ttyflg;
71 
72 struct	termios tt;
73 
74 void	done(int) __dead2;
75 void	dooutput(void);
76 void	doshell(char **);
77 void	fail(void);
78 void	finish(void);
79 static void usage(void);
80 
81 int
82 main(int argc, char *argv[])
83 {
84 	int cc;
85 	struct termios rtt, stt;
86 	struct winsize win;
87 	int aflg, kflg, ch, n;
88 	struct timeval tv, *tvp;
89 	time_t tvec, start;
90 	char obuf[BUFSIZ];
91 	char ibuf[BUFSIZ];
92 	fd_set rfd;
93 	int flushtime = 30;
94 
95 	aflg = kflg = 0;
96 	while ((ch = getopt(argc, argv, "aqkt:")) != -1)
97 		switch(ch) {
98 		case 'a':
99 			aflg = 1;
100 			break;
101 		case 'q':
102 			qflg = 1;
103 			break;
104 		case 'k':
105 			kflg = 1;
106 			break;
107 		case 't':
108 			flushtime = atoi(optarg);
109 			if (flushtime < 0)
110 				err(1, "invalid flush time %d", flushtime);
111 			break;
112 		case '?':
113 		default:
114 			usage();
115 		}
116 	argc -= optind;
117 	argv += optind;
118 
119 	if (argc > 0) {
120 		fname = argv[0];
121 		argv++;
122 		argc--;
123 	} else
124 		fname = "typescript";
125 
126 	if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL)
127 		err(1, "%s", fname);
128 
129 	if (ttyflg = isatty(STDIN_FILENO)) {
130 		if (tcgetattr(STDIN_FILENO, &tt) == -1)
131 			err(1, "tcgetattr");
132 		if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1)
133 			err(1, "ioctl");
134 		if (openpty(&master, &slave, NULL, &tt, &win) == -1)
135 			err(1, "openpty");
136 	} else {
137 		if (openpty(&master, &slave, NULL, NULL, NULL) == -1)
138 			err(1, "openpty");
139 	}
140 
141 	if (!qflg) {
142 		tvec = time(NULL);
143 		(void)printf("Script started, output file is %s\n", fname);
144 		(void)fprintf(fscript, "Script started on %s", ctime(&tvec));
145 		fflush(fscript);
146 	}
147 	if (ttyflg) {
148 		rtt = tt;
149 		cfmakeraw(&rtt);
150 		rtt.c_lflag &= ~ECHO;
151 		(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
152 	}
153 
154 	child = fork();
155 	if (child < 0) {
156 		warn("fork");
157 		done(1);
158 	}
159 	if (child == 0)
160 		doshell(argv);
161 
162 	if (flushtime > 0)
163 		tvp = &tv;
164 	else
165 		tvp = NULL;
166 
167 	start = time(0);
168 	FD_ZERO(&rfd);
169 	for (;;) {
170 		FD_SET(master, &rfd);
171 		if (argv[0] == NULL)
172 			FD_SET(STDIN_FILENO, &rfd);
173 		if (flushtime > 0) {
174 			tv.tv_sec = flushtime;
175 			tv.tv_usec = 0;
176 		}
177 		n = select(master + 1, &rfd, 0, 0, tvp);
178 		if (n < 0 && errno != EINTR)
179 			break;
180 		if (n > 0 && FD_ISSET(STDIN_FILENO, &rfd)) {
181 			cc = read(STDIN_FILENO, ibuf, BUFSIZ);
182 			if (cc <= 0)
183 				break;
184 			if (cc > 0) {
185 				(void)write(master, ibuf, cc);
186 				if (kflg && tcgetattr(master, &stt) >= 0 &&
187 				    ((stt.c_lflag & ECHO) == 0)) {
188 					(void)fwrite(ibuf, 1, cc, fscript);
189 				}
190 			}
191 		}
192 		if (n > 0 && FD_ISSET(master, &rfd)) {
193 			cc = read(master, obuf, sizeof (obuf));
194 			if (cc <= 0)
195 				break;
196 			(void)write(STDOUT_FILENO, obuf, cc);
197 			(void)fwrite(obuf, 1, cc, fscript);
198 		}
199 		tvec = time(0);
200 		if (tvec - start >= flushtime) {
201 			fflush(fscript);
202 			start = tvec;
203 		}
204 	}
205 	finish();
206 	done(0);
207 }
208 
209 static void
210 usage(void)
211 {
212 	(void)fprintf(stderr,
213 	    "usage: script [-akq] [-t time] [file [command ...]]\n");
214 	exit(1);
215 }
216 
217 void
218 finish(void)
219 {
220 	pid_t pid;
221 	int die, e, status;
222 
223 	die = e = 0;
224 	while ((pid = wait3(&status, WNOHANG, 0)) > 0)
225 	        if (pid == child) {
226 			die = 1;
227 			if (WIFEXITED(status))
228 				e = WEXITSTATUS(status);
229 			else if (WIFSIGNALED(status))
230 				e = WTERMSIG(status);
231 			else /* can't happen */
232 				e = 1;
233 		}
234 
235 	if (die)
236 		done(e);
237 }
238 
239 void
240 doshell(char **av)
241 {
242 	const char *shell;
243 
244 	shell = getenv("SHELL");
245 	if (shell == NULL)
246 		shell = _PATH_BSHELL;
247 
248 	(void)close(master);
249 	(void)fclose(fscript);
250 	login_tty(slave);
251 	if (av[0]) {
252 		execvp(av[0], av);
253 		warn("%s", av[0]);
254 	} else {
255 		execl(shell, shell, "-i", (char *)NULL);
256 		warn("%s", shell);
257 	}
258 	fail();
259 }
260 
261 void
262 fail(void)
263 {
264 	(void)kill(0, SIGTERM);
265 	done(1);
266 }
267 
268 void
269 done(int eno)
270 {
271 	time_t tvec;
272 
273 	if (ttyflg)
274 		(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
275 	tvec = time(NULL);
276 	if (!qflg) {
277 		(void)fprintf(fscript,"\nScript done on %s", ctime(&tvec));
278 		(void)printf("\nScript done, output file is %s\n", fname);
279 	}
280 	(void)fclose(fscript);
281 	(void)close(master);
282 	exit(eno);
283 }
284