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');
19 open OUT,">$file" or die "Can't create $file: $!";
21 print "Extracting $file (with variable substitutions)\n";
23 # In this section, perl variables will be expanded during extraction.
24 # You can use $Config{...} to use Configure variables.
26 print OUT <<"!GROK!THIS!";
28 eval 'exec $Config{perlpath} -S \$0 \${1+"\$@"}'
29 if \$running_under_some_shell;
32 # In the following, perl variables are not expanded during extraction.
34 print OUT <<'!NO!SUBS!';
37 $perlincl = @Config{installsitearch};
39 chdir '/usr/include' || die "Can't cd /usr/include";
41 @isatype = split(' ',<<END);
49 @isatype{@isatype} = (1) x @isatype;
52 @ARGV = ('-') unless @ARGV;
54 foreach $file (@ARGV) {
55 # Recover from header files with unbalanced cpp directives
64 ($outfile = $file) =~ s/\.h$/.ph/ || next;
65 print "$file -> $outfile\n";
66 if ($file =~ m|^(.*)/|) {
68 if (!-d "$perlincl/$dir") {
69 mkdir("$perlincl/$dir",0777);
72 open(IN,"$file") || ((warn "Can't open $file: $!\n"),next);
73 open(OUT,">$perlincl/$outfile") || die "Can't create $outfile: $!\n";
84 s/\200[^\201]*\201//g; # delete single line comments
85 if (s/\200.*//) { # begin multi-line comment?
92 if (s/^define\s+(\w+)//) {
96 if (s/^\(([\w,\s]*)\)//) {
99 foreach $arg (split(/,\s*/,$args)) {
100 $arg =~ s/^\s*([^\s].*[^\s])\s*$/$1/;
103 $args =~ s/\b(\w)/\$$1/g;
104 $args = "local($args) = \@_;\n$t ";
108 $new =~ s/(["\\])/\\$1/g;
110 $new =~ s/(['\\])/\\$1/g;
112 "eval 'sub $name {\n$t ${args}eval \"$new\";\n$t}';\n";
115 print OUT "sub $name {\n ${args}eval \"$new\";\n}\n";
122 $new = 1 if $new eq '';
124 $new =~ s/(['\\])/\\$1/g;
125 print OUT $t,"eval 'sub $name {",$new,";}';\n";
128 print OUT $t,"sub $name {",$new,";}\n";
132 elsif (/^include\s*<(.*)>/) {
133 ($incl = $1) =~ s/\.h$/.ph/;
134 print OUT $t,"require '$incl';\n";
136 elsif (/^ifdef\s+(\w+)/) {
137 print OUT $t,"if (defined &$1) {\n";
139 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
141 elsif (/^ifndef\s+(\w+)/) {
142 print OUT $t,"if (!defined &$1) {\n";
144 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
151 print OUT $t,"if ($new) {\n";
153 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
155 elsif (s/^elif\s+//) {
161 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
162 print OUT $t,"}\n${t}elsif ($new) {\n";
164 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
168 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
169 print OUT $t,"}\n${t}else {\n";
171 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
175 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
185 s/^(\s+)// && do {$new .= ' '; next;};
186 s/^(0x[0-9a-fA-F]+)// && do {$new .= $1; next;};
187 s/^(\d+)[LlUu]*// && do {$new .= $1; next;};
188 s/^("(\\"|[^"])*")// && do {$new .= $1; next;};
189 s/^'((\\"|[^"])*)'// && do {
191 $new .= "ord('\$$1')";
198 # replace "sizeof(foo)" with "{foo}"
199 # also, remove * (C dereference operator) to avoid perl syntax
200 # problems. Where the %sizeof array comes from is anyone's
201 # guess (c2ph?), but this at least avoids fatal syntax errors.
202 # Behavior is undefined if sizeof() delimiters are unbalanced.
203 # This code was modified to able to handle constructs like this:
204 # sizeof(*(p)), which appear in the HP-UX 10.01 header files.
205 s/^sizeof\s*\(// && do {
207 my $lvl = 1; # already saw one open paren
208 # tack { on the front, and skip it in the loop
211 # find balanced closing paren
212 while ($index <= length($_) && $lvl > 0) {
213 $lvl++ if substr($_, $index, 1) eq "(";
214 $lvl-- if substr($_, $index, 1) eq ")";
217 # tack } on the end, replacing )
218 substr($_, $index - 1, 1) = "}";
219 # remove pesky * operators within the sizeof argument
220 substr($_, 0, $index - 1) =~ s/\*//g;
223 s/^([_a-zA-Z]\w*)// && do {
225 if ($id eq 'struct') {
230 elsif ($id eq 'unsigned') {
238 elsif ($id eq 'defined') {
242 s/^\((\w),/("$1",/ if $id =~ /^_IO[WR]*$/i; # cheat
245 elsif ($isatype{$id}) {
246 if ($new =~ /{\s*$/) {
249 elsif ($new =~ /\(\s*$/ && /^[\s*]*\)/) {
254 $new .= q(').$id.q(');
258 if ($inif && $new !~ /defined\s*\($/) {
259 $new .= '(defined(&' . $id . ') ? &' . $id . ' : 0)';
270 s/^(.)// && do { if ($1 ne '#') { $new .= $1; } next;};
273 ##############################################################################
278 h2ph - convert .h C header files to .ph Perl header files
282 B<h2ph [headerfiles]>
287 converts any C header files specified to the corresponding Perl header file
289 It is most easily run while in /usr/include:
291 cd /usr/include; h2ph * sys/*
293 If run with no arguments, filters standard input to standard output.
297 No environment variables are used.
316 The usual warnings if it can't read or write the files involved.
320 Doesn't construct the %sizeof array for you.
322 It doesn't handle all C constructs, but it does attempt to isolate
323 definitions inside evals so that you can get at the definitions
324 that it can translate.
326 It's only intended as a rough tool.
327 You may need to dicker with the files produced.
333 close OUT or die "Can't close $file: $!";
334 chmod 0755, $file or die "Can't reset permissions for $file: $!\n";
335 exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';