1 /* $NetBSD: script.c,v 1.22 2020/08/01 17:31:06 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1980, 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #ifndef lint 34 __COPYRIGHT("@(#) Copyright (c) 1980, 1992, 1993\ 35 The Regents of the University of California. All rights reserved."); 36 #endif /* not lint */ 37 38 #ifndef lint 39 #if 0 40 static char sccsid[] = "@(#)script.c 8.1 (Berkeley) 6/6/93"; 41 #endif 42 __RCSID("$NetBSD: script.c,v 1.22 2020/08/01 17:31:06 christos Exp $"); 43 #endif /* not lint */ 44 45 #include <sys/types.h> 46 #include <sys/wait.h> 47 #include <sys/stat.h> 48 #include <sys/ioctl.h> 49 #include <sys/time.h> 50 #include <sys/param.h> 51 #include <sys/uio.h> 52 53 #include <err.h> 54 #include <errno.h> 55 #include <fcntl.h> 56 #include <paths.h> 57 #include <signal.h> 58 #include <stdio.h> 59 #include <stdlib.h> 60 #include <string.h> 61 #include <termios.h> 62 #include <time.h> 63 #include <tzfile.h> 64 #include <unistd.h> 65 #include <util.h> 66 67 #define DEF_BUF 65536 68 69 struct stamp { 70 uint64_t scr_len; /* amount of data */ 71 uint64_t scr_sec; /* time it arrived in seconds... */ 72 uint32_t scr_usec; /* ...and microseconds */ 73 uint32_t scr_direction; /* 'i', 'o', etc (also indicates endianness) */ 74 }; 75 76 static FILE *fscript; 77 static int master, slave; 78 static int child, subchild; 79 static int outcc; 80 static int usesleep, rawout; 81 static int quiet, flush; 82 static const char *fname; 83 84 static int isterm; 85 static struct termios tt; 86 87 __dead static void done(void); 88 __dead static void dooutput(void); 89 __dead static void doshell(const char *); 90 __dead static void fail(void); 91 static void finish(int); 92 static void scriptflush(int); 93 static void record(FILE *, char *, size_t, int); 94 static void consume(FILE *, off_t, char *, int); 95 __dead static void playback(FILE *); 96 97 int 98 main(int argc, char *argv[]) 99 { 100 int cc; 101 struct termios rtt; 102 struct winsize win; 103 int aflg, pflg, ch; 104 char ibuf[BUFSIZ]; 105 const char *command; 106 107 aflg = 0; 108 pflg = 0; 109 usesleep = 1; 110 rawout = 0; 111 quiet = 0; 112 flush = 0; 113 command = NULL; 114 while ((ch = getopt(argc, argv, "ac:dfpqr")) != -1) 115 switch(ch) { 116 case 'a': 117 aflg = 1; 118 break; 119 case 'c': 120 command = optarg; 121 break; 122 case 'd': 123 usesleep = 0; 124 break; 125 case 'f': 126 flush = 1; 127 break; 128 case 'p': 129 pflg = 1; 130 break; 131 case 'q': 132 quiet = 1; 133 break; 134 case 'r': 135 rawout = 1; 136 break; 137 case '?': 138 default: 139 (void)fprintf(stderr, 140 "Usage: %s [-c <command>][-adfpqr] [file]\n", 141 getprogname()); 142 exit(1); 143 } 144 argc -= optind; 145 argv += optind; 146 147 if (argc > 0) 148 fname = argv[0]; 149 else 150 fname = "typescript"; 151 152 if ((fscript = fopen(fname, pflg ? "r" : aflg ? "a" : "w")) == NULL) 153 err(1, "fopen %s", fname); 154 155 if (pflg) 156 playback(fscript); 157 158 (void)tcgetattr(STDIN_FILENO, &tt); 159 (void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win); 160 if (openpty(&master, &slave, NULL, &tt, &win) == -1) 161 err(1, "openpty"); 162 163 if (!quiet) 164 (void)printf("Script started, output file is %s\n", fname); 165 rtt = tt; 166 cfmakeraw(&rtt); 167 rtt.c_lflag &= ~ECHO; 168 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt); 169 170 (void)signal(SIGCHLD, finish); 171 child = fork(); 172 if (child < 0) { 173 warn("fork"); 174 fail(); 175 } 176 if (child == 0) { 177 subchild = child = fork(); 178 if (child < 0) { 179 warn("fork"); 180 fail(); 181 } 182 if (child) 183 dooutput(); 184 else 185 doshell(command); 186 } 187 188 if (!rawout) 189 (void)fclose(fscript); 190 while ((cc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0) { 191 if (rawout) 192 record(fscript, ibuf, cc, 'i'); 193 (void)write(master, ibuf, cc); 194 } 195 done(); 196 /* NOTREACHED */ 197 return (0); 198 } 199 200 static void 201 finish(int signo) 202 { 203 int die, pid, status; 204 205 die = 0; 206 while ((pid = wait3(&status, WNOHANG, 0)) > 0) 207 if (pid == child) 208 die = 1; 209 210 if (die) 211 done(); 212 } 213 214 static void 215 dooutput(void) 216 { 217 struct itimerval value; 218 int cc; 219 time_t tvec; 220 char obuf[BUFSIZ]; 221 222 (void)close(STDIN_FILENO); 223 tvec = time(NULL); 224 if (rawout) 225 record(fscript, NULL, 0, 's'); 226 else if (!quiet) 227 (void)fprintf(fscript, "Script started on %s", ctime(&tvec)); 228 229 (void)signal(SIGALRM, scriptflush); 230 value.it_interval.tv_sec = SECSPERMIN / 2; 231 value.it_interval.tv_usec = 0; 232 value.it_value = value.it_interval; 233 (void)setitimer(ITIMER_REAL, &value, NULL); 234 for (;;) { 235 cc = read(master, obuf, sizeof (obuf)); 236 if (cc <= 0) 237 break; 238 (void)write(1, obuf, cc); 239 if (rawout) 240 record(fscript, obuf, cc, 'o'); 241 else 242 (void)fwrite(obuf, 1, cc, fscript); 243 outcc += cc; 244 if (flush) 245 (void)fflush(fscript); 246 } 247 done(); 248 } 249 250 static void 251 scriptflush(int signo) 252 { 253 if (outcc) { 254 (void)fflush(fscript); 255 outcc = 0; 256 } 257 } 258 259 static void 260 doshell(const char *command) 261 { 262 const char *shell; 263 264 (void)close(master); 265 (void)fclose(fscript); 266 login_tty(slave); 267 if (command == NULL) { 268 shell = getenv("SHELL"); 269 if (shell == NULL) 270 shell = _PATH_BSHELL; 271 execl(shell, shell, "-i", NULL); 272 warn("execl `%s'", shell); 273 } else { 274 if (system(command) == -1) 275 warn("system `%s'", command); 276 } 277 278 fail(); 279 } 280 281 static void 282 fail(void) 283 { 284 285 (void)kill(0, SIGTERM); 286 done(); 287 } 288 289 static void 290 done(void) 291 { 292 time_t tvec; 293 294 if (subchild) { 295 tvec = time(NULL); 296 if (rawout) 297 record(fscript, NULL, 0, 'e'); 298 else if (!quiet) 299 (void)fprintf(fscript,"\nScript done on %s", 300 ctime(&tvec)); 301 (void)fclose(fscript); 302 (void)close(master); 303 } else { 304 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt); 305 if (!quiet) 306 (void)printf("Script done, output file is %s\n", fname); 307 } 308 exit(0); 309 } 310 311 static void 312 record(FILE *fp, char *buf, size_t cc, int direction) 313 { 314 struct iovec iov[2]; 315 struct stamp stamp; 316 struct timeval tv; 317 318 (void)gettimeofday(&tv, NULL); 319 stamp.scr_len = cc; 320 stamp.scr_sec = tv.tv_sec; 321 stamp.scr_usec = tv.tv_usec; 322 stamp.scr_direction = direction; 323 iov[0].iov_len = sizeof(stamp); 324 iov[0].iov_base = &stamp; 325 iov[1].iov_len = cc; 326 iov[1].iov_base = buf; 327 if (writev(fileno(fp), &iov[0], 2) == -1) 328 err(1, "writev"); 329 } 330 331 static void 332 consume(FILE *fp, off_t len, char *buf, int reg) 333 { 334 size_t l; 335 336 if (reg) { 337 if (fseeko(fp, len, SEEK_CUR) == -1) 338 err(1, NULL); 339 } 340 else { 341 while (len > 0) { 342 l = MIN(DEF_BUF, len); 343 if (fread(buf, sizeof(char), l, fp) != l) 344 err(1, "cannot read buffer"); 345 len -= l; 346 } 347 } 348 } 349 350 #define swapstamp(stamp) do { \ 351 if (stamp.scr_direction > 0xff) { \ 352 stamp.scr_len = bswap64(stamp.scr_len); \ 353 stamp.scr_sec = bswap64(stamp.scr_sec); \ 354 stamp.scr_usec = bswap32(stamp.scr_usec); \ 355 stamp.scr_direction = bswap32(stamp.scr_direction); \ 356 } \ 357 } while (0/*CONSTCOND*/) 358 359 static void 360 termset(void) 361 { 362 struct termios traw; 363 364 isterm = isatty(STDOUT_FILENO); 365 if (!isterm) 366 return; 367 368 if (tcgetattr(STDOUT_FILENO, &tt) != 0) 369 err(1, "tcgetattr"); 370 371 traw = tt; 372 cfmakeraw(&traw); 373 traw.c_lflag |= ISIG; 374 if (tcsetattr(STDOUT_FILENO, TCSANOW, &traw) != 0) 375 err(1, "tcsetattr"); 376 } 377 378 static void 379 termreset(void) 380 { 381 if (isterm) 382 tcsetattr(STDOUT_FILENO, TCSADRAIN, &tt); 383 384 isterm = 0; 385 } 386 387 static void 388 playback(FILE *fp) 389 { 390 struct timespec tsi, tso; 391 struct stamp stamp; 392 struct stat pst; 393 char buf[DEF_BUF]; 394 off_t nread, save_len; 395 size_t l; 396 time_t tclock; 397 int reg; 398 399 if (fstat(fileno(fp), &pst) == -1) 400 err(1, "fstat failed"); 401 402 reg = S_ISREG(pst.st_mode); 403 404 for (nread = 0; !reg || nread < pst.st_size; nread += save_len) { 405 if (fread(&stamp, sizeof(stamp), 1, fp) != 1) { 406 if (reg) 407 err(1, "reading playback header"); 408 else 409 break; 410 } 411 swapstamp(stamp); 412 save_len = sizeof(stamp); 413 414 if (reg && stamp.scr_len > 415 (uint64_t)(pst.st_size - save_len) - nread) 416 errx(1, "invalid stamp"); 417 418 save_len += stamp.scr_len; 419 tclock = stamp.scr_sec; 420 tso.tv_sec = stamp.scr_sec; 421 tso.tv_nsec = stamp.scr_usec * 1000; 422 423 switch (stamp.scr_direction) { 424 case 's': 425 if (!quiet) 426 (void)printf("Script started on %s", 427 ctime(&tclock)); 428 tsi = tso; 429 (void)consume(fp, stamp.scr_len, buf, reg); 430 termset(); 431 atexit(termreset); 432 break; 433 case 'e': 434 if (!quiet) 435 (void)printf("\nScript done on %s", 436 ctime(&tclock)); 437 (void)consume(fp, stamp.scr_len, buf, reg); 438 break; 439 case 'i': 440 /* throw input away */ 441 (void)consume(fp, stamp.scr_len, buf, reg); 442 break; 443 case 'o': 444 tsi.tv_sec = tso.tv_sec - tsi.tv_sec; 445 tsi.tv_nsec = tso.tv_nsec - tsi.tv_nsec; 446 if (tsi.tv_nsec < 0) { 447 tsi.tv_sec -= 1; 448 tsi.tv_nsec += 1000000000; 449 } 450 if (usesleep) 451 (void)nanosleep(&tsi, NULL); 452 tsi = tso; 453 while (stamp.scr_len > 0) { 454 l = MIN(DEF_BUF, stamp.scr_len); 455 if (fread(buf, sizeof(char), l, fp) != l) 456 err(1, "cannot read buffer"); 457 458 (void)write(STDOUT_FILENO, buf, l); 459 stamp.scr_len -= l; 460 } 461 break; 462 default: 463 errx(1, "invalid direction"); 464 } 465 } 466 (void)fclose(fp); 467 exit(0); 468 } 469