blob: 92d9aa6cc4f5d2bd98c845adffb1e3b37d5e3749 [file] [log] [blame]
Finn Behrensc25ce582020-11-23 15:15:33 +01001#!/usr/bin/env perl
Mauro Carvalho Chehabecb351f2019-06-20 14:23:10 -03002# SPDX-License-Identifier: GPL-2.0
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -03003
4use strict;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +01005use warnings;
Mauro Carvalho Chehab55e54142020-10-30 08:40:29 +01006use utf8;
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -03007use Pod::Usage;
8use Getopt::Long;
9use File::Find;
10use Fcntl ':mode';
11
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +010012my $help = 0;
13my $man = 0;
14my $debug = 0;
15my $enable_lineno = 0;
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030016my $prefix="Documentation/ABI";
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -030017
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +010018#
19# If true, assumes that the description is formatted with ReST
20#
Mauro Carvalho Chehab2fcce372020-10-30 08:40:58 +010021my $description_is_rst = 1;
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +010022
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -030023GetOptions(
24 "debug|d+" => \$debug,
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +010025 "enable-lineno" => \$enable_lineno,
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +010026 "rst-source!" => \$description_is_rst,
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030027 "dir=s" => \$prefix,
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -030028 'help|?' => \$help,
29 man => \$man
30) or pod2usage(2);
31
32pod2usage(1) if $help;
33pod2usage(-exitstatus => 0, -verbose => 2) if $man;
34
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030035pod2usage(2) if (scalar @ARGV < 1 || @ARGV > 2);
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -030036
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030037my ($cmd, $arg) = @ARGV;
38
Mauro Carvalho Chehab7ce7b892019-06-20 14:23:04 -030039pod2usage(2) if ($cmd ne "search" && $cmd ne "rest" && $cmd ne "validate");
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030040pod2usage(2) if ($cmd eq "search" && !$arg);
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -030041
42require Data::Dumper if ($debug);
43
44my %data;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +010045my %symbols;
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -030046
47#
48# Displays an error message, printing file name and line
49#
50sub parse_error($$$$) {
51 my ($file, $ln, $msg, $data) = @_;
52
Mauro Carvalho Chehab75442fb2020-10-30 08:40:45 +010053 $data =~ s/\s+$/\n/;
54
55 print STDERR "Warning: file $file#$ln:\n\t$msg";
56
57 if ($data ne "") {
58 print STDERR ". Line\n\t\t$data";
59 } else {
60 print STDERR "\n";
61 }
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -030062}
63
64#
65# Parse an ABI file, storing its contents at %data
66#
67sub parse_abi {
68 my $file = $File::Find::name;
69
70 my $mode = (stat($file))[2];
71 return if ($mode & S_IFDIR);
72 return if ($file =~ m,/README,);
73
74 my $name = $file;
75 $name =~ s,.*/,,;
76
Mauro Carvalho Chehaba4ea67b2020-10-30 08:40:27 +010077 my $fn = $file;
78 $fn =~ s,Documentation/ABI/,,;
79
80 my $nametag = "File $fn";
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -030081 $data{$nametag}->{what} = "File $name";
82 $data{$nametag}->{type} = "File";
83 $data{$nametag}->{file} = $name;
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030084 $data{$nametag}->{filepath} = $file;
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -030085 $data{$nametag}->{is_file} = 1;
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +010086 $data{$nametag}->{line_no} = 1;
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -030087
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -030088 my $type = $file;
89 $type =~ s,.*/(.*)/.*,$1,;
90
91 my $what;
92 my $new_what;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +010093 my $tag = "";
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -030094 my $ln;
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -030095 my $xrefs;
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -030096 my $space;
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -030097 my @labels;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +010098 my $label = "";
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -030099
100 print STDERR "Opening $file\n" if ($debug > 1);
101 open IN, $file;
102 while(<IN>) {
103 $ln++;
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300104 if (m/^(\S+)(:\s*)(.*)/i) {
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300105 my $new_tag = lc($1);
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300106 my $sep = $2;
107 my $content = $3;
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300108
Mauro Carvalho Chehab7ce7b892019-06-20 14:23:04 -0300109 if (!($new_tag =~ m/(what|where|date|kernelversion|contact|description|users)/)) {
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300110 if ($tag eq "description") {
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300111 # New "tag" is actually part of
112 # description. Don't consider it a tag
113 $new_tag = "";
Mauro Carvalho Chehab7d7ea8d2019-06-20 14:23:01 -0300114 } elsif ($tag ne "") {
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300115 parse_error($file, $ln, "tag '$tag' is invalid", $_);
116 }
117 }
118
Mauro Carvalho Chehab2c0700e2019-06-20 14:23:03 -0300119 # Invalid, but it is a common mistake
120 if ($new_tag eq "where") {
Mauro Carvalho Chehab75442fb2020-10-30 08:40:45 +0100121 parse_error($file, $ln, "tag 'Where' is invalid. Should be 'What:' instead", "");
Mauro Carvalho Chehab2c0700e2019-06-20 14:23:03 -0300122 $new_tag = "what";
123 }
124
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300125 if ($new_tag =~ m/what/) {
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300126 $space = "";
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100127 $content =~ s/[,.;]$//;
128
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100129 push @{$symbols{$content}->{file}}, " $file:" . ($ln - 1);
130
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300131 if ($tag =~ m/what/) {
132 $what .= ", " . $content;
133 } else {
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100134 if ($what) {
135 parse_error($file, $ln, "What '$what' doesn't have a description", "") if (!$data{$what}->{description});
136
137 foreach my $w(split /, /, $what) {
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100138 $symbols{$w}->{xref} = $what;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100139 };
140 }
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300141
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300142 $what = $content;
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300143 $label = $content;
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300144 $new_what = 1;
145 }
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300146 push @labels, [($content, $label)];
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300147 $tag = $new_tag;
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -0300148
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100149 push @{$data{$nametag}->{symbols}}, $content if ($data{$nametag}->{what});
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300150 next;
151 }
152
Mauro Carvalho Chehab7d7ea8d2019-06-20 14:23:01 -0300153 if ($tag ne "" && $new_tag) {
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300154 $tag = $new_tag;
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300155
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300156 if ($new_what) {
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100157 @{$data{$what}->{label_list}} = @labels if ($data{$nametag}->{what});
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300158 @labels = ();
159 $label = "";
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300160 $new_what = 0;
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300161
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300162 $data{$what}->{type} = $type;
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100163 if (!defined($data{$what}->{file})) {
164 $data{$what}->{file} = $name;
165 $data{$what}->{filepath} = $file;
166 } else {
167 if ($name ne $data{$what}->{file}) {
168 $data{$what}->{file} .= " " . $name;
169 $data{$what}->{filepath} .= " " . $file;
170 }
171 }
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300172 print STDERR "\twhat: $what\n" if ($debug > 1);
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100173 $data{$what}->{line_no} = $ln;
174 } else {
175 $data{$what}->{line_no} = $ln if (!defined($data{$what}->{line_no}));
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300176 }
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300177
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300178 if (!$what) {
179 parse_error($file, $ln, "'What:' should come first:", $_);
180 next;
181 }
Mauro Carvalho Chehabf82a8a72020-10-30 08:40:23 +0100182 if ($new_tag eq "description") {
183 $sep =~ s,:, ,;
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +0100184 $content = ' ' x length($new_tag) . $sep . $content;
Mauro Carvalho Chehabf82a8a72020-10-30 08:40:23 +0100185 while ($content =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {}
186 if ($content =~ m/^(\s*)(\S.*)$/) {
187 # Preserve initial spaces for the first line
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +0100188 $space = $1;
Mauro Carvalho Chehabf82a8a72020-10-30 08:40:23 +0100189 $content = "$2\n";
190 $data{$what}->{$tag} .= $content;
191 } else {
192 undef($space);
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300193 }
Mauro Carvalho Chehabe9bca892020-10-30 08:40:21 +0100194
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300195 } else {
196 $data{$what}->{$tag} = $content;
197 }
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300198 next;
199 }
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300200 }
201
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300202 # Store any contents before tags at the database
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300203 if (!$tag && $data{$nametag}->{what}) {
204 $data{$nametag}->{description} .= $_;
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -0300205 next;
206 }
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300207
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300208 if ($tag eq "description") {
Mauro Carvalho Chehabe9bca892020-10-30 08:40:21 +0100209 my $content = $_;
210 while ($content =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {}
Mauro Carvalho Chehabf82a8a72020-10-30 08:40:23 +0100211 if (m/^\s*\n/) {
212 $data{$what}->{$tag} .= "\n";
213 next;
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300214 }
Mauro Carvalho Chehabf82a8a72020-10-30 08:40:23 +0100215
216 if (!defined($space)) {
217 # Preserve initial spaces for the first line
218 if ($content =~ m/^(\s*)(\S.*)$/) {
219 $space = $1;
220 $content = "$2\n";
221 }
222 } else {
223 $space = "" if (!($content =~ s/^($space)//));
224 }
225 $data{$what}->{$tag} .= $content;
226
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300227 next;
228 }
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300229 if (m/^\s*(.*)/) {
230 $data{$what}->{$tag} .= "\n$1";
231 $data{$what}->{$tag} =~ s/\n+$//;
232 next;
233 }
234
235 # Everything else is error
Mauro Carvalho Chehab75442fb2020-10-30 08:40:45 +0100236 parse_error($file, $ln, "Unexpected content", $_);
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300237 }
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100238 $data{$nametag}->{description} =~ s/^\n+// if ($data{$nametag}->{description});
239 if ($what) {
240 parse_error($file, $ln, "What '$what' doesn't have a description", "") if (!$data{$what}->{description});
241
242 foreach my $w(split /, /,$what) {
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100243 $symbols{$w}->{xref} = $what;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100244 };
245 }
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300246 close IN;
247}
248
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100249sub create_labels {
250 my %labels;
251
252 foreach my $what (keys %data) {
253 next if ($data{$what}->{file} eq "File");
254
255 foreach my $p (@{$data{$what}->{label_list}}) {
256 my ($content, $label) = @{$p};
257 $label = "abi_" . $label . " ";
258 $label =~ tr/A-Z/a-z/;
259
260 # Convert special chars to "_"
261 $label =~s/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\xff])/_/g;
262 $label =~ s,_+,_,g;
263 $label =~ s,_$,,;
264
265 # Avoid duplicated labels
266 while (defined($labels{$label})) {
267 my @chars = ("A".."Z", "a".."z");
268 $label .= $chars[rand @chars];
269 }
270 $labels{$label} = 1;
271
272 $data{$what}->{label} = $label;
273
274 # only one label is enough
275 last;
276 }
277 }
278}
279
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300280#
281# Outputs the book on ReST format
282#
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300283
Mauro Carvalho Chehab55e54142020-10-30 08:40:29 +0100284# \b doesn't work well with paths. So, we need to define something else
285my $bondary = qr { (?<![\w\/\`\{])(?=[\w\/\`\{])|(?<=[\w\/\`\{])(?![\w\/\`\{]) }x;
286
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300287sub output_rest {
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100288 create_labels();
289
Mauro Carvalho Chehab9d4fdda2020-11-02 11:32:16 +0100290 my $part = "";
291
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300292 foreach my $what (sort {
293 ($data{$a}->{type} eq "File") cmp ($data{$b}->{type} eq "File") ||
294 $a cmp $b
295 } keys %data) {
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300296 my $type = $data{$what}->{type};
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100297
298 my @file = split / /, $data{$what}->{file};
299 my @filepath = split / /, $data{$what}->{filepath};
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300300
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +0100301 if ($enable_lineno) {
302 printf "#define LINENO %s%s#%s\n\n",
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100303 $prefix, $file[0],
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +0100304 $data{$what}->{line_no};
305 }
306
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300307 my $w = $what;
308 $w =~ s/([\(\)\_\-\*\=\^\~\\])/\\$1/g;
309
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100310 if ($type ne "File") {
Mauro Carvalho Chehab9d4fdda2020-11-02 11:32:16 +0100311 my $cur_part = $what;
312 if ($what =~ '/') {
313 if ($what =~ m#^(\/?(?:[\w\-]+\/?){1,2})#) {
314 $cur_part = "Symbols under $1";
315 $cur_part =~ s,/$,,;
316 }
317 }
318
319 if ($cur_part ne "" && $part ne $cur_part) {
320 $part = $cur_part;
321 my $bar = $part;
322 $bar =~ s/./-/g;
323 print "$part\n$bar\n\n";
324 }
325
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100326 printf ".. _%s:\n\n", $data{$what}->{label};
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300327
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100328 my @names = split /, /,$w;
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300329 my $len = 0;
330
331 foreach my $name (@names) {
Mauro Carvalho Chehabc01d62d2020-10-30 08:40:28 +0100332 $name = "**$name**";
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300333 $len = length($name) if (length($name) > $len);
334 }
335
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300336 print "+-" . "-" x $len . "-+\n";
337 foreach my $name (@names) {
338 printf "| %s", $name . " " x ($len - length($name)) . " |\n";
339 print "+-" . "-" x $len . "-+\n";
340 }
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100341
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100342 print "\n";
343 }
344
345 for (my $i = 0; $i < scalar(@filepath); $i++) {
346 my $path = $filepath[$i];
347 my $f = $file[$i];
348
349 $path =~ s,.*/(.*/.*),$1,;;
350 $path =~ s,[/\-],_,g;;
351 my $fileref = "abi_file_".$path;
352
353 if ($type eq "File") {
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100354 print ".. _$fileref:\n\n";
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100355 } else {
356 print "Defined on file :ref:`$f <$fileref>`\n\n";
357 }
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300358 }
359
Mauro Carvalho Chehaba4ea67b2020-10-30 08:40:27 +0100360 if ($type eq "File") {
361 my $bar = $w;
362 $bar =~ s/./-/g;
363 print "$w\n$bar\n\n";
364 }
365
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100366 my $desc = "";
367 $desc = $data{$what}->{description} if (defined($data{$what}->{description}));
368 $desc =~ s/\s+$/\n/;
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300369
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300370 if (!($desc =~ /^\s*$/)) {
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +0100371 if ($description_is_rst) {
Mauro Carvalho Chehabdaaaf582020-11-02 11:32:15 +0100372 # Remove title markups from the description
373 # Having titles inside ABI files will only work if extra
374 # care would be taken in order to strictly follow the same
375 # level order for each markup.
376 $desc =~ s/\n[\-\*\=\^\~]+\n/\n\n/g;
377
Mauro Carvalho Chehab55e54142020-10-30 08:40:29 +0100378 # Enrich text by creating cross-references
379
380 $desc =~ s,Documentation/(?!devicetree)(\S+)\.rst,:doc:`/$1`,g;
381
382 my @matches = $desc =~ m,Documentation/ABI/([\w\/\-]+),;
383 foreach my $f (@matches) {
384 my $xref = $f;
385 my $path = $f;
386 $path =~ s,.*/(.*/.*),$1,;;
387 $path =~ s,[/\-],_,g;;
388 $xref .= " <abi_file_" . $path . ">";
389 $desc =~ s,\bDocumentation/ABI/$f\b,:ref:`$xref`,g;
390 }
391
392 @matches = $desc =~ m,$bondary(/sys/[^\s\.\,\;\:\*\s\`\'\(\)]+)$bondary,;
393
394 foreach my $s (@matches) {
395 if (defined($data{$s}) && defined($data{$s}->{label})) {
396 my $xref = $s;
397
398 $xref =~ s/([\x00-\x1f\x21-\x2f\x3a-\x40\x7b-\xff])/\\$1/g;
399 $xref = ":ref:`$xref <" . $data{$s}->{label} . ">`";
400
401 $desc =~ s,$bondary$s$bondary,$xref,g;
402 }
403 }
404
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300405 print "$desc\n\n";
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +0100406 } else {
407 $desc =~ s/^\s+//;
408
409 # Remove title markups from the description, as they won't work
410 $desc =~ s/\n[\-\*\=\^\~]+\n/\n\n/g;
411
412 if ($desc =~ m/\:\n/ || $desc =~ m/\n[\t ]+/ || $desc =~ m/[\x00-\x08\x0b-\x1f\x7b-\xff]/) {
413 # put everything inside a code block
414 $desc =~ s/\n/\n /g;
415
416 print "::\n\n";
417 print " $desc\n\n";
418 } else {
419 # Escape any special chars from description
420 $desc =~s/([\x00-\x08\x0b-\x1f\x21-\x2a\x2d\x2f\x3c-\x40\x5c\x5e-\x60\x7b-\xff])/\\$1/g;
421 print "$desc\n\n";
422 }
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300423 }
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300424 } else {
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300425 print "DESCRIPTION MISSING for $what\n\n" if (!$data{$what}->{is_file});
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300426 }
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -0300427
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100428 if ($data{$what}->{symbols}) {
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300429 printf "Has the following ABI:\n\n";
430
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100431 foreach my $content(@{$data{$what}->{symbols}}) {
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100432 my $label = $data{$symbols{$content}->{xref}}->{label};
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300433
434 # Escape special chars from content
435 $content =~s/([\x00-\x1f\x21-\x2f\x3a-\x40\x7b-\xff])/\\$1/g;
436
437 print "- :ref:`$content <$label>`\n\n";
438 }
439 }
Mauro Carvalho Chehaba16ab142020-10-30 08:40:26 +0100440
441 if (defined($data{$what}->{users})) {
442 my $users = $data{$what}->{users};
443
444 $users =~ s/\n/\n\t/g;
445 printf "Users:\n\t%s\n\n", $users if ($users ne "");
446 }
447
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300448 }
449}
450
451#
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300452# Searches for ABI symbols
453#
454sub search_symbols {
455 foreach my $what (sort keys %data) {
456 next if (!($what =~ m/($arg)/));
457
458 my $type = $data{$what}->{type};
459 next if ($type eq "File");
460
461 my $file = $data{$what}->{filepath};
462
463 my $bar = $what;
464 $bar =~ s/./-/g;
465
466 print "\n$what\n$bar\n\n";
467
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100468 my $kernelversion = $data{$what}->{kernelversion} if (defined($data{$what}->{kernelversion}));
469 my $contact = $data{$what}->{contact} if (defined($data{$what}->{contact}));
470 my $users = $data{$what}->{users} if (defined($data{$what}->{users}));
471 my $date = $data{$what}->{date} if (defined($data{$what}->{date}));
472 my $desc = $data{$what}->{description} if (defined($data{$what}->{description}));
473
474 $kernelversion =~ s/^\s+// if ($kernelversion);
475 $contact =~ s/^\s+// if ($contact);
476 if ($users) {
477 $users =~ s/^\s+//;
478 $users =~ s/\n//g;
479 }
480 $date =~ s/^\s+// if ($date);
481 $desc =~ s/^\s+// if ($desc);
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300482
483 printf "Kernel version:\t\t%s\n", $kernelversion if ($kernelversion);
484 printf "Date:\t\t\t%s\n", $date if ($date);
485 printf "Contact:\t\t%s\n", $contact if ($contact);
486 printf "Users:\t\t\t%s\n", $users if ($users);
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100487 print "Defined on file(s):\t$file\n\n";
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300488 print "Description:\n\n$desc";
489 }
490}
491
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +0100492# Ensure that the prefix will always end with a slash
493# While this is not needed for find, it makes the patch nicer
494# with --enable-lineno
495$prefix =~ s,/?$,/,;
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300496
497#
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300498# Parses all ABI files located at $prefix dir
499#
500find({wanted =>\&parse_abi, no_chdir => 1}, $prefix);
501
502print STDERR Data::Dumper->Dump([\%data], [qw(*data)]) if ($debug);
503
504#
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300505# Handles the command
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300506#
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100507if ($cmd eq "search") {
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300508 search_symbols;
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100509} else {
510 if ($cmd eq "rest") {
511 output_rest;
512 }
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300513
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100514 # Warn about duplicated ABI entries
515 foreach my $what(sort keys %symbols) {
516 my @files = @{$symbols{$what}->{file}};
517
518 next if (scalar(@files) == 1);
519
520 printf STDERR "Warning: $what is defined %d times: @files\n",
521 scalar(@files);
522 }
523}
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300524
525__END__
526
527=head1 NAME
528
529abi_book.pl - parse the Linux ABI files and produce a ReST book.
530
531=head1 SYNOPSIS
532
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +0100533B<abi_book.pl> [--debug] [--enable-lineno] [--man] [--help]
534 [--(no-)rst-source] [--dir=<dir>] <COMAND> [<ARGUMENT>]
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300535
536Where <COMMAND> can be:
537
538=over 8
539
540B<search> [SEARCH_REGEX] - search for [SEARCH_REGEX] inside ABI
541
Mauro Carvalho Chehab7ce7b892019-06-20 14:23:04 -0300542B<rest> - output the ABI in ReST markup language
543
544B<validate> - validate the ABI contents
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300545
546=back
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300547
548=head1 OPTIONS
549
550=over 8
551
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300552=item B<--dir>
553
554Changes the location of the ABI search. By default, it uses
555the Documentation/ABI directory.
556
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +0100557=item B<--rst-source> and B<--no-rst-source>
558
559The input file may be using ReST syntax or not. Those two options allow
560selecting between a rst-compliant source ABI (--rst-source), or a
561plain text that may be violating ReST spec, so it requres some escaping
562logic (--no-rst-source).
563
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +0100564=item B<--enable-lineno>
565
566Enable output of #define LINENO lines.
567
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300568=item B<--debug>
569
570Put the script in verbose mode, useful for debugging. Can be called multiple
571times, to increase verbosity.
572
573=item B<--help>
574
575Prints a brief help message and exits.
576
577=item B<--man>
578
579Prints the manual page and exits.
580
581=back
582
583=head1 DESCRIPTION
584
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300585Parse the Linux ABI files from ABI DIR (usually located at Documentation/ABI),
586allowing to search for ABI symbols or to produce a ReST book containing
587the Linux ABI documentation.
588
589=head1 EXAMPLES
590
591Search for all stable symbols with the word "usb":
592
593=over 8
594
595$ scripts/get_abi.pl search usb --dir Documentation/ABI/stable
596
597=back
598
599Search for all symbols that match the regex expression "usb.*cap":
600
601=over 8
602
603$ scripts/get_abi.pl search usb.*cap
604
605=back
606
607Output all obsoleted symbols in ReST format
608
609=over 8
610
611$ scripts/get_abi.pl rest --dir Documentation/ABI/obsolete
612
613=back
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300614
615=head1 BUGS
616
Mauro Carvalho Chehab7ce7b892019-06-20 14:23:04 -0300617Report bugs to Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300618
619=head1 COPYRIGHT
620
Mauro Carvalho Chehab7ce7b892019-06-20 14:23:04 -0300621Copyright (c) 2016-2019 by Mauro Carvalho Chehab <mchehab+samsung@kernel.org>.
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300622
623License GPLv2: GNU GPL version 2 <http://gnu.org/licenses/gpl.html>.
624
625This is free software: you are free to change and redistribute it.
626There is NO WARRANTY, to the extent permitted by law.
627
628=cut