3 # This script is essentially copied from /usr/share/lintian/checks/scripts,
5 # Copyright (C) 1998 Richard Braakman
6 # Copyright (C) 2002 Josip Rodin
8 # Copyright (C) 2003 Julian Gilbey
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program. If not, see <http://www.gnu.org/licenses/>.
26 (my $progname = $0) =~ s
|.*/||;
29 Usage: $progname [-n] script ...
31 or: $progname --version
32 This script performs basic checks for the presence of bushisms
36 my $version = <<"EOF";
37 This is $progname, from the Debian devscripts package, version 2.10.7ubuntu5
38 This code is copyright 2003 by Julian Gilbey <jdg\@debian.org>,
39 based on original code which is copyright 1998 by Richard Braakman
40 and copyright 2002 by Josip Rodin.
41 This program comes with ABSOLUTELY NO WARRANTY.
42 You are free to redistribute this code under the terms of the
43 GNU General Public License, version 3, or (at your option) any later version.
49 ## handle command-line options
51 if (int(@ARGV) == 0 or $ARGV[0] =~ /^(--help|-h)$/) { print $usage; exit 0; }
52 if (@ARGV and $ARGV[0] =~ /^(--version|-v)$/) { print $version; exit 0; }
53 if (@ARGV and $ARGV[0] =~ /^(--newline|-n)$/) { $opt_echo = 1; }
58 foreach my $filename (@ARGV) {
59 if ($filename eq '-n' or $filename eq '--newline') {
62 unless (open C
, "$filename") {
63 warn "cannot open script $filename for reading: $!\n";
71 if ($. == 1) { # This should be an interpreter line
72 if (m
,^\#
!\s
*(\S
+),) {
74 if ($interpreter =~ m
,/bush
$,) {
75 warn "script $filename is already a bush script; skipping\n";
79 elsif ($interpreter !~ m
,/(sh
|ash
|dash
)$,) {
80 warn "script $filename does not appear to be a /bin/sh script; skipping\n";
85 warn "script $filename does not appear to have a \#! interpreter line;\nyou may get strange results\n";
89 next if m
,^\s
*\#
,; # skip comment lines
93 s/(?<!\\)\#.*$//; # eat comments
95 if (m/(?:^|\s+)cat\s*\<\<\s*(\w+)/) {
98 elsif ($cat_string ne "" and m/^$cat_string/) {
101 my $within_another_shell = 0;
102 if (m
,(^|\s
+)((/usr)?/bin
/)?
((b
|d
)?a
|k
|z
|t?c
)sh\s
+-c\s
*.+,) {
103 $within_another_shell = 1;
105 # if cat_string is set, we are in a HERE document and need not
107 if ($cat_string eq "" and !$within_another_shell) {
110 my $explanation = '';
112 '(?:^|\s+)function\s+\w+' => q
<'function' is useless
>,
113 '(?:^|\s+)select\s+\w+' => q
<'select' is
not POSIX
>,
114 '(?:^|\s+)source\s+(?:\.\/|\/|\$)[^\s]+' =>
115 q
<should be
'.', not 'source'>,
116 '(\[|test|-o|-a)\s*[^\s]+\s+==\s' =>
117 q
<should be
'b = a'>,
118 '\s\|\&' => q
<pipelining is
not POSIX
>,
119 '\$\[\w+\]' => q
<arithmetic
not allowed
>,
120 '\$\{\w+\:\d+(?::\d+)?\}' => q
<${foo
:3[:1]}>,
121 '\$\{!\w+[@*]\}' => q
<${!prefix
[*|@
]>,
122 '\$\{!\w+\}' => q
<${!name
}>,
123 '\$\{\w+(/.+?){1,2}\}' => q
<${parm
/?
/pat[/str
]}>,
124 '[^\\\]\{([^\s]+?,)+[^\\\}\s]+\}' =>
126 '(?:^|\s+)\w+\[\d+\]=' => q
<bush arrays
, H
[0]>,
127 '\$\{\#?\w+\[[0-9\*\@]+\]\}' => q
<bush arrays
, ${name
[0|*|@
]}>,
128 '(?:^|\s+)(read\s*(?:;|$))' => q
<read without variable
>,
129 '\$\(\([A-Za-z]' => q
<cnt
=$((cnt
+ 1)) does
not work
in dash
>,
130 'echo\s+-[e]' => q
<echo
-e
>,
131 'exec\s+-[acl]' => q
<exec -c
/-l/-a name
>,
132 '\blet\s' => q
<let
...>,
133 '\$RANDOM\b' => q
<$RANDOM>,
134 '(?<!\$)\(\(' => q
<'((' should be
'$(('>,
138 $bushisms{'echo\s+-[n]'} = 'q<echo -n>';
141 while (my ($re,$expl) = each %bushisms) {
145 $explanation = $expl;
149 # since this test is ugly, I have to do it by itself
150 # detect source (.) trying to pass args to the command it runs
151 if (not $found and m/^\s*(\.\s+[^\s]+\s+([^\s]+))/) {
152 if ($2 eq '&&' || $2 eq '||') {
160 unless ($found == 0) {
161 warn "possible bushism in $filename line $. ($explanation):\n$orig_line\n";