xref: /netbsd-src/usr.bin/script/script.c (revision a0713f21decbc1c03e083913b3f5b03f8c8e97c8)
1 /*	$NetBSD: script.c,v 1.11 2006/03/29 15:40:49 rpaulo 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.11 2006/03/29 15:40:49 rpaulo 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(void);
83 void	dooutput(void);
84 void	doshell(void);
85 void	fail(void);
86 void	finish(int);
87 int	main(int, char **);
88 void	scriptflush(int);
89 void	record(FILE *, char *, size_t, int);
90 void	playback(FILE *);
91 
92 int
93 main(int argc, char *argv[])
94 {
95 	int cc;
96 	struct termios rtt;
97 	struct winsize win;
98 	int aflg, pflg, ch;
99 	char ibuf[BUFSIZ];
100 
101 	aflg = 0;
102 	pflg = 0;
103 	usesleep = 1;
104 	rawout = 0;
105 	while ((ch = getopt(argc, argv, "adpr")) != -1)
106 		switch(ch) {
107 		case 'a':
108 			aflg = 1;
109 			break;
110 		case 'd':
111 			usesleep = 0;
112 			break;
113 		case 'p':
114 			pflg = 1;
115 			break;
116 		case 'r':
117 			rawout = 1;
118 			break;
119 		case '?':
120 		default:
121 			(void)fprintf(stderr, "usage: %s [-adpr] [file]\n",
122 			    getprogname());
123 			exit(1);
124 		}
125 	argc -= optind;
126 	argv += optind;
127 
128 	if (argc > 0)
129 		fname = argv[0];
130 	else
131 		fname = "typescript";
132 
133 	if ((fscript = fopen(fname, pflg ? "r" : aflg ? "a" : "w")) == NULL)
134 		err(1, "fopen %s", fname);
135 
136 	if (pflg)
137 		playback(fscript);
138 
139 	(void)tcgetattr(STDIN_FILENO, &tt);
140 	(void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win);
141 	if (openpty(&master, &slave, NULL, &tt, &win) == -1)
142 		err(1, "openpty");
143 
144 	(void)printf("Script started, output file is %s\n", fname);
145 	rtt = tt;
146 	cfmakeraw(&rtt);
147 	rtt.c_lflag &= ~ECHO;
148 	(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
149 
150 	(void)signal(SIGCHLD, finish);
151 	child = fork();
152 	if (child < 0) {
153 		warn("fork");
154 		fail();
155 	}
156 	if (child == 0) {
157 		subchild = child = fork();
158 		if (child < 0) {
159 			warn("fork");
160 			fail();
161 		}
162 		if (child)
163 			dooutput();
164 		else
165 			doshell();
166 	}
167 
168 	if (!rawout)
169 		(void)fclose(fscript);
170 	while ((cc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0) {
171 		if (rawout)
172 			record(fscript, ibuf, cc, 'i');
173 		(void)write(master, ibuf, cc);
174 	}
175 	done();
176 	/* NOTREACHED */
177 	return (0);
178 }
179 
180 void
181 finish(int signo)
182 {
183 	int die, pid, status;
184 
185 	die = 0;
186 	while ((pid = wait3(&status, WNOHANG, 0)) > 0)
187 		if (pid == child)
188 			die = 1;
189 
190 	if (die)
191 		done();
192 }
193 
194 void
195 dooutput()
196 {
197 	struct itimerval value;
198 	int cc;
199 	time_t tvec;
200 	char obuf[BUFSIZ];
201 
202 	(void)close(STDIN_FILENO);
203 	tvec = time(NULL);
204 	if (rawout)
205 		record(fscript, NULL, 0, 's');
206 	else
207 		(void)fprintf(fscript, "Script started on %s", ctime(&tvec));
208 
209 	(void)signal(SIGALRM, scriptflush);
210 	value.it_interval.tv_sec = SECSPERMIN / 2;
211 	value.it_interval.tv_usec = 0;
212 	value.it_value = value.it_interval;
213 	(void)setitimer(ITIMER_REAL, &value, NULL);
214 	for (;;) {
215 		cc = read(master, obuf, sizeof (obuf));
216 		if (cc <= 0)
217 			break;
218 		(void)write(1, obuf, cc);
219 		if (rawout)
220 			record(fscript, obuf, cc, 'o');
221 		else
222 			(void)fwrite(obuf, 1, cc, fscript);
223 		outcc += cc;
224 	}
225 	done();
226 }
227 
228 void
229 scriptflush(int signo)
230 {
231 	if (outcc) {
232 		(void)fflush(fscript);
233 		outcc = 0;
234 	}
235 }
236 
237 void
238 doshell()
239 {
240 	char *shell;
241 
242 	shell = getenv("SHELL");
243 	if (shell == NULL)
244 		shell = _PATH_BSHELL;
245 
246 	(void)close(master);
247 	(void)fclose(fscript);
248 	login_tty(slave);
249 	execl(shell, shell, "-i", NULL);
250 	warn("execl %s", shell);
251 	fail();
252 }
253 
254 void
255 fail()
256 {
257 
258 	(void)kill(0, SIGTERM);
259 	done();
260 }
261 
262 void
263 done()
264 {
265 	time_t tvec;
266 
267 	if (subchild) {
268 		tvec = time(NULL);
269 		if (rawout)
270 			record(fscript, NULL, 0, 'e');
271 		else
272 			(void)fprintf(fscript,"\nScript done on %s",
273 			    ctime(&tvec));
274 		(void)fclose(fscript);
275 		(void)close(master);
276 	} else {
277 		(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
278 		(void)printf("Script done, output file is %s\n", fname);
279 	}
280 	exit(0);
281 }
282 
283 void
284 record(FILE *fscript, char *buf, size_t cc, int direction)
285 {
286 	struct iovec iov[2];
287 	struct stamp stamp;
288 	struct timeval tv;
289 
290 	(void)gettimeofday(&tv, NULL);
291 	stamp.scr_len = cc;
292 	stamp.scr_sec = tv.tv_sec;
293 	stamp.scr_usec = tv.tv_usec;
294 	stamp.scr_direction = direction;
295 	iov[0].iov_len = sizeof(stamp);
296 	iov[0].iov_base = &stamp;
297 	iov[1].iov_len = cc;
298 	iov[1].iov_base = buf;
299 	if (writev(fileno(fscript), &iov[0], 2) == -1)
300 		err(1, "writev");
301 }
302 
303 #define swapstamp(stamp) do { \
304 	if (stamp.scr_direction > 0xff) { \
305 		stamp.scr_len = bswap64(stamp.scr_len); \
306 		stamp.scr_sec = bswap64(stamp.scr_sec); \
307 		stamp.scr_usec = bswap32(stamp.scr_usec); \
308 		stamp.scr_direction = bswap32(stamp.scr_direction); \
309 	} \
310 } while (0/*CONSTCOND*/)
311 
312 void
313 playback(FILE *fscript)
314 {
315 	struct timespec tsi, tso;
316 	struct stamp stamp;
317 	char buf[BUFSIZ];
318 	size_t l;
319 	time_t clock;
320 
321 	do {
322 		if (fread(&stamp, sizeof(stamp), 1, fscript) != 1)
323 			err(1, "reading playback header");
324 
325 		swapstamp(stamp);
326 		l = fread(buf, 1, stamp.scr_len, fscript);
327 		clock = stamp.scr_sec;
328 		tso.tv_sec = stamp.scr_sec;
329 		tso.tv_nsec = stamp.scr_usec * 1000;
330 
331 		switch (stamp.scr_direction) {
332 		case 's':
333 			(void)printf("Script started on %s", ctime(&clock));
334 			tsi = tso;
335 			break;
336 		case 'e':
337 			(void)printf("\nScript done on %s", ctime(&clock));
338 			break;
339 		case 'i':
340 			/* throw input away */
341 			break;
342 		case 'o':
343 			tsi.tv_sec = tso.tv_sec - tsi.tv_sec;
344 			tsi.tv_nsec = tso.tv_nsec - tsi.tv_nsec;
345 			if (tsi.tv_nsec < 0) {
346 				tsi.tv_sec -= 1;
347 				tsi.tv_nsec += 1000000000;
348 			}
349 			if (usesleep)
350 				(void)nanosleep(&tsi, NULL);
351 			tsi = tso;
352 			(void)write(STDOUT_FILENO, buf, l);
353 			break;
354 		}
355 	} while (stamp.scr_direction != 'e');
356 
357 	(void)fclose(fscript);
358 	exit(0);
359 }
360