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 <avarab@gmail.com> Signed-off-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Sunshine committed May 10, 2019 at 20:18 UTC 142997d4895b2b39ade3560da774f181f0131086
1 file changed +5 -4
t/check-non-portable-shell.pl
+5 -4
@@ -27,14 +27,14 @@ for my $i (@ARGV) {
27 close $f;
28 }
29
30 +my $line = '';
31 while (<>) {
32 chomp;
33 + $line .= $_;
34 # stitch together incomplete lines (those ending with "\")
33 - while (s/\\$//) {
34 - $_ .= readline;
35 - chomp;
36 - }
35 + next if $line =~ s/\\$//;
36
37 + $_ = $line;
38 /\bcp\s+-a/ and err 'cp -a is not portable';
39 /\bsed\s+-[^efn]\s+/ and err 'sed option not portable (use only -n, -e, -f)';
40 /\becho\s+-[neE]/ and err 'echo with option is not portable (use printf)';
@@ -48,6 +48,7 @@ while (<>) {
48 /\bexport\s+[A-Za-z0-9_]*=/ and err '"export FOO=bar" is not portable (use FOO=bar && export FOO)';
49 /^\s*([A-Z0-9_]+=(\w+|(["']).*?\3)\s+)+(\w+)/ and exists($func{$4}) and
50 err '"FOO=bar shell_func" assignment extends beyond "shell_func"';
51 + $line = '';
52 # this resets our $. for each file
53 close ARGV if eof;
54 }