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

168 lines
3.4 KiB
Bash

# -*- mode: shell-script; sh-basic-offset: 8; indent-tabs-mode: t -*-
# ex: ts=8 sw=8 noet filetype=sh
#
# bash completion for Postgresql
have psql && {
_pg_databases()
{
return # See https://launchpad.net/bugs/164772
COMPREPLY=( $( compgen -W "$( psql -l 2>/dev/null | \
sed -e '1,/^-/d' -e '/^(/,$d' | \
awk '{print $1}' )" -- "$cur" ) )
}
_pg_users()
{
# See https://launchpad.net/bugs/164772
#COMPREPLY=( $( psql -qtc 'select usename from pg_user' template1 2>/dev/null | \
# grep "^ $cur" ) )
#[ ${#COMPREPLY[@]} -eq 0 ] && COMPREPLY=( $( compgen -u -- $cur ) )
COMPREPLY=( $( compgen -u -- "$cur" ) )
}
# createdb(1) completion
#
_createdb()
{
local cur prev split=false
COMPREPLY=()
cur=`_get_cword`
prev=${COMP_WORDS[COMP_CWORD-1]}
_split_longopt && split=true
case "$prev" in
-h|--host)
_known_hosts_real "$cur"
return 0
;;
-U|--username|-O|--owner)
_pg_users
return 0
;;
-p|--port|-D|--tablespace|-E|--encoding|-T|--template)
# argument required but no completions available
return 0
;;
--help|--version)
# all other arguments are noop with these
return 0
;;
esac
$split && return 0
if [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W '-D -T -E -h -p -U -W -e -q \
--tablespace --template --encoding --host --port \
--username --password --echo --quiet --help --version' \
-- "$cur" ))
else
_pg_databases
fi
}
complete -F _createdb $default createdb
# dropdb(1) completion
#
_dropdb()
{
local cur prev split=false
COMPREPLY=()
cur=`_get_cword`
prev=${COMP_WORDS[COMP_CWORD-1]}
_split_longopt && split=true
case "$prev" in
-h|--host)
_known_hosts_real "$cur"
return 0
;;
-U|--username)
_pg_users
return 0
;;
--help|--version)
# all other arguments are noop with these
return 0
;;
esac
$split && return 0
if [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W '-h -p -U -W -i -e -q \
--host --port --username --password --interactive \
--echo --quiet --help --version' -- "$cur" ) )
else
_pg_databases
fi
}
complete -F _dropdb $default dropdb
# psql(1) completion
#
_psql()
{
local cur prev split=false
COMPREPLY=()
cur=`_get_cword`
prev=${COMP_WORDS[COMP_CWORD-1]}
_split_longopt && split=true
case "$prev" in
-h|--host)
_known_hosts_real "$cur"
return 0
;;
-U|--username)
_pg_users
return 0
;;
-d|--dbname)
_pg_databases
return 0
;;
-o|--output|-f|--file|-L|--log-file)
_filedir
return 0
;;
-c|--command|-F|--field-separator|-p|--port|-P|--pset|\
-R|--record-separator|-T|--table-attr|-v|--set|--variable)
# argument required but no completions available
return 0
;;
-\?|--help|-V|--version)
# all other arguments are noop with these
return 0
;;
esac
$split && return 0
if [[ "$cur" == -* ]]; then
# return list of available options
COMPREPLY=( $( compgen -W '-a --echo-all -A --no-align \
-c --command -d --dbname -e --echo-queries \
-E --echo-hidden -f --file -F --field-separator \
-h --host -H --html -l --list -L --log-file -n \
-o --output -p --port -P --pset -q --quiet \
-R --record-separator -s --single-step \
-S --single-line -t --tuples-only -T --table-attr \
-U --username -v --set --variable -V --version \
-W --password -x --expanded -X --no-psqlrc \
-1 --single-transaction -? --help' -- "$cur" ) )
else
# return list of available databases
_pg_databases
fi
}
complete -F _psql $filenames psql
}