2021-03-02 17:06:05 +00:00
|
|
|
set +o posix
|
|
|
|
|
|
|
|
quiet_safe_cd() {
|
2021-08-19 13:32:15 +01:00
|
|
|
cd "$1" &>/dev/null || { echo "Error: failed to cd to $1" >&2; exit 1; }
|
2021-03-02 17:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
absdir() {
|
|
|
|
quiet_safe_cd "${1%/*}/" && pwd -P
|
|
|
|
}
|
|
|
|
|
|
|
|
dirbasepath() {
|
|
|
|
local dir="$1"
|
|
|
|
local base="${2##*/}"
|
2021-04-18 16:44:53 +09:00
|
|
|
echo "${dir}/${base}"
|
2021-03-02 17:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
realpath() {
|
|
|
|
local path="$1"
|
|
|
|
local dir
|
|
|
|
local dest
|
|
|
|
|
2021-04-18 16:44:53 +09:00
|
|
|
dir="$(absdir "${path}")"
|
|
|
|
path="$(dirbasepath "${dir}" "${path}")"
|
2021-03-02 17:06:05 +00:00
|
|
|
|
2021-04-18 16:44:53 +09:00
|
|
|
while [[ -L "${path}" ]]
|
2021-03-02 17:06:05 +00:00
|
|
|
do
|
2021-04-18 16:44:53 +09:00
|
|
|
dest="$(readlink "${path}")"
|
|
|
|
if [[ "${dest}" = "/"* ]]
|
2021-03-02 17:06:05 +00:00
|
|
|
then
|
2021-04-18 16:44:53 +09:00
|
|
|
path="${dest}"
|
2021-03-02 17:06:05 +00:00
|
|
|
else
|
2021-04-18 16:44:53 +09:00
|
|
|
path="${dir}/${dest}"
|
2021-03-02 17:06:05 +00:00
|
|
|
fi
|
2021-04-18 16:44:53 +09:00
|
|
|
dir="$(absdir "${path}")"
|
|
|
|
path="$(dirbasepath "${dir}" "${path}")"
|
2021-03-02 17:06:05 +00:00
|
|
|
done
|
|
|
|
|
2021-04-18 16:44:53 +09:00
|
|
|
echo "${path}"
|
2021-03-02 17:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
executable() {
|
|
|
|
local file="$1"
|
2021-04-18 16:44:53 +09:00
|
|
|
[[ -f "${file}" && -x "${file}" ]]
|
2021-03-02 17:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lowercase() {
|
|
|
|
echo "$1" | tr "[:upper:]" "[:lower:]"
|
|
|
|
}
|
|
|
|
|
|
|
|
safe_exec() {
|
|
|
|
local arg0="$1"
|
2021-04-18 16:44:53 +09:00
|
|
|
if ! executable "${arg0}"
|
2021-03-02 17:06:05 +00:00
|
|
|
then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
# prevent fork-bombs
|
2021-04-18 16:44:53 +09:00
|
|
|
if [[ "$(lowercase "${arg0}")" = "${SHIM_FILE}" || "$(realpath "${arg0}")" = "${SHIM_REAL}" ]]
|
2021-03-02 17:06:05 +00:00
|
|
|
then
|
|
|
|
return
|
|
|
|
fi
|
2021-04-18 16:44:53 +09:00
|
|
|
if [[ "${HOMEBREW}" = "print-path" ]]
|
2021-03-02 17:06:05 +00:00
|
|
|
then
|
2021-03-30 01:15:05 +02:00
|
|
|
local dir
|
|
|
|
dir="$(quiet_safe_cd "${arg0%/*}/" && pwd)"
|
|
|
|
local path
|
2021-04-18 16:44:53 +09:00
|
|
|
path="$(dirbasepath "${dir}" "${arg0}")"
|
|
|
|
echo "${path}"
|
2021-03-02 17:06:05 +00:00
|
|
|
exit
|
|
|
|
fi
|
|
|
|
exec "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
try_exec_non_system() {
|
|
|
|
local file="$1"
|
|
|
|
shift
|
|
|
|
|
|
|
|
IFS=$'\n'
|
2021-04-18 16:44:53 +09:00
|
|
|
for path in $(type -aP "${file}")
|
2021-03-02 17:06:05 +00:00
|
|
|
do
|
2021-04-18 16:44:53 +09:00
|
|
|
if [[ "${path}" != "/usr/bin/${file}" ]]
|
2021-03-02 17:06:05 +00:00
|
|
|
then
|
2021-04-18 16:44:53 +09:00
|
|
|
safe_exec "${path}" "$@"
|
2021-03-02 17:06:05 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
unset IFS
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SHIM_FILE="${0##*/}"
|
|
|
|
SHIM_REAL="$(realpath "$0")"
|
|
|
|
|
|
|
|
if [[ "$1" = --homebrew=* ]]
|
|
|
|
then
|
|
|
|
HOMEBREW="${1:11}"
|
|
|
|
shift
|
|
|
|
fi
|