#! /users/jonh/bin/perl
$bindir        = "//amd/tmp_mnt/garnet/fs25/jonh/BSP/bin/";
$includedir    = "//amd/tmp_mnt/garnet/fs25/jonh/BSP/bin/../include/";
$libdir        = "//amd/tmp_mnt/garnet/fs25/jonh/BSP/bin/../lib/";
$mandir        = "/amd/tmp_mnt/garnet/fs25/jonh/BSP/man";























push(@INC,$includedir);
push(@INC,$bindir);
require "bsputil";

select(STDERR); $| = 1; select(STDOUT); # no STDERR buffering, please.
($Pgm = $0) =~ s|.*/||;
$Version        = "1.3, 25th Nov 1997";
$bug_reports_to = 'bsplib-bugs@comlab.ox.ac.uk';

$ShortUsage = "\n$Pgm usage: for basic information, try the `-help' option\n";
$Arch   = &backtick("$bindir/bsparch -arch");
$Arch   = &backtick("bsparch -arch")  if ($Arch eq "");
$Device = &backtick("$bindir/bsparch -device");
$Device = &backtick("bsparch -device") if ($Device eq "");







$Verbose    = 0;
$InputFile  = "a.out";
$OutputFile = "a.out.tab";

%nmCommand = (
        "SunOS",        "nm -n",
	"Solaris",	"nm -p -x",
	"SolarisX86",	"nm -p -x",
        "SP2",          "nm -B",
        "SGI",          "nm -B -n",
        "SGIORIGIN",    "nm -B -n",
        "SGICHALL64",   "nm -B -n",
        "OSF1",         "nm -B -n",
        "LINUX",        "nm -B -n"

);  # Nothing to do on SGI's and CRAY's




  arg: while ($_ = $ARGV[0]) {
  shift(@ARGV);
  #--------HELP------------------------------------------------
  /^-(help|man)$/ && do {&FormatManual("bspnm.1");exit(0);};

  /^-v$/          && do {$Verbose = 1; next arg;};

  /^-o$/          && do {$OutputFile = &grab_next_arg("-o");next arg;};
  
  if (-x $_) {
	$InputFile = $_;
	last arg;
  }
  print STDERR "Unrecognised option \"",$_,"\"\n";
  $Status++;
  }
  if ($Status>0) {
    print STDERR $ShortUsage;
    exit(1);
  }
  print STDERR "$Pgm: ($Version)[$Arch,$Device]\n" if $Verbose;
  $timenow = time;
  &nmFile();
  printf STDERR "$Pgm: extracted symbol table in %f seconds\n",
                 time-$timenow if $Verbose;
  exit(0);









sub myhex {
  local($x)=$_[0];

  $x = $' if ($x =~ /^0x/); 
  return(hex($x));
}

sub nmFile {
  local($line);
  local($nolines)=0;
  local($errfile)= $Tmpfile_prefix . "bspnm";
  local($error);

  if (!open(INPUT,$nmCommand{$Arch} . " " . $InputFile . " 2>$errfile |")) {
    print STDERR "$Pgm: failed to run nm";
    exit(1);
  }
  print STDERR "$Pgm: $nmCommand{$Arch} $InputFile \n" if $Verbose;
  
  if (!open(OUTPUT,"> $OutputFile")) {
    print STDERR "$Pgm: unable to open \"$OutputFile\" for writing";
    exit(1);
  }
  $read=0;                # TO DO!!! CHECK WHETHER THE FILES ARE STRIPED.
  $line=<INPUT>;
  while ($line) { 

    if ($line =~ /\.text/) {
    } elsif (($Arch eq "SunOS") || ($Arch eq "Solaris") || 
	     ($Arch eq "SolarisX86") || ($Arch eq "LINUX")) {
      if ($line=~/^\s*([a-fx0-9]+)\s+T\s+_*(.+)/) {
         print OUTPUT &myhex($1)," $2\n";
      } elsif ($line=~/^\s*([a-fx0-9]+)\s+t\s+_*(.+)/) {
          print OUTPUT &myhex($1)-1," ",$2,"\n";
      }
    } elsif ($Arch eq "OSF1") {
      if ($line=~/^\s*([a-fx0-9]+)\s+T\s+_*(.+)/) {
         $a=$1; $b=$2;
         if ($b =~ /^\./) {
           print OUTPUT &myhex($a)-1," $b\n";
         } else { print OUTPUT &myhex($a)," $b\n"; }
      } elsif ($line=~/^\s*([a-f0-9]+)\s+t\s+_*(.+)/) {
          print OUTPUT &myhex($1)-1," ",$2,"\n";
      }
    } elsif ($Arch eq "SP2") {
      if ($line=~/^\s*([0-9\-]+)\s+T\s+\.*_*(.+)/) {
        if (!(($2 eq ".bb") || ($2 eq ".bc") || ($2 eq ".bf") || ($2 eq ".bi")
           || ($2 eq ".bs") || ($2 eq ".eb") || ($2 eq ".ec") || ($2 eq ".ef")
           || ($2 eq ".ei") || ($2 eq ".es"))) {
          print OUTPUT $1," ",$2,"\n";
        } elsif ($line=~/\s*([0-9\-]+)\s+t\s+\.*_*(.+)$/) {
          print OUTPUT $1-1," ",$2,"\n";
          }
      }
    } elsif ($Arch =~/^SGI/) {               # may add SGI_NO_NAMES
      if ($line=~/^\s*([a-f0-9]+)\s+T\s+_*(.+)/) {
         print OUTPUT &myhex($1)," $2\n";
      } elsif ($line=~/^\s*([a-f0-9]+)\s+t\s+_*(.+)/) {
          print OUTPUT &myhex($1)-1," ",$2,"\n";
      } elsif ($line=~/^\s*([a-f0-9]+)\s+T\s+\.+(.+)/) {
          print OUTPUT &myhex($1)-1," ",$2,"\n";
      }
    } elsif ($Arch =~/^CRAY/) {}
    else {
         print STDERR "$Pgm: \"$Arch\" is an unknown architecture\n";
         exit(1);
      }
    if (!$read) { $line=<INPUT>; }
  }
  close INPUT;
  close OUTPUT;

  if (!open(INPUT,$errfile)) {
    print STDERR "$Pgm: failed to open error file nm";
    exit(1);
  }
  if ($line=<INPUT>) {
    print STDERR "$Pgm: nm failed to execute properly.\n";
    do {
      print "ERROR<$Pgm>: $line\n";
    } until (!($line=<INPUT>));
  }
  close INPUT;
  unlink $errfile;

}





