Files
shakespeare/src/lib/commands/cat.ts
T
Alex Gleason 376a982449 Overhaul shell commands for POSIX compliance and AI-agent ergonomics
Each built-in shell command has been audited and rewritten to support the
options AI agents (and humans) typically reach for, close critical behavior
gaps, and align with POSIX/GNU conventions:

- Shared argument parser (utils.ts) handles combined short flags, long
  options with '=VALUE' or space-separated values, the '--' sentinel,
  numeric shortcuts like 'head -5', and '-' for stdin.
- cat, ls, cp, mv, rm, mkdir, touch: added missing common flags; removed
  the legacy 'absolute paths are not supported' and 'reading from stdin
  is not supported' barriers where they made agent use awkward.
- mv now overwrites destinations by default (POSIX) and supports -n.
- touch updates mtime on existing files (by rewriting content) and
  accepts -c/-a/-m/-h/-r/-d/-t options.
- find: new expression engine with -iname, -path, -regex, -type,
  -empty, -size, -maxdepth, -mindepth, -not, -or, -print/-print0,
  -delete, -quit.
- grep: -v, -c, -l, -L, -w, -x, -o, -q, -E, -F, -G, -H, -h, -A/-B/-C
  context, --include/--exclude, multiple -e, -f scriptfile.
- head/tail: -c bytes, -NUM shorthand, negative/positive line counts,
  proper multi-file headers.
- sort: -k keys, -t separator, -o output, -f/-b/-d/-s, -h/-V/-M modes.
- uniq: -i, -f, -s, -w, -D, and the [input [output]] form.
- wc: -m characters and -L longest line; -c now counts bytes (UTF-8).
- cut: open-ended ranges (3-, -5), -s, --complement, --output-delimiter.
- tr: -c/-s/-t, [:class:] character classes, escape sequences.
- sed: real multi-command parser supporting ';', addresses (N, $,
  /re/, N,M), '!' negation, proper -n quiet mode, y///, and -i[SUFFIX].
- echo: -n, -e, -E with standard backslash escapes.
- pwd/cd: -L/-P, plus cd -, cd ~, cd $HOME with OLDPWD tracking.
- diff: proper LCS algorithm producing correct normal and unified
  output; adds -q, -s, -N, -i, -w, -b, -B.
- env: -i, -u, NAME=VAL assignments; reads the live shell environment.
- date: -u, -d, -R, -I, and a full GNU-subset format specifier set.

Tests for each command were updated to reflect the corrected behavior
(e.g. absolute paths and stdin are now accepted, POSIX error messages).
All 1334 tests pass; typecheck and production build succeed.
2026-04-26 23:58:13 -05:00

175 lines
5.6 KiB
TypeScript

