| 1 |
#!/usr/bin/perl -w |
| 2 |
use strict; |
| 3 |
|
| 4 |
# $Id: bones-info,v 1.7 2007-05-14 13:59:00 roderick Exp $ |
| 5 |
# |
| 6 |
# Roderick Schertler <[email protected]> |
| 7 |
# |
| 8 |
# Print some info about a Nethack bones file. |
| 9 |
|
| 10 |
# Copyright (C) 2002 Roderick Schertler |
| 11 |
# |
| 12 |
# This program is free software; you can redistribute it and/or modify |
| 13 |
# it under the terms of the GNU General Public License as published by |
| 14 |
# the Free Software Foundation; either version 2 of the License, or (at |
| 15 |
# your option) any later version. |
| 16 |
# |
| 17 |
# This program is distributed in the hope that it will be useful, |
| 18 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 |
# General Public License for more details. |
| 21 |
# |
| 22 |
# For a copy of the GNU General Public License write to the Free Software |
| 23 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 24 |
# USA |
| 25 |
|
| 26 |
use Getopt::Long (); |
| 27 |
|
| 28 |
# BS == byte sex |
| 29 |
sub BS_AUTO () { 0 } |
| 30 |
sub BS_LITTLE () { 1 } |
| 31 |
sub BS_BIG () { 2 } |
| 32 |
|
| 33 |
(my $Me = $0) =~ s-.*/--; |
| 34 |
my $Byte_sex = BS_LITTLE; |
| 35 |
my $Debug = 0; |
| 36 |
my $Exit = 0; |
| 37 |
my $Num_fmt = 'u'; |
| 38 |
my $Verbose = 0; |
| 39 |
my $Version = q$Revision: 1.7 $ =~ /(\d\S+)/ ? $1 : '?'; |
| 40 |
|
| 41 |
my @Option_spec = ( |
| 42 |
'auto|a' => sub { $Byte_sex = BS_AUTO }, |
| 43 |
'big-endian|b' => sub { $Byte_sex = BS_BIG }, |
| 44 |
'debug!' => \$Debug, |
| 45 |
'help' => sub { usage() }, |
| 46 |
'hexadecimal|x' => sub { $Num_fmt = 'x' }, |
| 47 |
'little-endian|l' => sub { $Byte_sex = BS_LITTLE }, |
| 48 |
'verbose|v' => \$Verbose, |
| 49 |
'version' => sub { print "$Me version $Version\n"; exit }, |
| 50 |
); |
| 51 |
|
| 52 |
my $Usage = <<EOF; |
| 53 |
usage: $Me [switch]... |
| 54 |
switches: |
| 55 |
-a, --auto try to guess at byte sex of each input file |
| 56 |
-b, --big-endian read version bytes in big endian (Mac) order |
| 57 |
--debug turn debugging on |
| 58 |
--help show this and then die |
| 59 |
-x, --hexadecimal output in hex rather than decimal |
| 60 |
-l, --little-endian read version bytes in little endian order (default) |
| 61 |
-v, --verbose display detailed bones info |
| 62 |
--version show the version ($Version) and exit |
| 63 |
Use \`perldoc $Me\' to see the full documentation. |
| 64 |
EOF |
| 65 |
|
| 66 |
# A @v array contains |
| 67 |
# |
| 68 |
# $v[0] "little" or "big", byte sex used to read the data |
| 69 |
# $v[1] version_info.incarnation; /* actual version number */ |
| 70 |
# $v[2] version_info.feature_set; /* bitmask of config settings */ |
| 71 |
# $v[3] version_info.entity_count; /* # of monsters and objects */ |
| 72 |
# $v[4] version_info.struct_sizes; /* size of key structs */ |
| 73 |
# |
| 74 |
# A @vdec array comes is the result of "decode_version @v", it replaces |
| 75 |
# the 4 version_info elements with array refs. The elements of the sub |
| 76 |
# arrays are indexed by the below subs. |
| 77 |
|
| 78 |
my @Feature = qw( |
| 79 |
UNKNOWN-0 |
| 80 |
REINCARNATION |
| 81 |
SINKS |
| 82 |
UNKNOWN-3 |
| 83 |
UNKNOWN-4 |
| 84 |
UNKNOWN-5 |
| 85 |
KOPS |
| 86 |
MAIL |
| 87 |
UNKNOWN-8 |
| 88 |
UNKNOWN-9 |
| 89 |
TOURIST |
| 90 |
STEED |
| 91 |
GOLDOBJ |
| 92 |
UNKNOWN-13 |
| 93 |
UNKNOWN-14 |
| 94 |
UNKNOWN-15 |
| 95 |
UNKNOWN-16 |
| 96 |
TEXTCOLOR |
| 97 |
INSURANCE |
| 98 |
ELBERETH |
| 99 |
EXP_ON_BOTL |
| 100 |
SCORE_ON_BOTL |
| 101 |
UNKNOWN-22 |
| 102 |
TIMED_DELAY |
| 103 |
UNKNOWN-24 |
| 104 |
UNKNOWN-25 |
| 105 |
UNKNOWN-26 |
| 106 |
ZEROCOMP |
| 107 |
RLECOMP |
| 108 |
UNKNOWN-29 |
| 109 |
UNKNOWN-30 |
| 110 |
UNKNOWN-31 |
| 111 |
); |
| 112 |
|
| 113 |
sub V1_VERSION_MAJOR () { 0 } |
| 114 |
sub V1_VERSION_MINOR () { 1 } |
| 115 |
sub V1_PATCH_LEVEL () { 2 } |
| 116 |
sub V1_EDIT_LEVEL () { 3 } |
| 117 |
|
| 118 |
sub V3_ARTIFACTS () { 0 } |
| 119 |
sub V3_OBJECTS () { 1 } |
| 120 |
sub V3_MONSTERS () { 2 } |
| 121 |
|
| 122 |
sub V4_FLAG () { 0 } |
| 123 |
sub V4_OBJ () { 1 } |
| 124 |
sub V4_MONST () { 2 } |
| 125 |
sub V4_YOU () { 3 } |
| 126 |
|
| 127 |
sub debug { |
| 128 |
print STDERR "debug: ", @_, "\n" if $Debug; |
| 129 |
} |
| 130 |
|
| 131 |
sub usage { |
| 132 |
warn "$Me: ", @_ if @_; |
| 133 |
# Use exit() rather than die(), as Getopt::Long does eval(). |
| 134 |
print STDERR $Usage; |
| 135 |
exit 1; |
| 136 |
} |
| 137 |
|
| 138 |
# Getopt::Long has some really awful defaults. This function configures |
| 139 |
# it to use more sane settings. |
| 140 |
|
| 141 |
sub getopt { |
| 142 |
Getopt::Long->import(2.11); |
| 143 |
|
| 144 |
# I'm setting this environment variable lest he sneaks more bad |
| 145 |
# defaults into the module. |
| 146 |
local $ENV{POSIXLY_CORRECT} = 1; |
| 147 |
Getopt::Long::config qw( |
| 148 |
default |
| 149 |
no_autoabbrev |
| 150 |
no_getopt_compat |
| 151 |
require_order |
| 152 |
bundling |
| 153 |
no_ignorecase |
| 154 |
); |
| 155 |
|
| 156 |
return Getopt::Long::GetOptions @_; |
| 157 |
} |
| 158 |
|
| 159 |
sub init { |
| 160 |
getopt @Option_spec or usage; |
| 161 |
} |
| 162 |
|
| 163 |
# Decode raw version info values. |
| 164 |
|
| 165 |
sub decode_version { |
| 166 |
my @v = @_; |
| 167 |
my (@v1, @v2, @v3, @v4); |
| 168 |
|
| 169 |
$v1[V1_VERSION_MAJOR] = ($v[1] & (255 << 24)) >> 24; |
| 170 |
$v1[V1_VERSION_MINOR] = ($v[1] & (255 << 16)) >> 16; |
| 171 |
$v1[V1_PATCH_LEVEL] = ($v[1] & (255 << 8)) >> 8; |
| 172 |
$v1[V1_EDIT_LEVEL] = ($v[1] & 255); |
| 173 |
|
| 174 |
@v2 = split //, unpack "b*", pack "V", $v[2]; |
| 175 |
|
| 176 |
$v3[V3_ARTIFACTS] = ($v[3] & (255 << 24)) >> 24; |
| 177 |
$v3[V3_OBJECTS] = ($v[3] & (4095 << 12)) >> 12; |
| 178 |
$v3[V3_MONSTERS] = ($v[3] & 4095); |
| 179 |
|
| 180 |
$v4[V4_FLAG] = ($v[4] & (255 << 24)) >> 24; |
| 181 |
$v4[V4_OBJ] = ($v[4] & (127 << 17)) >> 17; |
| 182 |
$v4[V4_MONST] = ($v[4] & (127 << 10)) >> 10; |
| 183 |
$v4[V4_YOU] = ($v[4] & 1023); |
| 184 |
|
| 185 |
return $v[0], \@v1, \@v2, \@v3, \@v4; |
| 186 |
} |
| 187 |
|
| 188 |
# Return true if it looks like the given version info is invalid. |
| 189 |
|
| 190 |
sub invalid_version { |
| 191 |
my (@vdec) = decode_version @_; |
| 192 |
return 1 if $vdec[1][V1_VERSION_MAJOR] < 3; |
| 193 |
return 1 if $vdec[4][V4_MONST] > 1000; |
| 194 |
return 1 if grep { $vdec[2][$_] && $Feature[$_] =~ /^UNKNOWN/ } |
| 195 |
0..$#{ $vdec[2] }; |
| 196 |
return 0; |
| 197 |
} |
| 198 |
|
| 199 |
# Output version info info in verbose form. |
| 200 |
|
| 201 |
sub verbose { |
| 202 |
my ($file, $size, @vin) = @_; |
| 203 |
my @vdec = decode_version @vin; |
| 204 |
|
| 205 |
print "$file: $size bytes\n"; |
| 206 |
|
| 207 |
printf " read as: %s endian\n", $vin[0]; |
| 208 |
|
| 209 |
printf " incarnation: %-10$Num_fmt (%s)\n", $vin[1], |
| 210 |
join '.', @{ $vdec[1] }; |
| 211 |
|
| 212 |
my $l = sprintf " feature_set: %-10$Num_fmt (", $vin[2]; |
| 213 |
my @f = map { $Feature[$_] } grep { $vdec[2][$_] } 0..$#Feature; |
| 214 |
my $w = 78; |
| 215 |
while (@f) { |
| 216 |
my $s = "$l" . shift @f; # always eat at least 1 @f |
| 217 |
$s .= ' ' . shift @f while @f && length("$s $f[0]") <= $w; |
| 218 |
$s .= ")" unless @f; |
| 219 |
print "$s\n"; |
| 220 |
$l = ' ' x length $l; |
| 221 |
} |
| 222 |
print "$l)\n" if $l =~ /\S/; # no feature were set |
| 223 |
|
| 224 |
printf " entity_count: %-10$Num_fmt (%s)\n", $vin[3], join ', ', |
| 225 |
"$vdec[3][V3_ARTIFACTS] artifacts", |
| 226 |
"$vdec[3][V3_OBJECTS] objects", |
| 227 |
"$vdec[3][V3_MONSTERS] monsters"; |
| 228 |
|
| 229 |
printf " struct_sizes: %-10$Num_fmt (%s)\n", $vin[4], join ', ', |
| 230 |
"$vdec[4][V4_FLAG] flag", |
| 231 |
"$vdec[4][V4_OBJ] obj", |
| 232 |
"$vdec[4][V4_MONST] monst", |
| 233 |
"$vdec[4][V4_YOU] you"; |
| 234 |
|
| 235 |
print "\n"; |
| 236 |
} |
| 237 |
|
| 238 |
sub one_file { |
| 239 |
my ($file) = @_; |
| 240 |
|
| 241 |
my $open = $file; |
| 242 |
$open = "gzip -dc \Q$file\E |" |
| 243 |
if $file =~ /\.(gz|z|Z)\z/; |
| 244 |
if (!open FILE, $open) { |
| 245 |
warn "$Me: can't read $open: $!\n"; |
| 246 |
$Exit ||= 1; |
| 247 |
return; |
| 248 |
} |
| 249 |
|
| 250 |
my $data = do { local $/; <FILE> }; |
| 251 |
my $size = length $data; |
| 252 |
my @v_little = (little => unpack 'V' x 4, $data); |
| 253 |
my @v_big = (big => unpack 'N' x 4, $data); |
| 254 |
|
| 255 |
my @v; |
| 256 |
if ($Byte_sex == BS_AUTO) { |
| 257 |
my $good_little = !invalid_version @v_little; |
| 258 |
my $good_big = !invalid_version @v_big; |
| 259 |
if (!($good_little ^ $good_big)) { |
| 260 |
warn "$Me: can't intuit byte sex of $file\n"; |
| 261 |
$Exit ||= 1; |
| 262 |
return; |
| 263 |
} |
| 264 |
@v = $good_little ? @v_little : @v_big; |
| 265 |
} |
| 266 |
else { |
| 267 |
@v = $Byte_sex == BS_LITTLE ? @v_little : @v_big; |
| 268 |
} |
| 269 |
|
| 270 |
if ($Verbose) { |
| 271 |
verbose $file, $size, @v; |
| 272 |
} |
| 273 |
else { |
| 274 |
my $n = "%-10$Num_fmt"; |
| 275 |
printf "%-11s sex=%s v1=$n v2=$n v3=$n v4=$n\n", |
| 276 |
$file, substr($v[0], 0, 1), @v[1..4]; |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
sub main { |
| 281 |
init; |
| 282 |
@ARGV or die "$Me: no files specified\n"; |
| 283 |
one_file $_ for @ARGV; |
| 284 |
return 0; |
| 285 |
} |
| 286 |
|
| 287 |
$Exit = main || $Exit; |
| 288 |
$Exit = 1 if $Exit && !($Exit % 256); |
| 289 |
exit $Exit; |
| 290 |
|
| 291 |
__END__ |
| 292 |
|
| 293 |
=head1 NAME |
| 294 |
|
| 295 |
bones-info - display information about a Nethack bones file |
| 296 |
|
| 297 |
=head1 SYNOPSIS |
| 298 |
|
| 299 |
B<bones-info> |
| 300 |
[B<-a | --auto>] |
| 301 |
[B<-b | --big-endian>] |
| 302 |
[B<--debug>] |
| 303 |
[B<--help>] |
| 304 |
[B<-x | --hexadecimal>] |
| 305 |
[B<-l | --little-endian>] |
| 306 |
[B<-v | --verbose>] |
| 307 |
[B<--version>] |
| 308 |
I<file>... |
| 309 |
|
| 310 |
=head1 DESCRIPTION |
| 311 |
|
| 312 |
B<bones-info> displays information about a Nethack bones file. By |
| 313 |
default it shows what byte sex it used to read the file and the 4 |
| 314 |
version numbers which constitute the feature set and platform for the |
| 315 |
Nethack binary which generated it. |
| 316 |
|
| 317 |
=head1 ENDIANNESS (aka BYTE SEX) |
| 318 |
|
| 319 |
Normally B<bones-info> reads the bones file in little endian order, |
| 320 |
regardless of the byte sex of the current system, mostly because it was |
| 321 |
originally written to help with diagnosing problems with |
| 322 |
L<hearse|hearse> and that's the most useful behavior for that purpose. |
| 323 |
You can use the B<--auto>, B<--big-endian>, and B<--little-endian> |
| 324 |
switches to change this. |
| 325 |
|
| 326 |
B<--auto> is particularly useful (and appropriate) when using B<--verbose>. |
| 327 |
|
| 328 |
=head1 OPTIONS |
| 329 |
|
| 330 |
=over 4 |
| 331 |
|
| 332 |
=item B<-a>, B<--auto> |
| 333 |
|
| 334 |
Try to guess the right byte sex (little endian or big endian) for each |
| 335 |
input file. If there doesn't seem to be a right choice, B<bones-info> |
| 336 |
will output a warning, set a non-zero exit status, and move on to the |
| 337 |
next file. |
| 338 |
|
| 339 |
=item B<-b>, B<--big-endian> |
| 340 |
|
| 341 |
Read the bones files in big endian order, such as is used by Macs. See |
| 342 |
also L</--auto>. |
| 343 |
|
| 344 |
=item B<--debug> |
| 345 |
|
| 346 |
Turn debugging on. |
| 347 |
|
| 348 |
=item B<--help> |
| 349 |
|
| 350 |
Show the usage message and die. |
| 351 |
|
| 352 |
=item B<-x>, B<--hexadecimal> |
| 353 |
|
| 354 |
Output numbers in hexadecimal form. |
| 355 |
|
| 356 |
=item B<-l>, B<--little-endian> |
| 357 |
|
| 358 |
Read the bones files in little endian order, such as is used by Intel |
| 359 |
hardware. This is the default, I include it so that you don't have to |
| 360 |
check what the default is if you know you want it a certain way. |
| 361 |
|
| 362 |
=item B<-v>, B<--verbose> |
| 363 |
|
| 364 |
Output more info about the bones file. This tries to decode the 4 |
| 365 |
version numbers. Its useful when you want to see what the differences |
| 366 |
are between two sets of version numbers. You'd normally want to use |
| 367 |
B<--auto> when you use B<--verbose>. |
| 368 |
|
| 369 |
=item B<--version> |
| 370 |
|
| 371 |
Show the version number and exit. |
| 372 |
|
| 373 |
=back |
| 374 |
|
| 375 |
=head1 EXAMPLES |
| 376 |
|
| 377 |
Output the values as used by the L<hearse|hearse> server: |
| 378 |
|
| 379 |
$ bones-info * |
| 380 |
bonD0.0 sex=l v1=1 v2=2 v3=3 v4=4 |
| 381 |
bonD0.4.gz sex=l v1=50593792 v2=10357958 v3=555422078 v4=2759955912 |
| 382 |
bonD0.8.Z sex=l v1=1027 v2=3322682880 v3=2115050273 v4=3365241252 |
| 383 |
bonD0.19 sex=l v1=50528512 v2=10357830 v3=555409789 v4=2558629316 |
| 384 |
bonM0.1 sex=l v1=50593792 v2=404622406 v3=555417981 v4=2759955916 |
| 385 |
bonM0.T sex=l v1=50593792 v2=1969222 v3=555417981 v4=2759955912 |
| 386 |
|
| 387 |
Output the real values as seen on the system which wrote the file (by |
| 388 |
guessing the byte sex of the file): |
| 389 |
|
| 390 |
$ bones-info --auto * |
| 391 |
bones-info: can't intuit byte sex of bonD0.0 |
| 392 |
bonD0.4.gz sex=l v1=50593792 v2=10357958 v3=555422078 v4=2759955912 |
| 393 |
bonD0.8.Z sex=b v1=50593792 v2=1969350 v3=555422078 v4=2759955912 |
| 394 |
bonD0.19 sex=l v1=50528512 v2=10357830 v3=555409789 v4=2558629316 |
| 395 |
bonM0.1 sex=l v1=50593792 v2=404622406 v3=555417981 v4=2759955916 |
| 396 |
bonM0.T sex=l v1=50593792 v2=1969222 v3=555417981 v4=2759955912 |
| 397 |
zsh: exit 1 bones-info --auto * |
| 398 |
|
| 399 |
Decode the version numbers: |
| 400 |
|
| 401 |
$ bones-info --auto --verbose bonD0.4.gz bonD0.8.Z |
| 402 |
bonD0.4.gz: 18389 bytes |
| 403 |
read as: little endian |
| 404 |
incarnation: 50593792 (3.4.0.0) |
| 405 |
feature_set: 10357958 (REINCARNATION SINKS KOPS MAIL TOURIST STEED |
| 406 |
TEXTCOLOR INSURANCE ELBERETH EXP_ON_BOTL |
| 407 |
TIMED_DELAY) |
| 408 |
entity_count: 555422078 (33 artifacts, 433 objects, 382 monsters) |
| 409 |
struct_sizes: 2759955912 (164 flag, 64 obj, 101 monst, 456 you) |
| 410 |
|
| 411 |
bonD0.8.Z: 22296 bytes |
| 412 |
read as: big endian |
| 413 |
incarnation: 50593792 (3.4.0.0) |
| 414 |
feature_set: 1969350 (REINCARNATION SINKS KOPS MAIL TOURIST STEED |
| 415 |
TEXTCOLOR INSURANCE ELBERETH EXP_ON_BOTL) |
| 416 |
entity_count: 555422078 (33 artifacts, 433 objects, 382 monsters) |
| 417 |
struct_sizes: 2759955912 (164 flag, 64 obj, 101 monst, 456 you) |
| 418 |
|
| 419 |
$ _ |
| 420 |
|
| 421 |
=head1 BUGS |
| 422 |
|
| 423 |
Unsigned longs are assumed to be 4 bytes. |
| 424 |
|
| 425 |
The --auto byte sex detection isn't robust. |
| 426 |
|
| 427 |
It'd be nice to be provide --verbose output for bones files from older |
| 428 |
versions. |
| 429 |
|
| 430 |
=head1 AVAILABILITY |
| 431 |
|
| 432 |
This program is distributed with the Unix Hearse client. The code is |
| 433 |
licensed under the GNU GPL. Check http://www.argon.org/~roderick/hearse/ |
| 434 |
for updated versions. |
| 435 |
|
| 436 |
=head1 AUTHOR |
| 437 |
|
| 438 |
Roderick Schertler <[email protected]> |
| 439 |
|
| 440 |
=cut |