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.
109 lines
3.6 KiB
TypeScript
109 lines
3.6 KiB
TypeScript
import type { ShellCommand, ShellCommandResult } from "./ShellCommand";
|
|
import { createSuccessResult, createErrorResult } from "./ShellCommand";
|
|
import { parseOptions } from "./utils";
|
|
|
|
/**
|
|
* Implementation of the 'env' command.
|
|
*
|
|
* Supported options:
|
|
* -i, --ignore-environment Start with an empty environment
|
|
* -u NAME, --unset=NAME Remove NAME from the environment
|
|
* -0, --null End each output line with NUL, not newline
|
|
* -- End of options
|
|
*
|
|
* Synopsis:
|
|
* env [OPTION]... [NAME=VALUE]... [COMMAND [ARG]...]
|
|
*
|
|
* This implementation prints the (optionally modified) environment and
|
|
* supports NAME=VALUE prefixes. It does not exec commands because the
|
|
* shell integration runs each command through the registry; the COMMAND
|
|
* form is handled by the shell executor's command-prefix assignment.
|
|
*/
|
|
export class EnvCommand implements ShellCommand {
|
|
name = 'env';
|
|
description = 'Display environment variables';
|
|
usage = 'env [-i] [-u NAME] [-0] [--] [NAME=VALUE]...';
|
|
|
|
/**
|
|
* Optional accessor that the shell tool may populate so `env` can reflect
|
|
* the shell executor's current environment. When not supplied, falls back
|
|
* to a small default set of vars.
|
|
*/
|
|
envSource: (() => Record<string, string>) | undefined;
|
|
|
|
async execute(args: string[], cwd: string, _input?: string): Promise<ShellCommandResult> {
|
|
const parsed = parseOptions(args, {
|
|
booleanShort: ['i', '0'],
|
|
valueShort: ['u'],
|
|
booleanLong: ['ignore-environment', 'null'],
|
|
valueLong: ['unset'],
|
|
longToShort: {
|
|
'ignore-environment': 'i',
|
|
null: '0',
|
|
unset: 'u',
|
|
},
|
|
});
|
|
|
|
if (parsed.unknown.length > 0) {
|
|
return createErrorResult(`${this.name}: invalid option -- '${parsed.unknown[0].replace(/^-+/, '')}'`);
|
|
}
|
|
|
|
// Collect all -u NAMEs (parseOptions only keeps the last; scan args).
|
|
const unsets = new Set<string>();
|
|
for (let i = 0; i < args.length; i++) {
|
|
if (args[i] === '-u' || args[i] === '--unset') {
|
|
if (i + 1 < args.length) unsets.add(args[++i]);
|
|
} else if (args[i].startsWith('--unset=')) {
|
|
unsets.add(args[i].slice('--unset='.length));
|
|
}
|
|
}
|
|
|
|
const startFresh = parsed.flags.has('i');
|
|
const nullTerm = parsed.flags.has('0');
|
|
|
|
// Source environment
|
|
let env: Record<string, string> = startFresh
|
|
? {}
|
|
: (this.envSource?.() ?? defaultEnv(cwd));
|
|
|
|
// Ensure PWD is set from the live cwd if no source.
|
|
if (!this.envSource && !('PWD' in env)) env.PWD = cwd;
|
|
|
|
// Remove requested unsets.
|
|
for (const name of unsets) delete env[name];
|
|
|
|
// Apply NAME=VAL overrides and consume them; remaining args are COMMAND ARGS
|
|
// (which we do not exec).
|
|
for (const operand of parsed.operands) {
|
|
const eq = operand.indexOf('=');
|
|
if (eq > 0) {
|
|
const name = operand.slice(0, eq);
|
|
const value = operand.slice(eq + 1);
|
|
env = { ...env, [name]: value };
|
|
} else {
|
|
// Reaching a non-assignment means COMMAND — we do not exec; just stop
|
|
// consuming assignments and print the accumulated env.
|
|
break;
|
|
}
|
|
}
|
|
|
|
const sep = nullTerm ? '\0' : '\n';
|
|
const lines = Object.keys(env)
|
|
.sort()
|
|
.map((k) => `${k}=${env[k]}`);
|
|
return createSuccessResult(lines.join(sep) + (lines.length > 0 ? sep : ''));
|
|
}
|
|
}
|
|
|
|
function defaultEnv(cwd: string): Record<string, string> {
|
|
return {
|
|
HOME: '/',
|
|
PATH: '/usr/local/bin:/usr/bin:/bin',
|
|
SHELL: '/bin/sh',
|
|
USER: 'user',
|
|
PWD: cwd,
|
|
TERM: 'xterm-256color',
|
|
LANG: 'en_US.UTF-8',
|
|
};
|
|
}
|