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