This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Unresolved symbol in ext/re/re.xs
[perl5.git] / utils / h2ph.PL
1 #!/usr/local/bin/perl
2
3 use Config;
4 use File::Basename qw(basename dirname);
5 use Cwd;
6
7 # List explicitly here the variables you want Configure to
8 # generate.  Metaconfig only looks for shell variables, so you
9 # have to mention them as if they were shell variables, not
10 # %Config entries.  Thus you write
11 #  $startperl
12 # to ensure Configure will look for $Config{startperl}.
13 # Wanted:  $archlibexp
14
15 # This forces PL files to create target in same directory as PL file.
16 # This is so that make depend always knows where to find PL derivatives.
17 $origdir = cwd;
18 chdir dirname($0);
19 $file = basename($0, '.PL');
20 $file .= '.com' if $^O eq 'VMS';
21
22 open OUT,">$file" or die "Can't create $file: $!";
23
24 print "Extracting $file (with variable substitutions)\n";
25
26 # In this section, perl variables will be expanded during extraction.
27 # You can use $Config{...} to use Configure variables.
28
29 print OUT <<"!GROK!THIS!";
30 $Config{startperl}
31     eval 'exec $Config{perlpath} -S \$0 \${1+"\$@"}'
32         if \$running_under_some_shell;
33 !GROK!THIS!
34
35 # In the following, perl variables are not expanded during extraction.
36
37 print OUT <<'!NO!SUBS!';
38
39 use strict;
40
41 use Config;
42 use File::Path qw(mkpath);
43 use Getopt::Std;
44
45 getopts('Dd:rlhaQ');
46 use vars qw($opt_D $opt_d $opt_r $opt_l $opt_h $opt_a $opt_Q);
47 die "-r and -a options are mutually exclusive\n" if ($opt_r and $opt_a);
48 my @inc_dirs = inc_dirs() if $opt_a;
49
50 my $Exit = 0;
51
52 my $Dest_dir = $opt_d || $Config{installsitearch};
53 die "Destination directory $Dest_dir doesn't exist or isn't a directory\n"
54     unless -d $Dest_dir;
55
56 my @isatype = split(' ',<<END);
57         char    uchar   u_char
58         short   ushort  u_short
59         int     uint    u_int
60         long    ulong   u_long
61         FILE    key_t   caddr_t
62 END
63
64 my %isatype;
65 @isatype{@isatype} = (1) x @isatype;
66 my $inif = 0;
67 my %Is_converted;
68
69 @ARGV = ('-') unless @ARGV;
70
71 build_preamble_if_necessary();
72
73 my ($t, $tab, %curargs, $new, $eval_index, $dir, $name, $args, $outfile);
74 my ($incl, $next);
75 while (defined (my $file = next_file())) {
76     if (-l $file and -d $file) {
77         link_if_possible($file) if ($opt_l);
78         next;
79     }
80
81     # Recover from header files with unbalanced cpp directives
82     $t = '';
83     $tab = 0;
84
85     # $eval_index goes into ``#line'' directives, to help locate syntax errors:
86     $eval_index = 1;
87
88     if ($file eq '-') {
89         open(IN, "-");
90         open(OUT, ">-");
91     } else {
92         ($outfile = $file) =~ s/\.h$/.ph/ || next;
93         print "$file -> $outfile\n" unless $opt_Q;
94         if ($file =~ m|^(.*)/|) {
95             $dir = $1;
96             mkpath "$Dest_dir/$dir";
97         }
98
99         if ($opt_a) { # automagic mode:  locate header file in @inc_dirs
100             foreach (@inc_dirs) {
101                 chdir $_;
102                 last if -f $file;
103             }
104         }
105
106         open(IN,"$file") || (($Exit = 1),(warn "Can't open $file: $!\n"),next);
107         open(OUT,">$Dest_dir/$outfile") || die "Can't create $outfile: $!\n";
108     }
109
110     print OUT "require '_h2ph_pre.ph';\n\n";
111     while (defined (local $_ = next_line())) {
112         if (s/^\s*\#\s*//) {
113             if (s/^define\s+(\w+)//) {
114                 $name = $1;
115                 $new = '';
116                 s/\s+$//;
117                 if (s/^\(([\w,\s]*)\)//) {
118                     $args = $1;
119                     my $proto = '() ';
120                     if ($args ne '') {
121                         $proto = '';
122                         foreach my $arg (split(/,\s*/,$args)) {
123                             $arg =~ s/^\s*([^\s].*[^\s])\s*$/$1/;
124                             $curargs{$arg} = 1;
125                         }
126                         $args =~ s/\b(\w)/\$$1/g;
127                         $args = "local($args) = \@_;\n$t    ";
128                     }
129                     s/^\s+//;
130                     expr();
131                     $new =~ s/(["\\])/\\$1/g;       #"]);
132                     $new = reindent($new);
133                     $args = reindent($args);
134                     if ($t ne '') {
135                         $new =~ s/(['\\])/\\$1/g;   #']);
136                         if ($opt_h) {
137                             print OUT $t,
138                             "eval \"\\n#line $eval_index $outfile\\n\" . 'sub $name $proto\{\n$t    ${args}eval q($new);\n$t}' unless defined(\&$name);\n";
139                             $eval_index++;
140                         } else {
141                             print OUT $t,
142                             "eval 'sub $name $proto\{\n$t    ${args}eval q($new);\n$t}' unless defined(\&$name);\n";
143                         }
144                     } else {
145                       print OUT "unless(defined(\&$name)) {\n    sub $name $proto\{\n\t${args}eval q($new);\n    }\n}\n";
146                     }
147                     %curargs = ();
148                 } else {
149                     s/^\s+//;
150                     expr();
151                     $new = 1 if $new eq '';
152                     $new = reindent($new);
153                     $args = reindent($args);
154                     if ($t ne '') {
155                         $new =~ s/(['\\])/\\$1/g;        #']);
156
157                         if ($opt_h) {
158                             print OUT $t,"eval \"\\n#line $eval_index $outfile\\n\" . 'sub $name () {",$new,";}' unless defined(\&$name);\n";
159                             $eval_index++;
160                         } else {
161                             print OUT $t,"eval 'sub $name () {",$new,";}' unless defined(\&$name);\n";
162                         }
163                     } else {
164                         # Shunt around such directives as `#define FOO FOO':
165                         next if " \&$name" eq $new;
166
167                       print OUT $t,"unless(defined(\&$name)) {\n    sub $name () {\t",$new,";}\n}\n";
168                     }
169                 }
170             } elsif (/^(include|import)\s*[<"](.*)[>"]/) {
171                 ($incl = $2) =~ s/\.h$/.ph/;
172                 print OUT $t,"require '$incl';\n";
173             } elsif(/^include_next\s*[<"](.*)[>"]/) {
174                 ($incl = $1) =~ s/\.h$/.ph/;
175                 print OUT ($t,
176                            "eval {\n");
177                 $tab += 4;
178                 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
179                 print OUT ($t,
180                            "my(\%INCD) = map { \$INC{\$_} => 1 } ",
181                            "(grep { \$_ eq \"$incl\" } keys(\%INC));\n");
182                 print OUT ($t,
183                            "my(\@REM) = map { \"\$_/$incl\" } ",
184                            "(grep { not exists(\$INCD{\"\$_/$incl\"})",
185                            "and -f \"\$_/$incl\" } \@INC);\n");
186                 print OUT ($t,
187                            "require \"\$REM[0]\" if \@REM;\n");
188                 $tab -= 4;
189                 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
190                 print OUT ($t,
191                            "};\n");
192                 print OUT ($t,
193                            "warn(\$\@) if \$\@;\n");
194             } elsif (/^ifdef\s+(\w+)/) {
195                 print OUT $t,"if(defined(&$1)) {\n";
196                 $tab += 4;
197                 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
198             } elsif (/^ifndef\s+(\w+)/) {
199                 print OUT $t,"unless(defined(&$1)) {\n";
200                 $tab += 4;
201                 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
202             } elsif (s/^if\s+//) {
203                 $new = '';
204                 $inif = 1;
205                 expr();
206                 $inif = 0;
207                 print OUT $t,"if($new) {\n";
208                 $tab += 4;
209                 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
210             } elsif (s/^elif\s+//) {
211                 $new = '';
212                 $inif = 1;
213                 expr();
214                 $inif = 0;
215                 $tab -= 4;
216                 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
217                 print OUT $t,"}\n elsif($new) {\n";
218                 $tab += 4;
219                 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
220             } elsif (/^else/) {
221                 $tab -= 4;
222                 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
223                 print OUT $t,"} else {\n";
224                 $tab += 4;
225                 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
226             } elsif (/^endif/) {
227                 $tab -= 4;
228                 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
229                 print OUT $t,"}\n";
230             } elsif(/^undef\s+(\w+)/) {
231                 print OUT $t, "undef(&$1) if defined(&$1);\n";
232             } elsif(/^error\s+(".*")/) {
233                 print OUT $t, "die($1);\n";
234             } elsif(/^error\s+(.*)/) {
235                 print OUT $t, "die(\"", quotemeta($1), "\");\n";
236             } elsif(/^warning\s+(.*)/) {
237                 print OUT $t, "warn(\"", quotemeta($1), "\");\n";
238             } elsif(/^ident\s+(.*)/) {
239                 print OUT $t, "# $1\n";
240             }
241         } elsif(/^\s*(typedef\s*)?enum\s*(\s+[a-zA-Z_]\w*\s*)?\{/) {
242             until(/\}.*?;/) {
243                 chomp($next = <IN>);
244                 $_ .= $next;
245                 print OUT "# $next\n" if $opt_D;
246             }
247             s@/\*.*?\*/@@g;
248             s/\s+/ /g;
249             /^\s?(typedef\s?)?enum\s?([a-zA-Z_]\w*)?\s?\{(.*)\}\s?([a-zA-Z_]\w*)?\s?;/;
250             (my $enum_subs = $3) =~ s/\s//g;
251             my @enum_subs = split(/,/, $enum_subs);
252             my $enum_val = -1;
253             foreach my $enum (@enum_subs) {
254                 my ($enum_name, $enum_value) = $enum =~ /^([a-zA-Z_]\w*)(=.+)?$/;
255                 $enum_value =~ s/^=//;
256                 $enum_val = (length($enum_value) ? $enum_value : $enum_val + 1);
257                 if ($opt_h) {
258                     print OUT ($t,
259                                "eval(\"\\n#line $eval_index $outfile\\n",
260                                "sub $enum_name () \{ $enum_val; \}\") ",
261                                "unless defined(\&$enum_name);\n");
262                     ++ $eval_index;
263                 } else {
264                     print OUT ($t,
265                                "eval(\"sub $enum_name () \{ $enum_val; \}\") ",
266                                "unless defined(\&$enum_name);\n");
267                 }
268             }
269         }
270     }
271     print OUT "1;\n";
272
273     $Is_converted{$file} = 1;
274     queue_includes_from($file) if ($opt_a);
275 }
276
277 exit $Exit;
278
279
280 sub reindent($) {
281     my($text) = shift;
282     $text =~ s/\n/\n    /g;
283     $text =~ s/        /\t/g;
284     $text;
285 }
286
287
288 sub expr {
289     my $joined_args;
290     if(keys(%curargs)) {
291         $joined_args = join('|', keys(%curargs));
292     }
293     while ($_ ne '') {
294         s/^\&\&// && do { $new .= " &&"; next;}; # handle && operator
295         s/^\&([\(a-z\)]+)/$1/i; # hack for things that take the address of
296         s/^(\s+)//              && do {$new .= ' '; next;};
297         s/^(0X[0-9A-F]+)[UL]*//i        && do {$new .= lc($1); next;};
298         s/^(-?\d+\.\d+E[-+]\d+)F?//i    && do {$new .= $1; next;};
299         s/^(\d+)\s*[LU]*//i     && do {$new .= $1; next;};
300         s/^("(\\"|[^"])*")//    && do {$new .= $1; next;};
301         s/^'((\\"|[^"])*)'//    && do {
302             if ($curargs{$1}) {
303                 $new .= "ord('\$$1')";
304             } else {
305                 $new .= "ord('$1')";
306             }
307             next;
308         };
309         # replace "sizeof(foo)" with "{foo}"
310         # also, remove * (C dereference operator) to avoid perl syntax
311         # problems.  Where the %sizeof array comes from is anyone's
312         # guess (c2ph?), but this at least avoids fatal syntax errors.
313         # Behavior is undefined if sizeof() delimiters are unbalanced.
314         # This code was modified to able to handle constructs like this:
315         #   sizeof(*(p)), which appear in the HP-UX 10.01 header files.
316         s/^sizeof\s*\(// && do {
317             $new .= '$sizeof';
318             my $lvl = 1;  # already saw one open paren
319             # tack { on the front, and skip it in the loop
320             $_ = "{" . "$_";
321             my $index = 1;
322             # find balanced closing paren
323             while ($index <= length($_) && $lvl > 0) {
324                 $lvl++ if substr($_, $index, 1) eq "(";
325                 $lvl-- if substr($_, $index, 1) eq ")";
326                 $index++;
327             }
328             # tack } on the end, replacing )
329             substr($_, $index - 1, 1) = "}";
330             # remove pesky * operators within the sizeof argument
331             substr($_, 0, $index - 1) =~ s/\*//g;
332             next;
333         };
334         # Eliminate typedefs
335         /\(([\w\s]+)[\*\s]*\)\s*[\w\(]/ && do {
336             foreach (split /\s+/, $1) {  # Make sure all the words are types,
337                 last unless ($isatype{$_} or $_ eq 'struct');
338             }
339             s/\([\w\s]+[\*\s]*\)// && next;      # then eliminate them.
340         };
341         # struct/union member, including arrays:
342         s/^([_A-Z]\w*(\[[^\]]+\])?((\.|->)[_A-Z]\w*(\[[^\]]+\])?)+)//i && do {
343             my $id = $1;
344             $id =~ s/(\.|(->))([^\.\-]*)/->\{$3\}/g;
345             $id =~ s/\b([^\$])($joined_args)/$1\$$2/g if length($joined_args);
346             while($id =~ /\[\s*([^\$\&\d\]]+)\]/) {
347                 my($index) = $1;
348                 $index =~ s/\s//g;
349                 if(exists($curargs{$index})) {
350                     $index = "\$$index";
351                 } else {
352                     $index = "&$index";
353                 }
354                 $id =~ s/\[\s*([^\$\&\d\]]+)\]/[$index]/;
355             }
356             $new .= " (\$$id)";
357         };
358         s/^([_a-zA-Z]\w*)//     && do {
359             my $id = $1;
360             if ($id eq 'struct') {
361                 s/^\s+(\w+)//;
362                 $id .= ' ' . $1;
363                 $isatype{$id} = 1;
364             } elsif ($id =~ /^((un)?signed)|(long)|(short)$/) {
365                 while (s/^\s+(\w+)//) { $id .= ' ' . $1; }
366                 $isatype{$id} = 1;
367             }
368             if ($curargs{$id}) {
369                 $new .= "\$$id";
370                 $new .= '->' if /^[\[\{]/;
371             } elsif ($id eq 'defined') {
372                 $new .= 'defined';
373             } elsif (/^\s*\(/) {
374                 s/^\s*\((\w),/("$1",/ if $id =~ /^_IO[WR]*$/i;  # cheat
375                 $new .= " &$id";
376             } elsif ($isatype{$id}) {
377                 if ($new =~ /{\s*$/) {
378                     $new .= "'$id'";
379                 } elsif ($new =~ /\(\s*$/ && /^[\s*]*\)/) {
380                     $new =~ s/\(\s*$//;
381                     s/^[\s*]*\)//;
382                 } else {
383                     $new .= q(').$id.q(');
384                 }
385             } else {
386                 if ($inif && $new !~ /defined\s*\($/) {
387                     $new .= '(defined(&' . $id . ') ? &' . $id . ' : 0)';
388                 } elsif (/^\[/) {
389                     $new .= " \$$id";
390                 } else {
391                     $new .= ' &' . $id;
392                 }
393             }
394             next;
395         };
396         s/^(.)// && do { if ($1 ne '#') { $new .= $1; } next;};
397     }
398 }
399
400
401 sub next_line
402 {
403     my ($in, $out);
404
405     READ: while (not eof IN) {
406         $in  .= <IN>;
407         chomp $in;
408         next unless length $in;
409
410         while (length $in) {
411             if ($in =~ s/\\$//) {                           # \-newline
412                 $out    .= ' ';
413                 next READ;
414             } elsif ($in =~ s/^([^"'\\\/]+)//) {            # Passthrough
415                 $out    .= $1;
416             } elsif ($in =~ s/^(\\.)//) {                   # \...
417                 $out    .= $1;
418             } elsif ($in =~ s/^('(\\.|[^'\\])*')//) {       # '...
419                 $out    .= $1;
420             } elsif ($in =~ s/^("(\\.|[^"\\])*")//) {       # "...
421                 $out    .= $1;
422             } elsif ($in =~ s/^\/\/.*//) {                  # //...
423                 last READ;
424             } elsif ($in =~ m/^\/\*/) {                     # /*...
425                 # C comment removal adapted from perlfaq6:
426                 if ($in =~ s/^\/\*[^*]*\*+([^\/*][^*]*\*+)*\///) {
427                     $out    .= ' ';
428                 } else {                                    # Incomplete /* */
429                     next READ;
430                 }
431             } elsif ($in =~ s/^(\/)//) {                    # /...
432                 $out    .= $1;
433             } elsif ($in =~ s/^([^\'\"\\\/]+)//) {
434                 $out    .= $1;
435             } else {
436                 die "Cannot parse:\n$in\n";
437             }
438         }
439
440         last READ;
441     }
442
443     return $out;
444 }
445
446
447 # Handle recursive subdirectories without getting a grotesquely big stack.
448 # Could this be implemented using File::Find?
449 sub next_file
450 {
451     my $file;
452
453     while (@ARGV) {
454         $file = shift @ARGV;
455
456         if ($file eq '-' or -f $file or -l $file) {
457             return $file;
458         } elsif (-d $file) {
459             if ($opt_r) {
460                 expand_glob($file);
461             } else {
462                 print STDERR "Skipping directory `$file'\n";
463             }
464         } elsif ($opt_a) {
465             return $file;
466         } else {
467             print STDERR "Skipping `$file':  not a file or directory\n";
468         }
469     }
470
471     return undef;
472 }
473
474
475 # Put all the files in $directory into @ARGV for processing.
476 sub expand_glob
477 {
478     my ($directory)  = @_;
479
480     $directory =~ s:/$::;
481
482     opendir DIR, $directory;
483         foreach (readdir DIR) {
484             next if ($_ eq '.' or $_ eq '..');
485
486             # expand_glob() is going to be called until $ARGV[0] isn't a
487             # directory; so push directories, and unshift everything else.
488             if (-d "$directory/$_") { push    @ARGV, "$directory/$_" }
489             else                    { unshift @ARGV, "$directory/$_" }
490         }
491     closedir DIR;
492 }
493
494
495 # Given $file, a symbolic link to a directory in the C include directory,
496 # make an equivalent symbolic link in $Dest_dir, if we can figure out how.
497 # Otherwise, just duplicate the file or directory.
498 sub link_if_possible
499 {
500     my ($dirlink)  = @_;
501     my $target  = eval 'readlink($dirlink)';
502
503     if ($target =~ m:^\.\./: or $target =~ m:^/:) {
504         # The target of a parent or absolute link could leave the $Dest_dir
505         # hierarchy, so let's put all of the contents of $dirlink (actually,
506         # the contents of $target) into @ARGV; as a side effect down the
507         # line, $dirlink will get created as an _actual_ directory.
508         expand_glob($dirlink);
509     } else {
510         if (-l "$Dest_dir/$dirlink") {
511             unlink "$Dest_dir/$dirlink" or
512                 print STDERR "Could not remove link $Dest_dir/$dirlink:  $!\n";
513         }
514
515         if (eval 'symlink($target, "$Dest_dir/$dirlink")') {
516             print "Linking $target -> $Dest_dir/$dirlink\n";
517
518             # Make sure that the link _links_ to something:
519             if (! -e "$Dest_dir/$target") {
520                 mkpath("$Dest_dir/$target", 0755) or
521                     print STDERR "Could not create $Dest_dir/$target/\n";
522             }
523         } else {
524             print STDERR "Could not symlink $target -> $Dest_dir/$dirlink:  $!\n";
525         }
526     }
527 }
528
529
530 # Push all #included files in $file onto our stack, except for STDIN
531 # and files we've already processed.
532 sub queue_includes_from
533 {
534     my ($file)    = @_;
535     my $line;
536
537     return if ($file eq "-");
538
539     open HEADER, $file or return;
540         while (defined($line = <HEADER>)) {
541             while (/\\$/) { # Handle continuation lines
542                 chop $line;
543                 $line .= <HEADER>;
544             }
545
546             if ($line =~ /^#\s*include\s+<(.*?)>/) {
547                 push(@ARGV, $1) unless $Is_converted{$1};
548             }
549         }
550     close HEADER;
551 }
552
553
554 # Determine include directories; $Config{usrinc} should be enough for (all
555 # non-GCC?) C compilers, but gcc uses an additional include directory.
556 sub inc_dirs
557 {
558     my $from_gcc    = `$Config{cc} -v 2>&1`;
559     $from_gcc       =~ s:^Reading specs from (.*?)/specs\b.*:$1/include:s;
560
561     length($from_gcc) ? ($from_gcc, $Config{usrinc}) : ($Config{usrinc});
562 }
563
564
565 # Create "_h2ph_pre.ph", if it doesn't exist or was built by a different
566 # version of h2ph.
567 sub build_preamble_if_necessary
568 {
569     # Increment $VERSION every time this function is modified:
570     my $VERSION     = 2;
571     my $preamble    = "$Dest_dir/_h2ph_pre.ph";
572
573     # Can we skip building the preamble file?
574     if (-r $preamble) {
575         # Extract version number from first line of preamble:
576         open  PREAMBLE, $preamble or die "Cannot open $preamble:  $!";
577             my $line = <PREAMBLE>;
578             $line =~ /(\b\d+\b)/;
579         close PREAMBLE            or die "Cannot close $preamble:  $!";
580
581         # Don't build preamble if a compatible preamble exists:
582         return if $1 == $VERSION;
583     }
584
585     my (%define) = _extract_cc_defines();
586
587     open  PREAMBLE, ">$preamble" or die "Cannot open $preamble:  $!";
588         print PREAMBLE "# This file was created by h2ph version $VERSION\n";
589
590         foreach (sort keys %define) {
591             if ($opt_D) {
592                 print PREAMBLE "# $_=$define{$_}\n";
593             }
594
595             if ($define{$_} =~ /^\d+$/) {
596                 print PREAMBLE
597                     "unless (defined &$_) { sub $_() { $define{$_} } }\n\n";
598             } elsif ($define{$_} =~ /^\w+$/) {
599                 print PREAMBLE
600                     "unless (defined &$_) { sub $_() { &$define{$_} } }\n\n";
601             } else {
602                 print PREAMBLE
603                     "unless (defined &$_) { sub $_() { \"",
604                     quotemeta($define{$_}), "\" } }\n\n";
605             }
606         }
607     close PREAMBLE               or die "Cannot close $preamble:  $!";
608 }
609
610
611 # %Config contains information on macros that are pre-defined by the
612 # system's compiler.  We need this information to make the .ph files
613 # function with perl as the .h files do with cc.
614 sub _extract_cc_defines
615 {
616     my %define;
617     my $allsymbols  = join " ",
618         @Config{'ccsymbols', 'cppsymbols', 'cppccsymbols'};
619
620     # Split compiler pre-definitions into `key=value' pairs:
621     foreach (split /\s+/, $allsymbols) {
622         /(.+?)=(.+)/ and $define{$1} = $2;
623
624         if ($opt_D) {
625             print STDERR "$_:  $1 -> $2\n";
626         }
627     }
628
629     return %define;
630 }
631
632
633 1;
634
635 ##############################################################################
636 __END__
637
638 =head1 NAME
639
640 h2ph - convert .h C header files to .ph Perl header files
641
642 =head1 SYNOPSIS
643
644 B<h2ph [-d destination directory] [-r | -a] [-l] [headerfiles]>
645
646 =head1 DESCRIPTION
647
648 I<h2ph>
649 converts any C header files specified to the corresponding Perl header file
650 format.
651 It is most easily run while in /usr/include:
652
653         cd /usr/include; h2ph * sys/*
654
655 or
656
657         cd /usr/include; h2ph -r -l .
658
659 The output files are placed in the hierarchy rooted at Perl's
660 architecture dependent library directory.  You can specify a different
661 hierarchy with a B<-d> switch.
662
663 If run with no arguments, filters standard input to standard output.
664
665 =head1 OPTIONS
666
667 =over 4
668
669 =item -d destination_dir
670
671 Put the resulting B<.ph> files beneath B<destination_dir>, instead of
672 beneath the default Perl library location (C<$Config{'installsitsearch'}>).
673
674 =item -r
675
676 Run recursively; if any of B<headerfiles> are directories, then run I<h2ph>
677 on all files in those directories (and their subdirectories, etc.).  B<-r>
678 and B<-a> are mutually exclusive.
679
680 =item -a
681
682 Run automagically; convert B<headerfiles>, as well as any B<.h> files
683 which they include.  This option will search for B<.h> files in all
684 directories which your C compiler ordinarily uses.  B<-a> and B<-r> are
685 mutually exclusive.
686
687 =item -l
688
689 Symbolic links will be replicated in the destination directory.  If B<-l>
690 is not specified, then links are skipped over.
691
692 =item -h
693
694 Put ``hints'' in the .ph files which will help in locating problems with
695 I<h2ph>.  In those cases when you B<require> a B<.ph> file containing syntax
696 errors, instead of the cryptic
697
698         [ some error condition ] at (eval mmm) line nnn
699
700 you will see the slightly more helpful
701
702         [ some error condition ] at filename.ph line nnn
703
704 However, the B<.ph> files almost double in size when built using B<-h>.
705
706 =item -D
707
708 Include the code from the B<.h> file as a comment in the B<.ph> file.
709 This is primarily used for debugging I<h2ph>.
710
711 =item -Q
712
713 ``Quiet'' mode; don't print out the names of the files being converted.
714
715 =back
716
717 =head1 ENVIRONMENT
718
719 No environment variables are used.
720
721 =head1 FILES
722
723  /usr/include/*.h
724  /usr/include/sys/*.h
725
726 etc.
727
728 =head1 AUTHOR
729
730 Larry Wall
731
732 =head1 SEE ALSO
733
734 perl(1)
735
736 =head1 DIAGNOSTICS
737
738 The usual warnings if it can't read or write the files involved.
739
740 =head1 BUGS
741
742 Doesn't construct the %sizeof array for you.
743
744 It doesn't handle all C constructs, but it does attempt to isolate
745 definitions inside evals so that you can get at the definitions
746 that it can translate.
747
748 It's only intended as a rough tool.
749 You may need to dicker with the files produced.
750
751 You have to run this program by hand; it's not run as part of the Perl
752 installation.
753
754 Doesn't handle complicated expressions built piecemeal, a la:
755
756     enum {
757         FIRST_VALUE,
758         SECOND_VALUE,
759     #ifdef ABC
760         THIRD_VALUE
761     #endif
762     };
763
764 Doesn't necessarily locate all of your C compiler's internally-defined
765 symbols.
766
767 =cut
768
769 !NO!SUBS!
770
771 close OUT or die "Can't close $file: $!";
772 chmod 0755, $file or die "Can't reset permissions for $file: $!\n";
773 exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';
774 chdir $origdir;