#!/usr/bin/perl -w

# decomp_fixup_signs : A decomp filter to ensure -ve numbers are displayed -ve
# Copyright (C) 2002 Dion Mendel
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

# decomp_fixup_signs converts numbers like 0xfffffffd to -0x3.
# This makes reading the dump much easier.
#
# Sample usage:
# % gendump the-binary | decomp_fixup_signs > dump

use Getopt::Long "GetOptions";
use FileHandle;
use strict 'vars';
use vars '$VERSION', '$Verbose';

$VERSION = "1.0";                            # version of this program
$Verbose = 1;


&parse_command_line_for_options();
&usage if (scalar @ARGV != 0);

# do the filtering
&filter();


exit 0;

############################### Filter Functions ##############################

# -----------------------------------------------------------------------------
# Reads in data from stdin, performs the filter, then write result to stdout.
# Params: @symbols - list of symbols
# Returns: none

sub filter(\@)
{
   my ($ref_offsets) = @_;
   my ($line);

   # for each line (representing an object file)
   while ($line = <STDIN>) {
      chomp($line);

      # skip empty lines and comment lines
      unless ($line =~ /^$/ or $line =~ /^#/ or $line =~ /^;/) {
         $line =~ s/(0x[89a-f][0-9a-f]{7})\b/sprintf("-0x%x", 1+~hex($1))/eg;
      }

      print STDOUT $line, "\n";
   }
}

############################### Usage Functions ###############################

# -----------------------------------------------------------------------------
# Parses the command line for any specified options.  Sets the appropriate
# option flags if options are specified.  Prints usage info if invalid options
# are given.
# Returns: nothing

sub parse_command_line_for_options()
{
   my ($want_quiet)   = 0;
   my ($want_version) = 0;
   my ($want_help)    = 0;

   &GetOptions("q|quiet"   => \$want_quiet,
               "V|version" => \$want_version,
               "h|help"    => \$want_help,
              );

   if ($want_version) {
      print "$0 $VERSION\n";
      exit 0;
   }

   if ($want_help) {
      &usage();
   }

   $Verbose = !$want_quiet;
}

# -----------------------------------------------------------------------------
# Prints a nice usage message to stdout, and then exits.

sub usage()
{
   print <<"_END";

$0 v${VERSION}
A decomp filter to fixup signs for numbers in the dump.
Reads in the current dump from stdin and writes the modified dump to stdout.

Usage: $0 [options]

Options:
    -V, --version         outputs version information and exits
    -h, --help            displays this help and exits

_END

   exit 1;
}

