[win32] regenerate win32/config_H.?c
[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
1d3434b8 41getopts('Dd:rlha');
42die "-r and -a options are mutually exclusive\n" if ($opt_r and $opt_a);
43@inc_dirs = inc_dirs() if $opt_a;
2c2acf7e 44
b306bf39 45my $Exit = 0;
46
50f6e060 47my $Dest_dir = $opt_d || $Config{installsitearch};
b306bf39 48die "Destination directory $Dest_dir doesn't exist or isn't a directory\n"
49 unless -d $Dest_dir;
154e51a4 50
fe14fcc3 51@isatype = split(' ',<<END);
52 char uchar u_char
53 short ushort u_short
54 int uint u_int
55 long ulong u_long
fb73857a 56 FILE key_t caddr_t
fe14fcc3 57END
58
55204971 59@isatype{@isatype} = (1) x @isatype;
748a9306 60$inif = 0;
fe14fcc3 61
62@ARGV = ('-') unless @ARGV;
154e51a4 63
50f6e060 64while (defined ($file = next_file())) {
65 if (-l $file and -d $file) {
66 link_if_possible($file) if ($opt_l);
67 next;
68 }
69
5f05dabc 70 # Recover from header files with unbalanced cpp directives
71 $t = '';
72 $tab = 0;
73
50f6e060 74 # $eval_index goes into ``#line'' directives, to help locate syntax errors:
75 $eval_index = 1;
76
fe14fcc3 77 if ($file eq '-') {
78 open(IN, "-");
79 open(OUT, ">-");
ee580363 80 } else {
fe14fcc3 81 ($outfile = $file) =~ s/\.h$/.ph/ || next;
82 print "$file -> $outfile\n";
83 if ($file =~ m|^(.*)/|) {
84 $dir = $1;
b306bf39 85 mkpath "$Dest_dir/$dir";
154e51a4 86 }
1d3434b8 87
88 if ($opt_a) { # automagic mode: locate header file in @inc_dirs
89 foreach (@inc_dirs) {
90 chdir $_;
91 last if -f $file;
92 }
93 }
94
b306bf39 95 open(IN,"$file") || (($Exit = 1),(warn "Can't open $file: $!\n"),next);
96 open(OUT,">$Dest_dir/$outfile") || die "Can't create $outfile: $!\n";
154e51a4 97 }
154e51a4 98 while (<IN>) {
99 chop;
100 while (/\\$/) {
101 chop;
102 $_ .= <IN>;
103 chop;
104 }
ee580363 105 print OUT "# $_\n" if $opt_D;
154e51a4 106 if (s:/\*:\200:g) {
107 s:\*/:\201:g;
108 s/\200[^\201]*\201//g; # delete single line comments
109 if (s/\200.*//) { # begin multi-line comment?
110 $_ .= '/*';
111 $_ .= <IN>;
112 redo;
113 }
114 }
ee580363 115 if (s/^\s*#\s*//) {
154e51a4 116 if (s/^define\s+(\w+)//) {
117 $name = $1;
118 $new = '';
119 s/\s+$//;
120 if (s/^\(([\w,\s]*)\)//) {
121 $args = $1;
b306bf39 122 my $proto = '() ';
154e51a4 123 if ($args ne '') {
b306bf39 124 $proto = '';
154e51a4 125 foreach $arg (split(/,\s*/,$args)) {
55204971 126 $arg =~ s/^\s*([^\s].*[^\s])\s*$/$1/;
154e51a4 127 $curargs{$arg} = 1;
128 }
129 $args =~ s/\b(\w)/\$$1/g;
130 $args = "local($args) = \@_;\n$t ";
131 }
132 s/^\s+//;
5f05dabc 133 expr();
ee580363 134 $new =~ s/(["\\])/\\$1/g; #"]);
135 $new = reindent($new);
136 $args = reindent($args);
154e51a4 137 if ($t ne '') {
ee580363 138 $new =~ s/(['\\])/\\$1/g; #']);
50f6e060 139 if ($opt_h) {
140 print OUT $t,
ee580363 141 "eval \"\\n#line $eval_index $outfile\\n\" . 'sub $name $proto\{\n$t ${args}eval q($new);\n$t}' unless defined(\&$name);\n";
50f6e060 142 $eval_index++;
143 } else {
144 print OUT $t,
ee580363 145 "eval 'sub $name $proto\{\n$t ${args}eval q($new);\n$t}' unless defined(\&$name);\n";
50f6e060 146 }
ee580363 147 } else {
148 print OUT "unless(defined(\&$name)) {\n sub $name $proto\{\n\t${args}eval q($new);\n }\n}\n";
154e51a4 149 }
150 %curargs = ();
ee580363 151 } else {
154e51a4 152 s/^\s+//;
5f05dabc 153 expr();
154e51a4 154 $new = 1 if $new eq '';
ee580363 155 $new = reindent($new);
156 $args = reindent($args);
154e51a4 157 if ($t ne '') {
ee580363 158 $new =~ s/(['\\])/\\$1/g; #']);
50f6e060 159 if ($opt_h) {
160 print OUT $t,"eval \"\\n#line $eval_index $outfile\\n\" . 'sub $name () {",$new,";}' unless defined(\&$name);\n";
161 $eval_index++;
162 } else {
163 print OUT $t,"eval 'sub $name () {",$new,";}' unless defined(\&$name);\n";
164 }
ee580363 165 } else {
166 print OUT $t,"unless(defined(\&$name)) {\n sub $name () {\t",$new,";}\n}\n";
154e51a4 167 }
168 }
ee580363 169 } elsif (/^(include|import)\s*[<"](.*)[>"]/) {
170 ($incl = $2) =~ s/\.h$/.ph/;
d9d8d8de 171 print OUT $t,"require '$incl';\n";
ee580363 172 } elsif(/^include_next\s*[<"](.*)[>"]/) {
173 ($incl = $1) =~ s/\.h$/.ph/;
174 # should've read up on #include_next properly before attempting
175 # to implement it...
176 #
177 #print OUT $t, "{\n";
178 #$tab += 4;
179 #$t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
180 #print OUT $t, "my(\$INC) = shift(\@INC);\n";
181 #print OUT $t, "require '$incl';\n";
182 #print OUT $t, "unshift(\@INC, \$INC);}\n";
183 #$tab -= 4;
184 #$t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
185 #print OUT $t, "}\n";
186 #
187 # try this instead:
188 print OUT ($t, "my(\$i) = 0;\n");
189 print OUT ($t, "if(exists(\$INC{$incl})) {\n");
154e51a4 190 $tab += 4;
191 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
ee580363 192 print OUT ($t, "++\$i while (\$i <= \$#INC",
193 " and \$INC[\$i].'/$incl' ne \$INC{'$incl'});\n");
194 print OUT ($t, "\$i = 0 if \$INC[\$i].'/$incl' ne",
195 " \$INC{'$incl'};\n");
196 $tab -= 4;
197 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
198 print OUT ($t, "}\n");
199 print OUT ($t,
200 "eval(\"require '\" . ",
201 "(\$i ? \$INC[\$i].'/' : '') . \"\$incl';\");");
202 # any better? require is smart enough not to try and include a
203 # file twice, i believe, so require-ing the same actual file
204 # should end up just being a null operation...
205 } elsif (/^ifdef\s+(\w+)/) {
206 print OUT $t,"if(defined(&$1)) {\n";
154e51a4 207 $tab += 4;
208 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
ee580363 209 } elsif (/^ifndef\s+(\w+)/) {
210 print OUT $t,"unless(defined(&$1)) {\n";
211 $tab += 4;
212 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
213 } elsif (s/^if\s+//) {
154e51a4 214 $new = '';
748a9306 215 $inif = 1;
5f05dabc 216 expr();
748a9306 217 $inif = 0;
ee580363 218 print OUT $t,"if($new) {\n";
154e51a4 219 $tab += 4;
220 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
ee580363 221 } elsif (s/^elif\s+//) {
154e51a4 222 $new = '';
748a9306 223 $inif = 1;
5f05dabc 224 expr();
748a9306 225 $inif = 0;
154e51a4 226 $tab -= 4;
227 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
ee580363 228 print OUT $t,"}\n elsif($new) {\n";
154e51a4 229 $tab += 4;
230 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
ee580363 231 } elsif (/^else/) {
154e51a4 232 $tab -= 4;
233 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
ee580363 234 print OUT $t,"} else {\n";
154e51a4 235 $tab += 4;
236 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
ee580363 237 } elsif (/^endif/) {
154e51a4 238 $tab -= 4;
239 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
240 print OUT $t,"}\n";
ee580363 241 } elsif(/^undef\s+(\w+)/) {
242 print OUT $t, "undef(&$1) if defined(&$1);\n";
243 } elsif(/^error\s+(.*)/) {
244 print OUT $t, "die(\"$1\");\n";
245 } elsif(/^warning\s+(.*)/) {
246 print OUT $t, "warn(\"$1\");\n";
247 } elsif(/^ident\s+(.*)/) {
248 print OUT $t, "# $1\n";
154e51a4 249 }
250 }
251 }
252 print OUT "1;\n";
1d3434b8 253
254 $is_converted{$file} = 1;
255 queue_includes_from($file) if ($opt_a);
154e51a4 256}
257
b306bf39 258exit $Exit;
259
ee580363 260sub reindent($) {
261 my($text) = shift;
262 $text =~ s/\n/\n /g;
263 $text =~ s/ /\t/g;
264 $text;
265}
266
154e51a4 267sub expr {
ee580363 268 if(keys(%curargs)) {
269 my($joined_args) = join('|', keys(%curargs));
270 }
154e51a4 271 while ($_ ne '') {
ee580363 272 s/^\&\&// && do { $new .= " &&"; next;}; # handle && operator
273 s/^\&([\(a-z\)]+)/$1/i; # hack for things that take the address of
154e51a4 274 s/^(\s+)// && do {$new .= ' '; next;};
50f6e060 275 s/^(0X[0-9A-F]+)[UL]*//i && do {$new .= lc($1); next;};
276 s/^(-?\d+\.\d+E[-+]\d+)F?//i && do {$new .= $1; next;};
277 s/^(\d+)\s*[LU]*//i && do {$new .= $1; next;};
154e51a4 278 s/^("(\\"|[^"])*")// && do {$new .= $1; next;};
279 s/^'((\\"|[^"])*)'// && do {
280 if ($curargs{$1}) {
281 $new .= "ord('\$$1')";
ee580363 282 } else {
154e51a4 283 $new .= "ord('$1')";
284 }
285 next;
286 };
5f05dabc 287 # replace "sizeof(foo)" with "{foo}"
288 # also, remove * (C dereference operator) to avoid perl syntax
289 # problems. Where the %sizeof array comes from is anyone's
290 # guess (c2ph?), but this at least avoids fatal syntax errors.
291 # Behavior is undefined if sizeof() delimiters are unbalanced.
292 # This code was modified to able to handle constructs like this:
293 # sizeof(*(p)), which appear in the HP-UX 10.01 header files.
294 s/^sizeof\s*\(// && do {
295 $new .= '$sizeof';
296 my $lvl = 1; # already saw one open paren
297 # tack { on the front, and skip it in the loop
298 $_ = "{" . "$_";
299 my $index = 1;
300 # find balanced closing paren
301 while ($index <= length($_) && $lvl > 0) {
302 $lvl++ if substr($_, $index, 1) eq "(";
303 $lvl-- if substr($_, $index, 1) eq ")";
304 $index++;
305 }
306 # tack } on the end, replacing )
307 substr($_, $index - 1, 1) = "}";
308 # remove pesky * operators within the sizeof argument
309 substr($_, 0, $index - 1) =~ s/\*//g;
310 next;
311 };
50f6e060 312 # Eliminate typedefs
313 /\(([\w\s]+)[\*\s]*\)\s*[\w\(]/ && do {
314 foreach (split /\s+/, $1) { # Make sure all the words are types,
315 last unless ($isatype{$_} or $_ eq 'struct');
316 }
317 s/\([\w\s]+[\*\s]*\)// && next; # then eliminate them.
318 };
ee580363 319 # struct/union member, including arrays:
320 s/^([_A-Z]\w*(\[[^\]]+\])?((\.|->)[_A-Z]\w*(\[[^\]]+\])?)+)//i && do {
50f6e060 321 $id = $1;
ee580363 322 $id =~ s/(\.|(->))([^\.\-]*)/->\{$3\}/g;
323 $id =~ s/\b([^\$])($joined_args)/$1\$$2/g if length($joined_args);
324 while($id =~ /\[\s*([^\$\&\d\]]+)\]/) {
325 my($index) = $1;
326 $index =~ s/\s//g;
327 if(exists($curargs{$index})) {
328 $index = "\$$index";
329 } else {
330 $index = "&$index";
331 }
332 $id =~ s/\[\s*([^\$\&\d\]]+)\]/[$index]/;
333 }
334 $new .= " (\$$id)";
50f6e060 335 };
154e51a4 336 s/^([_a-zA-Z]\w*)// && do {
337 $id = $1;
fe14fcc3 338 if ($id eq 'struct') {
339 s/^\s+(\w+)//;
340 $id .= ' ' . $1;
341 $isatype{$id} = 1;
ee580363 342 } elsif ($id =~ /^((un)?signed)|(long)|(short)$/) {
50f6e060 343 while (s/^\s+(\w+)//) { $id .= ' ' . $1; }
fe14fcc3 344 $isatype{$id} = 1;
345 }
154e51a4 346 if ($curargs{$id}) {
ee580363 347 $new .= "\$$id";
348 $new .= '->' if /^[\[\{]/;
349 } elsif ($id eq 'defined') {
154e51a4 350 $new .= 'defined';
ee580363 351 } elsif (/^\(/) {
e5d73d77 352 s/^\((\w),/("$1",/ if $id =~ /^_IO[WR]*$/i; # cheat
154e51a4 353 $new .= " &$id";
ee580363 354 } elsif ($isatype{$id}) {
fe14fcc3 355 if ($new =~ /{\s*$/) {
356 $new .= "'$id'";
ee580363 357 } elsif ($new =~ /\(\s*$/ && /^[\s*]*\)/) {
fe14fcc3 358 $new =~ s/\(\s*$//;
359 s/^[\s*]*\)//;
ee580363 360 } else {
b276c83d 361 $new .= q(').$id.q(');
fe14fcc3 362 }
ee580363 363 } else {
c07a80fd 364 if ($inif && $new !~ /defined\s*\($/) {
748a9306 365 $new .= '(defined(&' . $id . ') ? &' . $id . ' : 0)';
ee580363 366 } elsif (/^\[/) {
367 $new .= " \$$id";
368 } else {
748a9306 369 $new .= ' &' . $id;
370 }
154e51a4 371 }
372 next;
373 };
fb21d8eb 374 s/^(.)// && do { if ($1 ne '#') { $new .= $1; } next;};
154e51a4 375 }
376}
50f6e060 377
378
379# Handle recursive subdirectories without getting a grotesquely big stack.
380# Could this be implemented using File::Find?
381sub next_file
382{
383 my $file;
384
385 while (@ARGV) {
386 $file = shift @ARGV;
387
388 if ($file eq '-' or -f $file or -l $file) {
389 return $file;
390 } elsif (-d $file) {
391 if ($opt_r) {
392 expand_glob($file);
393 } else {
394 print STDERR "Skipping directory `$file'\n";
395 }
1d3434b8 396 } elsif ($opt_a) {
397 return $file;
398 } else {
50f6e060 399 print STDERR "Skipping `$file': not a file or directory\n";
400 }
401 }
402
403 return undef;
404}
405
406
407# Put all the files in $directory into @ARGV for processing.
408sub expand_glob
409{
410 my ($directory) = @_;
411
412 $directory =~ s:/$::;
413
414 opendir DIR, $directory;
415 foreach (readdir DIR) {
416 next if ($_ eq '.' or $_ eq '..');
417
418 # expand_glob() is going to be called until $ARGV[0] isn't a
419 # directory; so push directories, and unshift everything else.
1d3434b8 420 if (-d "$directory/$_") { push @ARGV, "$directory/$_" }
421 else { unshift @ARGV, "$directory/$_" }
50f6e060 422 }
423 closedir DIR;
424}
425
426
427# Given $file, a symbolic link to a directory in the C include directory,
428# make an equivalent symbolic link in $Dest_dir, if we can figure out how.
429# Otherwise, just duplicate the file or directory.
430sub link_if_possible
431{
432 my ($dirlink) = @_;
433 my $target = eval 'readlink($dirlink)';
434
435 if ($target =~ m:^\.\./: or $target =~ m:^/:) {
436 # The target of a parent or absolute link could leave the $Dest_dir
437 # hierarchy, so let's put all of the contents of $dirlink (actually,
438 # the contents of $target) into @ARGV; as a side effect down the
439 # line, $dirlink will get created as an _actual_ directory.
440 expand_glob($dirlink);
441 } else {
442 if (-l "$Dest_dir/$dirlink") {
443 unlink "$Dest_dir/$dirlink" or
444 print STDERR "Could not remove link $Dest_dir/$dirlink: $!\n";
445 }
1d3434b8 446
50f6e060 447 if (eval 'symlink($target, "$Dest_dir/$dirlink")') {
448 print "Linking $target -> $Dest_dir/$dirlink\n";
449
450 # Make sure that the link _links_ to something:
451 if (! -e "$Dest_dir/$target") {
1d3434b8 452 mkpath("$Dest_dir/$target", 0755) or
50f6e060 453 print STDERR "Could not create $Dest_dir/$target/\n";
454 }
455 } else {
456 print STDERR "Could not symlink $target -> $Dest_dir/$dirlink: $!\n";
457 }
458 }
459}
460
461
1d3434b8 462# Push all #included files in $file onto our stack, except for STDIN
463# and files we've already processed.
464sub queue_includes_from
465{
466 my ($file) = @_;
467 my $line;
468
469 return if ($file eq "-");
470
471 open HEADER, $file or return;
472 while (defined($line = <HEADER>)) {
473 while (/\\$/) { # Handle continuation lines
474 chop $line;
475 $line .= <HEADER>;
476 }
477
478 if ($line =~ /^#\s*include\s+<(.*?)>/) {
479 push(@ARGV, $1) unless $is_converted{$1};
480 }
481 }
482 close HEADER;
483}
484
485
486# Determine include directories; $Config{usrinc} should be enough for (all
487# non-GCC?) C compilers, but gcc uses an additional include directory.
488sub inc_dirs
489{
490 my $from_gcc = `$Config{cc} -v 2>&1`;
491 $from_gcc =~ s:^Reading specs from (.*?)/specs\b.*:$1/include:s;
492
493 length($from_gcc) ? ($from_gcc, $Config{usrinc}) : ($Config{usrinc});
494}
495
496
50f6e060 4971;
498
154e51a4 499##############################################################################
1fef88e7 500__END__
501
502=head1 NAME
503
504h2ph - convert .h C header files to .ph Perl header files
505
506=head1 SYNOPSIS
507
1d3434b8 508B<h2ph [-d destination directory] [-r | -a] [-l] [headerfiles]>
1fef88e7 509
510=head1 DESCRIPTION
154e51a4 511
1fef88e7 512I<h2ph>
154e51a4 513converts any C header files specified to the corresponding Perl header file
514format.
515It is most easily run while in /usr/include:
154e51a4 516
517 cd /usr/include; h2ph * sys/*
518
50f6e060 519or
520
521 cd /usr/include; h2ph -r -l .
522
b306bf39 523The output files are placed in the hierarchy rooted at Perl's
524architecture dependent library directory. You can specify a different
525hierarchy with a B<-d> switch.
526
fe14fcc3 527If run with no arguments, filters standard input to standard output.
1fef88e7 528
50f6e060 529=head1 OPTIONS
530
531=over 4
532
533=item -d destination_dir
534
535Put the resulting B<.ph> files beneath B<destination_dir>, instead of
536beneath the default Perl library location (C<$Config{'installsitsearch'}>).
537
538=item -r
539
540Run recursively; if any of B<headerfiles> are directories, then run I<h2ph>
1d3434b8 541on all files in those directories (and their subdirectories, etc.). B<-r>
542and B<-a> are mutually exclusive.
543
544=item -a
545
546Run automagically; convert B<headerfiles>, as well as any B<.h> files
547which they include. This option will search for B<.h> files in all
548directories which your C compiler ordinarily uses. B<-a> and B<-r> are
549mutually exclusive.
50f6e060 550
551=item -l
552
553Symbolic links will be replicated in the destination directory. If B<-l>
554is not specified, then links are skipped over.
555
556=item -h
557
558Put ``hints'' in the .ph files which will help in locating problems with
559I<h2ph>. In those cases when you B<require> a B<.ph> file containing syntax
560errors, instead of the cryptic
561
562 [ some error condition ] at (eval mmm) line nnn
563
564you will see the slightly more helpful
565
566 [ some error condition ] at filename.ph line nnn
567
568However, the B<.ph> files almost double in size when built using B<-h>.
569
1d3434b8 570=item -D
571
572Include the code from the B<.h> file as a comment in the B<.ph> file.
573This is primarily used for debugging I<h2ph>.
574
50f6e060 575=back
576
1fef88e7 577=head1 ENVIRONMENT
578
154e51a4 579No environment variables are used.
1fef88e7 580
581=head1 FILES
582
583 /usr/include/*.h
584 /usr/include/sys/*.h
585
154e51a4 586etc.
1fef88e7 587
588=head1 AUTHOR
589
154e51a4 590Larry Wall
1fef88e7 591
592=head1 SEE ALSO
593
154e51a4 594perl(1)
1fef88e7 595
596=head1 DIAGNOSTICS
597
154e51a4 598The usual warnings if it can't read or write the files involved.
1fef88e7 599
600=head1 BUGS
601
154e51a4 602Doesn't construct the %sizeof array for you.
1fef88e7 603
154e51a4 604It doesn't handle all C constructs, but it does attempt to isolate
605definitions inside evals so that you can get at the definitions
606that it can translate.
1fef88e7 607
154e51a4 608It's only intended as a rough tool.
609You may need to dicker with the files produced.
1fef88e7 610
611=cut
612
154e51a4 613!NO!SUBS!
4633a7c4 614
615close OUT or die "Can't close $file: $!";
616chmod 0755, $file or die "Can't reset permissions for $file: $!\n";
617exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';