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