blob: ff4f9f82ecad4b6279253c3831d7c0dc17dd3bda [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 Chehab55e54142020-10-30 08:40:29 +01006use utf8;
Mauro Carvalho Chehabbbc249f2019-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 Chehabbbc249f2019-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#
21my $description_is_rst = 0;
22
Mauro Carvalho Chehabbbc249f2019-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 Chehabbbc249f2019-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 Chehabbbc249f2019-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 Chehabbbc249f2019-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 Chehabbbc249f2019-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
53 print STDERR "file $file#$ln: $msg at\n\t$data";
54}
55
56#
57# Parse an ABI file, storing its contents at %data
58#
59sub parse_abi {
60 my $file = $File::Find::name;
61
62 my $mode = (stat($file))[2];
63 return if ($mode & S_IFDIR);
64 return if ($file =~ m,/README,);
65
66 my $name = $file;
67 $name =~ s,.*/,,;
68
Mauro Carvalho Chehaba4ea67b2020-10-30 08:40:27 +010069 my $fn = $file;
70 $fn =~ s,Documentation/ABI/,,;
71
72 my $nametag = "File $fn";
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -030073 $data{$nametag}->{what} = "File $name";
74 $data{$nametag}->{type} = "File";
75 $data{$nametag}->{file} = $name;
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030076 $data{$nametag}->{filepath} = $file;
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -030077 $data{$nametag}->{is_file} = 1;
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +010078 $data{$nametag}->{line_no} = 1;
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -030079
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030080 my $type = $file;
81 $type =~ s,.*/(.*)/.*,$1,;
82
83 my $what;
84 my $new_what;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +010085 my $tag = "";
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030086 my $ln;
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -030087 my $xrefs;
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -030088 my $space;
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -030089 my @labels;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +010090 my $label = "";
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030091
92 print STDERR "Opening $file\n" if ($debug > 1);
93 open IN, $file;
94 while(<IN>) {
95 $ln++;
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -030096 if (m/^(\S+)(:\s*)(.*)/i) {
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030097 my $new_tag = lc($1);
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -030098 my $sep = $2;
99 my $content = $3;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300100
Mauro Carvalho Chehab7ce7b892019-06-20 14:23:04 -0300101 if (!($new_tag =~ m/(what|where|date|kernelversion|contact|description|users)/)) {
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300102 if ($tag eq "description") {
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300103 # New "tag" is actually part of
104 # description. Don't consider it a tag
105 $new_tag = "";
Mauro Carvalho Chehab7d7ea8d2019-06-20 14:23:01 -0300106 } elsif ($tag ne "") {
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300107 parse_error($file, $ln, "tag '$tag' is invalid", $_);
108 }
109 }
110
Mauro Carvalho Chehab2c0700e2019-06-20 14:23:03 -0300111 # Invalid, but it is a common mistake
112 if ($new_tag eq "where") {
113 parse_error($file, $ln, "tag 'Where' is invalid. Should be 'What:' instead", $_);
114 $new_tag = "what";
115 }
116
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300117 if ($new_tag =~ m/what/) {
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300118 $space = "";
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100119 $content =~ s/[,.;]$//;
120
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100121 push @{$symbols{$content}->{file}}, " $file:" . ($ln - 1);
122
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300123 if ($tag =~ m/what/) {
124 $what .= ", " . $content;
125 } else {
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100126 if ($what) {
127 parse_error($file, $ln, "What '$what' doesn't have a description", "") if (!$data{$what}->{description});
128
129 foreach my $w(split /, /, $what) {
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100130 $symbols{$w}->{xref} = $what;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100131 };
132 }
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300133
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300134 $what = $content;
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300135 $label = $content;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300136 $new_what = 1;
137 }
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300138 push @labels, [($content, $label)];
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300139 $tag = $new_tag;
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -0300140
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100141 push @{$data{$nametag}->{symbols}}, $content if ($data{$nametag}->{what});
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300142 next;
143 }
144
Mauro Carvalho Chehab7d7ea8d2019-06-20 14:23:01 -0300145 if ($tag ne "" && $new_tag) {
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300146 $tag = $new_tag;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300147
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300148 if ($new_what) {
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100149 @{$data{$what}->{label_list}} = @labels if ($data{$nametag}->{what});
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300150 @labels = ();
151 $label = "";
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300152 $new_what = 0;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300153
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300154 $data{$what}->{type} = $type;
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100155 if (!defined($data{$what}->{file})) {
156 $data{$what}->{file} = $name;
157 $data{$what}->{filepath} = $file;
158 } else {
159 if ($name ne $data{$what}->{file}) {
160 $data{$what}->{file} .= " " . $name;
161 $data{$what}->{filepath} .= " " . $file;
162 }
163 }
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300164 print STDERR "\twhat: $what\n" if ($debug > 1);
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100165 $data{$what}->{line_no} = $ln;
166 } else {
167 $data{$what}->{line_no} = $ln if (!defined($data{$what}->{line_no}));
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300168 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300169
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300170 if (!$what) {
171 parse_error($file, $ln, "'What:' should come first:", $_);
172 next;
173 }
Mauro Carvalho Chehabf82a8a72020-10-30 08:40:23 +0100174 if ($new_tag eq "description") {
175 $sep =~ s,:, ,;
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +0100176 $content = ' ' x length($new_tag) . $sep . $content;
Mauro Carvalho Chehabf82a8a72020-10-30 08:40:23 +0100177 while ($content =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {}
178 if ($content =~ m/^(\s*)(\S.*)$/) {
179 # Preserve initial spaces for the first line
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +0100180 $space = $1;
Mauro Carvalho Chehabf82a8a72020-10-30 08:40:23 +0100181 $content = "$2\n";
182 $data{$what}->{$tag} .= $content;
183 } else {
184 undef($space);
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300185 }
Mauro Carvalho Chehabe9bca892020-10-30 08:40:21 +0100186
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300187 } else {
188 $data{$what}->{$tag} = $content;
189 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300190 next;
191 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300192 }
193
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300194 # Store any contents before tags at the database
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300195 if (!$tag && $data{$nametag}->{what}) {
196 $data{$nametag}->{description} .= $_;
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -0300197 next;
198 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300199
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300200 if ($tag eq "description") {
Mauro Carvalho Chehabe9bca892020-10-30 08:40:21 +0100201 my $content = $_;
202 while ($content =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {}
Mauro Carvalho Chehabf82a8a72020-10-30 08:40:23 +0100203 if (m/^\s*\n/) {
204 $data{$what}->{$tag} .= "\n";
205 next;
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300206 }
Mauro Carvalho Chehabf82a8a72020-10-30 08:40:23 +0100207
208 if (!defined($space)) {
209 # Preserve initial spaces for the first line
210 if ($content =~ m/^(\s*)(\S.*)$/) {
211 $space = $1;
212 $content = "$2\n";
213 }
214 } else {
215 $space = "" if (!($content =~ s/^($space)//));
216 }
217 $data{$what}->{$tag} .= $content;
218
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300219 next;
220 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300221 if (m/^\s*(.*)/) {
222 $data{$what}->{$tag} .= "\n$1";
223 $data{$what}->{$tag} =~ s/\n+$//;
224 next;
225 }
226
227 # Everything else is error
228 parse_error($file, $ln, "Unexpected line:", $_);
229 }
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100230 $data{$nametag}->{description} =~ s/^\n+// if ($data{$nametag}->{description});
231 if ($what) {
232 parse_error($file, $ln, "What '$what' doesn't have a description", "") if (!$data{$what}->{description});
233
234 foreach my $w(split /, /,$what) {
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100235 $symbols{$w}->{xref} = $what;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100236 };
237 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300238 close IN;
239}
240
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100241sub create_labels {
242 my %labels;
243
244 foreach my $what (keys %data) {
245 next if ($data{$what}->{file} eq "File");
246
247 foreach my $p (@{$data{$what}->{label_list}}) {
248 my ($content, $label) = @{$p};
249 $label = "abi_" . $label . " ";
250 $label =~ tr/A-Z/a-z/;
251
252 # Convert special chars to "_"
253 $label =~s/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\xff])/_/g;
254 $label =~ s,_+,_,g;
255 $label =~ s,_$,,;
256
257 # Avoid duplicated labels
258 while (defined($labels{$label})) {
259 my @chars = ("A".."Z", "a".."z");
260 $label .= $chars[rand @chars];
261 }
262 $labels{$label} = 1;
263
264 $data{$what}->{label} = $label;
265
266 # only one label is enough
267 last;
268 }
269 }
270}
271
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300272#
273# Outputs the book on ReST format
274#
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300275
Mauro Carvalho Chehab55e54142020-10-30 08:40:29 +0100276# \b doesn't work well with paths. So, we need to define something else
277my $bondary = qr { (?<![\w\/\`\{])(?=[\w\/\`\{])|(?<=[\w\/\`\{])(?![\w\/\`\{]) }x;
278
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300279sub output_rest {
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100280 create_labels();
281
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300282 foreach my $what (sort {
283 ($data{$a}->{type} eq "File") cmp ($data{$b}->{type} eq "File") ||
284 $a cmp $b
285 } keys %data) {
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300286 my $type = $data{$what}->{type};
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100287
288 my @file = split / /, $data{$what}->{file};
289 my @filepath = split / /, $data{$what}->{filepath};
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300290
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +0100291 if ($enable_lineno) {
292 printf "#define LINENO %s%s#%s\n\n",
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100293 $prefix, $file[0],
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +0100294 $data{$what}->{line_no};
295 }
296
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300297 my $w = $what;
298 $w =~ s/([\(\)\_\-\*\=\^\~\\])/\\$1/g;
299
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100300 if ($type ne "File") {
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100301 printf ".. _%s:\n\n", $data{$what}->{label};
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300302
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100303 my @names = split /, /,$w;
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300304 my $len = 0;
305
306 foreach my $name (@names) {
Mauro Carvalho Chehabc01d62d2020-10-30 08:40:28 +0100307 $name = "**$name**";
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300308 $len = length($name) if (length($name) > $len);
309 }
310
311 print "What:\n\n";
312
313 print "+-" . "-" x $len . "-+\n";
314 foreach my $name (@names) {
315 printf "| %s", $name . " " x ($len - length($name)) . " |\n";
316 print "+-" . "-" x $len . "-+\n";
317 }
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100318
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100319 print "\n";
320 }
321
322 for (my $i = 0; $i < scalar(@filepath); $i++) {
323 my $path = $filepath[$i];
324 my $f = $file[$i];
325
326 $path =~ s,.*/(.*/.*),$1,;;
327 $path =~ s,[/\-],_,g;;
328 my $fileref = "abi_file_".$path;
329
330 if ($type eq "File") {
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100331 print ".. _$fileref:\n\n";
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100332 } else {
333 print "Defined on file :ref:`$f <$fileref>`\n\n";
334 }
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300335 }
336
Mauro Carvalho Chehaba4ea67b2020-10-30 08:40:27 +0100337 if ($type eq "File") {
338 my $bar = $w;
339 $bar =~ s/./-/g;
340 print "$w\n$bar\n\n";
341 }
342
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100343 my $desc = "";
344 $desc = $data{$what}->{description} if (defined($data{$what}->{description}));
345 $desc =~ s/\s+$/\n/;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300346
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300347 if (!($desc =~ /^\s*$/)) {
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +0100348 if ($description_is_rst) {
Mauro Carvalho Chehab55e54142020-10-30 08:40:29 +0100349 # Enrich text by creating cross-references
350
351 $desc =~ s,Documentation/(?!devicetree)(\S+)\.rst,:doc:`/$1`,g;
352
353 my @matches = $desc =~ m,Documentation/ABI/([\w\/\-]+),;
354 foreach my $f (@matches) {
355 my $xref = $f;
356 my $path = $f;
357 $path =~ s,.*/(.*/.*),$1,;;
358 $path =~ s,[/\-],_,g;;
359 $xref .= " <abi_file_" . $path . ">";
360 $desc =~ s,\bDocumentation/ABI/$f\b,:ref:`$xref`,g;
361 }
362
363 @matches = $desc =~ m,$bondary(/sys/[^\s\.\,\;\:\*\s\`\'\(\)]+)$bondary,;
364
365 foreach my $s (@matches) {
366 if (defined($data{$s}) && defined($data{$s}->{label})) {
367 my $xref = $s;
368
369 $xref =~ s/([\x00-\x1f\x21-\x2f\x3a-\x40\x7b-\xff])/\\$1/g;
370 $xref = ":ref:`$xref <" . $data{$s}->{label} . ">`";
371
372 $desc =~ s,$bondary$s$bondary,$xref,g;
373 }
374 }
375
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300376 print "$desc\n\n";
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +0100377 } else {
378 $desc =~ s/^\s+//;
379
380 # Remove title markups from the description, as they won't work
381 $desc =~ s/\n[\-\*\=\^\~]+\n/\n\n/g;
382
383 if ($desc =~ m/\:\n/ || $desc =~ m/\n[\t ]+/ || $desc =~ m/[\x00-\x08\x0b-\x1f\x7b-\xff]/) {
384 # put everything inside a code block
385 $desc =~ s/\n/\n /g;
386
387 print "::\n\n";
388 print " $desc\n\n";
389 } else {
390 # Escape any special chars from description
391 $desc =~s/([\x00-\x08\x0b-\x1f\x21-\x2a\x2d\x2f\x3c-\x40\x5c\x5e-\x60\x7b-\xff])/\\$1/g;
392 print "$desc\n\n";
393 }
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300394 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300395 } else {
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300396 print "DESCRIPTION MISSING for $what\n\n" if (!$data{$what}->{is_file});
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300397 }
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -0300398
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100399 if ($data{$what}->{symbols}) {
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300400 printf "Has the following ABI:\n\n";
401
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100402 foreach my $content(@{$data{$what}->{symbols}}) {
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100403 my $label = $data{$symbols{$content}->{xref}}->{label};
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300404
405 # Escape special chars from content
406 $content =~s/([\x00-\x1f\x21-\x2f\x3a-\x40\x7b-\xff])/\\$1/g;
407
408 print "- :ref:`$content <$label>`\n\n";
409 }
410 }
Mauro Carvalho Chehaba16ab142020-10-30 08:40:26 +0100411
412 if (defined($data{$what}->{users})) {
413 my $users = $data{$what}->{users};
414
415 $users =~ s/\n/\n\t/g;
416 printf "Users:\n\t%s\n\n", $users if ($users ne "");
417 }
418
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300419 }
420}
421
422#
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300423# Searches for ABI symbols
424#
425sub search_symbols {
426 foreach my $what (sort keys %data) {
427 next if (!($what =~ m/($arg)/));
428
429 my $type = $data{$what}->{type};
430 next if ($type eq "File");
431
432 my $file = $data{$what}->{filepath};
433
434 my $bar = $what;
435 $bar =~ s/./-/g;
436
437 print "\n$what\n$bar\n\n";
438
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100439 my $kernelversion = $data{$what}->{kernelversion} if (defined($data{$what}->{kernelversion}));
440 my $contact = $data{$what}->{contact} if (defined($data{$what}->{contact}));
441 my $users = $data{$what}->{users} if (defined($data{$what}->{users}));
442 my $date = $data{$what}->{date} if (defined($data{$what}->{date}));
443 my $desc = $data{$what}->{description} if (defined($data{$what}->{description}));
444
445 $kernelversion =~ s/^\s+// if ($kernelversion);
446 $contact =~ s/^\s+// if ($contact);
447 if ($users) {
448 $users =~ s/^\s+//;
449 $users =~ s/\n//g;
450 }
451 $date =~ s/^\s+// if ($date);
452 $desc =~ s/^\s+// if ($desc);
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300453
454 printf "Kernel version:\t\t%s\n", $kernelversion if ($kernelversion);
455 printf "Date:\t\t\t%s\n", $date if ($date);
456 printf "Contact:\t\t%s\n", $contact if ($contact);
457 printf "Users:\t\t\t%s\n", $users if ($users);
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100458 print "Defined on file(s):\t$file\n\n";
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300459 print "Description:\n\n$desc";
460 }
461}
462
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +0100463# Ensure that the prefix will always end with a slash
464# While this is not needed for find, it makes the patch nicer
465# with --enable-lineno
466$prefix =~ s,/?$,/,;
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300467
468#
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300469# Parses all ABI files located at $prefix dir
470#
471find({wanted =>\&parse_abi, no_chdir => 1}, $prefix);
472
473print STDERR Data::Dumper->Dump([\%data], [qw(*data)]) if ($debug);
474
475#
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300476# Handles the command
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300477#
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100478if ($cmd eq "search") {
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300479 search_symbols;
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100480} else {
481 if ($cmd eq "rest") {
482 output_rest;
483 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300484
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100485 # Warn about duplicated ABI entries
486 foreach my $what(sort keys %symbols) {
487 my @files = @{$symbols{$what}->{file}};
488
489 next if (scalar(@files) == 1);
490
491 printf STDERR "Warning: $what is defined %d times: @files\n",
492 scalar(@files);
493 }
494}
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300495
496__END__
497
498=head1 NAME
499
500abi_book.pl - parse the Linux ABI files and produce a ReST book.
501
502=head1 SYNOPSIS
503
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +0100504B<abi_book.pl> [--debug] [--enable-lineno] [--man] [--help]
505 [--(no-)rst-source] [--dir=<dir>] <COMAND> [<ARGUMENT>]
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300506
507Where <COMMAND> can be:
508
509=over 8
510
511B<search> [SEARCH_REGEX] - search for [SEARCH_REGEX] inside ABI
512
Mauro Carvalho Chehab7ce7b892019-06-20 14:23:04 -0300513B<rest> - output the ABI in ReST markup language
514
515B<validate> - validate the ABI contents
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300516
517=back
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300518
519=head1 OPTIONS
520
521=over 8
522
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300523=item B<--dir>
524
525Changes the location of the ABI search. By default, it uses
526the Documentation/ABI directory.
527
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +0100528=item B<--rst-source> and B<--no-rst-source>
529
530The input file may be using ReST syntax or not. Those two options allow
531selecting between a rst-compliant source ABI (--rst-source), or a
532plain text that may be violating ReST spec, so it requres some escaping
533logic (--no-rst-source).
534
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +0100535=item B<--enable-lineno>
536
537Enable output of #define LINENO lines.
538
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300539=item B<--debug>
540
541Put the script in verbose mode, useful for debugging. Can be called multiple
542times, to increase verbosity.
543
544=item B<--help>
545
546Prints a brief help message and exits.
547
548=item B<--man>
549
550Prints the manual page and exits.
551
552=back
553
554=head1 DESCRIPTION
555
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300556Parse the Linux ABI files from ABI DIR (usually located at Documentation/ABI),
557allowing to search for ABI symbols or to produce a ReST book containing
558the Linux ABI documentation.
559
560=head1 EXAMPLES
561
562Search for all stable symbols with the word "usb":
563
564=over 8
565
566$ scripts/get_abi.pl search usb --dir Documentation/ABI/stable
567
568=back
569
570Search for all symbols that match the regex expression "usb.*cap":
571
572=over 8
573
574$ scripts/get_abi.pl search usb.*cap
575
576=back
577
578Output all obsoleted symbols in ReST format
579
580=over 8
581
582$ scripts/get_abi.pl rest --dir Documentation/ABI/obsolete
583
584=back
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300585
586=head1 BUGS
587
Mauro Carvalho Chehab7ce7b892019-06-20 14:23:04 -0300588Report bugs to Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300589
590=head1 COPYRIGHT
591
Mauro Carvalho Chehab7ce7b892019-06-20 14:23:04 -0300592Copyright (c) 2016-2019 by Mauro Carvalho Chehab <mchehab+samsung@kernel.org>.
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300593
594License GPLv2: GNU GPL version 2 <http://gnu.org/licenses/gpl.html>.
595
596This is free software: you are free to change and redistribute it.
597There is NO WARRANTY, to the extent permitted by law.
598
599=cut