1 #------------------------------------------------------------------------------
2 # Detect broken &&-chains in tests.
4 # At present, only &&-chains in subshells are examined by this linter;
5 # top-level &&-chains are instead checked directly by the test framework. Like
6 # the top-level &&-chain linter, the subshell linter (intentionally) does not
7 # check &&-chains within {...} blocks.
9 # Checking for &&-chain breakage is done line-by-line by pure textual
12 # Incomplete lines (those ending with "\") are stitched together with following
13 # lines to simplify processing, particularly of "one-liner" statements.
14 # Top-level here-docs are swallowed to avoid false positives within the
15 # here-doc body, although the statement to which the here-doc is attached is
18 # Heuristics are used to detect end-of-subshell when the closing ")" is cuddled
19 # with the final subshell statement on the same line:
24 # in order to avoid misinterpreting the ")" in constructs such as "x=$(...)"
25 # and "case $x in *)" as ending the subshell.
27 # Lines missing a final "&&" are flagged with "?!AMP?!", as are lines which
28 # chain commands with ";" internally rather than "&&". A line may be flagged
29 # for both violations.
31 # Detection of a missing &&-link in a multi-line subshell is complicated by the
32 # fact that the last statement before the closing ")" must not end with "&&".
33 # Since processing is line-by-line, it is not known whether a missing "&&" is
34 # legitimate or not until the _next_ line is seen. To accommodate this, within
35 # multi-line subshells, each line is stored in sed's "hold" area until after
36 # the next line is seen and processed. If the next line is a stand-alone ")",
37 # then a missing "&&" on the previous line is legitimate; otherwise a missing
38 # "&&" is a break in the &&-chain.
45 # In practical terms, when "bar" is encountered, it is flagged with "?!AMP?!",
46 # but when the stand-alone ")" line is seen which closes the subshell, the
47 # "?!AMP?!" violation is removed from the "bar" line (retrieved from the "hold"
48 # area) since the final statement of a subshell must not end with "&&". The
49 # final line of a subshell may still break the &&-chain by using ";" internally
50 # to chain commands together rather than "&&", but an internal "?!AMP?!" is
51 # never removed from a line even though a line-ending "?!AMP?!" might be.
53 # Care is taken to recognize the last _statement_ of a multi-line subshell, not
54 # necessarily the last textual _line_ within the subshell, since &&-chaining
55 # applies to statements, not to lines. Consequently, blank lines, comment
56 # lines, and here-docs are swallowed (but not the command to which the here-doc
57 # is attached), leaving the last statement in the "hold" area, not the last
58 # line, thus simplifying &&-link checking.
60 # The final statement before "done" in for- and while-loops, and before "elif",
61 # "else", and "fi" in if-then-else likewise must not end with "&&", thus
62 # receives similar treatment.
64 # Swallowing here-docs with arbitrary tags requires a bit of finesse. When a
65 # line such as "cat <<EOF" is seen, the here-doc tag is copied to the front of
66 # the line enclosed in angle brackets as a sentinel, giving "<EOF>cat <<EOF".
67 # As each subsequent line is read, it is appended to the target line and a
68 # (whitespace-loose) back-reference match /^<(.*)>\n\1$/ is attempted to see if
69 # the content inside "<...>" matches the entirety of the newly-read line. For
70 # instance, if the next line read is "some data", when concatenated with the
71 # target line, it becomes "<EOF>cat <<EOF\nsome data", and a match is attempted
72 # to see if "EOF" matches "some data". Since it doesn't, the next line is
73 # attempted. When a line consisting of only "EOF" (and possible whitespace) is
74 # encountered, it is appended to the target line giving "<EOF>cat <<EOF\nEOF",
75 # in which case the "EOF" inside "<...>" does match the text following the
76 # newline, thus the closing here-doc tag has been found. The closing tag line
77 # and the "<...>" prefix on the target line are then discarded, leaving just
78 # the target line "cat <<EOF".
79 #------------------------------------------------------------------------------
81 # incomplete line -- slurp up next line
89 # here-doc -- swallow it to avoid false hits within its body (but keep the
90 # command to which it was attached)
91 /<<-*[ ]*[\\'"]*[A-Za-z0-9_]/ {
92 /"[^"]*<<[^"]*"/bnotdoc
93 s/^\(.*<<-*[ ]*\)[\\'"]*\([A-Za-z0-9_][A-Za-z0-9_]*\)['"]*/<\2>\1\2/
96 /^<\([^>]*\)>.*\n[ ]*\1[ ]*$/!{
105 # one-liner "(...) &&"
106 /^[ ]*!*[ ]*(..*)[ ]*&&[ ]*$/boneline
108 # same as above but without trailing "&&"
109 /^[ ]*!*[ ]*(..*)[ ]*$/boneline
111 # one-liner "(...) >x" (or "2>x" or "<x" or "|x" or "&"
112 /^[ ]*!*[ ]*(..*)[ ]*[0-9]*[<>|&]/boneline
114 # multi-line "(...\n...)"
117 # innocuous line -- print it and advance to next line
120 # found one-liner "(...)" -- mark suspect if it uses ";" internally rather than
121 # "&&" (but not ";" in a string)
124 /"[^"]*;[^"]*"/!s/;/; ?!AMP?!/
129 # bare "(" line? -- stash for later printing
134 # "(..." line -- split off and stash "(", then process "..." as its own line
146 # incomplete line "...\"
148 # multi-line quoted string "...\n..."?
150 # multi-line quoted string '...\n...'? (but not contraction in string "it's")
152 /"[^'"]*'[^'"]*"/!bsqstr
155 # here-doc -- swallow it (but not "<<" in a string)
156 /<<-*[ ]*[\\'"]*[A-Za-z0-9_]/{
157 /"[^"]*<<[^"]*"/!bheredoc
159 # comment or empty line -- discard since final non-comment, non-empty line
160 # before closing ")", "done", "elsif", "else", or "fi" will need to be
161 # re-visited to drop "suspect" marking since final line of those constructs
162 # legitimately lacks "&&", so "suspect" mark must be removed
165 # in-line comment -- strip it (but not "#" in a string, Bash ${#...} array
166 # length, or Perforce "//depot/path#42" revision in filespec)
168 /"[^"]*#[^"]*"/!s/[ ]#.*$//
170 # one-liner "case ... esac"
171 /^[ ]*case[ ]*..*esac/bchkchn
172 # multi-line "case ... esac"
173 /^[ ]*case[ ]..*[ ]in/bcase
174 # multi-line "for ... done" or "while ... done"
175 /^[ ]*for[ ]..*[ ]in/bcont
180 /^[ ]*done[ ]*&&[ ]*$/bdone
181 /^[ ]*done[ ]*$/bdone
182 /^[ ]*done[ ]*[<>|]/bdone
183 /^[ ]*done[ ]*)/bdone
185 /||[ ]*exit[ ]*$/bcont
186 # multi-line "if...elsif...else...fi"
189 /^[ ]*then[ ]*$/bcont
192 /^[ ]*elif[ ]*$/belse
194 /^[ ]*else[ ]*$/belse
195 /^[ ]*fi[ ]*&&[ ]*$/bdone
197 /^[ ]*fi[ ]*[<>|]/bdone
199 # nested one-liner "(...) &&"
200 /^[ ]*(.*)[ ]*&&[ ]*$/bchkchn
201 # nested one-liner "(...)"
202 /^[ ]*(.*)[ ]*$/bchkchn
203 # nested one-liner "(...) >x" (or "2>x" or "<x" or "|x")
204 /^[ ]*(.*)[ ]*[0-9]*[<>|]/bchkchn
205 # nested multi-line "(...\n...)"
207 # multi-line "{...\n...}"
209 # closing ")" on own line -- exit subshell
211 # "$((...))" -- arithmetic expansion; not closing ")"
212 /\$(([^)][^)]*))[^)]*$/bchkchn
213 # "$(...)" -- command substitution; not closing ")"
214 /\$([^)][^)]*)[^)]*$/bchkchn
215 # multi-line "$(...\n...)" -- command substitution; treat as nested subshell
217 # "=(...)" -- Bash array assignment; not closing ")"
223 # closing "...) >x" (or "2>x" or "<x" or "|x")
226 # mark suspect if line uses ";" internally rather than "&&" (but not ";" in a
227 # string and not ";;" in one-liner "case...esac")
230 /"[^"]*;[^"]*"/!s/;/; ?!AMP?!/
233 # line ends with pipe "...|" -- valid; not missing "&&"
235 # missing end-of-line "&&" -- mark suspect
236 /&&[ ]*$/!s/$/ ?!AMP?!/
238 # retrieve and print previous line
244 # found incomplete line "...\" -- slurp up next line
250 # check for multi-line double-quoted string "...\n..." -- fold to one line
252 # remove all quote pairs
253 s/"\([^"]*\)"/@!\1@!/g
254 # done if no dangling quote
256 # otherwise, slurp next line and try again
264 # check for multi-line single-quoted string '...\n...' -- fold to one line
266 # remove all quote pairs
267 s/'\([^']*\)'/@!\1@!/g
268 # done if no dangling quote
270 # otherwise, slurp next line and try again
278 # found here-doc -- swallow it to avoid false hits within its body (but keep
279 # the command to which it was attached)
281 s/^\(.*\)<<\(-*[ ]*\)[\\'"]*\([A-Za-z0-9_][A-Za-z0-9_]*\)['"]*/<\3>\1?!HERE?!\2\3/
284 /^<\([^>]*\)>.*\n[ ]*\1[ ]*$/!{
292 # found "case ... in" -- pass through untouched
306 # found "else" or "elif" -- drop "suspect" from final line before "else" since
307 # that line legitimately lacks "&&"
310 s/\( ?!AMP?!\)* ?!AMP?!$//
314 # found "done" closing for-loop or while-loop, or "fi" closing if-then -- drop
315 # "suspect" from final contained line since that line legitimately lacks "&&"
318 s/\( ?!AMP?!\)* ?!AMP?!$//
320 # is 'done' or 'fi' cuddled with ")" to close subshell?
325 # found nested multi-line "(...\n...)" -- pass through untouched
332 # comment -- not closing ")" if in comment
338 # closing ")" on own line -- stop nested slurp
340 # "$((...))" -- arithmetic expansion; not closing ")"
341 /\$(([^)][^)]*))[^)]*$/bnstcnt
342 # "$(...)" -- command substitution; not closing ")"
343 /\$([^)][^)]*)[^)]*$/bnstcnt
344 # closing "...)" -- stop nested slurp
350 # is it "))" which closes nested and parent subshells?
354 # found multi-line "{...\n...}" block -- pass through untouched
365 # closing "}" -- stop block slurp
369 # found closing ")" on own line -- drop "suspect" from final line of subshell
370 # since that line legitimately lacks "&&" and exit subshell loop
373 s/\( ?!AMP?!\)* ?!AMP?!$//
380 # found closing "...)" -- exit subshell loop