blob: 329ace635ac2b6561e8399deb8abc18e2e184cb9 [file] [log] [blame]
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -03001#!/usr/bin/perl
2
3use strict;
4use Pod::Usage;
5use Getopt::Long;
6use File::Find;
7use Fcntl ':mode';
8
9my $help;
10my $man;
11my $debug;
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030012my $prefix="Documentation/ABI";
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030013
14GetOptions(
15 "debug|d+" => \$debug,
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030016 "dir=s" => \$prefix,
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030017 'help|?' => \$help,
18 man => \$man
19) or pod2usage(2);
20
21pod2usage(1) if $help;
22pod2usage(-exitstatus => 0, -verbose => 2) if $man;
23
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030024pod2usage(2) if (scalar @ARGV < 1 || @ARGV > 2);
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030025
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030026my ($cmd, $arg) = @ARGV;
27
28pod2usage(2) if ($cmd ne "search" && $cmd ne "rest");
29pod2usage(2) if ($cmd eq "search" && !$arg);
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030030
31require Data::Dumper if ($debug);
32
33my %data;
34
35#
36# Displays an error message, printing file name and line
37#
38sub parse_error($$$$) {
39 my ($file, $ln, $msg, $data) = @_;
40
41 print STDERR "file $file#$ln: $msg at\n\t$data";
42}
43
44#
45# Parse an ABI file, storing its contents at %data
46#
47sub parse_abi {
48 my $file = $File::Find::name;
49
50 my $mode = (stat($file))[2];
51 return if ($mode & S_IFDIR);
52 return if ($file =~ m,/README,);
53
54 my $name = $file;
55 $name =~ s,.*/,,;
56
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -030057 my $nametag = "File $name";
58 $data{$nametag}->{what} = "File $name";
59 $data{$nametag}->{type} = "File";
60 $data{$nametag}->{file} = $name;
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030061 $data{$nametag}->{filepath} = $file;
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -030062 $data{$nametag}->{is_file} = 1;
63
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030064 my $type = $file;
65 $type =~ s,.*/(.*)/.*,$1,;
66
67 my $what;
68 my $new_what;
69 my $tag;
70 my $ln;
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -030071 my $xrefs;
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -030072 my $space;
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -030073 my @labels;
74 my $label;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030075
76 print STDERR "Opening $file\n" if ($debug > 1);
77 open IN, $file;
78 while(<IN>) {
79 $ln++;
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -030080 if (m/^(\S+)(:\s*)(.*)/i) {
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030081 my $new_tag = lc($1);
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -030082 my $sep = $2;
83 my $content = $3;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030084
85 if (!($new_tag =~ m/(what|date|kernelversion|contact|description|users)/)) {
86 if ($tag eq "description") {
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -030087 # New "tag" is actually part of
88 # description. Don't consider it a tag
89 $new_tag = "";
Mauro Carvalho Chehab7d7ea8d2019-06-20 14:23:01 -030090 } elsif ($tag ne "") {
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030091 parse_error($file, $ln, "tag '$tag' is invalid", $_);
92 }
93 }
94
95 if ($new_tag =~ m/what/) {
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -030096 $space = "";
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030097 if ($tag =~ m/what/) {
98 $what .= ", " . $content;
99 } else {
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300100 parse_error($file, $ln, "What '$what' doesn't have a description", "") if ($what && !$data{$what}->{description});
101
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300102 $what = $content;
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300103 $label = $content;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300104 $new_what = 1;
105 }
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300106 push @labels, [($content, $label)];
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300107 $tag = $new_tag;
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -0300108
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300109 push @{$data{$nametag}->{xrefs}}, [($content, $label)] if ($data{$nametag}->{what});
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300110 next;
111 }
112
Mauro Carvalho Chehab7d7ea8d2019-06-20 14:23:01 -0300113 if ($tag ne "" && $new_tag) {
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300114 $tag = $new_tag;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300115
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300116 if ($new_what) {
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300117 @{$data{$what}->{label}} = @labels if ($data{$nametag}->{what});
118 @labels = ();
119 $label = "";
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300120 $new_what = 0;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300121
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300122 $data{$what}->{type} = $type;
123 $data{$what}->{file} = $name;
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300124 $data{$what}->{filepath} = $file;
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300125 print STDERR "\twhat: $what\n" if ($debug > 1);
126 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300127
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300128 if (!$what) {
129 parse_error($file, $ln, "'What:' should come first:", $_);
130 next;
131 }
132 if ($tag eq "description") {
133 next if ($content =~ m/^\s*$/);
134 if ($content =~ m/^(\s*)(.*)/) {
135 my $new_content = $2;
136 $space = $new_tag . $sep . $1;
137 while ($space =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {}
138 $space =~ s/./ /g;
139 $data{$what}->{$tag} .= "$new_content\n";
140 }
141 } else {
142 $data{$what}->{$tag} = $content;
143 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300144 next;
145 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300146 }
147
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300148 # Store any contents before tags at the database
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300149 if (!$tag && $data{$nametag}->{what}) {
150 $data{$nametag}->{description} .= $_;
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -0300151 next;
152 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300153
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300154 if ($tag eq "description") {
155 if (!$data{$what}->{description}) {
156 next if (m/^\s*\n/);
157 if (m/^(\s*)(.*)/) {
158 $space = $1;
159 while ($space =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {}
160 $data{$what}->{$tag} .= "$2\n";
161 }
162 } else {
163 my $content = $_;
164 if (m/^\s*\n/) {
165 $data{$what}->{$tag} .= $content;
166 next;
167 }
168
169 while ($content =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {}
170 $space = "" if (!($content =~ s/^($space)//));
171
172 # Compress spaces with tabs
173 $content =~ s<^ {8}> <\t>;
174 $content =~ s<^ {1,7}\t> <\t>;
175 $content =~ s< {1,7}\t> <\t>;
176 $data{$what}->{$tag} .= $content;
177 }
178 next;
179 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300180 if (m/^\s*(.*)/) {
181 $data{$what}->{$tag} .= "\n$1";
182 $data{$what}->{$tag} =~ s/\n+$//;
183 next;
184 }
185
186 # Everything else is error
187 parse_error($file, $ln, "Unexpected line:", $_);
188 }
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300189 $data{$nametag}->{description} =~ s/^\n+//;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300190 close IN;
191}
192
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300193#
194# Outputs the book on ReST format
195#
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300196
Mauro Carvalho Chehab2e7ce052019-06-20 14:23:02 -0300197my %labels;
198
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300199sub output_rest {
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300200 foreach my $what (sort {
201 ($data{$a}->{type} eq "File") cmp ($data{$b}->{type} eq "File") ||
202 $a cmp $b
203 } keys %data) {
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300204 my $type = $data{$what}->{type};
205 my $file = $data{$what}->{file};
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300206 my $filepath = $data{$what}->{filepath};
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300207
208 my $w = $what;
209 $w =~ s/([\(\)\_\-\*\=\^\~\\])/\\$1/g;
210
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300211
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300212 foreach my $p (@{$data{$what}->{label}}) {
213 my ($content, $label) = @{$p};
214 $label = "abi_" . $label . " ";
215 $label =~ tr/A-Z/a-z/;
216
217 # Convert special chars to "_"
218 $label =~s/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\xff])/_/g;
219 $label =~ s,_+,_,g;
220 $label =~ s,_$,,;
221
Mauro Carvalho Chehab2e7ce052019-06-20 14:23:02 -0300222 # Avoid duplicated labels
223 while (defined($labels{$label})) {
224 my @chars = ("A".."Z", "a".."z");
225 $label .= $chars[rand @chars];
226 }
227 $labels{$label} = 1;
228
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300229 $data{$what}->{label} .= $label;
230
231 printf ".. _%s:\n\n", $label;
232
233 # only one label is enough
234 last;
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -0300235 }
236
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -0300237
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300238 $filepath =~ s,.*/(.*/.*),\1,;;
239 $filepath =~ s,[/\-],_,g;;
240 my $fileref = "abi_file_".$filepath;
241
242 if ($type eq "File") {
243 my $bar = $w;
244 $bar =~ s/./-/g;
245
246 print ".. _$fileref:\n\n";
247 print "$w\n$bar\n\n";
248 } else {
249 my @names = split /\s*,\s*/,$w;
250
251 my $len = 0;
252
253 foreach my $name (@names) {
254 $len = length($name) if (length($name) > $len);
255 }
256
257 print "What:\n\n";
258
259 print "+-" . "-" x $len . "-+\n";
260 foreach my $name (@names) {
261 printf "| %s", $name . " " x ($len - length($name)) . " |\n";
262 print "+-" . "-" x $len . "-+\n";
263 }
264 print "\n";
265 }
266
267 print "Defined on file :ref:`$file <$fileref>`\n\n" if ($type ne "File");
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300268
269 my $desc = $data{$what}->{description};
270 $desc =~ s/^\s+//;
271
272 # Remove title markups from the description, as they won't work
273 $desc =~ s/\n[\-\*\=\^\~]+\n/\n/g;
274
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300275 if (!($desc =~ /^\s*$/)) {
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300276 if ($desc =~ m/\:\n/ || $desc =~ m/\n[\t ]+/ || $desc =~ m/[\x00-\x08\x0b-\x1f\x7b-\xff]/) {
277 # put everything inside a code block
278 $desc =~ s/\n/\n /g;
279
280 print "::\n\n";
281 print " $desc\n\n";
282 } else {
283 # Escape any special chars from description
284 $desc =~s/([\x00-\x08\x0b-\x1f\x21-\x2a\x2d\x2f\x3c-\x40\x5c\x5e-\x60\x7b-\xff])/\\$1/g;
285
286 print "$desc\n\n";
287 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300288 } else {
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300289 print "DESCRIPTION MISSING for $what\n\n" if (!$data{$what}->{is_file});
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300290 }
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -0300291
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300292 if ($data{$what}->{xrefs}) {
293 printf "Has the following ABI:\n\n";
294
295 foreach my $p(@{$data{$what}->{xrefs}}) {
296 my ($content, $label) = @{$p};
297 $label = "abi_" . $label . " ";
298 $label =~ tr/A-Z/a-z/;
299
300 # Convert special chars to "_"
301 $label =~s/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\xff])/_/g;
302 $label =~ s,_+,_,g;
303 $label =~ s,_$,,;
304
305 # Escape special chars from content
306 $content =~s/([\x00-\x1f\x21-\x2f\x3a-\x40\x7b-\xff])/\\$1/g;
307
308 print "- :ref:`$content <$label>`\n\n";
309 }
310 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300311 }
312}
313
314#
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300315# Searches for ABI symbols
316#
317sub search_symbols {
318 foreach my $what (sort keys %data) {
319 next if (!($what =~ m/($arg)/));
320
321 my $type = $data{$what}->{type};
322 next if ($type eq "File");
323
324 my $file = $data{$what}->{filepath};
325
326 my $bar = $what;
327 $bar =~ s/./-/g;
328
329 print "\n$what\n$bar\n\n";
330
331 my $kernelversion = $data{$what}->{kernelversion};
332 my $contact = $data{$what}->{contact};
333 my $users = $data{$what}->{users};
334 my $date = $data{$what}->{date};
335 my $desc = $data{$what}->{description};
336 $kernelversion =~ s/^\s+//;
337 $contact =~ s/^\s+//;
338 $users =~ s/^\s+//;
339 $users =~ s/\n//g;
340 $date =~ s/^\s+//;
341 $desc =~ s/^\s+//;
342
343 printf "Kernel version:\t\t%s\n", $kernelversion if ($kernelversion);
344 printf "Date:\t\t\t%s\n", $date if ($date);
345 printf "Contact:\t\t%s\n", $contact if ($contact);
346 printf "Users:\t\t\t%s\n", $users if ($users);
347 print "Defined on file:\t$file\n\n";
348 print "Description:\n\n$desc";
349 }
350}
351
352
353#
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300354# Parses all ABI files located at $prefix dir
355#
356find({wanted =>\&parse_abi, no_chdir => 1}, $prefix);
357
358print STDERR Data::Dumper->Dump([\%data], [qw(*data)]) if ($debug);
359
360#
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300361# Handles the command
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300362#
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300363if ($cmd eq "rest") {
364 output_rest;
365} else {
366 search_symbols;
367}
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300368
369
370__END__
371
372=head1 NAME
373
374abi_book.pl - parse the Linux ABI files and produce a ReST book.
375
376=head1 SYNOPSIS
377
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300378B<abi_book.pl> [--debug] <COMAND> [<ARGUMENT>]
379
380Where <COMMAND> can be:
381
382=over 8
383
384B<search> [SEARCH_REGEX] - search for [SEARCH_REGEX] inside ABI
385
386B<rest> - output the ABI in ReST markup language
387
388=back
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300389
390=head1 OPTIONS
391
392=over 8
393
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300394=item B<--dir>
395
396Changes the location of the ABI search. By default, it uses
397the Documentation/ABI directory.
398
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300399=item B<--debug>
400
401Put the script in verbose mode, useful for debugging. Can be called multiple
402times, to increase verbosity.
403
404=item B<--help>
405
406Prints a brief help message and exits.
407
408=item B<--man>
409
410Prints the manual page and exits.
411
412=back
413
414=head1 DESCRIPTION
415
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300416Parse the Linux ABI files from ABI DIR (usually located at Documentation/ABI),
417allowing to search for ABI symbols or to produce a ReST book containing
418the Linux ABI documentation.
419
420=head1 EXAMPLES
421
422Search for all stable symbols with the word "usb":
423
424=over 8
425
426$ scripts/get_abi.pl search usb --dir Documentation/ABI/stable
427
428=back
429
430Search for all symbols that match the regex expression "usb.*cap":
431
432=over 8
433
434$ scripts/get_abi.pl search usb.*cap
435
436=back
437
438Output all obsoleted symbols in ReST format
439
440=over 8
441
442$ scripts/get_abi.pl rest --dir Documentation/ABI/obsolete
443
444=back
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300445
446=head1 BUGS
447
448Report bugs to Mauro Carvalho Chehab <mchehab@s-opensource.com>
449
450=head1 COPYRIGHT
451
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300452Copyright (c) 2016-2017 by Mauro Carvalho Chehab <mchehab@s-opensource.com>.
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300453
454License GPLv2: GNU GPL version 2 <http://gnu.org/licenses/gpl.html>.
455
456This is free software: you are free to change and redistribute it.
457There is NO WARRANTY, to the extent permitted by law.
458
459=cut