mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
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.
136 lines
4.8 KiB
TypeScript
136 lines
4.8 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 'wc' command.
|
|
*
|
|
* Supported options:
|
|
* -l, --lines Count newlines
|
|
* -w, --words Count whitespace-delimited words
|
|
* -c, --bytes Count bytes (UTF-8)
|
|
* -m, --chars Count characters (code points)
|
|
* -L, --max-line-length Print the longest line length
|
|
* -- End of options
|
|
* - Read from stdin
|
|
*
|
|
* Default (no count options) prints: lines, words, bytes.
|
|
*/
|
|
export class WcCommand implements ShellCommand {
|
|
name = 'wc';
|
|
description = 'Count lines, words, and characters in files';
|
|
usage = 'wc [-lwcmL] [--] [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: ['l', 'w', 'c', 'm', 'L'],
|
|
booleanLong: ['lines', 'words', 'bytes', 'chars', 'max-line-length'],
|
|
longToShort: {
|
|
lines: 'l', words: 'w', bytes: 'c', chars: 'm', 'max-line-length': 'L',
|
|
},
|
|
});
|
|
|
|
if (parsed.unknown.length > 0) {
|
|
return createErrorResult(`${this.name}: invalid option -- '${parsed.unknown[0].replace(/^-+/, '')}'`);
|
|
}
|
|
|
|
let opts = {
|
|
lines: parsed.flags.has('l'),
|
|
words: parsed.flags.has('w'),
|
|
bytes: parsed.flags.has('c'),
|
|
chars: parsed.flags.has('m'),
|
|
maxLine: parsed.flags.has('L'),
|
|
};
|
|
|
|
// Default: -lwc
|
|
if (!opts.lines && !opts.words && !opts.bytes && !opts.chars && !opts.maxLine) {
|
|
opts = { lines: true, words: true, bytes: true, chars: false, maxLine: false };
|
|
}
|
|
|
|
const files = parsed.operands.length > 0 ? parsed.operands : ['-'];
|
|
const results: Array<{ counts: Counts; name: string }> = [];
|
|
const total: Counts = { lines: 0, words: 0, bytes: 0, chars: 0, maxLine: 0 };
|
|
|
|
for (const filePath of files) {
|
|
let content: string;
|
|
if (filePath === '-') {
|
|
content = input ?? '';
|
|
} else {
|
|
try {
|
|
const absolutePath = resolvePath(filePath, cwd);
|
|
const stats = await this.fs.stat(absolutePath);
|
|
if (stats.isDirectory()) {
|
|
return createErrorResult(`${this.name}: ${filePath}: Is a directory`);
|
|
}
|
|
content = await this.fs.readFile(absolutePath, 'utf8');
|
|
} 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 : 'Unknown error'}`);
|
|
}
|
|
}
|
|
|
|
const counts = countContent(content);
|
|
results.push({ counts, name: filePath === '-' ? '' : filePath });
|
|
total.lines += counts.lines;
|
|
total.words += counts.words;
|
|
total.bytes += counts.bytes;
|
|
total.chars += counts.chars;
|
|
if (counts.maxLine > total.maxLine) total.maxLine = counts.maxLine;
|
|
}
|
|
|
|
const format = (c: Counts, name: string): string => {
|
|
const parts: string[] = [];
|
|
if (opts.lines) parts.push(String(c.lines).padStart(8));
|
|
if (opts.words) parts.push(String(c.words).padStart(8));
|
|
if (opts.bytes) parts.push(String(c.bytes).padStart(8));
|
|
if (opts.chars) parts.push(String(c.chars).padStart(8));
|
|
if (opts.maxLine) parts.push(String(c.maxLine).padStart(8));
|
|
if (name) parts.push(name);
|
|
return parts.join(' ').replace(/^ /, '');
|
|
};
|
|
|
|
const outputs = results.map((r) => format(r.counts, r.name));
|
|
if (results.length > 1) outputs.push(format(total, 'total'));
|
|
|
|
return createSuccessResult(outputs.join('\n') + (outputs.length > 0 ? '\n' : ''));
|
|
}
|
|
}
|
|
|
|
interface Counts {
|
|
lines: number;
|
|
words: number;
|
|
bytes: number;
|
|
chars: number;
|
|
maxLine: number;
|
|
}
|
|
|
|
function countContent(content: string): Counts {
|
|
const lines = (content.match(/\n/g) ?? []).length;
|
|
const trimmed = content.trim();
|
|
const words = trimmed ? trimmed.split(/\s+/).length : 0;
|
|
// POSIX -c is bytes: count UTF-8 byte length.
|
|
const bytes = new TextEncoder().encode(content).length;
|
|
// -m is characters (code points).
|
|
let chars = 0;
|
|
for (const _ of content) chars++;
|
|
// -L: max line length (without the trailing newline).
|
|
let maxLine = 0;
|
|
for (const line of content.split('\n')) {
|
|
if (line.length > maxLine) maxLine = line.length;
|
|
}
|
|
return { lines, words, bytes, chars, maxLine };
|
|
}
|