Browse Source

[Fix #125] Wrap GNU utilities in functions

There are three methods for calling prefixed GNU utilities interactively
non-prefixed aliasing, hashing, and wrapper functions. Two of these
methods are unreliable and are discussed bellow for reference only.

The aliasing method is unreliable since aliases are at risk of being
overridden resulting in non-GNU utilities being called with invalid
switches.

The hashing method is unreliable because hashed commands are lost
whenever hash -r or rehash -f are called. Thus, said built-ins have to
be wrapped to rehash GNU utilities. Unfortunately, altering $path will
cause Zsh to call the built-in rehash instead of the wrapped one
resulting in the hashed commands being lost.

The wrapper function method is currently the most reliable and is the
one used.
master
Sorin Ionescu 13 years ago
parent
commit
878d0fbab3
  1. 55
      modules/gnu-utils/init.zsh

55
modules/gnu-utils/init.zsh

@ -6,22 +6,14 @@
# #
# Get the prefix or use the default. # Get the prefix or use the default.
zstyle -s ':omz:module:gnu-utils' prefix '_gnu_utils_prefix' || zstyle -s ':omz:module:gnu-utils' prefix '_gnu_utils_p' || _gnu_utils_p='g'
_gnu_utils_prefix='g'
# Check for the presence of GNU Core Utilities. # Check for the presence of GNU Core Utilities.
if (( ! $+commands[${_gnu_utils_prefix}dircolors] )); then if (( ! ${+commands[${_gnu_utils_p}dircolors]} )); then
return 1 return 1
fi fi
function _gnu-utils-hash-commands { _gnu_utils_cmds=(
emulate -L zsh
local cmds
local cmd
local pcmd
cmds=(
# Coreutils # Coreutils
'[' 'base64' 'basename' 'cat' 'chcon' 'chgrp' 'chmod' 'chown' '[' 'base64' 'basename' 'cat' 'chcon' 'chgrp' 'chmod' 'chown'
'chroot' 'cksum' 'comm' 'cp' 'csplit' 'cut' 'date' 'dd' 'df' 'chroot' 'cksum' 'comm' 'cp' 'csplit' 'cut' 'date' 'dd' 'df'
@ -51,34 +43,19 @@ function _gnu-utils-hash-commands {
# Miscellaneous # Miscellaneous
'getopt' 'grep' 'indent' 'sed' 'tar' 'time' 'units' 'which' 'getopt' 'grep' 'indent' 'sed' 'tar' 'time' 'units' 'which'
) )
for cmd in "$cmds[@]"; do # Wrap GNU utilities in functions.
# for _gnu_utils_cmd in "${_gnu_utils_cmds[@]}"; do
# This method allows for builtin commands to be primary but it's _gnu_utils_pcmd="${_gnu_utils_p}${_gnu_utils_cmd}"
# lost if hash -r or rehash -f is executed. Thus, those two if (( ${+commands[${_gnu_utils_pcmd}]} )); then
# functions have to be wrapped. eval "
# function ${_gnu_utils_cmd} {
pcmd="${_gnu_utils_prefix}${cmd}" '${commands[${_gnu_utils_pcmd}]}' \"\$@\"
if (( $+commands[$pcmd] )); then }
builtin hash "$cmd"="$commands[$pcmd]" "
fi
done
return 0
}
_gnu-utils-hash-commands
function hash {
if (( $+argv[(er)-r] )) || (( $+argv[(er)-f] )); then
builtin hash "$@"
_gnu-utils-hash-commands
else
builtin hash "$@"
fi fi
} done
function rehash { unset _gnu_utils_{p,cmds,cmd,pcmd}
hash -r "$@"
}

Loading…
Cancel
Save