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