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