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