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