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.
113 lines
3.2 KiB
TypeScript
113 lines
3.2 KiB
TypeScript
import type { ShellCommand, ShellCommandResult } from "./ShellCommand";
|
|
import { createSuccessResult } from "./ShellCommand";
|
|
|
|
/**
|
|
* Implementation of the 'echo' command.
|
|
*
|
|
* Supported options:
|
|
* -n Do not output the trailing newline
|
|
* -e Enable interpretation of backslash escapes
|
|
* -E Disable interpretation of backslash escapes (default)
|
|
*
|
|
* Supported escape sequences (with -e):
|
|
* \\, \a, \b, \c (suppress further output + newline), \e, \f,
|
|
* \n, \r, \t, \v, \0NNN (octal), \xHH (hex)
|
|
*/
|
|
export class EchoCommand implements ShellCommand {
|
|
name = 'echo';
|
|
description = 'Display text';
|
|
usage = 'echo [-neE] [text...]';
|
|
|
|
async execute(args: string[], _cwd: string, _input?: string): Promise<ShellCommandResult> {
|
|
// POSIX echo's argument parsing is very conservative: it stops treating
|
|
// args as options the moment one is not a recognized flag group. This
|
|
// matches the bash builtin.
|
|
let newline = true;
|
|
let interpret = false;
|
|
let i = 0;
|
|
while (i < args.length) {
|
|
const arg = args[i];
|
|
if (!/^-[neE]+$/.test(arg)) break;
|
|
for (const ch of arg.slice(1)) {
|
|
if (ch === 'n') newline = false;
|
|
else if (ch === 'e') interpret = true;
|
|
else if (ch === 'E') interpret = false;
|
|
}
|
|
i++;
|
|
}
|
|
|
|
const rest = args.slice(i).join(' ');
|
|
let output = interpret ? interpretEscapes(rest) : rest;
|
|
|
|
// \c in -e suppresses the trailing newline.
|
|
if (interpret && output.indexOf('\u0000CSUPPRESS\u0000') !== -1) {
|
|
output = output.slice(0, output.indexOf('\u0000CSUPPRESS\u0000'));
|
|
newline = false;
|
|
}
|
|
|
|
return createSuccessResult(output + (newline ? '\n' : ''));
|
|
}
|
|
}
|
|
|
|
function interpretEscapes(s: string): string {
|
|
let out = '';
|
|
let i = 0;
|
|
while (i < s.length) {
|
|
const c = s[i];
|
|
if (c !== '\\' || i + 1 >= s.length) {
|
|
out += c;
|
|
i++;
|
|
continue;
|
|
}
|
|
const next = s[i + 1];
|
|
switch (next) {
|
|
case '\\': out += '\\'; i += 2; break;
|
|
case 'a': out += '\x07'; i += 2; break;
|
|
case 'b': out += '\b'; i += 2; break;
|
|
case 'c':
|
|
// Marker: caller truncates at this sentinel and suppresses newline.
|
|
out += '\u0000CSUPPRESS\u0000';
|
|
i += 2;
|
|
break;
|
|
case 'e': out += '\x1b'; i += 2; break;
|
|
case 'f': out += '\f'; i += 2; break;
|
|
case 'n': out += '\n'; i += 2; break;
|
|
case 'r': out += '\r'; i += 2; break;
|
|
case 't': out += '\t'; i += 2; break;
|
|
case 'v': out += '\v'; i += 2; break;
|
|
case '0': {
|
|
// \0NNN octal (up to 3 digits)
|
|
let j = i + 2;
|
|
let oct = '';
|
|
while (j < s.length && oct.length < 3 && /[0-7]/.test(s[j])) {
|
|
oct += s[j];
|
|
j++;
|
|
}
|
|
out += oct ? String.fromCharCode(parseInt(oct, 8)) : '\0';
|
|
i = j;
|
|
break;
|
|
}
|
|
case 'x': {
|
|
let j = i + 2;
|
|
let hex = '';
|
|
while (j < s.length && hex.length < 2 && /[0-9a-fA-F]/.test(s[j])) {
|
|
hex += s[j];
|
|
j++;
|
|
}
|
|
if (hex) {
|
|
out += String.fromCharCode(parseInt(hex, 16));
|
|
i = j;
|
|
} else {
|
|
out += '\\x';
|
|
i += 2;
|
|
}
|
|
break;
|
|
}
|
|
default:
|
|
out += '\\' + next;
|
|
i += 2;
|
|
}
|
|
}
|
|
return out;
|
|
}
|