From f325b6d15179bb02147039c89e9fcfac6d8e067b Mon Sep 17 00:00:00 2001 From: Agando Date: Tue, 27 Oct 2026 16:23:21 +0100 Subject: feat(newos4): the script runner runs scripts — control flow, variables, globs, redirects, stdin MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit MAINT-021 and MAINT-017, executed from .hermes/plans/2026-10-27_145500-newos4-script-runner.md. - shell.js: new block parser (_parseBlocks) and executor (_runInstructions/_runSimple/_runCommand/ _dispatchOne). `for`/`while`/`if` bodies now run, including one-line `if [ -f x ]; then y; fi`. Variables expand through _expandVars ($VAR, ${VAR}, $?, single quotes literal), globs expand through _expandGlobs (unquoted args only, per-command), and `>`/`>>` capture the command's output into the VFS instead of being passed to the command as text. - shell.js: a script never answers an unknown command by calling the AI any more. It prints `script::: : command not found` and sets $? — the AI fallback stays for typing. - shell.js: unclosed blocks and bad `for` headers surface as `Script failed: …` (the parse used to sit outside the try, so a bad script crashed the runner). - shell.js: interactive path gets the same treatment — prompt globbing and `echo hi > /f` now work. Cause of the asymmetry: the two code paths had separate dispatch. - shell.js: `_isPipeAware` no longer injects the __pipebuf__ file into wc (it reads _pipeInput), and the module registers itself with a versioned URL and calls its own init() — registerModule() does not call init(), so the executeCommand wrapper was never installed from the boot bundle. - module-fs.js: wc/head/tail/grep read piped stdin when no file operand is given; `cat` accepts several operands (`cat *.txt`); `ls ` names the file instead of printing "(empty)". - main.html: shell.js removed from the extension list — it is already inside critical-ext-bundle.js, and loading it twice produced two ShellModule instances that could disagree about their version. Verification: - test-shell-script.js 27 asserts (parser, variables, globs, executor, redirect, command-not-found, one-line if, parse error). RED on HEAD: `TypeError: shell._parseBlocks is not a function`. - test-shell-stdin.js 16 asserts (added by a subagent): wc -l/-c, head -n, tail -n over stdin, and the file-argument path unchanged. - Suite 18/18 files; maintain --fix "node tests 18/18 pass". - Live on v4.6.115, through the real executeCommand: `ls /n*.txt` -> /n1.txt · `cat /n*.txt` -> one|two · `echo hi > /r.txt` then `cat /r.txt` -> hi · `cat /probe.txt | wc -l` -> 3 (bare, no __pipebuf__) · `script /run.sh` -> item 1 / item 2 / item 3 / exists / command not found, exit 1, no AI routing. --- modules/core/core-bundle.js | 62 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 11 deletions(-) (limited to 'modules/core/core-bundle.js') diff --git a/modules/core/core-bundle.js b/modules/core/core-bundle.js index 8d6cbda..6f00658 100644 --- a/modules/core/core-bundle.js +++ b/modules/core/core-bundle.js @@ -6575,7 +6575,12 @@ class FileSystemModule { const showLong = flags.some(f => f.includes('l')); const showHidden = flags.some(f => f.includes('a')); const files = this.terminal.fs.ls(target); - if (!files || files.length === 0) { this.terminal.print('(empty)', 'info'); return; } + if (!files || files.length === 0) { + // `ls somefile` used to print "(empty)": a file operand should name the file, + // which is also what makes `ls *.txt` (glob-expanded to several paths) readable. + if (target && this.terminal.fs.readFile(target) !== null) { this.terminal.print(target, 'info'); return; } + this.terminal.print('(empty)', 'info'); return; + } const filtered = showHidden ? files : files.filter(f => !f.name.startsWith('.')); if (filtered.length === 0) { this.terminal.print('(no visible files, use ls -a)', 'info'); return; } const maxName = Math.max(...filtered.map(f => f.name.length), 8); @@ -6615,12 +6620,19 @@ class FileSystemModule { docs.forEach(f => this.terminal.print(` ${f.name} ${f.size || 0} B`, 'info')); }; this.commands['cat'] = (args) => { - if(!args[0]) return this.terminal.print('Usage: cat ', 'warning'); - const content = this.terminal.fs.readFile(args[0]); - if(content !== null && content !== undefined) { - if (content.length === 0) this.terminal.print('(empty file)', 'info'); - else this.terminal.print(content); - } else this.terminal.print('cat: ' + args[0] + ': No such file', 'error'); + if(!args[0]) return this.terminal.print('Usage: cat [...]', 'warning'); + // Multiple operands: `cat *.txt` is useless if only the first path is read. + const files = args.filter(a => !String(a).startsWith('-')); + let printed = 0; + files.forEach((f) => { + const content = this.terminal.fs.readFile(f); + if (content !== null && content !== undefined) { + if (content.length === 0) this.terminal.print('(empty file)', 'info'); + else this.terminal.print(content); + printed++; + } else this.terminal.print('cat: ' + f + ': No such file', 'error'); + }); + if (!printed && !files.length) this.terminal.print('Usage: cat [...]', 'warning'); }; this.commands['echo'] = (args) => { if (!args || args.length === 0) { this.terminal.print(''); return; } @@ -6848,7 +6860,12 @@ class FileSystemModule { else if (headFiles.length && /^\d+$/.test(a)) n = parseInt(a, 10); // head f 40 else headFiles.push(a); } - if (!headFiles.length) return this.terminal.print('Usage: head [-n] [...] [lines]', 'warning'); + // Piped input: `cat x | head -n 2` — no file operand means stdin, not usage. + const piped = this.terminal ? this.terminal._pipeInput : null; + if (!headFiles.length) { + if (piped === null || piped === undefined) return this.terminal.print('Usage: head [-n] [...] [lines]', 'warning'); + return this.terminal.print(String(piped).split('\n').slice(0, Math.max(1, n)).join('\n')); + } const headMulti = headFiles.length > 1; headFiles.forEach(file => { const c = this.terminal.fs.readFile(file); @@ -6877,7 +6894,15 @@ class FileSystemModule { // v4.6.66: every path argument (packet §1). A numeric second positional is still // the legacy `tail `, not a filename. const tailFiles = positional.filter((p, i) => !(i > 0 && /^\d+$/.test(p))); - if (!tailFiles.length) return this.terminal.print('Usage: tail [-f] [-n N] [...] — last N lines; -f follows changes (Ctrl+C or Esc to stop)', 'warning'); + // Piped input: `cat x | tail -n 1` — no file operand means stdin, not usage. + const piped = this.terminal ? this.terminal._pipeInput : null; + if (!tailFiles.length) { + if (piped === null || piped === undefined) return this.terminal.print('Usage: tail [-f] [-n N] [...] — last N lines; -f follows changes (Ctrl+C or Esc to stop)', 'warning'); + const _in = String(piped).split('\n'); + // same trailing-newline rule as the file path below + if (_in.length > 1 && _in[_in.length - 1] === '') _in.pop(); + return this.terminal.print(_in.slice(-n).join('\n')); + } if (positional[1] && /^\d+$/.test(positional[1])) n = parseInt(positional[1], 10); // legacy back-compat const tailMulti = tailFiles.length > 1; let shownFiles = tailFiles; @@ -7047,10 +7072,25 @@ class FileSystemModule { else if (/^-[a-zA-Z]+$/.test(a)) { /* ignore other flags */ } else files.push(a); }); - if (!files.length) return this.terminal.print('Usage: wc [-l] [-w] [-c] [...]', 'warning'); + // Piped input: `cat x | wc -l`. The pipe wrapper sets terminal._pipeInput for every + // stage (modules/ext/shell.js), so "no file operand" means "count stdin", not usage. + const piped = this.terminal ? this.terminal._pipeInput : null; + const stdinMode = !files.length && piped !== null && piped !== undefined; + if (!files.length && !stdinMode) return this.terminal.print('Usage: wc [-l] [-w] [-c] [...]', 'warning'); const anyFlag = want.l || want.w || want.c; const rows = []; let total = { l: 0, w: 0, c: 0 }; + if (stdinMode) { + const content = String(piped); + const r = { + file: null, // stdin has no name — GNU wc prints the bare counts + l: content.split('\n').length, + w: content.split(/\s+/).filter(w => w).length, + c: content.length + }; + total.l += r.l; total.w += r.w; total.c += r.c; + rows.push(r); + } files.forEach(file => { const content = this.terminal.fs.readFile(file); if (content === null || content === undefined) { @@ -7073,7 +7113,7 @@ class FileSystemModule { if (want.l || !anyFlag) out.push(r.l); if (want.w || !anyFlag) out.push(r.w); if (want.c || !anyFlag) out.push(r.c); - if (multi || (anyFlag && !(want.l && want.w && want.c))) out.push(name); + if ((multi || (anyFlag && !(want.l && want.w && want.c))) && name !== null) out.push(name); return out.join(' '); }; rows.forEach(r => this.terminal.print(fmt(r, r.file))); -- cgit v1.2.3