blob: db0186a7618f2a9ca9b712db1c9a40d4bb6e3d7c [file] [log] [blame]
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -03001#!/usr/bin/perl
2use strict;
3use Text::Tabs;
4
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -03005my $debug = 0;
6
Mauro Carvalho Chehabbcec7c22016-08-31 06:41:40 -03007while ($ARGV[0] =~ m/^-(.*)/) {
8 my $cmd = shift @ARGV;
9 if ($cmd eq "--debug") {
10 require Data::Dumper;
11 $debug = 1;
12 next;
13 }
14 die "argument $cmd unknown";
15}
16
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -030017if (scalar @ARGV < 2 || scalar @ARGV > 3) {
18 die "Usage:\n\t$0 <file in> <file out> [<exceptions file>]\n";
19}
20
21my ($file_in, $file_out, $file_exceptions) = @ARGV;
22
23my $data;
24my %ioctls;
25my %defines;
26my %typedefs;
27my %enums;
28my %enum_symbols;
29my %structs;
30
31#
32# read the file and get identifiers
33#
34
35my $is_enum = 0;
Mauro Carvalho Chehab034e6c82016-07-07 14:13:12 -030036my $is_comment = 0;
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -030037open IN, $file_in or die "Can't open $file_in";
38while (<IN>) {
39 $data .= $_;
40
Mauro Carvalho Chehab034e6c82016-07-07 14:13:12 -030041 my $ln = $_;
42 if (!$is_comment) {
43 $ln =~ s,/\*.*(\*/),,g;
44
45 $is_comment = 1 if ($ln =~ s,/\*.*,,);
46 } else {
47 if ($ln =~ s,^(.*\*/),,) {
48 $is_comment = 0;
49 } else {
50 next;
51 }
52 }
53
Mauro Carvalho Chehab9c80c742016-07-07 07:06:05 -030054 if ($is_enum && $ln =~ m/^\s*([_\w][\w\d_]+)\s*[\,=]?/) {
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -030055 my $s = $1;
56 my $n = $1;
57 $n =~ tr/A-Z/a-z/;
58 $n =~ tr/_/-/;
59
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -030060 $enum_symbols{$s} = "\\ :ref:`$s <$n>`\\ ";
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -030061
62 $is_enum = 0 if ($is_enum && m/\}/);
63 next;
64 }
65 $is_enum = 0 if ($is_enum && m/\}/);
66
Mauro Carvalho Chehab9c80c742016-07-07 07:06:05 -030067 if ($ln =~ m/^\s*#\s*define\s+([_\w][\w\d_]+)\s+_IO/) {
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -030068 my $s = $1;
69 my $n = $1;
70 $n =~ tr/A-Z/a-z/;
71
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -030072 $ioctls{$s} = "\\ :ref:`$s <$n>`\\ ";
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -030073 next;
74 }
75
Mauro Carvalho Chehab9c80c742016-07-07 07:06:05 -030076 if ($ln =~ m/^\s*#\s*define\s+([_\w][\w\d_]+)\s+/) {
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -030077 my $s = $1;
78 my $n = $1;
79 $n =~ tr/A-Z/a-z/;
80 $n =~ tr/_/-/;
81
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -030082 $defines{$s} = "\\ :ref:`$s <$n>`\\ ";
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -030083 next;
84 }
85
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -030086 if ($ln =~ m/^\s*typedef\s+([_\w][\w\d_]+)\s+(.*)\s+([_\w][\w\d_]+);/) {
87 my $s = $2;
88 my $n = $3;
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -030089
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -030090 $typedefs{$n} = "\\ :c:type:`$n <$s>`\\ ";
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -030091 next;
92 }
Mauro Carvalho Chehab9c80c742016-07-07 07:06:05 -030093 if ($ln =~ m/^\s*enum\s+([_\w][\w\d_]+)\s+\{/
Mauro Carvalho Chehab6c4c7da2016-07-07 07:20:27 -030094 || $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_]+)$/) {
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -030097 my $s = $1;
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -030098
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -030099 $enums{$s} = "enum :c:type:`$s`\\ ";
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300100
101 $is_enum = $1;
102 next;
103 }
Mauro Carvalho Chehab9c80c742016-07-07 07:06:05 -0300104 if ($ln =~ m/^\s*struct\s+([_\w][\w\d_]+)\s+\{/
Mauro Carvalho Chehab6c4c7da2016-07-07 07:20:27 -0300105 || $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_]+)$/
108 ) {
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300109 my $s = $1;
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300110
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300111 $structs{$s} = "struct :c:type:`$s`\\ ";
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300112 next;
113 }
114}
115close IN;
116
117#
118# Handle multi-line typedefs
119#
120
Mauro Carvalho Chehab4ff916a2016-07-07 08:09:37 -0300121my @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,);
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300123foreach my $m (@matches) {
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300124 my $s = $m;
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300125
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300126 $typedefs{$s} = "\\ :c:type:`$s`\\ ";
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300127 next;
128}
129
130#
131# Handle exceptions, if any
132#
133
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300134my %def_reftype = (
135 "ioctl" => ":ref",
136 "define" => ":ref",
137 "symbol" => ":ref",
138 "typedef" => ":c:type",
139 "enum" => ":c:type",
140 "struct" => ":c:type",
141);
142
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300143if ($file_exceptions) {
144 open IN, $file_exceptions or die "Can't read $file_exceptions";
145 while (<IN>) {
146 next if (m/^\s*$/ || m/^\s*#/);
147
148 # Parsers to ignore a symbol
149
150 if (m/^ignore\s+ioctl\s+(\S+)/) {
151 delete $ioctls{$1} if (exists($ioctls{$1}));
152 next;
153 }
154 if (m/^ignore\s+define\s+(\S+)/) {
155 delete $defines{$1} if (exists($defines{$1}));
156 next;
157 }
158 if (m/^ignore\s+typedef\s+(\S+)/) {
159 delete $typedefs{$1} if (exists($typedefs{$1}));
160 next;
161 }
162 if (m/^ignore\s+enum\s+(\S+)/) {
163 delete $enums{$1} if (exists($enums{$1}));
164 next;
165 }
166 if (m/^ignore\s+struct\s+(\S+)/) {
167 delete $structs{$1} if (exists($structs{$1}));
168 next;
169 }
Mauro Carvalho Chehab526b8842016-07-07 14:26:51 -0300170 if (m/^ignore\s+symbol\s+(\S+)/) {
171 delete $enum_symbols{$1} if (exists($enum_symbols{$1}));
172 next;
173 }
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300174
175 # Parsers to replace a symbol
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300176 my ($type, $old, $new, $reftype);
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300177
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300178 if (m/^replace\s+(\S+)\s+(\S+)\s+(\S+)/) {
179 $type = $1;
180 $old = $2;
181 $new = $3;
182 } else {
183 die "Can't parse $file_exceptions: $_";
184 }
185
186 if ($new =~ m/^\:c\:(data|func|macro|type)\:\`(.+)\`/) {
187 $reftype = ":c:$1";
188 $new = $2;
189 } elsif ($new =~ m/\:ref\:\`(.+)\`/) {
190 $reftype = ":ref";
191 $new = $1;
192 } else {
193 $reftype = $def_reftype{$type};
194 }
195 $new = "$reftype:`$old <$new>`";
196
197 if ($type eq "ioctl") {
198 $ioctls{$old} = $new if (exists($ioctls{$old}));
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300199 next;
200 }
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300201 if ($type eq "define") {
202 $defines{$old} = $new if (exists($defines{$old}));
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300203 next;
204 }
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300205 if ($type eq "symbol") {
206 $enum_symbols{$old} = $new if (exists($enum_symbols{$old}));
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300207 next;
208 }
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300209 if ($type eq "typedef") {
210 $typedefs{$old} = $new if (exists($typedefs{$old}));
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300211 next;
212 }
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300213 if ($type eq "enum") {
214 $enums{$old} = $new if (exists($enums{$old}));
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300215 next;
216 }
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300217 if ($type eq "struct") {
218 $structs{$old} = $new if (exists($structs{$old}));
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300219 next;
220 }
221
222 die "Can't parse $file_exceptions: $_";
223 }
224}
225
226if ($debug) {
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);
233}
234
235#
236# Align block
237#
238$data = expand($data);
239$data = " " . $data;
240$data =~ s/\n/\n /g;
241$data =~ s/\n\s+$/\n/g;
242$data =~ s/\n\s+\n/\n\n/g;
243
244#
245# Add escape codes for special characters
246#
Mauro Carvalho Chehab999d9982016-08-16 13:25:41 -0300247$data =~ s,([\_\`\*\<\>\&\\\\:\/\|\%\$\#\{\}\~\^]),\\$1,g;
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300248
Mauro Carvalho Chehab7d95fa82016-07-07 06:31:21 -0300249$data =~ s,DEPRECATED,**DEPRECATED**,g;
250
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300251#
252# Add references
253#
254
Mauro Carvalho Chehab6fe79d12016-07-07 06:27:54 -0300255my $start_delim = "[ \n\t\(\=\*\@]";
256my $end_delim = "(\\s|,|\\\\=|\\\\:|\\;|\\\)|\\}|\\{)";
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300257
258foreach my $r (keys %ioctls) {
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300259 my $s = $ioctls{$r};
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300260
261 $r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;
262
263 print "$r -> $s\n" if ($debug);
264
Mauro Carvalho Chehab6fe79d12016-07-07 06:27:54 -0300265 $data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300266}
267
268foreach my $r (keys %defines) {
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300269 my $s = $defines{$r};
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300270
271 $r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;
272
273 print "$r -> $s\n" if ($debug);
274
Mauro Carvalho Chehab6fe79d12016-07-07 06:27:54 -0300275 $data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300276}
277
278foreach my $r (keys %enum_symbols) {
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300279 my $s = $enum_symbols{$r};
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300280
281 $r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;
282
283 print "$r -> $s\n" if ($debug);
284
Mauro Carvalho Chehab6fe79d12016-07-07 06:27:54 -0300285 $data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300286}
287
288foreach my $r (keys %enums) {
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300289 my $s = $enums{$r};
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300290
291 $r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;
292
293 print "$r -> $s\n" if ($debug);
294
Mauro Carvalho Chehab6fe79d12016-07-07 06:27:54 -0300295 $data =~ s/enum\s+($r)$end_delim/$s$2/g;
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300296}
297
298foreach my $r (keys %structs) {
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300299 my $s = $structs{$r};
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300300
301 $r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;
302
303 print "$r -> $s\n" if ($debug);
304
Mauro Carvalho Chehab6fe79d12016-07-07 06:27:54 -0300305 $data =~ s/struct\s+($r)$end_delim/$s$2/g;
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300306}
307
308foreach my $r (keys %typedefs) {
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300309 my $s = $typedefs{$r};
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300310
311 $r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;
312
313 print "$r -> $s\n" if ($debug);
Mauro Carvalho Chehab6fe79d12016-07-07 06:27:54 -0300314 $data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300315}
316
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300317$data =~ s/\\ ([\n\s])/\1/g;
Mauro Carvalho Chehabfb6fc6c2016-07-09 09:35:34 -0300318
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300319#
320# Generate output file
321#
322
323my $title = $file_in;
324$title =~ s,.*/,,;
325
326open OUT, "> $file_out" or die "Can't open $file_out";
327print OUT ".. -*- coding: utf-8; mode: rst -*-\n\n";
328print OUT "$title\n";
329print OUT "=" x length($title);
330print OUT "\n\n.. parsed-literal::\n\n";
331print OUT $data;
332close OUT;