2 # SPDX-License-Identifier: GPL-2.0
14 my $enable_lineno = 0;
15 my $prefix="Documentation/ABI";
18 # If true, assumes that the description is formatted with ReST
20 my $description_is_rst = 0;
23 "debug|d+" => \$debug,
24 "enable-lineno" => \$enable_lineno,
25 "rst-source!" => \$description_is_rst,
31 pod2usage(1) if $help;
32 pod2usage(-exitstatus => 0, -verbose => 2) if $man;
34 pod2usage(2) if (scalar @ARGV < 1 || @ARGV > 2);
36 my ($cmd, $arg) = @ARGV;
38 pod2usage(2) if ($cmd ne "search" && $cmd ne "rest" && $cmd ne "validate");
39 pod2usage(2) if ($cmd eq "search" && !$arg);
41 require Data::Dumper if ($debug);
47 # Displays an error message, printing file name and line
49 sub parse_error($$$$) {
50 my ($file, $ln, $msg, $data) = @_;
52 print STDERR "file $file#$ln: $msg at\n\t$data";
56 # Parse an ABI file, storing its contents at %data
59 my $file = $File::Find::name;
61 my $mode = (stat($file))[2];
62 return if ($mode & S_IFDIR);
63 return if ($file =~ m,/README,);
69 $fn =~ s,Documentation/ABI/,,;
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;
80 $type =~ s,.*/(.*)/.*,$1,;
91 print STDERR "Opening $file\n" if ($debug > 1);
95 if (m/^(\S+)(:\s*)(.*)/i) {
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
105 } elsif ($tag ne "") {
106 parse_error($file, $ln, "tag '$tag' is invalid", $_);
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", $_);
116 if ($new_tag =~ m/what/) {
118 $content =~ s/[,.;]$//;
120 push @{$symbols{$content}->{file}}, " $file:" . ($ln - 1);
122 if ($tag =~ m/what/) {
123 $what .= ", " . $content;
126 parse_error($file, $ln, "What '$what' doesn't have a description", "") if (!$data{$what}->{description});
128 foreach my $w(split /, /, $what) {
129 $symbols{$w}->{xref} = $what;
137 push @labels, [($content, $label)];
140 push @{$data{$nametag}->{symbols}}, $content if ($data{$nametag}->{what});
144 if ($tag ne "" && $new_tag) {
148 @{$data{$what}->{label_list}} = @labels if ($data{$nametag}->{what});
153 $data{$what}->{type} = $type;
154 if (!defined($data{$what}->{file})) {
155 $data{$what}->{file} = $name;
156 $data{$what}->{filepath} = $file;
158 if ($name ne $data{$what}->{file}) {
159 $data{$what}->{file} .= " " . $name;
160 $data{$what}->{filepath} .= " " . $file;
163 print STDERR "\twhat: $what\n" if ($debug > 1);
164 $data{$what}->{line_no} = $ln;
166 $data{$what}->{line_no} = $ln if (!defined($data{$what}->{line_no}));
170 parse_error($file, $ln, "'What:' should come first:", $_);
173 if ($new_tag eq "description") {
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
181 $data{$what}->{$tag} .= $content;
187 $data{$what}->{$tag} = $content;
193 # Store any contents before tags at the database
194 if (!$tag && $data{$nametag}->{what}) {
195 $data{$nametag}->{description} .= $_;
199 if ($tag eq "description") {
201 while ($content =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {}
203 $data{$what}->{$tag} .= "\n";
207 if (!defined($space)) {
208 # Preserve initial spaces for the first line
209 if ($content =~ m/^(\s*)(\S.*)$/) {
214 $space = "" if (!($content =~ s/^($space)//));
216 $data{$what}->{$tag} .= $content;
221 $data{$what}->{$tag} .= "\n$1";
222 $data{$what}->{$tag} =~ s/\n+$//;
226 # Everything else is error
227 parse_error($file, $ln, "Unexpected line:", $_);
229 $data{$nametag}->{description} =~ s/^\n+// if ($data{$nametag}->{description});
231 parse_error($file, $ln, "What '$what' doesn't have a description", "") if (!$data{$what}->{description});
233 foreach my $w(split /, /,$what) {
234 $symbols{$w}->{xref} = $what;
243 foreach my $what (keys %data) {
244 next if ($data{$what}->{file} eq "File");
246 foreach my $p (@{$data{$what}->{label_list}}) {
247 my ($content, $label) = @{$p};
248 $label = "abi_" . $label . " ";
249 $label =~ tr/A-Z/a-z/;
251 # Convert special chars to "_"
252 $label =~s/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\xff])/_/g;
256 # Avoid duplicated labels
257 while (defined($labels{$label})) {
258 my @chars = ("A".."Z", "a".."z");
259 $label .= $chars[rand @chars];
263 $data{$what}->{label} = $label;
265 # only one label is enough
272 # Outputs the book on ReST format
278 foreach my $what (sort {
279 ($data{$a}->{type} eq "File") cmp ($data{$b}->{type} eq "File") ||
282 my $type = $data{$what}->{type};
284 my @file = split / /, $data{$what}->{file};
285 my @filepath = split / /, $data{$what}->{filepath};
287 if ($enable_lineno) {
288 printf "#define LINENO %s%s#%s\n\n",
290 $data{$what}->{line_no};
294 $w =~ s/([\(\)\_\-\*\=\^\~\\])/\\$1/g;
296 if ($type ne "File") {
297 printf ".. _%s:\n\n", $data{$what}->{label};
299 my @names = split /, /,$w;
302 foreach my $name (@names) {
304 $len = length($name) if (length($name) > $len);
309 print "+-" . "-" x $len . "-+\n";
310 foreach my $name (@names) {
311 printf "| %s", $name . " " x ($len - length($name)) . " |\n";
312 print "+-" . "-" x $len . "-+\n";
318 for (my $i = 0; $i < scalar(@filepath); $i++) {
319 my $path = $filepath[$i];
322 $path =~ s,.*/(.*/.*),$1,;;
323 $path =~ s,[/\-],_,g;;
324 my $fileref = "abi_file_".$path;
326 if ($type eq "File") {
327 print ".. _$fileref:\n\n";
329 print "Defined on file :ref:`$f <$fileref>`\n\n";
333 if ($type eq "File") {
336 print "$w\n$bar\n\n";
340 $desc = $data{$what}->{description} if (defined($data{$what}->{description}));
343 if (!($desc =~ /^\s*$/)) {
344 if ($description_is_rst) {
349 # Remove title markups from the description, as they won't work
350 $desc =~ s/\n[\-\*\=\^\~]+\n/\n\n/g;
352 if ($desc =~ m/\:\n/ || $desc =~ m/\n[\t ]+/ || $desc =~ m/[\x00-\x08\x0b-\x1f\x7b-\xff]/) {
353 # put everything inside a code block
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;
365 print "DESCRIPTION MISSING for $what\n\n" if (!$data{$what}->{is_file});
368 if ($data{$what}->{symbols}) {
369 printf "Has the following ABI:\n\n";
371 foreach my $content(@{$data{$what}->{symbols}}) {
372 my $label = $data{$symbols{$content}->{xref}}->{label};
374 # Escape special chars from content
375 $content =~s/([\x00-\x1f\x21-\x2f\x3a-\x40\x7b-\xff])/\\$1/g;
377 print "- :ref:`$content <$label>`\n\n";
381 if (defined($data{$what}->{users})) {
382 my $users = $data{$what}->{users};
384 $users =~ s/\n/\n\t/g;
385 printf "Users:\n\t%s\n\n", $users if ($users ne "");
392 # Searches for ABI symbols
395 foreach my $what (sort keys %data) {
396 next if (!($what =~ m/($arg)/));
398 my $type = $data{$what}->{type};
399 next if ($type eq "File");
401 my $file = $data{$what}->{filepath};
406 print "\n$what\n$bar\n\n";
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}));
414 $kernelversion =~ s/^\s+// if ($kernelversion);
415 $contact =~ s/^\s+// if ($contact);
420 $date =~ s/^\s+// if ($date);
421 $desc =~ s/^\s+// if ($desc);
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";
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
438 # Parses all ABI files located at $prefix dir
440 find({wanted =>\&parse_abi, no_chdir => 1}, $prefix);
442 print STDERR Data::Dumper->Dump([\%data], [qw(*data)]) if ($debug);
445 # Handles the command
447 if ($cmd eq "search") {
450 if ($cmd eq "rest") {
454 # Warn about duplicated ABI entries
455 foreach my $what(sort keys %symbols) {
456 my @files = @{$symbols{$what}->{file}};
458 next if (scalar(@files) == 1);
460 printf STDERR "Warning: $what is defined %d times: @files\n",
469 abi_book.pl - parse the Linux ABI files and produce a ReST book.
473 B<abi_book.pl> [--debug] [--enable-lineno] [--man] [--help]
474 [--(no-)rst-source] [--dir=<dir>] <COMAND> [<ARGUMENT>]
476 Where <COMMAND> can be:
480 B<search> [SEARCH_REGEX] - search for [SEARCH_REGEX] inside ABI
482 B<rest> - output the ABI in ReST markup language
484 B<validate> - validate the ABI contents
494 Changes the location of the ABI search. By default, it uses
495 the Documentation/ABI directory.
497 =item B<--rst-source> and B<--no-rst-source>
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).
504 =item B<--enable-lineno>
506 Enable output of #define LINENO lines.
510 Put the script in verbose mode, useful for debugging. Can be called multiple
511 times, to increase verbosity.
515 Prints a brief help message and exits.
519 Prints the manual page and exits.
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.
531 Search for all stable symbols with the word "usb":
535 $ scripts/get_abi.pl search usb --dir Documentation/ABI/stable
539 Search for all symbols that match the regex expression "usb.*cap":
543 $ scripts/get_abi.pl search usb.*cap
547 Output all obsoleted symbols in ReST format
551 $ scripts/get_abi.pl rest --dir Documentation/ABI/obsolete
557 Report bugs to Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
561 Copyright (c) 2016-2019 by Mauro Carvalho Chehab <mchehab+samsung@kernel.org>.
563 License GPLv2: GNU GPL version 2 <http://gnu.org/licenses/gpl.html>.
565 This is free software: you are free to change and redistribute it.
566 There is NO WARRANTY, to the extent permitted by law.