1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-12-22 13:33:56 +03:00

bash-completion: avoid losing backslashes in unit names

Use 'read -r' everywhere to consider backslashes as parts of the input line.
Single-quote the arguments to 'compgen -W' to avoid immediate expansion.
compgen itself will expand the argument.

Fixes a possible reason for "Failed to issue method call: Unknown unit"
after requesting completion.

https://bugzilla.redhat.com/show_bug.cgi?id=814966
This commit is contained in:
Michal Schmidt 2012-05-09 09:18:44 +02:00
parent 07f74a7ebd
commit f3b1766155

View File

@ -39,19 +39,19 @@ __filter_units_by_property () {
} }
__get_all_units () { __systemctl list-units --all \ __get_all_units () { __systemctl list-units --all \
| { while read a b; do echo "$a"; done; }; } | { while read -r a b; do echo "$a"; done; }; }
__get_active_units () { __systemctl list-units \ __get_active_units () { __systemctl list-units \
| { while read a b; do echo "$a"; done; }; } | { while read -r a b; do echo "$a"; done; }; }
__get_inactive_units () { __systemctl list-units --all \ __get_inactive_units () { __systemctl list-units --all \
| { while read a b c d; do [[ $c == "inactive" ]] && echo "$a"; done; }; } | { while read -r a b c d; do [[ $c == "inactive" ]] && echo "$a"; done; }; }
__get_failed_units () { __systemctl list-units \ __get_failed_units () { __systemctl list-units \
| { while read a b c d; do [[ $c == "failed" ]] && echo "$a"; done; }; } | { while read -r a b c d; do [[ $c == "failed" ]] && echo "$a"; done; }; }
__get_enabled_units () { __systemctl list-unit-files \ __get_enabled_units () { __systemctl list-unit-files \
| { while read a b c ; do [[ $b == "enabled" ]] && echo "$a"; done; }; } | { while read -r a b c ; do [[ $b == "enabled" ]] && echo "$a"; done; }; }
__get_disabled_units () { __systemctl list-unit-files \ __get_disabled_units () { __systemctl list-unit-files \
| { while read a b c ; do [[ $b == "disabled" ]] && echo "$a"; done; }; } | { while read -r a b c ; do [[ $b == "disabled" ]] && echo "$a"; done; }; }
__get_masked_units () { __systemctl list-unit-files \ __get_masked_units () { __systemctl list-unit-files \
| { while read a b c ; do [[ $b == "masked" ]] && echo "$a"; done; }; } | { while read -r a b c ; do [[ $b == "masked" ]] && echo "$a"; done; }; }
_systemctl () { _systemctl () {
local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
@ -89,13 +89,13 @@ _systemctl () {
comps='' comps=''
;; ;;
esac esac
COMPREPLY=( $(compgen -W "$comps" -- "$cur") ) COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
return 0 return 0
fi fi
if [[ "$cur" = -* ]]; then if [[ "$cur" = -* ]]; then
COMPREPLY=( $(compgen -W "${OPTS[*]}" -- "$cur") ) COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
return 0 return 0
fi fi
@ -143,14 +143,14 @@ _systemctl () {
elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then
comps=$( __filter_units_by_property CanStart yes \ comps=$( __filter_units_by_property CanStart yes \
$( __get_inactive_units \ $( __get_inactive_units \
| while read line; do \ | while read -r line; do \
[[ "$line" =~ \.(device|snapshot)$ ]] || echo "$line"; \ [[ "$line" =~ \.(device|snapshot)$ ]] || echo "$line"; \
done )) done ))
elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then
comps=$( __filter_units_by_property CanStart yes \ comps=$( __filter_units_by_property CanStart yes \
$( __get_all_units \ $( __get_all_units \
| while read line; do \ | while read -r line; do \
[[ "$line" =~ \.(device|snapshot|socket|timer)$ ]] || echo "$line"; \ [[ "$line" =~ \.(device|snapshot|socket|timer)$ ]] || echo "$line"; \
done )) done ))
@ -176,15 +176,15 @@ _systemctl () {
comps='' comps=''
elif __contains_word "$verb" ${VERBS[JOBS]}; then elif __contains_word "$verb" ${VERBS[JOBS]}; then
comps=$( __systemctl list-jobs | { while read a b; do echo "$a"; done; } ) comps=$( __systemctl list-jobs | { while read -r a b; do echo "$a"; done; } )
elif __contains_word "$verb" ${VERBS[SNAPSHOTS]}; then elif __contains_word "$verb" ${VERBS[SNAPSHOTS]}; then
comps=$( __systemctl list-units --type snapshot --full --all \ comps=$( __systemctl list-units --type snapshot --full --all \
| { while read a b; do echo "$a"; done; } ) | { while read -r a b; do echo "$a"; done; } )
elif __contains_word "$verb" ${VERBS[ENVS]}; then elif __contains_word "$verb" ${VERBS[ENVS]}; then
comps=$( __systemctl show-environment \ comps=$( __systemctl show-environment \
| while read line; do echo "${line%%=*}=";done ) | while read -r line; do echo "${line%%=*}=";done )
compopt -o nospace compopt -o nospace
elif __contains_word "$verb" ${VERBS[FILE]}; then elif __contains_word "$verb" ${VERBS[FILE]}; then
@ -192,14 +192,14 @@ _systemctl () {
compopt -o filenames compopt -o filenames
fi fi
COMPREPLY=( $(compgen -W "$comps" -- "$cur") ) COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
return 0 return 0
} }
complete -F _systemctl systemctl complete -F _systemctl systemctl
__get_all_sessions () { systemd-loginctl list-sessions | { while read a b; do echo "$a"; done; } ; } __get_all_sessions () { systemd-loginctl list-sessions | { while read -r a b; do echo "$a"; done; } ; }
__get_all_users () { systemd-loginctl list-users | { while read a b; do echo "$b"; done; } ; } __get_all_users () { systemd-loginctl list-users | { while read -r a b; do echo "$b"; done; } ; }
__get_all_seats () { systemd-loginctl list-seats | { while read a b; do echo "$a"; done; } ; } __get_all_seats () { systemd-loginctl list-seats | { while read -r a b; do echo "$a"; done; } ; }
_loginctl () { _loginctl () {
local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
@ -225,13 +225,13 @@ _loginctl () {
comps='' comps=''
;; ;;
esac esac
COMPREPLY=( $(compgen -W "$comps" -- "$cur") ) COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
return 0 return 0
fi fi
if [[ "$cur" = -* ]]; then if [[ "$cur" = -* ]]; then
COMPREPLY=( $(compgen -W "${OPTS[*]}" -- "$cur") ) COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
return 0 return 0
fi fi
@ -275,7 +275,7 @@ _loginctl () {
fi fi
fi fi
COMPREPLY=( $(compgen -W "$comps" -- "$cur") ) COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
return 0 return 0
} }
complete -F _loginctl loginctl complete -F _loginctl loginctl