From fd8061714f6c5c097dd56d60d567fc9d24849a50 Mon Sep 17 00:00:00 2001 From: Dan Church Date: Fri, 25 Nov 2022 15:40:20 -0600 Subject: [PATCH] Use a common method of reading lines Adds comment support for domain lists. --- make-block.pl | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/make-block.pl b/make-block.pl index 43d2fd1..ac9be32 100755 --- a/make-block.pl +++ b/make-block.pl @@ -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';