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