2 # SPDX-License-Identifier: GPL-2.0
15 my $enable_lineno = 0;
16 my $prefix="Documentation/ABI";
19 # If true, assumes that the description is formatted with ReST
21 my $description_is_rst = 1;
24 "debug|d+" => \$debug,
25 "enable-lineno" => \$enable_lineno,
26 "rst-source!" => \$description_is_rst,
32 pod2usage(1) if $help;
33 pod2usage(-exitstatus => 0, -verbose => 2) if $man;
35 pod2usage(2) if (scalar @ARGV < 1 || @ARGV > 2);
37 my ($cmd, $arg) = @ARGV;
39 pod2usage(2) if ($cmd ne "search" && $cmd ne "rest" && $cmd ne "validate");
40 pod2usage(2) if ($cmd eq "search" && !$arg);
42 require Data::Dumper if ($debug);
48 # Displays an error message, printing file name and line
50 sub parse_error($$$$) {
51 my ($file, $ln, $msg, $data) = @_;
55 print STDERR "Warning: file $file#$ln:\n\t$msg";
58 print STDERR ". Line\n\t\t$data";
65 # Parse an ABI file, storing its contents at %data
68 my $file = $File::Find::name;
70 my $mode = (stat($file))[2];
71 return if ($mode & S_IFDIR);
72 return if ($file =~ m,/README,);
78 $fn =~ s,Documentation/ABI/,,;
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;
89 $type =~ s,.*/(.*)/.*,$1,;
100 print STDERR "Opening $file\n" if ($debug > 1);
104 if (m/^(\S+)(:\s*)(.*)/i) {
105 my $new_tag = lc($1);
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
114 } elsif ($tag ne "") {
115 parse_error($file, $ln, "tag '$tag' is invalid", $_);
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", "");
125 if ($new_tag =~ m/what/) {
127 $content =~ s/[,.;]$//;
129 push @{$symbols{$content}->{file}}, " $file:" . ($ln - 1);
131 if ($tag =~ m/what/) {
132 $what .= ", " . $content;
135 parse_error($file, $ln, "What '$what' doesn't have a description", "") if (!$data{$what}->{description});
137 foreach my $w(split /, /, $what) {
138 $symbols{$w}->{xref} = $what;
146 push @labels, [($content, $label)];
149 push @{$data{$nametag}->{symbols}}, $content if ($data{$nametag}->{what});
153 if ($tag ne "" && $new_tag) {
157 @{$data{$what}->{label_list}} = @labels if ($data{$nametag}->{what});
162 $data{$what}->{type} = $type;
163 if (!defined($data{$what}->{file})) {
164 $data{$what}->{file} = $name;
165 $data{$what}->{filepath} = $file;
167 if ($name ne $data{$what}->{file}) {
168 $data{$what}->{file} .= " " . $name;
169 $data{$what}->{filepath} .= " " . $file;
172 print STDERR "\twhat: $what\n" if ($debug > 1);
173 $data{$what}->{line_no} = $ln;
175 $data{$what}->{line_no} = $ln if (!defined($data{$what}->{line_no}));
179 parse_error($file, $ln, "'What:' should come first:", $_);
182 if ($new_tag eq "description") {
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
190 $data{$what}->{$tag} .= $content;
196 $data{$what}->{$tag} = $content;
202 # Store any contents before tags at the database
203 if (!$tag && $data{$nametag}->{what}) {
204 $data{$nametag}->{description} .= $_;
208 if ($tag eq "description") {
210 while ($content =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {}
212 $data{$what}->{$tag} .= "\n";
216 if (!defined($space)) {
217 # Preserve initial spaces for the first line
218 if ($content =~ m/^(\s*)(\S.*)$/) {
223 $space = "" if (!($content =~ s/^($space)//));
225 $data{$what}->{$tag} .= $content;
230 $data{$what}->{$tag} .= "\n$1";
231 $data{$what}->{$tag} =~ s/\n+$//;
235 # Everything else is error
236 parse_error($file, $ln, "Unexpected content", $_);
238 $data{$nametag}->{description} =~ s/^\n+// if ($data{$nametag}->{description});
240 parse_error($file, $ln, "What '$what' doesn't have a description", "") if (!$data{$what}->{description});
242 foreach my $w(split /, /,$what) {
243 $symbols{$w}->{xref} = $what;
252 foreach my $what (keys %data) {
253 next if ($data{$what}->{file} eq "File");
255 foreach my $p (@{$data{$what}->{label_list}}) {
256 my ($content, $label) = @{$p};
257 $label = "abi_" . $label . " ";
258 $label =~ tr/A-Z/a-z/;
260 # Convert special chars to "_"
261 $label =~s/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\xff])/_/g;
265 # Avoid duplicated labels
266 while (defined($labels{$label})) {
267 my @chars = ("A".."Z", "a".."z");
268 $label .= $chars[rand @chars];
272 $data{$what}->{label} = $label;
274 # only one label is enough
281 # Outputs the book on ReST format
284 # \b doesn't work well with paths. So, we need to define something else
285 my $bondary = qr { (?<![\w\/\`\{])(?=[\w\/\`\{])|(?<=[\w\/\`\{])(?![\w\/\`\{]) }x;
290 foreach my $what (sort {
291 ($data{$a}->{type} eq "File") cmp ($data{$b}->{type} eq "File") ||
294 my $type = $data{$what}->{type};
296 my @file = split / /, $data{$what}->{file};
297 my @filepath = split / /, $data{$what}->{filepath};
299 if ($enable_lineno) {
300 printf "#define LINENO %s%s#%s\n\n",
302 $data{$what}->{line_no};
306 $w =~ s/([\(\)\_\-\*\=\^\~\\])/\\$1/g;
308 if ($type ne "File") {
309 printf ".. _%s:\n\n", $data{$what}->{label};
311 my @names = split /, /,$w;
314 foreach my $name (@names) {
316 $len = length($name) if (length($name) > $len);
319 print "+-" . "-" x $len . "-+\n";
320 foreach my $name (@names) {
321 printf "| %s", $name . " " x ($len - length($name)) . " |\n";
322 print "+-" . "-" x $len . "-+\n";
328 for (my $i = 0; $i < scalar(@filepath); $i++) {
329 my $path = $filepath[$i];
332 $path =~ s,.*/(.*/.*),$1,;;
333 $path =~ s,[/\-],_,g;;
334 my $fileref = "abi_file_".$path;
336 if ($type eq "File") {
337 print ".. _$fileref:\n\n";
339 print "Defined on file :ref:`$f <$fileref>`\n\n";
343 if ($type eq "File") {
346 print "$w\n$bar\n\n";
350 $desc = $data{$what}->{description} if (defined($data{$what}->{description}));
353 if (!($desc =~ /^\s*$/)) {
354 if ($description_is_rst) {
355 # Enrich text by creating cross-references
357 $desc =~ s,Documentation/(?!devicetree)(\S+)\.rst,:doc:`/$1`,g;
359 my @matches = $desc =~ m,Documentation/ABI/([\w\/\-]+),;
360 foreach my $f (@matches) {
363 $path =~ s,.*/(.*/.*),$1,;;
364 $path =~ s,[/\-],_,g;;
365 $xref .= " <abi_file_" . $path . ">";
366 $desc =~ s,\bDocumentation/ABI/$f\b,:ref:`$xref`,g;
369 @matches = $desc =~ m,$bondary(/sys/[^\s\.\,\;\:\*\s\`\'\(\)]+)$bondary,;
371 foreach my $s (@matches) {
372 if (defined($data{$s}) && defined($data{$s}->{label})) {
375 $xref =~ s/([\x00-\x1f\x21-\x2f\x3a-\x40\x7b-\xff])/\\$1/g;
376 $xref = ":ref:`$xref <" . $data{$s}->{label} . ">`";
378 $desc =~ s,$bondary$s$bondary,$xref,g;
386 # Remove title markups from the description, as they won't work
387 $desc =~ s/\n[\-\*\=\^\~]+\n/\n\n/g;
389 if ($desc =~ m/\:\n/ || $desc =~ m/\n[\t ]+/ || $desc =~ m/[\x00-\x08\x0b-\x1f\x7b-\xff]/) {
390 # put everything inside a code block
396 # Escape any special chars from description
397 $desc =~s/([\x00-\x08\x0b-\x1f\x21-\x2a\x2d\x2f\x3c-\x40\x5c\x5e-\x60\x7b-\xff])/\\$1/g;
402 print "DESCRIPTION MISSING for $what\n\n" if (!$data{$what}->{is_file});
405 if ($data{$what}->{symbols}) {
406 printf "Has the following ABI:\n\n";
408 foreach my $content(@{$data{$what}->{symbols}}) {
409 my $label = $data{$symbols{$content}->{xref}}->{label};
411 # Escape special chars from content
412 $content =~s/([\x00-\x1f\x21-\x2f\x3a-\x40\x7b-\xff])/\\$1/g;
414 print "- :ref:`$content <$label>`\n\n";
418 if (defined($data{$what}->{users})) {
419 my $users = $data{$what}->{users};
421 $users =~ s/\n/\n\t/g;
422 printf "Users:\n\t%s\n\n", $users if ($users ne "");
429 # Searches for ABI symbols
432 foreach my $what (sort keys %data) {
433 next if (!($what =~ m/($arg)/));
435 my $type = $data{$what}->{type};
436 next if ($type eq "File");
438 my $file = $data{$what}->{filepath};
443 print "\n$what\n$bar\n\n";
445 my $kernelversion = $data{$what}->{kernelversion} if (defined($data{$what}->{kernelversion}));
446 my $contact = $data{$what}->{contact} if (defined($data{$what}->{contact}));
447 my $users = $data{$what}->{users} if (defined($data{$what}->{users}));
448 my $date = $data{$what}->{date} if (defined($data{$what}->{date}));
449 my $desc = $data{$what}->{description} if (defined($data{$what}->{description}));
451 $kernelversion =~ s/^\s+// if ($kernelversion);
452 $contact =~ s/^\s+// if ($contact);
457 $date =~ s/^\s+// if ($date);
458 $desc =~ s/^\s+// if ($desc);
460 printf "Kernel version:\t\t%s\n", $kernelversion if ($kernelversion);
461 printf "Date:\t\t\t%s\n", $date if ($date);
462 printf "Contact:\t\t%s\n", $contact if ($contact);
463 printf "Users:\t\t\t%s\n", $users if ($users);
464 print "Defined on file(s):\t$file\n\n";
465 print "Description:\n\n$desc";
469 # Ensure that the prefix will always end with a slash
470 # While this is not needed for find, it makes the patch nicer
471 # with --enable-lineno
475 # Parses all ABI files located at $prefix dir
477 find({wanted =>\&parse_abi, no_chdir => 1}, $prefix);
479 print STDERR Data::Dumper->Dump([\%data], [qw(*data)]) if ($debug);
482 # Handles the command
484 if ($cmd eq "search") {
487 if ($cmd eq "rest") {
491 # Warn about duplicated ABI entries
492 foreach my $what(sort keys %symbols) {
493 my @files = @{$symbols{$what}->{file}};
495 next if (scalar(@files) == 1);
497 printf STDERR "Warning: $what is defined %d times: @files\n",
506 abi_book.pl - parse the Linux ABI files and produce a ReST book.
510 B<abi_book.pl> [--debug] [--enable-lineno] [--man] [--help]
511 [--(no-)rst-source] [--dir=<dir>] <COMAND> [<ARGUMENT>]
513 Where <COMMAND> can be:
517 B<search> [SEARCH_REGEX] - search for [SEARCH_REGEX] inside ABI
519 B<rest> - output the ABI in ReST markup language
521 B<validate> - validate the ABI contents
531 Changes the location of the ABI search. By default, it uses
532 the Documentation/ABI directory.
534 =item B<--rst-source> and B<--no-rst-source>
536 The input file may be using ReST syntax or not. Those two options allow
537 selecting between a rst-compliant source ABI (--rst-source), or a
538 plain text that may be violating ReST spec, so it requres some escaping
539 logic (--no-rst-source).
541 =item B<--enable-lineno>
543 Enable output of #define LINENO lines.
547 Put the script in verbose mode, useful for debugging. Can be called multiple
548 times, to increase verbosity.
552 Prints a brief help message and exits.
556 Prints the manual page and exits.
562 Parse the Linux ABI files from ABI DIR (usually located at Documentation/ABI),
563 allowing to search for ABI symbols or to produce a ReST book containing
564 the Linux ABI documentation.
568 Search for all stable symbols with the word "usb":
572 $ scripts/get_abi.pl search usb --dir Documentation/ABI/stable
576 Search for all symbols that match the regex expression "usb.*cap":
580 $ scripts/get_abi.pl search usb.*cap
584 Output all obsoleted symbols in ReST format
588 $ scripts/get_abi.pl rest --dir Documentation/ABI/obsolete
594 Report bugs to Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
598 Copyright (c) 2016-2019 by Mauro Carvalho Chehab <mchehab+samsung@kernel.org>.
600 License GPLv2: GNU GPL version 2 <http://gnu.org/licenses/gpl.html>.
602 This is free software: you are free to change and redistribute it.
603 There is NO WARRANTY, to the extent permitted by law.