On Tue, Mar 3, 2009 at 3:12 PM, Mike Kelly <pi...@pioto.org> wrote:
> Actually, which is available on every UNIX system I've ever used...
>
> What you actually want is "$(type -P vncviewer)". Otherwise, if they
> have set alias vncviewer='vncviewer -foo' somewhere, type -p returns
> nothing, while type -P will return the expected results.
>
> Also, readlink -f isn't portable -- it doesn't work on FreeBSD, at
> least. On FreeBSD, you need to use realpath instead.

Then I suggest we use `type' instead of `which', since `type' is a
builtin.  What do you think about this code then:

---8<-------------------------------------------------------------------

# Get real command.
# @param $1  Command
# @stdout  Filename of cmd in PATH, possible symbolic links resolved.
# @return  True (0) if command found, False (> 0) if not.
_realcommand() {
    if type -p realpath > /dev/null; then
        realpath "$(type -P "$1")"
    elif type -p readlink > /dev/null; then
        readlink -f "$(type -P "$1")"
    else
        echo "$1"
    fi
}

have vncviewer &&
_vncviewer_bootstrap() {
        local fname
        case "$(_realcommand vncviewer)" in
                *xvnc4viewer)      fname=_xvnc4viewer    ;;
                *tightvncviewer|*) fname=_tightvncviewer ;;
        esac
        # Install real completion for subsequent completions
        complete -F $fname vncviewer
        $fname  # Generate completions once for now
        unset -f _vncviewer_bootstrap
} &&
complete -F _vncviewer_bootstrap vncviewer

---8<-------------------------------------------------------------------

If we would let the `_vncviewer_bootstrap' `*-case' default to
`_tightvncviewer' this solution is gonna work on Mandriva... untill
a distribution comes along with another default `vncviewer' :-(  so
we still can improve with a package-level solution indeed.

Regards,
Freddy Vulto
http://fvue.nl

_______________________________________________
Bash-completion-devel mailing list
Bash-completion-devel@lists.alioth.debian.org
http://lists.alioth.debian.org/mailman/listinfo/bash-completion-devel

Reply via email to