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!';
40 use File::Path qw(mkpath);
44 die "-r and -a options are mutually exclusive\n" if ($opt_r and $opt_a);
45 @inc_dirs = inc_dirs() if $opt_a;
49 my $Dest_dir = $opt_d || $Config{installsitearch};
50 die "Destination directory $Dest_dir doesn't exist or isn't a directory\n"
53 @isatype = split(' ',<<END);
61 @isatype{@isatype} = (1) x @isatype;
64 @ARGV = ('-') unless @ARGV;
66 while (defined ($file = next_file())) {
67 if (-l $file and -d $file) {
68 link_if_possible($file) if ($opt_l);
72 # Recover from header files with unbalanced cpp directives
76 # $eval_index goes into ``#line'' directives, to help locate syntax errors:
83 ($outfile = $file) =~ s/\.h$/.ph/ || next;
84 print "$file -> $outfile\n" unless $opt_Q;
85 if ($file =~ m|^(.*)/|) {
87 mkpath "$Dest_dir/$dir";
90 if ($opt_a) { # automagic mode: locate header file in @inc_dirs
97 open(IN,"$file") || (($Exit = 1),(warn "Can't open $file: $!\n"),next);
98 open(OUT,">$Dest_dir/$outfile") || die "Can't create $outfile: $!\n";
107 print OUT "# $_\n" if $opt_D;
110 s/\200[^\201]*\201//g; # delete single line comments
111 if (s/\200.*//) { # begin multi-line comment?
118 if (s/^define\s+(\w+)//) {
122 if (s/^\(([\w,\s]*)\)//) {
127 foreach $arg (split(/,\s*/,$args)) {
128 $arg =~ s/^\s*([^\s].*[^\s])\s*$/$1/;
131 $args =~ s/\b(\w)/\$$1/g;
132 $args = "local($args) = \@_;\n$t ";
136 $new =~ s/(["\\])/\\$1/g; #"]);
137 $new = reindent($new);
138 $args = reindent($args);
140 $new =~ s/(['\\])/\\$1/g; #']);
143 "eval \"\\n#line $eval_index $outfile\\n\" . 'sub $name $proto\{\n$t ${args}eval q($new);\n$t}' unless defined(\&$name);\n";
147 "eval 'sub $name $proto\{\n$t ${args}eval q($new);\n$t}' unless defined(\&$name);\n";
150 print OUT "unless(defined(\&$name)) {\n sub $name $proto\{\n\t${args}eval q($new);\n }\n}\n";
156 $new = 1 if $new eq '';
157 $new = reindent($new);
158 $args = reindent($args);
160 $new =~ s/(['\\])/\\$1/g; #']);
162 print OUT $t,"eval \"\\n#line $eval_index $outfile\\n\" . 'sub $name () {",$new,";}' unless defined(\&$name);\n";
165 print OUT $t,"eval 'sub $name () {",$new,";}' unless defined(\&$name);\n";
168 print OUT $t,"unless(defined(\&$name)) {\n sub $name () {\t",$new,";}\n}\n";
171 } elsif (/^(include|import)\s*[<"](.*)[>"]/) {
172 ($incl = $2) =~ s/\.h$/.ph/;
173 print OUT $t,"require '$incl';\n";
174 } elsif(/^include_next\s*[<"](.*)[>"]/) {
175 ($incl = $1) =~ s/\.h$/.ph/;
179 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
181 "my(\%INCD) = map { \$INC{\$_} => 1 } ",
182 "(grep { \$_ eq \"$incl\" } keys(\%INC));\n");
184 "my(\@REM) = map { \"\$_/$incl\" } ",
185 "(grep { not exists(\$INCD{\"\$_/$incl\"})",
186 "and -f \"\$_/$incl\" } \@INC);\n");
188 "require \"\$REM[0]\" if \@REM;\n");
190 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
194 "warn(\$\@) if \$\@;\n");
195 } elsif (/^ifdef\s+(\w+)/) {
196 print OUT $t,"if(defined(&$1)) {\n";
198 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
199 } elsif (/^ifndef\s+(\w+)/) {
200 print OUT $t,"unless(defined(&$1)) {\n";
202 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
203 } elsif (s/^if\s+//) {
208 print OUT $t,"if($new) {\n";
210 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
211 } elsif (s/^elif\s+//) {
217 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
218 print OUT $t,"}\n elsif($new) {\n";
220 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
223 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
224 print OUT $t,"} else {\n";
226 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
229 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
231 } elsif(/^undef\s+(\w+)/) {
232 print OUT $t, "undef(&$1) if defined(&$1);\n";
233 } elsif(/^error\s+(".*")/) {
234 print OUT $t, "die($1);\n";
235 } elsif(/^error\s+(.*)/) {
236 print OUT $t, "die(\"", quotemeta($1), "\");\n";
237 } elsif(/^warning\s+(.*)/) {
238 print OUT $t, "warn(\"", quotemeta($1), "\");\n";
239 } elsif(/^ident\s+(.*)/) {
240 print OUT $t, "# $1\n";
242 } elsif(/^\s*(typedef\s*)?enum\s*(\s+[a-zA-Z_]\w*\s*)?\{/) {
246 print OUT "# $next\n" if $opt_D;
250 /^\s?(typedef\s?)?enum\s?([a-zA-Z_]\w*)?\s?\{(.*)\}\s?([a-zA-Z_]\w*)?\s?;/;
251 ($enum_subs = $3) =~ s/\s//g;
252 @enum_subs = split(/,/, $enum_subs);
254 for $enum (@enum_subs) {
255 ($enum_name, $enum_value) = $enum =~ /^([a-zA-Z_]\w*)(=.+)?$/;
256 $enum_value =~ s/^=//;
257 $enum_val = (length($enum_value) ? $enum_value : $enum_val + 1);
260 "eval(\"\\n#line $eval_index $outfile\\n",
261 "sub $enum_name () \{ $enum_val; \}\") ",
262 "unless defined(\&$enum_name);\n");
266 "eval(\"sub $enum_name () \{ $enum_val; \}\") ",
267 "unless defined(\&$enum_name);\n");
274 $is_converted{$file} = 1;
275 queue_includes_from($file) if ($opt_a);
289 my($joined_args) = join('|', keys(%curargs));
292 s/^\&\&// && do { $new .= " &&"; next;}; # handle && operator
293 s/^\&([\(a-z\)]+)/$1/i; # hack for things that take the address of
294 s/^(\s+)// && do {$new .= ' '; next;};
295 s/^(0X[0-9A-F]+)[UL]*//i && do {$new .= lc($1); next;};
296 s/^(-?\d+\.\d+E[-+]\d+)F?//i && do {$new .= $1; next;};
297 s/^(\d+)\s*[LU]*//i && do {$new .= $1; next;};
298 s/^("(\\"|[^"])*")// && do {$new .= $1; next;};
299 s/^'((\\"|[^"])*)'// && do {
301 $new .= "ord('\$$1')";
307 # replace "sizeof(foo)" with "{foo}"
308 # also, remove * (C dereference operator) to avoid perl syntax
309 # problems. Where the %sizeof array comes from is anyone's
310 # guess (c2ph?), but this at least avoids fatal syntax errors.
311 # Behavior is undefined if sizeof() delimiters are unbalanced.
312 # This code was modified to able to handle constructs like this:
313 # sizeof(*(p)), which appear in the HP-UX 10.01 header files.
314 s/^sizeof\s*\(// && do {
316 my $lvl = 1; # already saw one open paren
317 # tack { on the front, and skip it in the loop
320 # find balanced closing paren
321 while ($index <= length($_) && $lvl > 0) {
322 $lvl++ if substr($_, $index, 1) eq "(";
323 $lvl-- if substr($_, $index, 1) eq ")";
326 # tack } on the end, replacing )
327 substr($_, $index - 1, 1) = "}";
328 # remove pesky * operators within the sizeof argument
329 substr($_, 0, $index - 1) =~ s/\*//g;
333 /\(([\w\s]+)[\*\s]*\)\s*[\w\(]/ && do {
334 foreach (split /\s+/, $1) { # Make sure all the words are types,
335 last unless ($isatype{$_} or $_ eq 'struct');
337 s/\([\w\s]+[\*\s]*\)// && next; # then eliminate them.
339 # struct/union member, including arrays:
340 s/^([_A-Z]\w*(\[[^\]]+\])?((\.|->)[_A-Z]\w*(\[[^\]]+\])?)+)//i && do {
342 $id =~ s/(\.|(->))([^\.\-]*)/->\{$3\}/g;
343 $id =~ s/\b([^\$])($joined_args)/$1\$$2/g if length($joined_args);
344 while($id =~ /\[\s*([^\$\&\d\]]+)\]/) {
347 if(exists($curargs{$index})) {
352 $id =~ s/\[\s*([^\$\&\d\]]+)\]/[$index]/;
356 s/^([_a-zA-Z]\w*)// && do {
358 if ($id eq 'struct') {
362 } elsif ($id =~ /^((un)?signed)|(long)|(short)$/) {
363 while (s/^\s+(\w+)//) { $id .= ' ' . $1; }
368 $new .= '->' if /^[\[\{]/;
369 } elsif ($id eq 'defined') {
372 s/^\((\w),/("$1",/ if $id =~ /^_IO[WR]*$/i; # cheat
374 } elsif ($isatype{$id}) {
375 if ($new =~ /{\s*$/) {
377 } elsif ($new =~ /\(\s*$/ && /^[\s*]*\)/) {
381 $new .= q(').$id.q(');
384 if ($inif && $new !~ /defined\s*\($/) {
385 $new .= '(defined(&' . $id . ') ? &' . $id . ' : 0)';
394 s/^(.)// && do { if ($1 ne '#') { $new .= $1; } next;};
399 # Handle recursive subdirectories without getting a grotesquely big stack.
400 # Could this be implemented using File::Find?
408 if ($file eq '-' or -f $file or -l $file) {
414 print STDERR "Skipping directory `$file'\n";
419 print STDERR "Skipping `$file': not a file or directory\n";
427 # Put all the files in $directory into @ARGV for processing.
430 my ($directory) = @_;
432 $directory =~ s:/$::;
434 opendir DIR, $directory;
435 foreach (readdir DIR) {
436 next if ($_ eq '.' or $_ eq '..');
438 # expand_glob() is going to be called until $ARGV[0] isn't a
439 # directory; so push directories, and unshift everything else.
440 if (-d "$directory/$_") { push @ARGV, "$directory/$_" }
441 else { unshift @ARGV, "$directory/$_" }
447 # Given $file, a symbolic link to a directory in the C include directory,
448 # make an equivalent symbolic link in $Dest_dir, if we can figure out how.
449 # Otherwise, just duplicate the file or directory.
453 my $target = eval 'readlink($dirlink)';
455 if ($target =~ m:^\.\./: or $target =~ m:^/:) {
456 # The target of a parent or absolute link could leave the $Dest_dir
457 # hierarchy, so let's put all of the contents of $dirlink (actually,
458 # the contents of $target) into @ARGV; as a side effect down the
459 # line, $dirlink will get created as an _actual_ directory.
460 expand_glob($dirlink);
462 if (-l "$Dest_dir/$dirlink") {
463 unlink "$Dest_dir/$dirlink" or
464 print STDERR "Could not remove link $Dest_dir/$dirlink: $!\n";
467 if (eval 'symlink($target, "$Dest_dir/$dirlink")') {
468 print "Linking $target -> $Dest_dir/$dirlink\n";
470 # Make sure that the link _links_ to something:
471 if (! -e "$Dest_dir/$target") {
472 mkpath("$Dest_dir/$target", 0755) or
473 print STDERR "Could not create $Dest_dir/$target/\n";
476 print STDERR "Could not symlink $target -> $Dest_dir/$dirlink: $!\n";
482 # Push all #included files in $file onto our stack, except for STDIN
483 # and files we've already processed.
484 sub queue_includes_from
489 return if ($file eq "-");
491 open HEADER, $file or return;
492 while (defined($line = <HEADER>)) {
493 while (/\\$/) { # Handle continuation lines
498 if ($line =~ /^#\s*include\s+<(.*?)>/) {
499 push(@ARGV, $1) unless $is_converted{$1};
506 # Determine include directories; $Config{usrinc} should be enough for (all
507 # non-GCC?) C compilers, but gcc uses an additional include directory.
510 my $from_gcc = `$Config{cc} -v 2>&1`;
511 $from_gcc =~ s:^Reading specs from (.*?)/specs\b.*:$1/include:s;
513 length($from_gcc) ? ($from_gcc, $Config{usrinc}) : ($Config{usrinc});
519 ##############################################################################
524 h2ph - convert .h C header files to .ph Perl header files
528 B<h2ph [-d destination directory] [-r | -a] [-l] [headerfiles]>
533 converts any C header files specified to the corresponding Perl header file
535 It is most easily run while in /usr/include:
537 cd /usr/include; h2ph * sys/*
541 cd /usr/include; h2ph -r -l .
543 The output files are placed in the hierarchy rooted at Perl's
544 architecture dependent library directory. You can specify a different
545 hierarchy with a B<-d> switch.
547 If run with no arguments, filters standard input to standard output.
553 =item -d destination_dir
555 Put the resulting B<.ph> files beneath B<destination_dir>, instead of
556 beneath the default Perl library location (C<$Config{'installsitsearch'}>).
560 Run recursively; if any of B<headerfiles> are directories, then run I<h2ph>
561 on all files in those directories (and their subdirectories, etc.). B<-r>
562 and B<-a> are mutually exclusive.
566 Run automagically; convert B<headerfiles>, as well as any B<.h> files
567 which they include. This option will search for B<.h> files in all
568 directories which your C compiler ordinarily uses. B<-a> and B<-r> are
573 Symbolic links will be replicated in the destination directory. If B<-l>
574 is not specified, then links are skipped over.
578 Put ``hints'' in the .ph files which will help in locating problems with
579 I<h2ph>. In those cases when you B<require> a B<.ph> file containing syntax
580 errors, instead of the cryptic
582 [ some error condition ] at (eval mmm) line nnn
584 you will see the slightly more helpful
586 [ some error condition ] at filename.ph line nnn
588 However, the B<.ph> files almost double in size when built using B<-h>.
592 Include the code from the B<.h> file as a comment in the B<.ph> file.
593 This is primarily used for debugging I<h2ph>.
599 No environment variables are used.
618 The usual warnings if it can't read or write the files involved.
622 Doesn't construct the %sizeof array for you.
624 It doesn't handle all C constructs, but it does attempt to isolate
625 definitions inside evals so that you can get at the definitions
626 that it can translate.
628 It's only intended as a rough tool.
629 You may need to dicker with the files produced.
635 close OUT or die "Can't close $file: $!";
636 chmod 0755, $file or die "Can't reset permissions for $file: $!\n";
637 exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';