View | Details | Raw Unified | Return to bug 248377 | Differences between
and this patch

Collapse All | Expand All

(-)script.c (-7 / +55 lines)
Lines 30-35 Link Here
30
 * SUCH DAMAGE.
30
 * SUCH DAMAGE.
31
 */
31
 */
32
32
33
/*
34
 * 2020-07-31 Soumendra Ganguly <soumendraganguly@gmail.com>
35
 * - fixed playback of curses sessions
36
 */
37
33
#include <sys/param.h>
38
#include <sys/param.h>
34
__FBSDID("$FreeBSD$");
39
__FBSDID("$FreeBSD$");
35
#ifndef lint
40
#ifndef lint
Lines 400-418 Link Here
400
}
405
}
401
406
402
static void
407
static void
408
termset()
409
{
410
	struct termios tattr;
411
412
	ttyflg = isatty(STDOUT_FILENO);
413
	if (!ttyflg)
414
		return;
415
416
	if (tcgetattr(STDOUT_FILENO, &tt) != 0)
417
		err(1, "tcgetattr");
418
419
	tattr = tt;
420
	cfmakeraw(&tattr);
421
	tattr.c_lflag |= ISIG;
422
	if (tcsetattr(STDOUT_FILENO, TCSANOW, &tattr) != 0)
423
		err(1, "tcsetattr");
424
}
425
426
static void
427
termreset()
428
{
429
	if (ttyflg)
430
		tcsetattr(STDOUT_FILENO, TCSADRAIN, &tt);
431
432
	ttyflg = 0;
433
}
434
435
static void
403
consume(FILE *fp, off_t len, char *buf, int reg)
436
consume(FILE *fp, off_t len, char *buf, int reg)
404
{
437
{
405
	size_t l;
438
	size_t l;
406
439
407
	if (reg) {
440
	if (reg) {
408
		if (fseeko(fp, len, SEEK_CUR) == -1)
441
		if (fseeko(fp, len, SEEK_CUR) == -1) {
442
			termreset();
409
			err(1, NULL);
443
			err(1, NULL);
444
		}
410
	}
445
	}
411
	else {
446
	else {
412
		while (len > 0) {
447
		while (len > 0) {
413
			l = MIN(DEF_BUF, len);
448
			l = MIN(DEF_BUF, len);
414
			if (fread(buf, sizeof(char), l, fp) != l)
449
			if (fread(buf, sizeof(char), l, fp) != l) {
450
				termreset();
415
				err(1, "cannot read buffer");
451
				err(1, "cannot read buffer");
452
			}
416
			len -= l;
453
			len -= l;
417
		}
454
		}
418
	}
455
	}
Lines 439-444 Link Here
439
	time_t tclock;
476
	time_t tclock;
440
	int reg;
477
	int reg;
441
478
479
	ttyflg = 0;
480
442
	if (fstat(fileno(fp), &pst) == -1)
481
	if (fstat(fileno(fp), &pst) == -1)
443
		err(1, "fstat failed");
482
		err(1, "fstat failed");
444
483
Lines 446-462 Link Here
446
485
447
	for (nread = 0; !reg || nread < pst.st_size; nread += save_len) {
486
	for (nread = 0; !reg || nread < pst.st_size; nread += save_len) {
448
		if (fread(&stamp, sizeof(stamp), 1, fp) != 1) {
487
		if (fread(&stamp, sizeof(stamp), 1, fp) != 1) {
449
			if (reg)
488
			if (reg) {
489
				termreset();
450
				err(1, "reading playback header");
490
				err(1, "reading playback header");
451
			else
491
			}
452
				break;
492
			break;
453
		}
493
		}
454
		swapstamp(stamp);
494
		swapstamp(stamp);
455
		save_len = sizeof(stamp);
495
		save_len = sizeof(stamp);
456
496
457
		if (reg && stamp.scr_len >
497
		if (reg && stamp.scr_len >
458
		    (uint64_t)(pst.st_size - save_len) - nread)
498
		    (uint64_t)(pst.st_size - save_len) - nread) {
499
			termreset();
459
			errx(1, "invalid stamp");
500
			errx(1, "invalid stamp");
501
		}
460
502
461
		save_len += stamp.scr_len;
503
		save_len += stamp.scr_len;
462
		tclock = stamp.scr_sec;
504
		tclock = stamp.scr_sec;
Lines 470-477 Link Here
470
				ctime(&tclock));
512
				ctime(&tclock));
471
			tsi = tso;
513
			tsi = tso;
472
			(void)consume(fp, stamp.scr_len, buf, reg);
514
			(void)consume(fp, stamp.scr_len, buf, reg);
515
			termset();
473
			break;
516
			break;
474
		case 'e':
517
		case 'e':
518
			termreset();
475
			if (!qflg)
519
			if (!qflg)
476
				(void)printf("\nScript done on %s",
520
				(void)printf("\nScript done on %s",
477
				    ctime(&tclock));
521
				    ctime(&tclock));
Lines 493-509 Link Here
493
			tsi = tso;
537
			tsi = tso;
494
			while (stamp.scr_len > 0) {
538
			while (stamp.scr_len > 0) {
495
				l = MIN(DEF_BUF, stamp.scr_len);
539
				l = MIN(DEF_BUF, stamp.scr_len);
496
				if (fread(buf, sizeof(char), l, fp) != l)
540
				if (fread(buf, sizeof(char), l, fp) != l) {
541
					termreset();
497
					err(1, "cannot read buffer");
542
					err(1, "cannot read buffer");
543
				}
498
544
499
				(void)write(STDOUT_FILENO, buf, l);
545
				(void)write(STDOUT_FILENO, buf, l);
500
				stamp.scr_len -= l;
546
				stamp.scr_len -= l;
501
			}
547
			}
502
			break;
548
			break;
503
		default:
549
		default:
550
			termreset();
504
			errx(1, "invalid direction");
551
			errx(1, "invalid direction");
505
		}
552
		}
506
	}
553
	}
554
	termreset();
507
	(void)fclose(fp);
555
	(void)fclose(fp);
508
	exit(0);
556
	exit(0);
509
}
557
}

Return to bug 248377