From 6c20c8ed2cb9ab69a1a57ccb2b9b79969a808321 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 17:38:56 +0200 Subject: Adding upstream version 5.2.15. Signed-off-by: Daniel Baumann --- support/checkbashisms | 170 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100755 support/checkbashisms (limited to 'support/checkbashisms') diff --git a/support/checkbashisms b/support/checkbashisms new file mode 100755 index 0000000..ea6cc14 --- /dev/null +++ b/support/checkbashisms @@ -0,0 +1,170 @@ +#! /usr/bin/perl -w + +# This script is essentially copied from /usr/share/lintian/checks/scripts, +# which is: +# Copyright (C) 1998 Richard Braakman +# Copyright (C) 2002 Josip Rodin +# This version is +# Copyright (C) 2003 Julian Gilbey +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +use strict; + +(my $progname = $0) =~ s|.*/||; + +my $usage = <<"EOF"; +Usage: $progname [-n] script ... + or: $progname --help + or: $progname --version +This script performs basic checks for the presence of bashisms +in /bin/sh scripts. +EOF + +my $version = <<"EOF"; +This is $progname, from the Debian devscripts package, version 2.10.7ubuntu5 +This code is copyright 2003 by Julian Gilbey , +based on original code which is copyright 1998 by Richard Braakman +and copyright 2002 by Josip Rodin. +This program comes with ABSOLUTELY NO WARRANTY. +You are free to redistribute this code under the terms of the +GNU General Public License, version 3, or (at your option) any later version. +EOF + +my $opt_echo = 0; + +## +## handle command-line options +## +if (int(@ARGV) == 0 or $ARGV[0] =~ /^(--help|-h)$/) { print $usage; exit 0; } +if (@ARGV and $ARGV[0] =~ /^(--version|-v)$/) { print $version; exit 0; } +if (@ARGV and $ARGV[0] =~ /^(--newline|-n)$/) { $opt_echo = 1; } + + +my $status = 0; + +foreach my $filename (@ARGV) { + if ($filename eq '-n' or $filename eq '--newline') { + next; + } + unless (open C, "$filename") { + warn "cannot open script $filename for reading: $!\n"; + $status |= 2; + next; + } + + my $cat_string = ""; + + while () { + if ($. == 1) { # This should be an interpreter line + if (m,^\#!\s*(\S+),) { + my $interpreter = $1; + if ($interpreter =~ m,/bash$,) { + warn "script $filename is already a bash script; skipping\n"; + $status |= 2; + last; # end this file + } + elsif ($interpreter !~ m,/(sh|ash|dash)$,) { + warn "script $filename does not appear to be a /bin/sh script; skipping\n"; + $status |= 2; + last; + } + } else { + warn "script $filename does not appear to have a \#! interpreter line;\nyou may get strange results\n"; + } + } + + next if m,^\s*\#,; # skip comment lines + chomp; + my $orig_line = $_; + + s/(? q<'function' is useless>, + '(?:^|\s+)select\s+\w+' => q<'select' is not POSIX>, + '(?:^|\s+)source\s+(?:\.\/|\/|\$)[^\s]+' => + q, + '(\[|test|-o|-a)\s*[^\s]+\s+==\s' => + q, + '\s\|\&' => q, + '\$\[\w+\]' => q, + '\$\{\w+\:\d+(?::\d+)?\}' => q<${foo:3[:1]}>, + '\$\{!\w+[@*]\}' => q<${!prefix[*|@]>, + '\$\{!\w+\}' => q<${!name}>, + '\$\{\w+(/.+?){1,2}\}' => q<${parm/?/pat[/str]}>, + '[^\\\]\{([^\s]+?,)+[^\\\}\s]+\}' => + q, + '(?:^|\s+)\w+\[\d+\]=' => q, + '\$\{\#?\w+\[[0-9\*\@]+\]\}' => q, + '(?:^|\s+)(read\s*(?:;|$))' => q, + '\$\(\([A-Za-z]' => q, + 'echo\s+-[e]' => q, + 'exec\s+-[acl]' => q, + '\blet\s' => q, + '\$RANDOM\b' => q<$RANDOM>, + '(? q<'((' should be '$(('>, + ); + + if ($opt_echo) { + $bashisms{'echo\s+-[n]'} = 'q'; + } + + while (my ($re,$expl) = each %bashisms) { + if (m/($re)/) { + $found = 1; + $match = $1; + $explanation = $expl; + last; + } + } + # since this test is ugly, I have to do it by itself + # detect source (.) trying to pass args to the command it runs + if (not $found and m/^\s*(\.\s+[^\s]+\s+([^\s]+))/) { + if ($2 eq '&&' || $2 eq '||') { + # everything is ok + ; + } else { + $found = 1; + $match = $1; + } + } + unless ($found == 0) { + warn "possible bashism in $filename line $. ($explanation):\n$orig_line\n"; + $status |= 1; + } + } + } + + close C; +} + +exit $status; -- cgit v1.2.3