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