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 "$FreeBSD$"; 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((int)) __dead2; 75 void dooutput __P((void)); 76 void doshell __P((char **)); 77 void fail __P((void)); 78 void finish __P((void)); 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 child = fork(); 149 if (child < 0) { 150 warn("fork"); 151 done(1); 152 } 153 if (child == 0) 154 doshell(argv); 155 156 if (flushtime > 0) 157 tvp = &tv; 158 else 159 tvp = NULL; 160 161 start = time(0); 162 FD_ZERO(&rfd); 163 for (;;) { 164 FD_SET(master, &rfd); 165 FD_SET(STDIN_FILENO, &rfd); 166 if (flushtime > 0) { 167 tv.tv_sec = flushtime; 168 tv.tv_usec = 0; 169 } 170 n = select(master + 1, &rfd, 0, 0, tvp); 171 if (n < 0 && errno != EINTR) 172 break; 173 if (n > 0 && FD_ISSET(STDIN_FILENO, &rfd)) { 174 cc = read(STDIN_FILENO, ibuf, BUFSIZ); 175 if (cc <= 0) 176 break; 177 if (cc > 0) { 178 (void)write(master, ibuf, cc); 179 if (kflg && tcgetattr(master, &stt) >= 0 && 180 ((stt.c_lflag & ECHO) == 0)) { 181 (void)fwrite(ibuf, 1, cc, fscript); 182 } 183 } 184 } 185 if (n > 0 && FD_ISSET(master, &rfd)) { 186 cc = read(master, obuf, sizeof (obuf)); 187 if (cc <= 0) 188 break; 189 (void)write(1, obuf, cc); 190 (void)fwrite(obuf, 1, cc, fscript); 191 } 192 tvec = time(0); 193 if (tvec - start >= flushtime) { 194 fflush(fscript); 195 start = tvec; 196 } 197 } 198 finish(); 199 done(0); 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() 212 { 213 int die, e, pid; 214 union wait status; 215 216 die = e = 0; 217 while ((pid = wait3((int *)&status, WNOHANG, 0)) > 0) 218 if (pid == child) { 219 die = 1; 220 if (WIFEXITED(status)) 221 e = WEXITSTATUS(status); 222 else if (WIFSIGNALED(status)) 223 e = WTERMSIG(status); 224 else /* can't happen */ 225 e = 1; 226 } 227 228 if (die) 229 done(e); 230 } 231 232 void 233 doshell(av) 234 char **av; 235 { 236 char *shell; 237 238 shell = getenv("SHELL"); 239 if (shell == NULL) 240 shell = _PATH_BSHELL; 241 242 (void)close(master); 243 (void)fclose(fscript); 244 login_tty(slave); 245 if (av[0]) { 246 execvp(av[0], av); 247 warn(av[0]); 248 } else { 249 execl(shell, shell, "-i", NULL); 250 warn(shell); 251 } 252 fail(); 253 } 254 255 void 256 fail() 257 { 258 (void)kill(0, SIGTERM); 259 done(1); 260 } 261 262 void 263 done(eno) 264 int eno; 265 { 266 time_t tvec; 267 268 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt); 269 tvec = time(NULL); 270 if (!qflg) { 271 (void)fprintf(fscript,"\nScript done on %s", ctime(&tvec)); 272 (void)printf("\nScript done, output file is %s\n", fname); 273 } 274 (void)fclose(fscript); 275 (void)close(master); 276 exit(eno); 277 } 278