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)) =~ s/\.PL$//;
19 if ($^O eq 'VMS' or $^O eq 'os2'); # "case-forgiving"
21 open OUT,">$file" or die "Can't create $file: $!";
23 print "Extracting $file (with variable substitutions)\n";
25 # In this section, perl variables will be expanded during extraction.
26 # You can use $Config{...} to use Configure variables.
28 print OUT <<"!GROK!THIS!";
30 eval 'exec perl -S \$0 "\$@"'
35 # In the following, perl variables are not expanded during extraction.
37 print OUT <<'!NO!SUBS!';
40 $perlincl = @Config{installsitearch};
42 chdir '/usr/include' || die "Can't cd /usr/include";
44 @isatype = split(' ',<<END);
52 @isatype{@isatype} = (1) x @isatype;
55 @ARGV = ('-') unless @ARGV;
57 foreach $file (@ARGV) {
63 ($outfile = $file) =~ s/\.h$/.ph/ || next;
64 print "$file -> $outfile\n";
65 if ($file =~ m|^(.*)/|) {
67 if (!-d "$perlincl/$dir") {
68 mkdir("$perlincl/$dir",0777);
71 open(IN,"$file") || ((warn "Can't open $file: $!\n"),next);
72 open(OUT,">$perlincl/$outfile") || die "Can't create $outfile: $!\n";
83 s/\200[^\201]*\201//g; # delete single line comments
84 if (s/\200.*//) { # begin multi-line comment?
91 if (s/^define\s+(\w+)//) {
95 if (s/^\(([\w,\s]*)\)//) {
98 foreach $arg (split(/,\s*/,$args)) {
99 $arg =~ s/^\s*([^\s].*[^\s])\s*$/$1/;
102 $args =~ s/\b(\w)/\$$1/g;
103 $args = "local($args) = \@_;\n$t ";
107 $new =~ s/(["\\])/\\$1/g;
109 $new =~ s/(['\\])/\\$1/g;
111 "eval 'sub $name {\n$t ${args}eval \"$new\";\n$t}';\n";
114 print OUT "sub $name {\n ${args}eval \"$new\";\n}\n";
121 $new = 1 if $new eq '';
123 $new =~ s/(['\\])/\\$1/g;
124 print OUT $t,"eval 'sub $name {",$new,";}';\n";
127 print OUT $t,"sub $name {",$new,";}\n";
131 elsif (/^include\s*<(.*)>/) {
132 ($incl = $1) =~ s/\.h$/.ph/;
133 print OUT $t,"require '$incl';\n";
135 elsif (/^ifdef\s+(\w+)/) {
136 print OUT $t,"if (defined &$1) {\n";
138 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
140 elsif (/^ifndef\s+(\w+)/) {
141 print OUT $t,"if (!defined &$1) {\n";
143 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
150 print OUT $t,"if ($new) {\n";
152 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
154 elsif (s/^elif\s+//) {
160 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
161 print OUT $t,"}\n${t}elsif ($new) {\n";
163 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
167 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
168 print OUT $t,"}\n${t}else {\n";
170 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
174 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
184 s/^(\s+)// && do {$new .= ' '; next;};
185 s/^(0x[0-9a-fA-F]+)// && do {$new .= $1; next;};
186 s/^(\d+)[LlUu]*// && do {$new .= $1; next;};
187 s/^("(\\"|[^"])*")// && do {$new .= $1; next;};
188 s/^'((\\"|[^"])*)'// && do {
190 $new .= "ord('\$$1')";
197 s/^sizeof\s*\(([^)]+)\)/{$1}/ && do {
201 s/^([_a-zA-Z]\w*)// && do {
203 if ($id eq 'struct') {
208 elsif ($id eq 'unsigned') {
216 elsif ($id eq 'defined') {
220 s/^\((\w),/("$1",/ if $id =~ /^_IO[WR]*$/i; # cheat
223 elsif ($isatype{$id}) {
224 if ($new =~ /{\s*$/) {
227 elsif ($new =~ /\(\s*$/ && /^[\s*]*\)/) {
232 $new .= q(').$id.q(');
236 if ($inif && $new !~ /defined\s*\($/) {
237 $new .= '(defined(&' . $id . ') ? &' . $id . ' : 0)';
248 s/^(.)// && do { if ($1 ne '#') { $new .= $1; } next;};
251 ##############################################################################
256 h2ph - convert .h C header files to .ph Perl header files
260 B<h2ph [headerfiles]>
265 converts any C header files specified to the corresponding Perl header file
267 It is most easily run while in /usr/include:
269 cd /usr/include; h2ph * sys/*
271 If run with no arguments, filters standard input to standard output.
275 No environment variables are used.
294 The usual warnings if it can't read or write the files involved.
298 Doesn't construct the %sizeof array for you.
300 It doesn't handle all C constructs, but it does attempt to isolate
301 definitions inside evals so that you can get at the definitions
302 that it can translate.
304 It's only intended as a rough tool.
305 You may need to dicker with the files produced.
311 close OUT or die "Can't close $file: $!";
312 chmod 0755, $file or die "Can't reset permissions for $file: $!\n";
313 exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';