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
30 open IN, $file_in or die "Can't open $file_in";
34 if ($is_enum && m/^\s*([^\s\}\,\=]+)\s*[\,=]?/) {
40 $enum_symbols{$s} = $n;
42 $is_enum = 0 if ($is_enum && m/\}/);
45 $is_enum = 0 if ($is_enum && m/\}/);
47 if (m/^\s*#\s*define\s+([_A-Z]\S+)\s+_IO/) {
56 if (m/^\s*#\s*define\s+([_A-Z]\S+)\s+/) {
66 if (m/^\s*typedef\s+.*\s+([_\w]\S+);/) {
75 if (m/^\s*enum\s+(\S+)\s+\{/ || m/^\s*enum\s+(\S+)$/) {
86 if (m/^\s*struct\s+([_A-Za-z_]\S+)\s+\{/ || m/^\s*struct\s+([A-Za-z_]\S+)$/) {
99 # Handle multi-line typedefs
102 my @matches = $data =~ m/typedef\s+struct\s+\S+\s*\{[^\}]+\}\s*(\S+)\s*\;/g;
103 foreach my $m (@matches) {
114 # Handle exceptions, if any
117 if ($file_exceptions) {
118 open IN, $file_exceptions or die "Can't read $file_exceptions";
120 next if (m/^\s*$/ || m/^\s*#/);
122 # Parsers to ignore a symbol
124 if (m/^ignore\s+ioctl\s+(\S+)/) {
125 delete $ioctls{$1} if (exists($ioctls{$1}));
128 if (m/^ignore\s+define\s+(\S+)/) {
129 delete $defines{$1} if (exists($defines{$1}));
132 if (m/^ignore\s+typedef\s+(\S+)/) {
133 delete $typedefs{$1} if (exists($typedefs{$1}));
136 if (m/^ignore\s+enum\s+(\S+)/) {
137 delete $enums{$1} if (exists($enums{$1}));
140 if (m/^ignore\s+struct\s+(\S+)/) {
141 delete $structs{$1} if (exists($structs{$1}));
145 # Parsers to replace a symbol
147 if (m/^replace\s+ioctl\s+(\S+)\s+(\S+)/) {
148 $ioctls{$1} = $2 if (exists($ioctls{$1}));
151 if (m/^replace\s+define\s+(\S+)\s+(\S+)/) {
152 $defines{$1} = $2 if (exists($defines{$1}));
155 if (m/^replace\s+typedef\s+(\S+)\s+(\S+)/) {
156 $typedefs{$1} = $2 if (exists($typedefs{$1}));
159 if (m/^replace\s+enum\s+(\S+)\s+(\S+)/) {
160 $enums{$1} = $2 if (exists($enums{$1}));
163 if (m/^replace\s+symbol\s+(\S+)\s+(\S+)/) {
164 $enum_symbols{$1} = $2 if (exists($enum_symbols{$1}));
167 if (m/^replace\s+struct\s+(\S+)\s+(\S+)/) {
168 $structs{$1} = $2 if (exists($structs{$1}));
172 die "Can't parse $file_exceptions: $_";
177 print Data::Dumper->Dump([\%ioctls], [qw(*ioctls)]) if (%ioctls);
178 print Data::Dumper->Dump([\%typedefs], [qw(*typedefs)]) if (%typedefs);
179 print Data::Dumper->Dump([\%enums], [qw(*enums)]) if (%enums);
180 print Data::Dumper->Dump([\%structs], [qw(*structs)]) if (%structs);
181 print Data::Dumper->Dump([\%defines], [qw(*defines)]) if (%defines);
182 print Data::Dumper->Dump([\%enum_symbols], [qw(*enum_symbols)]) if (%enum_symbols);
188 $data = expand($data);
191 $data =~ s/\n\s+$/\n/g;
192 $data =~ s/\n\s+\n/\n\n/g;
195 # Add escape codes for special characters
197 $data =~ s,([\_\`\*\<\>\&\\\\:\/]),\\$1,g;
203 my $start_delim = "[ \n\t\(\=\*\@]";
204 my $end_delim = "(\\s|,|\\\\=|\\\\:|\\;|\\\)|\\}|\\{)";
206 foreach my $r (keys %ioctls) {
209 my $s = "\\ :ref:`$r <$n>`\\ ";
211 $r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;
213 print "$r -> $s\n" if ($debug);
215 $data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
218 foreach my $r (keys %defines) {
219 my $n = $defines{$r};
221 my $s = "\\ :ref:`$r <$n>`\\ ";
223 $r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;
225 print "$r -> $s\n" if ($debug);
227 $data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
230 foreach my $r (keys %enum_symbols) {
231 my $n = $enum_symbols{$r};
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 %enums) {
245 my $s = "\\ :ref:`enum $r <$n>`\\ ";
247 $r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;
249 print "$r -> $s\n" if ($debug);
251 $data =~ s/enum\s+($r)$end_delim/$s$2/g;
254 foreach my $r (keys %structs) {
255 my $n = $structs{$r};
257 my $s = "\\ :ref:`struct $r <$n>`\\ ";
259 $r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;
261 print "$r -> $s\n" if ($debug);
263 $data =~ s/struct\s+($r)$end_delim/$s$2/g;
266 foreach my $r (keys %typedefs) {
267 my $n = $typedefs{$r};
269 my $s = "\\ :ref:`$r <$n>`\\ ";
271 $r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;
273 print "$r -> $s\n" if ($debug);
275 $data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
279 # Generate output file
282 my $title = $file_in;
285 open OUT, "> $file_out" or die "Can't open $file_out";
286 print OUT ".. -*- coding: utf-8; mode: rst -*-\n\n";
287 print OUT "$title\n";
288 print OUT "=" x length($title);
289 print OUT "\n\n.. parsed-literal::\n\n";