Skip to content

Commit c3afc01

Browse files
authored
units: fail if custom unittab file doesn't open (#707)
* units: fail if custom unittab file doesn't open * The old code unhelpfully bypassed opening the file specified by -f FILE option if it did not exist * Make read_unittab() handle 3 cases... * case1: implicit unittab ($file is undefined; read definitions in __DATA__) * case2: $file is a directory (ignore it to match OpenBSD; read __DATA__) * case3: otherwise... open the file and terminate the program if it couldn't open * While here add defined() checks after prompting for input; this silences warnings seen when terminating program with ^D * I found this when testing against the OpenBSD version * missing return statement in prev commit
1 parent 4239cb9 commit c3afc01

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

bin/units

+16-17
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@ sub run {
9494
while (1) {
9595
print "You have: ";
9696
my $have = <>;
97-
exit 0 unless $have =~ /\S/;
97+
exit 0 unless defined($have) && $have =~ /\S/;
9898
my $have_hr = $class->unit_have($have);
9999
next if is_Zero($have_hr->{hu});
100100

101101
print "You want: ";
102102
my $want = <>;
103-
exit 0 unless $want =~ /\S/;
103+
exit 0 unless defined($want) && $want =~ /\S/;
104104
my $want_hr = $class->unit_want($want);
105105
next if is_Zero($want_hr->{wu});
106106

@@ -159,23 +159,22 @@ sub process_args {
159159
sub read_unittab {
160160
my( $class, $file ) = @_;
161161

162-
my( $name, $fh ) = do {
163-
if( defined $file and -e $file ) {
164-
open my($fh), '<:encoding(UTF-8)', $file or do {
165-
die "Could not open <$file>: Aborting.";
162+
my $fh;
163+
if (defined $file) {
164+
unless (-d $file) {
165+
open $fh, '<:encoding(UTF-8)', $file or do {
166+
die "Could not open <$file>: $!\n";
166167
};
167-
( $file, $fh )
168+
$class->read_defs($file, $fh);
169+
return;
168170
}
169-
else {
170-
debug_d( "Reading from DATA" );
171-
open my $fh, '<:encoding(UTF-8)', __FILE__ or die;
172-
while( <$fh> ) { last if /\A__(?:DATA|END)__/ }
173-
( 'DATA', $fh );
174-
}
175-
176-
};
177-
178-
$class->read_defs( $name, $fh );
171+
}
172+
debug_d('Reading from DATA');
173+
open $fh, '<:encoding(UTF-8)', __FILE__ or die;
174+
while (<$fh>) {
175+
last if /\A__(?:DATA|END)__/;
176+
}
177+
$class->read_defs('DATA', $fh);
179178
}
180179

181180
sub unit_have {

0 commit comments

Comments
 (0)