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