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