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