Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ormaaj/ea5333f330e5fa653005ed45c72e93b1 to your computer and use it in GitHub Desktop.
Save ormaaj/ea5333f330e5fa653005ed45c72e93b1 to your computer and use it in GitHub Desktop.
bash builtin loader with lazy loading
$ { function f { lsfd -np "$BASHPID" --summary=only -C ' :ASSOC == "mem"'; }; typeset -fx f; f; bash -ic 'f; type -t print; print -r :\); type -t print; f; print -r :D'; } | tr -s '\n ' ' '; echo
247 31 function :) builtin 36 :D
function initializeBuiltins {
typeset -a \
greylist=(ln head rm stat mkdir) \
blacklist=(hello truefalse mypid necho '*.*')
typeset -a libs
typeset x y z libdir IFS
local -
set +m
libdir=$(pkgconf --var loadablesdir bash) || libdir=/lib64/bash
[[ -d $libdir ]] || return
IFS=\|
eval libs+=\( "${libdir@Q}/!(${blacklist[*]})" \)
libs+=("${libdir}/ctypes.so")
unset -v IFS
printf -v x '[%q]= ' "${greylist[@]}"
unset -v greylist
typeset -A greylist
eval greylist+=\( "${x%% }" \)
for x in "${libs[@]}"; do
[[ $(file -b --mime-type -- "$x") == 'application/x-sharedlib' && !$? -ne 0 ]] || continue
nm -CDj --without-symbol-versions -- "$x" |
while IFS= read -r y; do
[[ $y =~ ^(.+)_struct$ ]] || continue
eval -- function "${BASH_REMATCH[1]}${greylist[$x]+2}" { $'\n' \
enable -f "${x@Q}" -- "${BASH_REMATCH[1]@Q}" $'\n' \
unset -f -- "${BASH_REMATCH[1]@Q}" $'\n' \
"${BASH_REMATCH[1]@Q}" '"$@"' $'\n' \
}
${greylist[$x]+\:} continue
printf -v y %q%.s "$(type -P "$x")" "$((z=$?))"
let z && continue
eval -- function "${x}" { command -- "$y" '"$@"' \; }
done
done
return 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment