scripts: get_abi.pl: output users in ReST format
[linux-block.git] / scripts / get_abi.pl
CommitLineData
bbc249f2 1#!/usr/bin/perl
ecb351f1 2# SPDX-License-Identifier: GPL-2.0
bbc249f2
MCC
3
4use strict;
234948bf 5use warnings;
bbc249f2
MCC
6use Pod::Usage;
7use Getopt::Long;
8use File::Find;
9use Fcntl ':mode';
10
234948bf
MCC
11my $help = 0;
12my $man = 0;
13my $debug = 0;
14my $enable_lineno = 0;
33e3e991 15my $prefix="Documentation/ABI";
bbc249f2 16
11ce90a4
MCC
17#
18# If true, assumes that the description is formatted with ReST
19#
20my $description_is_rst = 0;
21
bbc249f2
MCC
22GetOptions(
23 "debug|d+" => \$debug,
61439c4a 24 "enable-lineno" => \$enable_lineno,
11ce90a4 25 "rst-source!" => \$description_is_rst,
33e3e991 26 "dir=s" => \$prefix,
bbc249f2
MCC
27 'help|?' => \$help,
28 man => \$man
29) or pod2usage(2);
30
31pod2usage(1) if $help;
32pod2usage(-exitstatus => 0, -verbose => 2) if $man;
33
33e3e991 34pod2usage(2) if (scalar @ARGV < 1 || @ARGV > 2);
bbc249f2 35
33e3e991
MCC
36my ($cmd, $arg) = @ARGV;
37
7ce7b89b 38pod2usage(2) if ($cmd ne "search" && $cmd ne "rest" && $cmd ne "validate");
33e3e991 39pod2usage(2) if ($cmd eq "search" && !$arg);
bbc249f2
MCC
40
41require Data::Dumper if ($debug);
42
43my %data;
234948bf 44my %symbols;
bbc249f2
MCC
45
46#
47# Displays an error message, printing file name and line
48#
49sub parse_error($$$$) {
50 my ($file, $ln, $msg, $data) = @_;
51
52 print STDERR "file $file#$ln: $msg at\n\t$data";
53}
54
55#
56# Parse an ABI file, storing its contents at %data
57#
58sub parse_abi {
59 my $file = $File::Find::name;
60
61 my $mode = (stat($file))[2];
62 return if ($mode & S_IFDIR);
63 return if ($file =~ m,/README,);
64
65 my $name = $file;
66 $name =~ s,.*/,,;
67
d0ebaf51
MCC
68 my $nametag = "File $name";
69 $data{$nametag}->{what} = "File $name";
70 $data{$nametag}->{type} = "File";
71 $data{$nametag}->{file} = $name;
33e3e991 72 $data{$nametag}->{filepath} = $file;
d0ebaf51 73 $data{$nametag}->{is_file} = 1;
61439c4a 74 $data{$nametag}->{line_no} = 1;
d0ebaf51 75
bbc249f2
MCC
76 my $type = $file;
77 $type =~ s,.*/(.*)/.*,$1,;
78
79 my $what;
80 my $new_what;
234948bf 81 my $tag = "";
bbc249f2 82 my $ln;
6619c661 83 my $xrefs;
4e6a6234 84 my $space;
d0ebaf51 85 my @labels;
234948bf 86 my $label = "";
bbc249f2
MCC
87
88 print STDERR "Opening $file\n" if ($debug > 1);
89 open IN, $file;
90 while(<IN>) {
91 $ln++;
4e6a6234 92 if (m/^(\S+)(:\s*)(.*)/i) {
bbc249f2 93 my $new_tag = lc($1);
4e6a6234
MCC
94 my $sep = $2;
95 my $content = $3;
bbc249f2 96
7ce7b89b 97 if (!($new_tag =~ m/(what|where|date|kernelversion|contact|description|users)/)) {
bbc249f2 98 if ($tag eq "description") {
4e6a6234
MCC
99 # New "tag" is actually part of
100 # description. Don't consider it a tag
101 $new_tag = "";
7d7ea8d2 102 } elsif ($tag ne "") {
bbc249f2
MCC
103 parse_error($file, $ln, "tag '$tag' is invalid", $_);
104 }
105 }
106
2c0700e7
MCC
107 # Invalid, but it is a common mistake
108 if ($new_tag eq "where") {
109 parse_error($file, $ln, "tag 'Where' is invalid. Should be 'What:' instead", $_);
110 $new_tag = "what";
111 }
112
bbc249f2 113 if ($new_tag =~ m/what/) {
4e6a6234 114 $space = "";
234948bf
MCC
115 $content =~ s/[,.;]$//;
116
c7ba3334
MCC
117 push @{$symbols{$content}->{file}}, " $file:" . ($ln - 1);
118
bbc249f2
MCC
119 if ($tag =~ m/what/) {
120 $what .= ", " . $content;
121 } else {
234948bf
MCC
122 if ($what) {
123 parse_error($file, $ln, "What '$what' doesn't have a description", "") if (!$data{$what}->{description});
124
125 foreach my $w(split /, /, $what) {
c7ba3334 126 $symbols{$w}->{xref} = $what;
234948bf
MCC
127 };
128 }
4e6a6234 129
bbc249f2 130 $what = $content;
d0ebaf51 131 $label = $content;
bbc249f2
MCC
132 $new_what = 1;
133 }
d0ebaf51 134 push @labels, [($content, $label)];
bbc249f2 135 $tag = $new_tag;
6619c661 136
234948bf 137 push @{$data{$nametag}->{symbols}}, $content if ($data{$nametag}->{what});
bbc249f2
MCC
138 next;
139 }
140
7d7ea8d2 141 if ($tag ne "" && $new_tag) {
4e6a6234 142 $tag = $new_tag;
bbc249f2 143
4e6a6234 144 if ($new_what) {
234948bf 145 @{$data{$what}->{label_list}} = @labels if ($data{$nametag}->{what});
d0ebaf51
MCC
146 @labels = ();
147 $label = "";
4e6a6234 148 $new_what = 0;
bbc249f2 149
4e6a6234 150 $data{$what}->{type} = $type;
c7ba3334
MCC
151 if (!defined($data{$what}->{file})) {
152 $data{$what}->{file} = $name;
153 $data{$what}->{filepath} = $file;
154 } else {
155 if ($name ne $data{$what}->{file}) {
156 $data{$what}->{file} .= " " . $name;
157 $data{$what}->{filepath} .= " " . $file;
158 }
159 }
4e6a6234 160 print STDERR "\twhat: $what\n" if ($debug > 1);
c7ba3334
MCC
161 $data{$what}->{line_no} = $ln;
162 } else {
163 $data{$what}->{line_no} = $ln if (!defined($data{$what}->{line_no}));
4e6a6234 164 }
bbc249f2 165
4e6a6234
MCC
166 if (!$what) {
167 parse_error($file, $ln, "'What:' should come first:", $_);
168 next;
169 }
f82a8a74
MCC
170 if ($new_tag eq "description") {
171 $sep =~ s,:, ,;
11ce90a4 172 $content = ' ' x length($new_tag) . $sep . $content;
f82a8a74
MCC
173 while ($content =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {}
174 if ($content =~ m/^(\s*)(\S.*)$/) {
175 # Preserve initial spaces for the first line
11ce90a4 176 $space = $1;
f82a8a74
MCC
177 $content = "$2\n";
178 $data{$what}->{$tag} .= $content;
179 } else {
180 undef($space);
4e6a6234 181 }
e9bca891 182
4e6a6234
MCC
183 } else {
184 $data{$what}->{$tag} = $content;
185 }
bbc249f2
MCC
186 next;
187 }
bbc249f2
MCC
188 }
189
4e6a6234 190 # Store any contents before tags at the database
d0ebaf51
MCC
191 if (!$tag && $data{$nametag}->{what}) {
192 $data{$nametag}->{description} .= $_;
6619c661
MCC
193 next;
194 }
bbc249f2 195
4e6a6234 196 if ($tag eq "description") {
e9bca891
MCC
197 my $content = $_;
198 while ($content =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {}
f82a8a74
MCC
199 if (m/^\s*\n/) {
200 $data{$what}->{$tag} .= "\n";
201 next;
202 }
203
204 if (!defined($space)) {
e9bca891 205 # Preserve initial spaces for the first line
f82a8a74 206 if ($content =~ m/^(\s*)(\S.*)$/) {
e9bca891 207 $space = $1;
f82a8a74 208 $content = "$2\n";
4e6a6234
MCC
209 }
210 } else {
4e6a6234 211 $space = "" if (!($content =~ s/^($space)//));
4e6a6234 212 }
f82a8a74
MCC
213 $data{$what}->{$tag} .= $content;
214
4e6a6234
MCC
215 next;
216 }
bbc249f2
MCC
217 if (m/^\s*(.*)/) {
218 $data{$what}->{$tag} .= "\n$1";
219 $data{$what}->{$tag} =~ s/\n+$//;
220 next;
221 }
222
223 # Everything else is error
224 parse_error($file, $ln, "Unexpected line:", $_);
225 }
234948bf
MCC
226 $data{$nametag}->{description} =~ s/^\n+// if ($data{$nametag}->{description});
227 if ($what) {
228 parse_error($file, $ln, "What '$what' doesn't have a description", "") if (!$data{$what}->{description});
229
230 foreach my $w(split /, /,$what) {
c7ba3334 231 $symbols{$w}->{xref} = $what;
234948bf
MCC
232 };
233 }
bbc249f2
MCC
234 close IN;
235}
236
234948bf
MCC
237sub create_labels {
238 my %labels;
bbc249f2 239
234948bf
MCC
240 foreach my $what (keys %data) {
241 next if ($data{$what}->{file} eq "File");
4e6a6234 242
234948bf 243 foreach my $p (@{$data{$what}->{label_list}}) {
d0ebaf51
MCC
244 my ($content, $label) = @{$p};
245 $label = "abi_" . $label . " ";
246 $label =~ tr/A-Z/a-z/;
247
248 # Convert special chars to "_"
249 $label =~s/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\xff])/_/g;
250 $label =~ s,_+,_,g;
251 $label =~ s,_$,,;
252
2e7ce055
MCC
253 # Avoid duplicated labels
254 while (defined($labels{$label})) {
255 my @chars = ("A".."Z", "a".."z");
256 $label .= $chars[rand @chars];
257 }
258 $labels{$label} = 1;
259
234948bf 260 $data{$what}->{label} = $label;
d0ebaf51
MCC
261
262 # only one label is enough
263 last;
6619c661 264 }
234948bf
MCC
265 }
266}
267
268#
269# Outputs the book on ReST format
270#
271
272sub output_rest {
273 create_labels();
274
275 foreach my $what (sort {
276 ($data{$a}->{type} eq "File") cmp ($data{$b}->{type} eq "File") ||
277 $a cmp $b
278 } keys %data) {
279 my $type = $data{$what}->{type};
c7ba3334
MCC
280
281 my @file = split / /, $data{$what}->{file};
282 my @filepath = split / /, $data{$what}->{filepath};
234948bf
MCC
283
284 if ($enable_lineno) {
285 printf "#define LINENO %s%s#%s\n\n",
c7ba3334 286 $prefix, $file[0],
234948bf
MCC
287 $data{$what}->{line_no};
288 }
6619c661 289
234948bf
MCC
290 my $w = $what;
291 $w =~ s/([\(\)\_\-\*\=\^\~\\])/\\$1/g;
6619c661 292
c7ba3334 293 if ($type ne "File") {
234948bf 294 printf ".. _%s:\n\n", $data{$what}->{label};
45f96517 295
234948bf 296 my @names = split /, /,$w;
45f96517
MCC
297 my $len = 0;
298
299 foreach my $name (@names) {
300 $len = length($name) if (length($name) > $len);
301 }
302
303 print "What:\n\n";
304
305 print "+-" . "-" x $len . "-+\n";
306 foreach my $name (@names) {
307 printf "| %s", $name . " " x ($len - length($name)) . " |\n";
308 print "+-" . "-" x $len . "-+\n";
309 }
45f96517 310
c7ba3334
MCC
311 print "\n";
312 }
313
314 for (my $i = 0; $i < scalar(@filepath); $i++) {
315 my $path = $filepath[$i];
316 my $f = $file[$i];
317
318 $path =~ s,.*/(.*/.*),$1,;;
319 $path =~ s,[/\-],_,g;;
320 my $fileref = "abi_file_".$path;
321
322 if ($type eq "File") {
323 my $bar = $w;
324 $bar =~ s/./-/g;
325
326 print ".. _$fileref:\n\n";
327 print "$w\n$bar\n\n";
328 } else {
329 print "Defined on file :ref:`$f <$fileref>`\n\n";
330 }
234948bf 331 }
bbc249f2 332
234948bf
MCC
333 my $desc = "";
334 $desc = $data{$what}->{description} if (defined($data{$what}->{description}));
335 $desc =~ s/\s+$/\n/;
bbc249f2 336
4e6a6234 337 if (!($desc =~ /^\s*$/)) {
11ce90a4
MCC
338 if ($description_is_rst) {
339 print "$desc\n\n";
4e6a6234 340 } else {
11ce90a4 341 $desc =~ s/^\s+//;
bbc249f2 342
11ce90a4
MCC
343 # Remove title markups from the description, as they won't work
344 $desc =~ s/\n[\-\*\=\^\~]+\n/\n\n/g;
345
346 if ($desc =~ m/\:\n/ || $desc =~ m/\n[\t ]+/ || $desc =~ m/[\x00-\x08\x0b-\x1f\x7b-\xff]/) {
347 # put everything inside a code block
348 $desc =~ s/\n/\n /g;
349
350 print "::\n\n";
351 print " $desc\n\n";
352 } else {
353 # Escape any special chars from description
354 $desc =~s/([\x00-\x08\x0b-\x1f\x21-\x2a\x2d\x2f\x3c-\x40\x5c\x5e-\x60\x7b-\xff])/\\$1/g;
355 print "$desc\n\n";
356 }
4e6a6234 357 }
bbc249f2 358 } else {
d0ebaf51 359 print "DESCRIPTION MISSING for $what\n\n" if (!$data{$what}->{is_file});
bbc249f2 360 }
6619c661 361
234948bf 362 if ($data{$what}->{symbols}) {
d0ebaf51
MCC
363 printf "Has the following ABI:\n\n";
364
234948bf 365 foreach my $content(@{$data{$what}->{symbols}}) {
c7ba3334 366 my $label = $data{$symbols{$content}->{xref}}->{label};
d0ebaf51
MCC
367
368 # Escape special chars from content
369 $content =~s/([\x00-\x1f\x21-\x2f\x3a-\x40\x7b-\xff])/\\$1/g;
370
371 print "- :ref:`$content <$label>`\n\n";
372 }
373 }
a16ab14e
MCC
374
375 if (defined($data{$what}->{users})) {
376 my $users = $data{$what}->{users};
377
378 $users =~ s/\n/\n\t/g;
379 printf "Users:\n\t%s\n\n", $users if ($users ne "");
380 }
381
bbc249f2
MCC
382 }
383}
384
33e3e991
MCC
385#
386# Searches for ABI symbols
387#
388sub search_symbols {
389 foreach my $what (sort keys %data) {
390 next if (!($what =~ m/($arg)/));
391
392 my $type = $data{$what}->{type};
393 next if ($type eq "File");
394
395 my $file = $data{$what}->{filepath};
396
397 my $bar = $what;
398 $bar =~ s/./-/g;
399
400 print "\n$what\n$bar\n\n";
401
234948bf
MCC
402 my $kernelversion = $data{$what}->{kernelversion} if (defined($data{$what}->{kernelversion}));
403 my $contact = $data{$what}->{contact} if (defined($data{$what}->{contact}));
404 my $users = $data{$what}->{users} if (defined($data{$what}->{users}));
405 my $date = $data{$what}->{date} if (defined($data{$what}->{date}));
406 my $desc = $data{$what}->{description} if (defined($data{$what}->{description}));
407
408 $kernelversion =~ s/^\s+// if ($kernelversion);
409 $contact =~ s/^\s+// if ($contact);
410 if ($users) {
411 $users =~ s/^\s+//;
412 $users =~ s/\n//g;
413 }
414 $date =~ s/^\s+// if ($date);
415 $desc =~ s/^\s+// if ($desc);
33e3e991
MCC
416
417 printf "Kernel version:\t\t%s\n", $kernelversion if ($kernelversion);
418 printf "Date:\t\t\t%s\n", $date if ($date);
419 printf "Contact:\t\t%s\n", $contact if ($contact);
420 printf "Users:\t\t\t%s\n", $users if ($users);
c7ba3334 421 print "Defined on file(s):\t$file\n\n";
33e3e991
MCC
422 print "Description:\n\n$desc";
423 }
424}
425
61439c4a
MCC
426# Ensure that the prefix will always end with a slash
427# While this is not needed for find, it makes the patch nicer
428# with --enable-lineno
429$prefix =~ s,/?$,/,;
33e3e991 430
bbc249f2
MCC
431#
432# Parses all ABI files located at $prefix dir
433#
434find({wanted =>\&parse_abi, no_chdir => 1}, $prefix);
435
436print STDERR Data::Dumper->Dump([\%data], [qw(*data)]) if ($debug);
437
438#
33e3e991 439# Handles the command
bbc249f2 440#
c7ba3334 441if ($cmd eq "search") {
33e3e991 442 search_symbols;
c7ba3334
MCC
443} else {
444 if ($cmd eq "rest") {
445 output_rest;
446 }
447
448 # Warn about duplicated ABI entries
449 foreach my $what(sort keys %symbols) {
450 my @files = @{$symbols{$what}->{file}};
451
452 next if (scalar(@files) == 1);
bbc249f2 453
c7ba3334
MCC
454 printf STDERR "Warning: $what is defined %d times: @files\n",
455 scalar(@files);
456 }
457}
bbc249f2
MCC
458
459__END__
460
461=head1 NAME
462
463abi_book.pl - parse the Linux ABI files and produce a ReST book.
464
465=head1 SYNOPSIS
466
61439c4a
MCC
467B<abi_book.pl> [--debug] [--enable-lineno] [--man] [--help]
468 [--(no-)rst-source] [--dir=<dir>] <COMAND> [<ARGUMENT>]
33e3e991
MCC
469
470Where <COMMAND> can be:
471
472=over 8
473
474B<search> [SEARCH_REGEX] - search for [SEARCH_REGEX] inside ABI
475
7ce7b89b
MCC
476B<rest> - output the ABI in ReST markup language
477
478B<validate> - validate the ABI contents
33e3e991
MCC
479
480=back
bbc249f2
MCC
481
482=head1 OPTIONS
483
484=over 8
485
33e3e991
MCC
486=item B<--dir>
487
488Changes the location of the ABI search. By default, it uses
489the Documentation/ABI directory.
490
11ce90a4
MCC
491=item B<--rst-source> and B<--no-rst-source>
492
493The input file may be using ReST syntax or not. Those two options allow
494selecting between a rst-compliant source ABI (--rst-source), or a
495plain text that may be violating ReST spec, so it requres some escaping
496logic (--no-rst-source).
497
61439c4a
MCC
498=item B<--enable-lineno>
499
500Enable output of #define LINENO lines.
501
bbc249f2
MCC
502=item B<--debug>
503
504Put the script in verbose mode, useful for debugging. Can be called multiple
505times, to increase verbosity.
506
507=item B<--help>
508
509Prints a brief help message and exits.
510
511=item B<--man>
512
513Prints the manual page and exits.
514
515=back
516
517=head1 DESCRIPTION
518
33e3e991
MCC
519Parse the Linux ABI files from ABI DIR (usually located at Documentation/ABI),
520allowing to search for ABI symbols or to produce a ReST book containing
521the Linux ABI documentation.
522
523=head1 EXAMPLES
524
525Search for all stable symbols with the word "usb":
526
527=over 8
528
529$ scripts/get_abi.pl search usb --dir Documentation/ABI/stable
530
531=back
532
533Search for all symbols that match the regex expression "usb.*cap":
534
535=over 8
536
537$ scripts/get_abi.pl search usb.*cap
538
539=back
540
541Output all obsoleted symbols in ReST format
542
543=over 8
544
545$ scripts/get_abi.pl rest --dir Documentation/ABI/obsolete
546
547=back
bbc249f2
MCC
548
549=head1 BUGS
550
7ce7b89b 551Report bugs to Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
bbc249f2
MCC
552
553=head1 COPYRIGHT
554
7ce7b89b 555Copyright (c) 2016-2019 by Mauro Carvalho Chehab <mchehab+samsung@kernel.org>.
bbc249f2
MCC
556
557License GPLv2: GNU GPL version 2 <http://gnu.org/licenses/gpl.html>.
558
559This is free software: you are free to change and redistribute it.
560There is NO WARRANTY, to the extent permitted by law.
561
562=cut