From 40c4f5ccd9a54b60cad3a3f29c6c9d32d3fbdac5 Mon Sep 17 00:00:00 2001 From: Agando Date: Tue, 27 Oct 2026 16:26:30 +0100 Subject: fix(newos4): piped grep searched the working directory instead of its input MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The stdin work in 5cf5010 fixed wc/head/tail but left grep alone, and grep's no-operand branch sweeps the cwd. Measured on v4.6.115: `cat /gdir/in.txt | grep zzz` answered `other.txt:1: zzz in a cwd file` — a file that was never piped, and `cat in.txt | grep bbb` reported the match twice (`in.txt` from the cwd sweep plus `__pipebuf__` from the injected file). - module-fs.js: when terminal._pipeInput is set and no file operand was given, grep searches the staged text and prints `: ` (stdin has no filename, as in GNU). The cwd sweep stays for a bare interactive `grep x`. - shell.js: _isPipeAware now covers only sort/cut/uniq/expand. grep/head/tail/wc read _pipeInput themselves, so injecting the __pipebuf__ file produced duplicate hits and named stdin results. Verification: - test-shell-stdin.js 20 asserts (+4 for grep: no cwd match, non-match reports none, match prints no filename, -c counts stdin). RED on HEAD: G1/G2/G3 fail with `other.txt:1: zzz in a cwd file`. - Suite 18/18 files; maintain --fix "node tests 18/18 pass". - Live v4.6.116 after `cd /gdir` (where other.txt contains zzz): `cat /gdir/in.txt | grep zzz` -> (no matches) · `| grep bbb` -> `2: bbb` · `| grep -c b` -> 1 · `| head -n 2` -> aaa,bbb · `| tail -n 1` -> ccc · `| wc -l` -> 3. Credit: the false-positive grep was reported by the stdin subagent as an aside; re-derived here against the live terminal before fixing. --- modules/core/module-fs.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'modules/core/module-fs.js') diff --git a/modules/core/module-fs.js b/modules/core/module-fs.js index 757d1da..0dea0d5 100644 --- a/modules/core/module-fs.js +++ b/modules/core/module-fs.js @@ -305,6 +305,22 @@ class FileSystemModule { else this.terminal.print(grand + ' match(es)' + (multi ? ' in ' + readFiles + ' file(s)' : ''), 'text-muted'); } } else { + // Piped input wins over the cwd sweep. `cat f | grep x` used to grep the working + // directory, so it could match a file that was never piped — measured 2026-10-27: + // `cat /gdir/in.txt | grep zzz` answered "other.txt:1: zzz in a cwd file". + const piped = this.terminal._pipeInput; + if (piped !== null && piped !== undefined) { + let pipedTotal = 0; + String(piped).split('\n').forEach((line, i) => { + if (!hits(line)) return; + pipedTotal++; + if (!opt.count && !opt.files) this.terminal.print((i + 1) + ': ' + line); + }); + if (opt.files) { if (pipedTotal) this.terminal.print('(stdin)'); } + else if (opt.count) this.terminal.print(String(pipedTotal)); + else if (pipedTotal === 0) this.terminal.print('(no matches)', 'info'); + return; + } const dir = this.terminal.fs.getCurrentDir(); if (!dir) return this.terminal.print('grep: cannot search', 'error'); let total = 0; -- cgit v1.2.3