| 1 |
# shellcheck shell=bash |
| 2 |
# loginctl(1) completion -*- shell-script -*- |
| 3 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
# |
| 5 |
# This file is part of systemd. |
| 6 |
# |
| 7 |
# Copyright © 2010 Ran Benita |
| 8 |
# |
| 9 |
# systemd is free software; you can redistribute it and/or modify it |
| 10 |
# under the terms of the GNU Lesser General Public License as published by |
| 11 |
# the Free Software Foundation; either version 2.1 of the License, or |
| 12 |
# (at your option) any later version. |
| 13 |
# |
| 14 |
# systemd is distributed in the hope that it will be useful, but |
| 15 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
# General Public License for more details. |
| 18 |
# |
| 19 |
# You should have received a copy of the GNU Lesser General Public License |
| 20 |
# along with systemd; If not, see <https://www.gnu.org/licenses/>. |
| 21 |
|
| 22 |
__contains_word () { |
| 23 |
local w word=$1; shift |
| 24 |
for w in "$@"; do |
| 25 |
[[ $w = "$word" ]] && return |
| 26 |
done |
| 27 |
} |
| 28 |
|
| 29 |
__get_all_sessions () { loginctl --no-legend list-sessions 2>/dev/null | { while read -r a b; do printf "%s\n" "$a"; done; } ; } |
| 30 |
__get_all_users () { loginctl --no-legend list-users 2>/dev/null | { while read -r a b; do printf "%s\n" "$b"; done; } ; } |
| 31 |
__get_all_seats () { loginctl --no-legend list-seats 2>/dev/null | { while read -r a b; do printf "%s\n" "$a"; done; } ; } |
| 32 |
|
| 33 |
__get_machines() { |
| 34 |
local a b |
| 35 |
{ machinectl list --full --max-addresses=0 --no-legend --no-pager 2>/dev/null; echo ".host"; } | |
| 36 |
{ while read -r a b; do echo " $a"; done; } | |
| 37 |
sort -u |
| 38 |
} |
| 39 |
|
| 40 |
_loginctl () { |
| 41 |
local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} |
| 42 |
local i verb comps |
| 43 |
|
| 44 |
local -A OPTS=( |
| 45 |
[STANDALONE]='--all -a --help -h --no-pager --version |
| 46 |
--no-legend --no-ask-password -l --full --value -j' |
| 47 |
[ARG]='--host -H --kill-whom --property -p --signal -s -M --machine |
| 48 |
-n --lines -o --output -P --json' |
| 49 |
) |
| 50 |
|
| 51 |
if __contains_word "$prev" ${OPTS[ARG]}; then |
| 52 |
case $prev in |
| 53 |
--signal|-s) |
| 54 |
_signals |
| 55 |
return |
| 56 |
;; |
| 57 |
--kill-whom|--kill-who) |
| 58 |
comps='all leader' |
| 59 |
;; |
| 60 |
--host|-H) |
| 61 |
comps=$(compgen -A hostname) |
| 62 |
;; |
| 63 |
--machine|-M) |
| 64 |
comps=$( __get_machines ) |
| 65 |
;; |
| 66 |
--property|-p|-P) |
| 67 |
comps='' |
| 68 |
;; |
| 69 |
--output|-o) |
| 70 |
comps=$( loginctl --output=help 2>/dev/null ) |
| 71 |
;; |
| 72 |
--json) |
| 73 |
comps=$( loginctl --json=help 2>/dev/null ) |
| 74 |
;; |
| 75 |
esac |
| 76 |
COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) |
| 77 |
return 0 |
| 78 |
fi |
| 79 |
|
| 80 |
if [[ "$cur" = -* ]]; then |
| 81 |
COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") ) |
| 82 |
return 0 |
| 83 |
fi |
| 84 |
|
| 85 |
local -A VERBS=( |
| 86 |
[SESSIONS]='session-status show-session activate lock-session unlock-session terminate-session kill-session' |
| 87 |
[USERS]='user-status show-user enable-linger disable-linger terminate-user kill-user' |
| 88 |
[SEATS]='seat-status show-seat terminate-seat' |
| 89 |
[STANDALONE]='list-sessions lock-sessions unlock-sessions list-users list-seats flush-devices' |
| 90 |
[ATTACH]='attach' |
| 91 |
) |
| 92 |
|
| 93 |
for ((i=0; i < COMP_CWORD; i++)); do |
| 94 |
if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} && |
| 95 |
! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then |
| 96 |
verb=${COMP_WORDS[i]} |
| 97 |
break |
| 98 |
fi |
| 99 |
done |
| 100 |
|
| 101 |
if [[ -z ${verb-} ]]; then |
| 102 |
comps="${VERBS[*]}" |
| 103 |
|
| 104 |
elif __contains_word "$verb" ${VERBS[SESSIONS]}; then |
| 105 |
comps=$( __get_all_sessions ) |
| 106 |
|
| 107 |
elif __contains_word "$verb" ${VERBS[USERS]}; then |
| 108 |
comps=$( __get_all_users ) |
| 109 |
|
| 110 |
elif __contains_word "$verb" ${VERBS[SEATS]}; then |
| 111 |
comps=$( __get_all_seats ) |
| 112 |
|
| 113 |
elif __contains_word "$verb" ${VERBS[STANDALONE]}; then |
| 114 |
comps='' |
| 115 |
|
| 116 |
elif __contains_word "$verb" ${VERBS[ATTACH]}; then |
| 117 |
if [[ $prev = $verb ]]; then |
| 118 |
comps=$( __get_all_seats ) |
| 119 |
else |
| 120 |
comps=$(compgen -A file -- "$cur" ) |
| 121 |
compopt -o filenames |
| 122 |
fi |
| 123 |
fi |
| 124 |
|
| 125 |
COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) |
| 126 |
return 0 |
| 127 |
} |
| 128 |
|
| 129 |
complete -F _loginctl loginctl |