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;
|
|
|
|
}
|
|
|
|
|
2013-01-03 00:20:19 +01:00
|
|
|
while (<>) {
|
|
|
|
chomp;
|
2018-07-13 07:52:05 +02:00
|
|
|
# stitch together incomplete lines (those ending with "\")
|
|
|
|
while (s/\\$//) {
|
|
|
|
$_ .= readline;
|
|
|
|
chomp;
|
|
|
|
}
|
|
|
|
|
2014-05-23 20:44:05 +02:00
|
|
|
/\bsed\s+-i/ and err 'sed -i is not portable';
|
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-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"';
|
2013-01-03 00:20:19 +01:00
|
|
|
# this resets our $. for each file
|
|
|
|
close ARGV if eof;
|
|
|
|
}
|
|
|
|
exit $exit_code;
|