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,);
68 my $nametag = "File $name";
69 $data{$nametag}->{what} = "File $name";
70 $data{$nametag}->{type} = "File";
71 $data{$nametag}->{file} = $name;
72 $data{$nametag}->{filepath} = $file;
73 $data{$nametag}->{is_file} = 1;
74 $data{$nametag}->{line_no} = 1;
77 $type =~ s,.*/(.*)/.*,$1,;
88 print STDERR "Opening $file\n" if ($debug > 1);
92 if (m/^(\S+)(:\s*)(.*)/i) {
97 if (!($new_tag =~ m/(what|where|date|kernelversion|contact|description|users)/)) {
98 if ($tag eq "description") {
99 # New "tag" is actually part of
100 # description. Don't consider it a tag
102 } elsif ($tag ne "") {
103 parse_error($file, $ln, "tag '$tag' is invalid", $_);
107 # Invalid, but it is a common mistake
108 if ($new_tag eq "where") {
109 parse_error($file, $ln, "tag 'Where' is invalid. Should be 'What:' instead", $_);
113 if ($new_tag =~ m/what/) {
115 $content =~ s/[,.;]$//;
117 if ($tag =~ m/what/) {
118 $what .= ", " . $content;
121 parse_error($file, $ln, "What '$what' doesn't have a description", "") if (!$data{$what}->{description});
123 foreach my $w(split /, /, $what) {
124 $symbols{$w} = $what;
132 push @labels, [($content, $label)];
135 push @{$data{$nametag}->{symbols}}, $content if ($data{$nametag}->{what});
139 if ($tag ne "" && $new_tag) {
142 $data{$what}->{line_no} = $ln;
145 @{$data{$what}->{label_list}} = @labels if ($data{$nametag}->{what});
150 $data{$what}->{type} = $type;
151 $data{$what}->{file} = $name;
152 $data{$what}->{filepath} = $file;
153 print STDERR "\twhat: $what\n" if ($debug > 1);
157 parse_error($file, $ln, "'What:' should come first:", $_);
160 if ($new_tag eq "description") {
162 $content = ' ' x length($new_tag) . $sep . $content;
163 while ($content =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {}
164 if ($content =~ m/^(\s*)(\S.*)$/) {
165 # Preserve initial spaces for the first line
168 $data{$what}->{$tag} .= $content;
174 $data{$what}->{$tag} = $content;
180 # Store any contents before tags at the database
181 if (!$tag && $data{$nametag}->{what}) {
182 $data{$nametag}->{description} .= $_;
186 if ($tag eq "description") {
188 while ($content =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {}
190 $data{$what}->{$tag} .= "\n";
194 if (!defined($space)) {
195 # Preserve initial spaces for the first line
196 if ($content =~ m/^(\s*)(\S.*)$/) {
201 $space = "" if (!($content =~ s/^($space)//));
203 $data{$what}->{$tag} .= $content;
208 $data{$what}->{$tag} .= "\n$1";
209 $data{$what}->{$tag} =~ s/\n+$//;
213 # Everything else is error
214 parse_error($file, $ln, "Unexpected line:", $_);
216 $data{$nametag}->{description} =~ s/^\n+// if ($data{$nametag}->{description});
218 parse_error($file, $ln, "What '$what' doesn't have a description", "") if (!$data{$what}->{description});
220 foreach my $w(split /, /,$what) {
221 $symbols{$w} = $what;
230 foreach my $what (keys %data) {
231 next if ($data{$what}->{file} eq "File");
233 foreach my $p (@{$data{$what}->{label_list}}) {
234 my ($content, $label) = @{$p};
235 $label = "abi_" . $label . " ";
236 $label =~ tr/A-Z/a-z/;
238 # Convert special chars to "_"
239 $label =~s/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\xff])/_/g;
243 # Avoid duplicated labels
244 while (defined($labels{$label})) {
245 my @chars = ("A".."Z", "a".."z");
246 $label .= $chars[rand @chars];
250 $data{$what}->{label} = $label;
252 # only one label is enough
259 # Outputs the book on ReST format
265 foreach my $what (sort {
266 ($data{$a}->{type} eq "File") cmp ($data{$b}->{type} eq "File") ||
269 my $type = $data{$what}->{type};
270 my $file = $data{$what}->{file};
271 my $filepath = $data{$what}->{filepath};
273 if ($enable_lineno) {
274 printf "#define LINENO %s%s#%s\n\n",
275 $prefix, $data{$what}->{file},
276 $data{$what}->{line_no};
280 $w =~ s/([\(\)\_\-\*\=\^\~\\])/\\$1/g;
282 $filepath =~ s,.*/(.*/.*),$1,;;
283 $filepath =~ s,[/\-],_,g;;
284 my $fileref = "abi_file_".$filepath;
286 if ($type eq "File") {
290 print ".. _$fileref:\n\n";
291 print "$w\n$bar\n\n";
293 printf ".. _%s:\n\n", $data{$what}->{label};
295 my @names = split /, /,$w;
298 foreach my $name (@names) {
299 $len = length($name) if (length($name) > $len);
304 print "+-" . "-" x $len . "-+\n";
305 foreach my $name (@names) {
306 printf "| %s", $name . " " x ($len - length($name)) . " |\n";
307 print "+-" . "-" x $len . "-+\n";
310 print "\nDefined on file :ref:`$file <$fileref>`\n\n";
314 $desc = $data{$what}->{description} if (defined($data{$what}->{description}));
317 if (!($desc =~ /^\s*$/)) {
318 if ($description_is_rst) {
323 # Remove title markups from the description, as they won't work
324 $desc =~ s/\n[\-\*\=\^\~]+\n/\n\n/g;
326 if ($desc =~ m/\:\n/ || $desc =~ m/\n[\t ]+/ || $desc =~ m/[\x00-\x08\x0b-\x1f\x7b-\xff]/) {
327 # put everything inside a code block
333 # Escape any special chars from description
334 $desc =~s/([\x00-\x08\x0b-\x1f\x21-\x2a\x2d\x2f\x3c-\x40\x5c\x5e-\x60\x7b-\xff])/\\$1/g;
339 print "DESCRIPTION MISSING for $what\n\n" if (!$data{$what}->{is_file});
342 if ($data{$what}->{symbols}) {
343 printf "Has the following ABI:\n\n";
345 foreach my $content(@{$data{$what}->{symbols}}) {
346 my $label = $data{$symbols{$content}}->{label};
348 # Escape special chars from content
349 $content =~s/([\x00-\x1f\x21-\x2f\x3a-\x40\x7b-\xff])/\\$1/g;
351 print "- :ref:`$content <$label>`\n\n";
358 # Searches for ABI symbols
361 foreach my $what (sort keys %data) {
362 next if (!($what =~ m/($arg)/));
364 my $type = $data{$what}->{type};
365 next if ($type eq "File");
367 my $file = $data{$what}->{filepath};
372 print "\n$what\n$bar\n\n";
374 my $kernelversion = $data{$what}->{kernelversion} if (defined($data{$what}->{kernelversion}));
375 my $contact = $data{$what}->{contact} if (defined($data{$what}->{contact}));
376 my $users = $data{$what}->{users} if (defined($data{$what}->{users}));
377 my $date = $data{$what}->{date} if (defined($data{$what}->{date}));
378 my $desc = $data{$what}->{description} if (defined($data{$what}->{description}));
380 $kernelversion =~ s/^\s+// if ($kernelversion);
381 $contact =~ s/^\s+// if ($contact);
386 $date =~ s/^\s+// if ($date);
387 $desc =~ s/^\s+// if ($desc);
389 printf "Kernel version:\t\t%s\n", $kernelversion if ($kernelversion);
390 printf "Date:\t\t\t%s\n", $date if ($date);
391 printf "Contact:\t\t%s\n", $contact if ($contact);
392 printf "Users:\t\t\t%s\n", $users if ($users);
393 print "Defined on file:\t$file\n\n";
394 print "Description:\n\n$desc";
398 # Ensure that the prefix will always end with a slash
399 # While this is not needed for find, it makes the patch nicer
400 # with --enable-lineno
404 # Parses all ABI files located at $prefix dir
406 find({wanted =>\&parse_abi, no_chdir => 1}, $prefix);
408 print STDERR Data::Dumper->Dump([\%data], [qw(*data)]) if ($debug);
411 # Handles the command
413 if ($cmd eq "rest") {
415 } elsif ($cmd eq "search") {
424 abi_book.pl - parse the Linux ABI files and produce a ReST book.
428 B<abi_book.pl> [--debug] [--enable-lineno] [--man] [--help]
429 [--(no-)rst-source] [--dir=<dir>] <COMAND> [<ARGUMENT>]
431 Where <COMMAND> can be:
435 B<search> [SEARCH_REGEX] - search for [SEARCH_REGEX] inside ABI
437 B<rest> - output the ABI in ReST markup language
439 B<validate> - validate the ABI contents
449 Changes the location of the ABI search. By default, it uses
450 the Documentation/ABI directory.
452 =item B<--rst-source> and B<--no-rst-source>
454 The input file may be using ReST syntax or not. Those two options allow
455 selecting between a rst-compliant source ABI (--rst-source), or a
456 plain text that may be violating ReST spec, so it requres some escaping
457 logic (--no-rst-source).
459 =item B<--enable-lineno>
461 Enable output of #define LINENO lines.
465 Put the script in verbose mode, useful for debugging. Can be called multiple
466 times, to increase verbosity.
470 Prints a brief help message and exits.
474 Prints the manual page and exits.
480 Parse the Linux ABI files from ABI DIR (usually located at Documentation/ABI),
481 allowing to search for ABI symbols or to produce a ReST book containing
482 the Linux ABI documentation.
486 Search for all stable symbols with the word "usb":
490 $ scripts/get_abi.pl search usb --dir Documentation/ABI/stable
494 Search for all symbols that match the regex expression "usb.*cap":
498 $ scripts/get_abi.pl search usb.*cap
502 Output all obsoleted symbols in ReST format
506 $ scripts/get_abi.pl rest --dir Documentation/ABI/obsolete
512 Report bugs to Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
516 Copyright (c) 2016-2019 by Mauro Carvalho Chehab <mchehab+samsung@kernel.org>.
518 License GPLv2: GNU GPL version 2 <http://gnu.org/licenses/gpl.html>.
520 This is free software: you are free to change and redistribute it.
521 There is NO WARRANTY, to the extent permitted by law.