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