xref: /openbsd-src/usr.bin/script/script.c (revision df930be708d50e9715f173caa26ffe1b7599b157)
1 /*	$NetBSD: script.c,v 1.3 1994/12/21 08:55:43 jtc 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 #ifndef lint
37 static char copyright[] =
38 "@(#) 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 static char rcsid[] = "$NetBSD: script.c,v 1.3 1994/12/21 08:55:43 jtc 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 <errno.h>
56 #include <fcntl.h>
57 #include <paths.h>
58 #include <signal.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <termios.h>
63 #include <tzfile.h>
64 #include <unistd.h>
65 
66 FILE	*fscript;
67 int	master, slave;
68 int	child, subchild;
69 int	outcc;
70 char	*fname;
71 
72 struct	termios tt;
73 
74 __dead	void done __P((void));
75 	void dooutput __P((void));
76 	void doshell __P((void));
77 	void err __P((const char *, ...));
78 	void fail __P((void));
79 	void finish __P((int));
80 	void scriptflush __P((int));
81 
82 int
83 main(argc, argv)
84 	int argc;
85 	char *argv[];
86 {
87 	register int cc;
88 	struct termios rtt;
89 	struct winsize win;
90 	int aflg, ch;
91 	char ibuf[BUFSIZ];
92 
93 	aflg = 0;
94 	while ((ch = getopt(argc, argv, "a")) != EOF)
95 		switch(ch) {
96 		case 'a':
97 			aflg = 1;
98 			break;
99 		case '?':
100 		default:
101 			(void)fprintf(stderr, "usage: script [-a] [file]\n");
102 			exit(1);
103 		}
104 	argc -= optind;
105 	argv += optind;
106 
107 	if (argc > 0)
108 		fname = argv[0];
109 	else
110 		fname = "typescript";
111 
112 	if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL)
113 		err("%s: %s", fname, strerror(errno));
114 
115 	(void)tcgetattr(STDIN_FILENO, &tt);
116 	(void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win);
117 	if (openpty(&master, &slave, NULL, &tt, &win) == -1)
118 		err("openpty: %s", strerror(errno));
119 
120 	(void)printf("Script started, output file is %s\n", fname);
121 	rtt = tt;
122 	cfmakeraw(&rtt);
123 	rtt.c_lflag &= ~ECHO;
124 	(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
125 
126 	(void)signal(SIGCHLD, finish);
127 	child = fork();
128 	if (child < 0) {
129 		perror("fork");
130 		fail();
131 	}
132 	if (child == 0) {
133 		subchild = child = fork();
134 		if (child < 0) {
135 			perror("fork");
136 			fail();
137 		}
138 		if (child)
139 			dooutput();
140 		else
141 			doshell();
142 	}
143 
144 	(void)fclose(fscript);
145 	while ((cc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0)
146 		(void)write(master, ibuf, cc);
147 	done();
148 }
149 
150 void
151 finish(signo)
152 	int signo;
153 {
154 	register int die, pid;
155 	union wait status;
156 
157 	die = 0;
158 	while ((pid = wait3((int *)&status, WNOHANG, 0)) > 0)
159 		if (pid == child)
160 			die = 1;
161 
162 	if (die)
163 		done();
164 }
165 
166 void
167 dooutput()
168 {
169 	struct itimerval value;
170 	register int cc;
171 	time_t tvec;
172 	char obuf[BUFSIZ];
173 
174 	(void)close(STDIN_FILENO);
175 	tvec = time(NULL);
176 	(void)fprintf(fscript, "Script started on %s", ctime(&tvec));
177 
178 	(void)signal(SIGALRM, scriptflush);
179 	value.it_interval.tv_sec = SECSPERMIN / 2;
180 	value.it_interval.tv_usec = 0;
181 	value.it_value = value.it_interval;
182 	(void)setitimer(ITIMER_REAL, &value, NULL);
183 	for (;;) {
184 		cc = read(master, obuf, sizeof (obuf));
185 		if (cc <= 0)
186 			break;
187 		(void)write(1, obuf, cc);
188 		(void)fwrite(obuf, 1, cc, fscript);
189 		outcc += cc;
190 	}
191 	done();
192 }
193 
194 void
195 scriptflush(signo)
196 	int signo;
197 {
198 	if (outcc) {
199 		(void)fflush(fscript);
200 		outcc = 0;
201 	}
202 }
203 
204 void
205 doshell()
206 {
207 	char *shell;
208 
209 	shell = getenv("SHELL");
210 	if (shell == NULL)
211 		shell = _PATH_BSHELL;
212 
213 	(void)close(master);
214 	(void)fclose(fscript);
215 	login_tty(slave);
216 	execl(shell, "sh", "-i", NULL);
217 	perror(shell);
218 	fail();
219 }
220 
221 void
222 fail()
223 {
224 
225 	(void)kill(0, SIGTERM);
226 	done();
227 }
228 
229 void
230 done()
231 {
232 	time_t tvec;
233 
234 	if (subchild) {
235 		tvec = time(NULL);
236 		(void)fprintf(fscript,"\nScript done on %s", ctime(&tvec));
237 		(void)fclose(fscript);
238 		(void)close(master);
239 	} else {
240 		(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
241 		(void)printf("Script done, output file is %s\n", fname);
242 	}
243 	exit(0);
244 }
245 
246 #if __STDC__
247 #include <stdarg.h>
248 #else
249 #include <varargs.h>
250 #endif
251 
252 void
253 #if __STDC__
254 err(const char *fmt, ...)
255 #else
256 err(fmt, va_alist)
257 	char *fmt;
258 	va_dcl
259 #endif
260 {
261 	va_list ap;
262 #if __STDC__
263 	va_start(ap, fmt);
264 #else
265 	va_start(ap);
266 #endif
267 	(void)fprintf(stderr, "script: ");
268 	(void)vfprintf(stderr, fmt, ap);
269 	va_end(ap);
270 	(void)fprintf(stderr, "\n");
271 	exit(1);
272 	/* NOTREACHED */
273 }
274