checkbashisms.pl.in 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. #!@PERL@
  2. # This script is essentially copied from /usr/share/lintian/checks/scripts,
  3. # which is:
  4. # Copyright (C) 1998 Richard Braakman
  5. # Copyright (C) 2002 Josip Rodin
  6. # This version is
  7. # Copyright (C) 2003 Julian Gilbey
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. use strict;
  22. use warnings;
  23. use Getopt::Long qw(:config bundling permute no_getopt_compat);
  24. use File::Temp qw/tempfile/;
  25. sub init_hashes;
  26. (my $progname = $0) =~ s|.*/||;
  27. my $usage = <<"EOF";
  28. Usage: $progname [-n] [-f] [-x] script ...
  29. or: $progname --help
  30. or: $progname --version
  31. This script performs basic checks for the presence of bashisms
  32. in /bin/sh scripts and the lack of bashisms in /bin/bash ones.
  33. EOF
  34. my $version = <<"EOF";
  35. This is $progname, from the Debian devscripts package, version ###VERSION###
  36. This code is copyright 2003 by Julian Gilbey <jdg\@debian.org>,
  37. based on original code which is copyright 1998 by Richard Braakman
  38. and copyright 2002 by Josip Rodin.
  39. This program comes with ABSOLUTELY NO WARRANTY.
  40. You are free to redistribute this code under the terms of the
  41. GNU General Public License, version 2, or (at your option) any later version.
  42. EOF
  43. my ($opt_echo, $opt_force, $opt_extra, $opt_posix);
  44. my ($opt_help, $opt_version);
  45. my @filenames;
  46. # Detect if STDIN is a pipe
  47. if (scalar(@ARGV) == 0 && (-p STDIN or -f STDIN)) {
  48. push(@ARGV, '-');
  49. }
  50. ##
  51. ## handle command-line options
  52. ##
  53. $opt_help = 1 if int(@ARGV) == 0;
  54. GetOptions(
  55. "help|h" => \$opt_help,
  56. "version|v" => \$opt_version,
  57. "newline|n" => \$opt_echo,
  58. "force|f" => \$opt_force,
  59. "extra|x" => \$opt_extra,
  60. "posix|p" => \$opt_posix,
  61. )
  62. or die
  63. "Usage: $progname [options] filelist\nRun $progname --help for more details\n";
  64. if ($opt_help) { print $usage; exit 0; }
  65. if ($opt_version) { print $version; exit 0; }
  66. $opt_echo = 1 if $opt_posix;
  67. my $mode = 0;
  68. my $issues = 0;
  69. my $status = 0;
  70. my $makefile = 0;
  71. my (%bashisms, %string_bashisms, %singlequote_bashisms);
  72. my $LEADIN
  73. = qr'(?:(?:^|[`&;(|{])\s*|(?:(?:if|elif|while)(?:\s+!)?|then|do|shell)\s+)';
  74. init_hashes;
  75. my @bashisms_keys = sort keys %bashisms;
  76. my @string_bashisms_keys = sort keys %string_bashisms;
  77. my @singlequote_bashisms_keys = sort keys %singlequote_bashisms;
  78. foreach my $filename (@ARGV) {
  79. my $check_lines_count = -1;
  80. my $display_filename = $filename;
  81. if ($filename eq '-') {
  82. my $tmp_fh;
  83. ($tmp_fh, $filename)
  84. = tempfile("chkbashisms_tmp.XXXX", TMPDIR => 1, UNLINK => 1);
  85. while (my $line = <STDIN>) {
  86. print $tmp_fh $line;
  87. }
  88. close($tmp_fh);
  89. $display_filename = "(stdin)";
  90. }
  91. if (!$opt_force) {
  92. $check_lines_count = script_is_evil_and_wrong($filename);
  93. }
  94. if ($check_lines_count == 0 or $check_lines_count == 1) {
  95. warn
  96. "script $display_filename does not appear to be a /bin/sh script; skipping\n";
  97. next;
  98. }
  99. if ($check_lines_count != -1) {
  100. warn
  101. "script $display_filename appears to be a shell wrapper; only checking the first "
  102. . "$check_lines_count lines\n";
  103. }
  104. unless (open C, '<', $filename) {
  105. warn "cannot open script $display_filename for reading: $!\n";
  106. $status |= 2;
  107. next;
  108. }
  109. $issues = 0;
  110. $mode = 0;
  111. my $cat_string = "";
  112. my $cat_indented = 0;
  113. my $quote_string = "";
  114. my $last_continued = 0;
  115. my $continued = 0;
  116. my $found_rules = 0;
  117. my $buffered_orig_line = "";
  118. my $buffered_line = "";
  119. my %start_lines;
  120. while (<C>) {
  121. next unless ($check_lines_count == -1 or $. <= $check_lines_count);
  122. if ($. == 1) { # This should be an interpreter line
  123. if (m,^\#!\s*(?:\S+/env\s+)?(\S+),) {
  124. my $interpreter = $1;
  125. if ($interpreter =~ m,(?:^|/)make$,) {
  126. init_hashes if !$makefile++;
  127. $makefile = 1;
  128. } else {
  129. init_hashes if $makefile--;
  130. $makefile = 0;
  131. }
  132. next if $opt_force;
  133. if ($interpreter =~ m,(?:^|/)bash$,) {
  134. $mode = 1;
  135. } elsif ($interpreter !~ m,(?:^|/)(sh|dash|posh)$,) {
  136. ### ksh/zsh?
  137. warn
  138. "script $display_filename does not appear to be a /bin/sh script; skipping\n";
  139. $status |= 2;
  140. last;
  141. }
  142. } else {
  143. warn
  144. "script $display_filename does not appear to have a \#! interpreter line;\nyou may get strange results\n";
  145. }
  146. }
  147. chomp;
  148. my $orig_line = $_;
  149. # We want to remove end-of-line comments, so need to skip
  150. # comments that appear inside balanced pairs
  151. # of single or double quotes
  152. # Remove comments in the "quoted" part of a line that starts
  153. # in a quoted block? The problem is that we have no idea
  154. # whether the program interpreting the block treats the
  155. # quote character as part of the comment or as a quote
  156. # terminator. We err on the side of caution and assume it
  157. # will be treated as part of the comment.
  158. # s/^(?:.*?[^\\])?$quote_string(.*)$/$1/ if $quote_string ne "";
  159. # skip comment lines
  160. if ( m,^\s*\#,
  161. && $quote_string eq ''
  162. && $buffered_line eq ''
  163. && $cat_string eq '') {
  164. next;
  165. }
  166. # Remove quoted strings so we can more easily ignore comments
  167. # inside them
  168. s/(^|[^\\](?:\\\\)*)\'(?:\\.|[^\\\'])+\'/$1''/g;
  169. s/(^|[^\\](?:\\\\)*)\"(?:\\.|[^\\\"])+\"/$1""/g;
  170. # If inside a quoted string, remove everything before the quote
  171. s/^.+?\'//
  172. if ($quote_string eq "'");
  173. s/^.+?[^\\]\"//
  174. if ($quote_string eq '"');
  175. # If the remaining string contains what looks like a comment,
  176. # eat it. In either case, swap the unmodified script line
  177. # back in for processing.
  178. if (m/(?:^|[^[\\])[\s\&;\(\)](\#.*$)/) {
  179. $_ = $orig_line;
  180. s/\Q$1\E//; # eat comments
  181. } else {
  182. $_ = $orig_line;
  183. }
  184. # Handle line continuation
  185. if (!$makefile && $cat_string eq '' && m/\\$/) {
  186. chop;
  187. $buffered_line .= $_;
  188. $buffered_orig_line .= $orig_line . "\n";
  189. next;
  190. }
  191. if ($buffered_line ne '') {
  192. $_ = $buffered_line . $_;
  193. $orig_line = $buffered_orig_line . $orig_line;
  194. $buffered_line = '';
  195. $buffered_orig_line = '';
  196. }
  197. if ($makefile) {
  198. $last_continued = $continued;
  199. if (/[^\\]\\$/) {
  200. $continued = 1;
  201. } else {
  202. $continued = 0;
  203. }
  204. # Don't match lines that look like a rule if we're in a
  205. # continuation line before the start of the rules
  206. if (/^[\w%-]+:+\s.*?;?(.*)$/
  207. and !($last_continued and !$found_rules)) {
  208. $found_rules = 1;
  209. $_ = $1 if $1;
  210. }
  211. last
  212. if m%^\s*(override\s|export\s)?\s*SHELL\s*:?=\s*(/bin/)?bash\s*%;
  213. # Remove "simple" target names
  214. s/^[\w%.-]+(?:\s+[\w%.-]+)*::?//;
  215. s/^\t//;
  216. s/(?<!\$)\$\((\w+)\)/\${$1}/g;
  217. s/(\$){2}/$1/g;
  218. s/^[\s\t]*[@-]{1,2}//;
  219. }
  220. if (
  221. $cat_string ne ""
  222. && (m/^\Q$cat_string\E$/
  223. || ($cat_indented && m/^\t*\Q$cat_string\E$/))
  224. ) {
  225. $cat_string = "";
  226. next;
  227. }
  228. my $within_another_shell = 0;
  229. if (m,(^|\s+)((/usr)?/bin/)?((b|d)?a|k|z|t?c)sh\s+-c\s*.+,) {
  230. $within_another_shell = 1;
  231. }
  232. # if cat_string is set, we are in a HERE document and need not
  233. # check for things
  234. if ($cat_string eq "" and !$within_another_shell) {
  235. my $found = 0;
  236. my $match = '';
  237. my $explanation = '';
  238. my $line = $_;
  239. # Remove "" / '' as they clearly aren't quoted strings
  240. # and not considering them makes the matching easier
  241. $line =~ s/(^|[^\\])(\'\')+/$1/g;
  242. $line =~ s/(^|[^\\])(\"\")+/$1/g;
  243. if ($quote_string ne "") {
  244. my $otherquote = ($quote_string eq "\"" ? "\'" : "\"");
  245. # Inside a quoted block
  246. if ($line =~ /(?:^|^.*?[^\\])$quote_string(.*)$/) {
  247. my $rest = $1;
  248. my $templine = $line;
  249. # Remove quoted strings delimited with $otherquote
  250. $templine
  251. =~ s/(^|[^\\])$otherquote[^$quote_string]*?[^\\]$otherquote/$1/g;
  252. # Remove quotes that are themselves quoted
  253. # "a'b"
  254. $templine
  255. =~ s/(^|[^\\])$otherquote.*?$quote_string.*?[^\\]$otherquote/$1/g;
  256. # "\""
  257. $templine
  258. =~ s/(^|[^\\])$quote_string\\$quote_string$quote_string/$1/g;
  259. # After all that, were there still any quotes left?
  260. my $count = () = $templine =~ /(^|[^\\])$quote_string/g;
  261. next if $count == 0;
  262. $count = () = $rest =~ /(^|[^\\])$quote_string/g;
  263. if ($count % 2 == 0) {
  264. # Quoted block ends on this line
  265. # Ignore everything before the closing quote
  266. $line = $rest || '';
  267. $quote_string = "";
  268. } else {
  269. next;
  270. }
  271. } else {
  272. # Still inside the quoted block, skip this line
  273. next;
  274. }
  275. }
  276. # Check even if we removed the end of a quoted block
  277. # in the previous check, as a single line can end one
  278. # block and begin another
  279. if ($quote_string eq "") {
  280. # Possible start of a quoted block
  281. for my $quote ("\"", "\'") {
  282. my $templine = $line;
  283. my $otherquote = ($quote eq "\"" ? "\'" : "\"");
  284. # Remove balanced quotes and their content
  285. while (1) {
  286. my ($length_single, $length_double) = (0, 0);
  287. # Determine which one would match first:
  288. if ($templine
  289. =~ m/(^.+?(?:^|[^\\\"](?:\\\\)*)\')[^\']*\'/) {
  290. $length_single = length($1);
  291. }
  292. if ($templine
  293. =~ m/(^.*?(?:^|[^\\\'](?:\\\\)*)\")(?:\\.|[^\\\"])+\"/
  294. ) {
  295. $length_double = length($1);
  296. }
  297. # Now simplify accordingly (shorter is preferred):
  298. if (
  299. $length_single != 0
  300. && ( $length_single < $length_double
  301. || $length_double == 0)
  302. ) {
  303. $templine =~ s/(^|[^\\\"](?:\\\\)*)\'[^\']*\'/$1/;
  304. } elsif ($length_double != 0) {
  305. $templine
  306. =~ s/(^|[^\\\'](?:\\\\)*)\"(?:\\.|[^\\\"])+\"/$1/;
  307. } else {
  308. last;
  309. }
  310. }
  311. # Don't flag quotes that are themselves quoted
  312. # "a'b"
  313. $templine =~ s/$otherquote.*?$quote.*?$otherquote//g;
  314. # "\""
  315. $templine =~ s/(^|[^\\])$quote\\$quote$quote/$1/g;
  316. # \' or \"
  317. $templine =~ s/\\[\'\"]//g;
  318. my $count = () = $templine =~ /(^|(?!\\))$quote/g;
  319. # If there's an odd number of non-escaped
  320. # quotes in the line it's almost certainly the
  321. # start of a quoted block.
  322. if ($count % 2 == 1) {
  323. $quote_string = $quote;
  324. $start_lines{'quote_string'} = $.;
  325. $line =~ s/^(.*)$quote.*$/$1/;
  326. last;
  327. }
  328. }
  329. }
  330. # since this test is ugly, I have to do it by itself
  331. # detect source (.) trying to pass args to the command it runs
  332. # The first expression weeds out '. "foo bar"'
  333. if ( not $found
  334. and not
  335. m/$LEADIN\.\s+(\"[^\"]+\"|\'[^\']+\'|\$\([^)]+\)+(?:\/[^\s;]+)?)\s*(\&|\||\d?>|<|;|\Z)/o
  336. and m/$LEADIN(\.\s+[^\s;\`:]+\s+([^\s;]+))/o) {
  337. if ($2 =~ /^(\&|\||\d?>|<)/) {
  338. # everything is ok
  339. ;
  340. } else {
  341. $found = 1;
  342. $match = $1;
  343. $explanation = "sourced script with arguments";
  344. output_explanation($display_filename, $orig_line,
  345. $explanation);
  346. }
  347. }
  348. # Remove "quoted quotes". They're likely to be inside
  349. # another pair of quotes; we're not interested in
  350. # them for their own sake and removing them makes finding
  351. # the limits of the outer pair far easier.
  352. $line =~ s/(^|[^\\\'\"])\"\'\"/$1/g;
  353. $line =~ s/(^|[^\\\'\"])\'\"\'/$1/g;
  354. foreach my $re (@singlequote_bashisms_keys) {
  355. my $expl = $singlequote_bashisms{$re};
  356. if ($line =~ m/($re)/) {
  357. $found = 1;
  358. $match = $1;
  359. $explanation = $expl;
  360. output_explanation($display_filename, $orig_line,
  361. $explanation);
  362. }
  363. }
  364. my $re = '(?<![\$\\\])\$\'[^\']+\'';
  365. if ($line =~ m/(.*)($re)/o) {
  366. my $count = () = $1 =~ /(^|[^\\])\'/g;
  367. if ($count % 2 == 0) {
  368. output_explanation($display_filename, $orig_line,
  369. q<$'...' should be "$(printf '...')">);
  370. }
  371. }
  372. # $cat_line contains the version of the line we'll check
  373. # for heredoc delimiters later. Initially, remove any
  374. # spaces between << and the delimiter to make the following
  375. # updates to $cat_line easier. However, don't remove the
  376. # spaces if the delimiter starts with a -, as that changes
  377. # how the delimiter is searched.
  378. my $cat_line = $line;
  379. $cat_line =~ s/(<\<-?)\s+(?!-)/$1/g;
  380. # Ignore anything inside single quotes; it could be an
  381. # argument to grep or the like.
  382. $line =~ s/(^|[^\\\"](?:\\\\)*)\'(?:\\.|[^\\\'])+\'/$1''/g;
  383. # As above, with the exception that we don't remove the string
  384. # if the quote is immediately preceded by a < or a -, so we
  385. # can match "foo <<-?'xyz'" as a heredoc later
  386. # The check is a little more greedy than we'd like, but the
  387. # heredoc test itself will weed out any false positives
  388. $cat_line =~ s/(^|[^<\\\"-](?:\\\\)*)\'(?:\\.|[^\\\'])+\'/$1''/g;
  389. $re = '(?<![\$\\\])\$\"[^\"]+\"';
  390. if ($line =~ m/(.*)($re)/o) {
  391. my $count = () = $1 =~ /(^|[^\\])\"/g;
  392. if ($count % 2 == 0) {
  393. output_explanation($display_filename, $orig_line,
  394. q<$"foo" should be eval_gettext "foo">);
  395. }
  396. }
  397. foreach my $re (@string_bashisms_keys) {
  398. my $expl = $string_bashisms{$re};
  399. if ($line =~ m/($re)/) {
  400. $found = 1;
  401. $match = $1;
  402. $explanation = $expl;
  403. output_explanation($display_filename, $orig_line,
  404. $explanation);
  405. }
  406. }
  407. # We've checked for all the things we still want to notice in
  408. # double-quoted strings, so now remove those strings as well.
  409. $line =~ s/(^|[^\\\'](?:\\\\)*)\"(?:\\.|[^\\\"])+\"/$1""/g;
  410. $cat_line =~ s/(^|[^<\\\'-](?:\\\\)*)\"(?:\\.|[^\\\"])+\"/$1""/g;
  411. foreach my $re (@bashisms_keys) {
  412. my $expl = $bashisms{$re};
  413. if ($line =~ m/($re)/) {
  414. $found = 1;
  415. $match = $1;
  416. $explanation = $expl;
  417. output_explanation($display_filename, $orig_line,
  418. $explanation);
  419. }
  420. }
  421. # This check requires the value to be compared, which could
  422. # be done in the regex itself but requires "use re 'eval'".
  423. # So it's better done in its own
  424. if ($line =~ m/$LEADIN((?:exit|return)\s+(\d{3,}))/o && $2 > 255) {
  425. $explanation = 'exit|return status code greater than 255';
  426. output_explanation($display_filename, $orig_line,
  427. $explanation);
  428. }
  429. # Only look for the beginning of a heredoc here, after we've
  430. # stripped out quoted material, to avoid false positives.
  431. if ($cat_line
  432. =~ m/(?:^|[^<])\<\<(\-?)\s*(?:(?!<|'|")((?:[^\s;>|]+(?:(?<=\\)[\s;>|])?)+)|[\'\"](.*?)[\'\"])/
  433. ) {
  434. $cat_indented = ($1 && $1 eq '-') ? 1 : 0;
  435. my $quoted = defined($3);
  436. $cat_string = $quoted ? $3 : $2;
  437. unless ($quoted) {
  438. # Now strip backslashes. Keep the position of the
  439. # last match in a variable, as s/// resets it back
  440. # to undef, but we don't want that.
  441. my $pos = 0;
  442. pos($cat_string) = $pos;
  443. while ($cat_string =~ s/\G(.*?)\\/$1/) {
  444. # position += length of match + the character
  445. # that followed the backslash:
  446. $pos += length($1) + 1;
  447. pos($cat_string) = $pos;
  448. }
  449. }
  450. $start_lines{'cat_string'} = $.;
  451. }
  452. }
  453. }
  454. warn
  455. "error: $display_filename: Unterminated heredoc found, EOF reached. Wanted: <$cat_string>, opened in line $start_lines{'cat_string'}\n"
  456. if ($cat_string ne '');
  457. warn
  458. "error: $display_filename: Unterminated quoted string found, EOF reached. Wanted: <$quote_string>, opened in line $start_lines{'quote_string'}\n"
  459. if ($quote_string ne '');
  460. warn "error: $display_filename: EOF reached while on line continuation.\n"
  461. if ($buffered_line ne '');
  462. close C;
  463. if ($mode && !$issues) {
  464. warn "could not find any possible bashisms in bash script $filename\n";
  465. $status |= 4;
  466. }
  467. }
  468. exit $status;
  469. sub output_explanation {
  470. my ($filename, $line, $explanation) = @_;
  471. if ($mode) {
  472. # When examining a bash script, just flag that there are indeed
  473. # bashisms present
  474. $issues = 1;
  475. } else {
  476. warn "possible bashism in $filename line $. ($explanation):\n$line\n";
  477. $status |= 1;
  478. }
  479. }
  480. # Returns non-zero if the given file is not actually a shell script,
  481. # just looks like one.
  482. sub script_is_evil_and_wrong {
  483. my ($filename) = @_;
  484. my $ret = -1;
  485. # lintian's version of this function aborts if the file
  486. # can't be opened, but we simply return as the next
  487. # test in the calling code handles reporting the error
  488. # itself
  489. open(IN, '<', $filename) or return $ret;
  490. my $i = 0;
  491. my $var = "0";
  492. my $backgrounded = 0;
  493. local $_;
  494. while (<IN>) {
  495. chomp;
  496. next if /^#/o;
  497. next if /^$/o;
  498. last if (++$i > 55);
  499. if (
  500. m~
  501. # the exec should either be "eval"ed or a new statement
  502. (^\s*|\beval\s*[\'\"]|(;|&&|\b(then|else))\s*)
  503. # eat anything between the exec and $0
  504. exec\s*.+\s*
  505. # optionally quoted executable name (via $0)
  506. .?\$$var.?\s*
  507. # optional "end of options" indicator
  508. (--\s*)?
  509. # Match expressions of the form '${1+$@}', '${1:+"$@"',
  510. # '"${1+$@', "$@", etc where the quotes (before the dollar
  511. # sign(s)) are optional and the second (or only if the $1
  512. # clause is omitted) parameter may be $@ or $*.
  513. #
  514. # Finally the whole subexpression may be omitted for scripts
  515. # which do not pass on their parameters (i.e. after re-execing
  516. # they take their parameters (and potentially data) from stdin
  517. .?(\$\{1:?\+.?)?(\$(\@|\*))?~x
  518. ) {
  519. $ret = $. - 1;
  520. last;
  521. } elsif (/^\s*(\w+)=\$0;/) {
  522. $var = $1;
  523. } elsif (
  524. m~
  525. # Match scripts which use "foo $0 $@ &\nexec true\n"
  526. # Program name
  527. \S+\s+
  528. # As above
  529. .?\$$var.?\s*
  530. (--\s*)?
  531. .?(\$\{1:?\+.?)?(\$(\@|\*))?.?\s*\&~x
  532. ) {
  533. $backgrounded = 1;
  534. } elsif (
  535. $backgrounded
  536. and m~
  537. # the exec should either be "eval"ed or a new statement
  538. (^\s*|\beval\s*[\'\"]|(;|&&|\b(then|else))\s*)
  539. exec\s+true(\s|\Z)~x
  540. ) {
  541. $ret = $. - 1;
  542. last;
  543. } elsif (m~\@DPATCH\@~) {
  544. $ret = $. - 1;
  545. last;
  546. }
  547. }
  548. close IN;
  549. return $ret;
  550. }
  551. sub init_hashes {
  552. %bashisms = (
  553. qr'(?:^|\s+)function [^<>\(\)\[\]\{\};|\s]+(\s|\(|\Z)' =>
  554. q<'function' is useless>,
  555. $LEADIN . qr'select\s+\w+' => q<'select' is not POSIX>,
  556. qr'(test|-o|-a)\s*[^\s]+\s+==\s' => q<should be 'b = a'>,
  557. qr'\[\s+[^\]]+\s+==\s' => q<should be 'b = a'>,
  558. qr'\s\|\&' => q<pipelining is not POSIX>,
  559. qr'[^\\\$]\{([^\s\\\}]*?,)+[^\\\}\s]*\}' => q<brace expansion>,
  560. qr'\{\d+\.\.\d+(?:\.\.\d+)?\}' =>
  561. q<brace expansion, {a..b[..c]}should be $(seq a [c] b)>,
  562. qr'(?i)\{[a-z]\.\.[a-z](?:\.\.\d+)?\}' => q<brace expansion>,
  563. qr'(?:^|\s+)\w+\[\d+\]=' => q<bash arrays, H[0]>,
  564. $LEADIN
  565. . qr'read\s+(?:-[a-qs-zA-Z\d-]+)' =>
  566. q<read with option other than -r>,
  567. $LEADIN
  568. . qr'read\s*(?:-\w+\s*)*(?:\".*?\"|[\'].*?[\'])?\s*(?:;|$)' =>
  569. q<read without variable>,
  570. $LEADIN . qr'echo\s+(-n\s+)?-n?en?\s' => q<echo -e>,
  571. $LEADIN . qr'exec\s+-[acl]' => q<exec -c/-l/-a name>,
  572. $LEADIN . qr'let\s' => q<let ...>,
  573. qr'(?<![\$\(])\(\(.*\)\)' => q<'((' should be '$(('>,
  574. qr'(?:^|\s+)(\[|test)\s+-a' => q<test with unary -a (should be -e)>,
  575. qr'\&>' => q<should be \>word 2\>&1>,
  576. qr'(<\&|>\&)\s*((-|\d+)[^\s;|)}`&\\\\]|[^-\d\s]+(?<!\$)(?!\d))' =>
  577. q<should be \>word 2\>&1>,
  578. qr'\[\[(?!:)' =>
  579. q<alternative test command ([[ foo ]] should be [ foo ])>,
  580. qr'/dev/(tcp|udp)' => q</dev/(tcp|udp)>,
  581. $LEADIN . qr'builtin\s' => q<builtin>,
  582. $LEADIN . qr'caller\s' => q<caller>,
  583. $LEADIN . qr'compgen\s' => q<compgen>,
  584. $LEADIN . qr'complete\s' => q<complete>,
  585. $LEADIN . qr'declare\s' => q<declare>,
  586. $LEADIN . qr'dirs(\s|\Z)' => q<dirs>,
  587. $LEADIN . qr'disown\s' => q<disown>,
  588. $LEADIN . qr'enable\s' => q<enable>,
  589. $LEADIN . qr'mapfile\s' => q<mapfile>,
  590. $LEADIN . qr'readarray\s' => q<readarray>,
  591. $LEADIN . qr'shopt(\s|\Z)' => q<shopt>,
  592. $LEADIN . qr'suspend\s' => q<suspend>,
  593. $LEADIN . qr'time\s' => q<time>,
  594. $LEADIN . qr'type\s' => q<type>,
  595. $LEADIN . qr'typeset\s' => q<typeset>,
  596. $LEADIN . qr'ulimit(\s|\Z)' => q<ulimit>,
  597. $LEADIN . qr'set\s+-[BHT]+' => q<set -[BHT]>,
  598. $LEADIN . qr'alias\s+-p' => q<alias -p>,
  599. $LEADIN . qr'unalias\s+-a' => q<unalias -a>,
  600. $LEADIN . qr'local\s+-[a-zA-Z]+' => q<local -opt>,
  601. # function '=' is special-cased due to bash arrays (think of "foo=()")
  602. qr'(?:^|\s)\s*=\s*\(\s*\)\s*([\{|\(]|\Z)' =>
  603. q<function names should only contain [a-z0-9_]>,
  604. qr'(?:^|\s)(?<func>function\s)?\s*(?:[^<>\(\)\[\]\{\};|\s]*[^<>\(\)\[\]\{\};|\s\w][^<>\(\)\[\]\{\};|\s]*)(?(<func>)(?=)|(?<!=))\s*(?(<func>)(?:\(\s*\))?|\(\s*\))\s*([\{|\(]|\Z)'
  605. => q<function names should only contain [a-z0-9_]>,
  606. $LEADIN . qr'(push|pop)d(\s|\Z)' => q<(push|pop)d>,
  607. $LEADIN . qr'export\s+-[^p]' => q<export only takes -p as an option>,
  608. qr'(?:^|\s+)[<>]\(.*?\)' => q<\<() process substitution>,
  609. $LEADIN . qr'readonly\s+-[af]' => q<readonly -[af]>,
  610. $LEADIN . qr'(sh|\$\{?SHELL\}?) -[rD]' => q<sh -[rD]>,
  611. $LEADIN . qr'(sh|\$\{?SHELL\}?) --\w+' => q<sh --long-option>,
  612. $LEADIN . qr'(sh|\$\{?SHELL\}?) [-+]O' => q<sh [-+]O>,
  613. qr'\[\^[^]]+\]' => q<[^] should be [!]>,
  614. $LEADIN
  615. . qr'printf\s+-v' =>
  616. q<'printf -v var ...' should be var='$(printf ...)'>,
  617. $LEADIN . qr'coproc\s' => q<coproc>,
  618. qr';;?&' => q<;;& and ;& special case operators>,
  619. $LEADIN . qr'jobs\s' => q<jobs>,
  620. # $LEADIN . qr'jobs\s+-[^lp]\s' => q<'jobs' with option other than -l or -p>,
  621. $LEADIN
  622. . qr'command\s+-[^p]\s' => q<'command' with option other than -p>,
  623. $LEADIN
  624. . qr'setvar\s' =>
  625. q<setvar 'foo' 'bar' should be eval 'foo="'"$bar"'"'>,
  626. $LEADIN
  627. . qr'trap\s+["\']?.*["\']?\s+.*(?:ERR|DEBUG|RETURN)' =>
  628. q<trap with ERR|DEBUG|RETURN>,
  629. $LEADIN
  630. . qr'(?:exit|return)\s+-\d' =>
  631. q<exit|return with negative status code>,
  632. $LEADIN
  633. . qr'(?:exit|return)\s+--' =>
  634. q<'exit --' should be 'exit' (idem for return)>,
  635. $LEADIN
  636. . qr'sleep\s+(?:-|\d+(?:[.a-z]|\s+\d))' =>
  637. q<sleep only takes one integer>,
  638. $LEADIN . qr'hash(\s|\Z)' => q<hash>,
  639. qr'(?:[:=\s])~(?:[+-]|[+-]?\d+)(?:[/\s]|\Z)' =>
  640. q<non-standard tilde expansion>,
  641. );
  642. %string_bashisms = (
  643. qr'\$\[[^][]+\]' => q<'$[' should be '$(('>,
  644. qr'\$\{(?:\w+|@|\*)\:(?:\d+|\$\{?\w+\}?)+(?::(?:\d+|\$\{?\w+\}?)+)?\}'
  645. => q<${foo:3[:1]}>,
  646. qr'\$\{!\w+[\@*]\}' => q<${!prefix[*|@]>,
  647. qr'\$\{!\w+\}' => q<${!name}>,
  648. qr'\$\{(?:\w+|@|\*)([,^]{1,2}.*?)\}' =>
  649. q<${parm,[,][pat]} or ${parm^[^][pat]}>,
  650. qr'\$\{[@*]([#%]{1,2}.*?)\}' => q<${[@|*]#[#]pat} or ${[@|*]%[%]pat}>,
  651. qr'\$\{#[@*]\}' => q<${#@} or ${#*}>,
  652. qr'\$\{(?:\w+|@|\*)(/.+?){1,2}\}' => q<${parm/?/pat[/str]}>,
  653. qr'\$\{\#?\w+\[.+\](?:[/,:#%^].+?)?\}' =>
  654. q<bash arrays, ${name[0|*|@]}>,
  655. qr'\$\{?RANDOM\}?\b' => q<$RANDOM>,
  656. qr'\$\{?(OS|MACH)TYPE\}?\b' => q<$(OS|MACH)TYPE>,
  657. qr'\$\{?HOST(TYPE|NAME)\}?\b' => q<$HOST(TYPE|NAME)>,
  658. qr'\$\{?DIRSTACK\}?\b' => q<$DIRSTACK>,
  659. qr'\$\{?EUID\}?\b' => q<$EUID should be "$(id -u)">,
  660. qr'\$\{?UID\}?\b' => q<$UID should be "$(id -ru)">,
  661. qr'\$\{?SECONDS\}?\b' => q<$SECONDS>,
  662. qr'\$\{?BASH_[A-Z]+\}?\b' => q<$BASH_SOMETHING>,
  663. qr'\$\{?SHELLOPTS\}?\b' => q<$SHELLOPTS>,
  664. qr'\$\{?PIPESTATUS\}?\b' => q<$PIPESTATUS>,
  665. qr'\$\{?SHLVL\}?\b' => q<$SHLVL>,
  666. qr'\$\{?FUNCNAME\}?\b' => q<$FUNCNAME>,
  667. qr'\$\{?TMOUT\}?\b' => q<$TMOUT>,
  668. qr'(?:^|\s+)TMOUT=' => q<TMOUT=>,
  669. qr'\$\{?TIMEFORMAT\}?\b' => q<$TIMEFORMAT>,
  670. qr'(?:^|\s+)TIMEFORMAT=' => q<TIMEFORMAT=>,
  671. qr'(?<![$\\])\$\{?_\}?\b' => q<$_>,
  672. qr'(?:^|\s+)GLOBIGNORE=' => q<GLOBIGNORE=>,
  673. qr'<<<' => q<\<\<\< here string>,
  674. $LEADIN
  675. . qr'echo\s+(?:-[^e\s]+\s+)?\"[^\"]*(\\[abcEfnrtv0])+.*?[\"]' =>
  676. q<unsafe echo with backslash>,
  677. qr'\$\(\([\s\w$*/+-]*\w\+\+.*?\)\)' =>
  678. q<'$((n++))' should be '$n; $((n=n+1))'>,
  679. qr'\$\(\([\s\w$*/+-]*\+\+\w.*?\)\)' =>
  680. q<'$((++n))' should be '$((n=n+1))'>,
  681. qr'\$\(\([\s\w$*/+-]*\w\-\-.*?\)\)' =>
  682. q<'$((n--))' should be '$n; $((n=n-1))'>,
  683. qr'\$\(\([\s\w$*/+-]*\-\-\w.*?\)\)' =>
  684. q<'$((--n))' should be '$((n=n-1))'>,
  685. qr'\$\(\([\s\w$*/+-]*\*\*.*?\)\)' => q<exponentiation is not POSIX>,
  686. $LEADIN . qr'printf\s["\'][^"\']*?%q.+?["\']' => q<printf %q>,
  687. );
  688. %singlequote_bashisms = (
  689. $LEADIN
  690. . qr'echo\s+(?:-[^e\s]+\s+)?\'[^\']*(\\[abcEfnrtv0])+.*?[\']' =>
  691. q<unsafe echo with backslash>,
  692. $LEADIN
  693. . qr'source\s+[\"\']?(?:\.\/|\/|\$|[\w~.-])\S*' =>
  694. q<should be '.', not 'source'>,
  695. );
  696. if ($opt_echo) {
  697. $bashisms{ $LEADIN . qr'echo\s+-[A-Za-z]*n' } = q<echo -n>;
  698. }
  699. if ($opt_posix) {
  700. $bashisms{ $LEADIN . qr'local\s+\w+(\s+\W|\s*[;&|)]|$)' }
  701. = q<local foo>;
  702. $bashisms{ $LEADIN . qr'local\s+\w+=' } = q<local foo=bar>;
  703. $bashisms{ $LEADIN . qr'local\s+\w+\s+\w+' } = q<local x y>;
  704. $bashisms{ $LEADIN . qr'((?:test|\[)\s+.+\s-[ao])\s' } = q<test -a/-o>;
  705. $bashisms{ $LEADIN . qr'kill\s+-[^sl]\w*' } = q<kill -[0-9] or -[A-Z]>;
  706. $bashisms{ $LEADIN . qr'trap\s+["\']?.*["\']?\s+.*[1-9]' }
  707. = q<trap with signal numbers>;
  708. }
  709. if ($makefile) {
  710. $string_bashisms{qr'(\$\(|\`)\s*\<\s*([^\s\)]{2,}|[^DF])\s*(\)|\`)'}
  711. = q<'$(\< foo)' should be '$(cat foo)'>;
  712. } else {
  713. $bashisms{ $LEADIN . qr'\w+\+=' } = q<should be VAR="${VAR}foo">;
  714. $string_bashisms{qr'(\$\(|\`)\s*\<\s*\S+\s*(\)|\`)'}
  715. = q<'$(\< foo)' should be '$(cat foo)'>;
  716. }
  717. if ($opt_extra) {
  718. $string_bashisms{qr'\$\{?BASH\}?\b'} = q<$BASH>;
  719. $string_bashisms{qr'(?:^|\s+)RANDOM='} = q<RANDOM=>;
  720. $string_bashisms{qr'(?:^|\s+)(OS|MACH)TYPE='} = q<(OS|MACH)TYPE=>;
  721. $string_bashisms{qr'(?:^|\s+)HOST(TYPE|NAME)='} = q<HOST(TYPE|NAME)=>;
  722. $string_bashisms{qr'(?:^|\s+)DIRSTACK='} = q<DIRSTACK=>;
  723. $string_bashisms{qr'(?:^|\s+)EUID='} = q<EUID=>;
  724. $string_bashisms{qr'(?:^|\s+)UID='} = q<UID=>;
  725. $string_bashisms{qr'(?:^|\s+)BASH(_[A-Z]+)?='} = q<BASH(_SOMETHING)=>;
  726. $string_bashisms{qr'(?:^|\s+)SHELLOPTS='} = q<SHELLOPTS=>;
  727. $string_bashisms{qr'\$\{?POSIXLY_CORRECT\}?\b'} = q<$POSIXLY_CORRECT>;
  728. }
  729. }