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