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 ($Config{'osname'} eq 'VMS' or
20 $Config{'osname'} eq 'OS2'); # "case-forgiving"
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 perl -S \$0 "\$@"'
38 \$perlincl = "$Config{archlibexp}";
42 # In the following, perl variables are not expanded during extraction.
44 print OUT <<'!NO!SUBS!';
46 chdir '/usr/include' || die "Can't cd /usr/include";
48 @isatype = split(' ',<<END);
56 @isatype{@isatype} = (1) x @isatype;
59 @ARGV = ('-') unless @ARGV;
61 foreach $file (@ARGV) {
67 ($outfile = $file) =~ s/\.h$/.ph/ || next;
68 print "$file -> $outfile\n";
69 if ($file =~ m|^(.*)/|) {
71 if (!-d "$perlincl/$dir") {
72 mkdir("$perlincl/$dir",0777);
75 open(IN,"$file") || ((warn "Can't open $file: $!\n"),next);
76 open(OUT,">$perlincl/$outfile") || die "Can't create $outfile: $!\n";
87 s/\200[^\201]*\201//g; # delete single line comments
88 if (s/\200.*//) { # begin multi-line comment?
95 if (s/^define\s+(\w+)//) {
99 if (s/^\(([\w,\s]*)\)//) {
102 foreach $arg (split(/,\s*/,$args)) {
103 $arg =~ s/^\s*([^\s].*[^\s])\s*$/$1/;
106 $args =~ s/\b(\w)/\$$1/g;
107 $args = "local($args) = \@_;\n$t ";
111 $new =~ s/(["\\])/\\$1/g;
113 $new =~ s/(['\\])/\\$1/g;
115 "eval 'sub $name {\n$t ${args}eval \"$new\";\n$t}';\n";
118 print OUT "sub $name {\n ${args}eval \"$new\";\n}\n";
125 $new = 1 if $new eq '';
127 $new =~ s/(['\\])/\\$1/g;
128 print OUT $t,"eval 'sub $name {",$new,";}';\n";
131 print OUT $t,"sub $name {",$new,";}\n";
135 elsif (/^include\s+<(.*)>/) {
136 ($incl = $1) =~ s/\.h$/.ph/;
137 print OUT $t,"require '$incl';\n";
139 elsif (/^ifdef\s+(\w+)/) {
140 print OUT $t,"if (defined &$1) {\n";
142 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
144 elsif (/^ifndef\s+(\w+)/) {
145 print OUT $t,"if (!defined &$1) {\n";
147 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
154 print OUT $t,"if ($new) {\n";
156 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
158 elsif (s/^elif\s+//) {
164 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
165 print OUT $t,"}\n${t}elsif ($new) {\n";
167 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
171 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
172 print OUT $t,"}\n${t}else {\n";
174 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
178 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
188 s/^(\s+)// && do {$new .= ' '; next;};
189 s/^(0x[0-9a-fA-F]+)// && do {$new .= $1; next;};
190 s/^(\d+)// && do {$new .= $1; next;};
191 s/^("(\\"|[^"])*")// && do {$new .= $1; next;};
192 s/^'((\\"|[^"])*)'// && do {
194 $new .= "ord('\$$1')";
201 s/^sizeof\s*\(([^)]+)\)/{$1}/ && do {
205 s/^([_a-zA-Z]\w*)// && do {
207 if ($id eq 'struct') {
212 elsif ($id eq 'unsigned') {
220 elsif ($id eq 'defined') {
224 s/^\((\w),/("$1",/ if $id =~ /^_IO[WR]*$/i; # cheat
227 elsif ($isatype{$id}) {
228 if ($new =~ /{\s*$/) {
231 elsif ($new =~ /\(\s*$/ && /^[\s*]*\)/) {
240 if ($inif && $new !~ /defined\($/) {
241 $new .= '(defined(&' . $id . ') ? &' . $id . ' : 0)';
248 s/^(.)// && do {$new .= $1; next;};
251 ##############################################################################
253 # These next few lines are legal in both Perl and nroff.
257 'di \" finish diversion--previous line must be blank
258 .nr nl 0-1 \" fake up transition to first page again
259 .nr % 0 \" start at page 1
260 '; __END__ ############# From here on it's a standard manual page ############
261 .TH H2PH 1 "August 8, 1990"
264 h2ph \- convert .h C header files to .ph Perl header files
266 .B h2ph [headerfiles]
269 converts any C header files specified to the corresponding Perl header file
271 It is most easily run while in /usr/include:
274 cd /usr/include; h2ph * sys/*
277 If run with no arguments, filters standard input to standard output.
279 No environment variables are used.
291 The usual warnings if it can't read or write the files involved.
293 Doesn't construct the %sizeof array for you.
295 It doesn't handle all C constructs, but it does attempt to isolate
296 definitions inside evals so that you can get at the definitions
297 that it can translate.
299 It's only intended as a rough tool.
300 You may need to dicker with the files produced.
304 close OUT or die "Can't close $file: $!";
305 chmod 0755, $file or die "Can't reset permissions for $file: $!\n";
306 exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';