This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perlunicode: fix for 80 col display
[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 # Make sure read permissions for all are set:
46 if (defined umask && (umask() & 0444)) {
47     umask (umask() & ~0444);
48 }
49
50 getopts('Dd:rlhaQe');
51 use vars qw($opt_D $opt_d $opt_r $opt_l $opt_h $opt_a $opt_Q $opt_e);
52 die "-r and -a options are mutually exclusive\n" if ($opt_r and $opt_a);
53 my @inc_dirs = inc_dirs() if $opt_a;
54
55 my $Exit = 0;
56
57 my $Dest_dir = $opt_d || $Config{installsitearch};
58 die "Destination directory $Dest_dir doesn't exist or isn't a directory\n"
59     unless -d $Dest_dir;
60
61 my @isatype = qw(
62         char    uchar   u_char
63         short   ushort  u_short
64         int     uint    u_int
65         long    ulong   u_long
66         FILE    key_t   caddr_t
67         float   double  size_t
68 );
69
70 my %isatype;
71 @isatype{@isatype} = (1) x @isatype;
72 my $inif = 0;
73 my %Is_converted;
74 my %bad_file = ();
75
76 @ARGV = ('-') unless @ARGV;
77
78 build_preamble_if_necessary();
79
80 sub reindent($) {
81     my($text) = shift;
82     $text =~ s/\n/\n    /g;
83     $text =~ s/        /\t/g;
84     $text;
85 }
86
87 my ($t, $tab, %curargs, $new, $eval_index, $dir, $name, $args, $outfile);
88 my ($incl, $incl_type, $incl_quote, $next);
89 while (defined (my $file = next_file())) {
90     if (-l $file and -d $file) {
91         link_if_possible($file) if ($opt_l);
92         next;
93     }
94
95     # Recover from header files with unbalanced cpp directives
96     $t = '';
97     $tab = 0;
98
99     # $eval_index goes into ``#line'' directives, to help locate syntax errors:
100     $eval_index = 1;
101
102     if ($file eq '-') {
103         open(IN, "-");
104         open(OUT, ">-");
105     } else {
106         ($outfile = $file) =~ s/\.h$/.ph/ || next;
107         print "$file -> $outfile\n" unless $opt_Q;
108         if ($file =~ m|^(.*)/|) {
109             $dir = $1;
110             mkpath "$Dest_dir/$dir";
111         }
112
113         if ($opt_a) { # automagic mode:  locate header file in @inc_dirs
114             foreach (@inc_dirs) {
115                 chdir $_;
116                 last if -f $file;
117             }
118         }
119
120         open(IN,"$file") || (($Exit = 1),(warn "Can't open $file: $!\n"),next);
121         open(OUT,">$Dest_dir/$outfile") || die "Can't create $outfile: $!\n";
122     }
123
124     print OUT
125         "require '_h2ph_pre.ph';\n\n",
126         "no warnings qw(redefine misc);\n\n";
127
128     while (defined (local $_ = next_line($file))) {
129         if (s/^\s*\#\s*//) {
130             if (s/^define\s+(\w+)//) {
131                 $name = $1;
132                 $new = '';
133                 s/\s+$//;
134                 s/\(\w+\s*\(\*\)\s*\(\w*\)\)\s*(-?\d+)/$1/; # (int (*)(foo_t))0
135                 if (s/^\(([\w,\s]*)\)//) {
136                     $args = $1;
137                     my $proto = '() ';
138                     if ($args ne '') {
139                         $proto = '';
140                         foreach my $arg (split(/,\s*/,$args)) {
141                             $arg =~ s/^\s*([^\s].*[^\s])\s*$/$1/;
142                             $curargs{$arg} = 1;
143                         }
144                         $args =~ s/\b(\w)/\$$1/g;
145                         $args = "my($args) = \@_;\n$t    ";
146                     }
147                     s/^\s+//;
148                     expr();
149                     $new =~ s/(["\\])/\\$1/g;       #"]);
150                   EMIT:
151                     $new = reindent($new);
152                     $args = reindent($args);
153                     if ($t ne '') {
154                         $new =~ s/(['\\])/\\$1/g;   #']);
155                         if ($opt_h) {
156                             print OUT $t,
157                             "eval \"\\n#line $eval_index $outfile\\n\" . 'sub $name $proto\{\n$t    ${args}eval q($new);\n$t}' unless defined(\&$name);\n";
158                             $eval_index++;
159                         } else {
160                             print OUT $t,
161                             "eval 'sub $name $proto\{\n$t    ${args}eval q($new);\n$t}' unless defined(\&$name);\n";
162                         }
163                     } else {
164                       print OUT "unless(defined(\&$name)) {\n    sub $name $proto\{\n\t${args}eval q($new);\n    }\n}\n";
165                     }
166                     %curargs = ();
167                 } else {
168                     s/^\s+//;
169                     expr();
170                     $new = 1 if $new eq '';
171                     $new = reindent($new);
172                     $args = reindent($args);
173                     if ($t ne '') {
174                         $new =~ s/(['\\])/\\$1/g;        #']);
175
176                         if ($opt_h) {
177                             print OUT $t,"eval \"\\n#line $eval_index $outfile\\n\" . 'sub $name () {",$new,";}' unless defined(\&$name);\n";
178                             $eval_index++;
179                         } else {
180                             print OUT $t,"eval 'sub $name () {",$new,";}' unless defined(\&$name);\n";
181                         }
182                     } else {
183                         # Shunt around such directives as `#define FOO FOO':
184                         next if " \&$name" eq $new;
185
186                       print OUT $t,"unless(defined(\&$name)) {\n    sub $name () {\t",$new,";}\n}\n";
187                     }
188                 }
189             } elsif (/^(include|import|include_next)\s*([<\"])(.*)[>\"]/) {
190                 $incl_type = $1;
191                 $incl_quote = $2;
192                 $incl = $3;
193                 if (($incl_type eq 'include_next') ||
194                     ($opt_e && exists($bad_file{$incl}))) {
195                     $incl =~ s/\.h$/.ph/;
196                 print OUT ($t,
197                            "eval {\n");
198                 $tab += 4;
199                 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
200                     print OUT ($t, "my(\@REM);\n");
201                     if ($incl_type eq 'include_next') {
202                 print OUT ($t,
203                            "my(\%INCD) = map { \$INC{\$_} => 1 } ",
204                                    "(grep { \$_ eq \"$incl\" } ",
205                                    "keys(\%INC));\n");
206                 print OUT ($t,
207                                    "\@REM = map { \"\$_/$incl\" } ",
208                            "(grep { not exists(\$INCD{\"\$_/$incl\"})",
209                                    " and -f \"\$_/$incl\" } \@INC);\n");
210                     } else {
211                         print OUT ($t,
212                                    "\@REM = map { \"\$_/$incl\" } ",
213                                    "(grep {-r \"\$_/$incl\" } \@INC);\n");
214                     }
215                 print OUT ($t,
216                            "require \"\$REM[0]\" if \@REM;\n");
217                 $tab -= 4;
218                 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
219                 print OUT ($t,
220                            "};\n");
221                 print OUT ($t,
222                            "warn(\$\@) if \$\@;\n");
223                 } else {
224                     $incl =~ s/\.h$/.ph/;
225                     # copy the prefix in the quote syntax (#include "x.h") case
226                     if ($incl !~ m|/| && $incl_quote eq q{"} && $file =~ m|^(.*)/|) {
227                         $incl = "$1/$incl";
228                     }
229                     print OUT $t,"require '$incl';\n";
230                 }
231             } elsif (/^ifdef\s+(\w+)/) {
232                 print OUT $t,"if(defined(&$1)) {\n";
233                 $tab += 4;
234                 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
235             } elsif (/^ifndef\s+(\w+)/) {
236                 print OUT $t,"unless(defined(&$1)) {\n";
237                 $tab += 4;
238                 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
239             } elsif (s/^if\s+//) {
240                 $new = '';
241                 $inif = 1;
242                 expr();
243                 $inif = 0;
244                 print OUT $t,"if($new) {\n";
245                 $tab += 4;
246                 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
247             } elsif (s/^elif\s+//) {
248                 $new = '';
249                 $inif = 1;
250                 expr();
251                 $inif = 0;
252                 $tab -= 4;
253                 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
254                 print OUT $t,"}\n elsif($new) {\n";
255                 $tab += 4;
256                 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
257             } elsif (/^else/) {
258                 $tab -= 4;
259                 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
260                 print OUT $t,"} else {\n";
261                 $tab += 4;
262                 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
263             } elsif (/^endif/) {
264                 $tab -= 4;
265                 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
266                 print OUT $t,"}\n";
267             } elsif(/^undef\s+(\w+)/) {
268                 print OUT $t, "undef(&$1) if defined(&$1);\n";
269             } elsif(/^error\s+(".*")/) {
270                 print OUT $t, "die($1);\n";
271             } elsif(/^error\s+(.*)/) {
272                 print OUT $t, "die(\"", quotemeta($1), "\");\n";
273             } elsif(/^warning\s+(.*)/) {
274                 print OUT $t, "warn(\"", quotemeta($1), "\");\n";
275             } elsif(/^ident\s+(.*)/) {
276                 print OUT $t, "# $1\n";
277             }
278         } elsif (/^\s*(typedef\s*)?enum\s*(\s+[a-zA-Z_]\w*\s*)?/) { # { for vi
279             until(/\{[^}]*\}.*;/ || /;/) {
280                 last unless defined ($next = next_line($file));
281                 chomp $next;
282                 # drop "#define FOO FOO" in enums
283                 $next =~ s/^\s*#\s*define\s+(\w+)\s+\1\s*$//;
284                 # #defines in enums (aliases)
285                 $next =~ s/^\s*#\s*define\s+(\w+)\s+(\w+)\s*$/$1 = $2,/;
286                 $_ .= $next;
287                 print OUT "# $next\n" if $opt_D;
288             }
289             s/#\s*if.*?#\s*endif//g; # drop #ifdefs
290             s@/\*.*?\*/@@g;
291             s/\s+/ /g;
292             next unless /^\s?(typedef\s?)?enum\s?([a-zA-Z_]\w*)?\s?\{(.*)\}\s?([a-zA-Z_]\w*)?\s?;/;
293             (my $enum_subs = $3) =~ s/\s//g;
294             my @enum_subs = split(/,/, $enum_subs);
295             my $enum_val = -1;
296             foreach my $enum (@enum_subs) {
297                 my ($enum_name, $enum_value) = $enum =~ /^([a-zA-Z_]\w*)(=.+)?$/;
298                 $enum_name or next;
299                 $enum_value =~ s/^=//;
300                 $enum_val = (length($enum_value) ? $enum_value : $enum_val + 1);
301                 if ($opt_h) {
302                     print OUT ($t,
303                                "eval(\"\\n#line $eval_index $outfile\\n",
304                                "sub $enum_name () \{ $enum_val; \}\") ",
305                                "unless defined(\&$enum_name);\n");
306                     ++ $eval_index;
307                 } else {
308                     print OUT ($t,
309                                "eval(\"sub $enum_name () \{ $enum_val; \}\") ",
310                                "unless defined(\&$enum_name);\n");
311                 }
312             }
313         } elsif (/^(?:__extension__\s+)?(?:extern|static)\s+(?:__)?inline(?:__)?\s+/
314             and !/;\s*$/ and !/{\s*}\s*$/)
315         { # { for vi
316             # This is a hack to parse the inline functions in the glibc headers.
317             # Warning: massive kludge ahead. We suppose inline functions
318             # are mainly constructed like macros.
319             while (1) {
320                 last unless defined ($next = next_line($file));
321                 chomp $next;
322                 undef $_, last if $next =~ /__THROW\s*;/
323                                or $next =~ /^(__extension__|extern|static)\b/;
324                 $_ .= " $next";
325                 print OUT "# $next\n" if $opt_D;
326                 last if $next =~ /^}|^{.*}\s*$/;
327             }
328             next if not defined; # because it's only a prototype
329             s/\b(__extension__|extern|static|(?:__)?inline(?:__)?)\b//g;
330             # violently drop #ifdefs
331             s/#\s*if.*?#\s*endif//g
332                 and print OUT "# some #ifdef were dropped here -- fill in the blanks\n";
333             if (s/^(?:\w|\s|\*)*\s(\w+)\s*//) {
334                 $name = $1;
335             } else {
336                 warn "name not found"; next; # shouldn't occur...
337             }
338             my @args;
339             if (s/^\(([^()]*)\)\s*(\w+\s*)*//) {
340                 for my $arg (split /,/, $1) {
341                     if ($arg =~ /(\w+)\s*$/) {
342                         $curargs{$1} = 1;
343                         push @args, $1;
344                     }
345                 }
346             }
347             $args = (
348                 @args
349                 ? "my(" . (join ',', map "\$$_", @args) . ") = \@_;\n$t    "
350                 : ""
351             );
352             my $proto = @args ? '' : '() ';
353             $new = '';
354             s/\breturn\b//g; # "return" doesn't occur in macros usually...
355             expr();
356             # try to find and perlify local C variables
357             our @local_variables = (); # needs to be a our(): (?{...}) bug workaround
358             {
359                 use re "eval";
360                 my $typelist = join '|', keys %isatype;
361                 $new =~ s['
362                   (?:(?:__)?const(?:__)?\s+)?
363                   (?:(?:un)?signed\s+)?
364                   (?:long\s+)?
365                   (?:$typelist)\s+
366                   (\w+)
367                   (?{ push @local_variables, $1 })
368                   ']
369                  [my \$$1]gx;
370                 $new =~ s['
371                   (?:(?:__)?const(?:__)?\s+)?
372                   (?:(?:un)?signed\s+)?
373                   (?:long\s+)?
374                   (?:$typelist)\s+
375                   ' \s+ &(\w+) \s* ;
376                   (?{ push @local_variables, $1 })
377                   ]
378                  [my \$$1;]gx;
379              }
380             $new =~ s/&$_\b/\$$_/g for @local_variables;
381             $new =~ s/(["\\])/\\$1/g;       #"]);
382             # now that's almost like a macro (we hope)
383             goto EMIT;
384         }
385     }
386     $Is_converted{$file} = 1;
387     if ($opt_e && exists($bad_file{$file})) {
388         unlink($Dest_dir . '/' . $outfile);
389         $next = '';
390     } else {
391         print OUT "1;\n";
392         queue_includes_from($file) if $opt_a;
393     }
394 }
395
396 if ($opt_e && (scalar(keys %bad_file) > 0)) {
397     warn "Was unable to convert the following files:\n";
398     warn "\t" . join("\n\t",sort(keys %bad_file)) . "\n";
399 }
400
401 exit $Exit;
402
403 sub expr {
404     if (/\b__asm__\b/) {        # freak out
405         $new = '"(assembly code)"';
406         return
407     }
408     my $joined_args;
409     if(keys(%curargs)) {
410         $joined_args = join('|', keys(%curargs));
411     }
412     while ($_ ne '') {
413         s/^\&\&// && do { $new .= " &&"; next;}; # handle && operator
414         s/^\&([\(a-z\)]+)/$1/i; # hack for things that take the address of
415         s/^(\s+)//              && do {$new .= ' '; next;};
416         s/^0X([0-9A-F]+)[UL]*//i
417             && do {my $hex = $1;
418                    $hex =~ s/^0+//;
419                    if (length $hex > 8 && !$Config{use64bitint}) {
420                        # Croak if nv_preserves_uv_bits < 64 ?
421                        $new .=         hex(substr($hex, -8)) +
422                                2**32 * hex(substr($hex,  0, -8));
423                        # The above will produce "errorneus" code
424                        # if the hex constant was e.g. inside UINT64_C
425                        # macro, but then again, h2ph is an approximation.
426                    } else {
427                        $new .= lc("0x$hex");
428                    }
429                    next;};
430         s/^(-?\d+\.\d+E[-+]?\d+)[FL]?//i        && do {$new .= $1; next;};
431         s/^(\d+)\s*[LU]*//i     && do {$new .= $1; next;};
432         s/^("(\\"|[^"])*")//    && do {$new .= $1; next;};
433         s/^'((\\"|[^"])*)'//    && do {
434             if ($curargs{$1}) {
435                 $new .= "ord('\$$1')";
436             } else {
437                 $new .= "ord('$1')";
438             }
439             next;
440         };
441         # replace "sizeof(foo)" with "{foo}"
442         # also, remove * (C dereference operator) to avoid perl syntax
443         # problems.  Where the %sizeof array comes from is anyone's
444         # guess (c2ph?), but this at least avoids fatal syntax errors.
445         # Behavior is undefined if sizeof() delimiters are unbalanced.
446         # This code was modified to able to handle constructs like this:
447         #   sizeof(*(p)), which appear in the HP-UX 10.01 header files.
448         s/^sizeof\s*\(// && do {
449             $new .= '$sizeof';
450             my $lvl = 1;  # already saw one open paren
451             # tack { on the front, and skip it in the loop
452             $_ = "{" . "$_";
453             my $index = 1;
454             # find balanced closing paren
455             while ($index <= length($_) && $lvl > 0) {
456                 $lvl++ if substr($_, $index, 1) eq "(";
457                 $lvl-- if substr($_, $index, 1) eq ")";
458                 $index++;
459             }
460             # tack } on the end, replacing )
461             substr($_, $index - 1, 1) = "}";
462             # remove pesky * operators within the sizeof argument
463             substr($_, 0, $index - 1) =~ s/\*//g;
464             next;
465         };
466         # Eliminate typedefs
467         /\(([\w\s]+)[\*\s]*\)\s*[\w\(]/ && do {
468             my $doit = 1;
469             foreach (split /\s+/, $1) {  # Make sure all the words are types,
470                 unless($isatype{$_} or $_ eq 'struct' or $_ eq 'union'){
471                     $doit = 0;
472                     last;
473                 }
474             }
475             if( $doit ){
476                 s/\([\w\s]+[\*\s]*\)// && next;      # then eliminate them.
477             }
478         };
479         # struct/union member, including arrays:
480         s/^([_A-Z]\w*(\[[^\]]+\])?((\.|->)[_A-Z]\w*(\[[^\]]+\])?)+)//i && do {
481             my $id = $1;
482             $id =~ s/(\.|(->))([^\.\-]*)/->\{$3\}/g;
483             $id =~ s/\b([^\$])($joined_args)/$1\$$2/g if length($joined_args);
484             while($id =~ /\[\s*([^\$\&\d\]]+)\]/) {
485                 my($index) = $1;
486                 $index =~ s/\s//g;
487                 if(exists($curargs{$index})) {
488                     $index = "\$$index";
489                 } else {
490                     $index = "&$index";
491                 }
492                 $id =~ s/\[\s*([^\$\&\d\]]+)\]/[$index]/;
493             }
494             $new .= " (\$$id)";
495         };
496         s/^([_a-zA-Z]\w*)//     && do {
497             my $id = $1;
498             if ($id eq 'struct' || $id eq 'union') {
499                 s/^\s+(\w+)//;
500                 $id .= ' ' . $1;
501                 $isatype{$id} = 1;
502             } elsif ($id =~ /^((un)?signed)|(long)|(short)$/) {
503                 while (s/^\s+(\w+)//) { $id .= ' ' . $1; }
504                 $isatype{$id} = 1;
505             }
506             if ($curargs{$id}) {
507                 $new .= "\$$id";
508                 $new .= '->' if /^[\[\{]/;
509             } elsif ($id eq 'defined') {
510                 $new .= 'defined';
511             } elsif (/^\s*\(/) {
512                 s/^\s*\((\w),/("$1",/ if $id =~ /^_IO[WR]*$/i;  # cheat
513                 $new .= " &$id";
514             } elsif ($isatype{$id}) {
515                 if ($new =~ /\{\s*$/) {
516                     $new .= "'$id'";
517                 } elsif ($new =~ /\(\s*$/ && /^[\s*]*\)/) {
518                     $new =~ s/\(\s*$//;
519                     s/^[\s*]*\)//;
520                 } else {
521                     $new .= q(').$id.q(');
522                 }
523             } else {
524                 if ($inif) {
525                     if ($new =~ /defined\s*$/) {
526                         $new .= '(&' . $id . ')';
527                     } elsif ($new =~ /defined\s*\($/) {
528                         $new .= '&' . $id;
529                     } else {
530                         $new .= '(defined(&' . $id . ') ? &' . $id . ' : undef)';
531                     }
532                 } elsif (/^\[/) {
533                     $new .= " \$$id";
534                 } else {
535                     $new .= ' &' . $id;
536                 }
537             }
538             next;
539         };
540         s/^(.)// && do { if ($1 ne '#') { $new .= $1; } next;};
541     }
542 }
543
544
545 sub next_line
546 {
547     my $file = shift;
548     my ($in, $out);
549     my $pre_sub_tri_graphs = 1;
550
551     READ: while (not eof IN) {
552         $in  .= <IN>;
553         chomp $in;
554         next unless length $in;
555
556         while (length $in) {
557             if ($pre_sub_tri_graphs) {
558                 # Preprocess all tri-graphs
559                 # including things stuck in quoted string constants.
560                 $in =~ s/\?\?=/#/g;                         # | ??=|  #|
561                 $in =~ s/\?\?\!/|/g;                        # | ??!|  ||
562                 $in =~ s/\?\?'/^/g;                         # | ??'|  ^|
563                 $in =~ s/\?\?\(/[/g;                        # | ??(|  [|
564                 $in =~ s/\?\?\)/]/g;                        # | ??)|  ]|
565                 $in =~ s/\?\?\-/~/g;                        # | ??-|  ~|
566                 $in =~ s/\?\?\//\\/g;                       # | ??/|  \|
567                 $in =~ s/\?\?</{/g;                         # | ??<|  {|
568                 $in =~ s/\?\?>/}/g;                         # | ??>|  }|
569             }
570             if ($in =~ /^\#ifdef __LANGUAGE_PASCAL__/) {
571                 # Tru64 disassembler.h evilness: mixed C and Pascal.
572                 while (<IN>) {
573                     last if /^\#endif/;
574                 }
575                 $in = "";
576                 next READ;
577             }
578             if ($in =~ /^extern inline / && # Inlined assembler.
579                 $^O eq 'linux' && $file =~ m!(?:^|/)asm/[^/]+\.h$!) {
580                 while (<IN>) {
581                     last if /^}/;
582                 }
583                 $in = "";
584                 next READ;
585             }
586             if ($in =~ s/\\$//) {                           # \-newline
587                 $out    .= ' ';
588                 next READ;
589             } elsif ($in =~ s/^([^"'\\\/]+)//) {            # Passthrough
590                 $out    .= $1;
591             } elsif ($in =~ s/^(\\.)//) {                   # \...
592                 $out    .= $1;
593             } elsif ($in =~ /^'/) {                         # '...
594                 if ($in =~ s/^('(\\.|[^'\\])*')//) {
595                     $out    .= $1;
596                 } else {
597                     next READ;
598                 }
599             } elsif ($in =~ /^"/) {                         # "...
600                 if ($in =~ s/^("(\\.|[^"\\])*")//) {
601                     $out    .= $1;
602                 } else {
603                     next READ;
604                 }
605             } elsif ($in =~ s/^\/\/.*//) {                  # //...
606                 # fall through
607             } elsif ($in =~ m/^\/\*/) {                     # /*...
608                 # C comment removal adapted from perlfaq6:
609                 if ($in =~ s/^\/\*[^*]*\*+([^\/*][^*]*\*+)*\///) {
610                     $out    .= ' ';
611                 } else {                                    # Incomplete /* */
612                     next READ;
613                 }
614             } elsif ($in =~ s/^(\/)//) {                    # /...
615                 $out    .= $1;
616             } elsif ($in =~ s/^([^\'\"\\\/]+)//) {
617                 $out    .= $1;
618             } elsif ($^O eq 'linux' &&
619                      $file =~ m!(?:^|/)linux/byteorder/pdp_endian\.h$! &&
620                      $in   =~ s!\'T KNOW!!) {
621                 $out    =~ s!I DON$!I_DO_NOT_KNOW!;
622             } else {
623                 if ($opt_e) {
624                     warn "Cannot parse $file:\n$in\n";
625                     $bad_file{$file} = 1;
626                     $in = '';
627                     $out = undef;
628                     last READ;
629                 } else {
630                 die "Cannot parse:\n$in\n";
631                 }
632             }
633         }
634
635         last READ if $out =~ /\S/;
636     }
637
638     return $out;
639 }
640
641
642 # Handle recursive subdirectories without getting a grotesquely big stack.
643 # Could this be implemented using File::Find?
644 sub next_file
645 {
646     my $file;
647
648     while (@ARGV) {
649         $file = shift @ARGV;
650
651         if ($file eq '-' or -f $file or -l $file) {
652             return $file;
653         } elsif (-d $file) {
654             if ($opt_r) {
655                 expand_glob($file);
656             } else {
657                 print STDERR "Skipping directory `$file'\n";
658             }
659         } elsif ($opt_a) {
660             return $file;
661         } else {
662             print STDERR "Skipping `$file':  not a file or directory\n";
663         }
664     }
665
666     return undef;
667 }
668
669
670 # Put all the files in $directory into @ARGV for processing.
671 sub expand_glob
672 {
673     my ($directory)  = @_;
674
675     $directory =~ s:/$::;
676
677     opendir DIR, $directory;
678         foreach (readdir DIR) {
679             next if ($_ eq '.' or $_ eq '..');
680
681             # expand_glob() is going to be called until $ARGV[0] isn't a
682             # directory; so push directories, and unshift everything else.
683             if (-d "$directory/$_") { push    @ARGV, "$directory/$_" }
684             else                    { unshift @ARGV, "$directory/$_" }
685         }
686     closedir DIR;
687 }
688
689
690 # Given $file, a symbolic link to a directory in the C include directory,
691 # make an equivalent symbolic link in $Dest_dir, if we can figure out how.
692 # Otherwise, just duplicate the file or directory.
693 sub link_if_possible
694 {
695     my ($dirlink)  = @_;
696     my $target  = eval 'readlink($dirlink)';
697
698     if ($target =~ m:^\.\./: or $target =~ m:^/:) {
699         # The target of a parent or absolute link could leave the $Dest_dir
700         # hierarchy, so let's put all of the contents of $dirlink (actually,
701         # the contents of $target) into @ARGV; as a side effect down the
702         # line, $dirlink will get created as an _actual_ directory.
703         expand_glob($dirlink);
704     } else {
705         if (-l "$Dest_dir/$dirlink") {
706             unlink "$Dest_dir/$dirlink" or
707                 print STDERR "Could not remove link $Dest_dir/$dirlink:  $!\n";
708         }
709
710         if (eval 'symlink($target, "$Dest_dir/$dirlink")') {
711             print "Linking $target -> $Dest_dir/$dirlink\n";
712
713             # Make sure that the link _links_ to something:
714             if (! -e "$Dest_dir/$target") {
715                 mkpath("$Dest_dir/$target", 0755) or
716                     print STDERR "Could not create $Dest_dir/$target/\n";
717             }
718         } else {
719             print STDERR "Could not symlink $target -> $Dest_dir/$dirlink:  $!\n";
720         }
721     }
722 }
723
724
725 # Push all #included files in $file onto our stack, except for STDIN
726 # and files we've already processed.
727 sub queue_includes_from
728 {
729     my ($file)    = @_;
730     my $line;
731
732     return if ($file eq "-");
733
734     open HEADER, $file or return;
735         while (defined($line = <HEADER>)) {
736             while (/\\$/) { # Handle continuation lines
737                 chop $line;
738                 $line .= <HEADER>;
739             }
740
741             if ($line =~ /^#\s*include\s+([<"])(.*?)[>"]/) {
742                 my ($delimiter, $new_file) = ($1, $2);
743                 # copy the prefix in the quote syntax (#include "x.h") case
744                 if ($delimiter eq q{"} && $file =~ m|^(.*)/|) {
745                     $new_file = "$1/$new_file";
746                 }
747                 push(@ARGV, $new_file) unless $Is_converted{$new_file};
748             }
749         }
750     close HEADER;
751 }
752
753
754 # Determine include directories; $Config{usrinc} should be enough for (all
755 # non-GCC?) C compilers, but gcc uses additional include directories.
756 sub inc_dirs
757 {
758     my $from_gcc    = `LC_ALL=C $Config{cc} -v 2>&1`;
759     if( !( $from_gcc =~ s:^Reading specs from (.*?)/specs\b.*:$1/include:s ) )
760     { # gcc-4+ :
761        $from_gcc   = `LC_ALL=C $Config{cc} -print-search-dirs 2>&1`;
762        if ( !($from_gcc =~ s/^install:\s*([^\s]+[^\s\/])([\s\/]*).*$/$1\/include/s) )
763        {
764            $from_gcc = '';
765        };
766     };
767     length($from_gcc) ? ($from_gcc, $from_gcc . "-fixed", $Config{usrinc}) : ($Config{usrinc});
768 }
769
770
771 # Create "_h2ph_pre.ph", if it doesn't exist or was built by a different
772 # version of h2ph.
773 sub build_preamble_if_necessary
774 {
775     # Increment $VERSION every time this function is modified:
776     my $VERSION     = 3;
777     my $preamble    = "$Dest_dir/_h2ph_pre.ph";
778
779     # Can we skip building the preamble file?
780     if (-r $preamble) {
781         # Extract version number from first line of preamble:
782         open  PREAMBLE, $preamble or die "Cannot open $preamble:  $!";
783             my $line = <PREAMBLE>;
784             $line =~ /(\b\d+\b)/;
785         close PREAMBLE            or die "Cannot close $preamble:  $!";
786
787         # Don't build preamble if a compatible preamble exists:
788         return if $1 == $VERSION;
789     }
790
791     my (%define) = _extract_cc_defines();
792
793     open  PREAMBLE, ">$preamble" or die "Cannot open $preamble:  $!";
794         print PREAMBLE "# This file was created by h2ph version $VERSION\n";
795
796         foreach (sort keys %define) {
797             if ($opt_D) {
798                 print PREAMBLE "# $_=$define{$_}\n";
799             }
800             if ($define{$_} =~ /^\((.*)\)$/) {
801                 # parenthesized value:  d=(v)
802                 $define{$_} = $1;
803             }
804             if (/^(\w+)\((\w)\)$/) {
805                 my($macro, $arg) = ($1, $2);
806                 my $def = $define{$_};
807                 $def =~ s/$arg/\$\{$arg\}/g;
808                 print PREAMBLE <<DEFINE;
809 unless (defined &$macro) { sub $macro(\$) { my (\$$arg) = \@_; \"$def\" } }
810
811 DEFINE
812             } elsif
813                 ($define{$_} =~ /^([+-]?(\d+)?\.\d+([eE][+-]?\d+)?)[FL]?$/) {
814                 # float:
815                 print PREAMBLE
816                     "unless (defined &$_) { sub $_() { $1 } }\n\n";
817             } elsif ($define{$_} =~ /^([+-]?\d+)U?L{0,2}$/i) {
818                 # integer:
819                 print PREAMBLE
820                     "unless (defined &$_) { sub $_() { $1 } }\n\n";
821             } elsif ($define{$_} =~ /^\w+$/) {
822                 my $def = $define{$_};
823                 if ($isatype{$def}) {
824                   print PREAMBLE
825                     "unless (defined &$_) { sub $_() { \"$def\" } }\n\n";
826                 } else {
827                   print PREAMBLE
828                     "unless (defined &$_) { sub $_() { &$def } }\n\n";
829                 }
830             } else {
831                 print PREAMBLE
832                     "unless (defined &$_) { sub $_() { \"",
833                     quotemeta($define{$_}), "\" } }\n\n";
834             }
835         }
836         print PREAMBLE "\n1;\n";  # avoid 'did not return a true value' when empty
837     close PREAMBLE               or die "Cannot close $preamble:  $!";
838 }
839
840
841 # %Config contains information on macros that are pre-defined by the
842 # system's compiler.  We need this information to make the .ph files
843 # function with perl as the .h files do with cc.
844 sub _extract_cc_defines
845 {
846     my %define;
847     my $allsymbols  = join " ",
848         @Config{'ccsymbols', 'cppsymbols', 'cppccsymbols'};
849
850     # Split compiler pre-definitions into `key=value' pairs:
851     while ($allsymbols =~ /([^\s]+)=((\\\s|[^\s])+)/g) {
852         $define{$1} = $2;
853         if ($opt_D) {
854             print STDERR "$_:  $1 -> $2\n";
855         }
856     }
857
858     return %define;
859 }
860
861
862 1;
863
864 ##############################################################################
865 __END__
866
867 =head1 NAME
868
869 h2ph - convert .h C header files to .ph Perl header files
870
871 =head1 SYNOPSIS
872
873 B<h2ph [-d destination directory] [-r | -a] [-l] [headerfiles]>
874
875 =head1 DESCRIPTION
876
877 I<h2ph>
878 converts any C header files specified to the corresponding Perl header file
879 format.
880 It is most easily run while in /usr/include:
881
882         cd /usr/include; h2ph * sys/*
883
884 or
885
886         cd /usr/include; h2ph * sys/* arpa/* netinet/*
887
888 or
889
890         cd /usr/include; h2ph -r -l .
891
892 The output files are placed in the hierarchy rooted at Perl's
893 architecture dependent library directory.  You can specify a different
894 hierarchy with a B<-d> switch.
895
896 If run with no arguments, filters standard input to standard output.
897
898 =head1 OPTIONS
899
900 =over 4
901
902 =item -d destination_dir
903
904 Put the resulting B<.ph> files beneath B<destination_dir>, instead of
905 beneath the default Perl library location (C<$Config{'installsitearch'}>).
906
907 =item -r
908
909 Run recursively; if any of B<headerfiles> are directories, then run I<h2ph>
910 on all files in those directories (and their subdirectories, etc.).  B<-r>
911 and B<-a> are mutually exclusive.
912
913 =item -a
914
915 Run automagically; convert B<headerfiles>, as well as any B<.h> files
916 which they include.  This option will search for B<.h> files in all
917 directories which your C compiler ordinarily uses.  B<-a> and B<-r> are
918 mutually exclusive.
919
920 =item -l
921
922 Symbolic links will be replicated in the destination directory.  If B<-l>
923 is not specified, then links are skipped over.
924
925 =item -h
926
927 Put ``hints'' in the .ph files which will help in locating problems with
928 I<h2ph>.  In those cases when you B<require> a B<.ph> file containing syntax
929 errors, instead of the cryptic
930
931         [ some error condition ] at (eval mmm) line nnn
932
933 you will see the slightly more helpful
934
935         [ some error condition ] at filename.ph line nnn
936
937 However, the B<.ph> files almost double in size when built using B<-h>.
938
939 =item -D
940
941 Include the code from the B<.h> file as a comment in the B<.ph> file.
942 This is primarily used for debugging I<h2ph>.
943
944 =item -Q
945
946 ``Quiet'' mode; don't print out the names of the files being converted.
947
948 =back
949
950 =head1 ENVIRONMENT
951
952 No environment variables are used.
953
954 =head1 FILES
955
956  /usr/include/*.h
957  /usr/include/sys/*.h
958
959 etc.
960
961 =head1 AUTHOR
962
963 Larry Wall
964
965 =head1 SEE ALSO
966
967 perl(1)
968
969 =head1 DIAGNOSTICS
970
971 The usual warnings if it can't read or write the files involved.
972
973 =head1 BUGS
974
975 Doesn't construct the %sizeof array for you.
976
977 It doesn't handle all C constructs, but it does attempt to isolate
978 definitions inside evals so that you can get at the definitions
979 that it can translate.
980
981 It's only intended as a rough tool.
982 You may need to dicker with the files produced.
983
984 You have to run this program by hand; it's not run as part of the Perl
985 installation.
986
987 Doesn't handle complicated expressions built piecemeal, a la:
988
989     enum {
990         FIRST_VALUE,
991         SECOND_VALUE,
992     #ifdef ABC
993         THIRD_VALUE
994     #endif
995     };
996
997 Doesn't necessarily locate all of your C compiler's internally-defined
998 symbols.
999
1000 =cut
1001
1002 !NO!SUBS!
1003
1004 close OUT or die "Can't close $file: $!";
1005 chmod 0755, $file or die "Can't reset permissions for $file: $!\n";
1006 exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';
1007 chdir $origdir;