5 $perlincl = '/usr/local/lib/perl';
7 chdir '/usr/include' || die "Can't cd /usr/include";
9 @isatype = split(' ',<<END);
17 @isatype{@isatype} = (1) x @isatype;
19 @ARGV = ('-') unless @ARGV;
21 foreach $file (@ARGV) {
27 ($outfile = $file) =~ s/\.h$/.ph/ || next;
28 print "$file -> $outfile\n";
29 if ($file =~ m|^(.*)/|) {
31 if (!-d "$perlincl/$dir") {
32 mkdir("$perlincl/$dir",0777);
35 open(IN,"$file") || ((warn "Can't open $file: $!\n"),next);
36 open(OUT,">$perlincl/$outfile") || die "Can't create $outfile: $!\n";
47 s/\200[^\201]*\201//g; # delete single line comments
48 if (s/\200.*//) { # begin multi-line comment?
55 if (s/^define\s+(\w+)//) {
59 if (s/^\(([\w,\s]*)\)//) {
62 foreach $arg (split(/,\s*/,$args)) {
63 $arg =~ s/^\s*([^\s].*[^\s])\s*$/$1/;
66 $args =~ s/\b(\w)/\$$1/g;
67 $args = "local($args) = \@_;\n$t ";
71 $new =~ s/(["\\])/\\$1/g;
73 $new =~ s/(['\\])/\\$1/g;
75 "eval 'sub $name {\n$t ${args}eval \"$new\";\n$t}';\n";
78 print OUT "sub $name {\n ${args}eval \"$new\";\n}\n";
85 $new = 1 if $new eq '';
87 $new =~ s/(['\\])/\\$1/g;
88 print OUT $t,"eval 'sub $name {",$new,";}';\n";
91 print OUT $t,"sub $name {",$new,";}\n";
95 elsif (/^include\s+<(.*)>/) {
96 ($incl = $1) =~ s/\.h$/.ph/;
97 print OUT $t,"require '$incl';\n";
99 elsif (/^ifdef\s+(\w+)/) {
100 print OUT $t,"if (defined &$1) {\n";
102 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
104 elsif (/^ifndef\s+(\w+)/) {
105 print OUT $t,"if (!defined &$1) {\n";
107 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
112 print OUT $t,"if ($new) {\n";
114 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
116 elsif (s/^elif\s+//) {
120 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
121 print OUT $t,"}\n${t}elsif ($new) {\n";
123 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
127 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
128 print OUT $t,"}\n${t}else {\n";
130 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
134 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
144 s/^(\s+)// && do {$new .= ' '; next;};
145 s/^(0x[0-9a-fA-F]+)[lL]?// && do {$new .= $1; next;};
146 s/^(\d+)[lL]?// && do {$new .= $1; next;};
147 s/^("(\\"|[^"])*")// && do {$new .= $1; next;};
148 s/^'((\\"|[^"])*)'// && do {
150 $new .= "ord('\$$1')";
157 s/^sizeof\s*\(([^)]+)\)/{$1}/ && do {
161 s/^([_a-zA-Z]\w*)// && do {
163 if ($id eq 'struct') {
168 elsif ($id eq 'unsigned') {
176 elsif ($id eq 'defined') {
180 s/^\((\w),/("$1",/ if $id =~ /^_IO[WR]*$/i; # cheat
183 elsif ($isatype{$id}) {
184 if ($new =~ /{\s*$/) {
187 elsif ($new =~ /\(\s*$/ && /^[\s*]*\)/) {
200 s/^(.)// && do {$new .= $1; next;};
203 ##############################################################################
205 # These next few lines are legal in both Perl and nroff.
209 'di \" finish diversion--previous line must be blank
210 .nr nl 0-1 \" fake up transition to first page again
211 .nr % 0 \" start at page 1
212 '; __END__ ############# From here on it's a standard manual page ############
213 .TH H2PH 1 "August 8, 1990"
216 h2ph \- convert .h C header files to .ph Perl header files
218 .B h2ph [headerfiles]
221 converts any C header files specified to the corresponding Perl header file
223 It is most easily run while in /usr/include:
226 cd /usr/include; h2ph * sys/*
229 If run with no arguments, filters standard input to standard output.
231 No environment variables are used.
243 The usual warnings if it can't read or write the files involved.
245 Doesn't construct the %sizeof array for you.
247 It doesn't handle all C constructs, but it does attempt to isolate
248 definitions inside evals so that you can get at the definitions
249 that it can translate.
251 It's only intended as a rough tool.
252 You may need to dicker with the files produced.