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 #ifndef lint 35 static const char copyright[] = 36 "@(#) Copyright (c) 1980, 1992, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 #if 0 42 static char sccsid[] = "@(#)script.c 8.1 (Berkeley) 6/6/93"; 43 #endif 44 static const char rcsid[] = 45 "$Id: script.c,v 1.7 1997/12/30 01:20:08 peter Exp $"; 46 #endif /* not lint */ 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 char *fname; 70 int qflg; 71 72 struct termios tt; 73 74 void done __P((void)) __dead2; 75 void dooutput __P((void)); 76 void doshell __P((char **)); 77 void fail __P((void)); 78 void finish __P((int)); 79 static void usage __P((void)); 80 81 int 82 main(argc, argv) 83 int argc; 84 char *argv[]; 85 { 86 register 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 int lastc = 1000; 97 98 aflg = kflg = 0; 99 while ((ch = getopt(argc, argv, "aqkt:")) != -1) 100 switch(ch) { 101 case 'a': 102 aflg = 1; 103 break; 104 case 'q': 105 qflg = 1; 106 break; 107 case 'k': 108 kflg = 1; 109 break; 110 case 't': 111 flushtime = atoi(optarg); 112 if (flushtime < 0) 113 err(1, "invalid flush time %d", flushtime); 114 break; 115 case '?': 116 default: 117 usage(); 118 } 119 argc -= optind; 120 argv += optind; 121 122 if (argc > 0) { 123 fname = argv[0]; 124 argv++; 125 argc--; 126 } else 127 fname = "typescript"; 128 129 if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL) 130 err(1, "%s", fname); 131 132 (void)tcgetattr(STDIN_FILENO, &tt); 133 (void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win); 134 if (openpty(&master, &slave, NULL, &tt, &win) == -1) 135 err(1, "openpty"); 136 137 if (!qflg) { 138 tvec = time(NULL); 139 (void)printf("Script started, output file is %s\n", fname); 140 (void)fprintf(fscript, "Script started on %s", ctime(&tvec)); 141 fflush(fscript); 142 } 143 rtt = tt; 144 cfmakeraw(&rtt); 145 rtt.c_lflag &= ~ECHO; 146 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt); 147 148 (void)signal(SIGCHLD, finish); 149 child = fork(); 150 if (child < 0) { 151 warn("fork"); 152 fail(); 153 } 154 if (child == 0) 155 doshell(argv); 156 157 if (flushtime > 0) 158 tvp = &tv; 159 else 160 tvp = NULL; 161 162 start = time(0); 163 FD_ZERO(&rfd); 164 for (;;) { 165 FD_SET(master, &rfd); 166 FD_SET(STDIN_FILENO, &rfd); 167 if (flushtime > 0) { 168 tv.tv_sec = flushtime; 169 tv.tv_usec = 0; 170 } 171 n = select(master + 1, &rfd, 0, 0, tvp); 172 if (n < 0 && errno != EINTR) 173 break; 174 if (n > 0 && FD_ISSET(STDIN_FILENO, &rfd)) { 175 cc = read(STDIN_FILENO, ibuf, BUFSIZ); 176 if (cc <= 0) 177 break; 178 if (cc > 0) { 179 (void)write(master, ibuf, cc); 180 if (kflg && tcgetattr(master, &stt) >= 0 && 181 ((stt.c_lflag & ECHO) == 0)) { 182 (void)fwrite(ibuf, 1, cc, fscript); 183 } 184 } 185 } 186 if (n > 0 && FD_ISSET(master, &rfd)) { 187 cc = read(master, obuf, sizeof (obuf)); 188 if (cc <= 0) 189 break; 190 (void)write(1, obuf, cc); 191 (void)fwrite(obuf, 1, cc, fscript); 192 } 193 tvec = time(0); 194 if (tvec - start >= flushtime) { 195 fflush(fscript); 196 start = tvec; 197 } 198 } 199 done(); 200 } 201 202 static void 203 usage() 204 { 205 (void)fprintf(stderr, 206 "usage: script [-a] [-q] [-k] [-t time] [file] [command]\n"); 207 exit(1); 208 } 209 210 void 211 finish(signo) 212 int signo; 213 { 214 register int die, pid; 215 union wait status; 216 217 die = 0; 218 while ((pid = wait3((int *)&status, WNOHANG, 0)) > 0) 219 if (pid == child) 220 die = 1; 221 222 if (die) 223 done(); 224 } 225 226 void 227 doshell(av) 228 char **av; 229 { 230 char *shell; 231 232 shell = getenv("SHELL"); 233 if (shell == NULL) 234 shell = _PATH_BSHELL; 235 236 (void)close(master); 237 (void)fclose(fscript); 238 login_tty(slave); 239 if (av[0]) 240 execvp(av[0], av); 241 else 242 execl(shell, "sh", "-i", NULL); 243 warn(shell); 244 fail(); 245 } 246 247 void 248 fail() 249 { 250 251 (void)kill(0, SIGTERM); 252 done(); 253 } 254 255 void 256 done() 257 { 258 time_t tvec; 259 260 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt); 261 tvec = time(NULL); 262 if (!qflg) { 263 (void)fprintf(fscript,"\nScript done on %s", ctime(&tvec)); 264 (void)printf("\nScript done, output file is %s\n", fname); 265 } 266 (void)fclose(fscript); 267 (void)close(master); 268 exit(0); 269 } 270