2013-01-03 07:20:19 +08: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 13:52:05 +08:00
|
|
|
my %func;
|
2013-01-03 07:20:19 +08:00
|
|
|
|
|
|
|
sub err {
|
|
|
|
my $msg = shift;
|
2018-07-13 13:52:04 +08:00
|
|
|
s/^\s+//;
|
|
|
|
s/\s+$//;
|
2018-07-13 13:52:05 +08:00
|
|
|
s/\s+/ /g;
|
2013-01-03 07:20:19 +08:00
|
|
|
print "$ARGV:$.: error: $msg: $_\n";
|
|
|
|
$exit_code = 1;
|
|
|
|
}
|
|
|
|
|
2018-07-13 13:52:05 +08: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 08:18:53 +08:00
|
|
|
my $line = '';
|
2013-01-03 07:20:19 +08:00
|
|
|
while (<>) {
|
|
|
|
chomp;
|
2019-05-11 08:18:53 +08:00
|
|
|
$line .= $_;
|
2018-07-13 13:52:05 +08:00
|
|
|
# stitch together incomplete lines (those ending with "\")
|
2019-05-11 08:18:53 +08:00
|
|
|
next if $line =~ s/\\$//;
|
2018-07-13 13:52:05 +08:00
|
|
|
|
2019-05-11 08:18:53 +08:00
|
|
|
$_ = $line;
|
2018-12-02 01:36:45 +08: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 15:53:50 +08:00
|
|
|
/\bsed\s+-[^efn]\s+/ and err 'sed option not portable (use only -n, -e, -f)';
|
2018-07-13 13:52:03 +08:00
|
|
|
/\becho\s+-[neE]/ and err 'echo with option is not portable (use printf)';
|
2013-01-03 07:20:19 +08:00
|
|
|
/^\s*declare\s+/ and err 'arrays/declare not portable';
|
2018-07-13 13:52:03 +08: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 23:20:11 +08:00
|
|
|
/\bhead\s+-c\b/ and err 'head -c is not portable (use test_copy_bytes BYTES <file >out)';
|
2018-08-24 23:20:12 +08:00
|
|
|
/(?:\$\(seq|^\s*seq\b)/ and err 'seq is not portable (use test_seq)';
|
2018-08-24 23:20:16 +08:00
|
|
|
/\bgrep\b.*--file\b/ and err 'grep --file FILE is not portable (use grep -f FILE)';
|
2022-09-22 08:18:25 +08:00
|
|
|
/\b[ef]grep\b/ and err 'egrep/fgrep obsolescent (use grep -E/-F)';
|
2018-07-13 13:52:03 +08:00
|
|
|
/\bexport\s+[A-Za-z0-9_]*=/ and err '"export FOO=bar" is not portable (use FOO=bar && export FOO)';
|
2019-12-27 03:57:47 +08:00
|
|
|
/^\s*([A-Z0-9_]+=(\w*|(["']).*?\3)\s+)+(\w+)/ and exists($func{$4}) and
|
2018-07-13 13:52:05 +08:00
|
|
|
err '"FOO=bar shell_func" assignment extends beyond "shell_func"';
|
2019-05-11 08:18:53 +08:00
|
|
|
$line = '';
|
2013-01-03 07:20:19 +08:00
|
|
|
# this resets our $. for each file
|
|
|
|
close ARGV if eof;
|
|
|
|
}
|
|
|
|
exit $exit_code;
|