Skip to content

fix(stdio): adopt read_stdin_record for all plain-ReadFile console stdin readers (head/install/xargs/read_all_stdin) - #1458

Merged
caomengxuan666 merged 2 commits into
mainfrom
fix/stdin-record-adoption
Oct 30, 2026
Merged

caomengxuan666 merged 2 commits into
mainfrom
fix/stdin-record-adoption

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Stacked dependency: requires #1454 (fix/1450-cat-veof) to merge first — this branch sits directly on it (537a60f → f4f8d8a → this). The diff-vs-main shown below includes #1454's cat commit; the new diff is exactly 4 files, +46/−14.

Class-complete adoption of file_io::read_stdin_record() (the VEOF-simulation helper from #1454) across every command that reads console stdin via plain ReadFile, per the no-whack-a-mole directive: one helper, no per-command logic. The helper arms only on a line-input console handle and falls through to plain ReadFile everywhere else, so non-console faces are byte-identical by construction (and proven identical below).

Adopted (4 sites)

Site Face Behavior on the cooked console (^D)
head.cpp:348 output_first_records head -n/-c live stdin ^D-empty = early exit, like GNU head on a tty; pipe face unchanged
install.cpp:368 copy_stdin_to_dest install /dev/stdin dest ^D-empty ends the copy (GNU reads stdin until EOF)
xargs.cpp:1112 read_confirmation (CONIN$ arm) xargs -p /dev/tty answer ^D-empty → loud Fatal, the GNU tty-EOF contract (#1320)
file_io.cppm read_all_stdin shared whole-input reader (#1454 notes): envsubst, base32/base64/basenc/tac -, /dev/stdin aliases via read_all_file ^D-empty = EOF; pipe/file bytes identical to the old std::cin loop

One subtlety surfaced by the pipe battery: pipes end with ReadFile FALSE + ERROR_BROKEN_PIPE, not TRUE+0 — std::cin mapped that to clean EOF and GNU read(2) returns 0, so read_all_stdin classifies ERROR_BROKEN_PIPE/ERROR_NO_DATA/ERROR_HANDLE_EOF as EOF; anything else stays a genuine error (the std::cin.bad() analog). The other three sites already broke on FALSE (cat/head convention).

Audited and left untouched (classified in the report)

  • Pipe/fifo/child handles: head output_pipe_* (fifo operands head 行/记录模式在 FIFO 操作数上不截断:tee 写者全量打印,部分写者零输出(字节模式、stdin 面、cat 均正常) #1442), read_handle_to_string, diff3 child pipe; by-size file reads (cpio/patch/tsort/xxd/locate/look); raw key-event pagers (less/more/pager).
  • dd (class D — documented, not forced): on a real Unix tty dd does end at ^D-empty (read(2) returns 0), so adoption would be defensible — but dd's fixed-block contract (if=/skip=/count=/conv=, PeekNamedPipe probe, broken-pipe mapping) is too load-bearing for this sweep; the helper is drop-in at dd.cpp:1030 for a follow-up. Verified untouched A/B.
  • CRT-mediated stdin faces (follow-up family, not plain ReadFile): std::cin loops (sort/uniq/col/cksum/cmp/tsort/xxd) and fread(stdin) (md5sum*/sha*sum/tr/top) — swapping those to raw ReadFile is not byte-neutral on pipes (CRT text-mode CRLF translation) and needs its own design.

Evidence (matched Release builds: base f4f8d8a vs this branch)

GNU anchor: termios VEOF line discipline (canonical tty), as established in #1454 / f4f8d8a.

Fixes the head/install/read_all_stdin/xargs follow-ups named in the #1454 known-limits note (except dd, documented above).

caomengxuan666 and others added 2 commits October 31, 2026 01:22
…ugh read_stdin_record (winux#1450 family)

Class-complete adoption of file_io::read_stdin_record() (introduced by
#1454) for every command that reads console stdin via plain ReadFile:

- head: output_first_records stdin fast path (head -n/-c live stdin); a
  zero-byte read at ^D-empty ends head early, like GNU head on a tty
  (VEOF). The helper is not armed on the FILE_TYPE_PIPE face, so the
  #1289/R2 short-read pipe contract is byte-identical.
- install: copy_stdin_to_dest (install /dev/stdin dest); ^D-empty ends the
  copy, matching GNU reading stdin until EOF on a tty (uutils#12407).
- xargs: read_confirmation CONIN$ arm (the Windows /dev/tty answer,
  #1320); ^D at line start ends the answer read and lands on the loud
  Fatal path, matching tty EOF under GNU xargs -p.
- file_io: read_all_stdin switches from the std::cin loop to a
  read_stdin_record loop (named by #1454 notes as needing adoption), so
  envsubst and the base32/base64/basenc/tac "-" faces plus /dev/stdin
  aliases observe VEOF on a cooked console. Pipes end with ReadFile FALSE
  + ERROR_BROKEN_PIPE (not TRUE+0); std::cin mapped that to clean EOF and
  GNU read(2) returns 0, so the loop classifies ERROR_BROKEN_PIPE /
  ERROR_NO_DATA / ERROR_HANDLE_EOF as EOF and keeps everything else a
  genuine error (the std::cin.bad() analog).

Left untouched (audited, classified in the sweep report):
- pipe/fifo handles (head output_pipe_*, read_handle_to_string), by-size
  file reads (cpio/patch/tsort/xxd/locate/look), child pipes (diff3),
  raw key-event pagers (less/more/pager) - different mechanism by design.
- dd (class D, documented): on a Unix tty dd does end at ^D-empty, so
  adoption would be defensible, but dd's fixed-block contract (if=/skip=/
  count=/conv=, PeekNamedPipe probe, broken-pipe mapping) is judged too
  load-bearing for this mechanical sweep; recorded as follow-up material.
- CRT-mediated faces (std::cin/fread stdin: sort/uniq/col/cksum/cmp/tsort/
  xxd/md5sum*/sha*sum/tr/top) - the helper is handle-shaped; recorded as
  the follow-up family.

Evidence (matched Release builds, base f4f8d8a vs this branch):
- ConPTY battery 9/9 adopt-PASS; base red exactly on the six adopted
  faces (head/envsubst/base64/tac/tac /dev/stdin/install hang swallowing
  the sentinel line; xargs -p takes ^D+zz as a "No" answer) and green on
  both guard cells (cat #1454, xargs answer "y").
- Pipe/file battery 12/12 byte-identical (incl. dd guard, xargs CRT arm,
  uniq CRT face); battery run 1 caught the ERROR_BROKEN_PIPE regression
  before it shipped - fixed and re-proven on the final binary.
- ctest 3462 tests: base 99% / 8 pre-existing failures (NUL #1333, #1288,
  cygdrive form); adopt failure set == base set, no new failures.

GNU anchor: termios VEOF line discipline (canonical tty), as established
in #1454 / f4f8d8a.

Stacked on #1454 (fix/1450-cat-veof) - not mergeable before it.
@caomengxuan666
caomengxuan666 merged commit 5943f36 into main Oct 30, 2026
7 of 9 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/stdin-record-adoption branch November 2, 2026 01:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant