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