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