4 use File::Basename qw(basename dirname);
7 # List explicitly here the variables you want Configure to
8 # generate. Metaconfig only looks for shell variables, so you
9 # have to mention them as if they were shell variables, not
10 # %Config entries. Thus you write
12 # to ensure Configure will look for $Config{startperl}.
15 # This forces PL files to create target in same directory as PL file.
16 # This is so that make depend always knows where to find PL derivatives.
19 $file = basename($0, '.PL');
20 $file .= '.com' if $^O eq 'VMS';
22 open OUT,">$file" or die "Can't create $file: $!";
24 print "Extracting $file (with variable substitutions)\n";
26 # In this section, perl variables will be expanded during extraction.
27 # You can use $Config{...} to use Configure variables.
29 print OUT <<"!GROK!THIS!";
31 eval 'exec $Config{perlpath} -S \$0 \${1+"\$@"}'
32 if \$running_under_some_shell;
35 # In the following, perl variables are not expanded during extraction.
37 print OUT <<'!NO!SUBS!';
42 use File::Path qw(mkpath);
46 use vars qw($opt_D $opt_d $opt_r $opt_l $opt_h $opt_a $opt_Q);
47 die "-r and -a options are mutually exclusive\n" if ($opt_r and $opt_a);
48 my @inc_dirs = inc_dirs() if $opt_a;
52 my $Dest_dir = $opt_d || $Config{installsitearch};
53 die "Destination directory $Dest_dir doesn't exist or isn't a directory\n"
56 my @isatype = split(' ',<<END);
65 @isatype{@isatype} = (1) x @isatype;
69 @ARGV = ('-') unless @ARGV;
71 build_preamble_if_necessary();
73 my ($t, $tab, %curargs, $new, $eval_index, $dir, $name, $args, $outfile);
75 while (defined (my $file = next_file())) {
76 if (-l $file and -d $file) {
77 link_if_possible($file) if ($opt_l);
81 # Recover from header files with unbalanced cpp directives
85 # $eval_index goes into ``#line'' directives, to help locate syntax errors:
92 ($outfile = $file) =~ s/\.h$/.ph/ || next;
93 print "$file -> $outfile\n" unless $opt_Q;
94 if ($file =~ m|^(.*)/|) {
96 mkpath "$Dest_dir/$dir";
99 if ($opt_a) { # automagic mode: locate header file in @inc_dirs
100 foreach (@inc_dirs) {
106 open(IN,"$file") || (($Exit = 1),(warn "Can't open $file: $!\n"),next);
107 open(OUT,">$Dest_dir/$outfile") || die "Can't create $outfile: $!\n";
110 print OUT "require '_h2ph_pre.ph';\n\n";
112 while (defined (local $_ = next_line($file))) {
114 if (s/^define\s+(\w+)//) {
118 s/\(\w+\s*\(\*\)\s*\(\w*\)\)\s*(-?\d+)/$1/; # (int (*)(foo_t))0
119 if (s/^\(([\w,\s]*)\)//) {
124 foreach my $arg (split(/,\s*/,$args)) {
125 $arg =~ s/^\s*([^\s].*[^\s])\s*$/$1/;
128 $args =~ s/\b(\w)/\$$1/g;
129 $args = "local($args) = \@_;\n$t ";
133 $new =~ s/(["\\])/\\$1/g; #"]);
134 $new = reindent($new);
135 $args = reindent($args);
137 $new =~ s/(['\\])/\\$1/g; #']);
140 "eval \"\\n#line $eval_index $outfile\\n\" . 'sub $name $proto\{\n$t ${args}eval q($new);\n$t}' unless defined(\&$name);\n";
144 "eval 'sub $name $proto\{\n$t ${args}eval q($new);\n$t}' unless defined(\&$name);\n";
147 print OUT "unless(defined(\&$name)) {\n sub $name $proto\{\n\t${args}eval q($new);\n }\n}\n";
153 $new = 1 if $new eq '';
154 $new = reindent($new);
155 $args = reindent($args);
157 $new =~ s/(['\\])/\\$1/g; #']);
160 print OUT $t,"eval \"\\n#line $eval_index $outfile\\n\" . 'sub $name () {",$new,";}' unless defined(\&$name);\n";
163 print OUT $t,"eval 'sub $name () {",$new,";}' unless defined(\&$name);\n";
166 # Shunt around such directives as `#define FOO FOO':
167 next if " \&$name" eq $new;
169 print OUT $t,"unless(defined(\&$name)) {\n sub $name () {\t",$new,";}\n}\n";
172 } elsif (/^(include|import)\s*[<"](.*)[>"]/) {
173 ($incl = $2) =~ s/\.h$/.ph/;
174 print OUT $t,"require '$incl';\n";
175 } elsif(/^include_next\s*[<"](.*)[>"]/) {
176 ($incl = $1) =~ s/\.h$/.ph/;
180 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
182 "my(\%INCD) = map { \$INC{\$_} => 1 } ",
183 "(grep { \$_ eq \"$incl\" } keys(\%INC));\n");
185 "my(\@REM) = map { \"\$_/$incl\" } ",
186 "(grep { not exists(\$INCD{\"\$_/$incl\"})",
187 "and -f \"\$_/$incl\" } \@INC);\n");
189 "require \"\$REM[0]\" if \@REM;\n");
191 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
195 "warn(\$\@) if \$\@;\n");
196 } elsif (/^ifdef\s+(\w+)/) {
197 print OUT $t,"if(defined(&$1)) {\n";
199 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
200 } elsif (/^ifndef\s+(\w+)/) {
201 print OUT $t,"unless(defined(&$1)) {\n";
203 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
204 } elsif (s/^if\s+//) {
209 print OUT $t,"if($new) {\n";
211 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
212 } elsif (s/^elif\s+//) {
218 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
219 print OUT $t,"}\n elsif($new) {\n";
221 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
224 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
225 print OUT $t,"} else {\n";
227 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
230 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
232 } elsif(/^undef\s+(\w+)/) {
233 print OUT $t, "undef(&$1) if defined(&$1);\n";
234 } elsif(/^error\s+(".*")/) {
235 print OUT $t, "die($1);\n";
236 } elsif(/^error\s+(.*)/) {
237 print OUT $t, "die(\"", quotemeta($1), "\");\n";
238 } elsif(/^warning\s+(.*)/) {
239 print OUT $t, "warn(\"", quotemeta($1), "\");\n";
240 } elsif(/^ident\s+(.*)/) {
241 print OUT $t, "# $1\n";
243 } elsif(/^\s*(typedef\s*)?enum\s*(\s+[a-zA-Z_]\w*\s*)?/) {
244 until(/\{[^}]*\}.*;/ || /;/) {
245 last unless defined ($next = next_line($file));
247 # drop "#define FOO FOO" in enums
248 $next =~ s/^\s*#\s*define\s+(\w+)\s+\1\s*$//;
250 print OUT "# $next\n" if $opt_D;
252 s/#\s*if.*?#\s*endif//g; # drop #ifdefs
255 next unless /^\s?(typedef\s?)?enum\s?([a-zA-Z_]\w*)?\s?\{(.*)\}\s?([a-zA-Z_]\w*)?\s?;/;
256 (my $enum_subs = $3) =~ s/\s//g;
257 my @enum_subs = split(/,/, $enum_subs);
259 foreach my $enum (@enum_subs) {
260 my ($enum_name, $enum_value) = $enum =~ /^([a-zA-Z_]\w*)(=.+)?$/;
261 $enum_value =~ s/^=//;
262 $enum_val = (length($enum_value) ? $enum_value : $enum_val + 1);
265 "eval(\"\\n#line $eval_index $outfile\\n",
266 "sub $enum_name () \{ $enum_val; \}\") ",
267 "unless defined(\&$enum_name);\n");
271 "eval(\"sub $enum_name () \{ $enum_val; \}\") ",
272 "unless defined(\&$enum_name);\n");
279 $Is_converted{$file} = 1;
280 queue_includes_from($file) if ($opt_a);
297 $joined_args = join('|', keys(%curargs));
300 s/^\&\&// && do { $new .= " &&"; next;}; # handle && operator
301 s/^\&([\(a-z\)]+)/$1/i; # hack for things that take the address of
302 s/^(\s+)// && do {$new .= ' '; next;};
303 s/^0X([0-9A-F]+)[UL]*//i
306 if (length $hex > 8 && !$Config{use64bitint}) {
307 # Croak if nv_preserves_uv_bits < 64 ?
308 $new .= hex(substr($hex, -8)) +
309 2**32 * hex(substr($hex, 0, -8));
310 # The above will produce "errorneus" code
311 # if the hex constant was e.g. inside UINT64_C
312 # macro, but then again, h2ph is an approximation.
314 $new .= lc("0x$hex");
317 s/^(-?\d+\.\d+E[-+]?\d+)[FL]?//i && do {$new .= $1; next;};
318 s/^(\d+)\s*[LU]*//i && do {$new .= $1; next;};
319 s/^("(\\"|[^"])*")// && do {$new .= $1; next;};
320 s/^'((\\"|[^"])*)'// && do {
322 $new .= "ord('\$$1')";
328 # replace "sizeof(foo)" with "{foo}"
329 # also, remove * (C dereference operator) to avoid perl syntax
330 # problems. Where the %sizeof array comes from is anyone's
331 # guess (c2ph?), but this at least avoids fatal syntax errors.
332 # Behavior is undefined if sizeof() delimiters are unbalanced.
333 # This code was modified to able to handle constructs like this:
334 # sizeof(*(p)), which appear in the HP-UX 10.01 header files.
335 s/^sizeof\s*\(// && do {
337 my $lvl = 1; # already saw one open paren
338 # tack { on the front, and skip it in the loop
341 # find balanced closing paren
342 while ($index <= length($_) && $lvl > 0) {
343 $lvl++ if substr($_, $index, 1) eq "(";
344 $lvl-- if substr($_, $index, 1) eq ")";
347 # tack } on the end, replacing )
348 substr($_, $index - 1, 1) = "}";
349 # remove pesky * operators within the sizeof argument
350 substr($_, 0, $index - 1) =~ s/\*//g;
354 /\(([\w\s]+)[\*\s]*\)\s*[\w\(]/ && do {
355 foreach (split /\s+/, $1) { # Make sure all the words are types,
356 last unless ($isatype{$_} or $_ eq 'struct' or $_ eq 'union');
358 s/\([\w\s]+[\*\s]*\)// && next; # then eliminate them.
360 # struct/union member, including arrays:
361 s/^([_A-Z]\w*(\[[^\]]+\])?((\.|->)[_A-Z]\w*(\[[^\]]+\])?)+)//i && do {
363 $id =~ s/(\.|(->))([^\.\-]*)/->\{$3\}/g;
364 $id =~ s/\b([^\$])($joined_args)/$1\$$2/g if length($joined_args);
365 while($id =~ /\[\s*([^\$\&\d\]]+)\]/) {
368 if(exists($curargs{$index})) {
373 $id =~ s/\[\s*([^\$\&\d\]]+)\]/[$index]/;
377 s/^([_a-zA-Z]\w*)// && do {
379 if ($id eq 'struct' || $id eq 'union') {
383 } elsif ($id =~ /^((un)?signed)|(long)|(short)$/) {
384 while (s/^\s+(\w+)//) { $id .= ' ' . $1; }
389 $new .= '->' if /^[\[\{]/;
390 } elsif ($id eq 'defined') {
393 s/^\s*\((\w),/("$1",/ if $id =~ /^_IO[WR]*$/i; # cheat
395 } elsif ($isatype{$id}) {
396 if ($new =~ /{\s*$/) {
398 } elsif ($new =~ /\(\s*$/ && /^[\s*]*\)/) {
402 $new .= q(').$id.q(');
405 if ($inif && $new !~ /defined\s*\($/) {
406 $new .= '(defined(&' . $id . ') ? &' . $id . ' : 0)';
415 s/^(.)// && do { if ($1 ne '#') { $new .= $1; } next;};
424 my $pre_sub_tri_graphs = 1;
426 READ: while (not eof IN) {
429 next unless length $in;
432 if ($pre_sub_tri_graphs) {
433 # Preprocess all tri-graphs
434 # including things stuck in quoted string constants.
435 $in =~ s/\?\?=/#/g; # | ??=| #|
436 $in =~ s/\?\?\!/|/g; # | ??!| ||
437 $in =~ s/\?\?'/^/g; # | ??'| ^|
438 $in =~ s/\?\?\(/[/g; # | ??(| [|
439 $in =~ s/\?\?\)/]/g; # | ??)| ]|
440 $in =~ s/\?\?\-/~/g; # | ??-| ~|
441 $in =~ s/\?\?\//\\/g; # | ??/| \|
442 $in =~ s/\?\?</{/g; # | ??<| {|
443 $in =~ s/\?\?>/}/g; # | ??>| }|
445 if ($in =~ /^\#ifdef __LANGUAGE_PASCAL__/) {
446 # Tru64 disassembler.h evilness: mixed C and Pascal.
452 if ($in =~ /^extern inline / && # Inlined assembler.
453 $^O eq 'linux' && $file =~ m!(?:^|/)asm/[^/]+\.h$!) {
459 if ($in =~ s/\\$//) { # \-newline
462 } elsif ($in =~ s/^([^"'\\\/]+)//) { # Passthrough
464 } elsif ($in =~ s/^(\\.)//) { # \...
466 } elsif ($in =~ s/^('(\\.|[^'\\])*')//) { # '...
468 } elsif ($in =~ s/^("(\\.|[^"\\])*")//) { # "...
470 } elsif ($in =~ s/^\/\/.*//) { # //...
472 } elsif ($in =~ m/^\/\*/) { # /*...
473 # C comment removal adapted from perlfaq6:
474 if ($in =~ s/^\/\*[^*]*\*+([^\/*][^*]*\*+)*\///) {
476 } else { # Incomplete /* */
479 } elsif ($in =~ s/^(\/)//) { # /...
481 } elsif ($in =~ s/^([^\'\"\\\/]+)//) {
483 } elsif ($^O eq 'linux' &&
484 $file =~ m!(?:^|/)linux/byteorder/pdp_endian\.h$! &&
485 $in =~ s!\'T KNOW!!) {
486 $out =~ s!I DON$!I_DO_NOT_KNOW!;
488 die "Cannot parse:\n$in\n";
492 last READ if $out =~ /\S/;
499 # Handle recursive subdirectories without getting a grotesquely big stack.
500 # Could this be implemented using File::Find?
508 if ($file eq '-' or -f $file or -l $file) {
514 print STDERR "Skipping directory `$file'\n";
519 print STDERR "Skipping `$file': not a file or directory\n";
527 # Put all the files in $directory into @ARGV for processing.
530 my ($directory) = @_;
532 $directory =~ s:/$::;
534 opendir DIR, $directory;
535 foreach (readdir DIR) {
536 next if ($_ eq '.' or $_ eq '..');
538 # expand_glob() is going to be called until $ARGV[0] isn't a
539 # directory; so push directories, and unshift everything else.
540 if (-d "$directory/$_") { push @ARGV, "$directory/$_" }
541 else { unshift @ARGV, "$directory/$_" }
547 # Given $file, a symbolic link to a directory in the C include directory,
548 # make an equivalent symbolic link in $Dest_dir, if we can figure out how.
549 # Otherwise, just duplicate the file or directory.
553 my $target = eval 'readlink($dirlink)';
555 if ($target =~ m:^\.\./: or $target =~ m:^/:) {
556 # The target of a parent or absolute link could leave the $Dest_dir
557 # hierarchy, so let's put all of the contents of $dirlink (actually,
558 # the contents of $target) into @ARGV; as a side effect down the
559 # line, $dirlink will get created as an _actual_ directory.
560 expand_glob($dirlink);
562 if (-l "$Dest_dir/$dirlink") {
563 unlink "$Dest_dir/$dirlink" or
564 print STDERR "Could not remove link $Dest_dir/$dirlink: $!\n";
567 if (eval 'symlink($target, "$Dest_dir/$dirlink")') {
568 print "Linking $target -> $Dest_dir/$dirlink\n";
570 # Make sure that the link _links_ to something:
571 if (! -e "$Dest_dir/$target") {
572 mkpath("$Dest_dir/$target", 0755) or
573 print STDERR "Could not create $Dest_dir/$target/\n";
576 print STDERR "Could not symlink $target -> $Dest_dir/$dirlink: $!\n";
582 # Push all #included files in $file onto our stack, except for STDIN
583 # and files we've already processed.
584 sub queue_includes_from
589 return if ($file eq "-");
591 open HEADER, $file or return;
592 while (defined($line = <HEADER>)) {
593 while (/\\$/) { # Handle continuation lines
598 if ($line =~ /^#\s*include\s+<(.*?)>/) {
599 push(@ARGV, $1) unless $Is_converted{$1};
606 # Determine include directories; $Config{usrinc} should be enough for (all
607 # non-GCC?) C compilers, but gcc uses an additional include directory.
610 my $from_gcc = `$Config{cc} -v 2>&1`;
611 $from_gcc =~ s:^Reading specs from (.*?)/specs\b.*:$1/include:s;
613 length($from_gcc) ? ($from_gcc, $Config{usrinc}) : ($Config{usrinc});
617 # Create "_h2ph_pre.ph", if it doesn't exist or was built by a different
619 sub build_preamble_if_necessary
621 # Increment $VERSION every time this function is modified:
623 my $preamble = "$Dest_dir/_h2ph_pre.ph";
625 # Can we skip building the preamble file?
627 # Extract version number from first line of preamble:
628 open PREAMBLE, $preamble or die "Cannot open $preamble: $!";
629 my $line = <PREAMBLE>;
630 $line =~ /(\b\d+\b)/;
631 close PREAMBLE or die "Cannot close $preamble: $!";
633 # Don't build preamble if a compatible preamble exists:
634 return if $1 == $VERSION;
637 my (%define) = _extract_cc_defines();
639 open PREAMBLE, ">$preamble" or die "Cannot open $preamble: $!";
640 print PREAMBLE "# This file was created by h2ph version $VERSION\n";
642 foreach (sort keys %define) {
644 print PREAMBLE "# $_=$define{$_}\n";
647 if ($define{$_} =~ /^(\d+)U?L{0,2}$/i) {
649 "unless (defined &$_) { sub $_() { $1 } }\n\n";
650 } elsif ($define{$_} =~ /^\w+$/) {
652 "unless (defined &$_) { sub $_() { &$define{$_} } }\n\n";
655 "unless (defined &$_) { sub $_() { \"",
656 quotemeta($define{$_}), "\" } }\n\n";
659 close PREAMBLE or die "Cannot close $preamble: $!";
663 # %Config contains information on macros that are pre-defined by the
664 # system's compiler. We need this information to make the .ph files
665 # function with perl as the .h files do with cc.
666 sub _extract_cc_defines
669 my $allsymbols = join " ",
670 @Config{'ccsymbols', 'cppsymbols', 'cppccsymbols'};
672 # Split compiler pre-definitions into `key=value' pairs:
673 foreach (split /\s+/, $allsymbols) {
674 /(.+?)=(.+)/ and $define{$1} = $2;
677 print STDERR "$_: $1 -> $2\n";
687 ##############################################################################
692 h2ph - convert .h C header files to .ph Perl header files
696 B<h2ph [-d destination directory] [-r | -a] [-l] [headerfiles]>
701 converts any C header files specified to the corresponding Perl header file
703 It is most easily run while in /usr/include:
705 cd /usr/include; h2ph * sys/*
709 cd /usr/include; h2ph * sys/* arpa/* netinet/*
713 cd /usr/include; h2ph -r -l .
715 The output files are placed in the hierarchy rooted at Perl's
716 architecture dependent library directory. You can specify a different
717 hierarchy with a B<-d> switch.
719 If run with no arguments, filters standard input to standard output.
725 =item -d destination_dir
727 Put the resulting B<.ph> files beneath B<destination_dir>, instead of
728 beneath the default Perl library location (C<$Config{'installsitsearch'}>).
732 Run recursively; if any of B<headerfiles> are directories, then run I<h2ph>
733 on all files in those directories (and their subdirectories, etc.). B<-r>
734 and B<-a> are mutually exclusive.
738 Run automagically; convert B<headerfiles>, as well as any B<.h> files
739 which they include. This option will search for B<.h> files in all
740 directories which your C compiler ordinarily uses. B<-a> and B<-r> are
745 Symbolic links will be replicated in the destination directory. If B<-l>
746 is not specified, then links are skipped over.
750 Put ``hints'' in the .ph files which will help in locating problems with
751 I<h2ph>. In those cases when you B<require> a B<.ph> file containing syntax
752 errors, instead of the cryptic
754 [ some error condition ] at (eval mmm) line nnn
756 you will see the slightly more helpful
758 [ some error condition ] at filename.ph line nnn
760 However, the B<.ph> files almost double in size when built using B<-h>.
764 Include the code from the B<.h> file as a comment in the B<.ph> file.
765 This is primarily used for debugging I<h2ph>.
769 ``Quiet'' mode; don't print out the names of the files being converted.
775 No environment variables are used.
794 The usual warnings if it can't read or write the files involved.
798 Doesn't construct the %sizeof array for you.
800 It doesn't handle all C constructs, but it does attempt to isolate
801 definitions inside evals so that you can get at the definitions
802 that it can translate.
804 It's only intended as a rough tool.
805 You may need to dicker with the files produced.
807 You have to run this program by hand; it's not run as part of the Perl
810 Doesn't handle complicated expressions built piecemeal, a la:
820 Doesn't necessarily locate all of your C compiler's internally-defined
827 close OUT or die "Can't close $file: $!";
828 chmod 0755, $file or die "Can't reset permissions for $file: $!\n";
829 exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';