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