5 # Uncomment if debug is needed
8 # change to 1 to generate some debug prints
11 if (scalar @ARGV < 2 || scalar @ARGV > 3) {
12 die "Usage:\n\t$0 <file in> <file out> [<exceptions file>]\n";
15 my ($file_in, $file_out, $file_exceptions) = @ARGV;
26 # read the file and get identifiers
31 open IN
, $file_in or die "Can't open $file_in";
37 $ln =~ s
,/\*.*(\*/),,g
;
39 $is_comment = 1 if ($ln =~ s
,/\
*.*,,);
41 if ($ln =~ s
,^(.*\
*/),,) {
48 if ($is_enum && $ln =~ m/^\s*([_\w][\w\d_]+)\s*[\,=]?/) {
54 $enum_symbols{$s} = $n;
56 $is_enum = 0 if ($is_enum && m/\}/);
59 $is_enum = 0 if ($is_enum && m/\}/);
61 if ($ln =~ m/^\s*#\s*define\s+([_\w][\w\d_]+)\s+_IO/) {
70 if ($ln =~ m/^\s*#\s*define\s+([_\w][\w\d_]+)\s+/) {
80 if ($ln =~ m/^\s*typedef\s+.*\s+([_\w][\w\d_]+);/) {
89 if ($ln =~ m/^\s*enum\s+([_\w][\w\d_]+)\s+\{/
90 || $ln =~ m/^\s*enum\s+([_\w][\w\d_]+)$/
91 || $ln =~ m/^\s*typedef\s*enum\s+([_\w][\w\d_]+)\s+\{/
92 || $ln =~ m/^\s*typedef\s*enum\s+([_\w][\w\d_]+)$/) {
103 if ($ln =~ m/^\s*struct\s+([_\w][\w\d_]+)\s+\{/
104 || $ln =~ m/^\s*struct\s+([[_\w][\w\d_]+)$/
105 || $ln =~ m/^\s*typedef\s*struct\s+([_\w][\w\d_]+)\s+\{/
106 || $ln =~ m/^\s*typedef\s*struct\s+([[_\w][\w\d_]+)$/
120 # Handle multi-line typedefs
123 my @matches = ($data =~ m/typedef\s+struct\s+\S+?\s*\{[^\}]+\}\s*(\S+)\s*\;/g,
124 $data =~ m/typedef\s+enum\s+\S+?\s*\{[^\}]+\}\s*(\S+)\s*\;/g,);
125 foreach my $m (@matches) {
136 # Handle exceptions, if any
139 if ($file_exceptions) {
140 open IN
, $file_exceptions or die "Can't read $file_exceptions";
142 next if (m/^\s*$/ || m/^\s*#/);
144 # Parsers to ignore a symbol
146 if (m/^ignore\s+ioctl\s+(\S+)/) {
147 delete $ioctls{$1} if (exists($ioctls{$1}));
150 if (m/^ignore\s+define\s+(\S+)/) {
151 delete $defines{$1} if (exists($defines{$1}));
154 if (m/^ignore\s+typedef\s+(\S+)/) {
155 delete $typedefs{$1} if (exists($typedefs{$1}));
158 if (m/^ignore\s+enum\s+(\S+)/) {
159 delete $enums{$1} if (exists($enums{$1}));
162 if (m/^ignore\s+struct\s+(\S+)/) {
163 delete $structs{$1} if (exists($structs{$1}));
167 # Parsers to replace a symbol
169 if (m/^replace\s+ioctl\s+(\S+)\s+(\S+)/) {
170 $ioctls{$1} = $2 if (exists($ioctls{$1}));
173 if (m/^replace\s+define\s+(\S+)\s+(\S+)/) {
174 $defines{$1} = $2 if (exists($defines{$1}));
177 if (m/^replace\s+typedef\s+(\S+)\s+(\S+)/) {
178 $typedefs{$1} = $2 if (exists($typedefs{$1}));
181 if (m/^replace\s+enum\s+(\S+)\s+(\S+)/) {
182 $enums{$1} = $2 if (exists($enums{$1}));
185 if (m/^replace\s+symbol\s+(\S+)\s+(\S+)/) {
186 $enum_symbols{$1} = $2 if (exists($enum_symbols{$1}));
189 if (m/^replace\s+struct\s+(\S+)\s+(\S+)/) {
190 $structs{$1} = $2 if (exists($structs{$1}));
194 die "Can't parse $file_exceptions: $_";
199 print Data
::Dumper
->Dump([\
%ioctls], [qw(*ioctls)]) if (%ioctls);
200 print Data
::Dumper
->Dump([\
%typedefs], [qw(*typedefs)]) if (%typedefs);
201 print Data
::Dumper
->Dump([\
%enums], [qw(*enums)]) if (%enums);
202 print Data
::Dumper
->Dump([\
%structs], [qw(*structs)]) if (%structs);
203 print Data
::Dumper
->Dump([\
%defines], [qw(*defines)]) if (%defines);
204 print Data
::Dumper
->Dump([\
%enum_symbols], [qw(*enum_symbols)]) if (%enum_symbols);
210 $data = expand
($data);
213 $data =~ s/\n\s+$/\n/g;
214 $data =~ s/\n\s+\n/\n\n/g;
217 # Add escape codes for special characters
219 $data =~ s
,([\_\
`\*\<\>\&\\\\:\/]),\\$1,g;
221 $data =~ s,DEPRECATED,**DEPRECATED**,g;
227 my $start_delim = "[ \n\t\(\=\*\@]";
228 my $end_delim = "(\\s|,|\\\\=|\\\\:|\\;|\\\)|\\}|\\{)";
230 foreach my $r (keys %ioctls) {
233 my $s = "\\ :ref:`$r <$n>`\\ ";
235 $r =~ s,([\_\`\
*\
<\
>\
&\\\\:\
/]),\\\\$1,g
;
237 print "$r -> $s\n" if ($debug);
239 $data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
242 foreach my $r (keys %defines) {
243 my $n = $defines{$r};
245 my $s = "\\ :ref:`$r <$n>`\\ ";
247 $r =~ s
,([\_\
`\*\<\>\&\\\\:\/]),\\\\$1,g;
249 print "$r -> $s\n" if ($debug);
251 $data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
254 foreach my $r (keys %enum_symbols) {
255 my $n = $enum_symbols{$r};
257 my $s = "\\ :ref:`$r <$n>`\\ ";
259 $r =~ s,([\_\`\
*\
<\
>\
&\\\\:\
/]),\\\\$1,g
;
261 print "$r -> $s\n" if ($debug);
263 $data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
266 foreach my $r (keys %enums) {
269 my $s = "\\ :ref:`enum $r <$n>`\\ ";
271 $r =~ s
,([\_\
`\*\<\>\&\\\\:\/]),\\\\$1,g;
273 print "$r -> $s\n" if ($debug);
275 $data =~ s/enum\s+($r)$end_delim/$s$2/g;
278 foreach my $r (keys %structs) {
279 my $n = $structs{$r};
281 my $s = "\\ :ref:`struct
$r <$n>`\\ ";
283 $r =~ s,([\_\`\
*\
<\
>\
&\\\\:\
/]),\\\\$1,g
;
285 print "$r -> $s\n" if ($debug);
287 $data =~ s/struct\s+($r)$end_delim/$s$2/g;
290 foreach my $r (keys %typedefs) {
291 my $n = $typedefs{$r};
293 my $s = "\\ :ref:`$r <$n>`\\ ";
295 $r =~ s
,([\_\
`\*\<\>\&\\\\:\/]),\\\\$1,g;
297 print "$r -> $s\n" if ($debug);
299 $data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
303 # Generate output file
306 my $title = $file_in;
309 open OUT, "> $file_out" or die "Can't open $file_out";
310 print OUT ".. -*- coding: utf-8; mode: rst -*-\n\n";
311 print OUT "$title\n";
312 print OUT "=" x length($title);
313 print OUT "\n\n.. parsed-literal::\n\n";
This page took 0.041239 seconds and 5 git commands to generate.