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