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);
42 my $Dest_dir = (@ARGV && $ARGV[0] =~ s/^-d//)
44 : $Config{installsitearch};
45 die "Destination directory $Dest_dir doesn't exist or isn't a directory\n"
48 @isatype = split(' ',<<END);
56 @isatype{@isatype} = (1) x @isatype;
59 @ARGV = ('-') unless @ARGV;
61 foreach $file (@ARGV) {
62 # Recover from header files with unbalanced cpp directives
71 ($outfile = $file) =~ s/\.h$/.ph/ || next;
72 print "$file -> $outfile\n";
73 if ($file =~ m|^(.*)/|) {
75 mkpath "$Dest_dir/$dir";
77 open(IN,"$file") || (($Exit = 1),(warn "Can't open $file: $!\n"),next);
78 open(OUT,">$Dest_dir/$outfile") || die "Can't create $outfile: $!\n";
89 s/\200[^\201]*\201//g; # delete single line comments
90 if (s/\200.*//) { # begin multi-line comment?
97 if (s/^define\s+(\w+)//) {
101 if (s/^\(([\w,\s]*)\)//) {
106 foreach $arg (split(/,\s*/,$args)) {
107 $arg =~ s/^\s*([^\s].*[^\s])\s*$/$1/;
110 $args =~ s/\b(\w)/\$$1/g;
111 $args = "local($args) = \@_;\n$t ";
115 $new =~ s/(["\\])/\\$1/g;
117 $new =~ s/(['\\])/\\$1/g;
119 "eval 'sub $name $proto\{\n$t ${args}eval \"$new\";\n$t}';\n";
122 print OUT "sub $name $proto\{\n ${args}eval \"$new\";\n}\n";
129 $new = 1 if $new eq '';
131 $new =~ s/(['\\])/\\$1/g;
132 print OUT $t,"eval 'sub $name () {",$new,";}';\n";
135 print OUT $t,"sub $name () {",$new,";}\n";
139 elsif (/^include\s*<(.*)>/) {
140 ($incl = $1) =~ s/\.h$/.ph/;
141 print OUT $t,"require '$incl';\n";
143 elsif (/^ifdef\s+(\w+)/) {
144 print OUT $t,"if (defined &$1) {\n";
146 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
148 elsif (/^ifndef\s+(\w+)/) {
149 print OUT $t,"if (!defined &$1) {\n";
151 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
158 print OUT $t,"if ($new) {\n";
160 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
162 elsif (s/^elif\s+//) {
168 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
169 print OUT $t,"}\n${t}elsif ($new) {\n";
171 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
175 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
176 print OUT $t,"}\n${t}else {\n";
178 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
182 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
194 s/^(\s+)// && do {$new .= ' '; next;};
195 s/^(0x[0-9a-fA-F]+)// && do {$new .= $1; next;};
196 s/^(\d+)[LlUu]*// && do {$new .= $1; next;};
197 s/^("(\\"|[^"])*")// && do {$new .= $1; next;};
198 s/^'((\\"|[^"])*)'// && do {
200 $new .= "ord('\$$1')";
207 # replace "sizeof(foo)" with "{foo}"
208 # also, remove * (C dereference operator) to avoid perl syntax
209 # problems. Where the %sizeof array comes from is anyone's
210 # guess (c2ph?), but this at least avoids fatal syntax errors.
211 # Behavior is undefined if sizeof() delimiters are unbalanced.
212 # This code was modified to able to handle constructs like this:
213 # sizeof(*(p)), which appear in the HP-UX 10.01 header files.
214 s/^sizeof\s*\(// && do {
216 my $lvl = 1; # already saw one open paren
217 # tack { on the front, and skip it in the loop
220 # find balanced closing paren
221 while ($index <= length($_) && $lvl > 0) {
222 $lvl++ if substr($_, $index, 1) eq "(";
223 $lvl-- if substr($_, $index, 1) eq ")";
226 # tack } on the end, replacing )
227 substr($_, $index - 1, 1) = "}";
228 # remove pesky * operators within the sizeof argument
229 substr($_, 0, $index - 1) =~ s/\*//g;
232 s/^([_a-zA-Z]\w*)// && do {
234 if ($id eq 'struct') {
239 elsif ($id eq 'unsigned' || $id eq 'long') {
247 elsif ($id eq 'defined') {
251 s/^\((\w),/("$1",/ if $id =~ /^_IO[WR]*$/i; # cheat
254 elsif ($isatype{$id}) {
255 if ($new =~ /{\s*$/) {
258 elsif ($new =~ /\(\s*$/ && /^[\s*]*\)/) {
263 $new .= q(').$id.q(');
267 if ($inif && $new !~ /defined\s*\($/) {
268 $new .= '(defined(&' . $id . ') ? &' . $id . ' : 0)';
279 s/^(.)// && do { if ($1 ne '#') { $new .= $1; } next;};
282 ##############################################################################
287 h2ph - convert .h C header files to .ph Perl header files
291 B<h2ph [headerfiles]>
296 converts any C header files specified to the corresponding Perl header file
298 It is most easily run while in /usr/include:
300 cd /usr/include; h2ph * sys/*
302 The output files are placed in the hierarchy rooted at Perl's
303 architecture dependent library directory. You can specify a different
304 hierarchy with a B<-d> switch.
306 If run with no arguments, filters standard input to standard output.
310 No environment variables are used.
329 The usual warnings if it can't read or write the files involved.
333 Doesn't construct the %sizeof array for you.
335 It doesn't handle all C constructs, but it does attempt to isolate
336 definitions inside evals so that you can get at the definitions
337 that it can translate.
339 It's only intended as a rough tool.
340 You may need to dicker with the files produced.
346 close OUT or die "Can't close $file: $!";
347 chmod 0755, $file or die "Can't reset permissions for $file: $!\n";
348 exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';