xref: /curl/.github/scripts/badwords.pl (revision f0d8db15)
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#
11my $w;
12while(<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
27my $errors;
28
29sub file {
30    my ($f) = @_;
31    my $l = 0;
32    open(F, "<$f");
33    while(<F>) {
34        my $in = $_;
35        $l++;
36        chomp $in;
37        if($in =~ /^    /) {
38            next;
39        }
40        # remove the link part
41        $in =~ s/(\[.*\])\(.*\)/$1/g;
42        # remove backticked texts
43        $in =~ s/\`.*\`//g;
44        foreach my $w (@w) {
45            my $case = $exactcase{$w};
46            if(($in =~ /^(.*)$w/i && !$case) ||
47               ($in =~ /^(.*)$w/ && $case) ) {
48                my $p = $1;
49                my $c = length($p)+1;
50                print STDERR  "$f:$l:$c: error: found bad word \"$w\"\n";
51                printf STDERR " %4d | $in\n", $l;
52                printf STDERR "      | %*s^%s\n", length($p), " ",
53                    "~" x (length($w)-1);
54                printf STDERR " maybe use \"%s\" instead?\n", $alt{$w};
55                $errors++;
56            }
57        }
58    }
59    close(F);
60}
61
62my @files = @ARGV;
63
64foreach my $each (@files) {
65    file($each);
66}
67exit $errors;
68