| File: | lib/CheckSpelling/UnknownWordSplitter.pm |
| Coverage: | 87.9% |
| line | stmt | bran | cond | sub | time | code |
|---|---|---|---|---|---|---|
| 1 | #! -*-perl-*- | |||||
| 2 | ||||||
| 3 | # ~/bin/w | |||||
| 4 | # Search for potentially misspelled words | |||||
| 5 | # Output is: | |||||
| 6 | # misspellled | |||||
| 7 | # woord (WOORD, Woord, woord, woord's) | |||||
| 8 | package CheckSpelling::UnknownWordSplitter; | |||||
| 9 | ||||||
| 10 | 1 1 | 110725 3 | use 5.022; | |||
| 11 | 1 1 1 | 2 3 50 | use feature 'unicode_strings'; | |||
| 12 | 1 1 1 | 2 2 9 | use strict; | |||
| 13 | 1 1 1 | 2 0 20 | use warnings; | |||
| 14 | 1 1 1 | 1 1 14 | no warnings qw(experimental::vlb); | |||
| 15 | 1 1 1 | 1 1 2 | use utf8; | |||
| 16 | 1 1 1 | 13 0 27 | use Encode qw/decode_utf8 encode FB_DEFAULT/; | |||
| 17 | 1 1 1 | 2 0 26 | use File::Basename; | |||
| 18 | 1 1 1 | 17 1 21 | use Cwd 'abs_path'; | |||
| 19 | 1 1 1 | 1 1 21 | use File::Temp qw/ tempfile tempdir /; | |||
| 20 | 1 1 1 | 255 1 3153 | use CheckSpelling::Util; | |||
| 21 | our $VERSION='0.1.0'; | |||||
| 22 | ||||||
| 23 | my ($longest_word, $shortest_word, $word_match, $forbidden_re, $patterns_re, $candidates_re, $disable_word_collating, $check_file_names); | |||||
| 24 | my ($ignore_pattern, $upper_pattern, $lower_pattern, $not_lower_pattern, $not_upper_or_lower_pattern, $punctuation_pattern); | |||||
| 25 | my ($shortest, $longest) = (255, 0); | |||||
| 26 | my @forbidden_re_list; | |||||
| 27 | my %forbidden_re_descriptions; | |||||
| 28 | my @candidates_re_list; | |||||
| 29 | my $hunspell_dictionary_path; | |||||
| 30 | my @hunspell_dictionaries; | |||||
| 31 | my %dictionary = (); | |||||
| 32 | my $base_dict; | |||||
| 33 | my %unique; | |||||
| 34 | my %unique_unrecognized; | |||||
| 35 | my ($last_file, $words, $unrecognized) = ('', 0, 0); | |||||
| 36 | ||||||
| 37 | my $disable_flags; | |||||
| 38 | ||||||
| 39 | sub test_re { | |||||
| 40 | 14 | 11 | my ($expression) = @_; | |||
| 41 | 14 14 | 8 119 | return eval { qr /$expression/ }; | |||
| 42 | } | |||||
| 43 | ||||||
| 44 | sub quote_re { | |||||
| 45 | 14 | 12 | my ($expression) = @_; | |||
| 46 | 14 | 10 | return $expression if $expression =~ /\?\{/; | |||
| 47 | 14 | 53 | $expression =~ s/ | |||
| 48 | \G | |||||
| 49 | ( | |||||
| 50 | (?:[^\\]|\\[^Q])* | |||||
| 51 | ) | |||||
| 52 | (?: | |||||
| 53 | \\Q | |||||
| 54 | (?:[^\\]|\\[^E])* | |||||
| 55 | (?:\\E)? | |||||
| 56 | )? | |||||
| 57 | / | |||||
| 58 | 28 | 52 | $1 . (defined($2) ? quotemeta($2) : '') | |||
| 59 | /xge; | |||||
| 60 | 14 | 18 | return $expression; | |||
| 61 | } | |||||
| 62 | ||||||
| 63 | sub file_to_lists { | |||||
| 64 | 3 | 1 | my ($re) = @_; | |||
| 65 | 3 | 2 | my @patterns; | |||
| 66 | my %hints; | |||||
| 67 | 3 | 0 | my $fh; | |||
| 68 | 3 | 37 | if (open($fh, '<:utf8', $re)) { | |||
| 69 | 3 | 5 | local $/=undef; | |||
| 70 | 3 | 19 | my $file=<$fh>; | |||
| 71 | 3 | 7 | close $fh; | |||
| 72 | 3 | 2 | my $line_number = 0; | |||
| 73 | 3 | 3 | my $hint = ''; | |||
| 74 | 3 | 12 | for (split /\R/, $file) { | |||
| 75 | 17 | 8 | ++$line_number; | |||
| 76 | 17 | 9 | chomp; | |||
| 77 | 17 | 16 | if (/^#(?:\s(.+)|)/) { | |||
| 78 | 6 | 15 | $hint = $1 if ($hint eq '' && defined $1); | |||
| 79 | 6 | 4 | next; | |||
| 80 | } | |||||
| 81 | 11 | 11 | $hint = '' unless $_ ne ''; | |||
| 82 | 11 | 11 | my $pattern = $_; | |||
| 83 | 11 | 28 | next unless s/^(.+)/(?:$1)/; | |||
| 84 | 7 | 10 | my $quoted = quote_re($1); | |||
| 85 | 7 | 8 | unless (test_re $quoted) { | |||
| 86 | 1 | 1 | my $error = $@; | |||
| 87 | 1 | 43 | my $home = dirname(__FILE__); | |||
| 88 | 1 | 20 | $error =~ s/$home.*?\.pm line \d+\./$re line $line_number (bad-regex)/; | |||
| 89 | 1 | 12 | print STDERR $error; | |||
| 90 | 1 | 2 | $_ = '(?:\$^ - skipped because bad-regex)'; | |||
| 91 | 1 | 2 | $hint = ''; | |||
| 92 | } | |||||
| 93 | 7 | 10 | if (defined $hints{$_}) { | |||
| 94 | 1 | 1 | my $pattern_length = length $pattern; | |||
| 95 | 1 | 2 | my $wrapped = CheckSpelling::Util::wrap_in_backticks($pattern); | |||
| 96 | 1 | 17 | print STDERR "$re:$line_number:1 ... $pattern_length, Warning - duplicate pattern: $wrapped (duplicate-pattern)\n"; | |||
| 97 | 1 | 2 | $_ = '(?:\$^ - skipped because duplicate-pattern on $line_number)'; | |||
| 98 | } else { | |||||
| 99 | 6 | 5 | push @patterns, $_; | |||
| 100 | 6 | 7 | $hints{$_} = $hint; | |||
| 101 | } | |||||
| 102 | 7 | 13 | $hint = ''; | |||
| 103 | } | |||||
| 104 | } | |||||
| 105 | ||||||
| 106 | return { | |||||
| 107 | 3 | 14 | patterns => \@patterns, | |||
| 108 | hints => \%hints, | |||||
| 109 | }; | |||||
| 110 | } | |||||
| 111 | ||||||
| 112 | sub file_to_list { | |||||
| 113 | 2 | 1161 | my ($re) = @_; | |||
| 114 | 2 | 3 | my $lists = file_to_lists($re); | |||
| 115 | ||||||
| 116 | 2 2 | 2 6 | return @{$lists->{'patterns'}}; | |||
| 117 | } | |||||
| 118 | ||||||
| 119 | sub list_to_re { | |||||
| 120 | 2 | 2 | my (@list) = @_; | |||
| 121 | 2 5 5 | 2 1 3 | @list = map { my $quoted = quote_re($_); test_re($quoted) ? $quoted : '' } @list; | |||
| 122 | 2 5 | 2 5 | @list = grep { $_ ne '' } @list; | |||
| 123 | 2 | 1 | return '$^' unless scalar @list; | |||
| 124 | 2 | 8 | return join "|", (@list); | |||
| 125 | } | |||||
| 126 | ||||||
| 127 | sub not_empty { | |||||
| 128 | 51 | 40 | my ($thing) = @_; | |||
| 129 | 51 | 152 | return defined $thing && $thing ne '' | |||
| 130 | } | |||||
| 131 | ||||||
| 132 | sub valid_word { | |||||
| 133 | # shortest_word is an absolute | |||||
| 134 | 22 | 8 | our ($shortest, $longest, $shortest_word, $longest_word); | |||
| 135 | 22 | 21 | $shortest = $shortest_word if $shortest_word; | |||
| 136 | 22 | 21 | if ($longest_word) { | |||
| 137 | # longest_word is an absolute | |||||
| 138 | 20 | 17 | $longest = $longest_word; | |||
| 139 | } elsif (not_empty($longest)) { | |||||
| 140 | # we allow for some sloppiness (a couple of stuck keys per word) | |||||
| 141 | # it's possible that this should scale with word length | |||||
| 142 | 1 | 7 | $longest += 2; | |||
| 143 | } | |||||
| 144 | 22 | 13 | our ($upper_pattern, $lower_pattern, $punctuation_pattern); | |||
| 145 | 22 66 | 18 142 | my $word_pattern = join '|', (grep { defined $_ && /./ } ($upper_pattern, $lower_pattern, $punctuation_pattern)); | |||
| 146 | 22 | 17 | $word_pattern = q<\\w|'> unless $word_pattern; | |||
| 147 | 22 | 41 | if ((defined $shortest && not_empty($longest)) && | |||
| 148 | ($shortest > $longest)) { | |||||
| 149 | 0 | 0 | $word_pattern = "(?:$word_pattern){3}"; | |||
| 150 | 0 | 0 | return qr/$word_pattern/; | |||
| 151 | } | |||||
| 152 | 22 | 23 | $shortest = 3 unless defined $shortest; | |||
| 153 | 22 | 20 | $longest = '' unless defined $longest; | |||
| 154 | 22 | 74 | $word_match = "(?:$word_pattern){$shortest,$longest}"; | |||
| 155 | 22 | 175 | return qr/\b$word_match\b/; | |||
| 156 | } | |||||
| 157 | ||||||
| 158 | sub load_dictionary { | |||||
| 159 | 12 | 1993 | my ($dict) = @_; | |||
| 160 | 12 | 5 | our ($word_match, $longest, $shortest, $longest_word, $shortest_word, %dictionary); | |||
| 161 | 12 | 11 | $longest_word = CheckSpelling::Util::get_val_from_env('INPUT_LONGEST_WORD', undef); | |||
| 162 | 12 | 12 | $shortest_word = CheckSpelling::Util::get_val_from_env('INPUT_SHORTEST_WORD', undef); | |||
| 163 | 12 | 6 | our ($ignore_pattern, $upper_pattern, $lower_pattern, $not_lower_pattern, $not_upper_or_lower_pattern, $punctuation_pattern); | |||
| 164 | 12 | 10 | $ignore_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_IGNORE_PATTERN', q<[^a-zA-Z']>); | |||
| 165 | 12 | 38 | $upper_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_UPPER_PATTERN', '[A-Z]'); | |||
| 166 | 12 | 26 | $lower_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_LOWER_PATTERN', '[a-z]'); | |||
| 167 | 12 | 21 | $not_lower_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_NOT_LOWER_PATTERN', '[^a-z]'); | |||
| 168 | 12 | 21 | $not_upper_or_lower_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_NOT_UPPER_OR_LOWER_PATTERN', '[^A-Za-z]'); | |||
| 169 | 12 | 40 | $punctuation_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_PUNCTUATION_PATTERN', q<'>); | |||
| 170 | 12 | 24 | %dictionary = (); | |||
| 171 | ||||||
| 172 | 12 | 489 | open(DICT, '<:utf8', $dict); | |||
| 173 | 12 | 56 | while (!eof(DICT)) { | |||
| 174 | 31 | 36 | my $word = <DICT>; | |||
| 175 | 31 | 21 | chomp $word; | |||
| 176 | 31 | 72 | next unless $word =~ $word_match; | |||
| 177 | 28 | 25 | my $l = length $word; | |||
| 178 | 28 | 19 | $longest = -1 unless not_empty($longest); | |||
| 179 | 28 | 26 | $longest = $l if $l > $longest; | |||
| 180 | 28 | 28 | $shortest = $l if $l < $shortest; | |||
| 181 | 28 | 50 | $dictionary{$word}=1; | |||
| 182 | } | |||||
| 183 | 12 | 40 | close DICT; | |||
| 184 | ||||||
| 185 | 12 | 23 | $word_match = valid_word(); | |||
| 186 | } | |||||
| 187 | ||||||
| 188 | sub hunspell_dictionary { | |||||
| 189 | 3 | 6 | my ($dict) = @_; | |||
| 190 | 3 | 2 | my $name = $dict; | |||
| 191 | 3 | 6 | $name =~ s{/src/index/hunspell/index\.dic$}{}; | |||
| 192 | 3 | 12 | $name =~ s{.*/}{}; | |||
| 193 | 3 | 4 | my $aff = $dict; | |||
| 194 | 3 | 2 | my $encoding; | |||
| 195 | 3 | 7 | $aff =~ s/\.dic$/.aff/; | |||
| 196 | 3 | 26 | if (open AFF, '<', $aff) { | |||
| 197 | 3 | 19 | while (<AFF>) { | |||
| 198 | 0 | 0 | next unless /^SET\s+(\S+)/; | |||
| 199 | 0 | 0 | $encoding = $1 if ($1 !~ /utf-8/i); | |||
| 200 | 0 | 0 | last; | |||
| 201 | } | |||||
| 202 | 3 | 8 | close AFF; | |||
| 203 | } | |||||
| 204 | return { | |||||
| 205 | 3 | 361 | name => $name, | |||
| 206 | dict => $dict, | |||||
| 207 | aff => $aff, | |||||
| 208 | encoding => $encoding, | |||||
| 209 | engine => Text::Hunspell->new($aff, $dict), | |||||
| 210 | } | |||||
| 211 | } | |||||
| 212 | ||||||
| 213 | sub init { | |||||
| 214 | 9 | 10847 | my ($configuration) = @_; | |||
| 215 | 9 | 9 | our ($word_match, %unique, $patterns_re, @forbidden_re_list, $forbidden_re, @candidates_re_list, $candidates_re); | |||
| 216 | 9 | 21 | our $sandbox = CheckSpelling::Util::get_file_from_env('sandbox', ''); | |||
| 217 | 9 | 8 | our $hunspell_dictionary_path = CheckSpelling::Util::get_file_from_env('hunspell_dictionary_path', ''); | |||
| 218 | 9 | 13 | our $timeout = CheckSpelling::Util::get_val_from_env('splitter_timeout', 30); | |||
| 219 | 9 | 5 | our %forbidden_re_descriptions; | |||
| 220 | 9 | 9 | if ($hunspell_dictionary_path) { | |||
| 221 | 3 | 29 | our @hunspell_dictionaries = (); | |||
| 222 | 1 1 1 1 1 1 1 1 1 3 | 171 960 19 4 0 18 7 2 17 144 | if (eval 'use Text::Hunspell; 1') { | |||
| 223 | 3 | 108 | my @hunspell_dictionaries_list = glob("$hunspell_dictionary_path/*.dic"); | |||
| 224 | 3 | 8 | for my $hunspell_dictionary_file (@hunspell_dictionaries_list) { | |||
| 225 | 3 | 6 | push @hunspell_dictionaries, hunspell_dictionary($hunspell_dictionary_file); | |||
| 226 | } | |||||
| 227 | } else { | |||||
| 228 | 0 | 0 | print STDERR "Could not load Text::Hunspell for dictionaries (hunspell-unavailable)\n"; | |||
| 229 | } | |||||
| 230 | } | |||||
| 231 | 9 | 11 | my (@patterns_re_list, %in_patterns_re_list); | |||
| 232 | 9 | 61 | if (-e "$configuration/patterns.txt") { | |||
| 233 | 0 | 0 | @patterns_re_list = file_to_list "$configuration/patterns.txt"; | |||
| 234 | 0 | 0 | $patterns_re = list_to_re @patterns_re_list; | |||
| 235 | 0 0 | 0 0 | %in_patterns_re_list = map {$_ => 1} @patterns_re_list; | |||
| 236 | } else { | |||||
| 237 | 9 | 15 | $patterns_re = undef; | |||
| 238 | } | |||||
| 239 | ||||||
| 240 | 9 | 37 | if (-e "$configuration/forbidden.txt") { | |||
| 241 | 1 | 1 | my $forbidden_re_info = file_to_lists "$configuration/forbidden.txt"; | |||
| 242 | 1 1 | 0 2 | @forbidden_re_list = @{$forbidden_re_info->{'patterns'}}; | |||
| 243 | 1 1 | 1 2 | %forbidden_re_descriptions = %{$forbidden_re_info->{'hints'}}; | |||
| 244 | 1 | 1 | $forbidden_re = list_to_re @forbidden_re_list; | |||
| 245 | } else { | |||||
| 246 | 8 | 8 | $forbidden_re = undef; | |||
| 247 | } | |||||
| 248 | ||||||
| 249 | 9 | 42 | if (-e "$configuration/candidates.txt") { | |||
| 250 | 1 | 2 | @candidates_re_list = file_to_list "$configuration/candidates.txt"; | |||
| 251 | 1 2 2 | 1 2 4 | @candidates_re_list = map { my $quoted = quote_re($_); $in_patterns_re_list{$_} || !test_re($quoted) ? '' : $quoted } @candidates_re_list; | |||
| 252 | 1 | 1 | $candidates_re = list_to_re @candidates_re_list; | |||
| 253 | } else { | |||||
| 254 | 8 | 6 | $candidates_re = undef; | |||
| 255 | } | |||||
| 256 | ||||||
| 257 | 9 | 8 | our $largest_file = CheckSpelling::Util::get_val_from_env('INPUT_LARGEST_FILE', 1024*1024); | |||
| 258 | ||||||
| 259 | 9 | 9 | my $disable_flags = CheckSpelling::Util::get_file_from_env('INPUT_DISABLE_CHECKS', ''); | |||
| 260 | 9 | 10 | our $disable_word_collating = $disable_flags =~ /(?:^|,|\s)word-collating(?:,|\s|$)/; | |||
| 261 | 9 | 4 | our $disable_minified_file = $disable_flags =~ /(?:^|,|\s)minified-file(?:,|\s|$)/; | |||
| 262 | 9 | 6 | our $disable_single_line_file = $disable_flags =~ /(?:^|,|\s)single-line-file(?:,|\s|$)/; | |||
| 263 | ||||||
| 264 | 9 | 4 | our $check_file_names = CheckSpelling::Util::get_file_from_env('check_file_names', ''); | |||
| 265 | ||||||
| 266 | 9 | 6 | our $use_magic_file = CheckSpelling::Util::get_val_from_env('INPUT_USE_MAGIC_FILE', ''); | |||
| 267 | ||||||
| 268 | 9 | 12 | $word_match = valid_word(); | |||
| 269 | ||||||
| 270 | 9 | 13 | our $base_dict = CheckSpelling::Util::get_file_from_env('dict', "$configuration/words"); | |||
| 271 | 9 | 42 | $base_dict = '/usr/share/dict/words' unless -e $base_dict; | |||
| 272 | 9 | 14 | load_dictionary($base_dict); | |||
| 273 | } | |||||
| 274 | ||||||
| 275 | sub split_line { | |||||
| 276 | 1164 | 521 | our (%dictionary, $word_match, $disable_word_collating); | |||
| 277 | 1164 | 458 | our ($ignore_pattern, $upper_pattern, $lower_pattern, $not_lower_pattern, $not_upper_or_lower_pattern, $punctuation_pattern); | |||
| 278 | 1164 | 507 | our @hunspell_dictionaries; | |||
| 279 | 1164 | 426 | our $shortest; | |||
| 280 | 1164 | 525 | my $pattern = '.'; | |||
| 281 | # $pattern = "(?:$upper_pattern){$shortest,}|$upper_pattern(?:$lower_pattern){2,}\n"; | |||||
| 282 | ||||||
| 283 | # https://www.fileformat.info/info/unicode/char/2019/ | |||||
| 284 | 1164 | 474 | my $rsqm = "\xE2\x80\x99"; | |||
| 285 | ||||||
| 286 | 1164 | 647 | my ($words, $unrecognized) = (0, 0); | |||
| 287 | 1164 | 862 | my ($line, $unique_ref, $unique_unrecognized_ref, $unrecognized_line_items_ref) = @_; | |||
| 288 | 1164 | 6186 | $line =~ s/(?:$rsqm|'|'|\%27|’|’|’|\\u2019|\x{2019}|')+/'/g; | |||
| 289 | 1164 | 2490 | $line =~ s/(?:$ignore_pattern)+/ /g; | |||
| 290 | 1164 | 1813 | while ($line =~ s/($upper_pattern{2,})($upper_pattern$lower_pattern{2,})/ $1 $2 /g) {} | |||
| 291 | 1164 | 4484 | while ($line =~ s/((?:$lower_pattern|$punctuation_pattern)+)($upper_pattern)/$1 $2/g) {} | |||
| 292 | 1164 | 1512 | for my $token (split /\s+/, $line) { | |||
| 293 | 5871 | 5156 | next unless $token =~ /$pattern/; | |||
| 294 | 4708 | 3707 | $token =~ s/^(?:'|$rsqm)+//g; | |||
| 295 | 4708 | 4508 | $token =~ s/(?:'|$rsqm)+s?$//g; | |||
| 296 | 4708 | 2590 | my $raw_token = $token; | |||
| 297 | 4708 | 2626 | $token =~ s/^[^Ii]?'+(.*)/$1/; | |||
| 298 | 4708 | 2234 | $token =~ s/(.*?)'+$/$1/; | |||
| 299 | 4708 | 6698 | next unless $token =~ $word_match; | |||
| 300 | 4561 | 3864 | if (defined $dictionary{$token}) { | |||
| 301 | 1032 | 458 | ++$words; | |||
| 302 | 1032 | 532 | $unique_ref->{$token}=1; | |||
| 303 | 1032 | 836 | next; | |||
| 304 | } | |||||
| 305 | 3529 | 2342 | if (@hunspell_dictionaries) { | |||
| 306 | 3504 | 1707 | my $found = 0; | |||
| 307 | 3504 | 1847 | for my $hunspell_dictionary (@hunspell_dictionaries) { | |||
| 308 | my $token_encoded = defined $hunspell_dictionary->{'encoding'} ? | |||||
| 309 | 3504 | 2480 | encode($hunspell_dictionary->{'encoding'}, $token) : $token; | |||
| 310 | 3504 | 5953 | next unless ($hunspell_dictionary->{'engine'}->check($token_encoded)); | |||
| 311 | 0 | 0 | ++$words; | |||
| 312 | 0 | 0 | $dictionary{$token} = 1; | |||
| 313 | 0 | 0 | $unique_ref->{$token}=1; | |||
| 314 | 0 | 0 | $found = 1; | |||
| 315 | 0 | 0 | last; | |||
| 316 | } | |||||
| 317 | 3504 | 2399 | next if $found; | |||
| 318 | } | |||||
| 319 | 3529 | 2324 | my $key = lc $token; | |||
| 320 | 3529 | 2855 | if (defined $dictionary{$key}) { | |||
| 321 | 6 | 3 | ++$words; | |||
| 322 | 6 | 4 | $unique_ref->{$key}=1; | |||
| 323 | 6 | 8 | next; | |||
| 324 | } | |||||
| 325 | 3523 | 2264 | unless ($disable_word_collating) { | |||
| 326 | 3523 | 1929 | $key =~ s/''+/'/g; | |||
| 327 | 3523 | 1859 | $key =~ s/'[sd]$//; | |||
| 328 | } | |||||
| 329 | 3523 | 2736 | if (defined $dictionary{$key}) { | |||
| 330 | 0 | 0 | ++$words; | |||
| 331 | 0 | 0 | $unique_ref->{$key}=1; | |||
| 332 | 0 | 0 | next; | |||
| 333 | } | |||||
| 334 | 3523 | 1722 | ++$unrecognized; | |||
| 335 | 3523 | 1863 | $unique_unrecognized_ref->{$raw_token}=1; | |||
| 336 | 3523 | 4002 | $unrecognized_line_items_ref->{$raw_token}=1; | |||
| 337 | } | |||||
| 338 | 1164 | 1640 | return ($words, $unrecognized); | |||
| 339 | } | |||||
| 340 | ||||||
| 341 | sub skip_file { | |||||
| 342 | 15 | 28 | my ($temp_dir, $reason) = @_; | |||
| 343 | 15 | 471 | open(SKIPPED, '>:utf8', "$temp_dir/skipped"); | |||
| 344 | 15 | 91 | print SKIPPED $reason; | |||
| 345 | 15 | 374 | close SKIPPED; | |||
| 346 | } | |||||
| 347 | ||||||
| 348 | sub split_file { | |||||
| 349 | 15 | 10646 | my ($file) = @_; | |||
| 350 | our ( | |||||
| 351 | 15 | 10 | $unrecognized, $shortest, $largest_file, $words, | |||
| 352 | $word_match, %unique, %unique_unrecognized, $forbidden_re, | |||||
| 353 | @forbidden_re_list, $patterns_re, %dictionary, | |||||
| 354 | $candidates_re, @candidates_re_list, $check_file_names, $use_magic_file, $disable_minified_file, | |||||
| 355 | $disable_single_line_file, | |||||
| 356 | $sandbox, | |||||
| 357 | ); | |||||
| 358 | 15 | 8 | our %forbidden_re_descriptions; | |||
| 359 | 15 | 6 | our ($ignore_pattern, $upper_pattern, $lower_pattern, $not_lower_pattern, $not_upper_or_lower_pattern, $punctuation_pattern); | |||
| 360 | ||||||
| 361 | # https://www.fileformat.info/info/unicode/char/2019/ | |||||
| 362 | 15 | 7 | my $rsqm = "\xE2\x80\x99"; | |||
| 363 | ||||||
| 364 | 15 | 17 | my @candidates_re_hits = (0) x scalar @candidates_re_list; | |||
| 365 | 15 | 12 | my @candidates_re_lines = (0) x scalar @candidates_re_list; | |||
| 366 | 15 | 10 | my @forbidden_re_hits = (0) x scalar @forbidden_re_list; | |||
| 367 | 15 | 15 | my @forbidden_re_lines = (0) x scalar @forbidden_re_list; | |||
| 368 | 15 | 26 | my $temp_dir = tempdir(DIR=>$sandbox); | |||
| 369 | 15 | 2302 | print STDERR "checking file: $file\n" if defined $ENV{'DEBUG'}; | |||
| 370 | 15 | 324 | open(NAME, '>', "$temp_dir/name"); | |||
| 371 | 15 | 38 | print NAME $file; | |||
| 372 | 15 | 154 | close NAME; | |||
| 373 | 15 | 49 | my $file_size = -s $file; | |||
| 374 | 15 | 15 | if (defined $largest_file) { | |||
| 375 | 15 | 13 | unless ($check_file_names eq $file) { | |||
| 376 | 15 | 17 | if ($file_size > $largest_file) { | |||
| 377 | 1 | 2 | skip_file($temp_dir, "size `$file_size` exceeds limit `$largest_file` (large-file)\n"); | |||
| 378 | 1 | 3 | return $temp_dir; | |||
| 379 | } | |||||
| 380 | } | |||||
| 381 | } | |||||
| 382 | 14 | 17 | if ($use_magic_file) { | |||
| 383 | 8 | 12227 | if (open(my $file_fh, '-|', | |||
| 384 | '/usr/bin/file', | |||||
| 385 | '-b', | |||||
| 386 | '--mime', | |||||
| 387 | '-e', 'cdf', | |||||
| 388 | '-e', 'compress', | |||||
| 389 | '-e', 'csv', | |||||
| 390 | '-e', 'elf', | |||||
| 391 | '-e', 'json', | |||||
| 392 | '-e', 'tar', | |||||
| 393 | $file)) { | |||||
| 394 | 8 | 29390 | my $file_kind = <$file_fh>; | |||
| 395 | 8 | 5306 | close $file_fh; | |||
| 396 | 8 | 97 | if ($file_kind =~ /^(.*?); charset=binary/) { | |||
| 397 | 2 | 23 | skip_file($temp_dir, "it appears to be a binary file (`$1`) (binary-file)\n"); | |||
| 398 | 2 | 47 | return $temp_dir; | |||
| 399 | } | |||||
| 400 | } | |||||
| 401 | } | |||||
| 402 | 12 | 105 | open FILE, '<', $file; | |||
| 403 | 12 | 11 | binmode FILE; | |||
| 404 | 12 | 8 | my $head; | |||
| 405 | 12 | 81 | read(FILE, $head, 4096); | |||
| 406 | 12 | 832 | $head =~ s/(?:\r|\n)+$//; | |||
| 407 | 12 | 52 | my $dos_new_lines = () = $head =~ /\r\n/gi; | |||
| 408 | 12 | 30 | my $unix_new_lines = () = $head =~ /\n/gi; | |||
| 409 | 12 | 96 | my $mac_new_lines = () = $head =~ /\r/gi; | |||
| 410 | 12 | 46 | local $/; | |||
| 411 | 12 | 73 | if ($unix_new_lines == 0 && $mac_new_lines == 0) { | |||
| 412 | 3 | 8 | $/ = "\n"; | |||
| 413 | } elsif ($dos_new_lines >= $unix_new_lines && $dos_new_lines >= $mac_new_lines) { | |||||
| 414 | 1 | 6 | $/ = "\r\n"; | |||
| 415 | } elsif ($mac_new_lines > $unix_new_lines) { | |||||
| 416 | 2 | 6 | $/ = "\r"; | |||
| 417 | } else { | |||||
| 418 | 6 | 6 | $/ = "\n"; | |||
| 419 | } | |||||
| 420 | 12 | 20 | seek(FILE, 0, 0); | |||
| 421 | 12 | 13 | ($words, $unrecognized) = (0, 0); | |||
| 422 | 12 | 33 | %unique = (); | |||
| 423 | 12 | 27 | %unique_unrecognized = (); | |||
| 424 | ||||||
| 425 | local $SIG{__WARN__} = sub { | |||||
| 426 | 0 | 0 | my $message = shift; | |||
| 427 | 0 | 0 | $message =~ s/> line/> in $file - line/; | |||
| 428 | 0 | 0 | chomp $message; | |||
| 429 | 0 | 0 | print STDERR "$message\n"; | |||
| 430 | 12 | 102 | }; | |||
| 431 | ||||||
| 432 | 12 | 303 | open(WARNINGS, '>:utf8', "$temp_dir/warnings"); | |||
| 433 | 12 | 7 | our $timeout; | |||
| 434 | 12 | 12 | eval { | |||
| 435 | 12 0 | 83 0 | local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required | |||
| 436 | 12 | 25 | alarm $timeout; | |||
| 437 | ||||||
| 438 | 12 | 15 | my $offset = 0; | |||
| 439 | 12 | 102 | while (<FILE>) { | |||
| 440 | 1167 | 1011 | if ($. == 1) { | |||
| 441 | 12 | 17 | unless ($disable_minified_file) { | |||
| 442 | 12 | 55 | if ($file_size >= 512 && length($_) == $file_size) { | |||
| 443 | 1 | 8 | skip_file($temp_dir, "file only has a single line (single-line-file)\n"); | |||
| 444 | 1 | 4 | last; | |||
| 445 | } | |||||
| 446 | } | |||||
| 447 | } | |||||
| 448 | 1166 | 2330 | $_ = decode_utf8($_, FB_DEFAULT); | |||
| 449 | 1166 | 2660 | if (/[\x{D800}-\x{DFFF}]/) { | |||
| 450 | 0 | 0 | skip_file($temp_dir, "file contains a UTF-16 surrogate -- UTF-16 surrogates are not supported (utf16-surrogate-file)\n"); | |||
| 451 | 0 | 0 | last; | |||
| 452 | } | |||||
| 453 | 1166 | 1464 | s/\R$//; | |||
| 454 | 1166 | 1066 | s/^\x{FEFF}// if $. == 1; | |||
| 455 | 1166 | 1112 | next unless /./; | |||
| 456 | 1164 | 732 | my $raw_line = $_; | |||
| 457 | ||||||
| 458 | # hook for custom line based text exclusions: | |||||
| 459 | 1164 | 733 | if (defined $patterns_re) { | |||
| 460 | 2 6 | 11 8 | s/($patterns_re)/"="x length($1)/ge; | |||
| 461 | } | |||||
| 462 | 1164 | 653 | my $initial_line_state = $_; | |||
| 463 | 1164 | 798 | my $previous_line_state = $_; | |||
| 464 | 1164 | 545 | my $line_flagged; | |||
| 465 | 1164 | 797 | if ($forbidden_re) { | |||
| 466 | 9 5 | 62 11 | while (s/($forbidden_re)/"="x length($1)/e) { | |||
| 467 | 5 | 3 | $line_flagged = 1; | |||
| 468 | 5 | 11 | my ($begin, $end, $match) = ($-[0] + 1, $+[0] + 1, $1); | |||
| 469 | 5 | 2 | my $found_trigger_re; | |||
| 470 | 5 | 4 | for my $i (0 .. $#forbidden_re_list) { | |||
| 471 | 7 | 6 | my $forbidden_re_singleton = $forbidden_re_list[$i]; | |||
| 472 | 7 | 4 | my $test_line = $previous_line_state; | |||
| 473 | 7 4 | 75 6 | if ($test_line =~ s/($forbidden_re_singleton)/"="x length($1)/e) { | |||
| 474 | 4 | 6 | next unless $test_line eq $_; | |||
| 475 | 4 | 6 | my ($begin_test, $end_test, $match_test) = ($-[0] + 1, $+[0] + 1, $1); | |||
| 476 | 4 | 4 | next unless $begin == $begin_test; | |||
| 477 | 4 | 4 | next unless $end == $end_test; | |||
| 478 | 4 | 3 | next unless $match eq $match_test; | |||
| 479 | 4 | 2 | $found_trigger_re = $forbidden_re_singleton; | |||
| 480 | 4 | 8 | my $hit = "$.:$begin:$end"; | |||
| 481 | 4 | 2 | $forbidden_re_hits[$i]++; | |||
| 482 | 4 | 4 | $forbidden_re_lines[$i] = $hit unless $forbidden_re_lines[$i]; | |||
| 483 | 4 | 7 | last; | |||
| 484 | } | |||||
| 485 | } | |||||
| 486 | 5 | 6 | my $wrapped = CheckSpelling::Util::wrap_in_backticks($match); | |||
| 487 | 5 | 6 | if ($found_trigger_re) { | |||
| 488 | 4 | 8 | my $description = $forbidden_re_descriptions{$found_trigger_re} || ''; | |||
| 489 | 4 | 8 | $found_trigger_re =~ s/^\(\?:(.*)\)$/$1/; | |||
| 490 | 4 | 4 | my $quoted_trigger_re = CheckSpelling::Util::truncate_with_ellipsis(CheckSpelling::Util::wrap_in_backticks($found_trigger_re), 99); | |||
| 491 | 4 | 5 | if ($description ne '') { | |||
| 492 | 3 | 13 | print WARNINGS ":$.:$begin ... $end, Warning - $wrapped matches a line_forbidden.patterns rule: $description - $quoted_trigger_re (forbidden-pattern)\n"; | |||
| 493 | } else { | |||||
| 494 | 1 | 5 | print WARNINGS ":$.:$begin ... $end, Warning - $wrapped matches a line_forbidden.patterns entry: $quoted_trigger_re (forbidden-pattern)\n"; | |||
| 495 | } | |||||
| 496 | } else { | |||||
| 497 | 1 | 3 | print WARNINGS ":$.:$begin ... $end, Warning - $wrapped matches a line_forbidden.patterns entry (forbidden-pattern)\n"; | |||
| 498 | } | |||||
| 499 | 5 | 24 | $previous_line_state = $_; | |||
| 500 | } | |||||
| 501 | 9 | 9 | $_ = $initial_line_state; | |||
| 502 | } | |||||
| 503 | # This is to make it easier to deal w/ rules: | |||||
| 504 | 1164 | 1124 | s/^/ /; | |||
| 505 | 1164 | 679 | my %unrecognized_line_items = (); | |||
| 506 | 1164 | 860 | my ($new_words, $new_unrecognized) = split_line($_, \%unique, \%unique_unrecognized, \%unrecognized_line_items); | |||
| 507 | 1164 | 599 | $words += $new_words; | |||
| 508 | 1164 | 499 | $unrecognized += $new_unrecognized; | |||
| 509 | 1164 | 836 | my $line_length = length($raw_line); | |||
| 510 | 1164 | 1506 | for my $token (sort CheckSpelling::Util::case_biased keys %unrecognized_line_items) { | |||
| 511 | 1029 | 482 | my $found_token = 0; | |||
| 512 | 1029 | 491 | my $raw_token = $token; | |||
| 513 | 1029 | 525 | $token =~ s/'/(?:'|\x{2019}|\'|\')+/g; | |||
| 514 | 1029 | 459 | my $before; | |||
| 515 | 1029 | 1565 | if ($token =~ /^$upper_pattern$lower_pattern/) { | |||
| 516 | 5 | 3 | $before = '(?<=.)'; | |||
| 517 | } elsif ($token =~ /^$upper_pattern/) { | |||||
| 518 | 0 | 0 | $before = "(?<!$upper_pattern)"; | |||
| 519 | } else { | |||||
| 520 | 1024 | 599 | $before = "(?<=$not_lower_pattern)"; | |||
| 521 | } | |||||
| 522 | 1029 | 1179 | my $after = ($token =~ /$upper_pattern$/) ? "(?=$not_upper_or_lower_pattern)|(?=$upper_pattern$lower_pattern)" : "(?=$not_lower_pattern)"; | |||
| 523 | 1029 | 2104 | while ($raw_line =~ /(?:\b|$before)($token)(?:\b|$after)/g) { | |||
| 524 | 3520 | 1724 | $line_flagged = 1; | |||
| 525 | 3520 | 1426 | $found_token = 1; | |||
| 526 | 3520 | 4961 | my ($begin, $end, $match) = ($-[0] + 1, $+[0] + 1, $1); | |||
| 527 | 3520 | 2930 | next unless $match =~ /./; | |||
| 528 | 3520 | 2200 | my $wrapped = CheckSpelling::Util::wrap_in_backticks($match); | |||
| 529 | 3520 | 12017 | print WARNINGS ":$.:$begin ... $end: $wrapped\n"; | |||
| 530 | } | |||||
| 531 | 1029 | 1095 | unless ($found_token) { | |||
| 532 | 3 | 28 | if ($raw_line !~ /$token.*$token/ && $raw_line =~ /($token)/) { | |||
| 533 | 3 | 6 | my ($begin, $end, $match) = ($-[0] + 1, $+[0] + 1, $1); | |||
| 534 | 3 | 2 | my $wrapped = CheckSpelling::Util::wrap_in_backticks($raw_token); | |||
| 535 | 3 | 12 | print WARNINGS ":$.:$begin ... $end: $wrapped\n"; | |||
| 536 | } else { | |||||
| 537 | 0 | 0 | my $offset = $line_length + 1; | |||
| 538 | 0 | 0 | my $wrapped = CheckSpelling::Util::wrap_in_backticks($raw_token); | |||
| 539 | 0 | 0 | print WARNINGS ":$.:1 ... $offset, Warning - Could not identify whole word $wrapped in line (token-is-substring)\n"; | |||
| 540 | } | |||||
| 541 | } | |||||
| 542 | } | |||||
| 543 | 1164 | 1849 | if ($line_flagged && $candidates_re) { | |||
| 544 | 1 | 1 | $_ = $previous_line_state = $initial_line_state; | |||
| 545 | 1 1 | 17 2 | s/($candidates_re)/"="x length($1)/ge; | |||
| 546 | 1 | 2 | if ($_ ne $initial_line_state) { | |||
| 547 | 1 | 1 | $_ = $previous_line_state; | |||
| 548 | 1 | 1 | for my $i (0 .. $#candidates_re_list) { | |||
| 549 | 2 | 1 | my $candidate_re = $candidates_re_list[$i]; | |||
| 550 | 2 | 17 | next unless $candidate_re =~ /./ && $raw_line =~ /$candidate_re/; | |||
| 551 | 1 1 | 6 2 | if (($_ =~ s/($candidate_re)/"="x length($1)/e)) { | |||
| 552 | 1 | 2 | my ($begin, $end) = ($-[0] + 1, $+[0] + 1); | |||
| 553 | 1 | 3 | my $hit = "$.:$begin:$end"; | |||
| 554 | 1 | 1 | $_ = $previous_line_state; | |||
| 555 | 1 1 | 5 1 | my $replacements = ($_ =~ s/($candidate_re)/"="x length($1)/ge); | |||
| 556 | 1 | 2 | $candidates_re_hits[$i] += $replacements; | |||
| 557 | 1 | 1 | $candidates_re_lines[$i] = $hit unless $candidates_re_lines[$i]; | |||
| 558 | 1 | 2 | $_ = $previous_line_state; | |||
| 559 | } | |||||
| 560 | } | |||||
| 561 | } | |||||
| 562 | } | |||||
| 563 | 1164 | 835 | unless ($disable_minified_file) { | |||
| 564 | 1164 | 905 | s/={3,}//g; | |||
| 565 | 1164 | 847 | $offset += length; | |||
| 566 | 1164 | 1021 | my $ratio = int($offset / $.); | |||
| 567 | 1164 | 604 | my $ratio_threshold = 1000; | |||
| 568 | 1164 | 2854 | if ($ratio > $ratio_threshold) { | |||
| 569 | 11 | 24 | skip_file($temp_dir, "average line width ($ratio) exceeds the threshold ($ratio_threshold) (minified-file)\n"); | |||
| 570 | } | |||||
| 571 | } | |||||
| 572 | } | |||||
| 573 | ||||||
| 574 | 12 | 71 | alarm 0; | |||
| 575 | }; | |||||
| 576 | 12 | 15 | if ($@) { | |||
| 577 | 0 | 0 | die unless $@ eq "alarm\n"; | |||
| 578 | 0 | 0 | print WARNINGS ":$.:1 ... 1, Warning - Could not parse file within time limit (slow-file)\n"; | |||
| 579 | 0 | 0 | skip_file($temp_dir, "it could not be parsed file within time limit (slow-file)\n"); | |||
| 580 | } | |||||
| 581 | ||||||
| 582 | 12 | 39 | close FILE; | |||
| 583 | 12 | 129 | close WARNINGS; | |||
| 584 | ||||||
| 585 | 12 | 29 | if ($unrecognized || @candidates_re_hits || @forbidden_re_hits) { | |||
| 586 | 11 | 304 | open(STATS, '>:utf8', "$temp_dir/stats"); | |||
| 587 | 11 | 163 | print STATS "{words: $words, unrecognized: $unrecognized, unknown: ".(keys %unique_unrecognized). | |||
| 588 | ", unique: ".(keys %unique). | |||||
| 589 | (@candidates_re_hits ? ", candidates: [".(join ',', @candidates_re_hits)."]" : ""). | |||||
| 590 | (@candidates_re_lines ? ", candidate_lines: [".(join ',', @candidates_re_lines)."]" : ""). | |||||
| 591 | (@forbidden_re_hits ? ", forbidden: [".(join ',', @forbidden_re_hits)."]" : ""). | |||||
| 592 | (@forbidden_re_lines ? ", forbidden_lines: [".(join ',', @forbidden_re_lines)."]" : ""). | |||||
| 593 | "}"; | |||||
| 594 | 11 | 128 | close STATS; | |||
| 595 | 11 | 233 | open(UNKNOWN, '>:utf8', "$temp_dir/unknown"); | |||
| 596 | 11 19 | 39 36 | print UNKNOWN map { "$_\n" } sort CheckSpelling::Util::case_biased keys %unique_unrecognized; | |||
| 597 | 11 | 90 | close UNKNOWN; | |||
| 598 | } | |||||
| 599 | ||||||
| 600 | 12 | 108 | return $temp_dir; | |||
| 601 | } | |||||
| 602 | ||||||
| 603 | sub main { | |||||
| 604 | 2 | 402 | my ($configuration, @ARGV) = @_; | |||
| 605 | 2 | 0 | our %dictionary; | |||
| 606 | 2 | 3 | unless (%dictionary) { | |||
| 607 | 1 | 1 | init($configuration); | |||
| 608 | } | |||||
| 609 | ||||||
| 610 | # read all input | |||||
| 611 | 2 | 2 | my @reports; | |||
| 612 | ||||||
| 613 | 2 | 1 | for my $file (@ARGV) { | |||
| 614 | 2 | 3 | my $temp_dir = split_file($file); | |||
| 615 | 2 | 3 | push @reports, "$temp_dir\n"; | |||
| 616 | } | |||||
| 617 | 2 | 7 | print join '', @reports; | |||
| 618 | } | |||||
| 619 | ||||||
| 620 | 1; | |||||