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