#!/usr/bin/perl -w

use strict;
my %fileseen = ();
my @errno = ();
my %sigs = ();

sub scanfile($);

sub scanfile($) {
  my $file = shift;
  my %todo = ();

  if ($fileseen{$file}) { return ; }
  $fileseen{$file} = 1;
  if (open(FH, $file)) {
    #print "$file ok\n";
  } else {
    my $f =`find  /usr/include/ -type f | grep $file`;
    chomp $f;
    $file = $f;
    -r "$file" || return;
    if (open(FH, $file)) {
      # ok
    } else {
      #print(" File: $file, open failure\n");
      #`echo File: $file, open failure > /dev/tty`;
      return;
    }
  }
  #print " File: $file\n";

  while (<FH>) {
    if (m/^\#[ \t]*include[ \t]+<([^>]*)>/) {
      $todo{$1} = 1;
    } elsif (m/^\#[ \t]*define[ \t]+(SIG[A-Z0-9]+)[ \t]+(\d+)/) {
      if ($1 ne "SIGSTKSZ") {
	#$errno[$2] = $1;
	my $name = $1;
	my $val  = $2;
	$sigs{$val} = $name;
      }
    }
  }

  for $file (keys %todo) {
    scanfile($file);
  }
}

chdir "/usr/include";
for my $file (@ARGV) {
  scanfile($file);
}


my @num = sort { $a <=> $b } keys %sigs;
my $nsig = @num;

print "
#ifndef NSIG
#define NSIG $nsig
#endif

";

print "struct intstr signames[] = {\n";
for my $k (@num) {
    my $v = $sigs{$k};
    print "  INTSTR($v),\t/* $k */\n";
}
print "};\n";
