Freddy Vulto cfcf9fae8f Quote unquoted $cur to prevent globbing.
Closes Alioth #311614

Globbing might occur if $cur contains one of these globbing characters: * ? [ ]

The bug becomes apparent:

On Cygwin if the glob-string contains backslashes as well, causing a warning (Cygwin >= 1.7):

    MS-DOS style path detected: ...
    Preferred POSIX equivalent is: ...
    CYGWIN environment variable option "nodosfilewarning" turns off this warning.
    Consult the user's guide for more details about POSIX paths:
      http://cygwin.com/cygwin-ug-net/using.html#using-pathnames

On Linux, using strace, you can see bash-completion doing an unnecessary `open' system call.

Steps to reproduce on Linux using `strace':

Environment:  Linux, bash-completion-1.0

1.  Start bash with bash-completion loaded and find out PID ($$):

    $ echo $$
    MYPID

2.  In a second bash shell, `strace' the above PID:

    $ strace -e trace=open -f -o strace.log -p MYPID

3.  Within the first bash shell, type:

    $ cur="?"; _kernel_versions

4.  In the second bash shell, type ^C to quick `strace'.

5.  Check `strace.log', here you can see bash accessing
    something it shouldn't:

    ...
    open(".", O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY|O_CLOEXEC) = 3
    ...

6.  The above call to `open' disappears if $cur in _kernel_versions gets
    quoted, and you repeat the steps above:

    _kernel_versions()
    {
        COMPREPLY=( $( compgen -W '$( command ls /lib/modules )' -- "$cur" ) )
    }
2009-09-25 09:36:29 +02:00

80 lines
2.1 KiB
Bash

# -*- mode: shell-script; sh-basic-offset: 8; indent-tabs-mode: t -*-
# ex: ts=8 sw=8 noet filetype=sh
#
# bash completion for mock
have mock &&
_mock()
{
local cur prev plugins cfgdir split=false
COMPREPLY=()
cur=`_get_cword`
prev=${COMP_WORDS[COMP_CWORD-1]}
plugins='tmpfs root_cache yum_cache bind_mount ccache'
cfgdir=/etc/mock
count=0
for i in "${COMP_WORDS[@]}" ; do
[ $count -eq $COMP_CWORD ] && break
if [[ "$i" == --configdir ]] ; then
cfgdir="${COMP_WORDS[((count+1))]}"
elif [[ "$i" == --configdir=* ]] ; then
cfgdir=${i/*=/}
fi
count=$((++count))
done
_split_longopt && split=true
case $prev in
-@(h|-help|-copy@(in|out)|-arch|D|-define|-with?(out)|-uniqueext|-rpmbuild_timeout|-sources|-cwd))
return 0
;;
-r|--root)
COMPREPLY=( $( compgen -W "$( command ls $cfgdir )" \
-- $cur ) )
COMPREPLY=( ${COMPREPLY[@]/%.cfg/} )
return 0
;;
--@(config|result)dir)
_filedir -d
return 0
;;
--spec)
_filedir spec
return 0
;;
--target)
# Yep, compatible archs, not compatible build archs
# (e.g. ix86 chroot builds in x86_64 mock host)
# This would actually depend on what the target root
# can be used to build for...
COMPREPLY=( $( compgen -W "$( command rpm --showrc | sed -ne 's/^\s*compatible\s\+archs\s*:\s*\(.*\)/\1/i p' )" -- "$cur" ) )
return 0
;;
--@(en|dis)able-plugin)
COMPREPLY=( $( compgen -W "$plugins" -- "$cur" ) )
return 0
;;
esac
$split && return 0
if [[ "$cur" == -* ]] ; then
COMPREPLY=( $( compgen -W '--version -h --help --rebuild \
--buildsrpm --shell --chroot --clean --init \
--installdeps --install --update --orphanskill \
--copyin --copyout -r --root --offline --no-clean \
--cleanup-after --no-cleanup-after --arch --target \
-D --define --with --without --resultdir --uniqueext \
--configdir --rpmbuild_timeout --unpriv --cwd --spec \
--sources -v --verbose -q --quiet --trace \
--enable-plugin --disable-plugin --print-root-path' \
-- "$cur" ) )
else
_filedir '?(no)src.rpm'
fi
} &&
complete -F _mock $filenames mock