import type { JSRuntimeFS } from "../JSRuntime";
import type { ShellCommand, ShellCommandResult } from "./ShellCommand";
import { createSuccessResult, createErrorResult } from "./ShellCommand";
import { classifyFsError, parseOptions, resolvePath } from "./utils";
/**
* Implementation of the 'cat' command.
*
* Concatenate files and/or standard input to standard output.
*
* Supported options (POSIX + common GNU extensions):
* -n Number all output lines (1-based)
* -b Number non-empty output lines only
* -s Squeeze multiple blank lines into one
* -E Display $ at end of each line
* -T Display tabs as ^I
* -A Equivalent to -vET (minus control-char rendering for -v)
* -v Display non-printing characters as ^X or M-X
* -u POSIX: unbuffered (accepted, no-op)
* -- End of options
* - Read from standard input at this position
*/
export class CatCommand implements ShellCommand {
name = 'cat';
description = 'Concatenate and display file contents';
usage = 'cat [-nbsETAvu] [--] [file...]';
private fs: JSRuntimeFS;
constructor(fs: JSRuntimeFS) {
this.fs = fs;
}
async execute(args: string[], cwd: string, input?: string): Promise<ShellCommandResult> {
const parsed = parseOptions(args, {
booleanShort: ['n', 'b', 's', 'E', 'T', 'A', 'v', 'u'],
});
if (parsed.unknown.length > 0) {
return createErrorResult(`${this.name}: invalid option -- '${parsed.unknown[0].replace(/^-+/, '')}'`);
}
// -A implies -vET
if (parsed.flags.has('A')) {
parsed.flags.add('v');
parsed.flags.add('E');
parsed.flags.add('T');
}
// If no file operands (and no explicit `-`), default to stdin when input is provided,
// otherwise POSIX `cat` with no args reads stdin — we mirror that for pipelines.
const operands = parsed.operands.length > 0 ? parsed.operands : ['-'];
let combined = '';
for (const filePath of operands) {
if (filePath === '-') {
if (input === undefined) {
// No piped input and an explicit `-`: POSIX says read stdin; in our
// non-interactive environment, treat as empty.
continue;
}
combined += input;
continue;
}
try {
const absolutePath = resolvePath(filePath, cwd);
const stats = await this.fs.stat(absolutePath);
if (stats.isDirectory()) {
return createErrorResult(`${this.name}: ${filePath}: Is a directory`);
}
const content = await this.fs.readFile(absolutePath, 'utf8');
combined += content;
} catch (error) {
const { kind } = classifyFsError(error);
if (kind === 'ENOENT') {
return createErrorResult(`${this.name}: ${filePath}: No such file or directory`);
}
if (kind === 'EACCES') {
return createErrorResult(`${this.name}: ${filePath}: Permission denied`);
}
return createErrorResult(`${this.name}: ${filePath}: ${error instanceof Error ? error.message : String(error)}`);
}
}
// If no processing options are set, return raw content fast-path.
if (
!parsed.flags.has('n') &&
!parsed.flags.has('b') &&
!parsed.flags.has('s') &&
!parsed.flags.has('E') &&
!parsed.flags.has('T') &&
!parsed.flags.has('v')
) {
return createSuccessResult(combined);
}
const output = this.applyFilters(combined, parsed.flags);
return createSuccessResult(output);
}
private applyFilters(content: string, flags: Set<string>): string {
const hadTrailingNewline = content.endsWith('\n');
const bodyLines = (hadTrailingNewline ? content.slice(0, -1) : content).split('\n');
// -s: squeeze consecutive blank lines
let lines = bodyLines;
if (flags.has('s')) {
const squeezed: string[] = [];
let prevBlank = false;
for (const line of lines) {
const isBlank = line === '';
if (isBlank && prevBlank) continue;
squeezed.push(line);
prevBlank = isBlank;
}
lines = squeezed;
}
// -v: render non-printing characters
if (flags.has('v')) {
lines = lines.map((l) => renderNonPrinting(l, flags.has('T')));
}
// -T: show tabs as ^I
if (flags.has('T')) {
lines = lines.map((l) => l.replace(/\t/g, '^I'));
}
// -E: end-of-line markers
if (flags.has('E')) {
lines = lines.map((l) => l + '$');
}
// -n / -b: line numbering (-b wins over -n)
if (flags.has('b')) {
let n = 0;
lines = lines.map((l) => {
// -b numbers nonblank lines. With -E, $ was appended; check original blankness.
const original = flags.has('E') ? l.slice(0, -1) : l;
if (original === '') return '\t' + l;
n++;
return String(n).padStart(6) + '\t' + l;
});
} else if (flags.has('n')) {
lines = lines.map((l, idx) => String(idx + 1).padStart(6) + '\t' + l);
}
return lines.join('\n') + (hadTrailingNewline ? '\n' : '');
}
}
function renderNonPrinting(line: string, skipTab: boolean): string {
let out = '';
for (const ch of line) {
const code = ch.charCodeAt(0);
if (skipTab && ch === '\t') {
out += ch;
continue;
}
if (code < 32 && ch !== '\t') {
out += '^' + String.fromCharCode(code + 64);
} else if (code === 127) {
out += '^?';
} else if (code >= 128 && code < 160) {
out += 'M-^' + String.fromCharCode(code - 128 + 64);
} else if (code >= 160 && code < 256) {
out += 'M-' + String.fromCharCode(code - 128);
} else {
out += ch;
}
}
return out;
}