blob: 12a23f9a2f86975cad6b010e7be14dd79749cd19 [file] [log] [blame]
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -03001#!/usr/bin/perl
Mauro Carvalho Chehabecb351f2019-06-20 14:23:10 -03002# SPDX-License-Identifier: GPL-2.0
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -03003
4use strict;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +01005use warnings;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -03006use Pod::Usage;
7use Getopt::Long;
8use File::Find;
9use Fcntl ':mode';
10
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +010011my $help = 0;
12my $man = 0;
13my $debug = 0;
14my $enable_lineno = 0;
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030015my $prefix="Documentation/ABI";
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030016
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +010017#
18# If true, assumes that the description is formatted with ReST
19#
20my $description_is_rst = 0;
21
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030022GetOptions(
23 "debug|d+" => \$debug,
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +010024 "enable-lineno" => \$enable_lineno,
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +010025 "rst-source!" => \$description_is_rst,
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030026 "dir=s" => \$prefix,
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030027 'help|?' => \$help,
28 man => \$man
29) or pod2usage(2);
30
31pod2usage(1) if $help;
32pod2usage(-exitstatus => 0, -verbose => 2) if $man;
33
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030034pod2usage(2) if (scalar @ARGV < 1 || @ARGV > 2);
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030035
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030036my ($cmd, $arg) = @ARGV;
37
Mauro Carvalho Chehab7ce7b892019-06-20 14:23:04 -030038pod2usage(2) if ($cmd ne "search" && $cmd ne "rest" && $cmd ne "validate");
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030039pod2usage(2) if ($cmd eq "search" && !$arg);
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030040
41require Data::Dumper if ($debug);
42
43my %data;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +010044my %symbols;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030045
46#
47# Displays an error message, printing file name and line
48#
49sub parse_error($$$$) {
50 my ($file, $ln, $msg, $data) = @_;
51
52 print STDERR "file $file#$ln: $msg at\n\t$data";
53}
54
55#
56# Parse an ABI file, storing its contents at %data
57#
58sub parse_abi {
59 my $file = $File::Find::name;
60
61 my $mode = (stat($file))[2];
62 return if ($mode & S_IFDIR);
63 return if ($file =~ m,/README,);
64
65 my $name = $file;
66 $name =~ s,.*/,,;
67
Mauro Carvalho Chehaba4ea67b2020-10-30 08:40:27 +010068 my $fn = $file;
69 $fn =~ s,Documentation/ABI/,,;
70
71 my $nametag = "File $fn";
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -030072 $data{$nametag}->{what} = "File $name";
73 $data{$nametag}->{type} = "File";
74 $data{$nametag}->{file} = $name;
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030075 $data{$nametag}->{filepath} = $file;
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -030076 $data{$nametag}->{is_file} = 1;
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +010077 $data{$nametag}->{line_no} = 1;
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -030078
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030079 my $type = $file;
80 $type =~ s,.*/(.*)/.*,$1,;
81
82 my $what;
83 my $new_what;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +010084 my $tag = "";
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030085 my $ln;
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -030086 my $xrefs;
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -030087 my $space;
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -030088 my @labels;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +010089 my $label = "";
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030090
91 print STDERR "Opening $file\n" if ($debug > 1);
92 open IN, $file;
93 while(<IN>) {
94 $ln++;
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -030095 if (m/^(\S+)(:\s*)(.*)/i) {
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030096 my $new_tag = lc($1);
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -030097 my $sep = $2;
98 my $content = $3;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030099
Mauro Carvalho Chehab7ce7b892019-06-20 14:23:04 -0300100 if (!($new_tag =~ m/(what|where|date|kernelversion|contact|description|users)/)) {
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300101 if ($tag eq "description") {
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300102 # New "tag" is actually part of
103 # description. Don't consider it a tag
104 $new_tag = "";
Mauro Carvalho Chehab7d7ea8d2019-06-20 14:23:01 -0300105 } elsif ($tag ne "") {
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300106 parse_error($file, $ln, "tag '$tag' is invalid", $_);
107 }
108 }
109
Mauro Carvalho Chehab2c0700e2019-06-20 14:23:03 -0300110 # 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", $_);
113 $new_tag = "what";
114 }
115
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300116 if ($new_tag =~ m/what/) {
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300117 $space = "";
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100118 $content =~ s/[,.;]$//;
119
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100120 push @{$symbols{$content}->{file}}, " $file:" . ($ln - 1);
121
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300122 if ($tag =~ m/what/) {
123 $what .= ", " . $content;
124 } else {
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100125 if ($what) {
126 parse_error($file, $ln, "What '$what' doesn't have a description", "") if (!$data{$what}->{description});
127
128 foreach my $w(split /, /, $what) {
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100129 $symbols{$w}->{xref} = $what;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100130 };
131 }
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300132
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300133 $what = $content;
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300134 $label = $content;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300135 $new_what = 1;
136 }
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300137 push @labels, [($content, $label)];
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300138 $tag = $new_tag;
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -0300139
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100140 push @{$data{$nametag}->{symbols}}, $content if ($data{$nametag}->{what});
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300141 next;
142 }
143
Mauro Carvalho Chehab7d7ea8d2019-06-20 14:23:01 -0300144 if ($tag ne "" && $new_tag) {
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300145 $tag = $new_tag;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300146
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300147 if ($new_what) {
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100148 @{$data{$what}->{label_list}} = @labels if ($data{$nametag}->{what});
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300149 @labels = ();
150 $label = "";
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300151 $new_what = 0;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300152
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300153 $data{$what}->{type} = $type;
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100154 if (!defined($data{$what}->{file})) {
155 $data{$what}->{file} = $name;
156 $data{$what}->{filepath} = $file;
157 } else {
158 if ($name ne $data{$what}->{file}) {
159 $data{$what}->{file} .= " " . $name;
160 $data{$what}->{filepath} .= " " . $file;
161 }
162 }
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300163 print STDERR "\twhat: $what\n" if ($debug > 1);
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100164 $data{$what}->{line_no} = $ln;
165 } else {
166 $data{$what}->{line_no} = $ln if (!defined($data{$what}->{line_no}));
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300167 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300168
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300169 if (!$what) {
170 parse_error($file, $ln, "'What:' should come first:", $_);
171 next;
172 }
Mauro Carvalho Chehabf82a8a72020-10-30 08:40:23 +0100173 if ($new_tag eq "description") {
174 $sep =~ s,:, ,;
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +0100175 $content = ' ' x length($new_tag) . $sep . $content;
Mauro Carvalho Chehabf82a8a72020-10-30 08:40:23 +0100176 while ($content =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {}
177 if ($content =~ m/^(\s*)(\S.*)$/) {
178 # Preserve initial spaces for the first line
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +0100179 $space = $1;
Mauro Carvalho Chehabf82a8a72020-10-30 08:40:23 +0100180 $content = "$2\n";
181 $data{$what}->{$tag} .= $content;
182 } else {
183 undef($space);
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300184 }
Mauro Carvalho Chehabe9bca892020-10-30 08:40:21 +0100185
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300186 } else {
187 $data{$what}->{$tag} = $content;
188 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300189 next;
190 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300191 }
192
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300193 # Store any contents before tags at the database
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300194 if (!$tag && $data{$nametag}->{what}) {
195 $data{$nametag}->{description} .= $_;
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -0300196 next;
197 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300198
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300199 if ($tag eq "description") {
Mauro Carvalho Chehabe9bca892020-10-30 08:40:21 +0100200 my $content = $_;
201 while ($content =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {}
Mauro Carvalho Chehabf82a8a72020-10-30 08:40:23 +0100202 if (m/^\s*\n/) {
203 $data{$what}->{$tag} .= "\n";
204 next;
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300205 }
Mauro Carvalho Chehabf82a8a72020-10-30 08:40:23 +0100206
207 if (!defined($space)) {
208 # Preserve initial spaces for the first line
209 if ($content =~ m/^(\s*)(\S.*)$/) {
210 $space = $1;
211 $content = "$2\n";
212 }
213 } else {
214 $space = "" if (!($content =~ s/^($space)//));
215 }
216 $data{$what}->{$tag} .= $content;
217
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300218 next;
219 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300220 if (m/^\s*(.*)/) {
221 $data{$what}->{$tag} .= "\n$1";
222 $data{$what}->{$tag} =~ s/\n+$//;
223 next;
224 }
225
226 # Everything else is error
227 parse_error($file, $ln, "Unexpected line:", $_);
228 }
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100229 $data{$nametag}->{description} =~ s/^\n+// if ($data{$nametag}->{description});
230 if ($what) {
231 parse_error($file, $ln, "What '$what' doesn't have a description", "") if (!$data{$what}->{description});
232
233 foreach my $w(split /, /,$what) {
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100234 $symbols{$w}->{xref} = $what;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100235 };
236 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300237 close IN;
238}
239
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100240sub create_labels {
241 my %labels;
242
243 foreach my $what (keys %data) {
244 next if ($data{$what}->{file} eq "File");
245
246 foreach my $p (@{$data{$what}->{label_list}}) {
247 my ($content, $label) = @{$p};
248 $label = "abi_" . $label . " ";
249 $label =~ tr/A-Z/a-z/;
250
251 # Convert special chars to "_"
252 $label =~s/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\xff])/_/g;
253 $label =~ s,_+,_,g;
254 $label =~ s,_$,,;
255
256 # Avoid duplicated labels
257 while (defined($labels{$label})) {
258 my @chars = ("A".."Z", "a".."z");
259 $label .= $chars[rand @chars];
260 }
261 $labels{$label} = 1;
262
263 $data{$what}->{label} = $label;
264
265 # only one label is enough
266 last;
267 }
268 }
269}
270
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300271#
272# Outputs the book on ReST format
273#
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300274
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300275sub output_rest {
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100276 create_labels();
277
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300278 foreach my $what (sort {
279 ($data{$a}->{type} eq "File") cmp ($data{$b}->{type} eq "File") ||
280 $a cmp $b
281 } keys %data) {
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300282 my $type = $data{$what}->{type};
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100283
284 my @file = split / /, $data{$what}->{file};
285 my @filepath = split / /, $data{$what}->{filepath};
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300286
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +0100287 if ($enable_lineno) {
288 printf "#define LINENO %s%s#%s\n\n",
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100289 $prefix, $file[0],
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +0100290 $data{$what}->{line_no};
291 }
292
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300293 my $w = $what;
294 $w =~ s/([\(\)\_\-\*\=\^\~\\])/\\$1/g;
295
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100296 if ($type ne "File") {
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100297 printf ".. _%s:\n\n", $data{$what}->{label};
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300298
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100299 my @names = split /, /,$w;
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300300 my $len = 0;
301
302 foreach my $name (@names) {
Mauro Carvalho Chehabc01d62d2020-10-30 08:40:28 +0100303 $name = "**$name**";
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300304 $len = length($name) if (length($name) > $len);
305 }
306
307 print "What:\n\n";
308
309 print "+-" . "-" x $len . "-+\n";
310 foreach my $name (@names) {
311 printf "| %s", $name . " " x ($len - length($name)) . " |\n";
312 print "+-" . "-" x $len . "-+\n";
313 }
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100314
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100315 print "\n";
316 }
317
318 for (my $i = 0; $i < scalar(@filepath); $i++) {
319 my $path = $filepath[$i];
320 my $f = $file[$i];
321
322 $path =~ s,.*/(.*/.*),$1,;;
323 $path =~ s,[/\-],_,g;;
324 my $fileref = "abi_file_".$path;
325
326 if ($type eq "File") {
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100327 print ".. _$fileref:\n\n";
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100328 } else {
329 print "Defined on file :ref:`$f <$fileref>`\n\n";
330 }
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300331 }
332
Mauro Carvalho Chehaba4ea67b2020-10-30 08:40:27 +0100333 if ($type eq "File") {
334 my $bar = $w;
335 $bar =~ s/./-/g;
336 print "$w\n$bar\n\n";
337 }
338
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100339 my $desc = "";
340 $desc = $data{$what}->{description} if (defined($data{$what}->{description}));
341 $desc =~ s/\s+$/\n/;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300342
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300343 if (!($desc =~ /^\s*$/)) {
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +0100344 if ($description_is_rst) {
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300345 print "$desc\n\n";
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +0100346 } else {
347 $desc =~ s/^\s+//;
348
349 # Remove title markups from the description, as they won't work
350 $desc =~ s/\n[\-\*\=\^\~]+\n/\n\n/g;
351
352 if ($desc =~ m/\:\n/ || $desc =~ m/\n[\t ]+/ || $desc =~ m/[\x00-\x08\x0b-\x1f\x7b-\xff]/) {
353 # put everything inside a code block
354 $desc =~ s/\n/\n /g;
355
356 print "::\n\n";
357 print " $desc\n\n";
358 } else {
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;
361 print "$desc\n\n";
362 }
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300363 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300364 } else {
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300365 print "DESCRIPTION MISSING for $what\n\n" if (!$data{$what}->{is_file});
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300366 }
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -0300367
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100368 if ($data{$what}->{symbols}) {
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300369 printf "Has the following ABI:\n\n";
370
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100371 foreach my $content(@{$data{$what}->{symbols}}) {
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100372 my $label = $data{$symbols{$content}->{xref}}->{label};
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300373
374 # Escape special chars from content
375 $content =~s/([\x00-\x1f\x21-\x2f\x3a-\x40\x7b-\xff])/\\$1/g;
376
377 print "- :ref:`$content <$label>`\n\n";
378 }
379 }
Mauro Carvalho Chehaba16ab142020-10-30 08:40:26 +0100380
381 if (defined($data{$what}->{users})) {
382 my $users = $data{$what}->{users};
383
384 $users =~ s/\n/\n\t/g;
385 printf "Users:\n\t%s\n\n", $users if ($users ne "");
386 }
387
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300388 }
389}
390
391#
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300392# Searches for ABI symbols
393#
394sub search_symbols {
395 foreach my $what (sort keys %data) {
396 next if (!($what =~ m/($arg)/));
397
398 my $type = $data{$what}->{type};
399 next if ($type eq "File");
400
401 my $file = $data{$what}->{filepath};
402
403 my $bar = $what;
404 $bar =~ s/./-/g;
405
406 print "\n$what\n$bar\n\n";
407
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100408 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}));
413
414 $kernelversion =~ s/^\s+// if ($kernelversion);
415 $contact =~ s/^\s+// if ($contact);
416 if ($users) {
417 $users =~ s/^\s+//;
418 $users =~ s/\n//g;
419 }
420 $date =~ s/^\s+// if ($date);
421 $desc =~ s/^\s+// if ($desc);
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300422
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);
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100427 print "Defined on file(s):\t$file\n\n";
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300428 print "Description:\n\n$desc";
429 }
430}
431
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +0100432# 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
435$prefix =~ s,/?$,/,;
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300436
437#
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300438# Parses all ABI files located at $prefix dir
439#
440find({wanted =>\&parse_abi, no_chdir => 1}, $prefix);
441
442print STDERR Data::Dumper->Dump([\%data], [qw(*data)]) if ($debug);
443
444#
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300445# Handles the command
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300446#
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100447if ($cmd eq "search") {
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300448 search_symbols;
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100449} else {
450 if ($cmd eq "rest") {
451 output_rest;
452 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300453
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100454 # Warn about duplicated ABI entries
455 foreach my $what(sort keys %symbols) {
456 my @files = @{$symbols{$what}->{file}};
457
458 next if (scalar(@files) == 1);
459
460 printf STDERR "Warning: $what is defined %d times: @files\n",
461 scalar(@files);
462 }
463}
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300464
465__END__
466
467=head1 NAME
468
469abi_book.pl - parse the Linux ABI files and produce a ReST book.
470
471=head1 SYNOPSIS
472
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +0100473B<abi_book.pl> [--debug] [--enable-lineno] [--man] [--help]
474 [--(no-)rst-source] [--dir=<dir>] <COMAND> [<ARGUMENT>]
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300475
476Where <COMMAND> can be:
477
478=over 8
479
480B<search> [SEARCH_REGEX] - search for [SEARCH_REGEX] inside ABI
481
Mauro Carvalho Chehab7ce7b892019-06-20 14:23:04 -0300482B<rest> - output the ABI in ReST markup language
483
484B<validate> - validate the ABI contents
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300485
486=back
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300487
488=head1 OPTIONS
489
490=over 8
491
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300492=item B<--dir>
493
494Changes the location of the ABI search. By default, it uses
495the Documentation/ABI directory.
496
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +0100497=item B<--rst-source> and B<--no-rst-source>
498
499The input file may be using ReST syntax or not. Those two options allow
500selecting between a rst-compliant source ABI (--rst-source), or a
501plain text that may be violating ReST spec, so it requres some escaping
502logic (--no-rst-source).
503
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +0100504=item B<--enable-lineno>
505
506Enable output of #define LINENO lines.
507
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300508=item B<--debug>
509
510Put the script in verbose mode, useful for debugging. Can be called multiple
511times, to increase verbosity.
512
513=item B<--help>
514
515Prints a brief help message and exits.
516
517=item B<--man>
518
519Prints the manual page and exits.
520
521=back
522
523=head1 DESCRIPTION
524
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300525Parse the Linux ABI files from ABI DIR (usually located at Documentation/ABI),
526allowing to search for ABI symbols or to produce a ReST book containing
527the Linux ABI documentation.
528
529=head1 EXAMPLES
530
531Search for all stable symbols with the word "usb":
532
533=over 8
534
535$ scripts/get_abi.pl search usb --dir Documentation/ABI/stable
536
537=back
538
539Search for all symbols that match the regex expression "usb.*cap":
540
541=over 8
542
543$ scripts/get_abi.pl search usb.*cap
544
545=back
546
547Output all obsoleted symbols in ReST format
548
549=over 8
550
551$ scripts/get_abi.pl rest --dir Documentation/ABI/obsolete
552
553=back
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300554
555=head1 BUGS
556
Mauro Carvalho Chehab7ce7b892019-06-20 14:23:04 -0300557Report bugs to Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300558
559=head1 COPYRIGHT
560
Mauro Carvalho Chehab7ce7b892019-06-20 14:23:04 -0300561Copyright (c) 2016-2019 by Mauro Carvalho Chehab <mchehab+samsung@kernel.org>.
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300562
563License GPLv2: GNU GPL version 2 <http://gnu.org/licenses/gpl.html>.
564
565This is free software: you are free to change and redistribute it.
566There is NO WARRANTY, to the extent permitted by law.
567
568=cut