]> git.ipfire.org Git - thirdparty/systemd.git/blame - shell-completion/bash/loginctl
man: fix incorrectly placed full stop
[thirdparty/systemd.git] / shell-completion / bash / loginctl
CommitLineData
d611dadc 1# loginctl(1) completion -*- shell-script -*-
7059062c 2# SPDX-License-Identifier: LGPL-2.1+
d611dadc
MB
3#
4# This file is part of systemd.
5#
96b2fb93 6# Copyright © 2010 Ran Benita
d611dadc
MB
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 systemd; If not, see <http://www.gnu.org/licenses/>.
20
21__contains_word () {
843cfcb1
ZJS
22 local w word=$1; shift
23 for w in "$@"; do
24 [[ $w = "$word" ]] && return
25 done
d611dadc
MB
26}
27
841aa8c0
ZJS
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; } ; }
d611dadc 31
3b59e2cb 32__get_machines() {
843cfcb1 33 local a b
e2268fa4 34 machinectl list --full --no-legend --no-pager 2>/dev/null |
843cfcb1 35 { while read a b; do echo " $a"; done; };
3b59e2cb
YW
36}
37
d611dadc 38_loginctl () {
843cfcb1
ZJS
39 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
40 local i verb comps
41
42 local -A OPTS=(
43 [STANDALONE]='--all -a --help -h --no-pager --version
44 --no-legend --no-ask-password -l --full --value'
45 [ARG]='--host -H --kill-who --property -p --signal -s -M --machine
46 -n --lines -o --output'
47 )
48
49 if __contains_word "$prev" ${OPTS[ARG]}; then
50 case $prev in
51 --signal|-s)
52 _signals
53 return
54 ;;
55 --kill-who)
56 comps='all leader'
57 ;;
58 --host|-H)
59 comps=$(compgen -A hostname)
60 ;;
61 --machine|-M)
62 comps=$( __get_machines )
63 ;;
64 --property|-p)
65 comps=''
66 ;;
67 --output|-o)
68 comps=$( loginctl --output=help 2>/dev/null )
69 ;;
70 esac
71 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
72 return 0
73 fi
d611dadc 74
843cfcb1
ZJS
75 if [[ "$cur" = -* ]]; then
76 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
77 return 0
78 fi
79
80 local -A VERBS=(
81 [SESSIONS]='session-status show-session activate lock-session unlock-session terminate-session kill-session'
82 [USERS]='user-status show-user enable-linger disable-linger terminate-user kill-user'
83 [SEATS]='seat-status show-seat terminate-seat'
84 [STANDALONE]='list-sessions lock-sessions unlock-sessions list-users list-seats flush-devices'
85 [ATTACH]='attach'
86 )
87
88 for ((i=0; i < COMP_CWORD; i++)); do
89 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
90 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
91 verb=${COMP_WORDS[i]}
92 break
d611dadc 93 fi
843cfcb1 94 done
d611dadc 95
843cfcb1
ZJS
96 if [[ -z $verb ]]; then
97 comps="${VERBS[*]}"
d611dadc 98
843cfcb1
ZJS
99 elif __contains_word "$verb" ${VERBS[SESSIONS]}; then
100 comps=$( __get_all_sessions )
d611dadc 101
843cfcb1
ZJS
102 elif __contains_word "$verb" ${VERBS[USERS]}; then
103 comps=$( __get_all_users )
d611dadc 104
843cfcb1
ZJS
105 elif __contains_word "$verb" ${VERBS[SEATS]}; then
106 comps=$( __get_all_seats )
d611dadc 107
843cfcb1
ZJS
108 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
109 comps=''
d611dadc 110
843cfcb1
ZJS
111 elif __contains_word "$verb" ${VERBS[ATTACH]}; then
112 if [[ $prev = $verb ]]; then
113 comps=$( __get_all_seats )
114 else
115 comps=$(compgen -A file -- "$cur" )
116 compopt -o filenames
d611dadc 117 fi
843cfcb1 118 fi
d611dadc 119
843cfcb1
ZJS
120 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
121 return 0
d611dadc
MB
122}
123
124complete -F _loginctl loginctl