blob: a958d8b5e99da34b898a1a81b47d818a09c557b6 [file] [log] [blame]
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -03001#!/usr/bin/perl
2use strict;
3use Text::Tabs;
Mauro Carvalho Chehab327f5a72016-11-17 08:32:34 -02004use Getopt::Long;
5use Pod::Usage;
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -03006
Mauro Carvalho Chehab327f5a72016-11-17 08:32:34 -02007my $debug;
8my $help;
9my $man;
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -030010
Mauro Carvalho Chehab327f5a72016-11-17 08:32:34 -020011GetOptions(
12 "debug" => \$debug,
Mauro Carvalho Chehabc3396652016-11-30 08:00:20 -020013 'usage|?' => \$help,
14 'help' => \$man
Mauro Carvalho Chehab327f5a72016-11-17 08:32:34 -020015) or pod2usage(2);
Mauro Carvalho Chehabbcec7c22016-08-31 06:41:40 -030016
Mauro Carvalho Chehab327f5a72016-11-17 08:32:34 -020017pod2usage(1) if $help;
18pod2usage(-exitstatus => 0, -verbose => 2) if $man;
19pod2usage(2) if (scalar @ARGV < 2 || scalar @ARGV > 3);
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -030020
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
Mauro Carvalho Chehab327f5a72016-11-17 08:32:34 -020031require Data::Dumper if ($debug);
32
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -030033#
34# read the file and get identifiers
35#
36
37my $is_enum = 0;
Mauro Carvalho Chehab034e6c82016-07-07 14:13:12 -030038my $is_comment = 0;
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -030039open IN, $file_in or die "Can't open $file_in";
40while (<IN>) {
41 $data .= $_;
42
Mauro Carvalho Chehab034e6c82016-07-07 14:13:12 -030043 my $ln = $_;
44 if (!$is_comment) {
45 $ln =~ s,/\*.*(\*/),,g;
46
47 $is_comment = 1 if ($ln =~ s,/\*.*,,);
48 } else {
49 if ($ln =~ s,^(.*\*/),,) {
50 $is_comment = 0;
51 } else {
52 next;
53 }
54 }
55
Mauro Carvalho Chehab9c80c742016-07-07 07:06:05 -030056 if ($is_enum && $ln =~ m/^\s*([_\w][\w\d_]+)\s*[\,=]?/) {
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -030057 my $s = $1;
58 my $n = $1;
59 $n =~ tr/A-Z/a-z/;
60 $n =~ tr/_/-/;
61
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -030062 $enum_symbols{$s} = "\\ :ref:`$s <$n>`\\ ";
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -030063
64 $is_enum = 0 if ($is_enum && m/\}/);
65 next;
66 }
67 $is_enum = 0 if ($is_enum && m/\}/);
68
Mauro Carvalho Chehab9c80c742016-07-07 07:06:05 -030069 if ($ln =~ m/^\s*#\s*define\s+([_\w][\w\d_]+)\s+_IO/) {
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -030070 my $s = $1;
71 my $n = $1;
72 $n =~ tr/A-Z/a-z/;
73
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -030074 $ioctls{$s} = "\\ :ref:`$s <$n>`\\ ";
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -030075 next;
76 }
77
Mauro Carvalho Chehab9c80c742016-07-07 07:06:05 -030078 if ($ln =~ m/^\s*#\s*define\s+([_\w][\w\d_]+)\s+/) {
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -030079 my $s = $1;
80 my $n = $1;
81 $n =~ tr/A-Z/a-z/;
82 $n =~ tr/_/-/;
83
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -030084 $defines{$s} = "\\ :ref:`$s <$n>`\\ ";
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -030085 next;
86 }
87
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -030088 if ($ln =~ m/^\s*typedef\s+([_\w][\w\d_]+)\s+(.*)\s+([_\w][\w\d_]+);/) {
89 my $s = $2;
90 my $n = $3;
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -030091
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -030092 $typedefs{$n} = "\\ :c:type:`$n <$s>`\\ ";
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -030093 next;
94 }
Mauro Carvalho Chehab9c80c742016-07-07 07:06:05 -030095 if ($ln =~ m/^\s*enum\s+([_\w][\w\d_]+)\s+\{/
Mauro Carvalho Chehab6c4c7da2016-07-07 07:20:27 -030096 || $ln =~ m/^\s*enum\s+([_\w][\w\d_]+)$/
97 || $ln =~ m/^\s*typedef\s*enum\s+([_\w][\w\d_]+)\s+\{/
98 || $ln =~ m/^\s*typedef\s*enum\s+([_\w][\w\d_]+)$/) {
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -030099 my $s = $1;
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300100
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300101 $enums{$s} = "enum :c:type:`$s`\\ ";
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300102
103 $is_enum = $1;
104 next;
105 }
Mauro Carvalho Chehab9c80c742016-07-07 07:06:05 -0300106 if ($ln =~ m/^\s*struct\s+([_\w][\w\d_]+)\s+\{/
Mauro Carvalho Chehab6c4c7da2016-07-07 07:20:27 -0300107 || $ln =~ m/^\s*struct\s+([[_\w][\w\d_]+)$/
108 || $ln =~ m/^\s*typedef\s*struct\s+([_\w][\w\d_]+)\s+\{/
109 || $ln =~ m/^\s*typedef\s*struct\s+([[_\w][\w\d_]+)$/
110 ) {
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300111 my $s = $1;
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300112
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300113 $structs{$s} = "struct :c:type:`$s`\\ ";
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300114 next;
115 }
116}
117close IN;
118
119#
120# Handle multi-line typedefs
121#
122
Mauro Carvalho Chehab4ff916a2016-07-07 08:09:37 -0300123my @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,);
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300125foreach my $m (@matches) {
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300126 my $s = $m;
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300127
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300128 $typedefs{$s} = "\\ :c:type:`$s`\\ ";
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300129 next;
130}
131
132#
133# Handle exceptions, if any
134#
135
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300136my %def_reftype = (
137 "ioctl" => ":ref",
138 "define" => ":ref",
139 "symbol" => ":ref",
140 "typedef" => ":c:type",
141 "enum" => ":c:type",
142 "struct" => ":c:type",
143);
144
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300145if ($file_exceptions) {
146 open IN, $file_exceptions or die "Can't read $file_exceptions";
147 while (<IN>) {
148 next if (m/^\s*$/ || m/^\s*#/);
149
150 # Parsers to ignore a symbol
151
152 if (m/^ignore\s+ioctl\s+(\S+)/) {
153 delete $ioctls{$1} if (exists($ioctls{$1}));
154 next;
155 }
156 if (m/^ignore\s+define\s+(\S+)/) {
157 delete $defines{$1} if (exists($defines{$1}));
158 next;
159 }
160 if (m/^ignore\s+typedef\s+(\S+)/) {
161 delete $typedefs{$1} if (exists($typedefs{$1}));
162 next;
163 }
164 if (m/^ignore\s+enum\s+(\S+)/) {
165 delete $enums{$1} if (exists($enums{$1}));
166 next;
167 }
168 if (m/^ignore\s+struct\s+(\S+)/) {
169 delete $structs{$1} if (exists($structs{$1}));
170 next;
171 }
Mauro Carvalho Chehab526b8842016-07-07 14:26:51 -0300172 if (m/^ignore\s+symbol\s+(\S+)/) {
173 delete $enum_symbols{$1} if (exists($enum_symbols{$1}));
174 next;
175 }
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300176
177 # Parsers to replace a symbol
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300178 my ($type, $old, $new, $reftype);
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300179
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300180 if (m/^replace\s+(\S+)\s+(\S+)\s+(\S+)/) {
181 $type = $1;
182 $old = $2;
183 $new = $3;
184 } else {
185 die "Can't parse $file_exceptions: $_";
186 }
187
188 if ($new =~ m/^\:c\:(data|func|macro|type)\:\`(.+)\`/) {
189 $reftype = ":c:$1";
190 $new = $2;
191 } elsif ($new =~ m/\:ref\:\`(.+)\`/) {
192 $reftype = ":ref";
193 $new = $1;
194 } else {
195 $reftype = $def_reftype{$type};
196 }
197 $new = "$reftype:`$old <$new>`";
198
199 if ($type eq "ioctl") {
200 $ioctls{$old} = $new if (exists($ioctls{$old}));
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300201 next;
202 }
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300203 if ($type eq "define") {
204 $defines{$old} = $new if (exists($defines{$old}));
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300205 next;
206 }
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300207 if ($type eq "symbol") {
208 $enum_symbols{$old} = $new if (exists($enum_symbols{$old}));
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300209 next;
210 }
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300211 if ($type eq "typedef") {
212 $typedefs{$old} = $new if (exists($typedefs{$old}));
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300213 next;
214 }
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300215 if ($type eq "enum") {
216 $enums{$old} = $new if (exists($enums{$old}));
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300217 next;
218 }
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300219 if ($type eq "struct") {
220 $structs{$old} = $new if (exists($structs{$old}));
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300221 next;
222 }
223
224 die "Can't parse $file_exceptions: $_";
225 }
226}
227
228if ($debug) {
229 print Data::Dumper->Dump([\%ioctls], [qw(*ioctls)]) if (%ioctls);
230 print Data::Dumper->Dump([\%typedefs], [qw(*typedefs)]) if (%typedefs);
231 print Data::Dumper->Dump([\%enums], [qw(*enums)]) if (%enums);
232 print Data::Dumper->Dump([\%structs], [qw(*structs)]) if (%structs);
233 print Data::Dumper->Dump([\%defines], [qw(*defines)]) if (%defines);
234 print Data::Dumper->Dump([\%enum_symbols], [qw(*enum_symbols)]) if (%enum_symbols);
235}
236
237#
238# Align block
239#
240$data = expand($data);
241$data = " " . $data;
242$data =~ s/\n/\n /g;
243$data =~ s/\n\s+$/\n/g;
244$data =~ s/\n\s+\n/\n\n/g;
245
246#
247# Add escape codes for special characters
248#
Mauro Carvalho Chehab999d9982016-08-16 13:25:41 -0300249$data =~ s,([\_\`\*\<\>\&\\\\:\/\|\%\$\#\{\}\~\^]),\\$1,g;
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300250
Mauro Carvalho Chehab7d95fa82016-07-07 06:31:21 -0300251$data =~ s,DEPRECATED,**DEPRECATED**,g;
252
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300253#
254# Add references
255#
256
Mauro Carvalho Chehab6fe79d12016-07-07 06:27:54 -0300257my $start_delim = "[ \n\t\(\=\*\@]";
258my $end_delim = "(\\s|,|\\\\=|\\\\:|\\;|\\\)|\\}|\\{)";
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300259
260foreach my $r (keys %ioctls) {
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300261 my $s = $ioctls{$r};
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300262
263 $r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;
264
265 print "$r -> $s\n" if ($debug);
266
Mauro Carvalho Chehab6fe79d12016-07-07 06:27:54 -0300267 $data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300268}
269
270foreach my $r (keys %defines) {
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300271 my $s = $defines{$r};
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300272
273 $r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;
274
275 print "$r -> $s\n" if ($debug);
276
Mauro Carvalho Chehab6fe79d12016-07-07 06:27:54 -0300277 $data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300278}
279
280foreach my $r (keys %enum_symbols) {
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300281 my $s = $enum_symbols{$r};
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300282
283 $r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;
284
285 print "$r -> $s\n" if ($debug);
286
Mauro Carvalho Chehab6fe79d12016-07-07 06:27:54 -0300287 $data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300288}
289
290foreach my $r (keys %enums) {
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300291 my $s = $enums{$r};
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300292
293 $r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;
294
295 print "$r -> $s\n" if ($debug);
296
Mauro Carvalho Chehab6fe79d12016-07-07 06:27:54 -0300297 $data =~ s/enum\s+($r)$end_delim/$s$2/g;
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300298}
299
300foreach my $r (keys %structs) {
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300301 my $s = $structs{$r};
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300302
303 $r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;
304
305 print "$r -> $s\n" if ($debug);
306
Mauro Carvalho Chehab6fe79d12016-07-07 06:27:54 -0300307 $data =~ s/struct\s+($r)$end_delim/$s$2/g;
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300308}
309
310foreach my $r (keys %typedefs) {
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300311 my $s = $typedefs{$r};
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300312
313 $r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;
314
315 print "$r -> $s\n" if ($debug);
Mauro Carvalho Chehab6fe79d12016-07-07 06:27:54 -0300316 $data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300317}
318
Mauro Carvalho Chehab22c40032016-08-31 06:44:21 -0300319$data =~ s/\\ ([\n\s])/\1/g;
Mauro Carvalho Chehabfb6fc6c2016-07-09 09:35:34 -0300320
Mauro Carvalho Chehabdabf8be2016-07-06 22:58:54 -0300321#
322# Generate output file
323#
324
325my $title = $file_in;
326$title =~ s,.*/,,;
327
328open OUT, "> $file_out" or die "Can't open $file_out";
329print OUT ".. -*- coding: utf-8; mode: rst -*-\n\n";
330print OUT "$title\n";
331print OUT "=" x length($title);
332print OUT "\n\n.. parsed-literal::\n\n";
333print OUT $data;
334close OUT;
Mauro Carvalho Chehab327f5a72016-11-17 08:32:34 -0200335
336__END__
337
338=head1 NAME
339
340parse_headers.pl - parse a C file, in order to identify functions, structs,
341enums and defines and create cross-references to a Sphinx book.
342
343=head1 SYNOPSIS
344
345B<parse_headers.pl> [<options>] <C_FILE> <OUT_FILE> [<EXCEPTIONS_FILE>]
346
347Where <options> can be: --debug, --help or --man.
348
349=head1 OPTIONS
350
351=over 8
352
353=item B<--debug>
354
355Put the script in verbose mode, useful for debugging.
356
Mauro Carvalho Chehabc3396652016-11-30 08:00:20 -0200357=item B<--usage>
Mauro Carvalho Chehab327f5a72016-11-17 08:32:34 -0200358
359Prints a brief help message and exits.
360
Mauro Carvalho Chehabc3396652016-11-30 08:00:20 -0200361=item B<--help>
Mauro Carvalho Chehab327f5a72016-11-17 08:32:34 -0200362
Mauro Carvalho Chehabc3396652016-11-30 08:00:20 -0200363Prints a more detailed help message and exits.
Mauro Carvalho Chehab327f5a72016-11-17 08:32:34 -0200364
365=back
366
367=head1 DESCRIPTION
368
369Convert a C header or source file (C_FILE), into a ReStructured Text
370included via ..parsed-literal block with cross-references for the
371documentation files that describe the API. It accepts an optional
372EXCEPTIONS_FILE with describes what elements will be either ignored or
373be pointed to a non-default reference.
374
375The output is written at the (OUT_FILE).
376
377It is capable of identifying defines, functions, structs, typedefs,
378enums and enum symbols and create cross-references for all of them.
379It is also capable of distinguish #define used for specifying a Linux
380ioctl.
381
Mauro Carvalho Chehabc3396652016-11-30 08:00:20 -0200382The EXCEPTIONS_FILE contain two rules to allow ignoring a symbol or
383to replace the default references by a custom one.
Mauro Carvalho Chehab327f5a72016-11-17 08:32:34 -0200384
Mauro Carvalho Chehabc3396652016-11-30 08:00:20 -0200385Please read Documentation/doc-guide/parse-headers.rst at the Kernel's
386tree for more details.
Mauro Carvalho Chehab327f5a72016-11-17 08:32:34 -0200387
388=head1 BUGS
389
390Report bugs to Mauro Carvalho Chehab <mchehab@s-opensource.com>
391
392=head1 COPYRIGHT
393
394Copyright (c) 2016 by Mauro Carvalho Chehab <mchehab@s-opensource.com>.
395
396License GPLv2: GNU GPL version 2 <http://gnu.org/licenses/gpl.html>.
397
398This is free software: you are free to change and redistribute it.
399There is NO WARRANTY, to the extent permitted by law.
400
401=cut