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