2013-03-03 01:34:01 +04:00
# loginctl(1) completion -*- shell-script -*-
2020-11-09 07:23:58 +03:00
# SPDX-License-Identifier: LGPL-2.1-or-later
2013-03-03 01:34:01 +04:00
#
# This file is part of systemd.
#
2018-06-12 20:00:24 +03:00
# Copyright © 2010 Ran Benita
2013-03-03 01:34:01 +04:00
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# systemd is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
2022-06-28 17:07:35 +03:00
# along with systemd; If not, see <https://www.gnu.org/licenses/>.
2013-03-03 01:34:01 +04:00
__contains_word () {
2019-04-05 12:39:14 +03:00
local w word=$1; shift
for w in "$@"; do
[[ $w = "$word" ]] && return
done
2013-03-03 01:34:01 +04:00
}
2013-12-21 23:20:29 +04:00
__get_all_sessions () { loginctl --no-legend list-sessions | { while read -r a b; do printf "%s\n" "$a"; done; } ; }
__get_all_users () { loginctl --no-legend list-users | { while read -r a b; do printf "%s\n" "$b"; done; } ; }
__get_all_seats () { loginctl --no-legend list-seats | { while read -r a b; do printf "%s\n" "$a"; done; } ; }
2013-03-03 01:34:01 +04:00
2018-12-13 20:22:40 +03:00
__get_machines() {
2019-04-05 12:39:14 +03:00
local a b
2022-10-20 08:48:02 +03:00
{ machinectl list --full --max-addresses=0 --no-legend --no-pager 2>/dev/null; echo ".host"; } | \
{ while read a b; do echo " $a"; done; } | \
sort -u
2018-12-13 20:22:40 +03:00
}
2013-03-03 01:34:01 +04:00
_loginctl () {
2019-04-05 12:39:14 +03:00
local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
local i verb comps
local -A OPTS=(
[STANDALONE]='--all -a --help -h --no-pager --version
--no-legend --no-ask-password -l --full --value'
2022-08-24 11:41:30 +03:00
[ARG]='--host -H --kill-whom --property -p --signal -s -M --machine
2021-02-10 20:50:41 +03:00
-n --lines -o --output -P'
2019-04-05 12:39:14 +03:00
)
if __contains_word "$prev" ${OPTS[ARG]}; then
case $prev in
--signal|-s)
_signals
return
;;
2022-08-24 11:41:30 +03:00
--kill-whom|--kill-who)
2019-04-05 12:39:14 +03:00
comps='all leader'
;;
--host|-H)
comps=$(compgen -A hostname)
;;
--machine|-M)
comps=$( __get_machines )
;;
2021-02-10 20:50:41 +03:00
--property|-p|-P)
2019-04-05 12:39:14 +03:00
comps=''
;;
--output|-o)
comps=$( loginctl --output=help 2>/dev/null )
;;
esac
COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
return 0
fi
2013-03-03 01:34:01 +04:00
2019-04-05 12:39:14 +03:00
if [[ "$cur" = -* ]]; then
COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
return 0
fi
local -A VERBS=(
[SESSIONS]='session-status show-session activate lock-session unlock-session terminate-session kill-session'
[USERS]='user-status show-user enable-linger disable-linger terminate-user kill-user'
[SEATS]='seat-status show-seat terminate-seat'
[STANDALONE]='list-sessions lock-sessions unlock-sessions list-users list-seats flush-devices'
[ATTACH]='attach'
)
for ((i=0; i < COMP_CWORD; i++)); do
if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
verb=${COMP_WORDS[i]}
break
2013-03-03 01:34:01 +04:00
fi
2019-04-05 12:39:14 +03:00
done
2013-03-03 01:34:01 +04:00
2021-06-22 16:56:19 +03:00
if [[ -z ${verb-} ]]; then
2019-04-05 12:39:14 +03:00
comps="${VERBS[*]}"
2013-03-03 01:34:01 +04:00
2019-04-05 12:39:14 +03:00
elif __contains_word "$verb" ${VERBS[SESSIONS]}; then
comps=$( __get_all_sessions )
2013-03-03 01:34:01 +04:00
2019-04-05 12:39:14 +03:00
elif __contains_word "$verb" ${VERBS[USERS]}; then
comps=$( __get_all_users )
2013-03-03 01:34:01 +04:00
2019-04-05 12:39:14 +03:00
elif __contains_word "$verb" ${VERBS[SEATS]}; then
comps=$( __get_all_seats )
2013-03-03 01:34:01 +04:00
2019-04-05 12:39:14 +03:00
elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
comps=''
2013-03-03 01:34:01 +04:00
2019-04-05 12:39:14 +03:00
elif __contains_word "$verb" ${VERBS[ATTACH]}; then
if [[ $prev = $verb ]]; then
comps=$( __get_all_seats )
else
comps=$(compgen -A file -- "$cur" )
compopt -o filenames
2013-03-03 01:34:01 +04:00
fi
2019-04-05 12:39:14 +03:00
fi
2013-03-03 01:34:01 +04:00
2019-04-05 12:39:14 +03:00
COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
return 0
2013-03-03 01:34:01 +04:00
}
complete -F _loginctl loginctl