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