mirror of
https://github.com/notwa/rc
synced 2025-02-05 07:43:22 -08:00
rewrite witch
to be compatible with more shells
This commit is contained in:
parent
5b29189372
commit
19a28440ac
1 changed files with 43 additions and 56 deletions
99
sh/witch
99
sh/witch
|
@ -1,67 +1,54 @@
|
||||||
#!/usr/bin/env sh
|
#!/usr/bin/env sh
|
||||||
# NO_ZSH NO_BASH YES_DASH YES_ASH
|
# YES_ZSH YES_BASH YES_DASH YES_ASH
|
||||||
# this is `which` from Debian. the only changes are superficial (formatting).
|
### @ witch
|
||||||
# this does not work as-is with zsh.
|
### this is a personal rewrite of `which` from Debian.
|
||||||
|
### the original version didn't run on certain shells,
|
||||||
|
### and inherited inconsistent behaviors from getopts.
|
||||||
|
|
||||||
set -ef
|
set -ef
|
||||||
|
|
||||||
if test -n "$KSH_VERSION"; then
|
all=0
|
||||||
puts() {
|
for flag; do
|
||||||
print -r -- "$*"
|
case "$flag" in
|
||||||
}
|
(--) shift; break;;
|
||||||
else
|
(-?*)
|
||||||
puts() {
|
shift
|
||||||
printf '%s\n' "$*"
|
while flag="${flag#?}"; [ -n "$flag" ]; do
|
||||||
}
|
case "$flag" in
|
||||||
fi
|
(a*) all=1;;
|
||||||
|
(*)
|
||||||
ALLMATCHES=0
|
printf >&2 'Illegal option: -%.1s\n' "$flag"
|
||||||
|
printf 'Usage: %s\n' "$0 [-a] args"
|
||||||
while getopts a whichopts
|
exit 2;;
|
||||||
do
|
esac
|
||||||
case "$whichopts" in
|
done;;
|
||||||
a) ALLMATCHES=1 ;;
|
(*) break
|
||||||
?) puts "Usage: $0 [-a] args"; exit 2 ;;
|
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
shift $(($OPTIND - 1))
|
|
||||||
|
|
||||||
if [ "$#" -eq 0 ]; then
|
[ "$#" != 0 ] && res=1 || res=0
|
||||||
ALLRET=1
|
|
||||||
else
|
IFS=:
|
||||||
ALLRET=0
|
for prog; do
|
||||||
fi
|
err=1
|
||||||
case $PATH in
|
case "$prog" in
|
||||||
(*[!:]:) PATH="$PATH:" ;;
|
(*/*)
|
||||||
esac
|
if [ -f "$prog" ] && [ -x "$prog" ]; then
|
||||||
for PROGRAM in "$@"; do
|
printf %s\\n "$prog"
|
||||||
RET=1
|
err=0
|
||||||
IFS_SAVE="$IFS"
|
fi;;
|
||||||
IFS=:
|
(*)
|
||||||
case $PROGRAM in
|
set -- $(printf %s: "$PATH")
|
||||||
*/*)
|
for sub; do
|
||||||
if [ -f "$PROGRAM" ] && [ -x "$PROGRAM" ]; then
|
[ -n "$sub" ] || sub=.
|
||||||
puts "$PROGRAM"
|
if [ -f "$sub/$prog" ] && [ -x "$sub/$prog" ]; then
|
||||||
RET=0
|
printf %s\\n "$sub/$prog"
|
||||||
|
err=0
|
||||||
|
[ "$all" = 1 ] || break
|
||||||
fi
|
fi
|
||||||
;;
|
done;;
|
||||||
*)
|
|
||||||
for ELEMENT in $PATH; do
|
|
||||||
if [ -z "$ELEMENT" ]; then
|
|
||||||
ELEMENT=.
|
|
||||||
fi
|
|
||||||
if [ -f "$ELEMENT/$PROGRAM" ] && [ -x "$ELEMENT/$PROGRAM" ]; then
|
|
||||||
puts "$ELEMENT/$PROGRAM"
|
|
||||||
RET=0
|
|
||||||
[ "$ALLMATCHES" -eq 1 ] || break
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
;;
|
|
||||||
esac
|
esac
|
||||||
IFS="$IFS_SAVE"
|
[ "$err" = 0 ] || res=1
|
||||||
if [ "$RET" -ne 0 ]; then
|
|
||||||
ALLRET=1
|
|
||||||
fi
|
|
||||||
done
|
done
|
||||||
|
|
||||||
exit "$ALLRET"
|
exit "$res"
|
||||||
|
|
Loading…
Add table
Reference in a new issue