]>
Commit | Line | Data |
---|---|---|
c25ce589 | 1 | #!/usr/bin/env perl |
ecb351f1 | 2 | # SPDX-License-Identifier: GPL-2.0 |
bbc249f2 MCC |
3 | |
4 | use strict; | |
234948bf | 5 | use warnings; |
55e5414f | 6 | use utf8; |
bbc249f2 MCC |
7 | use Pod::Usage; |
8 | use Getopt::Long; | |
9 | use File::Find; | |
10 | use Fcntl ':mode'; | |
ab02c515 | 11 | use Cwd 'abs_path'; |
46f661fd | 12 | use Data::Dumper; |
bbc249f2 | 13 | |
234948bf | 14 | my $help = 0; |
ab02c515 | 15 | my $hint = 0; |
234948bf MCC |
16 | my $man = 0; |
17 | my $debug = 0; | |
18 | my $enable_lineno = 0; | |
f090db43 | 19 | my $show_warnings = 1; |
33e3e991 | 20 | my $prefix="Documentation/ABI"; |
f090db43 | 21 | my $sysfs_prefix="/sys"; |
14c94257 | 22 | my $search_string; |
bbc249f2 | 23 | |
46f661fd MCC |
24 | # Debug options |
25 | my $dbg_what_parsing = 1; | |
26 | my $dbg_what_open = 2; | |
27 | my $dbg_dump_abi_structs = 4; | |
f34f6729 | 28 | my $dbg_undefined = 8; |
46f661fd | 29 | |
11ce90a4 MCC |
30 | # |
31 | # If true, assumes that the description is formatted with ReST | |
32 | # | |
2fcce37a | 33 | my $description_is_rst = 1; |
11ce90a4 | 34 | |
bbc249f2 | 35 | GetOptions( |
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 | ||
46 | pod2usage(1) if $help; | |
47 | pod2usage(-exitstatus => 0, -verbose => 2) if $man; | |
48 | ||
33e3e991 | 49 | pod2usage(2) if (scalar @ARGV < 1 || @ARGV > 2); |
bbc249f2 | 50 | |
33e3e991 MCC |
51 | my ($cmd, $arg) = @ARGV; |
52 | ||
f090db43 | 53 | pod2usage(2) if ($cmd ne "search" && $cmd ne "rest" && $cmd ne "validate" && $cmd ne "undefined"); |
33e3e991 | 54 | pod2usage(2) if ($cmd eq "search" && !$arg); |
bbc249f2 | 55 | |
46f661fd | 56 | require Data::Dumper if ($debug & $dbg_dump_abi_structs); |
bbc249f2 MCC |
57 | |
58 | my %data; | |
234948bf | 59 | my %symbols; |
bbc249f2 MCC |
60 | |
61 | # | |
62 | # Displays an error message, printing file name and line | |
63 | # | |
64 | sub 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 | # | |
83 | sub 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 |
265 | sub 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 | |
302 | my $start = qr {(^|\s|\() }x; | |
303 | my $bondary = qr { ([,.:;\)\s]|\z) }x; | |
87ec9ea1 | 304 | my $xref_match = qr { $start(\/(sys|config|proc|dev|kvd)\/[^,.:;\)\s]+)$bondary }x; |
b0f9580a | 305 | my $symbols = qr { ([\x01-\x08\x0e-\x1f\x21-\x2f\x3a-\x40\x7b-\xff]) }x; |
55e5414f | 306 | |
234948bf MCC |
307 | sub 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 | # | |
502 | sub 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 | 542 | sub 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 | ||
554 | my %leaf; | |
ab02c515 MCC |
555 | my %aliases; |
556 | my @files; | |
ca8e055c MCC |
557 | my %root; |
558 | ||
559 | sub 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 | ||
585 | sub 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 | 625 | my $escape_symbols = qr { ([\x01-\x08\x0e-\x1f\x21-\x29\x2b-\x2d\x3a-\x40\x7b-\xfe]) }x; |
f090db43 MCC |
626 | sub 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 | ||
657 | sub 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 | 684 | sub 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 | |
cb06b8dd | 726 | if ($hint && (!$search_string || $found_string)) { |
f34f6729 | 727 | my $what = $leaf{$leave}->{what}; |
ca8e055c MCC |
728 | $what =~ s/\xac/\n\t/g; |
729 | if ($leave ne "others") { | |
730 | print " more likely regexes:\n\t$what\n"; | |
731 | } else { | |
732 | print " tested regexes:\n\t$what\n"; | |
733 | } | |
ab02c515 MCC |
734 | next; |
735 | } | |
14c94257 | 736 | print "$file not found.\n" if (!$search_string || $found_string); |
ab02c515 | 737 | } |
f090db43 MCC |
738 | } |
739 | ||
740 | sub undefined_symbols { | |
ab02c515 MCC |
741 | find({ |
742 | wanted =>\&parse_existing_sysfs, | |
743 | preprocess =>\&dont_parse_special_attributes, | |
744 | no_chdir => 1 | |
745 | }, $sysfs_prefix); | |
746 | ||
f34f6729 | 747 | $leaf{"others"}->{what} = ""; |
ca8e055c | 748 | |
f090db43 | 749 | foreach my $w (sort keys %data) { |
ca8e055c | 750 | foreach my $what (split /\xac/,$w) { |
ab02c515 MCC |
751 | next if (!($what =~ m/^$sysfs_prefix/)); |
752 | ||
753 | # Convert what into regular expressions | |
754 | ||
755 | $what =~ s,/\.\.\./,/*/,g; | |
756 | $what =~ s,\*,.*,g; | |
757 | ||
758 | # Temporarily change [0-9]+ type of patterns | |
759 | $what =~ s/\[0\-9\]\+/\xff/g; | |
760 | ||
761 | # Temporarily change [\d+-\d+] type of patterns | |
762 | $what =~ s/\[0\-\d+\]/\xff/g; | |
763 | $what =~ s/\[(\d+)\]/\xf4$1\xf5/g; | |
764 | ||
765 | # Temporarily change [0-9] type of patterns | |
766 | $what =~ s/\[(\d)\-(\d)\]/\xf4$1-$2\xf5/g; | |
767 | ||
768 | # Handle multiple option patterns | |
769 | $what =~ s/[\{\<\[]([\w_]+)(?:[,|]+([\w_]+)){1,}[\}\>\]]/($1|$2)/g; | |
770 | ||
771 | # Handle wildcards | |
772 | $what =~ s/\<[^\>]+\>/.*/g; | |
773 | $what =~ s/\{[^\}]+\}/.*/g; | |
774 | $what =~ s/\[[^\]]+\]/.*/g; | |
775 | ||
776 | $what =~ s/[XYZ]/.*/g; | |
777 | ||
778 | # Recover [0-9] type of patterns | |
779 | $what =~ s/\xf4/[/g; | |
780 | $what =~ s/\xf5/]/g; | |
781 | ||
782 | # Remove duplicated spaces | |
783 | $what =~ s/\s+/ /g; | |
784 | ||
785 | # Special case: this ABI has a parenthesis on it | |
786 | $what =~ s/sqrt\(x^2\+y^2\+z^2\)/sqrt\(x^2\+y^2\+z^2\)/; | |
787 | ||
788 | # Special case: drop comparition as in: | |
789 | # What: foo = <something> | |
790 | # (this happens on a few IIO definitions) | |
791 | $what =~ s,\s*\=.*$,,; | |
792 | ||
ab02c515 MCC |
793 | # Escape all other symbols |
794 | $what =~ s/$escape_symbols/\\$1/g; | |
795 | $what =~ s/\\\\/\\/g; | |
796 | $what =~ s/\\([\[\]\(\)\|])/$1/g; | |
797 | $what =~ s/(\d+)\\(-\d+)/$1$2/g; | |
798 | ||
14c94257 MCC |
799 | $what =~ s/\xff/\\d+/g; |
800 | ||
14c94257 MCC |
801 | # Special case: IIO ABI which a parenthesis. |
802 | $what =~ s/sqrt(.*)/sqrt\(.*\)/; | |
803 | ||
45495db9 | 804 | my $leave = get_leave($what); |
f34f6729 | 805 | |
14c94257 | 806 | my $added = 0; |
ab02c515 MCC |
807 | foreach my $l (split /\|/, $leave) { |
808 | if (defined($leaf{$l})) { | |
f34f6729 MCC |
809 | next if ($leaf{$l}->{what} =~ m/\b$what\b/); |
810 | $leaf{$l}->{what} .= "\xac" . $what; | |
14c94257 | 811 | $added = 1; |
ab02c515 | 812 | } else { |
f34f6729 | 813 | $leaf{$l}->{what} = $what; |
14c94257 | 814 | $added = 1; |
ab02c515 | 815 | } |
f090db43 | 816 | } |
14c94257 MCC |
817 | if ($search_string && $added) { |
818 | print "What: $what\n" if ($what =~ m#$search_string#); | |
819 | } | |
820 | ||
f090db43 MCC |
821 | } |
822 | } | |
f34f6729 MCC |
823 | # Compile regexes |
824 | foreach my $l (keys %leaf) { | |
825 | my @expr; | |
826 | foreach my $w(split /\xac/, $leaf{$l}->{what}) { | |
827 | push @expr, qr /^$w$/; | |
828 | } | |
829 | $leaf{$l}->{expr} = \@expr; | |
830 | } | |
831 | ||
ca8e055c MCC |
832 | # Take links into account |
833 | foreach my $link (keys %aliases) { | |
834 | my $abs_file = $aliases{$link}; | |
835 | graph_add_link($abs_file, $link); | |
836 | } | |
ab02c515 | 837 | check_undefined_symbols; |
f090db43 MCC |
838 | } |
839 | ||
61439c4a MCC |
840 | # Ensure that the prefix will always end with a slash |
841 | # While this is not needed for find, it makes the patch nicer | |
842 | # with --enable-lineno | |
843 | $prefix =~ s,/?$,/,; | |
33e3e991 | 844 | |
f090db43 MCC |
845 | if ($cmd eq "undefined" || $cmd eq "search") { |
846 | $show_warnings = 0; | |
847 | } | |
bbc249f2 MCC |
848 | # |
849 | # Parses all ABI files located at $prefix dir | |
850 | # | |
851 | find({wanted =>\&parse_abi, no_chdir => 1}, $prefix); | |
852 | ||
46f661fd | 853 | print STDERR Data::Dumper->Dump([\%data], [qw(*data)]) if ($debug & $dbg_dump_abi_structs); |
bbc249f2 MCC |
854 | |
855 | # | |
33e3e991 | 856 | # Handles the command |
bbc249f2 | 857 | # |
f090db43 MCC |
858 | if ($cmd eq "undefined") { |
859 | undefined_symbols; | |
860 | } elsif ($cmd eq "search") { | |
33e3e991 | 861 | search_symbols; |
c7ba3334 MCC |
862 | } else { |
863 | if ($cmd eq "rest") { | |
864 | output_rest; | |
865 | } | |
866 | ||
867 | # Warn about duplicated ABI entries | |
868 | foreach my $what(sort keys %symbols) { | |
869 | my @files = @{$symbols{$what}->{file}}; | |
870 | ||
871 | next if (scalar(@files) == 1); | |
bbc249f2 | 872 | |
c7ba3334 MCC |
873 | printf STDERR "Warning: $what is defined %d times: @files\n", |
874 | scalar(@files); | |
875 | } | |
876 | } | |
bbc249f2 MCC |
877 | |
878 | __END__ | |
879 | ||
880 | =head1 NAME | |
881 | ||
882 | abi_book.pl - parse the Linux ABI files and produce a ReST book. | |
883 | ||
884 | =head1 SYNOPSIS | |
885 | ||
46f661fd | 886 | B<abi_book.pl> [--debug <level>] [--enable-lineno] [--man] [--help] |
ab02c515 | 887 | [--(no-)rst-source] [--dir=<dir>] [--show-hints] |
14c94257 | 888 | [--search-string <regex>] |
ab02c515 | 889 | <COMAND> [<ARGUMENT>] |
33e3e991 MCC |
890 | |
891 | Where <COMMAND> can be: | |
892 | ||
893 | =over 8 | |
894 | ||
895 | B<search> [SEARCH_REGEX] - search for [SEARCH_REGEX] inside ABI | |
896 | ||
7ce7b89b MCC |
897 | B<rest> - output the ABI in ReST markup language |
898 | ||
899 | B<validate> - validate the ABI contents | |
33e3e991 | 900 | |
f090db43 MCC |
901 | B<undefined> - existing symbols at the system that aren't |
902 | defined at Documentation/ABI | |
903 | ||
33e3e991 | 904 | =back |
bbc249f2 MCC |
905 | |
906 | =head1 OPTIONS | |
907 | ||
908 | =over 8 | |
909 | ||
33e3e991 MCC |
910 | =item B<--dir> |
911 | ||
912 | Changes the location of the ABI search. By default, it uses | |
913 | the Documentation/ABI directory. | |
914 | ||
11ce90a4 MCC |
915 | =item B<--rst-source> and B<--no-rst-source> |
916 | ||
917 | The input file may be using ReST syntax or not. Those two options allow | |
918 | selecting between a rst-compliant source ABI (--rst-source), or a | |
919 | plain text that may be violating ReST spec, so it requres some escaping | |
920 | logic (--no-rst-source). | |
921 | ||
61439c4a MCC |
922 | =item B<--enable-lineno> |
923 | ||
924 | Enable output of #define LINENO lines. | |
925 | ||
46f661fd MCC |
926 | =item B<--debug> I<debug level> |
927 | ||
928 | Print debug information according with the level, which is given by the | |
929 | following bitmask: | |
bbc249f2 | 930 | |
46f661fd MCC |
931 | - 1: Debug parsing What entries from ABI files; |
932 | - 2: Shows what files are opened from ABI files; | |
933 | - 4: Dump the structs used to store the contents of the ABI files. | |
bbc249f2 | 934 | |
ab02c515 MCC |
935 | =item B<--show-hints> |
936 | ||
937 | Show hints about possible definitions for the missing ABI symbols. | |
938 | Used only when B<undefined>. | |
939 | ||
14c94257 MCC |
940 | =item B<--search-string> [regex string] |
941 | ||
942 | Show only occurences that match a search string. | |
943 | Used only when B<undefined>. | |
944 | ||
bbc249f2 MCC |
945 | =item B<--help> |
946 | ||
947 | Prints a brief help message and exits. | |
948 | ||
949 | =item B<--man> | |
950 | ||
951 | Prints the manual page and exits. | |
952 | ||
953 | =back | |
954 | ||
955 | =head1 DESCRIPTION | |
956 | ||
33e3e991 MCC |
957 | Parse the Linux ABI files from ABI DIR (usually located at Documentation/ABI), |
958 | allowing to search for ABI symbols or to produce a ReST book containing | |
959 | the Linux ABI documentation. | |
960 | ||
961 | =head1 EXAMPLES | |
962 | ||
963 | Search for all stable symbols with the word "usb": | |
964 | ||
965 | =over 8 | |
966 | ||
967 | $ scripts/get_abi.pl search usb --dir Documentation/ABI/stable | |
968 | ||
969 | =back | |
970 | ||
971 | Search for all symbols that match the regex expression "usb.*cap": | |
972 | ||
973 | =over 8 | |
974 | ||
975 | $ scripts/get_abi.pl search usb.*cap | |
976 | ||
977 | =back | |
978 | ||
979 | Output all obsoleted symbols in ReST format | |
980 | ||
981 | =over 8 | |
982 | ||
983 | $ scripts/get_abi.pl rest --dir Documentation/ABI/obsolete | |
984 | ||
985 | =back | |
bbc249f2 MCC |
986 | |
987 | =head1 BUGS | |
988 | ||
7ce7b89b | 989 | Report bugs to Mauro Carvalho Chehab <mchehab+samsung@kernel.org> |
bbc249f2 MCC |
990 | |
991 | =head1 COPYRIGHT | |
992 | ||
7ce7b89b | 993 | Copyright (c) 2016-2019 by Mauro Carvalho Chehab <mchehab+samsung@kernel.org>. |
bbc249f2 MCC |
994 | |
995 | License GPLv2: GNU GPL version 2 <http://gnu.org/licenses/gpl.html>. | |
996 | ||
997 | This is free software: you are free to change and redistribute it. | |
998 | There is NO WARRANTY, to the extent permitted by law. | |
999 | ||
1000 | =cut |