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