~kris/dots

srice

512b7381d0155d6247fad64b640b0704496a8035 — Kris Yotam 2 months ago 9b45724
mksh: port bashisms to mksh-safe (process subs, DEBUG-trap history hook, direnv, cd- alias)
3 files changed, 30 insertions(+), 11 deletions(-)

M .config/.mksh/core.sh
M .config/.mksh/cyber.sh
M .mkshrc
M .config/.mksh/core.sh => .config/.mksh/core.sh +7 -2
@@ 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

M .config/.mksh/cyber.sh => .config/.mksh/cyber.sh +6 -3
@@ 870,11 870,14 @@ unpack() {
diffbin() {
    bin1="$1"; bin2="$2"; format="${3:-hex}"
    [ -z "$bin2" ] && { echo "Usage: diffbin <bin1> <bin2> [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() {

M .mkshrc => .mkshrc +17 -6
@@ 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