Skip to content

Commit 142997d

Browse files
sunshinecogitster
authored andcommitted
check-non-portable-shell: support Perl versions older than 5.10
For thoroughness when checking for one-shot environment variable assignments at shell function call sites, check-non-portable-shell stitches together incomplete lines (those ending with backslash). This allows it to correctly flag such undesirable usage even when the variable assignment and function call are split across lines, for example: FOO=bar \ func where 'func' is a shell function. The stitching is accomplished like this: while (<>) { chomp; # stitch together incomplete lines (those ending with "\") while (s/\\$//) { $_ .= readline; chomp; } # detect unportable/undesirable shell constructs ... } Although this implementation is well supported in reasonably modern Perl versions (5.10 and later), it fails with older versions (such as Perl 5.8 shipped with ancient Mac OS 10.5). In particular, in older Perl versions, 'readline' is not connected to the file handle associated with the "magic" while (<>) {...} construct, so 'readline' throws a "readline() on unopened filehandle" error. Work around this problem by dropping readline() and instead incorporating the stitching of incomplete lines directly into the existing while (<>) {...} loop. Helped-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6a6c0f1 commit 142997d

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

t/check-non-portable-shell.pl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ sub err {
2727
close $f;
2828
}
2929

30+
my $line = '';
3031
while (<>) {
3132
chomp;
33+
$line .= $_;
3234
# stitch together incomplete lines (those ending with "\")
33-
while (s/\\$//) {
34-
$_ .= readline;
35-
chomp;
36-
}
35+
next if $line =~ s/\\$//;
3736

37+
$_ = $line;
3838
/\bcp\s+-a/ and err 'cp -a is not portable';
3939
/\bsed\s+-[^efn]\s+/ and err 'sed option not portable (use only -n, -e, -f)';
4040
/\becho\s+-[neE]/ and err 'echo with option is not portable (use printf)';
@@ -48,6 +48,7 @@ sub err {
4848
/\bexport\s+[A-Za-z0-9_]*=/ and err '"export FOO=bar" is not portable (use FOO=bar && export FOO)';
4949
/^\s*([A-Z0-9_]+=(\w+|(["']).*?\3)\s+)+(\w+)/ and exists($func{$4}) and
5050
err '"FOO=bar shell_func" assignment extends beyond "shell_func"';
51+
$line = '';
5152
# this resets our $. for each file
5253
close ARGV if eof;
5354
}

0 commit comments

Comments
 (0)