From 512b7381d0155d6247fad64b640b0704496a8035 Mon Sep 17 00:00:00 2001 From: Kris Yotam <75515498+krisyotam@users.noreply.github.com> Date: Wed, 1 Jul 2026 23:58:43 -0500 Subject: [PATCH] mksh: port bashisms to mksh-safe (process subs, DEBUG-trap history hook, direnv, cd- alias) --- .config/.mksh/core.sh | 9 +++++++-- .config/.mksh/cyber.sh | 9 ++++++--- .mkshrc | 23 +++++++++++++++++------ 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/.config/.mksh/core.sh b/.config/.mksh/core.sh index 7f111879a2fbaea12dd2638b5057944f1074253b..161b2aa525775215126a3c07a3b684e8027df364 100644 --- a/.config/.mksh/core.sh +++ b/.config/.mksh/core.sh @@ -44,7 +44,8 @@ alias ..='cd ..' alias ...='cd ../..' alias ....='cd ../../..' alias .....='cd ../../../..' -alias -- -='cd -' +# `alias -- -` is bash-only; mksh has `cd -` builtin natively +[ -n "$BASH_VERSION" ] && alias -- -='cd -' alias cdg='cd $(git rev-parse --show-toplevel 2>/dev/null || echo .)' # ============================================================================ @@ -1033,7 +1034,11 @@ fcount() { # diff two directories dirdiff() { - diff <(cd "$1" && find . -type f | sort) <(cd "$2" && find . -type f | sort) + typeset a=$(mktemp) b=$(mktemp) + (cd "$1" && find . -type f | sort) > "$a" + (cd "$2" && find . -type f | sort) > "$b" + diff "$a" "$b" + rm -f "$a" "$b" } # backup a file diff --git a/.config/.mksh/cyber.sh b/.config/.mksh/cyber.sh index 17aa0749d38f61654cd9c25e4201002f6ba1cfa6..bdc5dab8ffd65cf6f24551a7ecba5743a05f4a03 100644 --- a/.config/.mksh/cyber.sh +++ b/.config/.mksh/cyber.sh @@ -870,11 +870,14 @@ unpack() { diffbin() { bin1="$1"; bin2="$2"; format="${3:-hex}" [ -z "$bin2" ] && { echo "Usage: diffbin [hex|strings|symbols]"; return 1; } + typeset a=$(mktemp) b=$(mktemp) case "$format" in - hex) diff <(hexdump -C "$bin1") <(hexdump -C "$bin2") | head -50 ;; - strings) diff <(strings "$bin1" | sort) <(strings "$bin2" | sort) | head -50 ;; - symbols) diff <(nm "$bin1" | sort) <(nm "$bin2" | sort) | head -50 ;; + hex) hexdump -C "$bin1" > "$a"; hexdump -C "$bin2" > "$b" ;; + strings) strings "$bin1" | sort > "$a"; strings "$bin2" | sort > "$b" ;; + symbols) nm "$bin1" | sort > "$a"; nm "$bin2" | sort > "$b" ;; esac + diff "$a" "$b" | head -50 + rm -f "$a" "$b" } xrefs() { diff --git a/.mkshrc b/.mkshrc index 6d0521e9f3f2ba8e7d82975a547625a219f2baa9..216a048a6475c8176d71b58c20360aa10a07d691 100644 --- a/.mkshrc +++ b/.mkshrc @@ -19,8 +19,8 @@ HISTSIZE=10000 # Zoxide (smarter cd) command -v zoxide >/dev/null 2>&1 && eval "$(zoxide init posix --hook prompt)" -# Direnv -command -v direnv >/dev/null 2>&1 && eval "$(direnv hook ksh)" +# Direnv (bash hook works under mksh; there is no dedicated ksh hook) +command -v direnv >/dev/null 2>&1 && eval "$(direnv hook bash)" # History logging (encrypted with SQLCipher) _kh_db="$HOME/.local/share/history/history.db" @@ -38,7 +38,18 @@ _kh_log() { (sqlcipher "$_kh_db" "PRAGMA key = '$key'; INSERT INTO terminal_history (command, exit_code, pwd, shell) VALUES ('$escaped_cmd', $exit_code, '$escaped_pwd', 'mksh');" &) >/dev/null 2>&1 } -# PS4 trap to capture commands for history logging -_kh_last_cmd="" -trap '_kh_last_cmd=$(fc -ln -1 2>/dev/null | sed "s/^[[:space:]]*//")' DEBUG -PROMPT_COMMAND='_kh_log "$_kh_last_cmd" "$?"' +# Command history logging hook. +if [ -n "$BASH_VERSION" ]; then + # bash: DEBUG trap captures the command, PROMPT_COMMAND logs it + _kh_last_cmd="" + trap '_kh_last_cmd=$(fc -ln -1 2>/dev/null | sed "s/^[[:space:]]*//")' DEBUG + PROMPT_COMMAND='_kh_log "$_kh_last_cmd" "$?"' +else + # mksh: no DEBUG trap / PROMPT_COMMAND. Log from PS1 each prompt. + # The leading $(...) captures $? first, so the exit code is accurate, + # and _kh_prompt emits nothing so the visible prompt is unchanged. + _kh_prompt() { + _kh_log "$(fc -ln -1 2>/dev/null | sed 's/^[[:space:]]*//')" "$1" + } + PS1='$(_kh_prompt "$?")'"$PS1" +fi