Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 1 | #!/usr/bin/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'; |
| 11 | |
Mauro Carvalho Chehab | 234948b | 2020-10-30 08:40:24 +0100 | [diff] [blame] | 12 | my $help = 0; |
| 13 | my $man = 0; |
| 14 | my $debug = 0; |
| 15 | my $enable_lineno = 0; |
Mauro Carvalho Chehab | 33e3e99 | 2019-06-20 14:22:59 -0300 | [diff] [blame] | 16 | my $prefix="Documentation/ABI"; |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 17 | |
Mauro Carvalho Chehab | 11ce90a | 2020-10-30 08:40:20 +0100 | [diff] [blame] | 18 | # |
| 19 | # If true, assumes that the description is formatted with ReST |
| 20 | # |
Mauro Carvalho Chehab | 2fcce37 | 2020-10-30 08:40:58 +0100 | [diff] [blame] | 21 | my $description_is_rst = 1; |
Mauro Carvalho Chehab | 11ce90a | 2020-10-30 08:40:20 +0100 | [diff] [blame] | 22 | |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 23 | GetOptions( |
| 24 | "debug|d+" => \$debug, |
Mauro Carvalho Chehab | 61439c4 | 2020-10-30 08:40:22 +0100 | [diff] [blame] | 25 | "enable-lineno" => \$enable_lineno, |
Mauro Carvalho Chehab | 11ce90a | 2020-10-30 08:40:20 +0100 | [diff] [blame] | 26 | "rst-source!" => \$description_is_rst, |
Mauro Carvalho Chehab | 33e3e99 | 2019-06-20 14:22:59 -0300 | [diff] [blame] | 27 | "dir=s" => \$prefix, |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 28 | 'help|?' => \$help, |
| 29 | man => \$man |
| 30 | ) or pod2usage(2); |
| 31 | |
| 32 | pod2usage(1) if $help; |
| 33 | pod2usage(-exitstatus => 0, -verbose => 2) if $man; |
| 34 | |
Mauro Carvalho Chehab | 33e3e99 | 2019-06-20 14:22:59 -0300 | [diff] [blame] | 35 | pod2usage(2) if (scalar @ARGV < 1 || @ARGV > 2); |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 36 | |
Mauro Carvalho Chehab | 33e3e99 | 2019-06-20 14:22:59 -0300 | [diff] [blame] | 37 | my ($cmd, $arg) = @ARGV; |
| 38 | |
Mauro Carvalho Chehab | 7ce7b89 | 2019-06-20 14:23:04 -0300 | [diff] [blame] | 39 | pod2usage(2) if ($cmd ne "search" && $cmd ne "rest" && $cmd ne "validate"); |
Mauro Carvalho Chehab | 33e3e99 | 2019-06-20 14:22:59 -0300 | [diff] [blame] | 40 | pod2usage(2) if ($cmd eq "search" && !$arg); |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 41 | |
| 42 | require Data::Dumper if ($debug); |
| 43 | |
| 44 | my %data; |
Mauro Carvalho Chehab | 234948b | 2020-10-30 08:40:24 +0100 | [diff] [blame] | 45 | my %symbols; |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 46 | |
| 47 | # |
| 48 | # Displays an error message, printing file name and line |
| 49 | # |
| 50 | sub parse_error($$$$) { |
| 51 | my ($file, $ln, $msg, $data) = @_; |
| 52 | |
Mauro Carvalho Chehab | 75442fb | 2020-10-30 08:40:45 +0100 | [diff] [blame] | 53 | $data =~ s/\s+$/\n/; |
| 54 | |
| 55 | print STDERR "Warning: file $file#$ln:\n\t$msg"; |
| 56 | |
| 57 | if ($data ne "") { |
| 58 | print STDERR ". Line\n\t\t$data"; |
| 59 | } else { |
| 60 | print STDERR "\n"; |
| 61 | } |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | # |
| 65 | # Parse an ABI file, storing its contents at %data |
| 66 | # |
| 67 | sub parse_abi { |
| 68 | my $file = $File::Find::name; |
| 69 | |
| 70 | my $mode = (stat($file))[2]; |
| 71 | return if ($mode & S_IFDIR); |
| 72 | return if ($file =~ m,/README,); |
| 73 | |
| 74 | my $name = $file; |
| 75 | $name =~ s,.*/,,; |
| 76 | |
Mauro Carvalho Chehab | a4ea67b | 2020-10-30 08:40:27 +0100 | [diff] [blame] | 77 | my $fn = $file; |
| 78 | $fn =~ s,Documentation/ABI/,,; |
| 79 | |
| 80 | my $nametag = "File $fn"; |
Mauro Carvalho Chehab | d0ebaf5 | 2019-06-20 14:22:58 -0300 | [diff] [blame] | 81 | $data{$nametag}->{what} = "File $name"; |
| 82 | $data{$nametag}->{type} = "File"; |
| 83 | $data{$nametag}->{file} = $name; |
Mauro Carvalho Chehab | 33e3e99 | 2019-06-20 14:22:59 -0300 | [diff] [blame] | 84 | $data{$nametag}->{filepath} = $file; |
Mauro Carvalho Chehab | d0ebaf5 | 2019-06-20 14:22:58 -0300 | [diff] [blame] | 85 | $data{$nametag}->{is_file} = 1; |
Mauro Carvalho Chehab | 61439c4 | 2020-10-30 08:40:22 +0100 | [diff] [blame] | 86 | $data{$nametag}->{line_no} = 1; |
Mauro Carvalho Chehab | d0ebaf5 | 2019-06-20 14:22:58 -0300 | [diff] [blame] | 87 | |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 88 | my $type = $file; |
| 89 | $type =~ s,.*/(.*)/.*,$1,; |
| 90 | |
| 91 | my $what; |
| 92 | my $new_what; |
Mauro Carvalho Chehab | 234948b | 2020-10-30 08:40:24 +0100 | [diff] [blame] | 93 | my $tag = ""; |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 94 | my $ln; |
Mauro Carvalho Chehab | 6619c66 | 2019-06-20 14:22:56 -0300 | [diff] [blame] | 95 | my $xrefs; |
Mauro Carvalho Chehab | 4e6a623 | 2019-06-20 14:22:57 -0300 | [diff] [blame] | 96 | my $space; |
Mauro Carvalho Chehab | d0ebaf5 | 2019-06-20 14:22:58 -0300 | [diff] [blame] | 97 | my @labels; |
Mauro Carvalho Chehab | 234948b | 2020-10-30 08:40:24 +0100 | [diff] [blame] | 98 | my $label = ""; |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 99 | |
| 100 | print STDERR "Opening $file\n" if ($debug > 1); |
| 101 | open IN, $file; |
| 102 | while(<IN>) { |
| 103 | $ln++; |
Mauro Carvalho Chehab | 4e6a623 | 2019-06-20 14:22:57 -0300 | [diff] [blame] | 104 | if (m/^(\S+)(:\s*)(.*)/i) { |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 105 | my $new_tag = lc($1); |
Mauro Carvalho Chehab | 4e6a623 | 2019-06-20 14:22:57 -0300 | [diff] [blame] | 106 | my $sep = $2; |
| 107 | my $content = $3; |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 108 | |
Mauro Carvalho Chehab | 7ce7b89 | 2019-06-20 14:23:04 -0300 | [diff] [blame] | 109 | if (!($new_tag =~ m/(what|where|date|kernelversion|contact|description|users)/)) { |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 110 | if ($tag eq "description") { |
Mauro Carvalho Chehab | 4e6a623 | 2019-06-20 14:22:57 -0300 | [diff] [blame] | 111 | # New "tag" is actually part of |
| 112 | # description. Don't consider it a tag |
| 113 | $new_tag = ""; |
Mauro Carvalho Chehab | 7d7ea8d | 2019-06-20 14:23:01 -0300 | [diff] [blame] | 114 | } elsif ($tag ne "") { |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 115 | parse_error($file, $ln, "tag '$tag' is invalid", $_); |
| 116 | } |
| 117 | } |
| 118 | |
Mauro Carvalho Chehab | 2c0700e | 2019-06-20 14:23:03 -0300 | [diff] [blame] | 119 | # Invalid, but it is a common mistake |
| 120 | if ($new_tag eq "where") { |
Mauro Carvalho Chehab | 75442fb | 2020-10-30 08:40:45 +0100 | [diff] [blame] | 121 | 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] | 122 | $new_tag = "what"; |
| 123 | } |
| 124 | |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 125 | if ($new_tag =~ m/what/) { |
Mauro Carvalho Chehab | 4e6a623 | 2019-06-20 14:22:57 -0300 | [diff] [blame] | 126 | $space = ""; |
Mauro Carvalho Chehab | 234948b | 2020-10-30 08:40:24 +0100 | [diff] [blame] | 127 | $content =~ s/[,.;]$//; |
| 128 | |
Mauro Carvalho Chehab | c7ba333 | 2020-10-30 08:40:25 +0100 | [diff] [blame] | 129 | push @{$symbols{$content}->{file}}, " $file:" . ($ln - 1); |
| 130 | |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 131 | if ($tag =~ m/what/) { |
| 132 | $what .= ", " . $content; |
| 133 | } else { |
Mauro Carvalho Chehab | 234948b | 2020-10-30 08:40:24 +0100 | [diff] [blame] | 134 | if ($what) { |
| 135 | parse_error($file, $ln, "What '$what' doesn't have a description", "") if (!$data{$what}->{description}); |
| 136 | |
| 137 | foreach my $w(split /, /, $what) { |
Mauro Carvalho Chehab | c7ba333 | 2020-10-30 08:40:25 +0100 | [diff] [blame] | 138 | $symbols{$w}->{xref} = $what; |
Mauro Carvalho Chehab | 234948b | 2020-10-30 08:40:24 +0100 | [diff] [blame] | 139 | }; |
| 140 | } |
Mauro Carvalho Chehab | 4e6a623 | 2019-06-20 14:22:57 -0300 | [diff] [blame] | 141 | |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 142 | $what = $content; |
Mauro Carvalho Chehab | d0ebaf5 | 2019-06-20 14:22:58 -0300 | [diff] [blame] | 143 | $label = $content; |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 144 | $new_what = 1; |
| 145 | } |
Mauro Carvalho Chehab | d0ebaf5 | 2019-06-20 14:22:58 -0300 | [diff] [blame] | 146 | push @labels, [($content, $label)]; |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 147 | $tag = $new_tag; |
Mauro Carvalho Chehab | 6619c66 | 2019-06-20 14:22:56 -0300 | [diff] [blame] | 148 | |
Mauro Carvalho Chehab | 234948b | 2020-10-30 08:40:24 +0100 | [diff] [blame] | 149 | push @{$data{$nametag}->{symbols}}, $content if ($data{$nametag}->{what}); |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 150 | next; |
| 151 | } |
| 152 | |
Mauro Carvalho Chehab | 7d7ea8d | 2019-06-20 14:23:01 -0300 | [diff] [blame] | 153 | if ($tag ne "" && $new_tag) { |
Mauro Carvalho Chehab | 4e6a623 | 2019-06-20 14:22:57 -0300 | [diff] [blame] | 154 | $tag = $new_tag; |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 155 | |
Mauro Carvalho Chehab | 4e6a623 | 2019-06-20 14:22:57 -0300 | [diff] [blame] | 156 | if ($new_what) { |
Mauro Carvalho Chehab | 234948b | 2020-10-30 08:40:24 +0100 | [diff] [blame] | 157 | @{$data{$what}->{label_list}} = @labels if ($data{$nametag}->{what}); |
Mauro Carvalho Chehab | d0ebaf5 | 2019-06-20 14:22:58 -0300 | [diff] [blame] | 158 | @labels = (); |
| 159 | $label = ""; |
Mauro Carvalho Chehab | 4e6a623 | 2019-06-20 14:22:57 -0300 | [diff] [blame] | 160 | $new_what = 0; |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 161 | |
Mauro Carvalho Chehab | 4e6a623 | 2019-06-20 14:22:57 -0300 | [diff] [blame] | 162 | $data{$what}->{type} = $type; |
Mauro Carvalho Chehab | c7ba333 | 2020-10-30 08:40:25 +0100 | [diff] [blame] | 163 | if (!defined($data{$what}->{file})) { |
| 164 | $data{$what}->{file} = $name; |
| 165 | $data{$what}->{filepath} = $file; |
| 166 | } else { |
| 167 | if ($name ne $data{$what}->{file}) { |
| 168 | $data{$what}->{file} .= " " . $name; |
| 169 | $data{$what}->{filepath} .= " " . $file; |
| 170 | } |
| 171 | } |
Mauro Carvalho Chehab | 4e6a623 | 2019-06-20 14:22:57 -0300 | [diff] [blame] | 172 | print STDERR "\twhat: $what\n" if ($debug > 1); |
Mauro Carvalho Chehab | c7ba333 | 2020-10-30 08:40:25 +0100 | [diff] [blame] | 173 | $data{$what}->{line_no} = $ln; |
| 174 | } else { |
| 175 | $data{$what}->{line_no} = $ln if (!defined($data{$what}->{line_no})); |
Mauro Carvalho Chehab | 4e6a623 | 2019-06-20 14:22:57 -0300 | [diff] [blame] | 176 | } |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 177 | |
Mauro Carvalho Chehab | 4e6a623 | 2019-06-20 14:22:57 -0300 | [diff] [blame] | 178 | if (!$what) { |
| 179 | parse_error($file, $ln, "'What:' should come first:", $_); |
| 180 | next; |
| 181 | } |
Mauro Carvalho Chehab | f82a8a7 | 2020-10-30 08:40:23 +0100 | [diff] [blame] | 182 | if ($new_tag eq "description") { |
| 183 | $sep =~ s,:, ,; |
Mauro Carvalho Chehab | 11ce90a | 2020-10-30 08:40:20 +0100 | [diff] [blame] | 184 | $content = ' ' x length($new_tag) . $sep . $content; |
Mauro Carvalho Chehab | f82a8a7 | 2020-10-30 08:40:23 +0100 | [diff] [blame] | 185 | while ($content =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {} |
| 186 | if ($content =~ m/^(\s*)(\S.*)$/) { |
| 187 | # Preserve initial spaces for the first line |
Mauro Carvalho Chehab | 11ce90a | 2020-10-30 08:40:20 +0100 | [diff] [blame] | 188 | $space = $1; |
Mauro Carvalho Chehab | f82a8a7 | 2020-10-30 08:40:23 +0100 | [diff] [blame] | 189 | $content = "$2\n"; |
| 190 | $data{$what}->{$tag} .= $content; |
| 191 | } else { |
| 192 | undef($space); |
Mauro Carvalho Chehab | 4e6a623 | 2019-06-20 14:22:57 -0300 | [diff] [blame] | 193 | } |
Mauro Carvalho Chehab | e9bca89 | 2020-10-30 08:40:21 +0100 | [diff] [blame] | 194 | |
Mauro Carvalho Chehab | 4e6a623 | 2019-06-20 14:22:57 -0300 | [diff] [blame] | 195 | } else { |
| 196 | $data{$what}->{$tag} = $content; |
| 197 | } |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 198 | next; |
| 199 | } |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 200 | } |
| 201 | |
Mauro Carvalho Chehab | 4e6a623 | 2019-06-20 14:22:57 -0300 | [diff] [blame] | 202 | # Store any contents before tags at the database |
Mauro Carvalho Chehab | d0ebaf5 | 2019-06-20 14:22:58 -0300 | [diff] [blame] | 203 | if (!$tag && $data{$nametag}->{what}) { |
| 204 | $data{$nametag}->{description} .= $_; |
Mauro Carvalho Chehab | 6619c66 | 2019-06-20 14:22:56 -0300 | [diff] [blame] | 205 | next; |
| 206 | } |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 207 | |
Mauro Carvalho Chehab | 4e6a623 | 2019-06-20 14:22:57 -0300 | [diff] [blame] | 208 | if ($tag eq "description") { |
Mauro Carvalho Chehab | e9bca89 | 2020-10-30 08:40:21 +0100 | [diff] [blame] | 209 | my $content = $_; |
| 210 | while ($content =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {} |
Mauro Carvalho Chehab | f82a8a7 | 2020-10-30 08:40:23 +0100 | [diff] [blame] | 211 | if (m/^\s*\n/) { |
| 212 | $data{$what}->{$tag} .= "\n"; |
| 213 | next; |
Mauro Carvalho Chehab | 4e6a623 | 2019-06-20 14:22:57 -0300 | [diff] [blame] | 214 | } |
Mauro Carvalho Chehab | f82a8a7 | 2020-10-30 08:40:23 +0100 | [diff] [blame] | 215 | |
| 216 | if (!defined($space)) { |
| 217 | # Preserve initial spaces for the first line |
| 218 | if ($content =~ m/^(\s*)(\S.*)$/) { |
| 219 | $space = $1; |
| 220 | $content = "$2\n"; |
| 221 | } |
| 222 | } else { |
| 223 | $space = "" if (!($content =~ s/^($space)//)); |
| 224 | } |
| 225 | $data{$what}->{$tag} .= $content; |
| 226 | |
Mauro Carvalho Chehab | 4e6a623 | 2019-06-20 14:22:57 -0300 | [diff] [blame] | 227 | next; |
| 228 | } |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 229 | if (m/^\s*(.*)/) { |
| 230 | $data{$what}->{$tag} .= "\n$1"; |
| 231 | $data{$what}->{$tag} =~ s/\n+$//; |
| 232 | next; |
| 233 | } |
| 234 | |
| 235 | # Everything else is error |
Mauro Carvalho Chehab | 75442fb | 2020-10-30 08:40:45 +0100 | [diff] [blame] | 236 | parse_error($file, $ln, "Unexpected content", $_); |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 237 | } |
Mauro Carvalho Chehab | 234948b | 2020-10-30 08:40:24 +0100 | [diff] [blame] | 238 | $data{$nametag}->{description} =~ s/^\n+// if ($data{$nametag}->{description}); |
| 239 | if ($what) { |
| 240 | parse_error($file, $ln, "What '$what' doesn't have a description", "") if (!$data{$what}->{description}); |
| 241 | |
| 242 | foreach my $w(split /, /,$what) { |
Mauro Carvalho Chehab | c7ba333 | 2020-10-30 08:40:25 +0100 | [diff] [blame] | 243 | $symbols{$w}->{xref} = $what; |
Mauro Carvalho Chehab | 234948b | 2020-10-30 08:40:24 +0100 | [diff] [blame] | 244 | }; |
| 245 | } |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 246 | close IN; |
| 247 | } |
| 248 | |
Mauro Carvalho Chehab | 234948b | 2020-10-30 08:40:24 +0100 | [diff] [blame] | 249 | sub create_labels { |
| 250 | my %labels; |
| 251 | |
| 252 | foreach my $what (keys %data) { |
| 253 | next if ($data{$what}->{file} eq "File"); |
| 254 | |
| 255 | foreach my $p (@{$data{$what}->{label_list}}) { |
| 256 | my ($content, $label) = @{$p}; |
| 257 | $label = "abi_" . $label . " "; |
| 258 | $label =~ tr/A-Z/a-z/; |
| 259 | |
| 260 | # Convert special chars to "_" |
| 261 | $label =~s/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\xff])/_/g; |
| 262 | $label =~ s,_+,_,g; |
| 263 | $label =~ s,_$,,; |
| 264 | |
| 265 | # Avoid duplicated labels |
| 266 | while (defined($labels{$label})) { |
| 267 | my @chars = ("A".."Z", "a".."z"); |
| 268 | $label .= $chars[rand @chars]; |
| 269 | } |
| 270 | $labels{$label} = 1; |
| 271 | |
| 272 | $data{$what}->{label} = $label; |
| 273 | |
| 274 | # only one label is enough |
| 275 | last; |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
Mauro Carvalho Chehab | 33e3e99 | 2019-06-20 14:22:59 -0300 | [diff] [blame] | 280 | # |
| 281 | # Outputs the book on ReST format |
| 282 | # |
Mauro Carvalho Chehab | 45f9651 | 2019-06-20 14:23:00 -0300 | [diff] [blame] | 283 | |
Mauro Carvalho Chehab | 55e5414 | 2020-10-30 08:40:29 +0100 | [diff] [blame] | 284 | # \b doesn't work well with paths. So, we need to define something else |
| 285 | my $bondary = qr { (?<![\w\/\`\{])(?=[\w\/\`\{])|(?<=[\w\/\`\{])(?![\w\/\`\{]) }x; |
| 286 | |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 287 | sub output_rest { |
Mauro Carvalho Chehab | 234948b | 2020-10-30 08:40:24 +0100 | [diff] [blame] | 288 | create_labels(); |
| 289 | |
Mauro Carvalho Chehab | 45f9651 | 2019-06-20 14:23:00 -0300 | [diff] [blame] | 290 | foreach my $what (sort { |
| 291 | ($data{$a}->{type} eq "File") cmp ($data{$b}->{type} eq "File") || |
| 292 | $a cmp $b |
| 293 | } keys %data) { |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 294 | my $type = $data{$what}->{type}; |
Mauro Carvalho Chehab | c7ba333 | 2020-10-30 08:40:25 +0100 | [diff] [blame] | 295 | |
| 296 | my @file = split / /, $data{$what}->{file}; |
| 297 | my @filepath = split / /, $data{$what}->{filepath}; |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 298 | |
Mauro Carvalho Chehab | 61439c4 | 2020-10-30 08:40:22 +0100 | [diff] [blame] | 299 | if ($enable_lineno) { |
| 300 | printf "#define LINENO %s%s#%s\n\n", |
Mauro Carvalho Chehab | c7ba333 | 2020-10-30 08:40:25 +0100 | [diff] [blame] | 301 | $prefix, $file[0], |
Mauro Carvalho Chehab | 61439c4 | 2020-10-30 08:40:22 +0100 | [diff] [blame] | 302 | $data{$what}->{line_no}; |
| 303 | } |
| 304 | |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 305 | my $w = $what; |
| 306 | $w =~ s/([\(\)\_\-\*\=\^\~\\])/\\$1/g; |
| 307 | |
Mauro Carvalho Chehab | c7ba333 | 2020-10-30 08:40:25 +0100 | [diff] [blame] | 308 | if ($type ne "File") { |
Mauro Carvalho Chehab | 234948b | 2020-10-30 08:40:24 +0100 | [diff] [blame] | 309 | printf ".. _%s:\n\n", $data{$what}->{label}; |
Mauro Carvalho Chehab | 45f9651 | 2019-06-20 14:23:00 -0300 | [diff] [blame] | 310 | |
Mauro Carvalho Chehab | 234948b | 2020-10-30 08:40:24 +0100 | [diff] [blame] | 311 | my @names = split /, /,$w; |
Mauro Carvalho Chehab | 45f9651 | 2019-06-20 14:23:00 -0300 | [diff] [blame] | 312 | my $len = 0; |
| 313 | |
| 314 | foreach my $name (@names) { |
Mauro Carvalho Chehab | c01d62d | 2020-10-30 08:40:28 +0100 | [diff] [blame] | 315 | $name = "**$name**"; |
Mauro Carvalho Chehab | 45f9651 | 2019-06-20 14:23:00 -0300 | [diff] [blame] | 316 | $len = length($name) if (length($name) > $len); |
| 317 | } |
| 318 | |
Mauro Carvalho Chehab | 45f9651 | 2019-06-20 14:23:00 -0300 | [diff] [blame] | 319 | print "+-" . "-" x $len . "-+\n"; |
| 320 | foreach my $name (@names) { |
| 321 | printf "| %s", $name . " " x ($len - length($name)) . " |\n"; |
| 322 | print "+-" . "-" x $len . "-+\n"; |
| 323 | } |
Mauro Carvalho Chehab | 234948b | 2020-10-30 08:40:24 +0100 | [diff] [blame] | 324 | |
Mauro Carvalho Chehab | c7ba333 | 2020-10-30 08:40:25 +0100 | [diff] [blame] | 325 | print "\n"; |
| 326 | } |
| 327 | |
| 328 | for (my $i = 0; $i < scalar(@filepath); $i++) { |
| 329 | my $path = $filepath[$i]; |
| 330 | my $f = $file[$i]; |
| 331 | |
| 332 | $path =~ s,.*/(.*/.*),$1,;; |
| 333 | $path =~ s,[/\-],_,g;; |
| 334 | my $fileref = "abi_file_".$path; |
| 335 | |
| 336 | if ($type eq "File") { |
Mauro Carvalho Chehab | c7ba333 | 2020-10-30 08:40:25 +0100 | [diff] [blame] | 337 | print ".. _$fileref:\n\n"; |
Mauro Carvalho Chehab | c7ba333 | 2020-10-30 08:40:25 +0100 | [diff] [blame] | 338 | } else { |
| 339 | print "Defined on file :ref:`$f <$fileref>`\n\n"; |
| 340 | } |
Mauro Carvalho Chehab | 45f9651 | 2019-06-20 14:23:00 -0300 | [diff] [blame] | 341 | } |
| 342 | |
Mauro Carvalho Chehab | a4ea67b | 2020-10-30 08:40:27 +0100 | [diff] [blame] | 343 | if ($type eq "File") { |
| 344 | my $bar = $w; |
| 345 | $bar =~ s/./-/g; |
| 346 | print "$w\n$bar\n\n"; |
| 347 | } |
| 348 | |
Mauro Carvalho Chehab | 234948b | 2020-10-30 08:40:24 +0100 | [diff] [blame] | 349 | my $desc = ""; |
| 350 | $desc = $data{$what}->{description} if (defined($data{$what}->{description})); |
| 351 | $desc =~ s/\s+$/\n/; |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 352 | |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 353 | if (!($desc =~ /^\s*$/)) { |
Mauro Carvalho Chehab | 11ce90a | 2020-10-30 08:40:20 +0100 | [diff] [blame] | 354 | if ($description_is_rst) { |
Mauro Carvalho Chehab | daaaf58 | 2020-11-02 11:32:15 +0100 | [diff] [blame^] | 355 | # Remove title markups from the description |
| 356 | # Having titles inside ABI files will only work if extra |
| 357 | # care would be taken in order to strictly follow the same |
| 358 | # level order for each markup. |
| 359 | $desc =~ s/\n[\-\*\=\^\~]+\n/\n\n/g; |
| 360 | |
Mauro Carvalho Chehab | 55e5414 | 2020-10-30 08:40:29 +0100 | [diff] [blame] | 361 | # Enrich text by creating cross-references |
| 362 | |
| 363 | $desc =~ s,Documentation/(?!devicetree)(\S+)\.rst,:doc:`/$1`,g; |
| 364 | |
| 365 | my @matches = $desc =~ m,Documentation/ABI/([\w\/\-]+),; |
| 366 | foreach my $f (@matches) { |
| 367 | my $xref = $f; |
| 368 | my $path = $f; |
| 369 | $path =~ s,.*/(.*/.*),$1,;; |
| 370 | $path =~ s,[/\-],_,g;; |
| 371 | $xref .= " <abi_file_" . $path . ">"; |
| 372 | $desc =~ s,\bDocumentation/ABI/$f\b,:ref:`$xref`,g; |
| 373 | } |
| 374 | |
| 375 | @matches = $desc =~ m,$bondary(/sys/[^\s\.\,\;\:\*\s\`\'\(\)]+)$bondary,; |
| 376 | |
| 377 | foreach my $s (@matches) { |
| 378 | if (defined($data{$s}) && defined($data{$s}->{label})) { |
| 379 | my $xref = $s; |
| 380 | |
| 381 | $xref =~ s/([\x00-\x1f\x21-\x2f\x3a-\x40\x7b-\xff])/\\$1/g; |
| 382 | $xref = ":ref:`$xref <" . $data{$s}->{label} . ">`"; |
| 383 | |
| 384 | $desc =~ s,$bondary$s$bondary,$xref,g; |
| 385 | } |
| 386 | } |
| 387 | |
Mauro Carvalho Chehab | 4e6a623 | 2019-06-20 14:22:57 -0300 | [diff] [blame] | 388 | print "$desc\n\n"; |
Mauro Carvalho Chehab | 11ce90a | 2020-10-30 08:40:20 +0100 | [diff] [blame] | 389 | } else { |
| 390 | $desc =~ s/^\s+//; |
| 391 | |
| 392 | # Remove title markups from the description, as they won't work |
| 393 | $desc =~ s/\n[\-\*\=\^\~]+\n/\n\n/g; |
| 394 | |
| 395 | if ($desc =~ m/\:\n/ || $desc =~ m/\n[\t ]+/ || $desc =~ m/[\x00-\x08\x0b-\x1f\x7b-\xff]/) { |
| 396 | # put everything inside a code block |
| 397 | $desc =~ s/\n/\n /g; |
| 398 | |
| 399 | print "::\n\n"; |
| 400 | print " $desc\n\n"; |
| 401 | } else { |
| 402 | # Escape any special chars from description |
| 403 | $desc =~s/([\x00-\x08\x0b-\x1f\x21-\x2a\x2d\x2f\x3c-\x40\x5c\x5e-\x60\x7b-\xff])/\\$1/g; |
| 404 | print "$desc\n\n"; |
| 405 | } |
Mauro Carvalho Chehab | 4e6a623 | 2019-06-20 14:22:57 -0300 | [diff] [blame] | 406 | } |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 407 | } else { |
Mauro Carvalho Chehab | d0ebaf5 | 2019-06-20 14:22:58 -0300 | [diff] [blame] | 408 | 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] | 409 | } |
Mauro Carvalho Chehab | 6619c66 | 2019-06-20 14:22:56 -0300 | [diff] [blame] | 410 | |
Mauro Carvalho Chehab | 234948b | 2020-10-30 08:40:24 +0100 | [diff] [blame] | 411 | if ($data{$what}->{symbols}) { |
Mauro Carvalho Chehab | d0ebaf5 | 2019-06-20 14:22:58 -0300 | [diff] [blame] | 412 | printf "Has the following ABI:\n\n"; |
| 413 | |
Mauro Carvalho Chehab | 234948b | 2020-10-30 08:40:24 +0100 | [diff] [blame] | 414 | foreach my $content(@{$data{$what}->{symbols}}) { |
Mauro Carvalho Chehab | c7ba333 | 2020-10-30 08:40:25 +0100 | [diff] [blame] | 415 | my $label = $data{$symbols{$content}->{xref}}->{label}; |
Mauro Carvalho Chehab | d0ebaf5 | 2019-06-20 14:22:58 -0300 | [diff] [blame] | 416 | |
| 417 | # Escape special chars from content |
| 418 | $content =~s/([\x00-\x1f\x21-\x2f\x3a-\x40\x7b-\xff])/\\$1/g; |
| 419 | |
| 420 | print "- :ref:`$content <$label>`\n\n"; |
| 421 | } |
| 422 | } |
Mauro Carvalho Chehab | a16ab14 | 2020-10-30 08:40:26 +0100 | [diff] [blame] | 423 | |
| 424 | if (defined($data{$what}->{users})) { |
| 425 | my $users = $data{$what}->{users}; |
| 426 | |
| 427 | $users =~ s/\n/\n\t/g; |
| 428 | printf "Users:\n\t%s\n\n", $users if ($users ne ""); |
| 429 | } |
| 430 | |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
| 434 | # |
Mauro Carvalho Chehab | 33e3e99 | 2019-06-20 14:22:59 -0300 | [diff] [blame] | 435 | # Searches for ABI symbols |
| 436 | # |
| 437 | sub search_symbols { |
| 438 | foreach my $what (sort keys %data) { |
| 439 | next if (!($what =~ m/($arg)/)); |
| 440 | |
| 441 | my $type = $data{$what}->{type}; |
| 442 | next if ($type eq "File"); |
| 443 | |
| 444 | my $file = $data{$what}->{filepath}; |
| 445 | |
| 446 | my $bar = $what; |
| 447 | $bar =~ s/./-/g; |
| 448 | |
| 449 | print "\n$what\n$bar\n\n"; |
| 450 | |
Mauro Carvalho Chehab | 234948b | 2020-10-30 08:40:24 +0100 | [diff] [blame] | 451 | my $kernelversion = $data{$what}->{kernelversion} if (defined($data{$what}->{kernelversion})); |
| 452 | my $contact = $data{$what}->{contact} if (defined($data{$what}->{contact})); |
| 453 | my $users = $data{$what}->{users} if (defined($data{$what}->{users})); |
| 454 | my $date = $data{$what}->{date} if (defined($data{$what}->{date})); |
| 455 | my $desc = $data{$what}->{description} if (defined($data{$what}->{description})); |
| 456 | |
| 457 | $kernelversion =~ s/^\s+// if ($kernelversion); |
| 458 | $contact =~ s/^\s+// if ($contact); |
| 459 | if ($users) { |
| 460 | $users =~ s/^\s+//; |
| 461 | $users =~ s/\n//g; |
| 462 | } |
| 463 | $date =~ s/^\s+// if ($date); |
| 464 | $desc =~ s/^\s+// if ($desc); |
Mauro Carvalho Chehab | 33e3e99 | 2019-06-20 14:22:59 -0300 | [diff] [blame] | 465 | |
| 466 | printf "Kernel version:\t\t%s\n", $kernelversion if ($kernelversion); |
| 467 | printf "Date:\t\t\t%s\n", $date if ($date); |
| 468 | printf "Contact:\t\t%s\n", $contact if ($contact); |
| 469 | printf "Users:\t\t\t%s\n", $users if ($users); |
Mauro Carvalho Chehab | c7ba333 | 2020-10-30 08:40:25 +0100 | [diff] [blame] | 470 | print "Defined on file(s):\t$file\n\n"; |
Mauro Carvalho Chehab | 33e3e99 | 2019-06-20 14:22:59 -0300 | [diff] [blame] | 471 | print "Description:\n\n$desc"; |
| 472 | } |
| 473 | } |
| 474 | |
Mauro Carvalho Chehab | 61439c4 | 2020-10-30 08:40:22 +0100 | [diff] [blame] | 475 | # Ensure that the prefix will always end with a slash |
| 476 | # While this is not needed for find, it makes the patch nicer |
| 477 | # with --enable-lineno |
| 478 | $prefix =~ s,/?$,/,; |
Mauro Carvalho Chehab | 33e3e99 | 2019-06-20 14:22:59 -0300 | [diff] [blame] | 479 | |
| 480 | # |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 481 | # Parses all ABI files located at $prefix dir |
| 482 | # |
| 483 | find({wanted =>\&parse_abi, no_chdir => 1}, $prefix); |
| 484 | |
| 485 | print STDERR Data::Dumper->Dump([\%data], [qw(*data)]) if ($debug); |
| 486 | |
| 487 | # |
Mauro Carvalho Chehab | 33e3e99 | 2019-06-20 14:22:59 -0300 | [diff] [blame] | 488 | # Handles the command |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 489 | # |
Mauro Carvalho Chehab | c7ba333 | 2020-10-30 08:40:25 +0100 | [diff] [blame] | 490 | if ($cmd eq "search") { |
Mauro Carvalho Chehab | 33e3e99 | 2019-06-20 14:22:59 -0300 | [diff] [blame] | 491 | search_symbols; |
Mauro Carvalho Chehab | c7ba333 | 2020-10-30 08:40:25 +0100 | [diff] [blame] | 492 | } else { |
| 493 | if ($cmd eq "rest") { |
| 494 | output_rest; |
| 495 | } |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 496 | |
Mauro Carvalho Chehab | c7ba333 | 2020-10-30 08:40:25 +0100 | [diff] [blame] | 497 | # Warn about duplicated ABI entries |
| 498 | foreach my $what(sort keys %symbols) { |
| 499 | my @files = @{$symbols{$what}->{file}}; |
| 500 | |
| 501 | next if (scalar(@files) == 1); |
| 502 | |
| 503 | printf STDERR "Warning: $what is defined %d times: @files\n", |
| 504 | scalar(@files); |
| 505 | } |
| 506 | } |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 507 | |
| 508 | __END__ |
| 509 | |
| 510 | =head1 NAME |
| 511 | |
| 512 | abi_book.pl - parse the Linux ABI files and produce a ReST book. |
| 513 | |
| 514 | =head1 SYNOPSIS |
| 515 | |
Mauro Carvalho Chehab | 61439c4 | 2020-10-30 08:40:22 +0100 | [diff] [blame] | 516 | B<abi_book.pl> [--debug] [--enable-lineno] [--man] [--help] |
| 517 | [--(no-)rst-source] [--dir=<dir>] <COMAND> [<ARGUMENT>] |
Mauro Carvalho Chehab | 33e3e99 | 2019-06-20 14:22:59 -0300 | [diff] [blame] | 518 | |
| 519 | Where <COMMAND> can be: |
| 520 | |
| 521 | =over 8 |
| 522 | |
| 523 | B<search> [SEARCH_REGEX] - search for [SEARCH_REGEX] inside ABI |
| 524 | |
Mauro Carvalho Chehab | 7ce7b89 | 2019-06-20 14:23:04 -0300 | [diff] [blame] | 525 | B<rest> - output the ABI in ReST markup language |
| 526 | |
| 527 | B<validate> - validate the ABI contents |
Mauro Carvalho Chehab | 33e3e99 | 2019-06-20 14:22:59 -0300 | [diff] [blame] | 528 | |
| 529 | =back |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 530 | |
| 531 | =head1 OPTIONS |
| 532 | |
| 533 | =over 8 |
| 534 | |
Mauro Carvalho Chehab | 33e3e99 | 2019-06-20 14:22:59 -0300 | [diff] [blame] | 535 | =item B<--dir> |
| 536 | |
| 537 | Changes the location of the ABI search. By default, it uses |
| 538 | the Documentation/ABI directory. |
| 539 | |
Mauro Carvalho Chehab | 11ce90a | 2020-10-30 08:40:20 +0100 | [diff] [blame] | 540 | =item B<--rst-source> and B<--no-rst-source> |
| 541 | |
| 542 | The input file may be using ReST syntax or not. Those two options allow |
| 543 | selecting between a rst-compliant source ABI (--rst-source), or a |
| 544 | plain text that may be violating ReST spec, so it requres some escaping |
| 545 | logic (--no-rst-source). |
| 546 | |
Mauro Carvalho Chehab | 61439c4 | 2020-10-30 08:40:22 +0100 | [diff] [blame] | 547 | =item B<--enable-lineno> |
| 548 | |
| 549 | Enable output of #define LINENO lines. |
| 550 | |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 551 | =item B<--debug> |
| 552 | |
| 553 | Put the script in verbose mode, useful for debugging. Can be called multiple |
| 554 | times, to increase verbosity. |
| 555 | |
| 556 | =item B<--help> |
| 557 | |
| 558 | Prints a brief help message and exits. |
| 559 | |
| 560 | =item B<--man> |
| 561 | |
| 562 | Prints the manual page and exits. |
| 563 | |
| 564 | =back |
| 565 | |
| 566 | =head1 DESCRIPTION |
| 567 | |
Mauro Carvalho Chehab | 33e3e99 | 2019-06-20 14:22:59 -0300 | [diff] [blame] | 568 | Parse the Linux ABI files from ABI DIR (usually located at Documentation/ABI), |
| 569 | allowing to search for ABI symbols or to produce a ReST book containing |
| 570 | the Linux ABI documentation. |
| 571 | |
| 572 | =head1 EXAMPLES |
| 573 | |
| 574 | Search for all stable symbols with the word "usb": |
| 575 | |
| 576 | =over 8 |
| 577 | |
| 578 | $ scripts/get_abi.pl search usb --dir Documentation/ABI/stable |
| 579 | |
| 580 | =back |
| 581 | |
| 582 | Search for all symbols that match the regex expression "usb.*cap": |
| 583 | |
| 584 | =over 8 |
| 585 | |
| 586 | $ scripts/get_abi.pl search usb.*cap |
| 587 | |
| 588 | =back |
| 589 | |
| 590 | Output all obsoleted symbols in ReST format |
| 591 | |
| 592 | =over 8 |
| 593 | |
| 594 | $ scripts/get_abi.pl rest --dir Documentation/ABI/obsolete |
| 595 | |
| 596 | =back |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 597 | |
| 598 | =head1 BUGS |
| 599 | |
Mauro Carvalho Chehab | 7ce7b89 | 2019-06-20 14:23:04 -0300 | [diff] [blame] | 600 | Report bugs to Mauro Carvalho Chehab <mchehab+samsung@kernel.org> |
Mauro Carvalho Chehab | bbc249f | 2019-06-20 14:22:55 -0300 | [diff] [blame] | 601 | |
| 602 | =head1 COPYRIGHT |
| 603 | |
Mauro Carvalho Chehab | 7ce7b89 | 2019-06-20 14:23:04 -0300 | [diff] [blame] | 604 | 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] | 605 | |
| 606 | License GPLv2: GNU GPL version 2 <http://gnu.org/licenses/gpl.html>. |
| 607 | |
| 608 | This is free software: you are free to change and redistribute it. |
| 609 | There is NO WARRANTY, to the extent permitted by law. |
| 610 | |
| 611 | =cut |