blob: 1389db76cff31d1fef8f5a075bfac8d5040b7df2 [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
Mauro Carvalho Chehab42f09842021-09-27 15:49:51 +02004BEGIN { $Pod::Usage::Formatter = 'Pod::Text::Termcap'; }
5
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -03006use strict;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +01007use warnings;
Mauro Carvalho Chehab55e54142020-10-30 08:40:29 +01008use utf8;
Mauro Carvalho Chehab42f09842021-09-27 15:49:51 +02009use Pod::Usage qw(pod2usage);
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -030010use Getopt::Long;
11use File::Find;
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +020012use IO::Handle;
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -030013use Fcntl ':mode';
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +020014use Cwd 'abs_path';
Mauro Carvalho Chehab46f661f2021-09-23 17:41:14 +020015use Data::Dumper;
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -030016
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +010017my $help = 0;
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +020018my $hint = 0;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +010019my $man = 0;
20my $debug = 0;
21my $enable_lineno = 0;
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +020022my $show_warnings = 1;
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030023my $prefix="Documentation/ABI";
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +020024my $sysfs_prefix="/sys";
Mauro Carvalho Chehab14c94252021-09-18 11:52:14 +020025my $search_string;
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -030026
Mauro Carvalho Chehab46f661f2021-09-23 17:41:14 +020027# Debug options
28my $dbg_what_parsing = 1;
29my $dbg_what_open = 2;
30my $dbg_dump_abi_structs = 4;
Mauro Carvalho Chehabf34f6722021-09-23 17:41:18 +020031my $dbg_undefined = 8;
Mauro Carvalho Chehab46f661f2021-09-23 17:41:14 +020032
Mauro Carvalho Chehab3a1cc062021-09-27 15:49:49 +020033$Data::Dumper::Indent = 1;
34$Data::Dumper::Terse = 1;
35
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +010036#
37# If true, assumes that the description is formatted with ReST
38#
Mauro Carvalho Chehab2fcce372020-10-30 08:40:58 +010039my $description_is_rst = 1;
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +010040
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -030041GetOptions(
Mauro Carvalho Chehab46f661f2021-09-23 17:41:14 +020042 "debug=i" => \$debug,
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +010043 "enable-lineno" => \$enable_lineno,
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +010044 "rst-source!" => \$description_is_rst,
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030045 "dir=s" => \$prefix,
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -030046 'help|?' => \$help,
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +020047 "show-hints" => \$hint,
Mauro Carvalho Chehab14c94252021-09-18 11:52:14 +020048 "search-string=s" => \$search_string,
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -030049 man => \$man
50) or pod2usage(2);
51
52pod2usage(1) if $help;
Mauro Carvalho Chehab42f09842021-09-27 15:49:51 +020053pod2usage(-exitstatus => 0, -noperldoc, -verbose => 2) if $man;
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -030054
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030055pod2usage(2) if (scalar @ARGV < 1 || @ARGV > 2);
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -030056
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030057my ($cmd, $arg) = @ARGV;
58
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +020059pod2usage(2) if ($cmd ne "search" && $cmd ne "rest" && $cmd ne "validate" && $cmd ne "undefined");
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030060pod2usage(2) if ($cmd eq "search" && !$arg);
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -030061
Mauro Carvalho Chehab46f661f2021-09-23 17:41:14 +020062require Data::Dumper if ($debug & $dbg_dump_abi_structs);
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -030063
64my %data;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +010065my %symbols;
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -030066
67#
68# Displays an error message, printing file name and line
69#
70sub parse_error($$$$) {
71 my ($file, $ln, $msg, $data) = @_;
72
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +020073 return if (!$show_warnings);
74
Mauro Carvalho Chehab75442fb2020-10-30 08:40:45 +010075 $data =~ s/\s+$/\n/;
76
77 print STDERR "Warning: file $file#$ln:\n\t$msg";
78
79 if ($data ne "") {
80 print STDERR ". Line\n\t\t$data";
81 } else {
82 print STDERR "\n";
83 }
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -030084}
85
86#
87# Parse an ABI file, storing its contents at %data
88#
89sub parse_abi {
90 my $file = $File::Find::name;
91
92 my $mode = (stat($file))[2];
93 return if ($mode & S_IFDIR);
94 return if ($file =~ m,/README,);
Jonathan Neuschäferaa21a1b2022-01-29 01:50:18 +010095 return if ($file =~ m,/\.,);
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -030096
97 my $name = $file;
98 $name =~ s,.*/,,;
99
Mauro Carvalho Chehaba4ea67b2020-10-30 08:40:27 +0100100 my $fn = $file;
101 $fn =~ s,Documentation/ABI/,,;
102
103 my $nametag = "File $fn";
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300104 $data{$nametag}->{what} = "File $name";
105 $data{$nametag}->{type} = "File";
106 $data{$nametag}->{file} = $name;
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300107 $data{$nametag}->{filepath} = $file;
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300108 $data{$nametag}->{is_file} = 1;
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +0100109 $data{$nametag}->{line_no} = 1;
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300110
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300111 my $type = $file;
112 $type =~ s,.*/(.*)/.*,$1,;
113
114 my $what;
115 my $new_what;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100116 my $tag = "";
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300117 my $ln;
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -0300118 my $xrefs;
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300119 my $space;
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300120 my @labels;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100121 my $label = "";
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300122
Mauro Carvalho Chehab46f661f2021-09-23 17:41:14 +0200123 print STDERR "Opening $file\n" if ($debug & $dbg_what_open);
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300124 open IN, $file;
125 while(<IN>) {
126 $ln++;
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300127 if (m/^(\S+)(:\s*)(.*)/i) {
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300128 my $new_tag = lc($1);
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300129 my $sep = $2;
130 my $content = $3;
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300131
Mauro Carvalho Chehab7ce7b892019-06-20 14:23:04 -0300132 if (!($new_tag =~ m/(what|where|date|kernelversion|contact|description|users)/)) {
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300133 if ($tag eq "description") {
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300134 # New "tag" is actually part of
135 # description. Don't consider it a tag
136 $new_tag = "";
Mauro Carvalho Chehab7d7ea8d2019-06-20 14:23:01 -0300137 } elsif ($tag ne "") {
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300138 parse_error($file, $ln, "tag '$tag' is invalid", $_);
139 }
140 }
141
Mauro Carvalho Chehab2c0700e2019-06-20 14:23:03 -0300142 # Invalid, but it is a common mistake
143 if ($new_tag eq "where") {
Mauro Carvalho Chehab75442fb2020-10-30 08:40:45 +0100144 parse_error($file, $ln, "tag 'Where' is invalid. Should be 'What:' instead", "");
Mauro Carvalho Chehab2c0700e2019-06-20 14:23:03 -0300145 $new_tag = "what";
146 }
147
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300148 if ($new_tag =~ m/what/) {
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300149 $space = "";
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100150 $content =~ s/[,.;]$//;
151
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100152 push @{$symbols{$content}->{file}}, " $file:" . ($ln - 1);
153
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300154 if ($tag =~ m/what/) {
Mauro Carvalho Chehabab9c1482021-09-18 11:52:11 +0200155 $what .= "\xac" . $content;
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300156 } else {
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100157 if ($what) {
158 parse_error($file, $ln, "What '$what' doesn't have a description", "") if (!$data{$what}->{description});
159
Mauro Carvalho Chehabab9c1482021-09-18 11:52:11 +0200160 foreach my $w(split /\xac/, $what) {
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100161 $symbols{$w}->{xref} = $what;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100162 };
163 }
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300164
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300165 $what = $content;
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300166 $label = $content;
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300167 $new_what = 1;
168 }
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300169 push @labels, [($content, $label)];
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300170 $tag = $new_tag;
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -0300171
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100172 push @{$data{$nametag}->{symbols}}, $content if ($data{$nametag}->{what});
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300173 next;
174 }
175
Mauro Carvalho Chehab7d7ea8d2019-06-20 14:23:01 -0300176 if ($tag ne "" && $new_tag) {
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300177 $tag = $new_tag;
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300178
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300179 if ($new_what) {
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100180 @{$data{$what}->{label_list}} = @labels if ($data{$nametag}->{what});
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300181 @labels = ();
182 $label = "";
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300183 $new_what = 0;
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300184
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300185 $data{$what}->{type} = $type;
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100186 if (!defined($data{$what}->{file})) {
187 $data{$what}->{file} = $name;
188 $data{$what}->{filepath} = $file;
189 } else {
Mauro Carvalho Chehabff3777d2021-09-27 13:10:50 +0200190 $data{$what}->{description} .= "\n\n" if (defined($data{$what}->{description}));
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100191 if ($name ne $data{$what}->{file}) {
192 $data{$what}->{file} .= " " . $name;
193 $data{$what}->{filepath} .= " " . $file;
194 }
195 }
Mauro Carvalho Chehab46f661f2021-09-23 17:41:14 +0200196 print STDERR "\twhat: $what\n" if ($debug & $dbg_what_parsing);
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100197 $data{$what}->{line_no} = $ln;
198 } else {
199 $data{$what}->{line_no} = $ln if (!defined($data{$what}->{line_no}));
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300200 }
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300201
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300202 if (!$what) {
203 parse_error($file, $ln, "'What:' should come first:", $_);
204 next;
205 }
Mauro Carvalho Chehabf82a8a72020-10-30 08:40:23 +0100206 if ($new_tag eq "description") {
207 $sep =~ s,:, ,;
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +0100208 $content = ' ' x length($new_tag) . $sep . $content;
Mauro Carvalho Chehabf82a8a72020-10-30 08:40:23 +0100209 while ($content =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {}
210 if ($content =~ m/^(\s*)(\S.*)$/) {
211 # Preserve initial spaces for the first line
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +0100212 $space = $1;
Mauro Carvalho Chehabf82a8a72020-10-30 08:40:23 +0100213 $content = "$2\n";
214 $data{$what}->{$tag} .= $content;
215 } else {
216 undef($space);
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300217 }
Mauro Carvalho Chehabe9bca892020-10-30 08:40:21 +0100218
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300219 } else {
220 $data{$what}->{$tag} = $content;
221 }
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300222 next;
223 }
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300224 }
225
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300226 # Store any contents before tags at the database
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300227 if (!$tag && $data{$nametag}->{what}) {
228 $data{$nametag}->{description} .= $_;
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -0300229 next;
230 }
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300231
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300232 if ($tag eq "description") {
Mauro Carvalho Chehabe9bca892020-10-30 08:40:21 +0100233 my $content = $_;
234 while ($content =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {}
Mauro Carvalho Chehabf82a8a72020-10-30 08:40:23 +0100235 if (m/^\s*\n/) {
236 $data{$what}->{$tag} .= "\n";
237 next;
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300238 }
Mauro Carvalho Chehabf82a8a72020-10-30 08:40:23 +0100239
240 if (!defined($space)) {
241 # Preserve initial spaces for the first line
242 if ($content =~ m/^(\s*)(\S.*)$/) {
243 $space = $1;
244 $content = "$2\n";
245 }
246 } else {
247 $space = "" if (!($content =~ s/^($space)//));
248 }
249 $data{$what}->{$tag} .= $content;
250
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300251 next;
252 }
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300253 if (m/^\s*(.*)/) {
254 $data{$what}->{$tag} .= "\n$1";
255 $data{$what}->{$tag} =~ s/\n+$//;
256 next;
257 }
258
259 # Everything else is error
Mauro Carvalho Chehab75442fb2020-10-30 08:40:45 +0100260 parse_error($file, $ln, "Unexpected content", $_);
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300261 }
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100262 $data{$nametag}->{description} =~ s/^\n+// if ($data{$nametag}->{description});
263 if ($what) {
264 parse_error($file, $ln, "What '$what' doesn't have a description", "") if (!$data{$what}->{description});
265
Mauro Carvalho Chehabab9c1482021-09-18 11:52:11 +0200266 foreach my $w(split /\xac/,$what) {
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100267 $symbols{$w}->{xref} = $what;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100268 };
269 }
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300270 close IN;
271}
272
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100273sub create_labels {
274 my %labels;
275
276 foreach my $what (keys %data) {
277 next if ($data{$what}->{file} eq "File");
278
279 foreach my $p (@{$data{$what}->{label_list}}) {
280 my ($content, $label) = @{$p};
281 $label = "abi_" . $label . " ";
282 $label =~ tr/A-Z/a-z/;
283
284 # Convert special chars to "_"
285 $label =~s/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\xff])/_/g;
286 $label =~ s,_+,_,g;
287 $label =~ s,_$,,;
288
289 # Avoid duplicated labels
290 while (defined($labels{$label})) {
291 my @chars = ("A".."Z", "a".."z");
292 $label .= $chars[rand @chars];
293 }
294 $labels{$label} = 1;
295
296 $data{$what}->{label} = $label;
297
298 # only one label is enough
299 last;
300 }
301 }
302}
303
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300304#
305# Outputs the book on ReST format
306#
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300307
Mauro Carvalho Chehab50ebf8f2021-03-25 11:38:24 +0100308# \b doesn't work well with paths. So, we need to define something else:
309# Boundaries are punct characters, spaces and end-of-line
310my $start = qr {(^|\s|\() }x;
311my $bondary = qr { ([,.:;\)\s]|\z) }x;
Mauro Carvalho Chehab87ec9ea12021-03-25 11:38:25 +0100312my $xref_match = qr { $start(\/(sys|config|proc|dev|kvd)\/[^,.:;\)\s]+)$bondary }x;
Mauro Carvalho Chehabb0f95802021-03-25 11:38:22 +0100313my $symbols = qr { ([\x01-\x08\x0e-\x1f\x21-\x2f\x3a-\x40\x7b-\xff]) }x;
Mauro Carvalho Chehab55e54142020-10-30 08:40:29 +0100314
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300315sub output_rest {
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100316 create_labels();
317
Mauro Carvalho Chehab9d4fdda2020-11-02 11:32:16 +0100318 my $part = "";
319
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300320 foreach my $what (sort {
321 ($data{$a}->{type} eq "File") cmp ($data{$b}->{type} eq "File") ||
322 $a cmp $b
323 } keys %data) {
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300324 my $type = $data{$what}->{type};
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100325
326 my @file = split / /, $data{$what}->{file};
327 my @filepath = split / /, $data{$what}->{filepath};
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300328
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +0100329 if ($enable_lineno) {
Mauro Carvalho Chehab92b6de12022-03-26 11:27:23 +0100330 printf ".. LINENO %s%s#%s\n\n",
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100331 $prefix, $file[0],
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +0100332 $data{$what}->{line_no};
333 }
334
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300335 my $w = $what;
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300336
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100337 if ($type ne "File") {
Mauro Carvalho Chehab9d4fdda2020-11-02 11:32:16 +0100338 my $cur_part = $what;
339 if ($what =~ '/') {
340 if ($what =~ m#^(\/?(?:[\w\-]+\/?){1,2})#) {
341 $cur_part = "Symbols under $1";
342 $cur_part =~ s,/$,,;
343 }
344 }
345
346 if ($cur_part ne "" && $part ne $cur_part) {
347 $part = $cur_part;
348 my $bar = $part;
349 $bar =~ s/./-/g;
350 print "$part\n$bar\n\n";
351 }
352
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100353 printf ".. _%s:\n\n", $data{$what}->{label};
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300354
Mauro Carvalho Chehabab9c1482021-09-18 11:52:11 +0200355 my @names = split /\xac/,$w;
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300356 my $len = 0;
357
358 foreach my $name (@names) {
Mauro Carvalho Chehabb0f95802021-03-25 11:38:22 +0100359 $name =~ s/$symbols/\\$1/g;
Mauro Carvalho Chehabc01d62d2020-10-30 08:40:28 +0100360 $name = "**$name**";
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300361 $len = length($name) if (length($name) > $len);
362 }
363
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300364 print "+-" . "-" x $len . "-+\n";
365 foreach my $name (@names) {
366 printf "| %s", $name . " " x ($len - length($name)) . " |\n";
367 print "+-" . "-" x $len . "-+\n";
368 }
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100369
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100370 print "\n";
371 }
372
373 for (my $i = 0; $i < scalar(@filepath); $i++) {
374 my $path = $filepath[$i];
375 my $f = $file[$i];
376
377 $path =~ s,.*/(.*/.*),$1,;;
378 $path =~ s,[/\-],_,g;;
379 my $fileref = "abi_file_".$path;
380
381 if ($type eq "File") {
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100382 print ".. _$fileref:\n\n";
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100383 } else {
384 print "Defined on file :ref:`$f <$fileref>`\n\n";
385 }
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300386 }
387
Mauro Carvalho Chehaba4ea67b2020-10-30 08:40:27 +0100388 if ($type eq "File") {
389 my $bar = $w;
390 $bar =~ s/./-/g;
391 print "$w\n$bar\n\n";
392 }
393
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100394 my $desc = "";
395 $desc = $data{$what}->{description} if (defined($data{$what}->{description}));
396 $desc =~ s/\s+$/\n/;
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300397
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300398 if (!($desc =~ /^\s*$/)) {
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +0100399 if ($description_is_rst) {
Mauro Carvalho Chehabdaaaf582020-11-02 11:32:15 +0100400 # Remove title markups from the description
401 # Having titles inside ABI files will only work if extra
402 # care would be taken in order to strictly follow the same
403 # level order for each markup.
404 $desc =~ s/\n[\-\*\=\^\~]+\n/\n\n/g;
405
Mauro Carvalho Chehab55e54142020-10-30 08:40:29 +0100406 # Enrich text by creating cross-references
407
Mauro Carvalho Chehabc27c2e32021-03-25 11:38:26 +0100408 my $new_desc = "";
Mauro Carvalho Chehab2ae7bb52021-03-25 11:38:27 +0100409 my $init_indent = -1;
410 my $literal_indent = -1;
411
Mauro Carvalho Chehabc27c2e32021-03-25 11:38:26 +0100412 open(my $fh, "+<", \$desc);
413 while (my $d = <$fh>) {
Mauro Carvalho Chehab2ae7bb52021-03-25 11:38:27 +0100414 my $indent = $d =~ m/^(\s+)/;
415 my $spaces = length($indent);
416 $init_indent = $indent if ($init_indent < 0);
417 if ($literal_indent >= 0) {
418 if ($spaces > $literal_indent) {
419 $new_desc .= $d;
420 next;
421 } else {
422 $literal_indent = -1;
423 }
424 } else {
425 if ($d =~ /()::$/ && !($d =~ /^\s*\.\./)) {
426 $literal_indent = $spaces;
427 }
428 }
429
Mauro Carvalho Chehabc27c2e32021-03-25 11:38:26 +0100430 $d =~ s,Documentation/(?!devicetree)(\S+)\.rst,:doc:`/$1`,g;
Mauro Carvalho Chehab55e54142020-10-30 08:40:29 +0100431
Mauro Carvalho Chehabc27c2e32021-03-25 11:38:26 +0100432 my @matches = $d =~ m,Documentation/ABI/([\w\/\-]+),g;
433 foreach my $f (@matches) {
434 my $xref = $f;
435 my $path = $f;
436 $path =~ s,.*/(.*/.*),$1,;;
437 $path =~ s,[/\-],_,g;;
438 $xref .= " <abi_file_" . $path . ">";
439 $d =~ s,\bDocumentation/ABI/$f\b,:ref:`$xref`,g;
Mauro Carvalho Chehab55e54142020-10-30 08:40:29 +0100440 }
Mauro Carvalho Chehab55e54142020-10-30 08:40:29 +0100441
Mauro Carvalho Chehabc27c2e32021-03-25 11:38:26 +0100442 # Seek for cross reference symbols like /sys/...
443 @matches = $d =~ m/$xref_match/g;
444
445 foreach my $s (@matches) {
446 next if (!($s =~ m,/,));
447 if (defined($data{$s}) && defined($data{$s}->{label})) {
448 my $xref = $s;
449
450 $xref =~ s/$symbols/\\$1/g;
451 $xref = ":ref:`$xref <" . $data{$s}->{label} . ">`";
452
453 $d =~ s,$start$s$bondary,$1$xref$2,g;
454 }
455 }
456 $new_desc .= $d;
457 }
458 close $fh;
459
460
461 print "$new_desc\n\n";
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +0100462 } else {
463 $desc =~ s/^\s+//;
464
465 # Remove title markups from the description, as they won't work
466 $desc =~ s/\n[\-\*\=\^\~]+\n/\n\n/g;
467
468 if ($desc =~ m/\:\n/ || $desc =~ m/\n[\t ]+/ || $desc =~ m/[\x00-\x08\x0b-\x1f\x7b-\xff]/) {
469 # put everything inside a code block
470 $desc =~ s/\n/\n /g;
471
472 print "::\n\n";
473 print " $desc\n\n";
474 } else {
475 # Escape any special chars from description
476 $desc =~s/([\x00-\x08\x0b-\x1f\x21-\x2a\x2d\x2f\x3c-\x40\x5c\x5e-\x60\x7b-\xff])/\\$1/g;
477 print "$desc\n\n";
478 }
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300479 }
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300480 } else {
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300481 print "DESCRIPTION MISSING for $what\n\n" if (!$data{$what}->{is_file});
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300482 }
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -0300483
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100484 if ($data{$what}->{symbols}) {
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300485 printf "Has the following ABI:\n\n";
486
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100487 foreach my $content(@{$data{$what}->{symbols}}) {
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100488 my $label = $data{$symbols{$content}->{xref}}->{label};
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300489
490 # Escape special chars from content
491 $content =~s/([\x00-\x1f\x21-\x2f\x3a-\x40\x7b-\xff])/\\$1/g;
492
493 print "- :ref:`$content <$label>`\n\n";
494 }
495 }
Mauro Carvalho Chehaba16ab142020-10-30 08:40:26 +0100496
497 if (defined($data{$what}->{users})) {
498 my $users = $data{$what}->{users};
499
500 $users =~ s/\n/\n\t/g;
501 printf "Users:\n\t%s\n\n", $users if ($users ne "");
502 }
503
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300504 }
505}
506
507#
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300508# Searches for ABI symbols
509#
510sub search_symbols {
511 foreach my $what (sort keys %data) {
512 next if (!($what =~ m/($arg)/));
513
514 my $type = $data{$what}->{type};
515 next if ($type eq "File");
516
517 my $file = $data{$what}->{filepath};
518
Mauro Carvalho Chehabe27c42a2021-09-23 17:41:12 +0200519 $what =~ s/\xac/, /g;
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300520 my $bar = $what;
521 $bar =~ s/./-/g;
522
523 print "\n$what\n$bar\n\n";
524
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100525 my $kernelversion = $data{$what}->{kernelversion} if (defined($data{$what}->{kernelversion}));
526 my $contact = $data{$what}->{contact} if (defined($data{$what}->{contact}));
527 my $users = $data{$what}->{users} if (defined($data{$what}->{users}));
528 my $date = $data{$what}->{date} if (defined($data{$what}->{date}));
529 my $desc = $data{$what}->{description} if (defined($data{$what}->{description}));
530
531 $kernelversion =~ s/^\s+// if ($kernelversion);
532 $contact =~ s/^\s+// if ($contact);
533 if ($users) {
534 $users =~ s/^\s+//;
535 $users =~ s/\n//g;
536 }
537 $date =~ s/^\s+// if ($date);
538 $desc =~ s/^\s+// if ($desc);
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300539
540 printf "Kernel version:\t\t%s\n", $kernelversion if ($kernelversion);
541 printf "Date:\t\t\t%s\n", $date if ($date);
542 printf "Contact:\t\t%s\n", $contact if ($contact);
543 printf "Users:\t\t\t%s\n", $users if ($users);
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100544 print "Defined on file(s):\t$file\n\n";
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300545 print "Description:\n\n$desc";
546 }
547}
548
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200549# Exclude /sys/kernel/debug and /sys/kernel/tracing from the search path
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200550sub dont_parse_special_attributes {
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200551 if (($File::Find::dir =~ m,^/sys/kernel,)) {
552 return grep {!/(debug|tracing)/ } @_;
553 }
554
555 if (($File::Find::dir =~ m,^/sys/fs,)) {
556 return grep {!/(pstore|bpf|fuse)/ } @_;
557 }
558
559 return @_
560}
561
562my %leaf;
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200563my %aliases;
564my @files;
Mauro Carvalho Chehabca8e0552021-09-18 11:52:17 +0200565my %root;
566
567sub graph_add_file {
568 my $file = shift;
569 my $type = shift;
570
571 my $dir = $file;
572 $dir =~ s,^(.*/).*,$1,;
573 $file =~ s,.*/,,;
574
575 my $name;
576 my $file_ref = \%root;
577 foreach my $edge(split "/", $dir) {
578 $name .= "$edge/";
579 if (!defined ${$file_ref}{$edge}) {
580 ${$file_ref}{$edge} = { };
581 }
582 $file_ref = \%{$$file_ref{$edge}};
583 ${$file_ref}{"__name"} = [ $name ];
584 }
585 $name .= "$file";
586 ${$file_ref}{$file} = {
587 "__name" => [ $name ]
588 };
589
590 return \%{$$file_ref{$file}};
591}
592
593sub graph_add_link {
594 my $file = shift;
595 my $link = shift;
596
597 # Traverse graph to find the reference
598 my $file_ref = \%root;
599 foreach my $edge(split "/", $file) {
600 $file_ref = \%{$$file_ref{$edge}} || die "Missing node!";
601 }
602
603 # do a BFS
604
605 my @queue;
606 my %seen;
Mauro Carvalho Chehabca8e0552021-09-18 11:52:17 +0200607 my $st;
608
609 push @queue, $file_ref;
610 $seen{$start}++;
611
612 while (@queue) {
613 my $v = shift @queue;
614 my @child = keys(%{$v});
615
616 foreach my $c(@child) {
617 next if $seen{$$v{$c}};
618 next if ($c eq "__name");
619
Mauro Carvalho Chehab3a1cc062021-09-27 15:49:49 +0200620 if (!defined($$v{$c}{"__name"})) {
621 printf STDERR "Error: Couldn't find a non-empty name on a children of $file/.*: ";
622 print STDERR Dumper(%{$v});
623 exit;
624 }
625
Mauro Carvalho Chehabca8e0552021-09-18 11:52:17 +0200626 # Add new name
627 my $name = @{$$v{$c}{"__name"}}[0];
628 if ($name =~ s#^$file/#$link/#) {
629 push @{$$v{$c}{"__name"}}, $name;
630 }
631 # Add child to the queue and mark as seen
632 push @queue, $$v{$c};
633 $seen{$c}++;
634 }
635 }
636}
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200637
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200638my $escape_symbols = qr { ([\x01-\x08\x0e-\x1f\x21-\x29\x2b-\x2d\x3a-\x40\x7b-\xfe]) }x;
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200639sub parse_existing_sysfs {
640 my $file = $File::Find::name;
Mauro Carvalho Chehab0b87a1b2021-09-18 11:52:16 +0200641
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200642 my $mode = (lstat($file))[2];
643 my $abs_file = abs_path($file);
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200644
Mauro Carvalho Chehab87b58c62021-09-27 15:49:50 +0200645 my @tmp;
646 push @tmp, $file;
647 push @tmp, $abs_file if ($abs_file ne $file);
648
649 foreach my $f(@tmp) {
650 # Ignore cgroup, as this is big and has zero docs under ABI
651 return if ($f =~ m#^/sys/fs/cgroup/#);
652
653 # Ignore firmware as it is documented elsewhere
654 # Either ACPI or under Documentation/devicetree/bindings/
655 return if ($f =~ m#^/sys/firmware/#);
656
657 # Ignore some sysfs nodes that aren't actually part of ABI
658 return if ($f =~ m#/sections|notes/#);
659
660 # Would need to check at
661 # Documentation/admin-guide/kernel-parameters.txt, but this
662 # is not easily parseable.
663 return if ($f =~ m#/parameters/#);
664 }
665
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200666 if (S_ISLNK($mode)) {
667 $aliases{$file} = $abs_file;
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200668 return;
669 }
670
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200671 return if (S_ISDIR($mode));
672
673 # Trivial: file is defined exactly the same way at ABI What:
674 return if (defined($data{$file}));
675 return if (defined($data{$abs_file}));
676
Mauro Carvalho Chehabca8e0552021-09-18 11:52:17 +0200677 push @files, graph_add_file($abs_file, "file");
678}
679
680sub get_leave($)
681{
682 my $what = shift;
683 my $leave;
684
685 my $l = $what;
686 my $stop = 1;
687
688 $leave = $l;
689 $leave =~ s,/$,,;
690 $leave =~ s,.*/,,;
691 $leave =~ s/[\(\)]//g;
692
693 # $leave is used to improve search performance at
694 # check_undefined_symbols, as the algorithm there can seek
695 # for a small number of "what". It also allows giving a
696 # hint about a leave with the same name somewhere else.
697 # However, there are a few occurences where the leave is
698 # either a wildcard or a number. Just group such cases
699 # altogether.
Mauro Carvalho Chehab926358942021-09-23 17:41:15 +0200700 if ($leave =~ m/\.\*/ || $leave eq "" || $leave =~ /\\d/) {
Mauro Carvalho Chehabca8e0552021-09-18 11:52:17 +0200701 $leave = "others";
702 }
703
704 return $leave;
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200705}
706
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +0200707my @not_found;
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200708
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +0200709sub check_file($$)
710{
711 my $file_ref = shift;
712 my $names_ref = shift;
713 my @names = @{$names_ref};
714 my $file = $names[0];
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200715
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +0200716 my $found_string;
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200717
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +0200718 my $leave = get_leave($file);
719 if (!defined($leaf{$leave})) {
720 $leave = "others";
721 }
722 my @expr = @{$leaf{$leave}->{expr}};
723 die ("\rmissing rules for $leave") if (!defined($leaf{$leave}));
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200724
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +0200725 my $path = $file;
726 $path =~ s,(.*/).*,$1,;
Mauro Carvalho Chehab14c94252021-09-18 11:52:14 +0200727
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +0200728 if ($search_string) {
729 return if (!($file =~ m#$search_string#));
730 $found_string = 1;
731 }
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200732
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +0200733 for (my $i = 0; $i < @names; $i++) {
734 if ($found_string && $hint) {
735 if (!$i) {
736 print STDERR "--> $names[$i]\n";
Mauro Carvalho Chehabca8e0552021-09-18 11:52:17 +0200737 } else {
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +0200738 print STDERR " $names[$i]\n";
Mauro Carvalho Chehabca8e0552021-09-18 11:52:17 +0200739 }
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200740 }
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +0200741 foreach my $re (@expr) {
742 print STDERR "$names[$i] =~ /^$re\$/\n" if ($debug && $dbg_undefined);
743 if ($names[$i] =~ $re) {
744 return;
745 }
746 }
747 }
748
749 if ($leave ne "others") {
Mauro Carvalho Chehab4dcce5b2021-09-30 11:39:59 +0200750 my @expr = @{$leaf{"others"}->{expr}};
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +0200751 for (my $i = 0; $i < @names; $i++) {
752 foreach my $re (@expr) {
753 print STDERR "$names[$i] =~ /^$re\$/\n" if ($debug && $dbg_undefined);
754 if ($names[$i] =~ $re) {
755 return;
756 }
757 }
758 }
759 }
760
761 push @not_found, $file if (!$search_string || $found_string);
762
763 if ($hint && (!$search_string || $found_string)) {
764 my $what = $leaf{$leave}->{what};
765 $what =~ s/\xac/\n\t/g;
766 if ($leave ne "others") {
767 print STDERR "\r more likely regexes:\n\t$what\n";
768 } else {
769 print STDERR "\r tested regexes:\n\t$what\n";
770 }
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200771 }
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200772}
773
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +0200774sub check_undefined_symbols {
775 my $num_files = scalar @files;
776 my $next_i = 0;
777 my $start_time = times;
778
Mauro Carvalho Chehabe5c044c2021-09-28 23:51:32 +0200779 @files = sort @files;
780
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +0200781 my $last_time = $start_time;
782
783 # When either debug or hint is enabled, there's no sense showing
784 # progress, as the progress will be overriden.
785 if ($hint || ($debug && $dbg_undefined)) {
786 $next_i = $num_files;
787 }
788
789 my $is_console;
790 $is_console = 1 if (-t STDERR);
791
792 for (my $i = 0; $i < $num_files; $i++) {
793 my $file_ref = $files[$i];
794 my @names = @{$$file_ref{"__name"}};
795
796 check_file($file_ref, \@names);
797
798 my $cur_time = times;
799
800 if ($i == $next_i || $cur_time > $last_time + 1) {
801 my $percent = $i * 100 / $num_files;
802
803 my $tm = $cur_time - $start_time;
804 my $time = sprintf "%d:%02d", int($tm), 60 * ($tm - int($tm));
805
806 printf STDERR "\33[2K\r", if ($is_console);
807 printf STDERR "%s: processing sysfs files... %i%%: $names[0]", $time, $percent;
808 printf STDERR "\n", if (!$is_console);
809 STDERR->flush();
810
811 $next_i = int (($percent + 1) * $num_files / 100);
812 $last_time = $cur_time;
813 }
814 }
815
816 my $cur_time = times;
817 my $tm = $cur_time - $start_time;
818 my $time = sprintf "%d:%02d", int($tm), 60 * ($tm - int($tm));
819
820 printf STDERR "\33[2K\r", if ($is_console);
821 printf STDERR "%s: processing sysfs files... done\n", $time;
822
823 foreach my $file (@not_found) {
824 print "$file not found.\n";
825 }
826}
827
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200828sub undefined_symbols {
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +0200829 print STDERR "Reading $sysfs_prefix directory contents...";
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200830 find({
831 wanted =>\&parse_existing_sysfs,
832 preprocess =>\&dont_parse_special_attributes,
833 no_chdir => 1
834 }, $sysfs_prefix);
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +0200835 print STDERR "done.\n";
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200836
Mauro Carvalho Chehabf34f6722021-09-23 17:41:18 +0200837 $leaf{"others"}->{what} = "";
Mauro Carvalho Chehabca8e0552021-09-18 11:52:17 +0200838
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +0200839 print STDERR "Converting ABI What fields into regexes...";
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200840 foreach my $w (sort keys %data) {
Mauro Carvalho Chehabca8e0552021-09-18 11:52:17 +0200841 foreach my $what (split /\xac/,$w) {
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200842 next if (!($what =~ m/^$sysfs_prefix/));
843
844 # Convert what into regular expressions
845
Mauro Carvalho Chehabdf2205de2021-09-30 11:40:00 +0200846 # Escape dot characters
847 $what =~ s/\./\xf6/g;
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200848
849 # Temporarily change [0-9]+ type of patterns
850 $what =~ s/\[0\-9\]\+/\xff/g;
851
852 # Temporarily change [\d+-\d+] type of patterns
853 $what =~ s/\[0\-\d+\]/\xff/g;
854 $what =~ s/\[(\d+)\]/\xf4$1\xf5/g;
855
856 # Temporarily change [0-9] type of patterns
857 $what =~ s/\[(\d)\-(\d)\]/\xf4$1-$2\xf5/g;
858
859 # Handle multiple option patterns
860 $what =~ s/[\{\<\[]([\w_]+)(?:[,|]+([\w_]+)){1,}[\}\>\]]/($1|$2)/g;
861
862 # Handle wildcards
Mauro Carvalho Chehabdf2205de2021-09-30 11:40:00 +0200863 $what =~ s,\*,.*,g;
864 $what =~ s,/\xf6..,/.*,g;
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200865 $what =~ s/\<[^\>]+\>/.*/g;
866 $what =~ s/\{[^\}]+\}/.*/g;
867 $what =~ s/\[[^\]]+\]/.*/g;
868
869 $what =~ s/[XYZ]/.*/g;
870
871 # Recover [0-9] type of patterns
872 $what =~ s/\xf4/[/g;
873 $what =~ s/\xf5/]/g;
874
875 # Remove duplicated spaces
876 $what =~ s/\s+/ /g;
877
878 # Special case: this ABI has a parenthesis on it
879 $what =~ s/sqrt\(x^2\+y^2\+z^2\)/sqrt\(x^2\+y^2\+z^2\)/;
880
881 # Special case: drop comparition as in:
882 # What: foo = <something>
883 # (this happens on a few IIO definitions)
884 $what =~ s,\s*\=.*$,,;
885
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200886 # Escape all other symbols
887 $what =~ s/$escape_symbols/\\$1/g;
888 $what =~ s/\\\\/\\/g;
889 $what =~ s/\\([\[\]\(\)\|])/$1/g;
890 $what =~ s/(\d+)\\(-\d+)/$1$2/g;
891
Mauro Carvalho Chehab14c94252021-09-18 11:52:14 +0200892 $what =~ s/\xff/\\d+/g;
893
Mauro Carvalho Chehab14c94252021-09-18 11:52:14 +0200894 # Special case: IIO ABI which a parenthesis.
895 $what =~ s/sqrt(.*)/sqrt\(.*\)/;
896
Mauro Carvalho Chehabdf2205de2021-09-30 11:40:00 +0200897 # Simplify regexes with multiple .*
898 $what =~ s#(?:\.\*){2,}##g;
899# $what =~ s#\.\*/\.\*#.*#g;
900
901 # Recover dot characters
902 $what =~ s/\xf6/\./g;
903
Mauro Carvalho Chehab45495db2021-09-23 17:41:13 +0200904 my $leave = get_leave($what);
Mauro Carvalho Chehabf34f6722021-09-23 17:41:18 +0200905
Mauro Carvalho Chehab14c94252021-09-18 11:52:14 +0200906 my $added = 0;
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200907 foreach my $l (split /\|/, $leave) {
908 if (defined($leaf{$l})) {
Mauro Carvalho Chehabf34f6722021-09-23 17:41:18 +0200909 next if ($leaf{$l}->{what} =~ m/\b$what\b/);
910 $leaf{$l}->{what} .= "\xac" . $what;
Mauro Carvalho Chehab14c94252021-09-18 11:52:14 +0200911 $added = 1;
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200912 } else {
Mauro Carvalho Chehabf34f6722021-09-23 17:41:18 +0200913 $leaf{$l}->{what} = $what;
Mauro Carvalho Chehab14c94252021-09-18 11:52:14 +0200914 $added = 1;
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200915 }
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200916 }
Mauro Carvalho Chehab14c94252021-09-18 11:52:14 +0200917 if ($search_string && $added) {
Mauro Carvalho Chehab2833e30a2021-09-28 12:14:02 +0200918 print STDERR "What: $what\n" if ($what =~ m#$search_string#);
Mauro Carvalho Chehab14c94252021-09-18 11:52:14 +0200919 }
920
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200921 }
922 }
Mauro Carvalho Chehabf34f6722021-09-23 17:41:18 +0200923 # Compile regexes
Mauro Carvalho Chehabe5c044c2021-09-28 23:51:32 +0200924 foreach my $l (sort keys %leaf) {
Mauro Carvalho Chehabf34f6722021-09-23 17:41:18 +0200925 my @expr;
Mauro Carvalho Chehabe5c044c2021-09-28 23:51:32 +0200926 foreach my $w(sort split /\xac/, $leaf{$l}->{what}) {
Mauro Carvalho Chehabf34f6722021-09-23 17:41:18 +0200927 push @expr, qr /^$w$/;
928 }
929 $leaf{$l}->{expr} = \@expr;
930 }
931
Mauro Carvalho Chehabca8e0552021-09-18 11:52:17 +0200932 # Take links into account
Mauro Carvalho Chehabe5c044c2021-09-28 23:51:32 +0200933 foreach my $link (sort keys %aliases) {
Mauro Carvalho Chehabca8e0552021-09-18 11:52:17 +0200934 my $abs_file = $aliases{$link};
935 graph_add_link($abs_file, $link);
936 }
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +0200937 print STDERR "done.\n";
938
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200939 check_undefined_symbols;
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200940}
941
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +0100942# Ensure that the prefix will always end with a slash
943# While this is not needed for find, it makes the patch nicer
944# with --enable-lineno
945$prefix =~ s,/?$,/,;
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300946
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200947if ($cmd eq "undefined" || $cmd eq "search") {
948 $show_warnings = 0;
949}
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300950#
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300951# Parses all ABI files located at $prefix dir
952#
953find({wanted =>\&parse_abi, no_chdir => 1}, $prefix);
954
Mauro Carvalho Chehab46f661f2021-09-23 17:41:14 +0200955print STDERR Data::Dumper->Dump([\%data], [qw(*data)]) if ($debug & $dbg_dump_abi_structs);
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300956
957#
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300958# Handles the command
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300959#
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200960if ($cmd eq "undefined") {
961 undefined_symbols;
962} elsif ($cmd eq "search") {
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300963 search_symbols;
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100964} else {
965 if ($cmd eq "rest") {
966 output_rest;
967 }
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300968
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100969 # Warn about duplicated ABI entries
970 foreach my $what(sort keys %symbols) {
971 my @files = @{$symbols{$what}->{file}};
972
973 next if (scalar(@files) == 1);
974
975 printf STDERR "Warning: $what is defined %d times: @files\n",
976 scalar(@files);
977 }
978}
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300979
980__END__
981
982=head1 NAME
983
984abi_book.pl - parse the Linux ABI files and produce a ReST book.
985
986=head1 SYNOPSIS
987
Mauro Carvalho Chehab46f661f2021-09-23 17:41:14 +0200988B<abi_book.pl> [--debug <level>] [--enable-lineno] [--man] [--help]
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200989 [--(no-)rst-source] [--dir=<dir>] [--show-hints]
Mauro Carvalho Chehab14c94252021-09-18 11:52:14 +0200990 [--search-string <regex>]
Michal Simek5bff9632022-02-25 12:40:08 +0100991 <COMMAND> [<ARGUMENT>]
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300992
Mauro Carvalho Chehab42f09842021-09-27 15:49:51 +0200993Where B<COMMAND> can be:
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300994
995=over 8
996
Mauro Carvalho Chehab42f09842021-09-27 15:49:51 +0200997B<search> I<SEARCH_REGEX> - search for I<SEARCH_REGEX> inside ABI
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300998
Mauro Carvalho Chehab42f09842021-09-27 15:49:51 +0200999B<rest> - output the ABI in ReST markup language
Mauro Carvalho Chehab7ce7b892019-06-20 14:23:04 -03001000
Mauro Carvalho Chehab42f09842021-09-27 15:49:51 +02001001B<validate> - validate the ABI contents
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -03001002
Mauro Carvalho Chehab42f09842021-09-27 15:49:51 +02001003B<undefined> - existing symbols at the system that aren't
1004 defined at Documentation/ABI
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +02001005
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -03001006=back
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -03001007
1008=head1 OPTIONS
1009
1010=over 8
1011
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -03001012=item B<--dir>
1013
1014Changes the location of the ABI search. By default, it uses
1015the Documentation/ABI directory.
1016
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +01001017=item B<--rst-source> and B<--no-rst-source>
1018
1019The input file may be using ReST syntax or not. Those two options allow
Mauro Carvalho Chehab42f09842021-09-27 15:49:51 +02001020selecting between a rst-compliant source ABI (B<--rst-source>), or a
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +01001021plain text that may be violating ReST spec, so it requres some escaping
Mauro Carvalho Chehab42f09842021-09-27 15:49:51 +02001022logic (B<--no-rst-source>).
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +01001023
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +01001024=item B<--enable-lineno>
1025
Mauro Carvalho Chehab92b6de12022-03-26 11:27:23 +01001026Enable output of .. LINENO lines.
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +01001027
Mauro Carvalho Chehab46f661f2021-09-23 17:41:14 +02001028=item B<--debug> I<debug level>
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -03001029
Mauro Carvalho Chehab46f661f2021-09-23 17:41:14 +02001030Print debug information according with the level, which is given by the
1031following bitmask:
1032
1033 - 1: Debug parsing What entries from ABI files;
1034 - 2: Shows what files are opened from ABI files;
1035 - 4: Dump the structs used to store the contents of the ABI files.
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -03001036
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +02001037=item B<--show-hints>
1038
1039Show hints about possible definitions for the missing ABI symbols.
1040Used only when B<undefined>.
1041
Mauro Carvalho Chehab42f09842021-09-27 15:49:51 +02001042=item B<--search-string> I<regex string>
Mauro Carvalho Chehab14c94252021-09-18 11:52:14 +02001043
1044Show only occurences that match a search string.
1045Used only when B<undefined>.
1046
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -03001047=item B<--help>
1048
1049Prints a brief help message and exits.
1050
1051=item B<--man>
1052
1053Prints the manual page and exits.
1054
1055=back
1056
1057=head1 DESCRIPTION
1058
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -03001059Parse the Linux ABI files from ABI DIR (usually located at Documentation/ABI),
1060allowing to search for ABI symbols or to produce a ReST book containing
1061the Linux ABI documentation.
1062
1063=head1 EXAMPLES
1064
1065Search for all stable symbols with the word "usb":
1066
1067=over 8
1068
1069$ scripts/get_abi.pl search usb --dir Documentation/ABI/stable
1070
1071=back
1072
1073Search for all symbols that match the regex expression "usb.*cap":
1074
1075=over 8
1076
1077$ scripts/get_abi.pl search usb.*cap
1078
1079=back
1080
1081Output all obsoleted symbols in ReST format
1082
1083=over 8
1084
1085$ scripts/get_abi.pl rest --dir Documentation/ABI/obsolete
1086
1087=back
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -03001088
1089=head1 BUGS
1090
Mauro Carvalho Chehab42f09842021-09-27 15:49:51 +02001091Report bugs to Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -03001092
1093=head1 COPYRIGHT
1094
Mauro Carvalho Chehab42f09842021-09-27 15:49:51 +02001095Copyright (c) 2016-2021 by Mauro Carvalho Chehab <mchehab+huawei@kernel.org>.
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -03001096
1097License GPLv2: GNU GPL version 2 <http://gnu.org/licenses/gpl.html>.
1098
1099This is free software: you are free to change and redistribute it.
1100There is NO WARRANTY, to the extent permitted by law.
1101
1102=cut