blob: 6212f58b69c612d1b84ac3b1ce8ebec7d9d14de3 [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,);
95
96 my $name = $file;
97 $name =~ s,.*/,,;
98
Mauro Carvalho Chehaba4ea67b2020-10-30 08:40:27 +010099 my $fn = $file;
100 $fn =~ s,Documentation/ABI/,,;
101
102 my $nametag = "File $fn";
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300103 $data{$nametag}->{what} = "File $name";
104 $data{$nametag}->{type} = "File";
105 $data{$nametag}->{file} = $name;
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300106 $data{$nametag}->{filepath} = $file;
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300107 $data{$nametag}->{is_file} = 1;
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +0100108 $data{$nametag}->{line_no} = 1;
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300109
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300110 my $type = $file;
111 $type =~ s,.*/(.*)/.*,$1,;
112
113 my $what;
114 my $new_what;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100115 my $tag = "";
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300116 my $ln;
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -0300117 my $xrefs;
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300118 my $space;
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300119 my @labels;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100120 my $label = "";
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300121
Mauro Carvalho Chehab46f661f2021-09-23 17:41:14 +0200122 print STDERR "Opening $file\n" if ($debug & $dbg_what_open);
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300123 open IN, $file;
124 while(<IN>) {
125 $ln++;
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300126 if (m/^(\S+)(:\s*)(.*)/i) {
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300127 my $new_tag = lc($1);
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300128 my $sep = $2;
129 my $content = $3;
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300130
Mauro Carvalho Chehab7ce7b892019-06-20 14:23:04 -0300131 if (!($new_tag =~ m/(what|where|date|kernelversion|contact|description|users)/)) {
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300132 if ($tag eq "description") {
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300133 # New "tag" is actually part of
134 # description. Don't consider it a tag
135 $new_tag = "";
Mauro Carvalho Chehab7d7ea8d2019-06-20 14:23:01 -0300136 } elsif ($tag ne "") {
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300137 parse_error($file, $ln, "tag '$tag' is invalid", $_);
138 }
139 }
140
Mauro Carvalho Chehab2c0700e2019-06-20 14:23:03 -0300141 # Invalid, but it is a common mistake
142 if ($new_tag eq "where") {
Mauro Carvalho Chehab75442fb2020-10-30 08:40:45 +0100143 parse_error($file, $ln, "tag 'Where' is invalid. Should be 'What:' instead", "");
Mauro Carvalho Chehab2c0700e2019-06-20 14:23:03 -0300144 $new_tag = "what";
145 }
146
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300147 if ($new_tag =~ m/what/) {
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300148 $space = "";
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100149 $content =~ s/[,.;]$//;
150
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100151 push @{$symbols{$content}->{file}}, " $file:" . ($ln - 1);
152
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300153 if ($tag =~ m/what/) {
Mauro Carvalho Chehabab9c1482021-09-18 11:52:11 +0200154 $what .= "\xac" . $content;
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300155 } else {
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100156 if ($what) {
157 parse_error($file, $ln, "What '$what' doesn't have a description", "") if (!$data{$what}->{description});
158
Mauro Carvalho Chehabab9c1482021-09-18 11:52:11 +0200159 foreach my $w(split /\xac/, $what) {
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100160 $symbols{$w}->{xref} = $what;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100161 };
162 }
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300163
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300164 $what = $content;
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300165 $label = $content;
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300166 $new_what = 1;
167 }
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300168 push @labels, [($content, $label)];
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300169 $tag = $new_tag;
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -0300170
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100171 push @{$data{$nametag}->{symbols}}, $content if ($data{$nametag}->{what});
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300172 next;
173 }
174
Mauro Carvalho Chehab7d7ea8d2019-06-20 14:23:01 -0300175 if ($tag ne "" && $new_tag) {
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300176 $tag = $new_tag;
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300177
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300178 if ($new_what) {
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100179 @{$data{$what}->{label_list}} = @labels if ($data{$nametag}->{what});
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300180 @labels = ();
181 $label = "";
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300182 $new_what = 0;
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300183
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300184 $data{$what}->{type} = $type;
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100185 if (!defined($data{$what}->{file})) {
186 $data{$what}->{file} = $name;
187 $data{$what}->{filepath} = $file;
188 } else {
Mauro Carvalho Chehabff3777d2021-09-27 13:10:50 +0200189 $data{$what}->{description} .= "\n\n" if (defined($data{$what}->{description}));
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100190 if ($name ne $data{$what}->{file}) {
191 $data{$what}->{file} .= " " . $name;
192 $data{$what}->{filepath} .= " " . $file;
193 }
194 }
Mauro Carvalho Chehab46f661f2021-09-23 17:41:14 +0200195 print STDERR "\twhat: $what\n" if ($debug & $dbg_what_parsing);
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100196 $data{$what}->{line_no} = $ln;
197 } else {
198 $data{$what}->{line_no} = $ln if (!defined($data{$what}->{line_no}));
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300199 }
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300200
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300201 if (!$what) {
202 parse_error($file, $ln, "'What:' should come first:", $_);
203 next;
204 }
Mauro Carvalho Chehabf82a8a72020-10-30 08:40:23 +0100205 if ($new_tag eq "description") {
206 $sep =~ s,:, ,;
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +0100207 $content = ' ' x length($new_tag) . $sep . $content;
Mauro Carvalho Chehabf82a8a72020-10-30 08:40:23 +0100208 while ($content =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {}
209 if ($content =~ m/^(\s*)(\S.*)$/) {
210 # Preserve initial spaces for the first line
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +0100211 $space = $1;
Mauro Carvalho Chehabf82a8a72020-10-30 08:40:23 +0100212 $content = "$2\n";
213 $data{$what}->{$tag} .= $content;
214 } else {
215 undef($space);
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300216 }
Mauro Carvalho Chehabe9bca892020-10-30 08:40:21 +0100217
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300218 } else {
219 $data{$what}->{$tag} = $content;
220 }
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300221 next;
222 }
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300223 }
224
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300225 # Store any contents before tags at the database
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300226 if (!$tag && $data{$nametag}->{what}) {
227 $data{$nametag}->{description} .= $_;
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -0300228 next;
229 }
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300230
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300231 if ($tag eq "description") {
Mauro Carvalho Chehabe9bca892020-10-30 08:40:21 +0100232 my $content = $_;
233 while ($content =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {}
Mauro Carvalho Chehabf82a8a72020-10-30 08:40:23 +0100234 if (m/^\s*\n/) {
235 $data{$what}->{$tag} .= "\n";
236 next;
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300237 }
Mauro Carvalho Chehabf82a8a72020-10-30 08:40:23 +0100238
239 if (!defined($space)) {
240 # Preserve initial spaces for the first line
241 if ($content =~ m/^(\s*)(\S.*)$/) {
242 $space = $1;
243 $content = "$2\n";
244 }
245 } else {
246 $space = "" if (!($content =~ s/^($space)//));
247 }
248 $data{$what}->{$tag} .= $content;
249
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300250 next;
251 }
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300252 if (m/^\s*(.*)/) {
253 $data{$what}->{$tag} .= "\n$1";
254 $data{$what}->{$tag} =~ s/\n+$//;
255 next;
256 }
257
258 # Everything else is error
Mauro Carvalho Chehab75442fb2020-10-30 08:40:45 +0100259 parse_error($file, $ln, "Unexpected content", $_);
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300260 }
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100261 $data{$nametag}->{description} =~ s/^\n+// if ($data{$nametag}->{description});
262 if ($what) {
263 parse_error($file, $ln, "What '$what' doesn't have a description", "") if (!$data{$what}->{description});
264
Mauro Carvalho Chehabab9c1482021-09-18 11:52:11 +0200265 foreach my $w(split /\xac/,$what) {
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100266 $symbols{$w}->{xref} = $what;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100267 };
268 }
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300269 close IN;
270}
271
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100272sub create_labels {
273 my %labels;
274
275 foreach my $what (keys %data) {
276 next if ($data{$what}->{file} eq "File");
277
278 foreach my $p (@{$data{$what}->{label_list}}) {
279 my ($content, $label) = @{$p};
280 $label = "abi_" . $label . " ";
281 $label =~ tr/A-Z/a-z/;
282
283 # Convert special chars to "_"
284 $label =~s/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\xff])/_/g;
285 $label =~ s,_+,_,g;
286 $label =~ s,_$,,;
287
288 # Avoid duplicated labels
289 while (defined($labels{$label})) {
290 my @chars = ("A".."Z", "a".."z");
291 $label .= $chars[rand @chars];
292 }
293 $labels{$label} = 1;
294
295 $data{$what}->{label} = $label;
296
297 # only one label is enough
298 last;
299 }
300 }
301}
302
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300303#
304# Outputs the book on ReST format
305#
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300306
Mauro Carvalho Chehab50ebf8f2021-03-25 11:38:24 +0100307# \b doesn't work well with paths. So, we need to define something else:
308# Boundaries are punct characters, spaces and end-of-line
309my $start = qr {(^|\s|\() }x;
310my $bondary = qr { ([,.:;\)\s]|\z) }x;
Mauro Carvalho Chehab87ec9ea12021-03-25 11:38:25 +0100311my $xref_match = qr { $start(\/(sys|config|proc|dev|kvd)\/[^,.:;\)\s]+)$bondary }x;
Mauro Carvalho Chehabb0f95802021-03-25 11:38:22 +0100312my $symbols = qr { ([\x01-\x08\x0e-\x1f\x21-\x2f\x3a-\x40\x7b-\xff]) }x;
Mauro Carvalho Chehab55e54142020-10-30 08:40:29 +0100313
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300314sub output_rest {
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100315 create_labels();
316
Mauro Carvalho Chehab9d4fdda2020-11-02 11:32:16 +0100317 my $part = "";
318
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300319 foreach my $what (sort {
320 ($data{$a}->{type} eq "File") cmp ($data{$b}->{type} eq "File") ||
321 $a cmp $b
322 } keys %data) {
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300323 my $type = $data{$what}->{type};
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100324
325 my @file = split / /, $data{$what}->{file};
326 my @filepath = split / /, $data{$what}->{filepath};
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300327
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +0100328 if ($enable_lineno) {
329 printf "#define LINENO %s%s#%s\n\n",
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100330 $prefix, $file[0],
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +0100331 $data{$what}->{line_no};
332 }
333
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300334 my $w = $what;
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300335
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100336 if ($type ne "File") {
Mauro Carvalho Chehab9d4fdda2020-11-02 11:32:16 +0100337 my $cur_part = $what;
338 if ($what =~ '/') {
339 if ($what =~ m#^(\/?(?:[\w\-]+\/?){1,2})#) {
340 $cur_part = "Symbols under $1";
341 $cur_part =~ s,/$,,;
342 }
343 }
344
345 if ($cur_part ne "" && $part ne $cur_part) {
346 $part = $cur_part;
347 my $bar = $part;
348 $bar =~ s/./-/g;
349 print "$part\n$bar\n\n";
350 }
351
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100352 printf ".. _%s:\n\n", $data{$what}->{label};
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300353
Mauro Carvalho Chehabab9c1482021-09-18 11:52:11 +0200354 my @names = split /\xac/,$w;
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300355 my $len = 0;
356
357 foreach my $name (@names) {
Mauro Carvalho Chehabb0f95802021-03-25 11:38:22 +0100358 $name =~ s/$symbols/\\$1/g;
Mauro Carvalho Chehabc01d62d2020-10-30 08:40:28 +0100359 $name = "**$name**";
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300360 $len = length($name) if (length($name) > $len);
361 }
362
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300363 print "+-" . "-" x $len . "-+\n";
364 foreach my $name (@names) {
365 printf "| %s", $name . " " x ($len - length($name)) . " |\n";
366 print "+-" . "-" x $len . "-+\n";
367 }
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100368
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100369 print "\n";
370 }
371
372 for (my $i = 0; $i < scalar(@filepath); $i++) {
373 my $path = $filepath[$i];
374 my $f = $file[$i];
375
376 $path =~ s,.*/(.*/.*),$1,;;
377 $path =~ s,[/\-],_,g;;
378 my $fileref = "abi_file_".$path;
379
380 if ($type eq "File") {
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100381 print ".. _$fileref:\n\n";
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100382 } else {
383 print "Defined on file :ref:`$f <$fileref>`\n\n";
384 }
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300385 }
386
Mauro Carvalho Chehaba4ea67b2020-10-30 08:40:27 +0100387 if ($type eq "File") {
388 my $bar = $w;
389 $bar =~ s/./-/g;
390 print "$w\n$bar\n\n";
391 }
392
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100393 my $desc = "";
394 $desc = $data{$what}->{description} if (defined($data{$what}->{description}));
395 $desc =~ s/\s+$/\n/;
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300396
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300397 if (!($desc =~ /^\s*$/)) {
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +0100398 if ($description_is_rst) {
Mauro Carvalho Chehabdaaaf582020-11-02 11:32:15 +0100399 # Remove title markups from the description
400 # Having titles inside ABI files will only work if extra
401 # care would be taken in order to strictly follow the same
402 # level order for each markup.
403 $desc =~ s/\n[\-\*\=\^\~]+\n/\n\n/g;
404
Mauro Carvalho Chehab55e54142020-10-30 08:40:29 +0100405 # Enrich text by creating cross-references
406
Mauro Carvalho Chehabc27c2e32021-03-25 11:38:26 +0100407 my $new_desc = "";
Mauro Carvalho Chehab2ae7bb52021-03-25 11:38:27 +0100408 my $init_indent = -1;
409 my $literal_indent = -1;
410
Mauro Carvalho Chehabc27c2e32021-03-25 11:38:26 +0100411 open(my $fh, "+<", \$desc);
412 while (my $d = <$fh>) {
Mauro Carvalho Chehab2ae7bb52021-03-25 11:38:27 +0100413 my $indent = $d =~ m/^(\s+)/;
414 my $spaces = length($indent);
415 $init_indent = $indent if ($init_indent < 0);
416 if ($literal_indent >= 0) {
417 if ($spaces > $literal_indent) {
418 $new_desc .= $d;
419 next;
420 } else {
421 $literal_indent = -1;
422 }
423 } else {
424 if ($d =~ /()::$/ && !($d =~ /^\s*\.\./)) {
425 $literal_indent = $spaces;
426 }
427 }
428
Mauro Carvalho Chehabc27c2e32021-03-25 11:38:26 +0100429 $d =~ s,Documentation/(?!devicetree)(\S+)\.rst,:doc:`/$1`,g;
Mauro Carvalho Chehab55e54142020-10-30 08:40:29 +0100430
Mauro Carvalho Chehabc27c2e32021-03-25 11:38:26 +0100431 my @matches = $d =~ m,Documentation/ABI/([\w\/\-]+),g;
432 foreach my $f (@matches) {
433 my $xref = $f;
434 my $path = $f;
435 $path =~ s,.*/(.*/.*),$1,;;
436 $path =~ s,[/\-],_,g;;
437 $xref .= " <abi_file_" . $path . ">";
438 $d =~ s,\bDocumentation/ABI/$f\b,:ref:`$xref`,g;
Mauro Carvalho Chehab55e54142020-10-30 08:40:29 +0100439 }
Mauro Carvalho Chehab55e54142020-10-30 08:40:29 +0100440
Mauro Carvalho Chehabc27c2e32021-03-25 11:38:26 +0100441 # Seek for cross reference symbols like /sys/...
442 @matches = $d =~ m/$xref_match/g;
443
444 foreach my $s (@matches) {
445 next if (!($s =~ m,/,));
446 if (defined($data{$s}) && defined($data{$s}->{label})) {
447 my $xref = $s;
448
449 $xref =~ s/$symbols/\\$1/g;
450 $xref = ":ref:`$xref <" . $data{$s}->{label} . ">`";
451
452 $d =~ s,$start$s$bondary,$1$xref$2,g;
453 }
454 }
455 $new_desc .= $d;
456 }
457 close $fh;
458
459
460 print "$new_desc\n\n";
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +0100461 } else {
462 $desc =~ s/^\s+//;
463
464 # Remove title markups from the description, as they won't work
465 $desc =~ s/\n[\-\*\=\^\~]+\n/\n\n/g;
466
467 if ($desc =~ m/\:\n/ || $desc =~ m/\n[\t ]+/ || $desc =~ m/[\x00-\x08\x0b-\x1f\x7b-\xff]/) {
468 # put everything inside a code block
469 $desc =~ s/\n/\n /g;
470
471 print "::\n\n";
472 print " $desc\n\n";
473 } else {
474 # Escape any special chars from description
475 $desc =~s/([\x00-\x08\x0b-\x1f\x21-\x2a\x2d\x2f\x3c-\x40\x5c\x5e-\x60\x7b-\xff])/\\$1/g;
476 print "$desc\n\n";
477 }
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300478 }
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300479 } else {
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300480 print "DESCRIPTION MISSING for $what\n\n" if (!$data{$what}->{is_file});
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300481 }
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -0300482
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100483 if ($data{$what}->{symbols}) {
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300484 printf "Has the following ABI:\n\n";
485
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100486 foreach my $content(@{$data{$what}->{symbols}}) {
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100487 my $label = $data{$symbols{$content}->{xref}}->{label};
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300488
489 # Escape special chars from content
490 $content =~s/([\x00-\x1f\x21-\x2f\x3a-\x40\x7b-\xff])/\\$1/g;
491
492 print "- :ref:`$content <$label>`\n\n";
493 }
494 }
Mauro Carvalho Chehaba16ab142020-10-30 08:40:26 +0100495
496 if (defined($data{$what}->{users})) {
497 my $users = $data{$what}->{users};
498
499 $users =~ s/\n/\n\t/g;
500 printf "Users:\n\t%s\n\n", $users if ($users ne "");
501 }
502
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300503 }
504}
505
506#
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300507# Searches for ABI symbols
508#
509sub search_symbols {
510 foreach my $what (sort keys %data) {
511 next if (!($what =~ m/($arg)/));
512
513 my $type = $data{$what}->{type};
514 next if ($type eq "File");
515
516 my $file = $data{$what}->{filepath};
517
Mauro Carvalho Chehabe27c42a2021-09-23 17:41:12 +0200518 $what =~ s/\xac/, /g;
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300519 my $bar = $what;
520 $bar =~ s/./-/g;
521
522 print "\n$what\n$bar\n\n";
523
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100524 my $kernelversion = $data{$what}->{kernelversion} if (defined($data{$what}->{kernelversion}));
525 my $contact = $data{$what}->{contact} if (defined($data{$what}->{contact}));
526 my $users = $data{$what}->{users} if (defined($data{$what}->{users}));
527 my $date = $data{$what}->{date} if (defined($data{$what}->{date}));
528 my $desc = $data{$what}->{description} if (defined($data{$what}->{description}));
529
530 $kernelversion =~ s/^\s+// if ($kernelversion);
531 $contact =~ s/^\s+// if ($contact);
532 if ($users) {
533 $users =~ s/^\s+//;
534 $users =~ s/\n//g;
535 }
536 $date =~ s/^\s+// if ($date);
537 $desc =~ s/^\s+// if ($desc);
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300538
539 printf "Kernel version:\t\t%s\n", $kernelversion if ($kernelversion);
540 printf "Date:\t\t\t%s\n", $date if ($date);
541 printf "Contact:\t\t%s\n", $contact if ($contact);
542 printf "Users:\t\t\t%s\n", $users if ($users);
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100543 print "Defined on file(s):\t$file\n\n";
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300544 print "Description:\n\n$desc";
545 }
546}
547
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200548# Exclude /sys/kernel/debug and /sys/kernel/tracing from the search path
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200549sub dont_parse_special_attributes {
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200550 if (($File::Find::dir =~ m,^/sys/kernel,)) {
551 return grep {!/(debug|tracing)/ } @_;
552 }
553
554 if (($File::Find::dir =~ m,^/sys/fs,)) {
555 return grep {!/(pstore|bpf|fuse)/ } @_;
556 }
557
558 return @_
559}
560
561my %leaf;
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200562my %aliases;
563my @files;
Mauro Carvalho Chehabca8e0552021-09-18 11:52:17 +0200564my %root;
565
566sub graph_add_file {
567 my $file = shift;
568 my $type = shift;
569
570 my $dir = $file;
571 $dir =~ s,^(.*/).*,$1,;
572 $file =~ s,.*/,,;
573
574 my $name;
575 my $file_ref = \%root;
576 foreach my $edge(split "/", $dir) {
577 $name .= "$edge/";
578 if (!defined ${$file_ref}{$edge}) {
579 ${$file_ref}{$edge} = { };
580 }
581 $file_ref = \%{$$file_ref{$edge}};
582 ${$file_ref}{"__name"} = [ $name ];
583 }
584 $name .= "$file";
585 ${$file_ref}{$file} = {
586 "__name" => [ $name ]
587 };
588
589 return \%{$$file_ref{$file}};
590}
591
592sub graph_add_link {
593 my $file = shift;
594 my $link = shift;
595
596 # Traverse graph to find the reference
597 my $file_ref = \%root;
598 foreach my $edge(split "/", $file) {
599 $file_ref = \%{$$file_ref{$edge}} || die "Missing node!";
600 }
601
602 # do a BFS
603
604 my @queue;
605 my %seen;
Mauro Carvalho Chehabca8e0552021-09-18 11:52:17 +0200606 my $st;
607
608 push @queue, $file_ref;
609 $seen{$start}++;
610
611 while (@queue) {
612 my $v = shift @queue;
613 my @child = keys(%{$v});
614
615 foreach my $c(@child) {
616 next if $seen{$$v{$c}};
617 next if ($c eq "__name");
618
Mauro Carvalho Chehab3a1cc062021-09-27 15:49:49 +0200619 if (!defined($$v{$c}{"__name"})) {
620 printf STDERR "Error: Couldn't find a non-empty name on a children of $file/.*: ";
621 print STDERR Dumper(%{$v});
622 exit;
623 }
624
Mauro Carvalho Chehabca8e0552021-09-18 11:52:17 +0200625 # Add new name
626 my $name = @{$$v{$c}{"__name"}}[0];
627 if ($name =~ s#^$file/#$link/#) {
628 push @{$$v{$c}{"__name"}}, $name;
629 }
630 # Add child to the queue and mark as seen
631 push @queue, $$v{$c};
632 $seen{$c}++;
633 }
634 }
635}
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200636
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200637my $escape_symbols = qr { ([\x01-\x08\x0e-\x1f\x21-\x29\x2b-\x2d\x3a-\x40\x7b-\xfe]) }x;
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200638sub parse_existing_sysfs {
639 my $file = $File::Find::name;
Mauro Carvalho Chehab0b87a1b2021-09-18 11:52:16 +0200640
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200641 my $mode = (lstat($file))[2];
642 my $abs_file = abs_path($file);
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200643
Mauro Carvalho Chehab87b58c62021-09-27 15:49:50 +0200644 my @tmp;
645 push @tmp, $file;
646 push @tmp, $abs_file if ($abs_file ne $file);
647
648 foreach my $f(@tmp) {
649 # Ignore cgroup, as this is big and has zero docs under ABI
650 return if ($f =~ m#^/sys/fs/cgroup/#);
651
652 # Ignore firmware as it is documented elsewhere
653 # Either ACPI or under Documentation/devicetree/bindings/
654 return if ($f =~ m#^/sys/firmware/#);
655
656 # Ignore some sysfs nodes that aren't actually part of ABI
657 return if ($f =~ m#/sections|notes/#);
658
659 # Would need to check at
660 # Documentation/admin-guide/kernel-parameters.txt, but this
661 # is not easily parseable.
662 return if ($f =~ m#/parameters/#);
663 }
664
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200665 if (S_ISLNK($mode)) {
666 $aliases{$file} = $abs_file;
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200667 return;
668 }
669
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200670 return if (S_ISDIR($mode));
671
672 # Trivial: file is defined exactly the same way at ABI What:
673 return if (defined($data{$file}));
674 return if (defined($data{$abs_file}));
675
Mauro Carvalho Chehabca8e0552021-09-18 11:52:17 +0200676 push @files, graph_add_file($abs_file, "file");
677}
678
679sub get_leave($)
680{
681 my $what = shift;
682 my $leave;
683
684 my $l = $what;
685 my $stop = 1;
686
687 $leave = $l;
688 $leave =~ s,/$,,;
689 $leave =~ s,.*/,,;
690 $leave =~ s/[\(\)]//g;
691
692 # $leave is used to improve search performance at
693 # check_undefined_symbols, as the algorithm there can seek
694 # for a small number of "what". It also allows giving a
695 # hint about a leave with the same name somewhere else.
696 # However, there are a few occurences where the leave is
697 # either a wildcard or a number. Just group such cases
698 # altogether.
Mauro Carvalho Chehab926358942021-09-23 17:41:15 +0200699 if ($leave =~ m/\.\*/ || $leave eq "" || $leave =~ /\\d/) {
Mauro Carvalho Chehabca8e0552021-09-18 11:52:17 +0200700 $leave = "others";
701 }
702
703 return $leave;
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200704}
705
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +0200706my @not_found;
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200707
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +0200708sub check_file($$)
709{
710 my $file_ref = shift;
711 my $names_ref = shift;
712 my @names = @{$names_ref};
713 my $file = $names[0];
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200714
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +0200715 my $found_string;
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200716
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +0200717 my $leave = get_leave($file);
718 if (!defined($leaf{$leave})) {
719 $leave = "others";
720 }
721 my @expr = @{$leaf{$leave}->{expr}};
722 die ("\rmissing rules for $leave") if (!defined($leaf{$leave}));
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200723
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +0200724 my $path = $file;
725 $path =~ s,(.*/).*,$1,;
Mauro Carvalho Chehab14c94252021-09-18 11:52:14 +0200726
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +0200727 if ($search_string) {
728 return if (!($file =~ m#$search_string#));
729 $found_string = 1;
730 }
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200731
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +0200732 for (my $i = 0; $i < @names; $i++) {
733 if ($found_string && $hint) {
734 if (!$i) {
735 print STDERR "--> $names[$i]\n";
Mauro Carvalho Chehabca8e0552021-09-18 11:52:17 +0200736 } else {
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +0200737 print STDERR " $names[$i]\n";
Mauro Carvalho Chehabca8e0552021-09-18 11:52:17 +0200738 }
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200739 }
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +0200740 foreach my $re (@expr) {
741 print STDERR "$names[$i] =~ /^$re\$/\n" if ($debug && $dbg_undefined);
742 if ($names[$i] =~ $re) {
743 return;
744 }
745 }
746 }
747
748 if ($leave ne "others") {
Mauro Carvalho Chehab4dcce5b2021-09-30 11:39:59 +0200749 my @expr = @{$leaf{"others"}->{expr}};
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +0200750 for (my $i = 0; $i < @names; $i++) {
751 foreach my $re (@expr) {
752 print STDERR "$names[$i] =~ /^$re\$/\n" if ($debug && $dbg_undefined);
753 if ($names[$i] =~ $re) {
754 return;
755 }
756 }
757 }
758 }
759
760 push @not_found, $file if (!$search_string || $found_string);
761
762 if ($hint && (!$search_string || $found_string)) {
763 my $what = $leaf{$leave}->{what};
764 $what =~ s/\xac/\n\t/g;
765 if ($leave ne "others") {
766 print STDERR "\r more likely regexes:\n\t$what\n";
767 } else {
768 print STDERR "\r tested regexes:\n\t$what\n";
769 }
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200770 }
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200771}
772
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +0200773sub check_undefined_symbols {
774 my $num_files = scalar @files;
775 my $next_i = 0;
776 my $start_time = times;
777
Mauro Carvalho Chehabe5c044c2021-09-28 23:51:32 +0200778 @files = sort @files;
779
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +0200780 my $last_time = $start_time;
781
782 # When either debug or hint is enabled, there's no sense showing
783 # progress, as the progress will be overriden.
784 if ($hint || ($debug && $dbg_undefined)) {
785 $next_i = $num_files;
786 }
787
788 my $is_console;
789 $is_console = 1 if (-t STDERR);
790
791 for (my $i = 0; $i < $num_files; $i++) {
792 my $file_ref = $files[$i];
793 my @names = @{$$file_ref{"__name"}};
794
795 check_file($file_ref, \@names);
796
797 my $cur_time = times;
798
799 if ($i == $next_i || $cur_time > $last_time + 1) {
800 my $percent = $i * 100 / $num_files;
801
802 my $tm = $cur_time - $start_time;
803 my $time = sprintf "%d:%02d", int($tm), 60 * ($tm - int($tm));
804
805 printf STDERR "\33[2K\r", if ($is_console);
806 printf STDERR "%s: processing sysfs files... %i%%: $names[0]", $time, $percent;
807 printf STDERR "\n", if (!$is_console);
808 STDERR->flush();
809
810 $next_i = int (($percent + 1) * $num_files / 100);
811 $last_time = $cur_time;
812 }
813 }
814
815 my $cur_time = times;
816 my $tm = $cur_time - $start_time;
817 my $time = sprintf "%d:%02d", int($tm), 60 * ($tm - int($tm));
818
819 printf STDERR "\33[2K\r", if ($is_console);
820 printf STDERR "%s: processing sysfs files... done\n", $time;
821
822 foreach my $file (@not_found) {
823 print "$file not found.\n";
824 }
825}
826
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200827sub undefined_symbols {
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +0200828 print STDERR "Reading $sysfs_prefix directory contents...";
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200829 find({
830 wanted =>\&parse_existing_sysfs,
831 preprocess =>\&dont_parse_special_attributes,
832 no_chdir => 1
833 }, $sysfs_prefix);
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +0200834 print STDERR "done.\n";
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200835
Mauro Carvalho Chehabf34f6722021-09-23 17:41:18 +0200836 $leaf{"others"}->{what} = "";
Mauro Carvalho Chehabca8e0552021-09-18 11:52:17 +0200837
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +0200838 print STDERR "Converting ABI What fields into regexes...";
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200839 foreach my $w (sort keys %data) {
Mauro Carvalho Chehabca8e0552021-09-18 11:52:17 +0200840 foreach my $what (split /\xac/,$w) {
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200841 next if (!($what =~ m/^$sysfs_prefix/));
842
843 # Convert what into regular expressions
844
Mauro Carvalho Chehabdf2205de2021-09-30 11:40:00 +0200845 # Escape dot characters
846 $what =~ s/\./\xf6/g;
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200847
848 # Temporarily change [0-9]+ type of patterns
849 $what =~ s/\[0\-9\]\+/\xff/g;
850
851 # Temporarily change [\d+-\d+] type of patterns
852 $what =~ s/\[0\-\d+\]/\xff/g;
853 $what =~ s/\[(\d+)\]/\xf4$1\xf5/g;
854
855 # Temporarily change [0-9] type of patterns
856 $what =~ s/\[(\d)\-(\d)\]/\xf4$1-$2\xf5/g;
857
858 # Handle multiple option patterns
859 $what =~ s/[\{\<\[]([\w_]+)(?:[,|]+([\w_]+)){1,}[\}\>\]]/($1|$2)/g;
860
861 # Handle wildcards
Mauro Carvalho Chehabdf2205de2021-09-30 11:40:00 +0200862 $what =~ s,\*,.*,g;
863 $what =~ s,/\xf6..,/.*,g;
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200864 $what =~ s/\<[^\>]+\>/.*/g;
865 $what =~ s/\{[^\}]+\}/.*/g;
866 $what =~ s/\[[^\]]+\]/.*/g;
867
868 $what =~ s/[XYZ]/.*/g;
869
870 # Recover [0-9] type of patterns
871 $what =~ s/\xf4/[/g;
872 $what =~ s/\xf5/]/g;
873
874 # Remove duplicated spaces
875 $what =~ s/\s+/ /g;
876
877 # Special case: this ABI has a parenthesis on it
878 $what =~ s/sqrt\(x^2\+y^2\+z^2\)/sqrt\(x^2\+y^2\+z^2\)/;
879
880 # Special case: drop comparition as in:
881 # What: foo = <something>
882 # (this happens on a few IIO definitions)
883 $what =~ s,\s*\=.*$,,;
884
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200885 # Escape all other symbols
886 $what =~ s/$escape_symbols/\\$1/g;
887 $what =~ s/\\\\/\\/g;
888 $what =~ s/\\([\[\]\(\)\|])/$1/g;
889 $what =~ s/(\d+)\\(-\d+)/$1$2/g;
890
Mauro Carvalho Chehab14c94252021-09-18 11:52:14 +0200891 $what =~ s/\xff/\\d+/g;
892
Mauro Carvalho Chehab14c94252021-09-18 11:52:14 +0200893 # Special case: IIO ABI which a parenthesis.
894 $what =~ s/sqrt(.*)/sqrt\(.*\)/;
895
Mauro Carvalho Chehabdf2205de2021-09-30 11:40:00 +0200896 # Simplify regexes with multiple .*
897 $what =~ s#(?:\.\*){2,}##g;
898# $what =~ s#\.\*/\.\*#.*#g;
899
900 # Recover dot characters
901 $what =~ s/\xf6/\./g;
902
Mauro Carvalho Chehab45495db2021-09-23 17:41:13 +0200903 my $leave = get_leave($what);
Mauro Carvalho Chehabf34f6722021-09-23 17:41:18 +0200904
Mauro Carvalho Chehab14c94252021-09-18 11:52:14 +0200905 my $added = 0;
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200906 foreach my $l (split /\|/, $leave) {
907 if (defined($leaf{$l})) {
Mauro Carvalho Chehabf34f6722021-09-23 17:41:18 +0200908 next if ($leaf{$l}->{what} =~ m/\b$what\b/);
909 $leaf{$l}->{what} .= "\xac" . $what;
Mauro Carvalho Chehab14c94252021-09-18 11:52:14 +0200910 $added = 1;
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200911 } else {
Mauro Carvalho Chehabf34f6722021-09-23 17:41:18 +0200912 $leaf{$l}->{what} = $what;
Mauro Carvalho Chehab14c94252021-09-18 11:52:14 +0200913 $added = 1;
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200914 }
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200915 }
Mauro Carvalho Chehab14c94252021-09-18 11:52:14 +0200916 if ($search_string && $added) {
Mauro Carvalho Chehab2833e30a2021-09-28 12:14:02 +0200917 print STDERR "What: $what\n" if ($what =~ m#$search_string#);
Mauro Carvalho Chehab14c94252021-09-18 11:52:14 +0200918 }
919
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200920 }
921 }
Mauro Carvalho Chehabf34f6722021-09-23 17:41:18 +0200922 # Compile regexes
Mauro Carvalho Chehabe5c044c2021-09-28 23:51:32 +0200923 foreach my $l (sort keys %leaf) {
Mauro Carvalho Chehabf34f6722021-09-23 17:41:18 +0200924 my @expr;
Mauro Carvalho Chehabe5c044c2021-09-28 23:51:32 +0200925 foreach my $w(sort split /\xac/, $leaf{$l}->{what}) {
Mauro Carvalho Chehabf34f6722021-09-23 17:41:18 +0200926 push @expr, qr /^$w$/;
927 }
928 $leaf{$l}->{expr} = \@expr;
929 }
930
Mauro Carvalho Chehabca8e0552021-09-18 11:52:17 +0200931 # Take links into account
Mauro Carvalho Chehabe5c044c2021-09-28 23:51:32 +0200932 foreach my $link (sort keys %aliases) {
Mauro Carvalho Chehabca8e0552021-09-18 11:52:17 +0200933 my $abs_file = $aliases{$link};
934 graph_add_link($abs_file, $link);
935 }
Mauro Carvalho Chehab28331a02021-09-28 12:14:03 +0200936 print STDERR "done.\n";
937
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200938 check_undefined_symbols;
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200939}
940
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +0100941# Ensure that the prefix will always end with a slash
942# While this is not needed for find, it makes the patch nicer
943# with --enable-lineno
944$prefix =~ s,/?$,/,;
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300945
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200946if ($cmd eq "undefined" || $cmd eq "search") {
947 $show_warnings = 0;
948}
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300949#
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300950# Parses all ABI files located at $prefix dir
951#
952find({wanted =>\&parse_abi, no_chdir => 1}, $prefix);
953
Mauro Carvalho Chehab46f661f2021-09-23 17:41:14 +0200954print STDERR Data::Dumper->Dump([\%data], [qw(*data)]) if ($debug & $dbg_dump_abi_structs);
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300955
956#
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300957# Handles the command
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300958#
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200959if ($cmd eq "undefined") {
960 undefined_symbols;
961} elsif ($cmd eq "search") {
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300962 search_symbols;
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100963} else {
964 if ($cmd eq "rest") {
965 output_rest;
966 }
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300967
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100968 # Warn about duplicated ABI entries
969 foreach my $what(sort keys %symbols) {
970 my @files = @{$symbols{$what}->{file}};
971
972 next if (scalar(@files) == 1);
973
974 printf STDERR "Warning: $what is defined %d times: @files\n",
975 scalar(@files);
976 }
977}
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -0300978
979__END__
980
981=head1 NAME
982
983abi_book.pl - parse the Linux ABI files and produce a ReST book.
984
985=head1 SYNOPSIS
986
Mauro Carvalho Chehab46f661f2021-09-23 17:41:14 +0200987B<abi_book.pl> [--debug <level>] [--enable-lineno] [--man] [--help]
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200988 [--(no-)rst-source] [--dir=<dir>] [--show-hints]
Mauro Carvalho Chehab14c94252021-09-18 11:52:14 +0200989 [--search-string <regex>]
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200990 <COMAND> [<ARGUMENT>]
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300991
Mauro Carvalho Chehab42f09842021-09-27 15:49:51 +0200992Where B<COMMAND> can be:
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300993
994=over 8
995
Mauro Carvalho Chehab42f09842021-09-27 15:49:51 +0200996B<search> I<SEARCH_REGEX> - search for I<SEARCH_REGEX> inside ABI
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300997
Mauro Carvalho Chehab42f09842021-09-27 15:49:51 +0200998B<rest> - output the ABI in ReST markup language
Mauro Carvalho Chehab7ce7b892019-06-20 14:23:04 -0300999
Mauro Carvalho Chehab42f09842021-09-27 15:49:51 +02001000B<validate> - validate the ABI contents
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -03001001
Mauro Carvalho Chehab42f09842021-09-27 15:49:51 +02001002B<undefined> - existing symbols at the system that aren't
1003 defined at Documentation/ABI
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +02001004
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -03001005=back
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -03001006
1007=head1 OPTIONS
1008
1009=over 8
1010
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -03001011=item B<--dir>
1012
1013Changes the location of the ABI search. By default, it uses
1014the Documentation/ABI directory.
1015
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +01001016=item B<--rst-source> and B<--no-rst-source>
1017
1018The input file may be using ReST syntax or not. Those two options allow
Mauro Carvalho Chehab42f09842021-09-27 15:49:51 +02001019selecting between a rst-compliant source ABI (B<--rst-source>), or a
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +01001020plain text that may be violating ReST spec, so it requres some escaping
Mauro Carvalho Chehab42f09842021-09-27 15:49:51 +02001021logic (B<--no-rst-source>).
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +01001022
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +01001023=item B<--enable-lineno>
1024
1025Enable output of #define LINENO lines.
1026
Mauro Carvalho Chehab46f661f2021-09-23 17:41:14 +02001027=item B<--debug> I<debug level>
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -03001028
Mauro Carvalho Chehab46f661f2021-09-23 17:41:14 +02001029Print debug information according with the level, which is given by the
1030following bitmask:
1031
1032 - 1: Debug parsing What entries from ABI files;
1033 - 2: Shows what files are opened from ABI files;
1034 - 4: Dump the structs used to store the contents of the ABI files.
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -03001035
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +02001036=item B<--show-hints>
1037
1038Show hints about possible definitions for the missing ABI symbols.
1039Used only when B<undefined>.
1040
Mauro Carvalho Chehab42f09842021-09-27 15:49:51 +02001041=item B<--search-string> I<regex string>
Mauro Carvalho Chehab14c94252021-09-18 11:52:14 +02001042
1043Show only occurences that match a search string.
1044Used only when B<undefined>.
1045
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -03001046=item B<--help>
1047
1048Prints a brief help message and exits.
1049
1050=item B<--man>
1051
1052Prints the manual page and exits.
1053
1054=back
1055
1056=head1 DESCRIPTION
1057
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -03001058Parse the Linux ABI files from ABI DIR (usually located at Documentation/ABI),
1059allowing to search for ABI symbols or to produce a ReST book containing
1060the Linux ABI documentation.
1061
1062=head1 EXAMPLES
1063
1064Search for all stable symbols with the word "usb":
1065
1066=over 8
1067
1068$ scripts/get_abi.pl search usb --dir Documentation/ABI/stable
1069
1070=back
1071
1072Search for all symbols that match the regex expression "usb.*cap":
1073
1074=over 8
1075
1076$ scripts/get_abi.pl search usb.*cap
1077
1078=back
1079
1080Output all obsoleted symbols in ReST format
1081
1082=over 8
1083
1084$ scripts/get_abi.pl rest --dir Documentation/ABI/obsolete
1085
1086=back
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -03001087
1088=head1 BUGS
1089
Mauro Carvalho Chehab42f09842021-09-27 15:49:51 +02001090Report bugs to Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -03001091
1092=head1 COPYRIGHT
1093
Mauro Carvalho Chehab42f09842021-09-27 15:49:51 +02001094Copyright (c) 2016-2021 by Mauro Carvalho Chehab <mchehab+huawei@kernel.org>.
Mauro Carvalho Chehabbbc249f22019-06-20 14:22:55 -03001095
1096License GPLv2: GNU GPL version 2 <http://gnu.org/licenses/gpl.html>.
1097
1098This is free software: you are free to change and redistribute it.
1099There is NO WARRANTY, to the extent permitted by law.
1100
1101=cut