xref: /freebsd/usr.bin/script/script.c (revision 5ae50c80859f7da940bfb972337845a59158e81d)
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 		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, cc);
185 				if (kflg && tcgetattr(master, &stt) >= 0 &&
186 				    ((stt.c_lflag & ECHO) == 0)) {
187 					(void)fwrite(ibuf, 1, cc, fscript);
188 				}
189 			}
190 		}
191 		if (n > 0 && FD_ISSET(master, &rfd)) {
192 			cc = read(master, obuf, sizeof (obuf));
193 			if (cc <= 0)
194 				break;
195 			(void)write(STDOUT_FILENO, obuf, cc);
196 			(void)fwrite(obuf, 1, cc, fscript);
197 		}
198 		tvec = time(0);
199 		if (tvec - start >= flushtime) {
200 			fflush(fscript);
201 			start = tvec;
202 		}
203 	}
204 	finish();
205 	done(0);
206 }
207 
208 static void
209 usage(void)
210 {
211 	(void)fprintf(stderr,
212 	    "usage: script [-akq] [-t time] [file [command ...]]\n");
213 	exit(1);
214 }
215 
216 void
217 finish(void)
218 {
219 	pid_t pid;
220 	int die, e, status;
221 
222 	die = e = 0;
223 	while ((pid = wait3(&status, WNOHANG, 0)) > 0)
224 	        if (pid == child) {
225 			die = 1;
226 			if (WIFEXITED(status))
227 				e = WEXITSTATUS(status);
228 			else if (WIFSIGNALED(status))
229 				e = WTERMSIG(status);
230 			else /* can't happen */
231 				e = 1;
232 		}
233 
234 	if (die)
235 		done(e);
236 }
237 
238 void
239 doshell(char **av)
240 {
241 	const char *shell;
242 
243 	shell = getenv("SHELL");
244 	if (shell == NULL)
245 		shell = _PATH_BSHELL;
246 
247 	(void)close(master);
248 	(void)fclose(fscript);
249 	login_tty(slave);
250 	if (av[0]) {
251 		execvp(av[0], av);
252 		warn("%s", av[0]);
253 	} else {
254 		execl(shell, shell, "-i", (char *)NULL);
255 		warn("%s", shell);
256 	}
257 	fail();
258 }
259 
260 void
261 fail(void)
262 {
263 	(void)kill(0, SIGTERM);
264 	done(1);
265 }
266 
267 void
268 done(int eno)
269 {
270 	time_t tvec;
271 
272 	if (ttyflg)
273 		(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
274 	tvec = time(NULL);
275 	if (!qflg) {
276 		(void)fprintf(fscript,"\nScript done on %s", ctime(&tvec));
277 		(void)printf("\nScript done, output file is %s\n", fname);
278 	}
279 	(void)fclose(fscript);
280 	(void)close(master);
281 	exit(eno);
282 }
283