xref: /dragonfly/usr.bin/script/script.c (revision 4e7eb5cc)
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  * @(#) Copyright (c) 1980, 1992, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)script.c	8.1 (Berkeley) 6/6/93
35  * $FreeBSD: src/usr.bin/script/script.c,v 1.11.2.1 2000/07/20 10:35:21 kris Exp $
36  * $DragonFly: src/usr.bin/script/script.c,v 1.3 2003/10/04 20:36:50 hmp Exp $
37  */
38 
39 #include <sys/types.h>
40 #include <sys/wait.h>
41 #include <sys/stat.h>
42 #include <sys/ioctl.h>
43 #include <sys/time.h>
44 
45 #include <err.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <libutil.h>
49 #include <paths.h>
50 #include <signal.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <termios.h>
55 #include <unistd.h>
56 
57 FILE	*fscript;
58 int	master, slave;
59 int	child;
60 char	*fname;
61 int	qflg;
62 
63 struct	termios tt;
64 
65 void	done(int) __dead2;
66 void	dooutput(void);
67 void	doshell(char **);
68 void	fail(void);
69 void	finish(void);
70 static void usage(void);
71 
72 int
73 main(int argc, char **argv)
74 {
75 	register int cc;
76 	struct termios rtt, stt;
77 	struct winsize win;
78 	int aflg, kflg, ch, n;
79 	struct timeval tv, *tvp;
80 	time_t tvec, start;
81 	char obuf[BUFSIZ];
82 	char ibuf[BUFSIZ];
83 	fd_set rfd;
84 	int flushtime = 30;
85 	int lastc = 1000;
86 
87 	aflg = kflg = 0;
88 	while ((ch = getopt(argc, argv, "aqkt:")) != -1)
89 		switch(ch) {
90 		case 'a':
91 			aflg = 1;
92 			break;
93 		case 'q':
94 			qflg = 1;
95 			break;
96 		case 'k':
97 			kflg = 1;
98 			break;
99 		case 't':
100 			flushtime = atoi(optarg);
101 			if (flushtime < 0)
102 				err(1, "invalid flush time %d", flushtime);
103 			break;
104 		case '?':
105 		default:
106 			usage();
107 		}
108 	argc -= optind;
109 	argv += optind;
110 
111 	if (argc > 0) {
112 		fname = argv[0];
113 		argv++;
114 		argc--;
115 	} else
116 		fname = "typescript";
117 
118 	if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL)
119 		err(1, "%s", fname);
120 
121 	(void)tcgetattr(STDIN_FILENO, &tt);
122 	(void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win);
123 	if (openpty(&master, &slave, NULL, &tt, &win) == -1)
124 		err(1, "openpty");
125 
126 	if (!qflg) {
127 		tvec = time(NULL);
128 		(void)printf("Script started, output file is %s\n", fname);
129 		(void)fprintf(fscript, "Script started on %s", ctime(&tvec));
130 		fflush(fscript);
131 	}
132 	rtt = tt;
133 	cfmakeraw(&rtt);
134 	rtt.c_lflag &= ~ECHO;
135 	(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
136 
137 	child = fork();
138 	if (child < 0) {
139 		warn("fork");
140 		done(1);
141 	}
142 	if (child == 0)
143 		doshell(argv);
144 
145 	if (flushtime > 0)
146 		tvp = &tv;
147 	else
148 		tvp = NULL;
149 
150 	start = time(0);
151 	FD_ZERO(&rfd);
152 	for (;;) {
153 		FD_SET(master, &rfd);
154 		FD_SET(STDIN_FILENO, &rfd);
155 		if (flushtime > 0) {
156 			tv.tv_sec = flushtime;
157 			tv.tv_usec = 0;
158 		}
159 		n = select(master + 1, &rfd, 0, 0, tvp);
160 		if (n < 0 && errno != EINTR)
161 			break;
162 		if (n > 0 && FD_ISSET(STDIN_FILENO, &rfd)) {
163 			cc = read(STDIN_FILENO, ibuf, BUFSIZ);
164 			if (cc <= 0)
165 				break;
166 			if (cc > 0) {
167 				(void)write(master, ibuf, cc);
168 				if (kflg && tcgetattr(master, &stt) >= 0 &&
169 				    ((stt.c_lflag & ECHO) == 0)) {
170 					(void)fwrite(ibuf, 1, cc, fscript);
171 				}
172 			}
173 		}
174 		if (n > 0 && FD_ISSET(master, &rfd)) {
175 			cc = read(master, obuf, sizeof (obuf));
176 			if (cc <= 0)
177 				break;
178 			(void)write(1, obuf, cc);
179 			(void)fwrite(obuf, 1, cc, fscript);
180 		}
181 		tvec = time(0);
182 		if (tvec - start >= flushtime) {
183 			fflush(fscript);
184 			start = tvec;
185 		}
186 	}
187 	finish();
188 	done(0);
189 }
190 
191 static void
192 usage(void)
193 {
194 	(void)fprintf(stderr,
195 	    "usage: script [-a] [-q] [-k] [-t time] [file] [command]\n");
196 	exit(1);
197 }
198 
199 void
200 finish(void)
201 {
202 	int die, e, pid;
203 	union wait status;
204 
205 	die = e = 0;
206 	while ((pid = wait3((int *)&status, WNOHANG, 0)) > 0)
207 	        if (pid == child) {
208 			die = 1;
209 			if (WIFEXITED(status))
210 				e = WEXITSTATUS(status);
211 			else if (WIFSIGNALED(status))
212 				e = WTERMSIG(status);
213 			else /* can't happen */
214 				e = 1;
215 		}
216 
217 	if (die)
218 		done(e);
219 }
220 
221 void
222 doshell(char **av)
223 {
224 	char *shell;
225 
226 	shell = getenv("SHELL");
227 	if (shell == NULL)
228 		shell = _PATH_BSHELL;
229 
230 	(void)close(master);
231 	(void)fclose(fscript);
232 	login_tty(slave);
233 	if (av[0]) {
234 		execvp(av[0], av);
235 		warn("%s", av[0]);
236 	} else {
237 		execl(shell, shell, "-i", NULL);
238 		warn("%s", shell);
239 	}
240 	fail();
241 }
242 
243 void
244 fail(void)
245 {
246 	(void)kill(0, SIGTERM);
247 	done(1);
248 }
249 
250 void
251 done(int eno)
252 {
253 	time_t tvec;
254 
255 	(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
256 	tvec = time(NULL);
257 	if (!qflg) {
258 		(void)fprintf(fscript,"\nScript done on %s", ctime(&tvec));
259 		(void)printf("\nScript done, output file is %s\n", fname);
260 	}
261 	(void)fclose(fscript);
262 	(void)close(master);
263 	exit(eno);
264 }
265