blob: c5038a0a73136f252095610086e9642ecfd20611 [file] [log] [blame]
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -03001#!/usr/bin/perl
2
3use strict;
4use Pod::Usage;
5use Getopt::Long;
6use File::Find;
7use Fcntl ':mode';
8
9my $help;
10my $man;
11my $debug;
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030012my $prefix="Documentation/ABI";
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030013
14GetOptions(
15 "debug|d+" => \$debug,
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030016 "dir=s" => \$prefix,
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030017 'help|?' => \$help,
18 man => \$man
19) or pod2usage(2);
20
21pod2usage(1) if $help;
22pod2usage(-exitstatus => 0, -verbose => 2) if $man;
23
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030024pod2usage(2) if (scalar @ARGV < 1 || @ARGV > 2);
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030025
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030026my ($cmd, $arg) = @ARGV;
27
28pod2usage(2) if ($cmd ne "search" && $cmd ne "rest");
29pod2usage(2) if ($cmd eq "search" && !$arg);
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030030
31require Data::Dumper if ($debug);
32
33my %data;
34
35#
36# Displays an error message, printing file name and line
37#
38sub parse_error($$$$) {
39 my ($file, $ln, $msg, $data) = @_;
40
41 print STDERR "file $file#$ln: $msg at\n\t$data";
42}
43
44#
45# Parse an ABI file, storing its contents at %data
46#
47sub parse_abi {
48 my $file = $File::Find::name;
49
50 my $mode = (stat($file))[2];
51 return if ($mode & S_IFDIR);
52 return if ($file =~ m,/README,);
53
54 my $name = $file;
55 $name =~ s,.*/,,;
56
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -030057 my $nametag = "File $name";
58 $data{$nametag}->{what} = "File $name";
59 $data{$nametag}->{type} = "File";
60 $data{$nametag}->{file} = $name;
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030061 $data{$nametag}->{filepath} = $file;
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -030062 $data{$nametag}->{is_file} = 1;
63
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030064 my $type = $file;
65 $type =~ s,.*/(.*)/.*,$1,;
66
67 my $what;
68 my $new_what;
69 my $tag;
70 my $ln;
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -030071 my $xrefs;
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -030072 my $space;
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -030073 my @labels;
74 my $label;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030075
76 print STDERR "Opening $file\n" if ($debug > 1);
77 open IN, $file;
78 while(<IN>) {
79 $ln++;
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -030080 if (m/^(\S+)(:\s*)(.*)/i) {
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030081 my $new_tag = lc($1);
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -030082 my $sep = $2;
83 my $content = $3;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030084
85 if (!($new_tag =~ m/(what|date|kernelversion|contact|description|users)/)) {
86 if ($tag eq "description") {
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -030087 # New "tag" is actually part of
88 # description. Don't consider it a tag
89 $new_tag = "";
Mauro Carvalho Chehab7d7ea8d2019-06-20 14:23:01 -030090 } elsif ($tag ne "") {
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030091 parse_error($file, $ln, "tag '$tag' is invalid", $_);
92 }
93 }
94
Mauro Carvalho Chehab2c0700e2019-06-20 14:23:03 -030095 # Invalid, but it is a common mistake
96 if ($new_tag eq "where") {
97 parse_error($file, $ln, "tag 'Where' is invalid. Should be 'What:' instead", $_);
98 $new_tag = "what";
99 }
100
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300101 if ($new_tag =~ m/what/) {
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300102 $space = "";
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300103 if ($tag =~ m/what/) {
104 $what .= ", " . $content;
105 } else {
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300106 parse_error($file, $ln, "What '$what' doesn't have a description", "") if ($what && !$data{$what}->{description});
107
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300108 $what = $content;
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300109 $label = $content;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300110 $new_what = 1;
111 }
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300112 push @labels, [($content, $label)];
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300113 $tag = $new_tag;
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -0300114
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300115 push @{$data{$nametag}->{xrefs}}, [($content, $label)] if ($data{$nametag}->{what});
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300116 next;
117 }
118
Mauro Carvalho Chehab7d7ea8d2019-06-20 14:23:01 -0300119 if ($tag ne "" && $new_tag) {
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300120 $tag = $new_tag;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300121
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300122 if ($new_what) {
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300123 @{$data{$what}->{label}} = @labels if ($data{$nametag}->{what});
124 @labels = ();
125 $label = "";
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300126 $new_what = 0;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300127
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300128 $data{$what}->{type} = $type;
129 $data{$what}->{file} = $name;
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300130 $data{$what}->{filepath} = $file;
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300131 print STDERR "\twhat: $what\n" if ($debug > 1);
132 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300133
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300134 if (!$what) {
135 parse_error($file, $ln, "'What:' should come first:", $_);
136 next;
137 }
138 if ($tag eq "description") {
139 next if ($content =~ m/^\s*$/);
140 if ($content =~ m/^(\s*)(.*)/) {
141 my $new_content = $2;
142 $space = $new_tag . $sep . $1;
143 while ($space =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {}
144 $space =~ s/./ /g;
145 $data{$what}->{$tag} .= "$new_content\n";
146 }
147 } else {
148 $data{$what}->{$tag} = $content;
149 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300150 next;
151 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300152 }
153
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300154 # Store any contents before tags at the database
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300155 if (!$tag && $data{$nametag}->{what}) {
156 $data{$nametag}->{description} .= $_;
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -0300157 next;
158 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300159
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300160 if ($tag eq "description") {
161 if (!$data{$what}->{description}) {
162 next if (m/^\s*\n/);
163 if (m/^(\s*)(.*)/) {
164 $space = $1;
165 while ($space =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {}
166 $data{$what}->{$tag} .= "$2\n";
167 }
168 } else {
169 my $content = $_;
170 if (m/^\s*\n/) {
171 $data{$what}->{$tag} .= $content;
172 next;
173 }
174
175 while ($content =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {}
176 $space = "" if (!($content =~ s/^($space)//));
177
178 # Compress spaces with tabs
179 $content =~ s<^ {8}> <\t>;
180 $content =~ s<^ {1,7}\t> <\t>;
181 $content =~ s< {1,7}\t> <\t>;
182 $data{$what}->{$tag} .= $content;
183 }
184 next;
185 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300186 if (m/^\s*(.*)/) {
187 $data{$what}->{$tag} .= "\n$1";
188 $data{$what}->{$tag} =~ s/\n+$//;
189 next;
190 }
191
192 # Everything else is error
193 parse_error($file, $ln, "Unexpected line:", $_);
194 }
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300195 $data{$nametag}->{description} =~ s/^\n+//;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300196 close IN;
197}
198
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300199#
200# Outputs the book on ReST format
201#
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300202
Mauro Carvalho Chehab2e7ce052019-06-20 14:23:02 -0300203my %labels;
204
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300205sub output_rest {
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300206 foreach my $what (sort {
207 ($data{$a}->{type} eq "File") cmp ($data{$b}->{type} eq "File") ||
208 $a cmp $b
209 } keys %data) {
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300210 my $type = $data{$what}->{type};
211 my $file = $data{$what}->{file};
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300212 my $filepath = $data{$what}->{filepath};
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300213
214 my $w = $what;
215 $w =~ s/([\(\)\_\-\*\=\^\~\\])/\\$1/g;
216
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300217
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300218 foreach my $p (@{$data{$what}->{label}}) {
219 my ($content, $label) = @{$p};
220 $label = "abi_" . $label . " ";
221 $label =~ tr/A-Z/a-z/;
222
223 # Convert special chars to "_"
224 $label =~s/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\xff])/_/g;
225 $label =~ s,_+,_,g;
226 $label =~ s,_$,,;
227
Mauro Carvalho Chehab2e7ce052019-06-20 14:23:02 -0300228 # Avoid duplicated labels
229 while (defined($labels{$label})) {
230 my @chars = ("A".."Z", "a".."z");
231 $label .= $chars[rand @chars];
232 }
233 $labels{$label} = 1;
234
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300235 $data{$what}->{label} .= $label;
236
237 printf ".. _%s:\n\n", $label;
238
239 # only one label is enough
240 last;
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -0300241 }
242
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -0300243
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300244 $filepath =~ s,.*/(.*/.*),\1,;;
245 $filepath =~ s,[/\-],_,g;;
246 my $fileref = "abi_file_".$filepath;
247
248 if ($type eq "File") {
249 my $bar = $w;
250 $bar =~ s/./-/g;
251
252 print ".. _$fileref:\n\n";
253 print "$w\n$bar\n\n";
254 } else {
255 my @names = split /\s*,\s*/,$w;
256
257 my $len = 0;
258
259 foreach my $name (@names) {
260 $len = length($name) if (length($name) > $len);
261 }
262
263 print "What:\n\n";
264
265 print "+-" . "-" x $len . "-+\n";
266 foreach my $name (@names) {
267 printf "| %s", $name . " " x ($len - length($name)) . " |\n";
268 print "+-" . "-" x $len . "-+\n";
269 }
270 print "\n";
271 }
272
273 print "Defined on file :ref:`$file <$fileref>`\n\n" if ($type ne "File");
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300274
275 my $desc = $data{$what}->{description};
276 $desc =~ s/^\s+//;
277
278 # Remove title markups from the description, as they won't work
279 $desc =~ s/\n[\-\*\=\^\~]+\n/\n/g;
280
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300281 if (!($desc =~ /^\s*$/)) {
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300282 if ($desc =~ m/\:\n/ || $desc =~ m/\n[\t ]+/ || $desc =~ m/[\x00-\x08\x0b-\x1f\x7b-\xff]/) {
283 # put everything inside a code block
284 $desc =~ s/\n/\n /g;
285
286 print "::\n\n";
287 print " $desc\n\n";
288 } else {
289 # Escape any special chars from description
290 $desc =~s/([\x00-\x08\x0b-\x1f\x21-\x2a\x2d\x2f\x3c-\x40\x5c\x5e-\x60\x7b-\xff])/\\$1/g;
291
292 print "$desc\n\n";
293 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300294 } else {
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300295 print "DESCRIPTION MISSING for $what\n\n" if (!$data{$what}->{is_file});
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300296 }
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -0300297
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300298 if ($data{$what}->{xrefs}) {
299 printf "Has the following ABI:\n\n";
300
301 foreach my $p(@{$data{$what}->{xrefs}}) {
302 my ($content, $label) = @{$p};
303 $label = "abi_" . $label . " ";
304 $label =~ tr/A-Z/a-z/;
305
306 # Convert special chars to "_"
307 $label =~s/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\xff])/_/g;
308 $label =~ s,_+,_,g;
309 $label =~ s,_$,,;
310
311 # Escape special chars from content
312 $content =~s/([\x00-\x1f\x21-\x2f\x3a-\x40\x7b-\xff])/\\$1/g;
313
314 print "- :ref:`$content <$label>`\n\n";
315 }
316 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300317 }
318}
319
320#
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300321# Searches for ABI symbols
322#
323sub search_symbols {
324 foreach my $what (sort keys %data) {
325 next if (!($what =~ m/($arg)/));
326
327 my $type = $data{$what}->{type};
328 next if ($type eq "File");
329
330 my $file = $data{$what}->{filepath};
331
332 my $bar = $what;
333 $bar =~ s/./-/g;
334
335 print "\n$what\n$bar\n\n";
336
337 my $kernelversion = $data{$what}->{kernelversion};
338 my $contact = $data{$what}->{contact};
339 my $users = $data{$what}->{users};
340 my $date = $data{$what}->{date};
341 my $desc = $data{$what}->{description};
342 $kernelversion =~ s/^\s+//;
343 $contact =~ s/^\s+//;
344 $users =~ s/^\s+//;
345 $users =~ s/\n//g;
346 $date =~ s/^\s+//;
347 $desc =~ s/^\s+//;
348
349 printf "Kernel version:\t\t%s\n", $kernelversion if ($kernelversion);
350 printf "Date:\t\t\t%s\n", $date if ($date);
351 printf "Contact:\t\t%s\n", $contact if ($contact);
352 printf "Users:\t\t\t%s\n", $users if ($users);
353 print "Defined on file:\t$file\n\n";
354 print "Description:\n\n$desc";
355 }
356}
357
358
359#
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300360# Parses all ABI files located at $prefix dir
361#
362find({wanted =>\&parse_abi, no_chdir => 1}, $prefix);
363
364print STDERR Data::Dumper->Dump([\%data], [qw(*data)]) if ($debug);
365
366#
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300367# Handles the command
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300368#
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300369if ($cmd eq "rest") {
370 output_rest;
371} else {
372 search_symbols;
373}
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300374
375
376__END__
377
378=head1 NAME
379
380abi_book.pl - parse the Linux ABI files and produce a ReST book.
381
382=head1 SYNOPSIS
383
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300384B<abi_book.pl> [--debug] <COMAND> [<ARGUMENT>]
385
386Where <COMMAND> can be:
387
388=over 8
389
390B<search> [SEARCH_REGEX] - search for [SEARCH_REGEX] inside ABI
391
392B<rest> - output the ABI in ReST markup language
393
394=back
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300395
396=head1 OPTIONS
397
398=over 8
399
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300400=item B<--dir>
401
402Changes the location of the ABI search. By default, it uses
403the Documentation/ABI directory.
404
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300405=item B<--debug>
406
407Put the script in verbose mode, useful for debugging. Can be called multiple
408times, to increase verbosity.
409
410=item B<--help>
411
412Prints a brief help message and exits.
413
414=item B<--man>
415
416Prints the manual page and exits.
417
418=back
419
420=head1 DESCRIPTION
421
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300422Parse the Linux ABI files from ABI DIR (usually located at Documentation/ABI),
423allowing to search for ABI symbols or to produce a ReST book containing
424the Linux ABI documentation.
425
426=head1 EXAMPLES
427
428Search for all stable symbols with the word "usb":
429
430=over 8
431
432$ scripts/get_abi.pl search usb --dir Documentation/ABI/stable
433
434=back
435
436Search for all symbols that match the regex expression "usb.*cap":
437
438=over 8
439
440$ scripts/get_abi.pl search usb.*cap
441
442=back
443
444Output all obsoleted symbols in ReST format
445
446=over 8
447
448$ scripts/get_abi.pl rest --dir Documentation/ABI/obsolete
449
450=back
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300451
452=head1 BUGS
453
454Report bugs to Mauro Carvalho Chehab <mchehab@s-opensource.com>
455
456=head1 COPYRIGHT
457
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300458Copyright (c) 2016-2017 by Mauro Carvalho Chehab <mchehab@s-opensource.com>.
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300459
460License GPLv2: GNU GPL version 2 <http://gnu.org/licenses/gpl.html>.
461
462This is free software: you are free to change and redistribute it.
463There is NO WARRANTY, to the extent permitted by law.
464
465=cut