ABI: sysfs-platform-intel-pmc: add blank lines to make it valid for ReST
[linux-block.git] / scripts / get_abi.pl
CommitLineData
c25ce589 1#!/usr/bin/env perl
ecb351f1 2# SPDX-License-Identifier: GPL-2.0
bbc249f2
MCC
3
4use strict;
234948bf 5use warnings;
55e5414f 6use utf8;
bbc249f2
MCC
7use Pod::Usage;
8use Getopt::Long;
9use File::Find;
10use Fcntl ':mode';
ab02c515 11use Cwd 'abs_path';
46f661fd 12use Data::Dumper;
bbc249f2 13
234948bf 14my $help = 0;
ab02c515 15my $hint = 0;
234948bf
MCC
16my $man = 0;
17my $debug = 0;
18my $enable_lineno = 0;
f090db43 19my $show_warnings = 1;
33e3e991 20my $prefix="Documentation/ABI";
f090db43 21my $sysfs_prefix="/sys";
14c94257 22my $search_string;
bbc249f2 23
46f661fd
MCC
24# Debug options
25my $dbg_what_parsing = 1;
26my $dbg_what_open = 2;
27my $dbg_dump_abi_structs = 4;
f34f6729 28my $dbg_undefined = 8;
46f661fd 29
11ce90a4
MCC
30#
31# If true, assumes that the description is formatted with ReST
32#
2fcce37a 33my $description_is_rst = 1;
11ce90a4 34
bbc249f2 35GetOptions(
46f661fd 36 "debug=i" => \$debug,
61439c4a 37 "enable-lineno" => \$enable_lineno,
11ce90a4 38 "rst-source!" => \$description_is_rst,
33e3e991 39 "dir=s" => \$prefix,
bbc249f2 40 'help|?' => \$help,
ab02c515 41 "show-hints" => \$hint,
14c94257 42 "search-string=s" => \$search_string,
bbc249f2
MCC
43 man => \$man
44) or pod2usage(2);
45
46pod2usage(1) if $help;
47pod2usage(-exitstatus => 0, -verbose => 2) if $man;
48
33e3e991 49pod2usage(2) if (scalar @ARGV < 1 || @ARGV > 2);
bbc249f2 50
33e3e991
MCC
51my ($cmd, $arg) = @ARGV;
52
f090db43 53pod2usage(2) if ($cmd ne "search" && $cmd ne "rest" && $cmd ne "validate" && $cmd ne "undefined");
33e3e991 54pod2usage(2) if ($cmd eq "search" && !$arg);
bbc249f2 55
46f661fd 56require Data::Dumper if ($debug & $dbg_dump_abi_structs);
bbc249f2
MCC
57
58my %data;
234948bf 59my %symbols;
bbc249f2
MCC
60
61#
62# Displays an error message, printing file name and line
63#
64sub parse_error($$$$) {
65 my ($file, $ln, $msg, $data) = @_;
66
f090db43
MCC
67 return if (!$show_warnings);
68
75442fb0
MCC
69 $data =~ s/\s+$/\n/;
70
71 print STDERR "Warning: file $file#$ln:\n\t$msg";
72
73 if ($data ne "") {
74 print STDERR ". Line\n\t\t$data";
75 } else {
76 print STDERR "\n";
77 }
bbc249f2
MCC
78}
79
80#
81# Parse an ABI file, storing its contents at %data
82#
83sub parse_abi {
84 my $file = $File::Find::name;
85
86 my $mode = (stat($file))[2];
87 return if ($mode & S_IFDIR);
88 return if ($file =~ m,/README,);
89
90 my $name = $file;
91 $name =~ s,.*/,,;
92
a4ea67bc
MCC
93 my $fn = $file;
94 $fn =~ s,Documentation/ABI/,,;
95
96 my $nametag = "File $fn";
d0ebaf51
MCC
97 $data{$nametag}->{what} = "File $name";
98 $data{$nametag}->{type} = "File";
99 $data{$nametag}->{file} = $name;
33e3e991 100 $data{$nametag}->{filepath} = $file;
d0ebaf51 101 $data{$nametag}->{is_file} = 1;
61439c4a 102 $data{$nametag}->{line_no} = 1;
d0ebaf51 103
bbc249f2
MCC
104 my $type = $file;
105 $type =~ s,.*/(.*)/.*,$1,;
106
107 my $what;
108 my $new_what;
234948bf 109 my $tag = "";
bbc249f2 110 my $ln;
6619c661 111 my $xrefs;
4e6a6234 112 my $space;
d0ebaf51 113 my @labels;
234948bf 114 my $label = "";
bbc249f2 115
46f661fd 116 print STDERR "Opening $file\n" if ($debug & $dbg_what_open);
bbc249f2
MCC
117 open IN, $file;
118 while(<IN>) {
119 $ln++;
4e6a6234 120 if (m/^(\S+)(:\s*)(.*)/i) {
bbc249f2 121 my $new_tag = lc($1);
4e6a6234
MCC
122 my $sep = $2;
123 my $content = $3;
bbc249f2 124
7ce7b89b 125 if (!($new_tag =~ m/(what|where|date|kernelversion|contact|description|users)/)) {
bbc249f2 126 if ($tag eq "description") {
4e6a6234
MCC
127 # New "tag" is actually part of
128 # description. Don't consider it a tag
129 $new_tag = "";
7d7ea8d2 130 } elsif ($tag ne "") {
bbc249f2
MCC
131 parse_error($file, $ln, "tag '$tag' is invalid", $_);
132 }
133 }
134
2c0700e7
MCC
135 # Invalid, but it is a common mistake
136 if ($new_tag eq "where") {
75442fb0 137 parse_error($file, $ln, "tag 'Where' is invalid. Should be 'What:' instead", "");
2c0700e7
MCC
138 $new_tag = "what";
139 }
140
bbc249f2 141 if ($new_tag =~ m/what/) {
4e6a6234 142 $space = "";
234948bf
MCC
143 $content =~ s/[,.;]$//;
144
c7ba3334
MCC
145 push @{$symbols{$content}->{file}}, " $file:" . ($ln - 1);
146
bbc249f2 147 if ($tag =~ m/what/) {
ab9c1480 148 $what .= "\xac" . $content;
bbc249f2 149 } else {
234948bf
MCC
150 if ($what) {
151 parse_error($file, $ln, "What '$what' doesn't have a description", "") if (!$data{$what}->{description});
152
ab9c1480 153 foreach my $w(split /\xac/, $what) {
c7ba3334 154 $symbols{$w}->{xref} = $what;
234948bf
MCC
155 };
156 }
4e6a6234 157
bbc249f2 158 $what = $content;
d0ebaf51 159 $label = $content;
bbc249f2
MCC
160 $new_what = 1;
161 }
d0ebaf51 162 push @labels, [($content, $label)];
bbc249f2 163 $tag = $new_tag;
6619c661 164
234948bf 165 push @{$data{$nametag}->{symbols}}, $content if ($data{$nametag}->{what});
bbc249f2
MCC
166 next;
167 }
168
7d7ea8d2 169 if ($tag ne "" && $new_tag) {
4e6a6234 170 $tag = $new_tag;
bbc249f2 171
4e6a6234 172 if ($new_what) {
234948bf 173 @{$data{$what}->{label_list}} = @labels if ($data{$nametag}->{what});
d0ebaf51
MCC
174 @labels = ();
175 $label = "";
4e6a6234 176 $new_what = 0;
bbc249f2 177
4e6a6234 178 $data{$what}->{type} = $type;
c7ba3334
MCC
179 if (!defined($data{$what}->{file})) {
180 $data{$what}->{file} = $name;
181 $data{$what}->{filepath} = $file;
182 } else {
ff3777d0 183 $data{$what}->{description} .= "\n\n" if (defined($data{$what}->{description}));
c7ba3334
MCC
184 if ($name ne $data{$what}->{file}) {
185 $data{$what}->{file} .= " " . $name;
186 $data{$what}->{filepath} .= " " . $file;
187 }
188 }
46f661fd 189 print STDERR "\twhat: $what\n" if ($debug & $dbg_what_parsing);
c7ba3334
MCC
190 $data{$what}->{line_no} = $ln;
191 } else {
192 $data{$what}->{line_no} = $ln if (!defined($data{$what}->{line_no}));
4e6a6234 193 }
bbc249f2 194
4e6a6234
MCC
195 if (!$what) {
196 parse_error($file, $ln, "'What:' should come first:", $_);
197 next;
198 }
f82a8a74
MCC
199 if ($new_tag eq "description") {
200 $sep =~ s,:, ,;
11ce90a4 201 $content = ' ' x length($new_tag) . $sep . $content;
f82a8a74
MCC
202 while ($content =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {}
203 if ($content =~ m/^(\s*)(\S.*)$/) {
204 # Preserve initial spaces for the first line
11ce90a4 205 $space = $1;
f82a8a74
MCC
206 $content = "$2\n";
207 $data{$what}->{$tag} .= $content;
208 } else {
209 undef($space);
4e6a6234 210 }
e9bca891 211
4e6a6234
MCC
212 } else {
213 $data{$what}->{$tag} = $content;
214 }
bbc249f2
MCC
215 next;
216 }
bbc249f2
MCC
217 }
218
4e6a6234 219 # Store any contents before tags at the database
d0ebaf51
MCC
220 if (!$tag && $data{$nametag}->{what}) {
221 $data{$nametag}->{description} .= $_;
6619c661
MCC
222 next;
223 }
bbc249f2 224
4e6a6234 225 if ($tag eq "description") {
e9bca891
MCC
226 my $content = $_;
227 while ($content =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {}
f82a8a74
MCC
228 if (m/^\s*\n/) {
229 $data{$what}->{$tag} .= "\n";
230 next;
231 }
232
233 if (!defined($space)) {
e9bca891 234 # Preserve initial spaces for the first line
f82a8a74 235 if ($content =~ m/^(\s*)(\S.*)$/) {
e9bca891 236 $space = $1;
f82a8a74 237 $content = "$2\n";
4e6a6234
MCC
238 }
239 } else {
4e6a6234 240 $space = "" if (!($content =~ s/^($space)//));
4e6a6234 241 }
f82a8a74
MCC
242 $data{$what}->{$tag} .= $content;
243
4e6a6234
MCC
244 next;
245 }
bbc249f2
MCC
246 if (m/^\s*(.*)/) {
247 $data{$what}->{$tag} .= "\n$1";
248 $data{$what}->{$tag} =~ s/\n+$//;
249 next;
250 }
251
252 # Everything else is error
75442fb0 253 parse_error($file, $ln, "Unexpected content", $_);
bbc249f2 254 }
234948bf
MCC
255 $data{$nametag}->{description} =~ s/^\n+// if ($data{$nametag}->{description});
256 if ($what) {
257 parse_error($file, $ln, "What '$what' doesn't have a description", "") if (!$data{$what}->{description});
258
ab9c1480 259 foreach my $w(split /\xac/,$what) {
c7ba3334 260 $symbols{$w}->{xref} = $what;
234948bf
MCC
261 };
262 }
bbc249f2
MCC
263 close IN;
264}
265
234948bf
MCC
266sub create_labels {
267 my %labels;
bbc249f2 268
234948bf
MCC
269 foreach my $what (keys %data) {
270 next if ($data{$what}->{file} eq "File");
4e6a6234 271
234948bf 272 foreach my $p (@{$data{$what}->{label_list}}) {
d0ebaf51
MCC
273 my ($content, $label) = @{$p};
274 $label = "abi_" . $label . " ";
275 $label =~ tr/A-Z/a-z/;
276
277 # Convert special chars to "_"
278 $label =~s/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\xff])/_/g;
279 $label =~ s,_+,_,g;
280 $label =~ s,_$,,;
281
2e7ce055
MCC
282 # Avoid duplicated labels
283 while (defined($labels{$label})) {
284 my @chars = ("A".."Z", "a".."z");
285 $label .= $chars[rand @chars];
286 }
287 $labels{$label} = 1;
288
234948bf 289 $data{$what}->{label} = $label;
d0ebaf51
MCC
290
291 # only one label is enough
292 last;
6619c661 293 }
234948bf
MCC
294 }
295}
296
297#
298# Outputs the book on ReST format
299#
300
50ebf8f4
MCC
301# \b doesn't work well with paths. So, we need to define something else:
302# Boundaries are punct characters, spaces and end-of-line
303my $start = qr {(^|\s|\() }x;
304my $bondary = qr { ([,.:;\)\s]|\z) }x;
87ec9ea1 305my $xref_match = qr { $start(\/(sys|config|proc|dev|kvd)\/[^,.:;\)\s]+)$bondary }x;
b0f9580a 306my $symbols = qr { ([\x01-\x08\x0e-\x1f\x21-\x2f\x3a-\x40\x7b-\xff]) }x;
55e5414f 307
234948bf
MCC
308sub output_rest {
309 create_labels();
310
9d4fdda3
MCC
311 my $part = "";
312
234948bf
MCC
313 foreach my $what (sort {
314 ($data{$a}->{type} eq "File") cmp ($data{$b}->{type} eq "File") ||
315 $a cmp $b
316 } keys %data) {
317 my $type = $data{$what}->{type};
c7ba3334
MCC
318
319 my @file = split / /, $data{$what}->{file};
320 my @filepath = split / /, $data{$what}->{filepath};
234948bf
MCC
321
322 if ($enable_lineno) {
323 printf "#define LINENO %s%s#%s\n\n",
c7ba3334 324 $prefix, $file[0],
234948bf
MCC
325 $data{$what}->{line_no};
326 }
6619c661 327
234948bf 328 my $w = $what;
6619c661 329
c7ba3334 330 if ($type ne "File") {
9d4fdda3
MCC
331 my $cur_part = $what;
332 if ($what =~ '/') {
333 if ($what =~ m#^(\/?(?:[\w\-]+\/?){1,2})#) {
334 $cur_part = "Symbols under $1";
335 $cur_part =~ s,/$,,;
336 }
337 }
338
339 if ($cur_part ne "" && $part ne $cur_part) {
340 $part = $cur_part;
341 my $bar = $part;
342 $bar =~ s/./-/g;
343 print "$part\n$bar\n\n";
344 }
345
234948bf 346 printf ".. _%s:\n\n", $data{$what}->{label};
45f96517 347
ab9c1480 348 my @names = split /\xac/,$w;
45f96517
MCC
349 my $len = 0;
350
351 foreach my $name (@names) {
b0f9580a 352 $name =~ s/$symbols/\\$1/g;
c01d62d3 353 $name = "**$name**";
45f96517
MCC
354 $len = length($name) if (length($name) > $len);
355 }
356
45f96517
MCC
357 print "+-" . "-" x $len . "-+\n";
358 foreach my $name (@names) {
359 printf "| %s", $name . " " x ($len - length($name)) . " |\n";
360 print "+-" . "-" x $len . "-+\n";
361 }
45f96517 362
c7ba3334
MCC
363 print "\n";
364 }
365
366 for (my $i = 0; $i < scalar(@filepath); $i++) {
367 my $path = $filepath[$i];
368 my $f = $file[$i];
369
370 $path =~ s,.*/(.*/.*),$1,;;
371 $path =~ s,[/\-],_,g;;
372 my $fileref = "abi_file_".$path;
373
374 if ($type eq "File") {
c7ba3334 375 print ".. _$fileref:\n\n";
c7ba3334
MCC
376 } else {
377 print "Defined on file :ref:`$f <$fileref>`\n\n";
378 }
234948bf 379 }
bbc249f2 380
a4ea67bc
MCC
381 if ($type eq "File") {
382 my $bar = $w;
383 $bar =~ s/./-/g;
384 print "$w\n$bar\n\n";
385 }
386
234948bf
MCC
387 my $desc = "";
388 $desc = $data{$what}->{description} if (defined($data{$what}->{description}));
389 $desc =~ s/\s+$/\n/;
bbc249f2 390
4e6a6234 391 if (!($desc =~ /^\s*$/)) {
11ce90a4 392 if ($description_is_rst) {
daaaf58a
MCC
393 # Remove title markups from the description
394 # Having titles inside ABI files will only work if extra
395 # care would be taken in order to strictly follow the same
396 # level order for each markup.
397 $desc =~ s/\n[\-\*\=\^\~]+\n/\n\n/g;
398
55e5414f
MCC
399 # Enrich text by creating cross-references
400
c27c2e34 401 my $new_desc = "";
2ae7bb57
MCC
402 my $init_indent = -1;
403 my $literal_indent = -1;
404
c27c2e34
MCC
405 open(my $fh, "+<", \$desc);
406 while (my $d = <$fh>) {
2ae7bb57
MCC
407 my $indent = $d =~ m/^(\s+)/;
408 my $spaces = length($indent);
409 $init_indent = $indent if ($init_indent < 0);
410 if ($literal_indent >= 0) {
411 if ($spaces > $literal_indent) {
412 $new_desc .= $d;
413 next;
414 } else {
415 $literal_indent = -1;
416 }
417 } else {
418 if ($d =~ /()::$/ && !($d =~ /^\s*\.\./)) {
419 $literal_indent = $spaces;
420 }
421 }
422
c27c2e34
MCC
423 $d =~ s,Documentation/(?!devicetree)(\S+)\.rst,:doc:`/$1`,g;
424
425 my @matches = $d =~ m,Documentation/ABI/([\w\/\-]+),g;
426 foreach my $f (@matches) {
427 my $xref = $f;
428 my $path = $f;
429 $path =~ s,.*/(.*/.*),$1,;;
430 $path =~ s,[/\-],_,g;;
431 $xref .= " <abi_file_" . $path . ">";
432 $d =~ s,\bDocumentation/ABI/$f\b,:ref:`$xref`,g;
433 }
55e5414f 434
c27c2e34
MCC
435 # Seek for cross reference symbols like /sys/...
436 @matches = $d =~ m/$xref_match/g;
55e5414f 437
c27c2e34
MCC
438 foreach my $s (@matches) {
439 next if (!($s =~ m,/,));
440 if (defined($data{$s}) && defined($data{$s}->{label})) {
441 my $xref = $s;
55e5414f 442
c27c2e34
MCC
443 $xref =~ s/$symbols/\\$1/g;
444 $xref = ":ref:`$xref <" . $data{$s}->{label} . ">`";
55e5414f 445
c27c2e34
MCC
446 $d =~ s,$start$s$bondary,$1$xref$2,g;
447 }
55e5414f 448 }
c27c2e34 449 $new_desc .= $d;
55e5414f 450 }
c27c2e34
MCC
451 close $fh;
452
55e5414f 453
c27c2e34 454 print "$new_desc\n\n";
4e6a6234 455 } else {
11ce90a4 456 $desc =~ s/^\s+//;
bbc249f2 457
11ce90a4
MCC
458 # Remove title markups from the description, as they won't work
459 $desc =~ s/\n[\-\*\=\^\~]+\n/\n\n/g;
460
461 if ($desc =~ m/\:\n/ || $desc =~ m/\n[\t ]+/ || $desc =~ m/[\x00-\x08\x0b-\x1f\x7b-\xff]/) {
462 # put everything inside a code block
463 $desc =~ s/\n/\n /g;
464
465 print "::\n\n";
466 print " $desc\n\n";
467 } else {
468 # Escape any special chars from description
469 $desc =~s/([\x00-\x08\x0b-\x1f\x21-\x2a\x2d\x2f\x3c-\x40\x5c\x5e-\x60\x7b-\xff])/\\$1/g;
470 print "$desc\n\n";
471 }
4e6a6234 472 }
bbc249f2 473 } else {
d0ebaf51 474 print "DESCRIPTION MISSING for $what\n\n" if (!$data{$what}->{is_file});
bbc249f2 475 }
6619c661 476
234948bf 477 if ($data{$what}->{symbols}) {
d0ebaf51
MCC
478 printf "Has the following ABI:\n\n";
479
234948bf 480 foreach my $content(@{$data{$what}->{symbols}}) {
c7ba3334 481 my $label = $data{$symbols{$content}->{xref}}->{label};
d0ebaf51
MCC
482
483 # Escape special chars from content
484 $content =~s/([\x00-\x1f\x21-\x2f\x3a-\x40\x7b-\xff])/\\$1/g;
485
486 print "- :ref:`$content <$label>`\n\n";
487 }
488 }
a16ab14e
MCC
489
490 if (defined($data{$what}->{users})) {
491 my $users = $data{$what}->{users};
492
493 $users =~ s/\n/\n\t/g;
494 printf "Users:\n\t%s\n\n", $users if ($users ne "");
495 }
496
bbc249f2
MCC
497 }
498}
499
33e3e991
MCC
500#
501# Searches for ABI symbols
502#
503sub search_symbols {
504 foreach my $what (sort keys %data) {
505 next if (!($what =~ m/($arg)/));
506
507 my $type = $data{$what}->{type};
508 next if ($type eq "File");
509
510 my $file = $data{$what}->{filepath};
511
e27c42a5 512 $what =~ s/\xac/, /g;
33e3e991
MCC
513 my $bar = $what;
514 $bar =~ s/./-/g;
515
516 print "\n$what\n$bar\n\n";
517
234948bf
MCC
518 my $kernelversion = $data{$what}->{kernelversion} if (defined($data{$what}->{kernelversion}));
519 my $contact = $data{$what}->{contact} if (defined($data{$what}->{contact}));
520 my $users = $data{$what}->{users} if (defined($data{$what}->{users}));
521 my $date = $data{$what}->{date} if (defined($data{$what}->{date}));
522 my $desc = $data{$what}->{description} if (defined($data{$what}->{description}));
523
524 $kernelversion =~ s/^\s+// if ($kernelversion);
525 $contact =~ s/^\s+// if ($contact);
526 if ($users) {
527 $users =~ s/^\s+//;
528 $users =~ s/\n//g;
529 }
530 $date =~ s/^\s+// if ($date);
531 $desc =~ s/^\s+// if ($desc);
33e3e991
MCC
532
533 printf "Kernel version:\t\t%s\n", $kernelversion if ($kernelversion);
534 printf "Date:\t\t\t%s\n", $date if ($date);
535 printf "Contact:\t\t%s\n", $contact if ($contact);
536 printf "Users:\t\t\t%s\n", $users if ($users);
c7ba3334 537 print "Defined on file(s):\t$file\n\n";
33e3e991
MCC
538 print "Description:\n\n$desc";
539 }
540}
541
f090db43 542# Exclude /sys/kernel/debug and /sys/kernel/tracing from the search path
ab02c515 543sub dont_parse_special_attributes {
f090db43
MCC
544 if (($File::Find::dir =~ m,^/sys/kernel,)) {
545 return grep {!/(debug|tracing)/ } @_;
546 }
547
548 if (($File::Find::dir =~ m,^/sys/fs,)) {
549 return grep {!/(pstore|bpf|fuse)/ } @_;
550 }
551
552 return @_
553}
554
555my %leaf;
ab02c515
MCC
556my %aliases;
557my @files;
ca8e055c
MCC
558my %root;
559
560sub graph_add_file {
561 my $file = shift;
562 my $type = shift;
563
564 my $dir = $file;
565 $dir =~ s,^(.*/).*,$1,;
566 $file =~ s,.*/,,;
567
568 my $name;
569 my $file_ref = \%root;
570 foreach my $edge(split "/", $dir) {
571 $name .= "$edge/";
572 if (!defined ${$file_ref}{$edge}) {
573 ${$file_ref}{$edge} = { };
574 }
575 $file_ref = \%{$$file_ref{$edge}};
576 ${$file_ref}{"__name"} = [ $name ];
577 }
578 $name .= "$file";
579 ${$file_ref}{$file} = {
580 "__name" => [ $name ]
581 };
582
583 return \%{$$file_ref{$file}};
584}
585
586sub graph_add_link {
587 my $file = shift;
588 my $link = shift;
589
590 # Traverse graph to find the reference
591 my $file_ref = \%root;
592 foreach my $edge(split "/", $file) {
593 $file_ref = \%{$$file_ref{$edge}} || die "Missing node!";
594 }
595
596 # do a BFS
597
598 my @queue;
599 my %seen;
600 my $base_name;
601 my $st;
602
603 push @queue, $file_ref;
604 $seen{$start}++;
605
606 while (@queue) {
607 my $v = shift @queue;
608 my @child = keys(%{$v});
609
610 foreach my $c(@child) {
611 next if $seen{$$v{$c}};
612 next if ($c eq "__name");
613
614 # Add new name
615 my $name = @{$$v{$c}{"__name"}}[0];
616 if ($name =~ s#^$file/#$link/#) {
617 push @{$$v{$c}{"__name"}}, $name;
618 }
619 # Add child to the queue and mark as seen
620 push @queue, $$v{$c};
621 $seen{$c}++;
622 }
623 }
624}
f090db43 625
ab02c515 626my $escape_symbols = qr { ([\x01-\x08\x0e-\x1f\x21-\x29\x2b-\x2d\x3a-\x40\x7b-\xfe]) }x;
f090db43
MCC
627sub parse_existing_sysfs {
628 my $file = $File::Find::name;
0b87a1b8
MCC
629
630 # Ignore cgroup and firmware
631 return if ($file =~ m#^/sys/(fs/cgroup|firmware)/#);
632
0cd9e25b
MCC
633 # Ignore some sysfs nodes
634 return if ($file =~ m#/(sections|notes)/#);
635
636 # Would need to check at
637 # Documentation/admin-guide/kernel-parameters.txt, but this
638 # is not easily parseable.
639 return if ($file =~ m#/parameters/#);
640
ab02c515
MCC
641 my $mode = (lstat($file))[2];
642 my $abs_file = abs_path($file);
f090db43 643
ab02c515
MCC
644 if (S_ISLNK($mode)) {
645 $aliases{$file} = $abs_file;
646 return;
647 }
648
649 return if (S_ISDIR($mode));
f090db43 650
ab02c515
MCC
651 # Trivial: file is defined exactly the same way at ABI What:
652 return if (defined($data{$file}));
653 return if (defined($data{$abs_file}));
f090db43 654
ca8e055c
MCC
655 push @files, graph_add_file($abs_file, "file");
656}
657
658sub get_leave($)
659{
660 my $what = shift;
661 my $leave;
662
663 my $l = $what;
664 my $stop = 1;
665
666 $leave = $l;
667 $leave =~ s,/$,,;
668 $leave =~ s,.*/,,;
669 $leave =~ s/[\(\)]//g;
670
671 # $leave is used to improve search performance at
672 # check_undefined_symbols, as the algorithm there can seek
673 # for a small number of "what". It also allows giving a
674 # hint about a leave with the same name somewhere else.
675 # However, there are a few occurences where the leave is
676 # either a wildcard or a number. Just group such cases
677 # altogether.
92635894 678 if ($leave =~ m/\.\*/ || $leave eq "" || $leave =~ /\\d/) {
ca8e055c
MCC
679 $leave = "others";
680 }
681
682 return $leave;
ab02c515 683}
f090db43 684
ab02c515 685sub check_undefined_symbols {
ca8e055c
MCC
686 foreach my $file_ref (sort @files) {
687 my @names = @{$$file_ref{"__name"}};
688 my $file = $names[0];
f090db43 689
ab02c515 690 my $exact = 0;
14c94257 691 my $found_string;
f090db43 692
ca8e055c
MCC
693 my $leave = get_leave($file);
694 if (!defined($leaf{$leave})) {
695 $leave = "others";
696 }
f34f6729
MCC
697 my @expr = @{$leaf{$leave}->{expr}};
698 die ("missing rules for $leave") if (!defined($leaf{$leave}));
ab02c515
MCC
699
700 my $path = $file;
701 $path =~ s,(.*/).*,$1,;
702
14c94257
MCC
703 if ($search_string) {
704 next if (!($file =~ m#$search_string#));
705 $found_string = 1;
706 }
707
f34f6729
MCC
708 for (my $i = 0; $i < @names; $i++) {
709 if ($found_string && $hint) {
710 if (!$i) {
711 print "--> $names[$i]\n";
712 } else {
713 print " $names[$i]\n";
714 }
715 }
716 foreach my $re (@expr) {
717 print "$names[$i] =~ /^$re\$/\n" if ($debug && $dbg_undefined);
718 if ($names[$i] =~ $re) {
ca8e055c
MCC
719 $exact = 1;
720 last;
ab02c515 721 }
f090db43 722 }
cb06b8dd 723 last if ($exact);
f090db43 724 }
ab02c515 725 next if ($exact);
f090db43 726
d4771993
MCC
727 if ($leave ne "others") {
728 my @expr = @{$leaf{$leave}->{expr}};
729 for (my $i = 0; $i < @names; $i++) {
730 foreach my $re (@expr) {
731 print "$names[$i] =~ /^$re\$/\n" if ($debug && $dbg_undefined);
732 if ($names[$i] =~ $re) {
733 $exact = 1;
734 last;
735 }
736 }
737 last if ($exact);
738 }
739 last if ($exact);
740 }
741 next if ($exact);
742
cb06b8dd 743 if ($hint && (!$search_string || $found_string)) {
f34f6729 744 my $what = $leaf{$leave}->{what};
ca8e055c
MCC
745 $what =~ s/\xac/\n\t/g;
746 if ($leave ne "others") {
747 print " more likely regexes:\n\t$what\n";
748 } else {
749 print " tested regexes:\n\t$what\n";
750 }
ab02c515
MCC
751 next;
752 }
14c94257 753 print "$file not found.\n" if (!$search_string || $found_string);
ab02c515 754 }
f090db43
MCC
755}
756
757sub undefined_symbols {
ab02c515
MCC
758 find({
759 wanted =>\&parse_existing_sysfs,
760 preprocess =>\&dont_parse_special_attributes,
761 no_chdir => 1
762 }, $sysfs_prefix);
763
f34f6729 764 $leaf{"others"}->{what} = "";
ca8e055c 765
f090db43 766 foreach my $w (sort keys %data) {
ca8e055c 767 foreach my $what (split /\xac/,$w) {
ab02c515
MCC
768 next if (!($what =~ m/^$sysfs_prefix/));
769
770 # Convert what into regular expressions
771
772 $what =~ s,/\.\.\./,/*/,g;
773 $what =~ s,\*,.*,g;
774
775 # Temporarily change [0-9]+ type of patterns
776 $what =~ s/\[0\-9\]\+/\xff/g;
777
778 # Temporarily change [\d+-\d+] type of patterns
779 $what =~ s/\[0\-\d+\]/\xff/g;
780 $what =~ s/\[(\d+)\]/\xf4$1\xf5/g;
781
782 # Temporarily change [0-9] type of patterns
783 $what =~ s/\[(\d)\-(\d)\]/\xf4$1-$2\xf5/g;
784
785 # Handle multiple option patterns
786 $what =~ s/[\{\<\[]([\w_]+)(?:[,|]+([\w_]+)){1,}[\}\>\]]/($1|$2)/g;
787
788 # Handle wildcards
789 $what =~ s/\<[^\>]+\>/.*/g;
790 $what =~ s/\{[^\}]+\}/.*/g;
791 $what =~ s/\[[^\]]+\]/.*/g;
792
793 $what =~ s/[XYZ]/.*/g;
794
795 # Recover [0-9] type of patterns
796 $what =~ s/\xf4/[/g;
797 $what =~ s/\xf5/]/g;
798
799 # Remove duplicated spaces
800 $what =~ s/\s+/ /g;
801
802 # Special case: this ABI has a parenthesis on it
803 $what =~ s/sqrt\(x^2\+y^2\+z^2\)/sqrt\(x^2\+y^2\+z^2\)/;
804
805 # Special case: drop comparition as in:
806 # What: foo = <something>
807 # (this happens on a few IIO definitions)
808 $what =~ s,\s*\=.*$,,;
809
ab02c515
MCC
810 # Escape all other symbols
811 $what =~ s/$escape_symbols/\\$1/g;
812 $what =~ s/\\\\/\\/g;
813 $what =~ s/\\([\[\]\(\)\|])/$1/g;
814 $what =~ s/(\d+)\\(-\d+)/$1$2/g;
815
14c94257
MCC
816 $what =~ s/\xff/\\d+/g;
817
14c94257
MCC
818 # Special case: IIO ABI which a parenthesis.
819 $what =~ s/sqrt(.*)/sqrt\(.*\)/;
820
45495db9 821 my $leave = get_leave($what);
f34f6729 822
14c94257 823 my $added = 0;
ab02c515
MCC
824 foreach my $l (split /\|/, $leave) {
825 if (defined($leaf{$l})) {
f34f6729
MCC
826 next if ($leaf{$l}->{what} =~ m/\b$what\b/);
827 $leaf{$l}->{what} .= "\xac" . $what;
14c94257 828 $added = 1;
ab02c515 829 } else {
f34f6729 830 $leaf{$l}->{what} = $what;
14c94257 831 $added = 1;
ab02c515 832 }
f090db43 833 }
14c94257
MCC
834 if ($search_string && $added) {
835 print "What: $what\n" if ($what =~ m#$search_string#);
836 }
837
f090db43
MCC
838 }
839 }
f34f6729
MCC
840 # Compile regexes
841 foreach my $l (keys %leaf) {
842 my @expr;
843 foreach my $w(split /\xac/, $leaf{$l}->{what}) {
844 push @expr, qr /^$w$/;
845 }
846 $leaf{$l}->{expr} = \@expr;
847 }
848
ca8e055c
MCC
849 # Take links into account
850 foreach my $link (keys %aliases) {
851 my $abs_file = $aliases{$link};
852 graph_add_link($abs_file, $link);
853 }
ab02c515 854 check_undefined_symbols;
f090db43
MCC
855}
856
61439c4a
MCC
857# Ensure that the prefix will always end with a slash
858# While this is not needed for find, it makes the patch nicer
859# with --enable-lineno
860$prefix =~ s,/?$,/,;
33e3e991 861
f090db43
MCC
862if ($cmd eq "undefined" || $cmd eq "search") {
863 $show_warnings = 0;
864}
bbc249f2
MCC
865#
866# Parses all ABI files located at $prefix dir
867#
868find({wanted =>\&parse_abi, no_chdir => 1}, $prefix);
869
46f661fd 870print STDERR Data::Dumper->Dump([\%data], [qw(*data)]) if ($debug & $dbg_dump_abi_structs);
bbc249f2
MCC
871
872#
33e3e991 873# Handles the command
bbc249f2 874#
f090db43
MCC
875if ($cmd eq "undefined") {
876 undefined_symbols;
877} elsif ($cmd eq "search") {
33e3e991 878 search_symbols;
c7ba3334
MCC
879} else {
880 if ($cmd eq "rest") {
881 output_rest;
882 }
883
884 # Warn about duplicated ABI entries
885 foreach my $what(sort keys %symbols) {
886 my @files = @{$symbols{$what}->{file}};
887
888 next if (scalar(@files) == 1);
bbc249f2 889
c7ba3334
MCC
890 printf STDERR "Warning: $what is defined %d times: @files\n",
891 scalar(@files);
892 }
893}
bbc249f2
MCC
894
895__END__
896
897=head1 NAME
898
899abi_book.pl - parse the Linux ABI files and produce a ReST book.
900
901=head1 SYNOPSIS
902
46f661fd 903B<abi_book.pl> [--debug <level>] [--enable-lineno] [--man] [--help]
ab02c515 904 [--(no-)rst-source] [--dir=<dir>] [--show-hints]
14c94257 905 [--search-string <regex>]
ab02c515 906 <COMAND> [<ARGUMENT>]
33e3e991
MCC
907
908Where <COMMAND> can be:
909
910=over 8
911
912B<search> [SEARCH_REGEX] - search for [SEARCH_REGEX] inside ABI
913
7ce7b89b
MCC
914B<rest> - output the ABI in ReST markup language
915
916B<validate> - validate the ABI contents
33e3e991 917
f090db43
MCC
918B<undefined> - existing symbols at the system that aren't
919 defined at Documentation/ABI
920
33e3e991 921=back
bbc249f2
MCC
922
923=head1 OPTIONS
924
925=over 8
926
33e3e991
MCC
927=item B<--dir>
928
929Changes the location of the ABI search. By default, it uses
930the Documentation/ABI directory.
931
11ce90a4
MCC
932=item B<--rst-source> and B<--no-rst-source>
933
934The input file may be using ReST syntax or not. Those two options allow
935selecting between a rst-compliant source ABI (--rst-source), or a
936plain text that may be violating ReST spec, so it requres some escaping
937logic (--no-rst-source).
938
61439c4a
MCC
939=item B<--enable-lineno>
940
941Enable output of #define LINENO lines.
942
46f661fd
MCC
943=item B<--debug> I<debug level>
944
945Print debug information according with the level, which is given by the
946following bitmask:
bbc249f2 947
46f661fd
MCC
948 - 1: Debug parsing What entries from ABI files;
949 - 2: Shows what files are opened from ABI files;
950 - 4: Dump the structs used to store the contents of the ABI files.
bbc249f2 951
ab02c515
MCC
952=item B<--show-hints>
953
954Show hints about possible definitions for the missing ABI symbols.
955Used only when B<undefined>.
956
14c94257
MCC
957=item B<--search-string> [regex string]
958
959Show only occurences that match a search string.
960Used only when B<undefined>.
961
bbc249f2
MCC
962=item B<--help>
963
964Prints a brief help message and exits.
965
966=item B<--man>
967
968Prints the manual page and exits.
969
970=back
971
972=head1 DESCRIPTION
973
33e3e991
MCC
974Parse the Linux ABI files from ABI DIR (usually located at Documentation/ABI),
975allowing to search for ABI symbols or to produce a ReST book containing
976the Linux ABI documentation.
977
978=head1 EXAMPLES
979
980Search for all stable symbols with the word "usb":
981
982=over 8
983
984$ scripts/get_abi.pl search usb --dir Documentation/ABI/stable
985
986=back
987
988Search for all symbols that match the regex expression "usb.*cap":
989
990=over 8
991
992$ scripts/get_abi.pl search usb.*cap
993
994=back
995
996Output all obsoleted symbols in ReST format
997
998=over 8
999
1000$ scripts/get_abi.pl rest --dir Documentation/ABI/obsolete
1001
1002=back
bbc249f2
MCC
1003
1004=head1 BUGS
1005
7ce7b89b 1006Report bugs to Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
bbc249f2
MCC
1007
1008=head1 COPYRIGHT
1009
7ce7b89b 1010Copyright (c) 2016-2019 by Mauro Carvalho Chehab <mchehab+samsung@kernel.org>.
bbc249f2
MCC
1011
1012License GPLv2: GNU GPL version 2 <http://gnu.org/licenses/gpl.html>.
1013
1014This is free software: you are free to change and redistribute it.
1015There is NO WARRANTY, to the extent permitted by law.
1016
1017=cut