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