fix: stdbuf cannot exec shell functions — buffer inside decode_stream [#355] #1

Merged
reachableceo merged 1 commits from fix/stdbuf-function-pipeline into main 2026-09-03 02:54:59 +00:00
Showing only changes of commit 1613dd5332 - Show all commits
+5 -2
View File
@@ -61,7 +61,10 @@ usage() {
# behavior); Enter on an empty buffer emits nothing (defensive fix). # behavior); Enter on an empty buffer emits nothing (defensive fix).
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
decode_stream() { # decode_stream <width> — od -t u1 records on stdin -> IDs decode_stream() { # decode_stream <width> — od -t u1 records on stdin -> IDs
awk -v w="$1" ' # stdbuf exec's binaries only — it can NEVER wrap this function, so
# line-buffering happens here inside (piped awk block-buffers otherwise).
# shellcheck disable=SC2016 # awk program is single-quoted by design; '"$SELF"' re-opens quotes deliberately
stdbuf -oL awk -v w="$1" '
{ {
if (NF != w) { next } if (NF != w) { next }
ti = w - 7; ci = w - 5; vi = w - 3 ti = w - 7; ci = w - 5; vi = w - 3
@@ -143,7 +146,7 @@ stream_device() {
log "listening on $dev (record width $WIDTH)" log "listening on $dev (record width $WIDTH)"
# stdbuf: od/awk block-buffer when piped; scans must not lag behind # stdbuf: od/awk block-buffer when piped; scans must not lag behind
stdbuf -oL od -v -A n -t u1 -w"$WIDTH" "$dev" 2>/dev/null \ stdbuf -oL od -v -A n -t u1 -w"$WIDTH" "$dev" 2>/dev/null \
| stdbuf -oL decode_stream "$WIDTH" \ | decode_stream "$WIDTH" \
| while read -r bid; do | while read -r bid; do
process_scan "$bid" process_scan "$bid"
done done