X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=utils%2Fh2ph.PL;h=e501067ff8a4c6f9ff05ab52bff0998480013fe5;hb=0e885527258c31e904926227adc84e71c6d0d687;hp=094a2750158363cdd9a8b3509f3a8abb27e64913;hpb=25146a1a5f206eb3696426eb0e4ae3d07b07ee6b;p=p5sagit%2Fp5-mst-13.2.git diff --git a/utils/h2ph.PL b/utils/h2ph.PL index 094a275..e501067 100644 --- a/utils/h2ph.PL +++ b/utils/h2ph.PL @@ -42,8 +42,13 @@ use Config; use File::Path qw(mkpath); use Getopt::Std; -getopts('Dd:rlhaQ'); -use vars qw($opt_D $opt_d $opt_r $opt_l $opt_h $opt_a $opt_Q); +# Make sure read permissions for all are set: +if (defined umask && (umask() & 0444)) { + umask (umask() & ~0444); +} + +getopts('Dd:rlhaQe'); +use vars qw($opt_D $opt_d $opt_r $opt_l $opt_h $opt_a $opt_Q $opt_e); die "-r and -a options are mutually exclusive\n" if ($opt_r and $opt_a); my @inc_dirs = inc_dirs() if $opt_a; @@ -65,13 +70,21 @@ my %isatype; @isatype{@isatype} = (1) x @isatype; my $inif = 0; my %Is_converted; +my %bad_file = (); @ARGV = ('-') unless @ARGV; build_preamble_if_necessary(); +sub reindent($) { + my($text) = shift; + $text =~ s/\n/\n /g; + $text =~ s/ /\t/g; + $text; +} + my ($t, $tab, %curargs, $new, $eval_index, $dir, $name, $args, $outfile); -my ($incl, $next); +my ($incl, $incl_type, $next); while (defined (my $file = next_file())) { if (-l $file and -d $file) { link_if_possible($file) if ($opt_l); @@ -107,7 +120,9 @@ while (defined (my $file = next_file())) { open(OUT,">$Dest_dir/$outfile") || die "Can't create $outfile: $!\n"; } - print OUT "require '_h2ph_pre.ph';\n\n"; + print OUT + "require '_h2ph_pre.ph';\n\n", + "no warnings 'redefine';\n\n"; while (defined (local $_ = next_line($file))) { if (s/^\s*\#\s*//) { @@ -118,9 +133,9 @@ while (defined (my $file = next_file())) { s/\(\w+\s*\(\*\)\s*\(\w*\)\)\s*(-?\d+)/$1/; # (int (*)(foo_t))0 if (s/^\(([\w,\s]*)\)//) { $args = $1; - my $proto = '() '; + my $proto = '() '; if ($args ne '') { - $proto = ''; + $proto = ''; foreach my $arg (split(/,\s*/,$args)) { $arg =~ s/^\s*([^\s].*[^\s])\s*$/$1/; $curargs{$arg} = 1; @@ -131,6 +146,7 @@ while (defined (my $file = next_file())) { s/^\s+//; expr(); $new =~ s/(["\\])/\\$1/g; #"]); + EMIT: $new = reindent($new); $args = reindent($args); if ($t ne '') { @@ -169,22 +185,31 @@ while (defined (my $file = next_file())) { print OUT $t,"unless(defined(\&$name)) {\n sub $name () {\t",$new,";}\n}\n"; } } - } elsif (/^(include|import)\s*[<"](.*)[>"]/) { - ($incl = $2) =~ s/\.h$/.ph/; - print OUT $t,"require '$incl';\n"; - } elsif(/^include_next\s*[<"](.*)[>"]/) { - ($incl = $1) =~ s/\.h$/.ph/; + } elsif (/^(include|import|include_next)\s*[<\"](.*)[>\"]/) { + $incl_type = $1; + $incl = $2; + if (($incl_type eq 'include_next') || + ($opt_e && exists($bad_file{$incl}))) { + $incl =~ s/\.h$/.ph/; print OUT ($t, "eval {\n"); $tab += 4; $t = "\t" x ($tab / 8) . ' ' x ($tab % 8); + print OUT ($t, "my(\@REM);\n"); + if ($incl_type eq 'include_next') { print OUT ($t, "my(\%INCD) = map { \$INC{\$_} => 1 } ", - "(grep { \$_ eq \"$incl\" } keys(\%INC));\n"); + "(grep { \$_ eq \"$incl\" } ", + "keys(\%INC));\n"); print OUT ($t, - "my(\@REM) = map { \"\$_/$incl\" } ", + "\@REM = map { \"\$_/$incl\" } ", "(grep { not exists(\$INCD{\"\$_/$incl\"})", - "and -f \"\$_/$incl\" } \@INC);\n"); + " and -f \"\$_/$incl\" } \@INC);\n"); + } else { + print OUT ($t, + "\@REM = map { \"\$_/$incl\" } ", + "(grep {-r \"\$_/$incl\" } \@INC);\n"); + } print OUT ($t, "require \"\$REM[0]\" if \@REM;\n"); $tab -= 4; @@ -193,6 +218,10 @@ while (defined (my $file = next_file())) { "};\n"); print OUT ($t, "warn(\$\@) if \$\@;\n"); + } else { + $incl =~ s/\.h$/.ph/; + print OUT $t,"require '$incl';\n"; + } } elsif (/^ifdef\s+(\w+)/) { print OUT $t,"if(defined(&$1)) {\n"; $tab += 4; @@ -240,7 +269,7 @@ while (defined (my $file = next_file())) { } elsif(/^ident\s+(.*)/) { print OUT $t, "# $1\n"; } - } elsif(/^\s*(typedef\s*)?enum\s*(\s+[a-zA-Z_]\w*\s*)?/) { + } elsif (/^\s*(typedef\s*)?enum\s*(\s+[a-zA-Z_]\w*\s*)?/) { # { for vi until(/\{[^}]*\}.*;/ || /;/) { last unless defined ($next = next_line($file)); chomp $next; @@ -272,24 +301,86 @@ while (defined (my $file = next_file())) { "unless defined(\&$enum_name);\n"); } } + } elsif (/^(?:__extension__\s+)?(?:extern|static)\s+(?:__)?inline(?:__)?\s+/) { # { for vi + # This is a hack to parse the inline functions in the glibc headers. + # Warning: massive kludge ahead. We suppose inline functions are mainly + # constructed like macros. + while (1) { + last unless defined ($next = next_line($file)); + chomp $next; + undef $_, last if $next =~ /__THROW\s*;/; + $_ .= " $next"; + print OUT "# $next\n" if $opt_D; + last if $next =~ /^}|^{.*}\s*$/; + } + next if not defined; # because it's only a prototype + s/\b(__extension__|extern|static|(?:__)?inline(?:__)?)\b//g; + # violently drop #ifdefs + s/#\s*if.*?#\s*endif//g + and print OUT "# some #ifdef were dropped here -- fill in the blanks\n"; + if (s/^(?:\w|\s|\*)*\s(\w+)\s*//) { + $name = $1; + } else { + warn "name not found"; next; # shouldn't occur... + } + my @args; + if (s/^\(([^()]*)\)\s*(\w+\s*)*//) { + for my $arg (split /,/, $1) { + if ($arg =~ /(\w+)\s*$/) { + $curargs{$1} = 1; + push @args, $1; + } + } + } + $args = ( + @args + ? "local(" . (join ',', map "\$$_", @args) . ") = \@_;\n$t " + : "" + ); + my $proto = @args ? '' : '() '; + $new = ''; + s/\breturn\b//g; # "return" doesn't occur in macros usually... + expr(); + # try to find and perlify local C variables + our @local_variables = (); # needs to be a our(): (?{...}) bug workaround + $new =~ s[' + (?:(?:un)?signed\s+)? + (?:long\s+)? + (?:long|int|float|double|char|\w+_t)\s+ + (\w+) + (?{ push @local_variables, $1 }) + '] + [my \$$1]gx; + $new =~ s[' + (?:(?:un)?signed\s+)? + (?:long\s+)? + (?:long|int|float|double|char|\w+_t) + ' \s+ &(\w+) \s* ; + (?{ push @local_variables, $1 }) + ] + [my \$$1;]gx; + $new =~ s/&$_\b/\$$_/g for @local_variables; + $new =~ s/(["\\])/\\$1/g; #"]); + # now that's almost like a macro (we hope) + goto EMIT; } } - print OUT "1;\n"; - $Is_converted{$file} = 1; - queue_includes_from($file) if ($opt_a); + if ($opt_e && exists($bad_file{$file})) { + unlink($Dest_dir . '/' . $outfile); + $next = ''; + } else { + print OUT "1;\n"; + queue_includes_from($file) if $opt_a; + } } -exit $Exit; - - -sub reindent($) { - my($text) = shift; - $text =~ s/\n/\n /g; - $text =~ s/ /\t/g; - $text; +if ($opt_e && (scalar(keys %bad_file) > 0)) { + warn "Was unable to convert the following files:\n"; + warn "\t" . join("\n\t",sort(keys %bad_file)) . "\n"; } +exit $Exit; sub expr { my $joined_args; @@ -352,10 +443,16 @@ sub expr { }; # Eliminate typedefs /\(([\w\s]+)[\*\s]*\)\s*[\w\(]/ && do { + my $doit = 1; foreach (split /\s+/, $1) { # Make sure all the words are types, - last unless ($isatype{$_} or $_ eq 'struct' or $_ eq 'union'); + unless($isatype{$_} or $_ eq 'struct' or $_ eq 'union'){ + $doit = 0; + last; + } + } + if( $doit ){ + s/\([\w\s]+[\*\s]*\)// && next; # then eliminate them. } - s/\([\w\s]+[\*\s]*\)// && next; # then eliminate them. }; # struct/union member, including arrays: s/^([_A-Z]\w*(\[[^\]]+\])?((\.|->)[_A-Z]\w*(\[[^\]]+\])?)+)//i && do { @@ -449,7 +546,7 @@ sub next_line } next READ; } - if ($in =~ /^extern inline / && + if ($in =~ /^extern inline / && # Inlined assembler. $^O eq 'linux' && $file =~ m!(?:^|/)asm/[^/]+\.h$!) { while () { last if /^}/; @@ -463,10 +560,18 @@ sub next_line $out .= $1; } elsif ($in =~ s/^(\\.)//) { # \... $out .= $1; - } elsif ($in =~ s/^('(\\.|[^'\\])*')//) { # '... - $out .= $1; - } elsif ($in =~ s/^("(\\.|[^"\\])*")//) { # "... - $out .= $1; + } elsif ($in =~ /^'/) { # '... + if ($in =~ s/^('(\\.|[^'\\])*')//) { + $out .= $1; + } else { + next READ; + } + } elsif ($in =~ /^"/) { # "... + if ($in =~ s/^("(\\.|[^"\\])*")//) { + $out .= $1; + } else { + next READ; + } } elsif ($in =~ s/^\/\/.*//) { # //... # fall through } elsif ($in =~ m/^\/\*/) { # /*... @@ -485,7 +590,15 @@ sub next_line $in =~ s!\'T KNOW!!) { $out =~ s!I DON$!I_DO_NOT_KNOW!; } else { + if ($opt_e) { + warn "Cannot parse $file:\n$in\n"; + $bad_file{$file} = 1; + $in = ''; + $out = undef; + last READ; + } else { die "Cannot parse:\n$in\n"; + } } }