]> git.pond.sub.org Git - empserver/blob - tests/normalize.pl
New make target check
[empserver] / tests / normalize.pl
1 #!/usr/bin/perl
2
3 # TODO Don't hardcode xdump columns, get them from xdump meta
4
5 use warnings;
6 use strict;
7 use Getopt::Std;
8
9 $Getopt::Std::STANDARD_HELP_VERSION = 1;
10 our ($opt_j, $opt_s);
11 getopts('js')
12     or die "$0: invalid options\n";
13 die "$0: either -j or -s, not both\n"
14     if $opt_j && $opt_s;
15
16 my $ctime_re = qr/(Sun|Mon|Tue|Wed|Thu|Fri|Sat) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) [ 123][0-9] [0-2][0-9]:[0-5][0-9]:[0-6][0-9] [0-9][0-9][0-9][0-9]/;
17 my $xdfld_re = qr/\([^)]*\)|[^ (][^ ]*/;
18
19 # Current dump, if any
20 # Either zero or the name of the dump we're currently processing.
21 # Legacy dump names start with an uppercase letter, and xdump names
22 # start with a lowercase letter.
23 my $dump = "";
24
25 sub norm_ctime {
26     my ($s) = @_;
27     $s =~ s/$ctime_re/Thu Jan  1 00:00:00 1970/g;
28     return $s;
29 }
30
31 while (<>) {
32     chomp;
33
34     my $pfx = '';
35
36     if ($opt_j || $opt_s) {
37         die "$0: malformed line" unless substr($_, 24, 1) eq ' ';
38         $pfx .= norm_ctime(substr($_, 0, 25));
39         $_ = substr($_, 25);
40     }
41
42     if ($opt_j) {
43         die "$0: malformed line" unless substr($_, 10, 1) eq ' ';
44         $pfx .= substr($_, 0, 11);
45         $_ = substr($_, 11);
46         if (/(^output [^ ]* 1 )(.*)/) {
47             $pfx .= $1;
48             $_ = $2;
49         } else {
50             $pfx .= $_;
51             $_ = '';
52         }
53     }
54
55     if ($opt_s) {
56         $_ = norm_ctime($_);
57         ### Host environment in logs
58         # getrusage() results in server.log
59         s/(End update|done assembling paths) .* user .* system/$1 0.0 user 0.0 system/g;
60         # PID in server.log
61         s/(Empire server \(pid) [0-9]+(\) started)/$1 42$2/g;
62         ### Harmless races
63         # shutdown wins race with logout
64         next if /Waiting for player threads to terminate/;
65         print "$pfx$_\n";
66         next;
67     }
68
69     $dump = ""
70         if ($dump =~ /^[a-z]/
71             and /^\//)
72         or ($dump =~ /^[A-Z]/
73             and (/\: No (sector|ship|plane|unit|nuke)\(s\)|\: Nothing lost/
74                  or /^[0-9]+ (sector|ship|plane|unit|nuke|lost item)/));
75
76     ### Formatted time
77     # nat_timeused in prompt
78     s/^\[[0-9]+(:[0-9]+\] Command \:)/[0$1/g;
79     # TODO command play column time
80     # result of ctime() in many commands
81     $_ = norm_ctime($_)
82         unless $dump;
83     ### Time values in legacy dumps
84     s/(DUMP (SECTOR|SHIPS|PLANES|LAND UNITS|NUKES|LOST ITEMS)) [0-9]+$/$1 0/g;
85     s/ [0-9]+$/ 0/
86         if $dump eq 'LOST ITEMS';
87     ### Time values in xdump
88     s/(XDUMP (meta )?[-a-z0-9]+) [0-9]+$/$1 0/
89         unless $dump;
90     # HACK: assume any integer with more than 10 digits is time
91     # TODO don't do that, use xdump meta instead
92     s/(^| )[0-9]{10,}/${1}0/g
93         if $dump =~ /^[a-z]/;
94     # timeused in xdump country (column 9)
95     s/^(($xdfld_re ){9})([0-9]+) /${1}255 /
96         if $dump eq 'country';
97     # timeused in xdump nat (column 14)
98     s/^(($xdfld_re ){14})([0-9]+) /${1}255 /
99         if $dump eq 'nat';
100     # duration in xdump news (column 4)
101     s/^(($xdfld_re ){4})([0-9]+) /${1}0 /
102         if $dump eq 'news';
103     ### nsc_type values in xdump
104     # Can vary between systems, because the width of enumeration types
105     # is implementation-defined.
106     # TODO type in xdump meta
107     ### nrndx values in xdump
108     # Encoding depends on the host, see resources[].  Too clever by half;
109     # perhaps we should change it.
110     # nrndx in xdump product (column 12)
111     s/^(($xdfld_re ){12})([0-9]+) /${1}0 /
112         if $dump eq 'product';
113     # value in xdump resources (column 0)
114     s/^[0-9]+ /0 /
115         if $dump eq 'resources';
116     ### Floating-point zero in xdump
117     # Windows %#g prints it with seven significant digits instead of six
118     s/ 0\.000000/ 0.00000/g
119         if $dump =~ /^[a-z]/;
120
121     print "$pfx$_\n";
122
123     if (/XDUMP (meta )?([-a-z0-9]+)/) {
124         $dump = $2;
125         die unless $dump =~ /^[a-z]/;
126     } elsif (/DUMP (SECTOR|SHIPS|PLANES|LAND UNITS|NUKES|LOST ITEMS) /) {
127         $dump = $1;
128     }
129 }