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