1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-06-26 01:07:11 -07:00
rc/sh/has

38 lines
1.2 KiB
Plaintext
Raw Normal View History

2021-07-29 00:37:35 -07:00
#!/usr/bin/env sh
# YES_ZSH
# YES_BASH
# YES_DASH
2021-09-23 06:48:05 -07:00
# YES_ASH
2021-07-29 00:37:35 -07:00
2021-09-30 08:29:28 -07:00
has() ( ### @- print the result of `which` if the program is found, else simply return 1.
2021-08-01 09:45:19 -07:00
### ```
2021-09-30 08:29:28 -07:00
### export SOLVER="$(has kissat || has picosat || has cadical || has minisat)"
2021-08-01 09:45:19 -07:00
### ```
fancy=0
[ -z "$ZSH_VERSION" ] && [ -z "$BASH_VERSION" ] || fancy=1
2021-10-13 20:22:10 -07:00
if [ "${1:-/}" = "${1#*[!A-Za-z_-]}" ]; then # has a simple name?
2021-10-13 20:22:10 -07:00
# only ash and dash support aliases beginning with hyphens.
# forcefully unalias it (regardless of if it even was an alias):
if [ $fancy = 0 ] || [ "${1:-/}" = "${1#-}" ]; then
alias "$1"=
2021-10-13 20:22:10 -07:00
unalias -- "$1"
fi
2021-10-13 20:22:10 -07:00
# only bash and zsh support function names containing hyphens.
# forcefully unset it (regardless of if it even was a function):
if [ "$FANCY" = 1 ]; then
eval -- "$1() (:)"
unset -f -- "$1"
elif [ "${1:-/}" = "${1#*-}" ]; then
eval "$1() (:)"
2021-10-13 20:22:10 -07:00
unset -f -- "$1"
fi
2021-07-29 00:37:35 -07:00
fi
2021-10-13 20:22:10 -07:00
2021-09-30 08:29:28 -07:00
which -- "$1" >/dev/null 2>&1 && which -- "$1"
) # this uses parentheses instead of braces so that the function is always run
# in a subshell. otherwise, the user's aliases and functions may be overwritten.
2021-07-29 00:37:35 -07:00
[ -n "${preload+-}" ] || has "$@"