badwords.pl 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/env perl
  2. # Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  3. #
  4. # SPDX-License-Identifier: curl
  5. #
  6. # bad[:=]correct
  7. #
  8. # If separator is '=', the string will be compared case sensitively.
  9. # If separator is ':', the check is done case insensitively.
  10. #
  11. my $w;
  12. while(<STDIN>) {
  13. chomp;
  14. if($_ =~ /^#/) {
  15. next;
  16. }
  17. if($_ =~ /^([^:=]*)([:=])(.*)/) {
  18. my ($bad, $sep, $better)=($1, $2, $3);
  19. push @w, $bad;
  20. $alt{$bad} = $better;
  21. if($sep eq "=") {
  22. $exactcase{$bad} = 1;
  23. }
  24. }
  25. }
  26. my $errors;
  27. sub file {
  28. my ($f) = @_;
  29. my $l = 0;
  30. open(F, "<$f");
  31. while(<F>) {
  32. my $in = $_;
  33. $l++;
  34. chomp $in;
  35. if($in =~ /^ /) {
  36. next;
  37. }
  38. # remove the link part
  39. $in =~ s/(\[.*\])\(.*\)/$1/g;
  40. # remove backticked texts
  41. $in =~ s/\`.*\`//g;
  42. foreach my $w (@w) {
  43. my $case = $exactcase{$w};
  44. if(($in =~ /^(.*)$w/i && !$case) ||
  45. ($in =~ /^(.*)$w/ && $case) ) {
  46. my $p = $1;
  47. my $c = length($p)+1;
  48. print STDERR "$f:$l:$c: error: found bad word \"$w\"\n";
  49. printf STDERR " %4d | $in\n", $l;
  50. printf STDERR " | %*s^%s\n", length($p), " ",
  51. "~" x (length($w)-1);
  52. printf STDERR " maybe use \"%s\" instead?\n", $alt{$w};
  53. $errors++;
  54. }
  55. }
  56. }
  57. close(F);
  58. }
  59. my @files = @ARGV;
  60. foreach my $each (@files) {
  61. file($each);
  62. }
  63. exit $errors;