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