dpb-replay 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #! /usr/bin/perl
  2. # ex:ts=8 sw=4:
  3. # $OpenBSD: dpb-replay,v 1.4 2017/01/25 14:13:50 espie Exp $
  4. #
  5. # Copyright (c) 2013 Marc Espie <espie@openbsd.org>
  6. #
  7. # Permission to use, copy, modify, and distribute this software for any
  8. # purpose with or without fee is hereby granted, provided that the above
  9. # copyright notice and this permission notice appear in all copies.
  10. #
  11. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  12. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  13. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  14. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  15. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  16. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  17. # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  18. use strict;
  19. use warnings;
  20. use FindBin;
  21. my $ports1;
  22. BEGIN {
  23. $ports1 = $ENV{PORTSDIR} || '/usr/ports';
  24. }
  25. use lib ("$ports1/infrastructure/lib", "$FindBin::Bin/../lib");
  26. use DPB::MiniCurses;
  27. use OpenBSD::State;
  28. package DPBReplay::State;
  29. our @ISA = (qw(OpenBSD::State));
  30. sub handle_options
  31. {
  32. my $state = shift;
  33. $state->SUPER::handle_options('cs:', '[-c] [-s speedup] file');
  34. if ($state->opt('c')) {
  35. $state->{color} = 1;
  36. }
  37. }
  38. package Term;
  39. our @ISA = qw(DPB::MiniCurses);
  40. sub new
  41. {
  42. my ($class, $state) = @_;
  43. my $self = bless {state => $state}, $class;
  44. $self->create_terminal;
  45. return $self;
  46. }
  47. package main;
  48. use Time::HiRes (qw(time sleep));
  49. my $state = DPBReplay::State->new('dpb-replay');
  50. $state->handle_options;
  51. if (@ARGV == 0) {
  52. $state->usage("Missing term-report file");
  53. }
  54. my $file = shift;
  55. my $speedup = $state->opt('s') // 10;
  56. $speedup += 0.0;
  57. my $term = Term->new($state);
  58. open(my $fh, '<', $file);
  59. my $start_ts;
  60. my $start_time = time();
  61. my $msg = '';
  62. while(<$fh>) {
  63. if (m/^\@\@\@(\d+)$/) {
  64. chomp;
  65. my $ts = int($1);
  66. $start_ts //= $ts;
  67. my $now = time();
  68. my $sleep = ($ts-$start_ts)/$speedup - ($now - $start_time);
  69. if ($sleep > 0) {
  70. sleep($sleep);
  71. }
  72. my $method = $term->{write};
  73. $term->$method($msg);
  74. $term->{msg} = $msg;
  75. $msg = '';
  76. } else {
  77. $msg .= $_;
  78. }
  79. }
  80. close($fh);