summaryrefslogtreecommitdiff
path: root/t/chainlint.sed
diff options
context:
space:
mode:
Diffstat (limited to 't/chainlint.sed')
-rw-r--r--t/chainlint.sed124
1 files changed, 77 insertions, 47 deletions
diff --git a/t/chainlint.sed b/t/chainlint.sed
index 8a25c5b855..dc4ce37cb5 100644
--- a/t/chainlint.sed
+++ b/t/chainlint.sed
@@ -24,9 +24,9 @@
# in order to avoid misinterpreting the ")" in constructs such as "x=$(...)"
# and "case $x in *)" as ending the subshell.
#
-# Lines missing a final "&&" are flagged with "?!AMP?!", and lines which chain
-# commands with ";" internally rather than "&&" are flagged "?!SEMI?!". A line
-# may be flagged for both violations.
+# Lines missing a final "&&" are flagged with "?!AMP?!", as are lines which
+# chain commands with ";" internally rather than "&&". A line may be flagged
+# for both violations.
#
# Detection of a missing &&-link in a multi-line subshell is complicated by the
# fact that the last statement before the closing ")" must not end with "&&".
@@ -47,8 +47,8 @@
# "?!AMP?!" violation is removed from the "bar" line (retrieved from the "hold"
# area) since the final statement of a subshell must not end with "&&". The
# final line of a subshell may still break the &&-chain by using ";" internally
-# to chain commands together rather than "&&", so "?!SEMI?!" is never removed
-# from a line (even though "?!AMP?!" might be).
+# to chain commands together rather than "&&", but an internal "?!AMP?!" is
+# never removed from a line even though a line-ending "?!AMP?!" might be.
#
# Care is taken to recognize the last _statement_ of a multi-line subshell, not
# necessarily the last textual _line_ within the subshell, since &&-chaining
@@ -62,26 +62,20 @@
# receives similar treatment.
#
# Swallowing here-docs with arbitrary tags requires a bit of finesse. When a
-# line such as "cat <<EOF >out" is seen, the here-doc tag is moved to the front
-# of the line enclosed in angle brackets as a sentinel, giving "<EOF>cat >out".
+# line such as "cat <<EOF" is seen, the here-doc tag is copied to the front of
+# the line enclosed in angle brackets as a sentinel, giving "<EOF>cat <<EOF".
# As each subsequent line is read, it is appended to the target line and a
# (whitespace-loose) back-reference match /^<(.*)>\n\1$/ is attempted to see if
# the content inside "<...>" matches the entirety of the newly-read line. For
# instance, if the next line read is "some data", when concatenated with the
-# target line, it becomes "<EOF>cat >out\nsome data", and a match is attempted
+# target line, it becomes "<EOF>cat <<EOF\nsome data", and a match is attempted
# to see if "EOF" matches "some data". Since it doesn't, the next line is
# attempted. When a line consisting of only "EOF" (and possible whitespace) is
-# encountered, it is appended to the target line giving "<EOF>cat >out\nEOF",
+# encountered, it is appended to the target line giving "<EOF>cat <<EOF\nEOF",
# in which case the "EOF" inside "<...>" does match the text following the
# newline, thus the closing here-doc tag has been found. The closing tag line
# and the "<...>" prefix on the target line are then discarded, leaving just
-# the target line "cat >out".
-#
-# To facilitate regression testing (and manual debugging), a ">" annotation is
-# applied to the line containing ")" which closes a subshell, ">>" to a line
-# closing a nested subshell, and ">>>" to a line closing both at once. This
-# makes it easy to detect whether the heuristics correctly identify
-# end-of-subshell.
+# the target line "cat <<EOF".
#------------------------------------------------------------------------------
# incomplete line -- slurp up next line
@@ -94,9 +88,9 @@
# here-doc -- swallow it to avoid false hits within its body (but keep the
# command to which it was attached)
-/<<[ ]*[-\\'"]*[A-Za-z0-9_]/ {
- s/^\(.*\)<<[ ]*[-\\'"]*\([A-Za-z0-9_][A-Za-z0-9_]*\)['"]*/<\2>\1<</
- s/[ ]*<<//
+/<<-*[ ]*[\\'"]*[A-Za-z0-9_]/ {
+ /"[^"]*<<[^"]*"/bnotdoc
+ s/^\(.*<<-*[ ]*\)[\\'"]*\([A-Za-z0-9_][A-Za-z0-9_]*\)['"]*/<\2>\1\2/
:hered
N
/^<\([^>]*\)>.*\n[ ]*\1[ ]*$/!{
@@ -106,6 +100,7 @@
s/^<[^>]*>//
s/\n.*$//
}
+:notdoc
# one-liner "(...) &&"
/^[ ]*!*[ ]*(..*)[ ]*&&[ ]*$/boneline
@@ -126,7 +121,7 @@ b
# "&&" (but not ";" in a string)
:oneline
/;/{
- /"[^"]*;[^"]*"/!s/^/?!SEMI?!/
+ /"[^"]*;[^"]*"/!s/;/; ?!AMP?!/
}
b
@@ -136,11 +131,15 @@ b
h
bnextln
}
-# "(..." line -- split off and stash "(", then process "..." as its own line
+# "(..." line -- "(" opening subshell cuddled with command; temporarily replace
+# "(" with sentinel "^" and process the line as if "(" had been seen solo on
+# the preceding line; this temporary replacement prevents several rules from
+# accidentally thinking "(" introduces a nested subshell; "^" is changed back
+# to "(" at output time
x
-s/.*/(/
+s/.*//
x
-s/(//
+s/(/^/
bslurp
:nextln
@@ -157,8 +156,10 @@ s/.*\n//
/"[^'"]*'[^'"]*"/!bsqstr
}
:folded
-# here-doc -- swallow it
-/<<[ ]*[-\\'"]*[A-Za-z0-9_]/bheredoc
+# here-doc -- swallow it (but not "<<" in a string)
+/<<-*[ ]*[\\'"]*[A-Za-z0-9_]/{
+ /"[^"]*<<[^"]*"/!bheredoc
+}
# comment or empty line -- discard since final non-comment, non-empty line
# before closing ")", "done", "elsif", "else", or "fi" will need to be
# re-visited to drop "suspect" marking since final line of those constructs
@@ -171,12 +172,12 @@ s/.*\n//
/"[^"]*#[^"]*"/!s/[ ]#.*$//
}
# one-liner "case ... esac"
-/^[ ]*case[ ]*..*esac/bchkchn
+/^[ ^]*case[ ]*..*esac/bchkchn
# multi-line "case ... esac"
-/^[ ]*case[ ]..*[ ]in/bcase
+/^[ ^]*case[ ]..*[ ]in/bcase
# multi-line "for ... done" or "while ... done"
-/^[ ]*for[ ]..*[ ]in/bcont
-/^[ ]*while[ ]/bcont
+/^[ ^]*for[ ]..*[ ]in/bcont
+/^[ ^]*while[ ]/bcont
/^[ ]*do[ ]/bcont
/^[ ]*do[ ]*$/bcont
/;[ ]*do/bcont
@@ -187,7 +188,7 @@ s/.*\n//
/||[ ]*exit[ ]/bcont
/||[ ]*exit[ ]*$/bcont
# multi-line "if...elsif...else...fi"
-/^[ ]*if[ ]/bcont
+/^[ ^]*if[ ]/bcont
/^[ ]*then[ ]/bcont
/^[ ]*then[ ]*$/bcont
/;[ ]*then/bcont
@@ -200,15 +201,15 @@ s/.*\n//
/^[ ]*fi[ ]*[<>|]/bdone
/^[ ]*fi[ ]*)/bdone
# nested one-liner "(...) &&"
-/^[ ]*(.*)[ ]*&&[ ]*$/bchkchn
+/^[ ^]*(.*)[ ]*&&[ ]*$/bchkchn
# nested one-liner "(...)"
-/^[ ]*(.*)[ ]*$/bchkchn
+/^[ ^]*(.*)[ ]*$/bchkchn
# nested one-liner "(...) >x" (or "2>x" or "<x" or "|x")
-/^[ ]*(.*)[ ]*[0-9]*[<>|]/bchkchn
+/^[ ^]*(.*)[ ]*[0-9]*[<>|]/bchkchn
# nested multi-line "(...\n...)"
-/^[ ]*(/bnest
+/^[ ^]*(/bnest
# multi-line "{...\n...}"
-/^[ ]*{/bblock
+/^[ ^]*{/bblock
# closing ")" on own line -- exit subshell
/^[ ]*)/bclssolo
# "$((...))" -- arithmetic expansion; not closing ")"
@@ -230,16 +231,18 @@ s/.*\n//
# string and not ";;" in one-liner "case...esac")
/;/{
/;;/!{
- /"[^"]*;[^"]*"/!s/^/?!SEMI?!/
+ /"[^"]*;[^"]*"/!s/;/; ?!AMP?!/
}
}
# line ends with pipe "...|" -- valid; not missing "&&"
/|[ ]*$/bcont
# missing end-of-line "&&" -- mark suspect
-/&&[ ]*$/!s/^/?!AMP?!/
+/&&[ ]*$/!s/$/ ?!AMP?!/
:cont
# retrieve and print previous line
x
+s/^\([ ]*\)^/\1(/
+s/?!HERE?!/<</g
n
bslurp
@@ -280,8 +283,7 @@ bfolded
# found here-doc -- swallow it to avoid false hits within its body (but keep
# the command to which it was attached)
:heredoc
-s/^\(.*\)<<[ ]*[-\\'"]*\([A-Za-z0-9_][A-Za-z0-9_]*\)['"]*/<\2>\1<</
-s/[ ]*<<//
+s/^\(.*\)<<\(-*[ ]*\)[\\'"]*\([A-Za-z0-9_][A-Za-z0-9_]*\)['"]*/<\3>\1?!HERE?!\2\3/
:hdocsub
N
/^<\([^>]*\)>.*\n[ ]*\1[ ]*$/!{
@@ -295,7 +297,15 @@ bfolded
# found "case ... in" -- pass through untouched
:case
x
+s/^\([ ]*\)^/\1(/
+s/?!HERE?!/<</g
n
+:cascom
+/^[ ]*#/{
+ N
+ s/.*\n//
+ bcascom
+}
/^[ ]*esac/bslurp
bcase
@@ -303,7 +313,7 @@ bcase
# that line legitimately lacks "&&"
:else
x
-s/?!AMP?!//
+s/\( ?!AMP?!\)* ?!AMP?!$//
x
bcont
@@ -311,7 +321,7 @@ bcont
# "suspect" from final contained line since that line legitimately lacks "&&"
:done
x
-s/?!AMP?!//
+s/\( ?!AMP?!\)* ?!AMP?!$//
x
# is 'done' or 'fi' cuddled with ")" to close subshell?
/done.*)/bclose
@@ -322,11 +332,18 @@ bchkchn
:nest
x
:nstslrp
+s/^\([ ]*\)^/\1(/
+s/?!HERE?!/<</g
n
+:nstcom
+# comment -- not closing ")" if in comment
+/^[ ]*#/{
+ N
+ s/.*\n//
+ bnstcom
+}
# closing ")" on own line -- stop nested slurp
/^[ ]*)/bnstcl
-# comment -- not closing ")" if in comment
-/^[ ]*#/bnstcnt
# "$((...))" -- arithmetic expansion; not closing ")"
/\$(([^)][^)]*))[^)]*$/bnstcnt
# "$(...)" -- command substitution; not closing ")"
@@ -337,7 +354,6 @@ n
x
bnstslrp
:nstcl
-s/^/>>/
# is it "))" which closes nested and parent subshells?
/)[ ]*)/bslurp
bchkchn
@@ -345,7 +361,15 @@ bchkchn
# found multi-line "{...\n...}" block -- pass through untouched
:block
x
+s/^\([ ]*\)^/\1(/
+s/?!HERE?!/<</g
n
+:blkcom
+/^[ ]*#/{
+ N
+ s/.*\n//
+ bblkcom
+}
# closing "}" -- stop block slurp
/}/bchkchn
bblock
@@ -354,16 +378,22 @@ bblock
# since that line legitimately lacks "&&" and exit subshell loop
:clssolo
x
-s/?!AMP?!//
+s/\( ?!AMP?!\)* ?!AMP?!$//
+s/^\([ ]*\)^/\1(/
+s/?!HERE?!/<</g
p
x
-s/^/>/
+s/^\([ ]*\)^/\1(/
+s/?!HERE?!/<</g
b
# found closing "...)" -- exit subshell loop
:close
x
+s/^\([ ]*\)^/\1(/
+s/?!HERE?!/<</g
p
x
-s/^/>/
+s/^\([ ]*\)^/\1(/
+s/?!HERE?!/<</g
b