This is my patch patch.1n for perl5.001.
[p5sagit/p5-mst-13.2.git] / h2ph.SH
diff --git a/h2ph.SH b/h2ph.SH
old mode 100644 (file)
new mode 100755 (executable)
index cac5ada..cb36ada
--- a/h2ph.SH
+++ b/h2ph.SH
@@ -5,8 +5,8 @@ case $CONFIG in
        ln ../../config.sh . || \
        ln ../../../config.sh . || \
        (echo "Can't find config.sh."; exit 1)
-    fi
-    . config.sh
+    fi 2>/dev/null
+    . ./config.sh
     ;;
 esac
 : This forces SH files to create target in same directory as SH file.
@@ -19,12 +19,14 @@ echo "Extracting h2ph (with variable substitutions)"
 : Move anything that needs config subs from !NO!SUBS! section to !GROK!THIS!.
 : Protect any dollar signs and backticks that you do not want interpreted
 : by putting a backslash in front.  You may delete these comments.
+rm -f h2ph
 $spitshell >h2ph <<!GROK!THIS!
-#!$bin/perl
-'di';
-'ig00';
+#!$binexp/perl
+'di ';
+'ds 00 \"';
+'ig 00 ';
 
-\$perlincl = '$privlib';
+\$perlincl = '$archlibexp';
 !GROK!THIS!
 
 : In the following dollars and backticks do not need the extra backslash.
@@ -32,19 +34,36 @@ $spitshell >>h2ph <<'!NO!SUBS!'
 
 chdir '/usr/include' || die "Can't cd /usr/include";
 
-%isatype = ('char',1,'short',1,'int',1,'long',1);
+@isatype = split(' ',<<END);
+       char    uchar   u_char
+       short   ushort  u_short
+       int     uint    u_int
+       long    ulong   u_long
+       FILE
+END
+
+@isatype{@isatype} = (1) x @isatype;
+$inif = 0;
+
+@ARGV = ('-') unless @ARGV;
 
 foreach $file (@ARGV) {
-    ($outfile = $file) =~ s/\.h$/.ph/;
-    print "$file -> $outfile\n";
-    if ($file =~ m|^(.*)/|) {
-       $dir = $1;
-       if (!-d "$perlincl/$dir") {
-           mkdir("$perlincl/$dir",0777);
+    if ($file eq '-') {
+       open(IN, "-");
+       open(OUT, ">-");
+    }
+    else {
+       ($outfile = $file) =~ s/\.h$/.ph/ || next;
+       print "$file -> $outfile\n";
+       if ($file =~ m|^(.*)/|) {
+           $dir = $1;
+           if (!-d "$perlincl/$dir") {
+               mkdir("$perlincl/$dir",0777);
+           }
        }
+       open(IN,"$file") || ((warn "Can't open $file: $!\n"),next);
+       open(OUT,">$perlincl/$outfile") || die "Can't create $outfile: $!\n";
     }
-    open(IN,"$file") || ((warn "Can't open $file: $!\n"),next);
-    open(OUT,">$perlincl/$outfile") || die "Can't create $outfile: $!\n";
     while (<IN>) {
        chop;
        while (/\\$/) {
@@ -70,6 +89,7 @@ foreach $file (@ARGV) {
                    $args = $1;
                    if ($args ne '') {
                        foreach $arg (split(/,\s*/,$args)) {
+                           $arg =~ s/^\s*([^\s].*[^\s])\s*$/$1/;
                            $curargs{$arg} = 1;
                        }
                        $args =~ s/\b(\w)/\$$1/g;
@@ -101,8 +121,9 @@ foreach $file (@ARGV) {
                    }
                }
            }
-           elsif (/^include <(.*)>/) {
-               print OUT $t,"do '$1' || die \"Can't include $1: \$!\";\n";
+           elsif (/^include\s+<(.*)>/) {
+               ($incl = $1) =~ s/\.h$/.ph/;
+               print OUT $t,"require '$incl';\n";
            }
            elsif (/^ifdef\s+(\w+)/) {
                print OUT $t,"if (defined &$1) {\n";
@@ -116,14 +137,18 @@ foreach $file (@ARGV) {
            }
            elsif (s/^if\s+//) {
                $new = '';
+               $inif = 1;
                do expr();
+               $inif = 0;
                print OUT $t,"if ($new) {\n";
                $tab += 4;
                $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
            }
            elsif (s/^elif\s+//) {
                $new = '';
+               $inif = 1;
                do expr();
+               $inif = 0;
                $tab -= 4;
                $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
                print OUT $t,"}\n${t}elsif ($new) {\n";
@@ -162,13 +187,22 @@ sub expr {
            }
            next;
        };
-       s/^(struct\s+\w+)//     && do {$new .= "'$1'"; next;};
        s/^sizeof\s*\(([^)]+)\)/{$1}/ && do {
            $new .= '$sizeof';
            next;
        };
        s/^([_a-zA-Z]\w*)//     && do {
            $id = $1;
+           if ($id eq 'struct') {
+               s/^\s+(\w+)//;
+               $id .= ' ' . $1;
+               $isatype{$id} = 1;
+           }
+           elsif ($id eq 'unsigned') {
+               s/^\s+(\w+)//;
+               $id .= ' ' . $1;
+               $isatype{$id} = 1;
+           }
            if ($curargs{$id}) {
                $new .= '$' . $id;
            }
@@ -176,14 +210,27 @@ sub expr {
                $new .= 'defined';
            }
            elsif (/^\(/) {
-               s/^\((\w),/("$1",/ if $id =~ /^_IO[WR]*$/;      # cheat
+               s/^\((\w),/("$1",/ if $id =~ /^_IO[WR]*$/i;     # cheat
                $new .= " &$id";
            }
            elsif ($isatype{$id}) {
-               $new .= "'$id'";
+               if ($new =~ /{\s*$/) {
+                   $new .= "'$id'";
+               }
+               elsif ($new =~ /\(\s*$/ && /^[\s*]*\)/) {
+                   $new =~ s/\(\s*$//;
+                   s/^[\s*]*\)//;
+               }
+               else {
+                   $new .= $id;
+               }
            }
            else {
-               $new .= ' &' . $id;
+               if ($inif && $new !~ /defined\($/) {
+                   $new .= '(defined(&' . $id . ') ? &' . $id . ' : 0)';
+               } else { 
+                   $new .= ' &' . $id;
+               }
            }
            next;
        };
@@ -194,8 +241,8 @@ sub expr {
 
        # These next few lines are legal in both Perl and nroff.
 
-.00;                   # finish .ig
+.00 ;                  # finish .ig
+
 'di                    \" finish diversion--previous line must be blank
 .nr nl 0-1             \" fake up transition to first page again
 .nr % 0                        \" start at page 1
@@ -216,6 +263,7 @@ It is most easily run while in /usr/include:
        cd /usr/include; h2ph * sys/*
 
 .fi
+If run with no arguments, filters standard input to standard output.
 .SH ENVIRONMENT
 No environment variables are used.
 .SH FILES