Fix real hardware/busybox breakage: dead compgen dependency, unreliable sourced-path detection, and fork-heavy pipelines in fx sources; verified against busybox ash+awk directly

This commit is contained in:
2026-08-17 23:19:07 -04:00
parent b4d619de8a
commit df3c328424
5 changed files with 51 additions and 14 deletions
+15 -1
View File
@@ -3,9 +3,23 @@
# typed and moved on from before (thefuck's history.py idea). Reads
# HISTFILE directly since a separate process doesn't inherit the parent
# shell's in-memory history.
#
# Single awk pass over the tail of the file: on weak/emulated hardware
# every avoided fork+exec matters (measured: an unoptimized PATH walk in
# 40-pathfuzzy.sh took >30s on this box before being fixed; pipeline
# depth has the same tax, just smaller per-hop) -- a five-process
# tail|grep|grep|awk|tail chain does the same job as one awk script
# reading the file once.
cmd="$1"
first_word=$(printf '%s' "$cmd" | awk '{print $1}')
[ -n "$first_word" ] || exit 0
hf="${HISTFILE:-$HOME/.bash_history}"
[ -r "$hf" ] || exit 0
tail -n 300 "$hf" 2>/dev/null | grep -F -- "$first_word " | grep -vF -- "$cmd" | awk '!seen[$0]++' | tail -5
tail -n 300 "$hf" 2>/dev/null | awk -v first="$first_word" -v full="$cmd" '
index($0, first " ") == 1 && $0 != full && !seen[$0]++ { buf[++n] = $0 }
END {
start = (n > 5) ? n - 4 : 1
for (i = start; i <= n; i++) print buf[i]
}
'
+6 -1
View File
@@ -12,6 +12,11 @@
# correctly across branches/arguments in practice. One taught fix this
# way covers every future self-diagnosing failure from that program, not
# just the one instance that happened to trigger the teaching prompt.
#
# The emitted function runs on every future call to that program for
# the rest of the session, so its own internal extraction is one awk
# pass, not a grep|sed|tail chain -- three fewer forks on every failure
# it ever handles, not just this first one.
cmd="$1"
prog=$(printf '%s' "$cmd" | awk '{print $1}')
[ -n "$prog" ] || exit 0
@@ -19,5 +24,5 @@ prog=$(printf '%s' "$cmd" | awk '{print $1}')
printf '%s\n' "$FX_OUTPUT" | grep -qE "^[[:space:]]*${prog}[[:space:]]" || exit 0
cat <<FIX
${prog}(){ out=\$(command ${prog} "\$@" 2>&1); ec=\$?; if [ \$ec -ne 0 ]; then sug=\$(printf '%s\\n' "\$out" | grep -E "^[[:space:]]*${prog}[[:space:]]" | sed 's/^[[:space:]]*//' | tail -1); if [ -n "\$sug" ]; then eval "command \$sug"; return \$?; fi; fi; printf '%s\\n' "\$out"; return \$ec; }
${prog}(){ out=\$(command ${prog} "\$@" 2>&1); ec=\$?; if [ \$ec -ne 0 ]; then sug=\$(printf '%s\\n' "\$out" | awk '/^[ \\t]*${prog}[ \\t]/{sub(/^[ \\t]*/,""); s=\$0} END{print s}'); if [ -n "\$sug" ]; then eval "command \$sug"; return \$?; fi; fi; printf '%s\\n' "\$out"; return \$ec; }
FIX
+11 -3
View File
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/sh
# PATH-fuzzy source: if the first word isn't a known command, alias, or
# function, offer a function wrapping the closest-spelled real command
# as a candidate -- as a fix you approve through the normal f() flow,
@@ -7,6 +7,12 @@
# otherwise), so an alias-based fix silently does nothing when f/fx run
# inside a script.
#
# Pure POSIX: walks $PATH directly instead of `compgen -c`, which is a
# bash-only builtin and does not exist under busybox ash -- this source
# used to be dead weight on exactly the minimal/embedded shells this
# project targets. Tested against busybox ash + busybox awk directly,
# not just bash + gawk.
#
# Uses Damerau-Levenshtein-lite (edit distance + adjacent transposition)
# so the canonical "gti" -> "git" typo scores 1, not 2 -- plain Hamming
# or substitution-only distance ties it with unrelated 3-letter commands
@@ -15,9 +21,11 @@ cmd="$1"
first_word=$(printf '%s' "$cmd" | awk '{print $1}')
[ -n "$first_word" ] || exit 0
command -v "$first_word" >/dev/null 2>&1 && exit 0
type "$first_word" >/dev/null 2>&1 && exit 0
compgen -c 2>/dev/null | sort -u | awk -v target="$first_word" '
IFS=:
for d in $PATH; do
ls "$d" 2>/dev/null
done | sort -u | awk -v target="$first_word" '
function min3(a, b, c) { return (a < b ? (a < c ? a : c) : (b < c ? b : c)) }
function distance(a, b, n, m, i, j, ca, cb, cost, tmp) {
n = length(a); m = length(b)