Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/named-zsh-redirects.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@aliou/sh": patch
---

Parse named file descriptor redirects in Bash and Zsh.
8 changes: 8 additions & 0 deletions src/parser/dialect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ describe("dialect enforcement: POSIX", () => {
expectErr("foo <<< bar", "posix", /<<</);
});

it("rejects named file descriptor redirects", () => {
expectErr("foo {fd}<f", "posix", /\{varname\}.*bash\/zsh feature/);
});

it("rejects process substitution", () => {
expectErr("diff <(foo) <(bar)", "posix", /process subst/);
});
Expand Down Expand Up @@ -98,6 +102,10 @@ describe("dialect enforcement: mksh", () => {
it("rejects ${!foo@}", () => {
expectErr("echo ${!foo@}", "mksh", /\$\{!/);
});

it("rejects named file descriptor redirects", () => {
expectErr("foo {fd}<f", "mksh", /\{varname\}.*bash\/zsh feature/);
});
});

describe("dialect enforcement: bash (default) accepts everything", () => {
Expand Down
15 changes: 15 additions & 0 deletions src/parser/redirects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ describe("parse (phase 3: assignments and redirects)", () => {
),
});
});

it.each([
"bash",
"zsh",
] as const)("parses named file descriptor redirects in %s", (dialect) => {
expect(parse("foo {fd}<f", { dialect })).toMatchAst({
ast: program(
stmt({
type: "SimpleCommand",
words: [word("foo")],
redirects: [redirect("<", "f", "{fd}")],
}),
),
});
});
});

describe("parse (phase 12: extended redirects)", () => {
Expand Down
35 changes: 34 additions & 1 deletion src/tokenizer/tokenize.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import type { ParseOptions } from "../ast";
import { checkLang } from "../dialect";
import { isDigit, operatorChars, redirChars, symbolChars } from "./charsets";
import {
isDigit,
isNameChar,
isNameStart,
operatorChars,
redirChars,
symbolChars,
} from "./charsets";
import { SourceMap } from "./cursor";
import { scanBacktick } from "./scan-backtick";
import { scanExpansion } from "./scan-expansion";
Expand Down Expand Up @@ -174,6 +181,32 @@ export function tokenize(source: string, options: ParseOptions = {}): Token[] {
}
}

if (ch === "{" && atBoundary && isNameStart(source.charAt(i + 1))) {
let j = i + 2;
while (j < source.length && isNameChar(source.charAt(j))) {
j += 1;
}
if (source.charAt(j) === "}") {
const redir = tryRedirOp(source, j + 1);
if (redir) {
checkLang(options.dialect, map.posAt(i), "`{varname}` redirects", [
"bash",
"zsh",
]);
tokens.push({
type: "redir",
op: redir.op,
fd: source.slice(i, j + 1),
pos: map.posAt(i),
end: map.posAt(j + 1 + redir.len),
});
i = j + 1 + redir.len;
atBoundary = true;
continue;
}
}
}

if (isDigit(ch)) {
let j = i;
while (j < source.length && isDigit(source.charAt(j))) {
Expand Down