@sema-agent/core
Version:
Stateless, task-oriented AI agent core
657 lines • 55.7 kB
JavaScript
import { shellWordsOf } from "../../shell/shell-words.js";
const READ = { kind: "path", effect: "read" };
const WRITE = { kind: "path", effect: "write" };
const WRITE_THROUGH = { kind: "path", effect: "write", through: true };
const CREATED = { kind: "path", effect: "create" };
const TOUCHED = { kind: "path", effect: "create", seats: ["landing"] };
const REMOVED = { kind: "path", effect: "write", seats: ["entry"] };
const COPY_SOURCE = { kind: "path", effect: "write", seats: ["landing", "entry"], writtenAt: [] };
const MOVE_SOURCE = { kind: "path", effect: "write", seats: ["landing", "entry"], writtenAt: ["entry"] };
const MOVE_DESTINATION = { kind: "path", effect: "write", seats: ["landing", "entry"] };
const COPY_DESTINATION = { kind: "path", effect: "write", seats: ["landing", "entry"], through: "plain" };
const EDITED = { kind: "path", effect: "edit-in-place", seats: ["entry"] };
const NAMES = { kind: "path", effect: "names" };
const INDIRECT = { kind: "path", effect: "read", valueForm: "indirect" };
const FORM = { kind: "path", effect: "read", valueForm: "form" };
const HEADER = { kind: "path", effect: "read", valueForm: "header" };
const URL = { kind: "path", effect: "read", valueForm: "url", programGlob: true };
const UPLOAD = { kind: "path", effect: "read", programGlob: true };
const COUNT = { kind: "datum", form: "count" };
const PATTERN = { kind: "datum", form: "pattern" };
const NAME_PATTERN = { kind: "datum", form: "name-pattern" };
const LABEL = { kind: "datum", form: "label" };
const DELIMITER = { kind: "datum", form: "delimiter" };
const MESSAGE = { kind: "datum", form: "message" };
const SHELL_TEXT = { kind: "shell-text" };
const ASSIGNMENT = { kind: "assignment" };
const ENTRY_PROBE = { kind: "entry-probe" };
const BINDS_NAME = { kind: "binds-name" };
const COMMAND = { kind: "command" };
const VOLUME = { kind: "mount-source", form: "volume" };
const MOUNT = { kind: "mount-source", form: "mount" };
const OBJECT_PATH = { kind: "object-path" };
const text = (language) => ({ kind: "program-text", language });
const file = (language) => ({ kind: "program-file", language });
const WALKS = { walks: true };
const SWITCH = { arity: 0 };
const QUERY = { arity: 0, mode: "queries" };
const val = (value, extra = {}) => ({ arity: 1, ...(value !== undefined ? { value } : {}), ...extra });
const optional = (value) => ({ arity: "attached", ...(value !== undefined ? { value } : {}) });
const switches = (letters, extra = {}) => Object.fromEntries([...letters].map((l) => [l, { arity: 0, ...extra }]));
const longSwitches = (names, extra = {}) => Object.fromEntries(names.map((n) => [n, { arity: 0, ...extra }]));
const values = (letters, value) => Object.fromEntries([...letters].map((l) => [l, val(value)]));
const grammar = (short, long = {}, extra = {}) => ({ dialect: "any", short, long, afterFirstOperand: "either", ...extra });
function implementations(union, gnu, bsd) {
const one = (dialect, d) => {
const short = { ...union.short };
const long = { ...union.long };
const dashWords = union.dashWords === undefined ? undefined : { ...union.dashWords };
const table = (spelling) => (spelling.startsWith("--") ? long : spelling.length === 2 ? short : dashWords);
const key = (spelling) => spelling.replace(/^--?/, "");
for (const s of d.lacks ?? [])
delete table(s)?.[key(s)];
for (const [s, spec] of Object.entries(d.reads ?? {})) {
const t = table(s);
if (t !== undefined)
t[key(s)] = spec;
}
return { ...union, dialect, short, long, ...(dashWords !== undefined ? { dashWords } : {}), afterFirstOperand: d.afterFirstOperand ?? union.afterFirstOperand, ...(d.obsoleteFirst !== undefined ? { obsoleteFirst: d.obsoleteFirst } : {}) };
};
return [one("gnu", gnu), one("bsd", bsd)];
}
const BINARY = "binary";
const BANNERS = longSwitches(["help", "version"], { mode: "banner" });
const head = {
key: "head",
grammars: implementations(grammar({ c: val(COUNT, { bounds: "bytes" }), n: val(COUNT, { bounds: "lines" }), ...switches("qvz") }, { bytes: val(COUNT, { bounds: "bytes" }), lines: val(COUNT, { bounds: "lines" }), ...longSwitches(["quiet", "silent", "verbose", "zero-terminated"]), ...BANNERS }), {}, { lacks: ["-q", "-v", "-z", "--quiet", "--silent", "--verbose", "--zero-terminated", "--help", "--version"] }),
operands: { stdinBelow: 1 },
witness: BINARY,
};
const TAIL_OBSOLETE = { f: { arity: 0, mode: "follows" }, F: { arity: 0, mode: "follows" }, b: SWITCH, c: SWITCH, l: SWITCH, r: SWITCH };
const tail = {
key: "tail",
grammars: implementations(grammar({ ...values("bcns", COUNT), ...switches("fF", { mode: "follows" }), ...switches("qvz") }, {
bytes: val(COUNT), lines: val(COUNT), "max-unchanged-stats": val(COUNT), pid: val(COUNT), "sleep-interval": val(COUNT),
follow: { arity: "attached", value: LABEL, mode: "follows" },
...longSwitches(["quiet", "retry", "silent", "verbose", "zero-terminated"]), ...BANNERS,
}), { lacks: ["-b"], obsoleteFirst: { signs: "+-", letters: TAIL_OBSOLETE } }, { lacks: ["-s", "-z", "--max-unchanged-stats", "--pid", "--sleep-interval", "--follow", "--retry", "--zero-terminated", "--help", "--version"], obsoleteFirst: { signs: "+-", letters: TAIL_OBSOLETE } }),
operands: { stdinBelow: 1 },
witness: BINARY,
};
const cut = {
key: "cut",
grammars: implementations(grammar({ b: val(COUNT), c: val(COUNT), d: val(DELIMITER), f: val(COUNT), ...switches("nsz") }, {
bytes: val(COUNT), characters: val(COUNT), delimiter: val(DELIMITER), fields: val(COUNT), "output-delimiter": val(DELIMITER),
...longSwitches(["complement", "only-delimited", "zero-terminated"]), ...BANNERS,
}), {}, { lacks: ["-z", "--bytes", "--characters", "--delimiter", "--fields", "--output-delimiter", "--complement", "--only-delimited", "--zero-terminated", "--help", "--version"] }),
operands: { stdinBelow: 1 },
witness: BINARY,
};
const GREP_GRAMMAR = grammar({
A: val(COUNT), B: val(COUNT), C: val(COUNT), D: val(LABEL), d: val(LABEL, { selectsWhenValue: "recurse" }), e: val(PATTERN, { fillsSlot: true }), f: val(READ, { fillsSlot: true }), m: val(COUNT),
...switches("0123456789abcEFGhHiIlLnoPqsUvwxyzZ"),
V: { arity: 0, mode: "banner" },
...switches("rR", { selects: "traversal" }),
}, {
"after-context": val(COUNT), "before-context": val(COUNT), "binary-files": val(LABEL), devices: val(LABEL), directories: val(LABEL, { selectsWhenValue: "recurse" }),
exclude: val(NAME_PATTERN), "exclude-dir": val(NAME_PATTERN), "exclude-from": val(READ), file: val(READ, { fillsSlot: true }), "group-separator": val(LABEL),
include: val(NAME_PATTERN), label: val(LABEL), "max-count": val(COUNT), regexp: val(PATTERN, { fillsSlot: true }),
color: optional(LABEL), colour: optional(LABEL), context: optional(COUNT),
...longSwitches(["basic-regexp", "byte-offset", "count", "extended-regexp", "files-with-matches", "files-without-match", "fixed-strings", "ignore-case", "initial-tab", "invert-match", "line-buffered", "line-number", "line-regexp", "no-filename", "no-group-separator", "no-messages", "null", "null-data", "only-matching", "perl-regexp", "quiet", "silent", "text", "with-filename", "word-regexp"]), ...BANNERS,
...longSwitches(["dereference-recursive", "recursive"], { selects: "traversal" }),
}, { walksCwdWithoutOperand: true });
const BSD_GREP_LACKS = ["-P", "--exclude-from", "--group-separator", "--initial-tab", "--no-group-separator", "--perl-regexp", "--dereference-recursive"];
const GNU_MATCHER_CONFLICTS = {
grep: [],
egrep: ["-F", "-G", "-P", "--fixed-strings", "--basic-regexp", "--perl-regexp"],
fgrep: ["-E", "-G", "-P", "--extended-regexp", "--basic-regexp", "--perl-regexp"],
};
const grepRow = (key) => ({
key,
grammars: implementations(GREP_GRAMMAR, { reads: { "--context": val(COUNT) }, lacks: GNU_MATCHER_CONFLICTS[key] ?? [] }, { lacks: BSD_GREP_LACKS }),
operands: { positional: [PATTERN], stdinBelow: 2 },
traversal: { discloses: "contents" },
witness: BINARY,
});
const diff = {
key: "diff",
grammars: implementations(grammar({ C: val(COUNT), D: val(LABEL), F: val(PATTERN), I: val(PATTERN), L: val(LABEL), S: val(NAME_PATTERN), U: val(COUNT), W: val(COUNT), X: val(READ), x: val(NAME_PATTERN), ...switches("abBcdeEHilnNpqstTuvwyZ"), ...switches("r", { selects: "traversal" }) }, {
exclude: val(NAME_PATTERN), "exclude-from": val(READ), "from-file": val(READ, WALKS), "horizon-lines": val(COUNT), ifdef: val(LABEL), "ignore-matching-lines": val(PATTERN),
label: val(LABEL), "show-function-line": val(PATTERN), "starting-file": val(NAME_PATTERN), tabsize: val(COUNT), "to-file": val(READ, WALKS), width: val(COUNT),
color: optional(LABEL), context: optional(COUNT), unified: optional(COUNT),
...longSwitches(["brief", "ed", "expand-tabs", "ignore-all-space", "ignore-blank-lines", "ignore-case", "ignore-space-change", "ignore-tab-expansion", "ignore-trailing-space", "initial-tab", "minimal", "new-file", "normal", "paginate", "report-identical-files", "rcs", "show-c-function", "side-by-side", "speed-large-files", "suppress-blank-empty", "text"]), ...BANNERS,
...longSwitches(["recursive"], { selects: "traversal" }),
}), { reads: { "-v": { arity: 0, mode: "banner" } } }, { lacks: ["-E", "-v", "-Z", "--from-file", "--to-file", "--ignore-tab-expansion", "--ignore-trailing-space", "--suppress-blank-empty"] }),
operands: { stdinBelow: 2 },
traversal: { discloses: "contents", always: true },
witness: BINARY,
};
const cmp = {
key: "cmp",
grammars: implementations(grammar({ i: val(COUNT), n: val(COUNT), ...switches("blsv") }, { bytes: val(COUNT), "ignore-initial": val(COUNT), ...longSwitches(["print-bytes", "quiet", "silent", "verbose"]), ...BANNERS }), { reads: { "-v": { arity: 0, mode: "banner" } } }, { lacks: ["-v", "--help", "--version"] }),
operands: { stdinBelow: 2 },
witness: BINARY,
};
const IN_PLACE_ATTACHED = { arity: "attached", value: LABEL, selects: "edit-in-place" };
const IN_PLACE_SUFFIX = { arity: "suffix", value: LABEL, selects: "edit-in-place" };
const INERT = { effect: "none" };
const SED_LONG = {
expression: val(text("sed"), { fillsSlot: true, ...INERT }), file: val(file("sed"), { fillsSlot: true, ...INERT }), "line-length": val(COUNT),
"in-place": IN_PLACE_ATTACHED,
"follow-symlinks": { arity: 0, writesAt: ["landing"] },
...longSwitches(["debug", "null-data", "sandbox", "separate", "unbuffered"]),
...longSwitches(["posix", "regexp-extended", "zero-terminated"], INERT),
...longSwitches(["quiet", "silent"], { ...INERT, mode: "quiet" }),
...BANNERS,
};
const SED_SHORT = {
e: val(text("sed"), { fillsSlot: true, ...INERT }), f: val(file("sed"), { fillsSlot: true, ...INERT }),
i: IN_PLACE_ATTACHED, I: IN_PLACE_SUFFIX,
n: { arity: 0, ...INERT, mode: "quiet" },
...switches("s"),
...switches("uzEraH", INERT),
};
const sed = {
key: "sed",
grammars: implementations({ dialect: "any", short: SED_SHORT, long: SED_LONG, afterFirstOperand: "either" }, { reads: { "-l": val(COUNT) }, lacks: ["-a", "-H", "-I"], afterFirstOperand: "options" }, { reads: { "-l": SWITCH, "-i": IN_PLACE_SUFFIX }, lacks: ["-s", "-z", ...Object.keys(SED_LONG).map((n) => `--${n}`)], afterFirstOperand: "operands" }),
operands: { positional: [text("sed")], editedInPlace: EDITED, stdinBelow: 2 },
witness: BINARY,
};
const date = {
key: "date",
grammars: implementations(grammar({ d: val(COUNT), f: val(READ), r: val(NAMES), s: val(COUNT, { effect: "writes" }), ...switches("uR") }, { date: val(COUNT), file: val(READ), reference: val(NAMES), "rfc-3339": val(COUNT), set: val(COUNT, { effect: "writes" }), "iso-8601": optional(LABEL), ...longSwitches(["debug", "rfc-822", "rfc-email", "universal", "utc"]), ...BANNERS }), {}, { reads: { "-f": val(LABEL) }, lacks: ["--file", "--reference"] }),
witness: BINARY,
};
const tar = {
key: "tar",
grammars: [grammar({ b: val(COUNT), C: val(NAMES, WALKS), f: val(READ), T: val(READ, { delivers: "operands" }), X: val(READ), g: val(), ...switches("adhjJkmOpPStvwWxzZ"), ...switches("cru", { selects: "traversal" }) }, {
"blocking-factor": val(COUNT), directory: val(NAMES, WALKS), exclude: val(NAME_PATTERN), "exclude-from": val(READ), file: val(READ), "files-from": val(READ, { delivers: "operands" }), "strip-components": val(COUNT),
...longSwitches(["absolute-names", "dereference", "extract", "get", "interactive", "keep-old-files", "list", "preserve-permissions", "sparse", "to-stdout", "touch", "verbose", "verify"]), ...BANNERS,
...longSwitches(["append", "create", "update"], { selects: "traversal" }),
})],
traversal: { discloses: "contents", keyWordLetters: "cru" },
witness: BINARY,
};
const find = {
key: "find",
grammars: implementations(grammar({ D: val(LABEL), O: val(COUNT), f: val(NAMES), ...switches("HLPdsx") }, BANNERS, {
afterFirstOperand: "options",
dashWords: {
amin: val(COUNT), anewer: val(NAMES), atime: val(COUNT), cmin: val(COUNT), cnewer: val(NAMES), context: val(LABEL),
ctime: val(COUNT), fstype: val(LABEL), gid: val(COUNT), group: val(LABEL), ilname: val(NAMES), iname: val(NAME_PATTERN),
inum: val(COUNT), ipath: val(NAMES), iregex: val(NAME_PATTERN), iwholename: val(NAMES), links: val(COUNT),
lname: val(NAMES), maxdepth: val(COUNT), mindepth: val(COUNT), mmin: val(COUNT), mtime: val(COUNT), name: val(NAME_PATTERN),
newer: val(NAMES), newerat: val(NAMES), neweraa: val(NAMES), neweram: val(NAMES), newerac: val(NAMES),
newerct: val(NAMES), newerca: val(NAMES), newercm: val(NAMES), newercc: val(NAMES),
newermt: val(NAMES), newerma: val(NAMES), newermm: val(NAMES), newermc: val(NAMES),
newerBt: val(NAMES), newerBa: val(NAMES), newerBm: val(NAMES), newerBc: val(NAMES),
path: val(NAMES), perm: val(LABEL), printf: val(LABEL), regex: val(NAME_PATTERN), regextype: val(LABEL),
samefile: val(NAMES), size: val(COUNT), type: val(LABEL), uid: val(COUNT), used: val(COUNT), user: val(LABEL),
wholename: val(NAMES), xtype: val(LABEL),
delete: { arity: 0, effect: "writes" },
...longSwitches(["exec", "execdir", "ok", "okdir"], { effect: "executes" }),
fprint: val(WRITE), fprint0: val(WRITE), fls: val(WRITE), fprintf: val(WRITE),
"files0-from": val(READ, { delivers: "operands" }),
...longSwitches([
"and", "daystart", "depth", "empty", "executable", "false", "follow", "ignore_readdir_race", "mount", "noignore_readdir_race", "noleaf", "not", "nogroup",
"nouser", "or", "print", "print0", "prune", "quit", "readable", "true", "writable", "xdev",
]),
...BANNERS,
},
}), { lacks: ["-f", "-s", "-x"], reads: { "-samefile": val(READ) } }, { lacks: ["-D", "-O", "--help", "--version", "-context", "-printf", "-regextype", "-used", "-xtype", "-fprint", "-fprint0", "-fls", "-fprintf", "-files0-from", "-daystart", "-executable", "-readable", "-writable", "-help", "-version"] }),
operands: { dash: "file" },
traversal: { discloses: "names", always: true },
witness: BINARY,
};
const curl = {
key: "curl",
grammars: [grammar({
A: val(LABEL), b: val(INDIRECT), C: val(COUNT), c: val(WRITE_THROUGH), D: val(WRITE_THROUGH), d: val(INDIRECT), E: val(READ), e: val(LABEL),
F: val(FORM), H: val(HEADER), K: val(READ), m: val(COUNT), o: val(WRITE_THROUGH), P: val(LABEL), Q: val(LABEL), r: val(COUNT),
T: val(UPLOAD), t: val(LABEL), U: val(LABEL), u: val(LABEL), w: val(INDIRECT), X: val(LABEL), x: val(LABEL), Y: val(COUNT),
y: val(COUNT), z: val(READ), g: { arity: 0, literalValues: true },
...switches("#012346aBfGiIjJklLnNOpqRsSvZ"),
...switches("hMV", { mode: "banner" }),
}, {
cacert: val(READ), capath: val(READ), cert: val(READ), config: val(READ), "connect-timeout": val(COUNT), cookie: val(INDIRECT), "cookie-jar": val(WRITE_THROUGH),
data: val(INDIRECT), "data-ascii": val(INDIRECT), "data-binary": val(INDIRECT), "data-raw": val(LABEL), "data-urlencode": val(FORM), "dump-header": val(WRITE_THROUGH),
form: val(FORM), "form-string": val(LABEL), header: val(HEADER), key: val(READ), "max-time": val(COUNT), "netrc-file": val(READ), output: val(WRITE_THROUGH),
proxy: val(LABEL), range: val(COUNT), referer: val(LABEL), request: val(LABEL), resolve: val(LABEL), trace: val(WRITE_THROUGH), "time-cond": val(READ),
"trace-ascii": val(WRITE_THROUGH), "upload-file": val(UPLOAD), url: val(URL), user: val(LABEL), "user-agent": val(LABEL), "write-out": val(INDIRECT),
...longSwitches(["append", "compressed", "disable", "fail", "get", "head", "include", "insecure", "junk-session-cookies", "list-only", "location", "netrc", "next", "no-buffer", "parallel", "proxytunnel", "remote-header-name", "remote-name", "remote-time", "show-error", "silent", "use-ascii", "verbose"]), ...BANNERS,
manual: { arity: 0, mode: "banner" }, globoff: { arity: 0, literalValues: true },
})],
operands: { rest: URL },
witness: BINARY,
};
const ls = {
key: "ls",
grammars: implementations(grammar(switches("R", { selects: "traversal" }), longSwitches(["recursive"], { selects: "traversal" })), {}, { lacks: ["--recursive"] }),
operands: { dash: "file", rest: NAMES },
traversal: { discloses: "names" },
witness: BINARY,
};
const namesWalker = (key) => ({ key, grammars: [], operands: { dash: "file", rest: NAMES }, traversal: { discloses: "names", always: true }, witness: BINARY });
const contentsWalker = (key, short, long, extra = {}) => ({
key,
grammars: [grammar(short, long, { walksCwdWithoutOperand: true, ...extra })],
operands: { positional: [PATTERN] },
traversal: { discloses: "contents", always: true },
witness: BINARY,
});
const rg = contentsWalker("rg", { e: val(PATTERN, { fillsSlot: true }), f: val(READ, { fillsSlot: true }), ...values("ABCdjmM", COUNT), ...values("ErtT", LABEL), g: val(NAME_PATTERN) }, {
regexp: val(PATTERN, { fillsSlot: true }), file: val(READ, { fillsSlot: true }),
files: { arity: 0, fillsSlot: true },
...Object.fromEntries(["after-context", "before-context", "context", "max-count", "max-depth", "max-columns", "threads"].map((n) => [n, val(COUNT)])),
...Object.fromEntries(["color", "colors", "context-separator", "dfa-size-limit", "encoding", "engine", "field-context-separator", "field-match-separator", "generate", "hyperlink-format", "max-filesize", "path-separator", "regex-size-limit", "replace", "sort", "sortr", "type", "type-add", "type-clear", "type-not"].map((n) => [n, val(LABEL)])),
glob: val(NAME_PATTERN), iglob: val(NAME_PATTERN), "pre-glob": val(NAME_PATTERN), "ignore-file": val(READ),
...BANNERS,
}, { acceptsAbbreviations: false });
const ag = contentsWalker("ag", { g: val(PATTERN, { fillsSlot: true }), G: val(PATTERN), m: val(COUNT), p: val(READ), A: val(COUNT), B: val(COUNT), C: val(COUNT) }, {
"file-search-regex": val(PATTERN), "max-count": val(COUNT), "path-to-ignore": val(READ),
after: optional(COUNT), before: optional(COUNT), context: optional(COUNT),
...BANNERS,
});
const ack = contentsWalker("ack", { f: { arity: 0, fillsSlot: true }, m: val(COUNT), A: optional(COUNT), B: optional(COUNT), C: optional(COUNT), x: { arity: 0, delivers: "operands" } }, {
match: val(PATTERN, { fillsSlot: true }), "max-count": val(COUNT), ackrc: val(READ), "files-from": val(READ, { delivers: "operands" }),
"after-context": optional(COUNT), "before-context": optional(COUNT), context: optional(COUNT),
...BANNERS,
});
const BANNER_LONGS = ["--version", "--help", "--usage", "-version", "-help"];
function interpreterGrammar(language, g) {
const short = {};
const long = {};
const dashWords = {};
const put = (spelling, spec) => {
if (spelling.startsWith("--"))
long[spelling.slice(2)] = spec;
else
dashWords[spelling.slice(1)] = spec;
};
for (const l of g.plain ?? "")
short[l] = SWITCH;
for (const l of g.interactive ?? "")
short[l] = { arity: 0, mode: "interactive" };
for (const l of g.banner ?? "")
short[l] = { arity: 0, mode: "banner" };
for (const l of g.separated ?? "")
short[l] = val();
for (const l of g.attached ?? "")
short[l] = { arity: "attached", ...((g.banner ?? "").includes(l) ? { mode: "banner" } : {}) };
for (const l of g.file ?? "")
short[l] = val(file(language), { fillsSlot: true, ...(g.fileEndsOptions === true ? { endsOptions: true } : {}) });
for (const l of g.module ?? "")
short[l] = { arity: 1, mode: "names-program", fillsSlot: true, ...(g.fileEndsOptions === true ? { endsOptions: true } : {}) };
for (const l of g.assignment ?? "")
short[l] = val(ASSIGNMENT);
for (const l of g.modules ?? "")
short[l] = val(file(language));
for (const l of g.text ?? "")
short[l] = val(text(language), { fillsSlot: true });
for (const s of g.longPlain ?? [])
put(s, SWITCH);
for (const s of g.longValue ?? [])
put(s, val());
for (const s of g.longAttached ?? [])
put(s, optional());
for (const s of BANNER_LONGS)
put(s, { arity: 0, mode: "banner" });
for (const s of g.longSupplemental ?? [])
put(s, val(READ));
for (const s of g.longModules ?? [])
put(s, val(file(language)));
for (const s of g.longNamesProgram ?? [])
put(s, { arity: 0, mode: "names-program" });
for (const s of g.longFile ?? [])
put(s, val(file(language), { fillsSlot: true, ...(g.fileEndsOptions === true ? { endsOptions: true } : {}) }));
for (const s of g.longText ?? [])
put(s, val(text(language), { fillsSlot: true }));
return { dialect: "any", short, long, dashWords, afterFirstOperand: "either" };
}
const interpreter = (key, language, g, program, stdinWhenUnnamed, extra = {}, departures) => ({
key,
versioned: true,
grammars: departures === undefined ? [interpreterGrammar(language, g)] : implementations(interpreterGrammar(language, g), departures.gnu, departures.bsd),
...(program === "none" ? {} : { operands: { positional: program === "file" ? [file(language)] : program === "text" ? [text(language)] : [undefined, text(language)] } }),
delivery: { stdinWhenUnnamed },
witness: BINARY,
...extra,
});
const PERL = { text: "eEmM", separated: "I", attached: "iCFxDV", plain: "nplawcsStTUWX", banner: "vVh" };
const RUBY = { text: "e", separated: "IrECKT", attached: "F", plain: "nplawcsSU", banner: "vh" };
const PYTHON = { text: "c", module: "m", separated: "WXQ", plain: "uBEIsSqbdvxOtR", banner: "Vh?", interactive: "i", fileEndsOptions: true };
const NODE = {
longText: ["--eval", "--print"], text: "ep",
longModules: ["--import", "--loader", "--experimental-loader", "--require"],
longValue: ["--env-file", "--conditions"],
longAttached: ["--max-old-space-size"],
longNamesProgram: ["--test"],
longPlain: ["--inspect", "--inspect-brk", "--no-warnings", "--trace-warnings", "--watch", "--enable-source-maps", "--experimental-modules", "--no-deprecation", "--check"],
modules: "r", plain: "c", banner: "vh", interactive: "i",
};
const PHP = { longText: ["--run", "--process-begin", "--process-code", "--process-end"], text: "rRBE", file: "fF", separated: "dcz", plain: "nqswl", banner: "vhi", interactive: "a" };
const LUA = { text: "e", separated: "l", banner: "vh", interactive: "i" };
const EXPECT = { text: "c", separated: "Db", plain: "dN", banner: "vh", interactive: "i" };
const AWK = { longText: ["--source"], text: "e", longFile: ["--file", "--exec"], file: "fE", separated: "F", assignment: "v", banner: "V" };
const POSIX_AWK = { file: "f", separated: "F", assignment: "v" };
const AWK_IMPLEMENTATIONS = { gnu: {}, bsd: { lacks: ["-E", "--file", "--exec", "--source"] } };
const JQ = { longFile: ["--from-file"], file: "f", separated: "L", plain: "nrjcsSaCMRe", banner: "h" };
const BC = { longText: ["--expression"], text: "e", plain: "lqsw", banner: "vh", interactive: "i" };
const DC = { longText: ["--expression"], text: "e", longFile: ["--file"], file: "f", banner: "hV" };
const SQLITE = { longText: ["-cmd"], longSupplemental: ["-init"] };
const PSQL = { longText: ["--command"], text: "c", longFile: ["--file"], file: "f", separated: "hpUdoF", plain: "aeEqtxXwW", banner: "lV?" };
const OSASCRIPT = { text: "e", separated: "ls", plain: "i", banner: "h" };
const RSCRIPT = { text: "e", banner: "h" };
const JULIA = { longText: ["--eval", "--print"], text: "eE", separated: "JLp", plain: "q", banner: "vh", interactive: "i" };
const GHCI = { text: "e", separated: "io" };
const GROOVY = { text: "e", separated: "Dcl", plain: "n", banner: "vh", interactive: "i" };
const BUN = { longText: ["--eval", "--print"], text: "ep", modules: "r", banner: "vh" };
const launcher = (key, spec = {}, g, extra = {}) => ({
key,
grammars: g === undefined ? [] : [g],
launcher: spec ?? {},
witness: BINARY,
...extra,
});
const PRIVILEGED = { declared: "runs the program as another user or in another root: the gate does not acquire privileges" };
const LINUX_NAMESPACE = { declared: "a Linux namespace / scheduling wrapper that needs privileges or a kernel feature the gate host may not have" };
const SHELL_TEXT_LAUNCHER = { declared: "hands its operands to a shell as text: the row reads no program word whose opens the gate could predict" };
const WORKDIR = { kind: "path", effect: "names", base: "working-directory" };
const ROOTDIR = { kind: "path", effect: "names", base: "root" };
const PROGRAM_SEARCH = { kind: "path", effect: "names", base: "program-search" };
export const DURATION_SPELLING = /^\d+(?:\.\d+)?[smhd]?$/;
const BANNER = { arity: 0, mode: "banner" };
const launcherSwitches = (spellings, banners) => {
const spec = (x) => (x === "--help" || x === "--version" || banners.includes(x) ? BANNER : SWITCH);
return {
short: Object.fromEntries(spellings.filter((x) => !x.startsWith("--")).map((x) => [x.slice(1), spec(x)])),
long: Object.fromEntries(spellings.filter((x) => x.startsWith("--")).map((x) => [x.slice(2), spec(x)])),
};
};
const launcherGrammar = (g, extra = {}) => {
const sw = launcherSwitches(g.switches ?? [], g.banners ?? []);
const short = { ...sw.short };
const long = { ...sw.long };
const put = (spelling, spec) => { if (spelling.startsWith("--"))
long[spelling.slice(2)] = spec;
else
short[spelling.slice(1)] = spec; };
for (const [spelling, role] of Object.entries(g.values ?? {}))
put(spelling, val(role));
for (const [spelling, role] of Object.entries(g.optional ?? {}))
put(spelling, optional(role));
for (const spelling of g.shellText ?? [])
put(spelling, val(SHELL_TEXT));
for (const spelling of g.shells ?? [])
put(spelling, { arity: 0, mode: "interactive" });
for (const spelling of g.programs ?? [])
put(spelling, { arity: 0, mode: "names-program" });
return { dialect: "any", short, long, afterFirstOperand: "either", ...extra };
};
const SU_OPTIONS = {
switches: ["-f", "-l", "-P", "-T", "-h", "-V", "--fast", "--login", "--pty", "--no-pty", "--help", "--version"],
banners: ["-h", "-V"],
values: { "-g": LABEL, "-G": LABEL, "-w": LABEL, "-s": COMMAND, "--group": LABEL, "--supp-group": LABEL, "--whitelist-environment": LABEL, "--shell": COMMAND },
shellText: ["-c", "--command", "--session-command"],
programs: ["-m", "-p", "--preserve-environment"],
};
const SU = launcherGrammar(SU_OPTIONS, { stepsDashDash: true });
const SU_LINUX_ONLY = [...SU_OPTIONS.switches, ...Object.keys(SU_OPTIONS.values), ...SU_OPTIONS.shellText, ...SU_OPTIONS.programs].filter((s) => !["-f", "-l", "-m"].includes(s));
const launchers = [
{ key: "env", grammars: implementations(launcherGrammar({
switches: ["-i", "-v", "--ignore-environment", "--debug", "--list-signal-handling", "--help", "--version"],
values: { "-u": LABEL, "--unset": LABEL, "-C": WORKDIR, "--chdir": WORKDIR, "-a": LABEL, "--argv0": LABEL, "-P": PROGRAM_SEARCH },
optional: { "--default-signal": LABEL, "--ignore-signal": LABEL },
shellText: ["-S", "--split-string"],
}, { stepsDashDash: true }), { lacks: ["-P"] }, { lacks: ["-a", "--ignore-environment", "--debug", "--list-signal-handling", "--help", "--version", "--unset", "--chdir", "--argv0", "--default-signal", "--ignore-signal", "--split-string"] }), launcher: { assignmentOperands: true, bareDash: true }, witness: BINARY },
launcher("command", {}, grammar({ p: SWITCH, v: QUERY, V: QUERY }, {}, { stepsDashDash: true })),
launcher("builtin", {}, grammar({}, {}, { stepsDashDash: true })),
launcher("exec", {}, launcherGrammar({ switches: ["-c", "-l"], values: { "-a": LABEL } }, { stepsDashDash: true })),
launcher("noglob", {}, undefined, { witness: { declared: "a zsh precommand modifier" } }),
launcher("nocorrect", {}, undefined, { witness: { declared: "a zsh precommand modifier" } }),
{ key: "xargs", grammars: implementations(launcherGrammar({
switches: ["-0", "-r", "-t", "-x", "-p", "-o", "--null", "--no-run-if-empty", "--verbose", "--exit", "--interactive", "--open-tty", "--show-limits", "--help", "--version"],
values: { "-n": COUNT, "-L": COUNT, "-P": COUNT, "-I": LABEL, "-d": DELIMITER, "-s": COUNT, "-E": LABEL, "--max-args": COUNT, "--max-procs": COUNT, "--delimiter": DELIMITER, "--max-chars": COUNT, "--process-slot-var": LABEL },
optional: { "-i": LABEL, "-l": COUNT, "--replace": LABEL, "--eof": LABEL, "--max-lines": COUNT },
}, {}), { reads: { "-a": val(READ, { delivers: "operands" }), "--arg-file": val(READ, { delivers: "operands" }) } }, { lacks: ["-d", "-i", "-l", "-a", "--arg-file", "--null", "--no-run-if-empty", "--verbose", "--exit", "--interactive", "--open-tty", "--show-limits", "--help", "--version", "--max-args", "--max-procs", "--delimiter", "--max-chars", "--process-slot-var", "--replace", "--eof", "--max-lines"] }), launcher: {}, reentry: { kind: "argv", defaultProgram: "echo" }, witness: BINARY },
launcher("busybox"),
launcher("sudo", { assignmentOperands: true, shellCommand: "escaped", asUser: { userWith: ["-u", "--user"], home: "policy", loginWith: ["-i", "--login"] } }, launcherGrammar({
switches: ["-A", "-b", "-E", "-H", "-K", "-k", "-l", "-n", "-P", "-S", "-V", "-v", "--askpass", "--background", "--preserve-env", "--set-home", "--remove-timestamp", "--reset-timestamp", "--list", "--non-interactive", "--preserve-groups", "--stdin", "--version", "--validate", "--bell", "--help"],
shells: ["-i", "-s", "--login", "--shell"],
values: {
"-u": LABEL, "-g": LABEL, "-U": LABEL, "-C": COUNT, "-D": WORKDIR, "-h": LABEL, "-p": LABEL, "-r": LABEL, "-R": ROOTDIR, "-t": LABEL, "-T": COUNT, "-a": LABEL,
"--user": LABEL, "--group": LABEL, "--other-user": LABEL, "--close-from": COUNT, "--chdir": WORKDIR, "--host": LABEL, "--prompt": LABEL, "--role": LABEL, "--chroot": ROOTDIR, "--type": LABEL, "--command-timeout": COUNT, "--auth-type": LABEL,
},
}, { stepsDashDash: true }), { witness: PRIVILEGED }),
launcher("doas", { asUser: { userWith: ["-u"], home: "target", loginWith: [] } }, launcherGrammar({ switches: ["-L", "-n"], shells: ["-s"], values: { "-a": LABEL, "-u": LABEL, "-C": READ } }, { stepsDashDash: true }), { witness: PRIVILEGED }),
launcher("pkexec", { whenNoProgram: "shell" }, launcherGrammar({ switches: ["--disable-internal-agent", "--keep-cwd", "--version", "--help"], values: { "--user": LABEL } }), { witness: PRIVILEGED }),
{ key: "su", grammars: implementations(SU, { afterFirstOperand: "options" }, { lacks: SU_LINUX_ONLY, afterFirstOperand: "operands" }), launcher: { shellText: "user", asUser: { home: "target", loginWith: ["-l", "--login"] } }, witness: PRIVILEGED },
{ key: "runuser", grammars: [launcherGrammar({ ...SU_OPTIONS, values: { ...SU_OPTIONS.values, "-u": LABEL, "--user": LABEL } }, { afterFirstOperand: "options", stepsDashDash: true })], launcher: { shellText: "user", programWith: ["-u", "--user"], asUser: { home: "target", loginWith: ["-l", "--login"] } }, witness: PRIVILEGED },
launcher("chroot", { requiredOperands: 1, whenNoProgram: "shell" }, undefined, { witness: PRIVILEGED }),
launcher("setpriv", {}, undefined, { witness: PRIVILEGED }),
launcher("nohup", {}, grammar({}, BANNERS, { stepsDashDash: true })),
{ key: "nice", grammars: implementations(grammar({ n: val(COUNT) }, { adjustment: val(COUNT), ...BANNERS }, { stepsDashDash: true }), {}, { lacks: ["--adjustment", "--help", "--version"] }), launcher: { negativeIntegerIsOwnArgument: true }, witness: BINARY },
launcher("ionice", {}, launcherGrammar({
switches: ["-t", "--ignore", "-h", "--help", "-V", "--version"],
banners: ["-h", "-V"],
values: { "-c": LABEL, "-n": COUNT, "-p": COUNT, "-P": COUNT, "-u": COUNT, "--class": LABEL, "--classdata": COUNT, "--pid": COUNT, "--pgid": COUNT, "--uid": COUNT },
}), { witness: LINUX_NAMESPACE }),
launcher("chrt", { requiredOperands: 1, requiredOperandSpelling: /^\d+$/ }, launcherGrammar({
switches: ["-b", "-f", "-i", "-o", "-r", "-d", "-m", "-a", "-R", "-v", "--batch", "--fifo", "--idle", "--other", "--rr", "--deadline", "--max", "--all-tasks", "--reset-on-fork", "--verbose", "--help", "--version"],
values: { "-p": COUNT, "--pid": COUNT, "-T": COUNT, "-P": COUNT, "-D": COUNT, "--sched-runtime": COUNT, "--sched-period": COUNT, "--sched-deadline": COUNT },
}), { witness: LINUX_NAMESPACE }),
launcher("taskset", { requiredOperands: 1, requiredOperandSpelling: /^(?:0x)?[\da-f]+$|^[\d,:-]+$/i }, launcherGrammar({ switches: ["-a", "-p", "-c", "--all-tasks", "--pid", "--cpu-list", "--help", "--version"] }), { witness: LINUX_NAMESPACE }),
{ key: "stdbuf", grammars: implementations(grammar({ i: val(LABEL), o: val(LABEL), e: val(LABEL) }, { input: val(LABEL), output: val(LABEL), error: val(LABEL), ...BANNERS }, { stepsDashDash: true }), {}, { lacks: ["--input", "--output", "--error", "--help", "--version"] }), launcher: {}, witness: BINARY },
launcher("timeout", { requiredOperands: 1, requiredOperandSpelling: DURATION_SPELLING }, grammar({ v: SWITCH, k: val(COUNT), s: val(LABEL) }, { foreground: SWITCH, "preserve-status": SWITCH, verbose: SWITCH, "kill-after": val(COUNT), signal: val(LABEL), ...BANNERS }, { stepsDashDash: true })),
{ key: "time", grammars: implementations(grammar({ p: SWITCH, a: SWITCH, v: SWITCH, q: SWITCH, f: val(LABEL), o: val(WRITE) }, { portability: SWITCH, append: SWITCH, verbose: SWITCH, quiet: SWITCH, help: SWITCH, version: SWITCH, format: val(LABEL), output: val(WRITE) }, { stepsDashDash: true }), {}, { lacks: ["-v", "-q", "-f", "--portability", "--append", "--verbose", "--quiet", "--help", "--version", "--format", "--output"] }), launcher: {}, witness: BINARY },
{ key: "watch", grammars: [
launcherGrammar({
switches: ["-b", "-C", "-c", "-e", "-f", "-g", "-h", "-p", "-r", "-t", "-w", "-v", "-x", "--beep", "--color", "--no-color", "--errexit", "--follow", "--chgexit", "--exec", "--precise", "--no-rerun", "--no-title", "--no-wrap", "--help", "--version"],
banners: ["-h", "-v"],
values: { "-n": COUNT, "--interval": COUNT, "-q": COUNT, "--equexit": COUNT, "-s": WRITE, "--shotsdir": WRITE },
optional: { "-d": LABEL, "--differences": LABEL },
}, { afterFirstOperand: "operands", stepsDashDash: true }),
launcherGrammar({ switches: ["-d", "-t"], values: { "-n": COUNT } }, { afterFirstOperand: "operands", stepsDashDash: true }),
], launcher: { shellText: "joined", programWith: ["-x", "--exec"] }, witness: SHELL_TEXT_LAUNCHER },
launcher("setsid", {}, launcherGrammar({ switches: ["-c", "-f", "-w", "--ctty", "--fork", "--wait", "--help", "--version"] }, { stepsDashDash: true })),
launcher("flock", { requiredOperands: 1 }, launcherGrammar({
switches: ["-s", "-x", "-u", "-n", "-o", "-F", "--shared", "--exclusive", "--unlock", "--nonblock", "--nb", "--close", "--no-fork", "--verbose", "--help", "--version"],
values: { "-w": COUNT, "-E": COUNT, "--timeout": COUNT, "--wait": COUNT, "--conflict-exit-code": COUNT },
shellText: ["-c", "--command"],
}, { stepsDashDash: true }), { operands: { rest: CREATED } }),
launcher("unshare", { whenNoProgram: "shell" }, launcherGrammar({
switches: ["-m", "-u", "-i", "-n", "-p", "-U", "-C", "-T", "-f", "-r", "-c", "--mount", "--uts", "--ipc", "--net", "--pid", "--user", "--cgroup", "--time", "--fork", "--map-root-user", "--map-current-user", "--kill-child", "--mount-proc", "--keep-caps", "--help", "--version"],
values: { "-R": ROOTDIR, "-w": WORKDIR, "-S": LABEL, "-G": LABEL, "--setuid": LABEL, "--setgid": LABEL, "--root": ROOTDIR, "--wd": WORKDIR, "--propagation": LABEL, "--setgroups": LABEL, "--monotonic": COUNT, "--boottime": COUNT, "--map-user": LABEL, "--map-group": LABEL },
}), { witness: LINUX_NAMESPACE }),
launcher("nsenter", { whenNoProgram: "shell" }, launcherGrammar({
switches: ["-a", "-m", "-u", "-i", "-n", "-p", "-U", "-C", "-T", "-F", "-Z", "--all", "--mount", "--uts", "--ipc", "--net", "--pid", "--user", "--cgroup", "--time", "--no-fork", "--preserve-credentials", "--help", "--version"],
values: { "-t": COUNT, "-S": LABEL, "-G": LABEL, "--target": COUNT, "--setuid": LABEL, "--setgid": LABEL },
optional: { "-r": ROOTDIR, "-w": WORKDIR, "--root": ROOTDIR, "--wd": WORKDIR },
}), { witness: LINUX_NAMESPACE }),
launcher("script", { shellText: "joined" }, launcherGrammar({ shellText: ["-c", "--command"] }), { witness: SHELL_TEXT_LAUNCHER }),
launcher("numactl", {}, undefined, { witness: LINUX_NAMESPACE }),
launcher("prlimit", {}, undefined, { witness: LINUX_NAMESPACE }),
launcher("systemd-run", {}, undefined, { witness: { declared: "hands the program to a service manager" } }),
launcher("strace", {}, launcherGrammar({
switches: ["-f", "-c", "-C", "-d", "-D", "-h", "-i", "-k", "-q", "-r", "-t", "-T", "-v", "-V", "-w", "-x", "-y", "-z", "-Z", "-F", "--follow-forks", "--summary-only", "--summary", "--debug", "--help", "--version"],
banners: ["-h", "-V"],
values: {
"-e": LABEL, "-o": WRITE, "-p": COUNT, "-s": COUNT, "-E": ASSIGNMENT, "-P": NAMES, "-S": LABEL, "-a": COUNT, "-b": LABEL, "-I": COUNT, "-u": LABEL, "-X": LABEL, "-O": COUNT, "-U": LABEL,
"--output": WRITE, "--trace": LABEL, "--expr": LABEL, "--attach": COUNT, "--string-limit": COUNT, "--env": ASSIGNMENT, "--trace-path": NAMES, "--columns": COUNT, "--user": LABEL, "--interruptible": COUNT,
"--detach-on": LABEL, "--const-print-style": LABEL, "--summary-sort-by": LABEL, "--summary-syscall-overhead": COUNT, "--summary-columns": LABEL,
},
}), { witness: LINUX_NAMESPACE }),
launcher("ltrace", {}, launcherGrammar({
switches: ["-c", "-C", "-f", "-i", "-L", "-r", "-S", "-t", "-T", "-V", "--demangle", "--help", "--version"],
banners: ["-V"],
values: { "-a": COUNT, "-A": COUNT, "-e": LABEL, "-l": NAMES, "-n": COUNT, "-o": WRITE, "-p": COUNT, "-s": COUNT, "-u": LABEL, "-x": LABEL, "-D": LABEL, "-F": READ, "-w": COUNT, "--align": COUNT, "--config": READ, "--debug": LABEL, "--indent": COUNT, "--library": NAMES, "--output": WRITE, "--string-max": COUNT, "--where": COUNT },
}), { witness: LINUX_NAMESPACE }),
launcher("valgrind"),
launcher("firejail", { whenNoProgram: "shell" }, undefined, { witness: LINUX_NAMESPACE }),
launcher("bwrap", {}, undefined, { witness: LINUX_NAMESPACE }),
];
function gitSubcommand(plain, attached, valued, longValued) {
return { grammar: grammar({ ...switches(plain), ...Object.fromEntries([...attached].map((l) => [l, optional()])), ...Object.fromEntries([...valued].map((l) => [l, l === "m" ? val(MESSAGE) : val()])) }, Object.fromEntries(longValued.map((n) => [n, n === "message" ? val(MESSAGE) : val()]))) };
}
const GIT_GREP = {
grammar: grammar({
e: val(PATTERN, { fillsSlot: true }), f: val(READ, { fillsSlot: true }), A: val(COUNT), B: val(COUNT), C: val(COUNT), m: val(COUNT),
O: { arity: "attached", value: LABEL, effect: "executes" },
...switches("aEFGHIPchilnoqrsvwzL"),
}, {
regexp: val(PATTERN, { fillsSlot: true }), file: val(READ, { fillsSlot: true }), "after-context": val(COUNT), "before-context": val(COUNT), context: val(COUNT),
"max-count": val(COUNT), "max-depth": val(COUNT), threads: val(COUNT), "open-files-in-pager": { arity: "attached", value: LABEL, effect: "executes" },
...longSwitches(["cached", "untracked", "no-index", "recurse-submodules", "text", "ignore-case", "word-regexp", "invert-match", "line-number", "column", "files-with-matches", "files-without-match", "count", "null", "break", "heading", "show-function", "function-context", "extended-regexp", "basic-regexp", "fixed-strings", "perl-regexp", "all-match", "quiet", "full-name"]),
}, { walksCwdWithoutOperand: true }),
operands: { positional: [PATTERN] },
traversal: { discloses: "contents", always: true },
};
const gitObjectSubcommand = (short = {}, long = {}) => ({ grammar: grammar(short, long), operands: { rest: OBJECT_PATH } });
const git = {
key: "git",
grammars: [grammar({ p: SWITCH, P: SWITCH, C: val(WORKDIR), c: val() }, {
...longSwitches(["paginate", "no-pager", "bare", "no-replace-objects", "literal-pathspecs", "glob-pathspecs", "noglob-pathspecs", "icase-pathspecs", "no-optional-locks"]),
"git-dir": val(WORKDIR), "work-tree": val(WORKDIR), namespace: val(), "config-env": val(), "super-prefix": val(),
})],
subcommands: {
commit: gitSubcommand("apsnvqeioz", "Su", "mFCct", ["message", "file", "template", "pathspec-from-file", "author", "date", "cleanup", "fixup", "squash", "trailer", "reuse-message", "reedit-message"]),
tag: gitSubcommand("asfdvle", "n", "mFu", ["message", "file", "local-user", "cleanup", "trailer", "sort", "format", "contains", "no-contains", "points-at", "merged", "no-merged"]),
merge: gitSubcommand("nqve", "S", "mFsX", ["message", "file", "strategy", "strategy-option", "into-name", "cleanup"]),
stash: gitSubcommand("pkuaqS", "", "m", ["message", "pathspec-from-file"]),
grep: GIT_GREP,
diff: { grammar: grammar({}, {}), operands: { rest: READ } },
show: gitObjectSubcommand(),
"cat-file": gitObjectSubcommand(switches("ptsep")),
archive: gitObjectSubcommand({ o: val(WRITE) }, { output: val(WRITE), format: val(LABEL), prefix: val(LABEL), remote: val(LABEL) }),
},
witness: BINARY,
};
const noPath = (key, rest, extra = {}) => ({ key, grammars: [], operands: { rest }, witness: BINARY, ...extra });
const BUILTIN_ONLY = { declared: "a shell builtin with no binary of its own on every host: its words are read by the shell" };
const noPathRows = [
noPath("pwd", LABEL, { builtin: "inert" }), noPath("echo", LABEL, { builtin: "inert", grammars: [grammar(switches("neE"), {}, { afterFirstOperand: "operands" })] }), noPath("printf", LABEL, { grammars: [grammar({ v: val(BINDS_NAME) })] }), noPath("whoami", LABEL), noPath("uname", LABEL),
noPath("which", LABEL), noPath("basename", LABEL), noPath("dirname", LABEL),
{ key: "tr", grammars: [], operands: { rest: LABEL, stdinBelow: "always" }, witness: BINARY },
noPath("cal", COUNT), noPath("uptime", LABEL), noPath("id", LABEL), noPath("groups", LABEL), noPath("nproc", LABEL), noPath("locale", LABEL),
noPath("free", LABEL, { grammars: [grammar({ s: val(COUNT, { mode: "repeats" }) }, { seconds: val(COUNT, { mode: "repeats" }) })] }),
noPath("true", LABEL, { builtin: "inert" }), noPath("false", LABEL, { builtin: "inert" }), noPath("seq", COUNT), noPath("expr", LABEL), noPath("type", LABEL, { witness: BUILTIN_ONLY }), noPath("sleep", COUNT),
{ key: "test", grammars: [grammar({ v: val(BINDS_NAME) })], operands: { rest: ENTRY_PROBE, evaluates: true }, witness: BINARY },
{ key: "[", grammars: [grammar({ v: val(BINDS_NAME) })], operands: { rest: ENTRY_PROBE, evaluates: true }, witness: BINARY },
];
const DECLARE_GRAMMAR = grammar({
i: { arity: 0, assigns: "integer" }, n: { arity: 0, assigns: "reference" }, r: { arity: 0, assigns: "frozen" },
...switches("aAluc", { assigns: "transformed" }), ...switches("pfFgI", { assigns: "unsettled" }), ...switches("tx"),
}, {}, { plusShort: { ...switches("afFirtx"), p: { arity: 0, assigns: "unsettled" } } });
const EXPORT_GRAMMAR = grammar({ ...switches("np"), ...switches("aAf", { assigns: "unsettled" }) });
const declaration = (key, g, rest = BINDS_NAME, assigns) => ({ key, grammars: [g], operands: { rest }, builtin: "declaration", ...(assigns !== undefined ? { assigns } : {}), witness: BUILTIN_ONLY });
const nameBinders = [
{ key: "read", grammars: [grammar({ a: val(BINDS_NAME), p: val(LABEL), d: val(DELIMITER), i: val(LABEL), t: val(COUNT), n: val(COUNT), N: val(COUNT), u: val(COUNT), ...switches("ers") })], operands: { rest: BINDS_NAME }, witness: BUILTIN_ONLY },
{ key: "getopts", grammars: [], operands: { positional: [LABEL, { kind: "binds-name", asWritten: true }], rest: LABEL }, witness: BUILTIN_ONLY },
{ key: "wait", grammars: [grammar({ ...switches("fn"), p: val(BINDS_NAME) })], operands: { rest: LABEL }, witness: BUILTIN_ONLY },
{ key: "unset", grammars: [], operands: { rest: BINDS_NAME }, witness: BUILTIN_ONLY },
{ key: "let", grammars: [], operands: { rest: { kind: "program-text", language: "arithmetic" } }, witness: BUILTIN_ONLY },
];
const assignmentNotPaths = (key) => ({ key, grammars: [], operands: { assignmentArguments: "not-paths" }, witness: { declared: "a build tool / container client: what a NAME=value argument means is the build's or the container's" } });
const CONTAINER_RUN = {
grammar: grammar({ v: val(VOLUME), e: val(ASSIGNMENT), w: val(LABEL), u: val(LABEL), p: val(LABEL), h: val(LABEL), l: val(LABEL), m: val(LABEL), ...switches("itdPq") }, {
volume: val(VOLUME), mount: val(MOUNT), env: val(ASSIGNMENT), "env-file": val(READ), "label-file": val(READ),
name: val(LABEL), network: val(LABEL), entrypoint: val(LABEL), user: val(LABEL), workdir: val(LABEL), publish: val(LABEL), platform: val(LABEL), hostname: val(LABEL), label: val(LABEL),
...longSwitches(["rm", "detach", "interactive", "tty", "privileged", "init", "read-only"]),
}, { afterFirstOperand: "operands" }),
operands: { assignmentArguments: "not-paths" },
};
const containerRuntime = (key) => ({
key,
grammars: [grammar({ H: val(LABEL), c: val(LABEL), l: val(LABEL), D: SWITCH }, { host: val(LABEL), context: val(LABEL), "log-level": val(LABEL), config: val(READ), tlscacert: val(READ), tlscert: val(READ), tlskey: val(READ), ...longSwitches(["debug", "tls", "tlsverify"]) })],
operands: { assignmentArguments: "not-paths" },
subcommands: { run: CONTAINER_RUN, create: CONTAINER_RUN },
witness: { declared: "a build tool / container client: what a NAME=value argument means is the build's or the container's" },
});
const shellRow = (key, textAt, witness = BINARY, grammars = [], stdin) => ({ key, grammars, reentry: { kind: "shell", text: textAt, ...(stdin !== undefined ? { stdin } : {}) }, witness });
const shellGrammar = (plain, valued, long = {}) => grammar({ ...switches(plain), ...(plain.includes("s") ? switches("s", { mode: "interactive" }) : {}), ...values(valued, LABEL), c: val(SHELL_TEXT) }, long);
const posixShellGrammar = (plain, valued) => ({ ...shellGrammar(plain, valued), plusOptions: true });
const POSIX_SHELL = posixShellGrammar("abCefhimnsuvx", "o");
const SHELL_GRAMMARS = {
sh: posixShellGrammar("abefimnsuvxCEI", "o"),
bash: posixShellGrammar("abefhiklmnprstuvxBCDEHPT", "oO"),
dash: posixShellGrammar("abefilmnsuvxCEIV", "o"),
ksh: posixShellGrammar("abefhiklmnprstuvxBCEGH", "o"),
zsh: posixShellGrammar("adefghiklmnprstuvwxyBCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", "o"),
csh: shellGrammar("defilmnqstvxFVX", ""),
tcsh: shellGrammar("defilmnqstvxFVX", ""),
fish: grammar({ ...switches("ilnNP"), ...switches("hv", { mode: "banner" }), c: val(SHELL_TEXT), C: val(), d: val(LABEL), f: val(LABEL), o: val(WRITE), p: val(WRITE) }, { command: val(SHELL_TEXT), ...BANNERS }),
ash: POSIX_SHELL, mksh: POSIX_SHELL, pdksh: POSIX_SHELL, yash: POSIX_SHELL,
};
const SHELLS = ["sh", "bash", "zsh", "dash", "ksh", "fish", "ash", "mksh", "yash", "pdksh", "csh", "tcsh"];
const codeBodyRows = [
{ key: "trap", grammars: [grammar(switches("lpP"))], body: "first-operand", witness: BINARY },
{ key: "alias", grammars: [], body: "assignment-values", witness: BINARY },
{ key: "mapfile", grammars: [grammar({ ...switches("t"), ...values("dunOcs"), C: val(SHELL_TEXT) })], operands: { rest: BINDS_NAME }, body: "option-values", witness: BINARY },
{ key: "readarray", grammars: [grammar({ ...switches("t"), ...values("dunOcs"), C: val(SHELL_TEXT) })], operands: { rest: BINDS_NAME }, body: "option-values", witness: BINARY },
{ key: "compgen", grammars: [grammar({ ...switches("abcdefgjksuv"), ...values("oAGWPSXV"), F: val(COMMAND), C: val(SHELL_TEXT) })], body: "option-values", witness: BINARY },
{ key: "complete", grammars: [grammar({ ...switches("abcdefgjksuvDEIpr"), ...values("oAGWPSXV"), F: val(COMMAND), C: val(SHELL_TEXT) })], body: "option-values", witness: { declared: "a completion spec runs its command only when a person asks for a completion interactively" } },
];
const copyGrammars = (bsdSuffixSwitch, destination) => implementations(grammar({ S: val(LABEL, { effect: "writes" }), t: val(destination, { fillsLast: true }) }, { "target-directory": val(destination, { fillsLast: true }), suffix: val(LABEL, { effect: "writes" }), "no-preserve": val(LABEL), sparse: val(LABEL) }), { afterFirstOperand: "options" }, { ...(bsdSuffixSwitch ? { reads: { "-S": SWITCH } } : {}), lacks: [...(bsdSuffixSwitch ? [] : ["-S"]), "-t", "--target-directory", "--suffix", "--no-preserve", "--sparse"], afterFirstOperand: "operands" });
const writerRows = [
{ key: "mkdir", grammars: [], operands: { rest: CREATED, dash: "file" }, witness: BINARY },
{ key: "touch", grammars: implementations(grammar({ h: { arity: 0, writesAt: ["landing", "entry"] } }, { "no-dereference": { arity: 0, writesAt: ["landing", "entry"] } }), {}, { lacks: ["--no-dereference"] }), operands: { rest: TOUCHED, dash: "file" }, witness: BINARY },
{ key: "rm", grammars: implementations(grammar({ ...switches("rR", { selects: "traversal" }), f: { arity: 0, mode: "forced" } }, { ...longSwitches(["recursive"], { selects: "traversal" }), force: { arity: 0, mode: "forced" } }), {}, { lacks: ["--recursive", "--force"] }), operands: { rest: REMOVED, dash: "file" }, witness: BINARY },
{ key: "rmdir", grammars: implementations(grammar({ p: { arity: 0, effect: "writes" } }, { parents: { arity: 0, effect: "writes" } }), {}, { lacks: ["--parents"] }), operands: { rest: REMOVED, dash: "file" }, witness: BINARY },
{ key: "tee", grammars: [], operands: { rest: WRITE_THROUGH, dash: "file" }, witness: BINARY },
{ key: "cp", grammars: copyGrammars(true, COPY_DESTINATION), operands: { rest: COPY_SOURCE, last: COPY_DESTINATION, dash: "file" }, witness: BINARY },
{ key: "mv", grammars: copyGrammars(false, MOVE_DESTINATION), operands: { rest: MOVE_SOURCE, last: MOVE_DESTINATION, dash: "file" }, witness: BINARY },
];
const stdinReaders = [
{ key: "cat", grammars: [], operands: { stdinBelow: 1 }, witness: BINARY },
{ key: "wc", grammars: [], operands: { stdinBelow: 1 }, witness: BINARY },
{ key: "comm", grammars: [], operands: { stdinBelow: 2 }, witness: BINARY },
];
const SYNTAX = { declared: "shell syntax, not a program: read by the lexer" };
const block = (key, g = []) => ({ key, grammars: g, syntax: "block", witness: SYNTAX });
const BLOCK_GRAMMARS = { "[[": [grammar({ v: val(BINDS_NAME) })] };
export const PROGRAM_ROWS = [
head, tail, cut, grepRow("grep"), grepRow("egrep"), grepRow("fgrep"), diff, cmp, sed, date, tar, find, curl,
ls, namesWalker("du"), rg, namesWalker("tree"), ag, ack,
interpreter("perl", "perl", PERL, "file", true),
interpreter("ruby", "ruby", RUBY, "file", true),
interpreter("python", "python", PYTHON, "file", true, { modules: { timeit: text("python") } }),
interpreter("node", "javascript", NODE, "file", true),
interpreter("nodejs", "javascript", NODE, "file", true),
interpreter("php", "php", PHP, "file", true),
interpreter("lua", "lua", LUA, "file", true),
interpreter("luajit", "lua", LUA, "file", true),
interpreter("tclsh", "tcl", {}, "file", true),
interpreter("wish", "tcl", {}, "file", true, { witness: { declared: "a windowing shell: it opens a display before it reads its script, and the gate host has none" } }),
interpreter("expect", "expect", EXPECT, "file", true),
interpreter("awk", "awk", AWK, "text", false, {}, AWK_IMPLEMENTATIONS),
interpreter("gawk", "awk", AWK, "text", false),
interpreter("mawk", "awk", POSIX_AWK, "text", false),
interpreter("nawk", "awk", POSIX_AWK, "text", false),
interpreter("jq", "jq", JQ, "text", false),
interpreter("bc", "bc", BC, "none", true),
interpreter("dc", "dc", DC, "none", true),
interpreter("sqlite", "sql", SQLITE, "text-second", true),
interpreter("psql", "sql", PSQL, "none", true, { witness: { declared: "a database client: it connects to a server before it reads a program file or text, and the gate host has no server" } }),
interpreter("osascript", "applescript", OSASCRIPT, "file", true),
interpreter("Rscript", "r", RSCRIPT, "file", false),
interpreter("julia", "julia", JULIA, "file", true),
interpreter("ghci", "haskell", GHCI, "none", true),
interpreter("groovy", "groovy", GROOVY, "file", false),
interpreter("bun", "javascript", BUN, "file", false),
...launchers,
git,
...noPathRows,
...writerRows,
...stdinReaders,
...["declare", "typeset", "local"].map((key) => declaration(key, DECLARE_GRAMMAR)),
declaration("export", EXPORT_GRAMMAR, { kind: "binds-name", asWritten: true }), declaration("readonly", EXPORT_GRAMMAR, { kind: "binds-name", asWritten: true }, "frozen"),
...nameBinders,
...["make", "gmake", "bmake", "cmake", "ninja", "meson"].map(assignmentNotPaths),
containerRuntime("docker"), containerRuntime("podman"),
...[...shellWordsOf("stack_mover")].map((key) => ({ key, grammars: [], movesBase: "stack", witness: BUILTIN_ONLY })),
...SHELLS.map((s) => shellRow(s, "c-option", BINARY, [SHELL_GRAMMARS[s]], "script")),
shellRow("eval", "words", BUILTIN_ONLY), shellRow("source", "file-operand", BUILTIN_ONLY), shellRow(".", "file-operand", BUILTIN_ONLY), shellRow("parallel", "non-option-words", BINARY, [], "script"),
...codeBodyRows,
...[...shellWordsOf("block")].map((k) => block(k, BLOCK_GRAMMARS[k])),
];