4 use File::Basename qw(basename dirname);
6 # List explicitly here the variables you want Configure to
7 # generate. Metaconfig only looks for shell variables, so you
8 # have to mention them as if they were shell variables, not
9 # %Config entries. Thus you write
11 # to ensure Configure will look for $Config{startperl}.
14 # This forces PL files to create target in same directory as PL file.
15 # This is so that make depend always knows where to find PL derivatives.
17 $file = basename($0, '.PL');
18 $file .= '.com' if $^O eq 'VMS';
20 open OUT,">$file" or die "Can't create $file: $!";
22 print "Extracting $file (with variable substitutions)\n";
24 # In this section, perl variables will be expanded during extraction.
25 # You can use $Config{...} to use Configure variables.
27 print OUT <<"!GROK!THIS!";
29 eval 'exec $Config{perlpath} -S \$0 \${1+"\$@"}'
30 if \$running_under_some_shell;
33 # In the following, perl variables are not expanded during extraction.
35 print OUT <<'!NO!SUBS!';
38 use File::Path qw(mkpath);
45 my $Dest_dir = $opt_d || $Config{installsitearch};
46 die "Destination directory $Dest_dir doesn't exist or isn't a directory\n"
49 @isatype = split(' ',<<END);
57 @isatype{@isatype} = (1) x @isatype;
60 @ARGV = ('-') unless @ARGV;
62 while (defined ($file = next_file())) {
63 if (-l $file and -d $file) {
64 link_if_possible($file) if ($opt_l);
68 # Recover from header files with unbalanced cpp directives
72 # $eval_index goes into ``#line'' directives, to help locate syntax errors:
79 ($outfile = $file) =~ s/\.h$/.ph/ || next;
80 print "$file -> $outfile\n";
81 if ($file =~ m|^(.*)/|) {
83 mkpath "$Dest_dir/$dir";
85 open(IN,"$file") || (($Exit = 1),(warn "Can't open $file: $!\n"),next);
86 open(OUT,">$Dest_dir/$outfile") || die "Can't create $outfile: $!\n";
95 print OUT "# $_\n" if $opt_D;
98 s/\200[^\201]*\201//g; # delete single line comments
99 if (s/\200.*//) { # begin multi-line comment?
106 if (s/^define\s+(\w+)//) {
110 if (s/^\(([\w,\s]*)\)//) {
115 foreach $arg (split(/,\s*/,$args)) {
116 $arg =~ s/^\s*([^\s].*[^\s])\s*$/$1/;
119 $args =~ s/\b(\w)/\$$1/g;
120 $args = "local($args) = \@_;\n$t ";
124 $new =~ s/(["\\])/\\$1/g; #"]);
125 $new = reindent($new);
126 $args = reindent($args);
128 $new =~ s/(['\\])/\\$1/g; #']);
131 "eval \"\\n#line $eval_index $outfile\\n\" . 'sub $name $proto\{\n$t ${args}eval q($new);\n$t}' unless defined(\&$name);\n";
135 "eval 'sub $name $proto\{\n$t ${args}eval q($new);\n$t}' unless defined(\&$name);\n";
138 print OUT "unless(defined(\&$name)) {\n sub $name $proto\{\n\t${args}eval q($new);\n }\n}\n";
144 $new = 1 if $new eq '';
145 $new = reindent($new);
146 $args = reindent($args);
148 $new =~ s/(['\\])/\\$1/g; #']);
150 print OUT $t,"eval \"\\n#line $eval_index $outfile\\n\" . 'sub $name () {",$new,";}' unless defined(\&$name);\n";
153 print OUT $t,"eval 'sub $name () {",$new,";}' unless defined(\&$name);\n";
156 print OUT $t,"unless(defined(\&$name)) {\n sub $name () {\t",$new,";}\n}\n";
159 } elsif (/^(include|import)\s*[<"](.*)[>"]/) {
160 ($incl = $2) =~ s/\.h$/.ph/;
161 print OUT $t,"require '$incl';\n";
162 } elsif(/^include_next\s*[<"](.*)[>"]/) {
163 ($incl = $1) =~ s/\.h$/.ph/;
164 # should've read up on #include_next properly before attempting
167 #print OUT $t, "{\n";
169 #$t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
170 #print OUT $t, "my(\$INC) = shift(\@INC);\n";
171 #print OUT $t, "require '$incl';\n";
172 #print OUT $t, "unshift(\@INC, \$INC);}\n";
174 #$t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
175 #print OUT $t, "}\n";
178 print OUT ($t, "my(\$i) = 0;\n");
179 print OUT ($t, "if(exists(\$INC{$incl})) {\n");
181 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
182 print OUT ($t, "++\$i while (\$i <= \$#INC",
183 " and \$INC[\$i].'/$incl' ne \$INC{'$incl'});\n");
184 print OUT ($t, "\$i = 0 if \$INC[\$i].'/$incl' ne",
185 " \$INC{'$incl'};\n");
187 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
188 print OUT ($t, "}\n");
190 "eval(\"require '\" . ",
191 "(\$i ? \$INC[\$i].'/' : '') . \"\$incl';\");");
192 # any better? require is smart enough not to try and include a
193 # file twice, i believe, so require-ing the same actual file
194 # should end up just being a null operation...
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(/^warning\s+(.*)/) {
236 print OUT $t, "warn(\"$1\");\n";
237 } elsif(/^ident\s+(.*)/) {
238 print OUT $t, "# $1\n";
256 my($joined_args) = join('|', keys(%curargs));
259 s/^\&\&// && do { $new .= " &&"; next;}; # handle && operator
260 s/^\&([\(a-z\)]+)/$1/i; # hack for things that take the address of
261 s/^(\s+)// && do {$new .= ' '; next;};
262 s/^(0X[0-9A-F]+)[UL]*//i && do {$new .= lc($1); next;};
263 s/^(-?\d+\.\d+E[-+]\d+)F?//i && do {$new .= $1; next;};
264 s/^(\d+)\s*[LU]*//i && do {$new .= $1; next;};
265 s/^("(\\"|[^"])*")// && do {$new .= $1; next;};
266 s/^'((\\"|[^"])*)'// && do {
268 $new .= "ord('\$$1')";
274 # replace "sizeof(foo)" with "{foo}"
275 # also, remove * (C dereference operator) to avoid perl syntax
276 # problems. Where the %sizeof array comes from is anyone's
277 # guess (c2ph?), but this at least avoids fatal syntax errors.
278 # Behavior is undefined if sizeof() delimiters are unbalanced.
279 # This code was modified to able to handle constructs like this:
280 # sizeof(*(p)), which appear in the HP-UX 10.01 header files.
281 s/^sizeof\s*\(// && do {
283 my $lvl = 1; # already saw one open paren
284 # tack { on the front, and skip it in the loop
287 # find balanced closing paren
288 while ($index <= length($_) && $lvl > 0) {
289 $lvl++ if substr($_, $index, 1) eq "(";
290 $lvl-- if substr($_, $index, 1) eq ")";
293 # tack } on the end, replacing )
294 substr($_, $index - 1, 1) = "}";
295 # remove pesky * operators within the sizeof argument
296 substr($_, 0, $index - 1) =~ s/\*//g;
300 /\(([\w\s]+)[\*\s]*\)\s*[\w\(]/ && do {
301 foreach (split /\s+/, $1) { # Make sure all the words are types,
302 last unless ($isatype{$_} or $_ eq 'struct');
304 s/\([\w\s]+[\*\s]*\)// && next; # then eliminate them.
306 # struct/union member, including arrays:
307 s/^([_A-Z]\w*(\[[^\]]+\])?((\.|->)[_A-Z]\w*(\[[^\]]+\])?)+)//i && do {
309 $id =~ s/(\.|(->))([^\.\-]*)/->\{$3\}/g;
310 $id =~ s/\b([^\$])($joined_args)/$1\$$2/g if length($joined_args);
311 while($id =~ /\[\s*([^\$\&\d\]]+)\]/) {
314 if(exists($curargs{$index})) {
319 $id =~ s/\[\s*([^\$\&\d\]]+)\]/[$index]/;
323 s/^([_a-zA-Z]\w*)// && do {
325 if ($id eq 'struct') {
329 } elsif ($id =~ /^((un)?signed)|(long)|(short)$/) {
330 while (s/^\s+(\w+)//) { $id .= ' ' . $1; }
335 $new .= '->' if /^[\[\{]/;
336 } elsif ($id eq 'defined') {
339 s/^\((\w),/("$1",/ if $id =~ /^_IO[WR]*$/i; # cheat
341 } elsif ($isatype{$id}) {
342 if ($new =~ /{\s*$/) {
344 } elsif ($new =~ /\(\s*$/ && /^[\s*]*\)/) {
348 $new .= q(').$id.q(');
351 if ($inif && $new !~ /defined\s*\($/) {
352 $new .= '(defined(&' . $id . ') ? &' . $id . ' : 0)';
361 s/^(.)// && do { if ($1 ne '#') { $new .= $1; } next;};
366 # Handle recursive subdirectories without getting a grotesquely big stack.
367 # Could this be implemented using File::Find?
375 if ($file eq '-' or -f $file or -l $file) {
381 print STDERR "Skipping directory `$file'\n";
384 print STDERR "Skipping `$file': not a file or directory\n";
392 # Put all the files in $directory into @ARGV for processing.
395 my ($directory) = @_;
397 $directory =~ s:/$::;
399 opendir DIR, $directory;
400 foreach (readdir DIR) {
401 next if ($_ eq '.' or $_ eq '..');
403 # expand_glob() is going to be called until $ARGV[0] isn't a
404 # directory; so push directories, and unshift everything else.
405 if (-d "$directory/$_") {
406 push @ARGV, "$directory/$_";
408 unshift @ARGV, "$directory/$_";
415 # Given $file, a symbolic link to a directory in the C include directory,
416 # make an equivalent symbolic link in $Dest_dir, if we can figure out how.
417 # Otherwise, just duplicate the file or directory.
421 my $target = eval 'readlink($dirlink)';
423 if ($target =~ m:^\.\./: or $target =~ m:^/:) {
424 # The target of a parent or absolute link could leave the $Dest_dir
425 # hierarchy, so let's put all of the contents of $dirlink (actually,
426 # the contents of $target) into @ARGV; as a side effect down the
427 # line, $dirlink will get created as an _actual_ directory.
428 expand_glob($dirlink);
430 if (-l "$Dest_dir/$dirlink") {
431 unlink "$Dest_dir/$dirlink" or
432 print STDERR "Could not remove link $Dest_dir/$dirlink: $!\n";
434 if (eval 'symlink($target, "$Dest_dir/$dirlink")') {
435 print "Linking $target -> $Dest_dir/$dirlink\n";
437 # Make sure that the link _links_ to something:
438 if (! -e "$Dest_dir/$target") {
439 mkdir("$Dest_dir/$target", 0755) or
440 print STDERR "Could not create $Dest_dir/$target/\n";
443 print STDERR "Could not symlink $target -> $Dest_dir/$dirlink: $!\n";
451 ##############################################################################
456 h2ph - convert .h C header files to .ph Perl header files
460 B<h2ph [-d destination directory] [-r] [-l] [headerfiles]>
465 converts any C header files specified to the corresponding Perl header file
467 It is most easily run while in /usr/include:
469 cd /usr/include; h2ph * sys/*
473 cd /usr/include; h2ph -r -l .
475 The output files are placed in the hierarchy rooted at Perl's
476 architecture dependent library directory. You can specify a different
477 hierarchy with a B<-d> switch.
479 If run with no arguments, filters standard input to standard output.
485 =item -d destination_dir
487 Put the resulting B<.ph> files beneath B<destination_dir>, instead of
488 beneath the default Perl library location (C<$Config{'installsitsearch'}>).
492 Run recursively; if any of B<headerfiles> are directories, then run I<h2ph>
493 on all files in those directories (and their subdirectories, etc.).
497 Symbolic links will be replicated in the destination directory. If B<-l>
498 is not specified, then links are skipped over.
502 Put ``hints'' in the .ph files which will help in locating problems with
503 I<h2ph>. In those cases when you B<require> a B<.ph> file containing syntax
504 errors, instead of the cryptic
506 [ some error condition ] at (eval mmm) line nnn
508 you will see the slightly more helpful
510 [ some error condition ] at filename.ph line nnn
512 However, the B<.ph> files almost double in size when built using B<-h>.
518 No environment variables are used.
537 The usual warnings if it can't read or write the files involved.
541 Doesn't construct the %sizeof array for you.
543 It doesn't handle all C constructs, but it does attempt to isolate
544 definitions inside evals so that you can get at the definitions
545 that it can translate.
547 It's only intended as a rough tool.
548 You may need to dicker with the files produced.
554 close OUT or die "Can't close $file: $!";
555 chmod 0755, $file or die "Can't reset permissions for $file: $!\n";
556 exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';