@@ -4,33 +4,66 @@ export type ShellToken = string | { op: string } | { command: string }
44
55/**
66 * Split a command string into individual sub-commands by
7- * chaining operators (&&, ||, ;, |, or &) and newlines.
7+ * chaining operators (&&, ||, ;, |, or &) and unquoted newlines.
88 *
99 * Uses shell-quote to properly handle:
10- * - Quoted strings (preserves quotes)
10+ * - Quoted strings (preserves quotes, including multi-line quoted strings )
1111 * - Subshell commands ($(cmd), `cmd`, <(cmd), >(cmd))
1212 * - PowerShell redirections (2>&1)
1313 * - Chain operators (&&, ||, ;, |, &)
14- * - Newlines as command separators
14+ * - Newlines as command separators (only when outside quoted strings)
15+ *
16+ * Key invariant: newlines that appear inside a quoted string (single or double)
17+ * are part of that string argument and must NOT be treated as command separators.
18+ * Only unquoted newlines split commands. For example:
19+ *
20+ * sh -c 'python3 -c "
21+ * import sys
22+ * print(sys.version)
23+ * "'
24+ *
25+ * ...is a single command, not multiple commands split at each newline.
1526 */
1627export function parseCommand ( command : string ) : string [ ] {
1728 if ( ! command ?. trim ( ) ) {
1829 return [ ]
1930 }
2031
21- // Split by newlines first (handle different line ending formats)
22- // This regex splits on \r\n (Windows), \n (Unix), or \r (old Mac)
23- const lines = command . split ( / \r \n | \r | \n / )
32+ // Mask quoted strings (both single and double) before splitting on newlines.
33+ // This ensures that newlines embedded inside a quoted argument are not
34+ // mistaken for command separators. Both quote styles are handled here because
35+ // either can span multiple lines (e.g. a heredoc-style script passed as a
36+ // single argument to sh -c '...' or a double-quoted multi-line string).
37+ //
38+ // A single left-to-right scan with an alternation is used (rather than two
39+ // separate passes) so that whichever quote opens first wins. This prevents a
40+ // quote of one style that appears inside a string of the other style from
41+ // starting a spurious match -- e.g. the apostrophe in "don't" must not begin
42+ // a single-quoted region, and a double quote inside 'a "b' must not begin a
43+ // double-quoted region. Negated character classes match newlines, so
44+ // multi-line quoted strings are captured whole.
45+ const topLevelQuotes : string [ ] = [ ]
46+
47+ const masked = command . replace ( / ' [ ^ ' ] * ' | " (?: [ ^ " \\ ] | \\ .) * " / g, ( match ) => {
48+ topLevelQuotes . push ( match )
49+ return `__TOPLEVEL_QUOTE_${ topLevelQuotes . length - 1 } __`
50+ } )
51+
52+ // Split on unquoted newlines (all line-ending formats).
53+ const lines = masked . split ( / \r \n | \r | \n / )
2454 const allCommands : string [ ] = [ ]
2555
2656 for ( const line of lines ) {
27- // Skip empty lines
2857 if ( ! line . trim ( ) ) {
2958 continue
3059 }
3160
32- // Process each line through the existing parsing logic
33- const lineCommands = parseCommandLine ( line )
61+ // Restore top-level quote placeholders before per-line parsing so that
62+ // parseCommandLine sees the original quoted content and can apply its own
63+ // masking for operator splitting.
64+ const restoredLine = line . replace ( / _ _ T O P L E V E L _ Q U O T E _ ( \d + ) _ _ / g, ( _ , i ) => topLevelQuotes [ parseInt ( i ) ] )
65+
66+ const lineCommands = parseCommandLine ( restoredLine )
3467 allCommands . push ( ...lineCommands )
3568 }
3669
@@ -47,6 +80,7 @@ function parseCommandLine(command: string): string[] {
4780 const redirections : string [ ] = [ ]
4881 const subshells : string [ ] = [ ]
4982 const quotes : string [ ] = [ ]
83+ const singleQuotes : string [ ] = [ ]
5084 const arrayIndexing : string [ ] = [ ]
5185 const arithmeticExpressions : string [ ] = [ ]
5286 const variables : string [ ] = [ ]
@@ -84,6 +118,17 @@ function parseCommandLine(command: string): string[] {
84118 return `__SUBSH_${ subshells . length - 1 } __`
85119 } )
86120
121+ // Handle ANSI-C quoting: $'...'. This must run before variable masking so the
122+ // leading $ is captured as part of the quoted unit rather than being treated
123+ // as a variable expansion (which would corrupt a following placeholder).
124+ // ANSI-C strings interpret backslash escapes, so the pattern is escape-aware.
125+ // The whole token (including the $ and quotes) is preserved in the single-
126+ // quote bucket so it is restored verbatim.
127+ processedCommand = processedCommand . replace ( / \$ ' (?: [ ^ ' \\ ] | \\ .) * ' / g, ( match ) => {
128+ singleQuotes . push ( match )
129+ return `__SQUOTE_${ singleQuotes . length - 1 } __`
130+ } )
131+
87132 // Handle simple variable references: $varname pattern
88133 // This prevents shell-quote from splitting $count into separate tokens
89134 processedCommand = processedCommand . replace ( / \$ [ a - z A - Z _ ] [ a - z A - Z 0 - 9 _ ] * / g, ( match ) => {
@@ -108,8 +153,22 @@ function parseCommandLine(command: string): string[] {
108153 return `__SUBSH_${ subshells . length - 1 } __`
109154 } )
110155
111- // Then handle quoted strings
112- processedCommand = processedCommand . replace ( / " [ ^ " ] * " / g, ( match ) => {
156+ // Mask quoted strings (single and double) so their contents -- including
157+ // operators like &&, |, ; and any embedded newlines -- are not treated as
158+ // command separators. A single left-to-right scan with an alternation is used
159+ // so that whichever quote opens first wins, preventing a quote of one style
160+ // inside a string of the other style from starting a spurious match.
161+ //
162+ // Single quotes are matched literally (POSIX single quotes are fully opaque,
163+ // no escaping inside them). Double quotes use an escape-aware pattern so that
164+ // an escaped quote (\") does not prematurely terminate the match. Negated
165+ // character classes match newlines, so multi-line quoted strings are captured
166+ // as a single token.
167+ processedCommand = processedCommand . replace ( / ' [ ^ ' ] * ' | " (?: [ ^ " \\ ] | \\ .) * " / g, ( match ) => {
168+ if ( match . startsWith ( "'" ) ) {
169+ singleQuotes . push ( match )
170+ return `__SQUOTE_${ singleQuotes . length - 1 } __`
171+ }
113172 quotes . push ( match )
114173 return `__QUOTE_${ quotes . length - 1 } __`
115174 } )
@@ -132,6 +191,7 @@ function parseCommandLine(command: string): string[] {
132191 restorePlaceholders (
133192 cmd ,
134193 quotes ,
194+ singleQuotes ,
135195 redirections ,
136196 arrayIndexing ,
137197 arithmeticExpressions ,
@@ -177,11 +237,12 @@ function parseCommandLine(command: string): string[] {
177237 commands . push ( currentCommand . join ( " " ) )
178238 }
179239
180- // Restore quotes and redirections
240+ // Restore all placeholders
181241 return commands . map ( ( cmd ) =>
182242 restorePlaceholders (
183243 cmd ,
184244 quotes ,
245+ singleQuotes ,
185246 redirections ,
186247 arrayIndexing ,
187248 arithmeticExpressions ,
@@ -198,6 +259,7 @@ function parseCommandLine(command: string): string[] {
198259function restorePlaceholders (
199260 command : string ,
200261 quotes : string [ ] ,
262+ singleQuotes : string [ ] ,
201263 redirections : string [ ] ,
202264 arrayIndexing : string [ ] ,
203265 arithmeticExpressions : string [ ] ,
@@ -206,8 +268,10 @@ function restorePlaceholders(
206268 subshells : string [ ] ,
207269) : string {
208270 let result = command
209- // Restore quotes
271+ // Restore double-quoted strings
210272 result = result . replace ( / _ _ Q U O T E _ ( \d + ) _ _ / g, ( _ , i ) => quotes [ parseInt ( i ) ] )
273+ // Restore single-quoted strings
274+ result = result . replace ( / _ _ S Q U O T E _ ( \d + ) _ _ / g, ( _ , i ) => singleQuotes [ parseInt ( i ) ] )
211275 // Restore redirections
212276 result = result . replace ( / _ _ R E D I R _ ( \d + ) _ _ / g, ( _ , i ) => redirections [ parseInt ( i ) ] )
213277 // Restore array indexing expressions
0 commit comments