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