diff options
| author | Soccera1 <[email protected]> | 2026-07-18 07:51:42 +1000 |
|---|---|---|
| committer | Soccera1 <[email protected]> | 2026-07-18 07:51:42 +1000 |
| commit | a753f669c9bedfccb9f7f400da018bb487dca5d0 (patch) | |
| tree | 6a5600e6719a6dd69ef035f8f22fea37bd4ea8c1 | |
| parent | 8b8e40523f999fa2e2d676f591ac85c60f063083 (diff) | |
| download | bnu-a753f669c9bedfccb9f7f400da018bb487dca5d0.tar.gz bnu-a753f669c9bedfccb9f7f400da018bb487dca5d0.zip | |
Fix shell quoting
| -rw-r--r-- | src/coreutils.js | 19 | ||||
| -rw-r--r-- | tests/coreutils.test.js | 5 |
2 files changed, 19 insertions, 5 deletions
diff --git a/src/coreutils.js b/src/coreutils.js index e4353bf..aabcac1 100644 --- a/src/coreutils.js +++ b/src/coreutils.js @@ -14836,7 +14836,7 @@ function lsResolvedQuotingStyle(opts) { if (opts.N) return "literal"; if (opts.Q || opts["quote-name"]) return "c"; const style = opts.quoting ?? opts["quoting-style"]; - return resolveLsQuotingStyle(style) ?? (opts.q ? "question" : "literal"); + return resolveLsQuotingStyle(style) ?? (opts.q ? "question" : process.stdout.isTTY ? "shell-escape" : "literal"); } function lsQuestionName(name) { @@ -14883,22 +14883,31 @@ function shellEscapeLsName(name, always = false) { if (!/[\x00-\x1f\x7f]/.test(text)) return shellQuoteLsName(text, always); const parts = []; let plain = ""; - const flush = () => { + let escaped = ""; + const flushPlain = () => { if (plain) { parts.push(`'${plain.replaceAll("'", "'\\''")}'`); plain = ""; } }; + const flushEscaped = () => { + if (escaped) { + parts.push(`$'${escaped}'`); + escaped = ""; + } + }; for (const ch of text) { const code = ch.codePointAt(0); if (code < 32 || code === 127) { - flush(); - parts.push(`$'${lsEscapedName(ch)}'`); + flushPlain(); + escaped += lsEscapedName(ch); } else { + flushEscaped(); plain += ch; } } - flush(); + flushPlain(); + flushEscaped(); if (!parts.length) return "''"; if (always && parts.length === 1 && !parts[0].startsWith("$")) return parts[0]; return parts.join(""); diff --git a/tests/coreutils.test.js b/tests/coreutils.test.js index 1d779d2..34e9dfa 100644 --- a/tests/coreutils.test.js +++ b/tests/coreutils.test.js @@ -5823,6 +5823,11 @@ test("ls supports common listing modes", async () => { await writeFile(join(dir, "quote/line\nbreak"), "x"); await writeFile(join(dir, "quote/q\x07"), "x"); await writeFile(join(dir, "quote/plain"), "x"); + await mkdir(join(dir, "tty-quote")); + await writeFile(join(dir, "tty-quote/line\n\nbreak"), "x"); + const ttyQuoted = await shell(`script -qefc '"$BUN" "$BNU" ls -1 tty-quote' /dev/null`); + expect(ttyQuoted).toMatchObject({ code: 0, stderr: "" }); + expect(ttyQuoted.stdout.replaceAll("\r\n", "\n")).toBe("'line'$'\\n\\n''break'\n"); expect((await run(["ls", "-1", "--quoting-style=shell", "quote/name with spaces", "quote/plain"])).stdout).toBe("'quote/name with spaces'\nquote/plain\n"); expect((await run(["ls", "-1", "--quoting-style=shell", "quote/name'with-quote"])).stdout).toBe("\"quote/name'with-quote\"\n"); expect((await run(["ls", "-1", "--quoting-style=shell-always", "quote/plain"])).stdout).toBe("'quote/plain'\n"); |
