xref: /netbsd-src/usr.bin/script/script.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: script.c,v 1.5 1997/10/19 22:57:49 lukem 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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1980, 1992, 1993\n\
39 	The Regents of the University of California.  All rights reserved.\n");
40 #endif /* not lint */
41 
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)script.c	8.1 (Berkeley) 6/6/93";
45 #endif
46 __RCSID("$NetBSD: script.c,v 1.5 1997/10/19 22:57:49 lukem Exp $");
47 #endif /* not lint */
48 
49 #include <sys/types.h>
50 #include <sys/wait.h>
51 #include <sys/stat.h>
52 #include <sys/ioctl.h>
53 #include <sys/time.h>
54 
55 #include <err.h>
56 #include <errno.h>
57 #include <fcntl.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 <tzfile.h>
65 #include <unistd.h>
66 #include <util.h>
67 
68 FILE	*fscript;
69 int	master, slave;
70 int	child, subchild;
71 int	outcc;
72 char	*fname;
73 
74 struct	termios tt;
75 
76 void	done __P((void));
77 void	dooutput __P((void));
78 void	doshell __P((void));
79 void	fail __P((void));
80 void	finish __P((int));
81 int	main __P((int, char **));
82 void	scriptflush __P((int));
83 
84 int
85 main(argc, argv)
86 	int argc;
87 	char *argv[];
88 {
89 	int cc;
90 	struct termios rtt;
91 	struct winsize win;
92 	int aflg, ch;
93 	char ibuf[BUFSIZ];
94 
95 	aflg = 0;
96 	while ((ch = getopt(argc, argv, "a")) != -1)
97 		switch(ch) {
98 		case 'a':
99 			aflg = 1;
100 			break;
101 		case '?':
102 		default:
103 			(void)fprintf(stderr, "usage: script [-a] [file]\n");
104 			exit(1);
105 		}
106 	argc -= optind;
107 	argv += optind;
108 
109 	if (argc > 0)
110 		fname = argv[0];
111 	else
112 		fname = "typescript";
113 
114 	if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL)
115 		err(1, "fopen %s", fname);
116 
117 	(void)tcgetattr(STDIN_FILENO, &tt);
118 	(void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win);
119 	if (openpty(&master, &slave, NULL, &tt, &win) == -1)
120 		err(1, "openpty");
121 
122 	(void)printf("Script started, output file is %s\n", fname);
123 	rtt = tt;
124 	cfmakeraw(&rtt);
125 	rtt.c_lflag &= ~ECHO;
126 	(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
127 
128 	(void)signal(SIGCHLD, finish);
129 	child = fork();
130 	if (child < 0) {
131 		warn("fork");
132 		fail();
133 	}
134 	if (child == 0) {
135 		subchild = child = fork();
136 		if (child < 0) {
137 			warn("fork");
138 			fail();
139 		}
140 		if (child)
141 			dooutput();
142 		else
143 			doshell();
144 	}
145 
146 	(void)fclose(fscript);
147 	while ((cc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0)
148 		(void)write(master, ibuf, cc);
149 	done();
150 	/* NOTREACHED */
151 	return (0);
152 }
153 
154 void
155 finish(signo)
156 	int signo;
157 {
158 	int die, pid;
159 	union wait status;
160 
161 	die = 0;
162 	while ((pid = wait3((int *)&status, WNOHANG, 0)) > 0)
163 		if (pid == child)
164 			die = 1;
165 
166 	if (die)
167 		done();
168 }
169 
170 void
171 dooutput()
172 {
173 	struct itimerval value;
174 	int cc;
175 	time_t tvec;
176 	char obuf[BUFSIZ];
177 
178 	(void)close(STDIN_FILENO);
179 	tvec = time(NULL);
180 	(void)fprintf(fscript, "Script started on %s", ctime(&tvec));
181 
182 	(void)signal(SIGALRM, scriptflush);
183 	value.it_interval.tv_sec = SECSPERMIN / 2;
184 	value.it_interval.tv_usec = 0;
185 	value.it_value = value.it_interval;
186 	(void)setitimer(ITIMER_REAL, &value, NULL);
187 	for (;;) {
188 		cc = read(master, obuf, sizeof (obuf));
189 		if (cc <= 0)
190 			break;
191 		(void)write(1, obuf, cc);
192 		(void)fwrite(obuf, 1, cc, fscript);
193 		outcc += cc;
194 	}
195 	done();
196 }
197 
198 void
199 scriptflush(signo)
200 	int signo;
201 {
202 	if (outcc) {
203 		(void)fflush(fscript);
204 		outcc = 0;
205 	}
206 }
207 
208 void
209 doshell()
210 {
211 	char *shell;
212 
213 	shell = getenv("SHELL");
214 	if (shell == NULL)
215 		shell = _PATH_BSHELL;
216 
217 	(void)close(master);
218 	(void)fclose(fscript);
219 	login_tty(slave);
220 	execl(shell, shell, "-i", NULL);
221 	warn("execl %s", shell);
222 	fail();
223 }
224 
225 void
226 fail()
227 {
228 
229 	(void)kill(0, SIGTERM);
230 	done();
231 }
232 
233 void
234 done()
235 {
236 	time_t tvec;
237 
238 	if (subchild) {
239 		tvec = time(NULL);
240 		(void)fprintf(fscript,"\nScript done on %s", ctime(&tvec));
241 		(void)fclose(fscript);
242 		(void)close(master);
243 	} else {
244 		(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
245 		(void)printf("Script done, output file is %s\n", fname);
246 	}
247 	exit(0);
248 }
249