xref: /netbsd-src/usr.bin/script/script.c (revision 21e37cc72a480a47828990a439cde7ac9ffaf0c6)
1 /*	$NetBSD: script.c,v 1.9 2003/08/07 11:15:48 agc Exp $	*/
2 
3 /*
4  * Copyright (c) 1980, 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1992, 1993\n\
35 	The Regents of the University of California.  All rights reserved.\n");
36 #endif /* not lint */
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)script.c	8.1 (Berkeley) 6/6/93";
41 #endif
42 __RCSID("$NetBSD: script.c,v 1.9 2003/08/07 11:15:48 agc Exp $");
43 #endif /* not lint */
44 
45 #include <sys/types.h>
46 #include <sys/wait.h>
47 #include <sys/stat.h>
48 #include <sys/ioctl.h>
49 #include <sys/time.h>
50 #include <sys/uio.h>
51 
52 #include <err.h>
53 #include <errno.h>
54 #include <fcntl.h>
55 #include <paths.h>
56 #include <signal.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <termios.h>
61 #include <time.h>
62 #include <tzfile.h>
63 #include <unistd.h>
64 #include <util.h>
65 
66 struct stamp {
67 	uint64_t scr_len;	/* amount of data */
68 	uint64_t scr_sec;	/* time it arrived in seconds... */
69 	uint32_t scr_usec;	/* ...and microseconds */
70 	uint32_t scr_direction;	/* 'i', 'o', etc (also indicates endianness) */
71 };
72 
73 FILE	*fscript;
74 int	master, slave;
75 int	child, subchild;
76 int	outcc;
77 int	usesleep, rawout;
78 char	*fname;
79 
80 struct	termios tt;
81 
82 void	done __P((void));
83 void	dooutput __P((void));
84 void	doshell __P((void));
85 void	fail __P((void));
86 void	finish __P((int));
87 int	main __P((int, char **));
88 void	scriptflush __P((int));
89 void	record __P((FILE *, char *, size_t, int));
90 void	playback __P((FILE *));
91 
92 int
93 main(argc, argv)
94 	int argc;
95 	char *argv[];
96 {
97 	int cc;
98 	struct termios rtt;
99 	struct winsize win;
100 	int aflg, pflg, ch;
101 	char ibuf[BUFSIZ];
102 
103 	aflg = 0;
104 	pflg = 0;
105 	usesleep = 1;
106 	rawout = 0;
107 	while ((ch = getopt(argc, argv, "adpr")) != -1)
108 		switch(ch) {
109 		case 'a':
110 			aflg = 1;
111 			break;
112 		case 'd':
113 			usesleep = 0;
114 			break;
115 		case 'p':
116 			pflg = 1;
117 			break;
118 		case 'r':
119 			rawout = 1;
120 			break;
121 		case '?':
122 		default:
123 			(void)fprintf(stderr, "usage: script [-apr] [file]\n");
124 			exit(1);
125 		}
126 	argc -= optind;
127 	argv += optind;
128 
129 	if (argc > 0)
130 		fname = argv[0];
131 	else
132 		fname = "typescript";
133 
134 	if ((fscript = fopen(fname, pflg ? "r" : aflg ? "a" : "w")) == NULL)
135 		err(1, "fopen %s", fname);
136 
137 	if (pflg)
138 		playback(fscript);
139 
140 	(void)tcgetattr(STDIN_FILENO, &tt);
141 	(void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win);
142 	if (openpty(&master, &slave, NULL, &tt, &win) == -1)
143 		err(1, "openpty");
144 
145 	(void)printf("Script started, output file is %s\n", fname);
146 	rtt = tt;
147 	cfmakeraw(&rtt);
148 	rtt.c_lflag &= ~ECHO;
149 	(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
150 
151 	(void)signal(SIGCHLD, finish);
152 	child = fork();
153 	if (child < 0) {
154 		warn("fork");
155 		fail();
156 	}
157 	if (child == 0) {
158 		subchild = child = fork();
159 		if (child < 0) {
160 			warn("fork");
161 			fail();
162 		}
163 		if (child)
164 			dooutput();
165 		else
166 			doshell();
167 	}
168 
169 	if (!rawout)
170 		(void)fclose(fscript);
171 	while ((cc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0) {
172 		if (rawout)
173 			record(fscript, ibuf, cc, 'i');
174 		(void)write(master, ibuf, cc);
175 	}
176 	done();
177 	/* NOTREACHED */
178 	return (0);
179 }
180 
181 void
182 finish(signo)
183 	int signo;
184 {
185 	int die, pid, status;
186 
187 	die = 0;
188 	while ((pid = wait3(&status, WNOHANG, 0)) > 0)
189 		if (pid == child)
190 			die = 1;
191 
192 	if (die)
193 		done();
194 }
195 
196 void
197 dooutput()
198 {
199 	struct itimerval value;
200 	int cc;
201 	time_t tvec;
202 	char obuf[BUFSIZ];
203 
204 	(void)close(STDIN_FILENO);
205 	tvec = time(NULL);
206 	if (rawout)
207 		record(fscript, NULL, 0, 's');
208 	else
209 		(void)fprintf(fscript, "Script started on %s", ctime(&tvec));
210 
211 	(void)signal(SIGALRM, scriptflush);
212 	value.it_interval.tv_sec = SECSPERMIN / 2;
213 	value.it_interval.tv_usec = 0;
214 	value.it_value = value.it_interval;
215 	(void)setitimer(ITIMER_REAL, &value, NULL);
216 	for (;;) {
217 		cc = read(master, obuf, sizeof (obuf));
218 		if (cc <= 0)
219 			break;
220 		(void)write(1, obuf, cc);
221 		if (rawout)
222 			record(fscript, obuf, cc, 'o');
223 		else
224 			(void)fwrite(obuf, 1, cc, fscript);
225 		outcc += cc;
226 	}
227 	done();
228 }
229 
230 void
231 scriptflush(signo)
232 	int signo;
233 {
234 	if (outcc) {
235 		(void)fflush(fscript);
236 		outcc = 0;
237 	}
238 }
239 
240 void
241 doshell()
242 {
243 	char *shell;
244 
245 	shell = getenv("SHELL");
246 	if (shell == NULL)
247 		shell = _PATH_BSHELL;
248 
249 	(void)close(master);
250 	(void)fclose(fscript);
251 	login_tty(slave);
252 	execl(shell, shell, "-i", NULL);
253 	warn("execl %s", shell);
254 	fail();
255 }
256 
257 void
258 fail()
259 {
260 
261 	(void)kill(0, SIGTERM);
262 	done();
263 }
264 
265 void
266 done()
267 {
268 	time_t tvec;
269 
270 	if (subchild) {
271 		tvec = time(NULL);
272 		if (rawout)
273 			record(fscript, NULL, 0, 'e');
274 		else
275 			(void)fprintf(fscript,"\nScript done on %s",
276 			    ctime(&tvec));
277 		(void)fclose(fscript);
278 		(void)close(master);
279 	} else {
280 		(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
281 		(void)printf("Script done, output file is %s\n", fname);
282 	}
283 	exit(0);
284 }
285 
286 void
287 record(fscript, buf, cc, direction)
288 	FILE *fscript;
289 	char *buf;
290 	size_t cc;
291 	int direction;
292 {
293 	struct iovec iov[2];
294 	struct stamp stamp;
295 	struct timeval tv;
296 
297 	(void)gettimeofday(&tv, NULL);
298 	stamp.scr_len = cc;
299 	stamp.scr_sec = tv.tv_sec;
300 	stamp.scr_usec = tv.tv_usec;
301 	stamp.scr_direction = direction;
302 	iov[0].iov_len = sizeof(stamp);
303 	iov[0].iov_base = &stamp;
304 	iov[1].iov_len = cc;
305 	iov[1].iov_base = buf;
306 	if (writev(fileno(fscript), &iov[0], 2) == -1)
307 		err(1, "writev");
308 }
309 
310 #define swapstamp(stamp) do { \
311 	if (stamp.scr_direction > 0xff) { \
312 		stamp.scr_len = bswap64(stamp.scr_len); \
313 		stamp.scr_sec = bswap64(stamp.scr_sec); \
314 		stamp.scr_usec = bswap32(stamp.scr_usec); \
315 		stamp.scr_direction = bswap32(stamp.scr_direction); \
316 	} \
317 } while (0/*CONSTCOND*/)
318 
319 void
320 playback(fscript)
321 	FILE *fscript;
322 {
323 	struct timespec tsi, tso;
324 	struct stamp stamp;
325 	char buf[BUFSIZ];
326 	size_t l;
327 	time_t clock;
328 
329 	do {
330 		if (fread(&stamp, sizeof(stamp), 1, fscript) != 1)
331 			err(1, "reading playback header");
332 
333 		swapstamp(stamp);
334 		l = fread(buf, 1, stamp.scr_len, fscript);
335 		clock = stamp.scr_sec;
336 		tso.tv_sec = stamp.scr_sec;
337 		tso.tv_nsec = stamp.scr_usec * 1000;
338 
339 		switch (stamp.scr_direction) {
340 		case 's':
341 			(void)printf("Script started on %s", ctime(&clock));
342 			tsi = tso;
343 			break;
344 		case 'e':
345 			(void)printf("\nScript done on %s", ctime(&clock));
346 			break;
347 		case 'i':
348 			/* throw input away */
349 			break;
350 		case 'o':
351 			tsi.tv_sec = tso.tv_sec - tsi.tv_sec;
352 			tsi.tv_nsec = tso.tv_nsec - tsi.tv_nsec;
353 			if (tsi.tv_nsec < 0) {
354 				tsi.tv_sec -= 1;
355 				tsi.tv_nsec += 1000000000;
356 			}
357 			if (usesleep)
358 				(void)nanosleep(&tsi, NULL);
359 			tsi = tso;
360 			(void)write(STDOUT_FILENO, buf, l);
361 			break;
362 		}
363 	} while (stamp.scr_direction != 'e');
364 
365 	(void)fclose(fscript);
366 	exit(0);
367 }
368