Skip to content

Commit d783ed2

Browse files
authored
asa: regex too greedy (#476)
* Problem1: asa would refuse to open a file with a dash * Problem2: if opening a file fails, asa did not proceed to the next file argument * This patch brings the code closer to NetBSD version [1] * test1: "echo hi | perl asa" --> no args; read stdin * test2: "perl asa real-file" --> one file argument; real-file is not a command option * test3: "perl asa notfound real-file" --> warn for not opening 'notfound', process 'real-file' then exit with code 1 to indicate error 1. http://cvsweb.netbsd.org/bsdweb.cgi/src/usr.bin/asa/asa.c?annotate=1.17
1 parent 2f381a8 commit d783ed2

File tree

1 file changed

+43
-16
lines changed

1 file changed

+43
-16
lines changed

bin/asa

+43-16
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,51 @@ License: perl
1414

1515
use strict;
1616

17-
exit 1 if grep {!-r} @ARGV; # traditional
17+
use File::Basename qw(basename);
1818

19-
if (grep /-/, @ARGV) {
20-
$0 =~ s(.*/)();
21-
warn "usage: $0 [filename ...]\n";
22-
exit 2; # traditional
23-
}
19+
use constant EX_SUCCESS => 0;
20+
use constant EX_FAILURE => 1;
21+
22+
my $Program = basename($0);
2423

25-
while (<>) {
26-
chomp;
27-
s/^$/ /;
28-
s/^[^10+-]/\n/;
29-
s/^1/\f/;
30-
s/^\+/\r/;
31-
s/^0/\n\n/;
32-
s/^-/\n\n\n/;
33-
print
34-
or exit 1; # traditional
24+
if (grep /\A\-/, @ARGV) {
25+
warn "usage: $Program [file ...]\n";
26+
exit EX_FAILURE;
27+
}
28+
my $rc = EX_SUCCESS;
29+
foreach my $file (@ARGV) {
30+
next if (-d $file);
31+
my $fh;
32+
unless (open $fh, '<', $file) {
33+
warn "$Program: Can't open '$file': $!\n";
34+
$rc = EX_FAILURE;
35+
next;
36+
}
37+
run_file($fh);
38+
39+
unless (close $fh) {
40+
warn "$Program: Can't close '$file': $!\n";
41+
$rc = EX_FAILURE;
42+
}
43+
}
44+
unless (@ARGV) {
45+
run_file(*STDIN);
46+
}
47+
exit $rc;
48+
49+
sub run_file {
50+
my $fh = shift;
51+
52+
while (<$fh>) {
53+
chomp;
54+
s/^$/ /;
55+
s/^[^10+-]/\n/;
56+
s/^1/\f/;
57+
s/^\+/\r/;
58+
s/^0/\n\n/;
59+
s/^-/\n\n\n/;
60+
print;
61+
}
3562
}
3663

3764
=head1 NAME

0 commit comments

Comments
 (0)