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