Debian Codegraph @

shell-completion/bash/loginctl

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