Use a common method of reading lines

Adds comment support for domain lists.
This commit is contained in:
Dan Church 2022-11-25 15:40:20 -06:00
parent 59ef0fd513
commit fd8061714f
Signed by: h3xx
GPG Key ID: EA2BF379CD2CDBD0
1 changed files with 16 additions and 16 deletions

View File

@ -25,13 +25,7 @@ my $dupes = 0;
my $skip = 0;
sub add_domain_list {
my $file = shift;
open my $fni, '<', $file
or die "Failed to open file $file for reading: $!";
foreach my $line (<$fni>) {
chomp $line;
$line =~ s/^\s+|\s+$//;
foreach my $line (&read_stripped($file)) {
my $domain = lc $line;
++$dupes if defined $domains{$domain};
$domains{$domain} = 1;
@ -40,15 +34,7 @@ sub add_domain_list {
sub add_host_file {
my $file = shift;
open my $fni, '<', $file
or die "Failed to open file $file for reading: $!";
foreach my $line (<$fni>) {
chomp $line;
# strip whitespace and comments
$line =~ s/^\s+|\s+$|\s*#.*$//;
next unless $line;
foreach my $line (&read_stripped($file)) {
my @parts = split /\s+/, $line;
die "Malformed line in $file: $line; @parts"
unless @parts > 1;
@ -66,6 +52,20 @@ sub add_host_file {
}
}
sub read_stripped {
my $file = shift;
open my $fni, '<', $file
or die "Failed to open file $file for reading: $!";
map {
chomp;
# Strip whitespace and comments
s/^\s+|\s+$|\s*#.*$//;
$_ || ()
} <$fni>;
}
MAIN: {
my $out;
my $block_ip = '0.0.0.0 ::1';