/
hparse.pl
executable file
·220 lines (174 loc) · 4.73 KB
/
hparse.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/perl
use strict;
# See https://rt.cpan.org/Public/Bug/Display.html?id=57341
use Getopt::Long;
use GCC::TranslationUnit;
use C::DynaLib::Parse qw(pack_types process_struct process_func);
my ($cc, $inc, $code, $file, $type);
GetOptions( "cc=s" => \$cc,
"I=s" => \$inc,
"code=s" => \$code,
"file=s", => \$file,
"t=s" => \$type );
$inc = "-I$inc" if $inc;
sub declare_func {
my $decl = shift;
if ($type eq 'c_dynalib') {
print "DeclareSub($decl->{name}, ",
pack_types($decl->{retn}),", ",
pack_types($decl->{parms}),");\n";
} else {
my $parms = join ", ", @{$decl->{parms}};
$parms =~ s/, $//;
print "$decl->{retn} $decl->{name}( $parms );\n";
}
}
sub declare_struct {
my $decl = shift;
if ($type eq 'c_dynalib') {
my $names = join '","', @{$decl->{names}};
print qq(Define C::DynaLib::Struct("$decl->{name}",
"$decl->{packnames}",
["$names"}]);
);
} else {
print "\n$decl->{type} $decl->{name} {";
for (0..@$decl->{names}) {
print $decl->{types}->[$_];
print "\t",$decl->{names}->[$_];
print ":",$decl->{sizees}->[$_],"\n";
}
print "}\n;"
}
}
my @post;
my %records;
my $filter;
my $header = shift;
if ($type) {
my %FFIs = map { $_ => 1 } qw( C::DynaLib FFI Win32::API P5NCI CTypes );
die "Error: Unknown -t $type. Valid: ".join(", ",keys(%FFIs)) ."\n"
unless $FFIs{$type};
print "use $type;\n";
print "# generated by hparse.pl $header;\n";
$type =~ s/::/_/g;
$type = lc($type); # c_dynalib ffi win32_api p5nci ctypes
}
my $node;
# .h or .hpp or .hh
$header .= ".h" if !$file and !$code and $header and $header !~ /\.h/;
if ($header and $header =~ /\.h(h|pp)$/ and !$cc) {
# for c++ support with newer g++ see
# https://rt.cpan.org/Public/Bug/Display.html?id=57349 (v1.01)
$cc = "g++";
}
$cc = 'gcc' unless $cc; # check Config?
if ($code) {
$filter = $header;
$node = C::DynaLib::Parse::GCC_prepare($code, "$cc $inc");
} elsif ($file) {
$filter = $header;
$node = C::DynaLib::Parse::GCC_prepare("#include \"$file\"\n", "$cc $inc");
} else {
$header = "stdlib.h" unless $header;
$filter = shift;
$node = C::DynaLib::Parse::GCC_prepare("#include <$header>\n", "$cc $inc");
}
while ($node) {
if ($node->isa('GCC::Node::function_decl')
and ($filter ? $node->name->identifier =~ /$filter/
: $node->name->identifier !~ /^_/))
{
declare_func process_func($node);
}
if ($node->isa('GCC::Node::record_type')
and ($filter ? $node->name->identifier =~ /$filter/
: $node->name->identifier !~ /^_/))
{
declare_struct process_struct($node);
}
} continue {
$node = $node->chain;
}
POST:
while ($node = shift @C::DynaLib::Parse::post) {
#print "\n(", ref $node, ")";
if ($node->isa('GCC::Node::record_type')) {
declare_struct process_struct($node);
}
}
__END__
=pod
=head1 NAME
hparse
=head1 DESCRIPTION
Parse function signatures for FFI from gcc4 -fdump-translation-unit
Also parses record types (union, struct) if used as arguments
of the used functions.
Note that the output should be compiler independent. So you CAN use
gcc for creating FFI signatures for shared libraries compiled with
other compilers. Theoretically.
=head1 SYNOPSIS
hparse.pl [OPTIONS] [header] [function-regex]
hparse.pl stdio.h '^fprintf$'
hparse.pl --code "int __cdecl ioctl (int __fd, int __cmd, ...)"
=head1 OPTIONS
-t FFI-TYPE - dump in the given FFI format (todo)
--cc gcc - use given gcc
-I - use given include path
--code string - parse string, not any header
--file file - parse file, not any header
=head2 FFI-TYPES (todo)
* C::DynaLib
* FFI
* Win32::API
* P5NCI
* Ctypes
=head1 EXAMPLES
=head2 hparse.pl stdio.h '^fr'
frexp
return=double
parms=double, *int, void
align=8, return-align=64
frexpf
return=float
parms=float, *int, void
align=8, return-align=32
frexpl
return=long double
parms=long double, *int, void
align=8, return-align=32
free
return=void
parms=*void, void
align=8, return-align=8
freopen
return=*FILE
parms=const *char, const *char, *FILE, void
align=8, return-align=32
fread
return=size_t
parms=*void, size_t, size_t, *FILE, void
align=8, return-align=32
=head2 hparse.pl poll.h 'poll'
poll
return=int
parms=struct pollfd *, nfds_t, int, void
align=8, return-align=32
struct pollfd (align=32)
{
int fd (align=32)
short int events (align=16)
short int revents (align=16)
}
=head1 TODO
Resolve size_t, nfds_t => integer_type
Calling convention _stdcall, _cdecl, _fastcall
Align syntax for the FFI's?
Varargs ... not detected
./hparse.pl --code "int __cdecl ioctl (int __fd, int __cmd, ...);" ioctl
ioctl
return=int
parms=int, int
align=8, return-align=32
=cut