Integrate win32
[p5sagit/p5-mst-13.2.git] / utils / h2ph.PL
CommitLineData
4633a7c4 1#!/usr/local/bin/perl
2
3use Config;
ee580363 4use File::Basename qw(basename dirname);
4633a7c4 5
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
10# $startperl
11# to ensure Configure will look for $Config{startperl}.
12# Wanted: $archlibexp
13
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.
44a8e56a 16chdir dirname($0);
17$file = basename($0, '.PL');
774d564b 18$file .= '.com' if $^O eq 'VMS';
4633a7c4 19
20open OUT,">$file" or die "Can't create $file: $!";
21
22print "Extracting $file (with variable substitutions)\n";
23
24# In this section, perl variables will be expanded during extraction.
25# You can use $Config{...} to use Configure variables.
26
27print OUT <<"!GROK!THIS!";
5f05dabc 28$Config{startperl}
29 eval 'exec $Config{perlpath} -S \$0 \${1+"\$@"}'
30 if \$running_under_some_shell;
154e51a4 31!GROK!THIS!
32
4633a7c4 33# In the following, perl variables are not expanded during extraction.
34
35print OUT <<'!NO!SUBS!';
154e51a4 36
2c2acf7e 37use Config;
b306bf39 38use File::Path qw(mkpath);
50f6e060 39use Getopt::Std;
40
ee580363 41getopts('Dd:rlh');
2c2acf7e 42
b306bf39 43my $Exit = 0;
44
50f6e060 45my $Dest_dir = $opt_d || $Config{installsitearch};
b306bf39 46die "Destination directory $Dest_dir doesn't exist or isn't a directory\n"
47 unless -d $Dest_dir;
154e51a4 48
fe14fcc3 49@isatype = split(' ',<<END);
50 char uchar u_char
51 short ushort u_short
52 int uint u_int
53 long ulong u_long
fb73857a 54 FILE key_t caddr_t
fe14fcc3 55END
56
55204971 57@isatype{@isatype} = (1) x @isatype;
748a9306 58$inif = 0;
fe14fcc3 59
60@ARGV = ('-') unless @ARGV;
154e51a4 61
50f6e060 62while (defined ($file = next_file())) {
63 if (-l $file and -d $file) {
64 link_if_possible($file) if ($opt_l);
65 next;
66 }
67
5f05dabc 68 # Recover from header files with unbalanced cpp directives
69 $t = '';
70 $tab = 0;
71
50f6e060 72 # $eval_index goes into ``#line'' directives, to help locate syntax errors:
73 $eval_index = 1;
74
fe14fcc3 75 if ($file eq '-') {
76 open(IN, "-");
77 open(OUT, ">-");
ee580363 78 } else {
fe14fcc3 79 ($outfile = $file) =~ s/\.h$/.ph/ || next;
80 print "$file -> $outfile\n";
81 if ($file =~ m|^(.*)/|) {
82 $dir = $1;
b306bf39 83 mkpath "$Dest_dir/$dir";
154e51a4 84 }
b306bf39 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";
154e51a4 87 }
154e51a4 88 while (<IN>) {
89 chop;
90 while (/\\$/) {
91 chop;
92 $_ .= <IN>;
93 chop;
94 }
ee580363 95 print OUT "# $_\n" if $opt_D;
154e51a4 96 if (s:/\*:\200:g) {
97 s:\*/:\201:g;
98 s/\200[^\201]*\201//g; # delete single line comments
99 if (s/\200.*//) { # begin multi-line comment?
100 $_ .= '/*';
101 $_ .= <IN>;
102 redo;
103 }
104 }
ee580363 105 if (s/^\s*#\s*//) {
154e51a4 106 if (s/^define\s+(\w+)//) {
107 $name = $1;
108 $new = '';
109 s/\s+$//;
110 if (s/^\(([\w,\s]*)\)//) {
111 $args = $1;
b306bf39 112 my $proto = '() ';
154e51a4 113 if ($args ne '') {
b306bf39 114 $proto = '';
154e51a4 115 foreach $arg (split(/,\s*/,$args)) {
55204971 116 $arg =~ s/^\s*([^\s].*[^\s])\s*$/$1/;
154e51a4 117 $curargs{$arg} = 1;
118 }
119 $args =~ s/\b(\w)/\$$1/g;
120 $args = "local($args) = \@_;\n$t ";
121 }
122 s/^\s+//;
5f05dabc 123 expr();
ee580363 124 $new =~ s/(["\\])/\\$1/g; #"]);
125 $new = reindent($new);
126 $args = reindent($args);
154e51a4 127 if ($t ne '') {
ee580363 128 $new =~ s/(['\\])/\\$1/g; #']);
50f6e060 129 if ($opt_h) {
130 print OUT $t,
ee580363 131 "eval \"\\n#line $eval_index $outfile\\n\" . 'sub $name $proto\{\n$t ${args}eval q($new);\n$t}' unless defined(\&$name);\n";
50f6e060 132 $eval_index++;
133 } else {
134 print OUT $t,
ee580363 135 "eval 'sub $name $proto\{\n$t ${args}eval q($new);\n$t}' unless defined(\&$name);\n";
50f6e060 136 }
ee580363 137 } else {
138 print OUT "unless(defined(\&$name)) {\n sub $name $proto\{\n\t${args}eval q($new);\n }\n}\n";
154e51a4 139 }
140 %curargs = ();
ee580363 141 } else {
154e51a4 142 s/^\s+//;
5f05dabc 143 expr();
154e51a4 144 $new = 1 if $new eq '';
ee580363 145 $new = reindent($new);
146 $args = reindent($args);
154e51a4 147 if ($t ne '') {
ee580363 148 $new =~ s/(['\\])/\\$1/g; #']);
50f6e060 149 if ($opt_h) {
150 print OUT $t,"eval \"\\n#line $eval_index $outfile\\n\" . 'sub $name () {",$new,";}' unless defined(\&$name);\n";
151 $eval_index++;
152 } else {
153 print OUT $t,"eval 'sub $name () {",$new,";}' unless defined(\&$name);\n";
154 }
ee580363 155 } else {
156 print OUT $t,"unless(defined(\&$name)) {\n sub $name () {\t",$new,";}\n}\n";
154e51a4 157 }
158 }
ee580363 159 } elsif (/^(include|import)\s*[<"](.*)[>"]/) {
160 ($incl = $2) =~ s/\.h$/.ph/;
d9d8d8de 161 print OUT $t,"require '$incl';\n";
ee580363 162 } elsif(/^include_next\s*[<"](.*)[>"]/) {
163 ($incl = $1) =~ s/\.h$/.ph/;
164 # should've read up on #include_next properly before attempting
165 # to implement it...
166 #
167 #print OUT $t, "{\n";
168 #$tab += 4;
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";
173 #$tab -= 4;
174 #$t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
175 #print OUT $t, "}\n";
176 #
177 # try this instead:
178 print OUT ($t, "my(\$i) = 0;\n");
179 print OUT ($t, "if(exists(\$INC{$incl})) {\n");
154e51a4 180 $tab += 4;
181 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
ee580363 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");
186 $tab -= 4;
187 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
188 print OUT ($t, "}\n");
189 print OUT ($t,
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";
154e51a4 197 $tab += 4;
198 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
ee580363 199 } elsif (/^ifndef\s+(\w+)/) {
200 print OUT $t,"unless(defined(&$1)) {\n";
201 $tab += 4;
202 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
203 } elsif (s/^if\s+//) {
154e51a4 204 $new = '';
748a9306 205 $inif = 1;
5f05dabc 206 expr();
748a9306 207 $inif = 0;
ee580363 208 print OUT $t,"if($new) {\n";
154e51a4 209 $tab += 4;
210 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
ee580363 211 } elsif (s/^elif\s+//) {
154e51a4 212 $new = '';
748a9306 213 $inif = 1;
5f05dabc 214 expr();
748a9306 215 $inif = 0;
154e51a4 216 $tab -= 4;
217 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
ee580363 218 print OUT $t,"}\n elsif($new) {\n";
154e51a4 219 $tab += 4;
220 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
ee580363 221 } elsif (/^else/) {
154e51a4 222 $tab -= 4;
223 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
ee580363 224 print OUT $t,"} else {\n";
154e51a4 225 $tab += 4;
226 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
ee580363 227 } elsif (/^endif/) {
154e51a4 228 $tab -= 4;
229 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
230 print OUT $t,"}\n";
ee580363 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";
154e51a4 239 }
240 }
241 }
242 print OUT "1;\n";
243}
244
b306bf39 245exit $Exit;
246
ee580363 247sub reindent($) {
248 my($text) = shift;
249 $text =~ s/\n/\n /g;
250 $text =~ s/ /\t/g;
251 $text;
252}
253
154e51a4 254sub expr {
ee580363 255 if(keys(%curargs)) {
256 my($joined_args) = join('|', keys(%curargs));
257 }
154e51a4 258 while ($_ ne '') {
ee580363 259 s/^\&\&// && do { $new .= " &&"; next;}; # handle && operator
260 s/^\&([\(a-z\)]+)/$1/i; # hack for things that take the address of
154e51a4 261 s/^(\s+)// && do {$new .= ' '; next;};
50f6e060 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;};
154e51a4 265 s/^("(\\"|[^"])*")// && do {$new .= $1; next;};
266 s/^'((\\"|[^"])*)'// && do {
267 if ($curargs{$1}) {
268 $new .= "ord('\$$1')";
ee580363 269 } else {
154e51a4 270 $new .= "ord('$1')";
271 }
272 next;
273 };
5f05dabc 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 {
282 $new .= '$sizeof';
283 my $lvl = 1; # already saw one open paren
284 # tack { on the front, and skip it in the loop
285 $_ = "{" . "$_";
286 my $index = 1;
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 ")";
291 $index++;
292 }
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;
297 next;
298 };
50f6e060 299 # Eliminate typedefs
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');
303 }
304 s/\([\w\s]+[\*\s]*\)// && next; # then eliminate them.
305 };
ee580363 306 # struct/union member, including arrays:
307 s/^([_A-Z]\w*(\[[^\]]+\])?((\.|->)[_A-Z]\w*(\[[^\]]+\])?)+)//i && do {
50f6e060 308 $id = $1;
ee580363 309 $id =~ s/(\.|(->))([^\.\-]*)/->\{$3\}/g;
310 $id =~ s/\b([^\$])($joined_args)/$1\$$2/g if length($joined_args);
311 while($id =~ /\[\s*([^\$\&\d\]]+)\]/) {
312 my($index) = $1;
313 $index =~ s/\s//g;
314 if(exists($curargs{$index})) {
315 $index = "\$$index";
316 } else {
317 $index = "&$index";
318 }
319 $id =~ s/\[\s*([^\$\&\d\]]+)\]/[$index]/;
320 }
321 $new .= " (\$$id)";
50f6e060 322 };
154e51a4 323 s/^([_a-zA-Z]\w*)// && do {
324 $id = $1;
fe14fcc3 325 if ($id eq 'struct') {
326 s/^\s+(\w+)//;
327 $id .= ' ' . $1;
328 $isatype{$id} = 1;
ee580363 329 } elsif ($id =~ /^((un)?signed)|(long)|(short)$/) {
50f6e060 330 while (s/^\s+(\w+)//) { $id .= ' ' . $1; }
fe14fcc3 331 $isatype{$id} = 1;
332 }
154e51a4 333 if ($curargs{$id}) {
ee580363 334 $new .= "\$$id";
335 $new .= '->' if /^[\[\{]/;
336 } elsif ($id eq 'defined') {
154e51a4 337 $new .= 'defined';
ee580363 338 } elsif (/^\(/) {
e5d73d77 339 s/^\((\w),/("$1",/ if $id =~ /^_IO[WR]*$/i; # cheat
154e51a4 340 $new .= " &$id";
ee580363 341 } elsif ($isatype{$id}) {
fe14fcc3 342 if ($new =~ /{\s*$/) {
343 $new .= "'$id'";
ee580363 344 } elsif ($new =~ /\(\s*$/ && /^[\s*]*\)/) {
fe14fcc3 345 $new =~ s/\(\s*$//;
346 s/^[\s*]*\)//;
ee580363 347 } else {
b276c83d 348 $new .= q(').$id.q(');
fe14fcc3 349 }
ee580363 350 } else {
c07a80fd 351 if ($inif && $new !~ /defined\s*\($/) {
748a9306 352 $new .= '(defined(&' . $id . ') ? &' . $id . ' : 0)';
ee580363 353 } elsif (/^\[/) {
354 $new .= " \$$id";
355 } else {
748a9306 356 $new .= ' &' . $id;
357 }
154e51a4 358 }
359 next;
360 };
fb21d8eb 361 s/^(.)// && do { if ($1 ne '#') { $new .= $1; } next;};
154e51a4 362 }
363}
50f6e060 364
365
366# Handle recursive subdirectories without getting a grotesquely big stack.
367# Could this be implemented using File::Find?
368sub next_file
369{
370 my $file;
371
372 while (@ARGV) {
373 $file = shift @ARGV;
374
375 if ($file eq '-' or -f $file or -l $file) {
376 return $file;
377 } elsif (-d $file) {
378 if ($opt_r) {
379 expand_glob($file);
380 } else {
381 print STDERR "Skipping directory `$file'\n";
382 }
ee580363 383 } else {
50f6e060 384 print STDERR "Skipping `$file': not a file or directory\n";
385 }
386 }
387
388 return undef;
389}
390
391
392# Put all the files in $directory into @ARGV for processing.
393sub expand_glob
394{
395 my ($directory) = @_;
396
397 $directory =~ s:/$::;
398
399 opendir DIR, $directory;
400 foreach (readdir DIR) {
401 next if ($_ eq '.' or $_ eq '..');
402
403 # expand_glob() is going to be called until $ARGV[0] isn't a
404 # directory; so push directories, and unshift everything else.
ee580363 405 if (-d "$directory/$_") {
406 push @ARGV, "$directory/$_";
407 } else {
408 unshift @ARGV, "$directory/$_";
409 }
50f6e060 410 }
411 closedir DIR;
412}
413
414
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.
418sub link_if_possible
419{
420 my ($dirlink) = @_;
421 my $target = eval 'readlink($dirlink)';
422
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);
429 } else {
430 if (-l "$Dest_dir/$dirlink") {
431 unlink "$Dest_dir/$dirlink" or
432 print STDERR "Could not remove link $Dest_dir/$dirlink: $!\n";
433 }
50f6e060 434 if (eval 'symlink($target, "$Dest_dir/$dirlink")') {
435 print "Linking $target -> $Dest_dir/$dirlink\n";
436
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";
441 }
442 } else {
443 print STDERR "Could not symlink $target -> $Dest_dir/$dirlink: $!\n";
444 }
445 }
446}
447
448
4491;
450
154e51a4 451##############################################################################
1fef88e7 452__END__
453
454=head1 NAME
455
456h2ph - convert .h C header files to .ph Perl header files
457
458=head1 SYNOPSIS
459
50f6e060 460B<h2ph [-d destination directory] [-r] [-l] [headerfiles]>
1fef88e7 461
462=head1 DESCRIPTION
154e51a4 463
1fef88e7 464I<h2ph>
154e51a4 465converts any C header files specified to the corresponding Perl header file
466format.
467It is most easily run while in /usr/include:
154e51a4 468
469 cd /usr/include; h2ph * sys/*
470
50f6e060 471or
472
473 cd /usr/include; h2ph -r -l .
474
b306bf39 475The output files are placed in the hierarchy rooted at Perl's
476architecture dependent library directory. You can specify a different
477hierarchy with a B<-d> switch.
478
fe14fcc3 479If run with no arguments, filters standard input to standard output.
1fef88e7 480
50f6e060 481=head1 OPTIONS
482
483=over 4
484
485=item -d destination_dir
486
487Put the resulting B<.ph> files beneath B<destination_dir>, instead of
488beneath the default Perl library location (C<$Config{'installsitsearch'}>).
489
490=item -r
491
492Run recursively; if any of B<headerfiles> are directories, then run I<h2ph>
493on all files in those directories (and their subdirectories, etc.).
494
495=item -l
496
497Symbolic links will be replicated in the destination directory. If B<-l>
498is not specified, then links are skipped over.
499
500=item -h
501
502Put ``hints'' in the .ph files which will help in locating problems with
503I<h2ph>. In those cases when you B<require> a B<.ph> file containing syntax
504errors, instead of the cryptic
505
506 [ some error condition ] at (eval mmm) line nnn
507
508you will see the slightly more helpful
509
510 [ some error condition ] at filename.ph line nnn
511
512However, the B<.ph> files almost double in size when built using B<-h>.
513
514=back
515
1fef88e7 516=head1 ENVIRONMENT
517
154e51a4 518No environment variables are used.
1fef88e7 519
520=head1 FILES
521
522 /usr/include/*.h
523 /usr/include/sys/*.h
524
154e51a4 525etc.
1fef88e7 526
527=head1 AUTHOR
528
154e51a4 529Larry Wall
1fef88e7 530
531=head1 SEE ALSO
532
154e51a4 533perl(1)
1fef88e7 534
535=head1 DIAGNOSTICS
536
154e51a4 537The usual warnings if it can't read or write the files involved.
1fef88e7 538
539=head1 BUGS
540
154e51a4 541Doesn't construct the %sizeof array for you.
1fef88e7 542
154e51a4 543It doesn't handle all C constructs, but it does attempt to isolate
544definitions inside evals so that you can get at the definitions
545that it can translate.
1fef88e7 546
154e51a4 547It's only intended as a rough tool.
548You may need to dicker with the files produced.
1fef88e7 549
550=cut
551
154e51a4 552!NO!SUBS!
4633a7c4 553
554close OUT or die "Can't close $file: $!";
555chmod 0755, $file or die "Can't reset permissions for $file: $!\n";
556exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';