2013-01-02 23:20:19 +00:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
# Test t0000..t9999.sh for non portable shell scripts
|
|
|
|
# This script can be called with one or more filenames as parameters
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
my $exit_code=0;
|
2018-07-13 05:52:05 +00:00
|
|
|
my %func;
|
2013-01-02 23:20:19 +00:00
|
|
|
|
|
|
|
sub err {
|
|
|
|
my $msg = shift;
|
2018-07-13 05:52:04 +00:00
|
|
|
s/^\s+//;
|
|
|
|
s/\s+$//;
|
2018-07-13 05:52:05 +00:00
|
|
|
s/\s+/ /g;
|
2013-01-02 23:20:19 +00:00
|
|
|
print "$ARGV:$.: error: $msg: $_\n";
|
|
|
|
$exit_code = 1;
|
|
|
|
}
|
|
|
|
|
2018-07-13 05:52:05 +00:00
|
|
|
# glean names of shell functions
|
|
|
|
for my $i (@ARGV) {
|
|
|
|
open(my $f, '<', $i) or die "$0: $i: $!\n";
|
|
|
|
while (<$f>) {
|
|
|
|
$func{$1} = 1 if /^\s*(\w+)\s*\(\)\s*{\s*$/;
|
|
|
|
}
|
|
|
|
close $f;
|
|
|
|
}
|
|
|
|
|
2019-05-11 00:18:53 +00:00
|
|
|
my $line = '';
|
2013-01-02 23:20:19 +00:00
|
|
|
while (<>) {
|
|
|
|
chomp;
|
2019-05-11 00:18:53 +00:00
|
|
|
$line .= $_;
|
2018-07-13 05:52:05 +00:00
|
|
|
# stitch together incomplete lines (those ending with "\")
|
2019-05-11 00:18:53 +00:00
|
|
|
next if $line =~ s/\\$//;
|
2018-07-13 05:52:05 +00:00
|
|
|
|
2019-05-11 00:18:53 +00:00
|
|
|
$_ = $line;
|
2018-12-01 17:36:45 +00:00
|
|
|
/\bcp\s+-a/ and err 'cp -a is not portable';
|
test-lint: only use only sed [-n] [-e command] [-f command_file]
From `man sed` (on a Mac OS X box):
The -E, -a and -i options are non-standard FreeBSD extensions and may not be available
on other operating systems.
From `man sed` on a Linux box:
REGULAR EXPRESSIONS
POSIX.2 BREs should be supported, but they aren't completely because of
performance problems. The \n sequence in a regular expression matches the newline
character, and similarly for \a, \t, and other sequences.
The -E option switches to using extended regular expressions instead; the -E option
has been supported for years by GNU sed, and is now included in POSIX.
Well, there are still a lot of systems out there, which don't support it.
Beside that, IEEE Std 1003.1TM-2017, see
http://pubs.opengroup.org/onlinepubs/9699919799/
does not mention -E either.
To be on the safe side, don't allow -E (or -r, which is GNU).
Change check-non-portable-shell.pl to only accept the portable options:
sed [-n] [-e command] [-f command_file]
Reported-by: SZEDER Gábor <szeder.dev@gmail.com>
Helped-by: Eric Sunshine <sunshine@sunshineco.com>
Helped-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Torsten Bögershausen <tboegi@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2019-01-20 07:53:50 +00:00
|
|
|
/\bsed\s+-[^efn]\s+/ and err 'sed option not portable (use only -n, -e, -f)';
|
2018-07-13 05:52:03 +00:00
|
|
|
/\becho\s+-[neE]/ and err 'echo with option is not portable (use printf)';
|
2013-01-02 23:20:19 +00:00
|
|
|
/^\s*declare\s+/ and err 'arrays/declare not portable';
|
2018-07-13 05:52:03 +00:00
|
|
|
/^\s*[^#]\s*which\s/ and err 'which is not portable (use type)';
|
|
|
|
/\btest\s+[^=]*==/ and err '"test a == b" is not portable (use =)';
|
|
|
|
/\bwc -l.*"\s*=/ and err '`"$(wc -l)"` is not portable (use test_line_count)';
|
2018-08-24 15:20:11 +00:00
|
|
|
/\bhead\s+-c\b/ and err 'head -c is not portable (use test_copy_bytes BYTES <file >out)';
|
2018-08-24 15:20:12 +00:00
|
|
|
/(?:\$\(seq|^\s*seq\b)/ and err 'seq is not portable (use test_seq)';
|
2018-08-24 15:20:16 +00:00
|
|
|
/\bgrep\b.*--file\b/ and err 'grep --file FILE is not portable (use grep -f FILE)';
|
2018-07-13 05:52:03 +00:00
|
|
|
/\bexport\s+[A-Za-z0-9_]*=/ and err '"export FOO=bar" is not portable (use FOO=bar && export FOO)';
|
2019-12-26 19:57:47 +00:00
|
|
|
/^\s*([A-Z0-9_]+=(\w*|(["']).*?\3)\s+)+(\w+)/ and exists($func{$4}) and
|
2018-07-13 05:52:05 +00:00
|
|
|
err '"FOO=bar shell_func" assignment extends beyond "shell_func"';
|
2019-05-11 00:18:53 +00:00
|
|
|
$line = '';
|
2013-01-02 23:20:19 +00:00
|
|
|
# this resets our $. for each file
|
|
|
|
close ARGV if eof;
|
|
|
|
}
|
|
|
|
exit $exit_code;
|