xref: /freebsd/usr.bin/script/script.c (revision a3cf0ef5a295c885c895fabfd56470c0d1db322d)
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 static FILE *fscript;
67 static int master, slave;
68 static int child;
69 static const char *fname;
70 static int qflg, ttyflg;
71 
72 static struct termios tt;
73 
74 static void done(int) __dead2;
75 static void doshell(char **);
76 static void fail(void);
77 static void finish(void);
78 static void usage(void);
79 
80 int
81 main(int argc, char *argv[])
82 {
83 	int cc;
84 	struct termios rtt, stt;
85 	struct winsize win;
86 	int aflg, kflg, ch, n;
87 	struct timeval tv, *tvp;
88 	time_t tvec, start;
89 	char obuf[BUFSIZ];
90 	char ibuf[BUFSIZ];
91 	fd_set rfd;
92 	int flushtime = 30;
93 
94 	aflg = kflg = 0;
95 	while ((ch = getopt(argc, argv, "aqkt:")) != -1)
96 		switch(ch) {
97 		case 'a':
98 			aflg = 1;
99 			break;
100 		case 'q':
101 			qflg = 1;
102 			break;
103 		case 'k':
104 			kflg = 1;
105 			break;
106 		case 't':
107 			flushtime = atoi(optarg);
108 			if (flushtime < 0)
109 				err(1, "invalid flush time %d", flushtime);
110 			break;
111 		case '?':
112 		default:
113 			usage();
114 		}
115 	argc -= optind;
116 	argv += optind;
117 
118 	if (argc > 0) {
119 		fname = argv[0];
120 		argv++;
121 		argc--;
122 	} else
123 		fname = "typescript";
124 
125 	if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL)
126 		err(1, "%s", fname);
127 
128 	if ((ttyflg = isatty(STDIN_FILENO)) != 0) {
129 		if (tcgetattr(STDIN_FILENO, &tt) == -1)
130 			err(1, "tcgetattr");
131 		if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1)
132 			err(1, "ioctl");
133 		if (openpty(&master, &slave, NULL, &tt, &win) == -1)
134 			err(1, "openpty");
135 	} else {
136 		if (openpty(&master, &slave, NULL, NULL, NULL) == -1)
137 			err(1, "openpty");
138 	}
139 
140 	if (!qflg) {
141 		tvec = time(NULL);
142 		(void)printf("Script started, output file is %s\n", fname);
143 		(void)fprintf(fscript, "Script started on %s", ctime(&tvec));
144 		fflush(fscript);
145 	}
146 	if (ttyflg) {
147 		rtt = tt;
148 		cfmakeraw(&rtt);
149 		rtt.c_lflag &= ~ECHO;
150 		(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
151 	}
152 
153 	child = fork();
154 	if (child < 0) {
155 		warn("fork");
156 		done(1);
157 	}
158 	if (child == 0)
159 		doshell(argv);
160 	close(slave);
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 		FD_SET(STDIN_FILENO, &rfd);
172 		if (flushtime > 0) {
173 			tv.tv_sec = flushtime;
174 			tv.tv_usec = 0;
175 		}
176 		n = select(master + 1, &rfd, 0, 0, tvp);
177 		if (n < 0 && errno != EINTR)
178 			break;
179 		if (n > 0 && FD_ISSET(STDIN_FILENO, &rfd)) {
180 			cc = read(STDIN_FILENO, ibuf, BUFSIZ);
181 			if (cc < 0)
182 				break;
183 			if (cc == 0)
184 				(void)write(master, ibuf, 0);
185 			if (cc > 0) {
186 				(void)write(master, ibuf, cc);
187 				if (kflg && tcgetattr(master, &stt) >= 0 &&
188 				    ((stt.c_lflag & ECHO) == 0)) {
189 					(void)fwrite(ibuf, 1, cc, fscript);
190 				}
191 			}
192 		}
193 		if (n > 0 && FD_ISSET(master, &rfd)) {
194 			cc = read(master, obuf, sizeof (obuf));
195 			if (cc <= 0)
196 				break;
197 			(void)write(STDOUT_FILENO, obuf, cc);
198 			(void)fwrite(obuf, 1, cc, fscript);
199 		}
200 		tvec = time(0);
201 		if (tvec - start >= flushtime) {
202 			fflush(fscript);
203 			start = tvec;
204 		}
205 	}
206 	finish();
207 	done(0);
208 }
209 
210 static void
211 usage(void)
212 {
213 	(void)fprintf(stderr,
214 	    "usage: script [-akq] [-t time] [file [command ...]]\n");
215 	exit(1);
216 }
217 
218 static void
219 finish(void)
220 {
221 	int e, status;
222 
223 	if (waitpid(child, &status, 0) == child) {
224 		if (WIFEXITED(status))
225 			e = WEXITSTATUS(status);
226 		else if (WIFSIGNALED(status))
227 			e = WTERMSIG(status);
228 		else /* can't happen */
229 			e = 1;
230 		done(e);
231 	}
232 }
233 
234 static void
235 doshell(char **av)
236 {
237 	const char *shell;
238 	int k;
239 
240 	shell = getenv("SHELL");
241 	if (shell == NULL)
242 		shell = _PATH_BSHELL;
243 
244 	if (av[0])
245 		for (k = 0 ; av[k] ; ++k)
246 			fprintf(fscript, "%s%s", k ? " " : "", av[k]);
247 		fprintf(fscript, "\r\n");
248 
249 	(void)close(master);
250 	(void)fclose(fscript);
251 	login_tty(slave);
252 	setenv("SCRIPT", fname, 1);
253 	if (av[0]) {
254 		execvp(av[0], av);
255 		warn("%s", av[0]);
256 	} else {
257 		execl(shell, shell, "-i", (char *)NULL);
258 		warn("%s", shell);
259 	}
260 	fail();
261 }
262 
263 static void
264 fail(void)
265 {
266 	(void)kill(0, SIGTERM);
267 	done(1);
268 }
269 
270 static void
271 done(int eno)
272 {
273 	time_t tvec;
274 
275 	if (ttyflg)
276 		(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
277 	tvec = time(NULL);
278 	if (!qflg) {
279 		(void)fprintf(fscript,"\nScript done on %s", ctime(&tvec));
280 		(void)printf("\nScript done, output file is %s\n", fname);
281 	}
282 	(void)fclose(fscript);
283 	(void)close(master);
284 	exit(eno);
285 }
286