]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - usr.bin/script/script.c
MFC r225809:
[FreeBSD/stable/8.git] / usr.bin / script / script.c
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         int readstdin;
95
96         aflg = kflg = 0;
97         while ((ch = getopt(argc, argv, "aqkt:")) != -1)
98                 switch(ch) {
99                 case 'a':
100                         aflg = 1;
101                         break;
102                 case 'q':
103                         qflg = 1;
104                         break;
105                 case 'k':
106                         kflg = 1;
107                         break;
108                 case 't':
109                         flushtime = atoi(optarg);
110                         if (flushtime < 0)
111                                 err(1, "invalid flush time %d", flushtime);
112                         break;
113                 case '?':
114                 default:
115                         usage();
116                 }
117         argc -= optind;
118         argv += optind;
119
120         if (argc > 0) {
121                 fname = argv[0];
122                 argv++;
123                 argc--;
124         } else
125                 fname = "typescript";
126
127         if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL)
128                 err(1, "%s", fname);
129
130         if (ttyflg = isatty(STDIN_FILENO)) {
131                 if (tcgetattr(STDIN_FILENO, &tt) == -1)
132                         err(1, "tcgetattr");
133                 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1)
134                         err(1, "ioctl");
135                 if (openpty(&master, &slave, NULL, &tt, &win) == -1)
136                         err(1, "openpty");
137         } else {
138                 if (openpty(&master, &slave, NULL, NULL, NULL) == -1)
139                         err(1, "openpty");
140         }
141
142         if (!qflg) {
143                 tvec = time(NULL);
144                 (void)printf("Script started, output file is %s\n", fname);
145                 (void)fprintf(fscript, "Script started on %s", ctime(&tvec));
146                 fflush(fscript);
147         }
148         if (ttyflg) {
149                 rtt = tt;
150                 cfmakeraw(&rtt);
151                 rtt.c_lflag &= ~ECHO;
152                 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
153         }
154
155         child = fork();
156         if (child < 0) {
157                 warn("fork");
158                 done(1);
159         }
160         if (child == 0)
161                 doshell(argv);
162         close(slave);
163
164         start = tvec = time(0);
165         readstdin = 1;
166         for (;;) {
167                 FD_ZERO(&rfd);
168                 FD_SET(master, &rfd);
169                 if (readstdin)
170                         FD_SET(STDIN_FILENO, &rfd);
171                 if ((!readstdin && ttyflg) || flushtime > 0) {
172                         tv.tv_sec = !readstdin && ttyflg ? 1 :
173                             flushtime - (tvec - start);
174                         tv.tv_usec = 0;
175                         tvp = &tv;
176                         readstdin = 1;
177                 } else {
178                         tvp = NULL;
179                 }
180                 n = select(master + 1, &rfd, 0, 0, tvp);
181                 if (n < 0 && errno != EINTR)
182                         break;
183                 if (n > 0 && FD_ISSET(STDIN_FILENO, &rfd)) {
184                         cc = read(STDIN_FILENO, ibuf, BUFSIZ);
185                         if (cc < 0)
186                                 break;
187                         if (cc == 0) {
188                                 if (tcgetattr(master, &stt) == 0 &&
189                                     (stt.c_lflag & ICANON) != 0) {
190                                         (void)write(master, &stt.c_cc[VEOF], 1);
191                                 }
192                                 readstdin = 0;
193                         }
194                         if (cc > 0) {
195                                 (void)write(master, ibuf, cc);
196                                 if (kflg && tcgetattr(master, &stt) >= 0 &&
197                                     ((stt.c_lflag & ECHO) == 0)) {
198                                         (void)fwrite(ibuf, 1, cc, fscript);
199                                 }
200                         }
201                 }
202                 if (n > 0 && FD_ISSET(master, &rfd)) {
203                         cc = read(master, obuf, sizeof (obuf));
204                         if (cc <= 0)
205                                 break;
206                         (void)write(STDOUT_FILENO, obuf, cc);
207                         (void)fwrite(obuf, 1, cc, fscript);
208                 }
209                 tvec = time(0);
210                 if (tvec - start >= flushtime) {
211                         fflush(fscript);
212                         start = tvec;
213                 }
214         }
215         finish();
216         done(0);
217 }
218
219 static void
220 usage(void)
221 {
222         (void)fprintf(stderr,
223             "usage: script [-akq] [-t time] [file [command ...]]\n");
224         exit(1);
225 }
226
227 void
228 finish(void)
229 {
230         int e, status;
231
232         if (waitpid(child, &status, 0) == child) {
233                 if (WIFEXITED(status))
234                         e = WEXITSTATUS(status);
235                 else if (WIFSIGNALED(status))
236                         e = WTERMSIG(status);
237                 else /* can't happen */
238                         e = 1;
239                 done(e);
240         }
241 }
242
243 void
244 doshell(char **av)
245 {
246         const char *shell;
247         int k;
248
249         shell = getenv("SHELL");
250         if (shell == NULL)
251                 shell = _PATH_BSHELL;
252
253         if (av[0])
254                 for (k = 0 ; av[k] ; ++k)
255                         fprintf(fscript, "%s%s", k ? " " : "", av[k]);
256                 fprintf(fscript, "\r\n");
257
258         (void)close(master);
259         (void)fclose(fscript);
260         login_tty(slave);
261         setenv("SCRIPT", fname, 1);
262         if (av[0]) {
263                 execvp(av[0], av);
264                 warn("%s", av[0]);
265         } else {
266                 execl(shell, shell, "-i", (char *)NULL);
267                 warn("%s", shell);
268         }
269         fail();
270 }
271
272 void
273 fail(void)
274 {
275         (void)kill(0, SIGTERM);
276         done(1);
277 }
278
279 void
280 done(int eno)
281 {
282         time_t tvec;
283
284         if (ttyflg)
285                 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
286         tvec = time(NULL);
287         if (!qflg) {
288                 (void)fprintf(fscript,"\nScript done on %s", ctime(&tvec));
289                 (void)printf("\nScript done, output file is %s\n", fname);
290         }
291         (void)fclose(fscript);
292         (void)close(master);
293         exit(eno);
294 }