7 while ($ARGV[0] =~ m/^-(.*)/) {
9 if ($cmd eq "--debug") {
14 die "argument $cmd unknown";
17 if (scalar @ARGV < 2 || scalar @ARGV > 3) {
18 die "Usage:\n\t$0 <file in> <file out> [<exceptions file>]\n";
21 my ($file_in, $file_out, $file_exceptions) = @ARGV;
32 # read the file and get identifiers
37 open IN
, $file_in or die "Can't open $file_in";
43 $ln =~ s
,/\*.*(\*/),,g
;
45 $is_comment = 1 if ($ln =~ s
,/\
*.*,,);
47 if ($ln =~ s
,^(.*\
*/),,) {
54 if ($is_enum && $ln =~ m/^\s*([_\w][\w\d_]+)\s*[\,=]?/) {
60 $enum_symbols{$s} = "\\ :ref:`$s <$n>`\\ ";
62 $is_enum = 0 if ($is_enum && m/\}/);
65 $is_enum = 0 if ($is_enum && m/\}/);
67 if ($ln =~ m/^\s*#\s*define\s+([_\w][\w\d_]+)\s+_IO/) {
72 $ioctls{$s} = "\\ :ref:`$s <$n>`\\ ";
76 if ($ln =~ m/^\s*#\s*define\s+([_\w][\w\d_]+)\s+/) {
82 $defines{$s} = "\\ :ref:`$s <$n>`\\ ";
86 if ($ln =~ m/^\s*typedef\s+([_\w][\w\d_]+)\s+(.*)\s+([_\w][\w\d_]+);/) {
90 $typedefs{$n} = "\\ :c:type:`$n <$s>`\\ ";
93 if ($ln =~ m/^\s*enum\s+([_\w][\w\d_]+)\s+\{/
94 || $ln =~ m/^\s*enum\s+([_\w][\w\d_]+)$/
95 || $ln =~ m/^\s*typedef\s*enum\s+([_\w][\w\d_]+)\s+\{/
96 || $ln =~ m/^\s*typedef\s*enum\s+([_\w][\w\d_]+)$/) {
99 $enums{$s} = "enum :c:type:`$s`\\ ";
104 if ($ln =~ m/^\s*struct\s+([_\w][\w\d_]+)\s+\{/
105 || $ln =~ m/^\s*struct\s+([[_\w][\w\d_]+)$/
106 || $ln =~ m/^\s*typedef\s*struct\s+([_\w][\w\d_]+)\s+\{/
107 || $ln =~ m/^\s*typedef\s*struct\s+([[_\w][\w\d_]+)$/
111 $structs{$s} = "struct :c:type:`$s`\\ ";
118 # Handle multi-line typedefs
121 my @matches = ($data =~ m/typedef\s+struct\s+\S+?\s*\{[^\}]+\}\s*(\S+)\s*\;/g,
122 $data =~ m/typedef\s+enum\s+\S+?\s*\{[^\}]+\}\s*(\S+)\s*\;/g,);
123 foreach my $m (@matches) {
126 $typedefs{$s} = "\\ :c:type:`$s`\\ ";
131 # Handle exceptions, if any
138 "typedef" => ":c:type",
140 "struct" => ":c:type",
143 if ($file_exceptions) {
144 open IN
, $file_exceptions or die "Can't read $file_exceptions";
146 next if (m/^\s*$/ || m/^\s*#/);
148 # Parsers to ignore a symbol
150 if (m/^ignore\s+ioctl\s+(\S+)/) {
151 delete $ioctls{$1} if (exists($ioctls{$1}));
154 if (m/^ignore\s+define\s+(\S+)/) {
155 delete $defines{$1} if (exists($defines{$1}));
158 if (m/^ignore\s+typedef\s+(\S+)/) {
159 delete $typedefs{$1} if (exists($typedefs{$1}));
162 if (m/^ignore\s+enum\s+(\S+)/) {
163 delete $enums{$1} if (exists($enums{$1}));
166 if (m/^ignore\s+struct\s+(\S+)/) {
167 delete $structs{$1} if (exists($structs{$1}));
170 if (m/^ignore\s+symbol\s+(\S+)/) {
171 delete $enum_symbols{$1} if (exists($enum_symbols{$1}));
175 # Parsers to replace a symbol
176 my ($type, $old, $new, $reftype);
178 if (m/^replace\s+(\S+)\s+(\S+)\s+(\S+)/) {
183 die "Can't parse $file_exceptions: $_";
186 if ($new =~ m/^\:c\:(data|func|macro|type)\:\`(.+)\`/) {
189 } elsif ($new =~ m/\:ref\:\`(.+)\`/) {
193 $reftype = $def_reftype{$type};
195 $new = "$reftype:`$old <$new>`";
197 if ($type eq "ioctl") {
198 $ioctls{$old} = $new if (exists($ioctls{$old}));
201 if ($type eq "define") {
202 $defines{$old} = $new if (exists($defines{$old}));
205 if ($type eq "symbol") {
206 $enum_symbols{$old} = $new if (exists($enum_symbols{$old}));
209 if ($type eq "typedef") {
210 $typedefs{$old} = $new if (exists($typedefs{$old}));
213 if ($type eq "enum") {
214 $enums{$old} = $new if (exists($enums{$old}));
217 if ($type eq "struct") {
218 $structs{$old} = $new if (exists($structs{$old}));
222 die "Can't parse $file_exceptions: $_";
227 print Data
::Dumper
->Dump([\
%ioctls], [qw(*ioctls)]) if (%ioctls);
228 print Data
::Dumper
->Dump([\
%typedefs], [qw(*typedefs)]) if (%typedefs);
229 print Data
::Dumper
->Dump([\
%enums], [qw(*enums)]) if (%enums);
230 print Data
::Dumper
->Dump([\
%structs], [qw(*structs)]) if (%structs);
231 print Data
::Dumper
->Dump([\
%defines], [qw(*defines)]) if (%defines);
232 print Data
::Dumper
->Dump([\
%enum_symbols], [qw(*enum_symbols)]) if (%enum_symbols);
238 $data = expand
($data);
241 $data =~ s/\n\s+$/\n/g;
242 $data =~ s/\n\s+\n/\n\n/g;
245 # Add escape codes for special characters
247 $data =~ s
,([\_\
`\*\<\>\&\\\\:\/\|\%\$\#\{\}\~\^]),\\$1,g;
249 $data =~ s,DEPRECATED,**DEPRECATED**,g;
255 my $start_delim = "[ \n\t\(\=\*\@]";
256 my $end_delim = "(\\s|,|\\\\=|\\\\:|\\;|\\\)|\\}|\\{)";
258 foreach my $r (keys %ioctls) {
261 $r =~ s,([\_\`\
*\
<\
>\
&\\\\:\
/]),\\\\$1,g
;
263 print "$r -> $s\n" if ($debug);
265 $data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
268 foreach my $r (keys %defines) {
269 my $s = $defines{$r};
271 $r =~ s
,([\_\
`\*\<\>\&\\\\:\/]),\\\\$1,g;
273 print "$r -> $s\n" if ($debug);
275 $data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
278 foreach my $r (keys %enum_symbols) {
279 my $s = $enum_symbols{$r};
281 $r =~ s,([\_\`\
*\
<\
>\
&\\\\:\
/]),\\\\$1,g
;
283 print "$r -> $s\n" if ($debug);
285 $data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
288 foreach my $r (keys %enums) {
291 $r =~ s
,([\_\
`\*\<\>\&\\\\:\/]),\\\\$1,g;
293 print "$r -> $s\n" if ($debug);
295 $data =~ s/enum\s+($r)$end_delim/$s$2/g;
298 foreach my $r (keys %structs) {
299 my $s = $structs{$r};
301 $r =~ s,([\_\`\
*\
<\
>\
&\\\\:\
/]),\\\\$1,g
;
303 print "$r -> $s\n" if ($debug);
305 $data =~ s/struct\s+($r)$end_delim/$s$2/g;
308 foreach my $r (keys %typedefs) {
309 my $s = $typedefs{$r};
311 $r =~ s
,([\_\
`\*\<\>\&\\\\:\/]),\\\\$1,g;
313 print "$r -> $s\n" if ($debug);
314 $data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
317 $data =~ s/\\ ([\n\s])/\1/g;
320 # Generate output file
323 my $title = $file_in;
326 open OUT, "> $file_out" or die "Can't open $file_out";
327 print OUT ".. -*- coding: utf-8; mode: rst -*-\n\n";
328 print OUT "$title\n";
329 print OUT "=" x length($title);
330 print OUT "\n\n.. parsed-literal::\n\n";