#!/usr/bin/env bash # cygwin-safe.sh — three primitives that defend Larry-Anywhere against the # Cygwin/MobaXterm CR-taint pattern that crashed OAuth in v0.7.3. # # Pattern (full diagnosis in # Deliverables/2026-05-27-cloverleaf-larry-oauth-arithmetic-fix.md and the # v0.7.5 CR-safety sweep deliverable): # # On MobaXterm / Cygwin / Git-Bash-for-Windows, any of the following can # return a string ending in a literal carriage return (\r): # - `$(date +%s)`, `$(date ...)`, `$(cmd)` against a Cygwin-built binary # - `read` of user input (depending on tty mode) # - `cat`/`head`/`tail` against a CRLF-line-ended file # - `$(/dev/null || cat } # `_sanitize_ctl_tty` — the DATA-TOOL variant. nc-msgs/nc-parse/hl7-* emit data # that downstream tooling consumes byte-for-byte (e.g. `nc-msgs ... > input.msgs` # feeding route_test, or `| awkcut`). The 0x1c HL7 framing and other control # bytes are LOAD-BEARING on a pipe/redirect — stripping them would silently # corrupt the data. So we only sanitize when stdout is an interactive TERMINAL # (protect the human's tty); on a pipe/file we pass through RAW, byte-identical. # # `[ -t 1 ]` is POSIX (true only when fd 1 is a terminal). Note this is a # FILTER (stdin→stdout); the gate decision is made once at call time. Callers # pipe their whole output region through it and propagate ${PIPESTATUS[0]} so # the upstream producer's exit code is preserved across the pipe. _sanitize_ctl_tty() { if [ -t 1 ]; then _sanitize_ctl else cat fi }