#!/usr/bin/perl 

# Dump the correspondence between frame numbers and file positions in
# a ttyrec, with timestamps for good measure. With -f, also produce a
# hex dump of the terminal contents in each frame.

@args = ();
$opts = 1;
$join = 0;
$full = 0;
$delta = 0;
$to_script = 0;
$lastframe = -1;
foreach $arg (@ARGV) {
    if ($opts and $arg =~ /^-/) {
        if ($arg eq "--") {
            $opts = 0;
        } elsif ($arg =~ /^--([^=]+)(?:=(.*))?$/) {
            my ($opt, $val) = ($1, $2);
            if ($opt eq "help") {
                # Deliberately do not reject --help=value; once the
                # user is _trying_ to ask for help, we should give it
                # without quibbling.
                print
"usage:    ttydump [options] <file> [<file>...]\
options:  -f        write a hex dump of each frame's data after the summary\
          -d        write each timestamp as a delta from the previous one\
          -s        output a plain stream of tty output, as if from script(1)\
          -j        treat all input files as one long one\
          --upto=N  only display frames up to the Nth\
          --help    display this text\
";
                exit 0;
            } elsif ($opt eq "upto") {
                die "ttydump: option '--upto' expects an argument ".
                    "(try 'ttydump --help')\n" unless defined $val;
                $lastframe = $val;
            } else {
                die "ttydump: unrecognised long option '--$opt' ".
                    "(try 'ttydump --help')\n";
            }
        } else {
            my $tail = substr $arg, 1;
            while ($tail =~ /^([a-zA-Z])(.*)/) {
                my $opt = $1;
                $tail = $2;
                if ($opt eq "j") {
                    $join = 1;
                } elsif ($opt eq "s") {
                    $to_script = 1;
                } elsif ($opt eq "f") {
                    $full = 1;
                } elsif ($opt eq "d") {
                    $delta = 1;
                } else {
                    die "ttydump: unrecognised short option '-$opt' ".
                        "(try 'ttydump --help')\n";
                }
            }
        }
    } else {
        push @args, $arg;
    }
}

if (!defined $args[0]) {
    die "ttydump: expected an input file name ".
        "(try 'ttydump --help')\n";
}

$tframe = 0;
$prev_time = undef;

foreach $file (@args) {
    open F, "<$file" or die "$0: unable to open '$file'\n";

    $frame = 0;

    while ($lastframe < 0 || $frame <= $lastframe) {
        $hdr = "";
        $hgot = read F, $hdr, 12;
        last if $hgot == 0; # clean EOF
        die "$0: unexpected EOF in '$file' frame header\n" if $hgot < 12;
        @hdrvals = unpack "VVV", $hdr;
        $dlen = $hdrvals[2];
        $data = "";
        $posdata = sprintf "offset 0x%x len 0x%x", (tell F), $dlen;
        $posdata = "$file " . $posdata if $join;
        if ($delta) {
            $this_time = $hdrvals[0] + $hdrvals[1] / 1000000.0;
            $prev_time = $this_time unless defined $prev_time;
            $timestamp = sprintf "+%.6f", $this_time - $prev_time;
            $prev_time = $this_time;
        } else {
            $timestamp = sprintf "%d.%06d", $hdrvals[0], $hdrvals[1];
        }
        $dgot = read F, $data, $dlen;
        die "$0: unexpected EOF in '$file' frame data\n" if $dgot < $dlen;
        if ($join) {
            $location = "$tframe";
        } else {
            $location = "$file:$frame";
        }
        if ($to_script) {
            print $data;
        } else {
            print "$location:$timestamp:$posdata\n";
            if ($full) {
                for ($pos = 0; $pos < length $data; $pos += 16) {
                    @dsub = unpack "C*", substr $data, $pos, 16;
                    printf "  %06x ", $pos;
                    for $chr (@dsub) {
                        printf " %02x", $chr;
                    }
                    print " " x (2 + 3 * (16 - @dsub));
                    for $chr (@dsub) {
                        if (defined $chr) {
                            if (0x20 <= $chr && $chr < 0x7F) {
                                printf "%c", $chr;
                            } else {
                                print ".";
                            }
                        }
                    }
                    print "\n";
                }
            }
        }
        $frame++;
        $tframe++;
    }

    close F;
}
