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