|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Quoted-region scanner for shell command lines. |
| 5 | + * |
| 6 | + * One forward pass records every quoted region: where it starts and ends, the |
| 7 | + * argv0 of the statement containing it (the first non-assignment, non-reserved |
| 8 | + * word before the quote; '' when the quote is part of that first word) and |
| 9 | + * whether the string carries a command substitution. A `$(` or backtick inside |
| 10 | + * "..." suspends the string: the substitution body is top-level shell again |
| 11 | + * (its own statements and quotes) until the matching `)` / backtick, after |
| 12 | + * which the string resumes as a new region. Both halves are flagged |
| 13 | + * `substitution`. Regions are appended only once complete, in order, so they |
| 14 | + * are disjoint and sorted. The result is cached per input, so a command line |
| 15 | + * holding thousands of quoted tokens is scanned once. |
| 16 | + */ |
| 17 | + |
| 18 | +// Words that open or structure a compound command; none of them receives the |
| 19 | +// quoted string that follows, so none may become a statement's argv0. |
| 20 | +const SHELL_RESERVED_WORDS = new Set([ |
| 21 | + 'if', 'then', 'else', 'elif', 'fi', 'do', 'done', 'while', 'until', 'for', |
| 22 | + 'case', 'esac', 'in', 'select', 'function', 'coproc', '!', '{', '}', '[[', ']]', |
| 23 | +]); |
| 24 | + |
| 25 | +const ASSIGNMENT_WORD = /^[A-Za-z_][A-Za-z0-9_]*=/; |
| 26 | + |
| 27 | +// Unquoted characters that start a new statement (or a nested command). |
| 28 | +const STATEMENT_SEPARATORS = new Set([';', '|', '&', '\n', '(', ')', '`']); |
| 29 | + |
| 30 | +let cache = { input: null, regions: [] }; |
| 31 | + |
| 32 | +function createState() { |
| 33 | + return { |
| 34 | + regions: [], |
| 35 | + suspended: [], |
| 36 | + quote: null, |
| 37 | + escaped: false, |
| 38 | + open: null, |
| 39 | + argv0: null, |
| 40 | + word: '', |
| 41 | + inWord: false, |
| 42 | + }; |
| 43 | +} |
| 44 | + |
| 45 | +function endWord(state) { |
| 46 | + if (!state.inWord) return; |
| 47 | + if ( |
| 48 | + state.argv0 === null && |
| 49 | + state.word !== '' && |
| 50 | + !SHELL_RESERVED_WORDS.has(state.word) && |
| 51 | + !ASSIGNMENT_WORD.test(state.word) |
| 52 | + ) { |
| 53 | + state.argv0 = state.word; |
| 54 | + } |
| 55 | + state.word = ''; |
| 56 | + state.inWord = false; |
| 57 | +} |
| 58 | + |
| 59 | +function newStatement(state) { |
| 60 | + endWord(state); |
| 61 | + state.argv0 = null; |
| 62 | +} |
| 63 | + |
| 64 | +function openRegion(state, start, quote, argv0, substitution) { |
| 65 | + state.quote = quote; |
| 66 | + state.open = { start, quote, argv0, substitution }; |
| 67 | +} |
| 68 | + |
| 69 | +function closeRegion(state, end, substitution) { |
| 70 | + const region = state.open; |
| 71 | + state.regions.push({ ...region, end, substitution: region.substitution || substitution }); |
| 72 | + state.open = null; |
| 73 | + state.quote = null; |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * `$(` or a backtick inside "...": park the outer statement's word state (the |
| 78 | + * outer word `FOO="pre$(cmd)post"` continues after the substitution as if it |
| 79 | + * were a single character) and scan the body as a fresh statement. Returns |
| 80 | + * the number of characters consumed. |
| 81 | + */ |
| 82 | +function suspendString(state, index, char) { |
| 83 | + state.suspended.push({ |
| 84 | + backtick: char === '`', |
| 85 | + depth: 0, |
| 86 | + argv0: state.open.argv0, |
| 87 | + outer: { word: state.word, inWord: state.inWord, argv0: state.argv0 }, |
| 88 | + }); |
| 89 | + closeRegion(state, index, true); |
| 90 | + state.word = ''; |
| 91 | + state.inWord = false; |
| 92 | + state.argv0 = null; |
| 93 | + return char === '$' ? 2 : 1; |
| 94 | +} |
| 95 | + |
| 96 | +function resumeString(state, index, outer) { |
| 97 | + state.suspended.pop(); |
| 98 | + state.word = outer.outer.word; |
| 99 | + state.inWord = outer.outer.inWord; |
| 100 | + state.argv0 = outer.outer.argv0; |
| 101 | + openRegion(state, index, '"', outer.argv0, true); |
| 102 | +} |
| 103 | + |
| 104 | +/** A character inside a quoted string. Returns the number of characters consumed. */ |
| 105 | +function scanQuotedChar(state, input, index) { |
| 106 | + const char = input.charAt(index); |
| 107 | + if (state.quote === '"' && char === '\\') { |
| 108 | + state.escaped = true; |
| 109 | + return 1; |
| 110 | + } |
| 111 | + if (char === state.quote) { |
| 112 | + closeRegion(state, index, false); |
| 113 | + return 1; |
| 114 | + } |
| 115 | + if (state.quote === '"' && (char === '`' || (char === '$' && input.charAt(index + 1) === '('))) { |
| 116 | + return suspendString(state, index, char); |
| 117 | + } |
| 118 | + state.word += char; |
| 119 | + state.inWord = true; |
| 120 | + return 1; |
| 121 | +} |
| 122 | + |
| 123 | +/** A character outside quotes. Returns the number of characters consumed. */ |
| 124 | +function scanBareChar(state, index, char) { |
| 125 | + if (char === '\\') { |
| 126 | + state.escaped = true; |
| 127 | + state.inWord = true; |
| 128 | + return 1; |
| 129 | + } |
| 130 | + if (char === '"' || char === "'") { |
| 131 | + state.inWord = true; |
| 132 | + openRegion(state, index, char, state.argv0 === null ? '' : state.argv0, false); |
| 133 | + return 1; |
| 134 | + } |
| 135 | + const outer = state.suspended.length > 0 ? state.suspended[state.suspended.length - 1] : null; |
| 136 | + if (outer !== null) { |
| 137 | + const resumes = outer.backtick ? char === '`' : char === ')' && outer.depth === 0; |
| 138 | + if (resumes) { |
| 139 | + resumeString(state, index, outer); |
| 140 | + return 1; |
| 141 | + } |
| 142 | + if (!outer.backtick && (char === '(' || char === ')')) { |
| 143 | + const depth = outer.depth + (char === '(' ? 1 : -1); |
| 144 | + state.suspended = [...state.suspended.slice(0, -1), { ...outer, depth }]; |
| 145 | + } |
| 146 | + } |
| 147 | + if (STATEMENT_SEPARATORS.has(char)) { |
| 148 | + newStatement(state); |
| 149 | + return 1; |
| 150 | + } |
| 151 | + if (/\s/.test(char)) { |
| 152 | + endWord(state); |
| 153 | + return 1; |
| 154 | + } |
| 155 | + state.word += char; |
| 156 | + state.inWord = true; |
| 157 | + return 1; |
| 158 | +} |
| 159 | + |
| 160 | +/** |
| 161 | + * Every quoted region of `input`, sorted by start and disjoint. |
| 162 | + * |
| 163 | + * @param {string} input |
| 164 | + * @returns {Array<{start: number, end: number, quote: string, argv0: string, substitution: boolean}>} |
| 165 | + */ |
| 166 | +function quotedRegions(input) { |
| 167 | + if (cache.input === input) return cache.regions; |
| 168 | + const state = createState(); |
| 169 | + for (let i = 0; i < input.length; ) { |
| 170 | + const char = input.charAt(i); |
| 171 | + if (state.escaped) { |
| 172 | + state.escaped = false; |
| 173 | + state.word += char; |
| 174 | + state.inWord = true; |
| 175 | + i += 1; |
| 176 | + continue; |
| 177 | + } |
| 178 | + i += state.quote ? scanQuotedChar(state, input, i) : scanBareChar(state, i, char); |
| 179 | + } |
| 180 | + if (state.open !== null) state.regions.push({ ...state.open, end: input.length }); |
| 181 | + cache = { input, regions: state.regions }; |
| 182 | + return state.regions; |
| 183 | +} |
| 184 | + |
| 185 | +/** |
| 186 | + * The quoted region that strictly contains `idx`, or null when `idx` is not |
| 187 | + * inside a quote. Regions are disjoint and sorted, so this is a binary search. |
| 188 | + * |
| 189 | + * @param {string} input |
| 190 | + * @param {number} idx |
| 191 | + */ |
| 192 | +function quotedRegionAt(input, idx) { |
| 193 | + const regions = quotedRegions(input); |
| 194 | + let lo = 0; |
| 195 | + let hi = regions.length - 1; |
| 196 | + while (lo <= hi) { |
| 197 | + const mid = (lo + hi) >> 1; |
| 198 | + const region = regions[mid]; |
| 199 | + if (idx <= region.start) { |
| 200 | + hi = mid - 1; |
| 201 | + } else if (idx >= region.end) { |
| 202 | + lo = mid + 1; |
| 203 | + } else { |
| 204 | + return region; |
| 205 | + } |
| 206 | + } |
| 207 | + return null; |
| 208 | +} |
| 209 | + |
| 210 | +module.exports = { quotedRegions, quotedRegionAt, SHELL_RESERVED_WORDS }; |
0 commit comments