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