(spec) Epoch 1 is when systemd and journalctl are split.
[systemd_ALT/systemd_imz.git] / loginctl-bash3
blob0f19acdbd1c6dcc49543f214c68a3b782df1c9c2
1 # This file is part of systemd.
3 # Copyright 2010 Ran Benita
5 # systemd is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as published by
7 # the Free Software Foundation; either version 2.1 of the License, or
8 # (at your option) any later version.
10 # systemd is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 __contains_word () {
19         local word=$1; shift
20         for w in $*; do [[ $w = $word ]] && return 0; done
21         return 1
24 __get_all_sessions () { loginctl list-sessions | { while read -r a b; do echo " $a"; done; } ; }
25 __get_all_users    () { loginctl list-users    | { while read -r a b; do echo " $b"; done; } ; }
26 __get_all_seats    () { loginctl list-seats    | { while read -r a b; do echo " $a"; done; } ; }
28 _loginctl () {
29         local i verb comps cur prev
30         _get_comp_words_by_ref cur prev
32         local OPTS_STANDALONE='--all -a --help -h --no-pager --privileged -P --version'
33         local OPTS_ARG='--host -H --kill-who --property -p --signal -s'
35         if __contains_word "$prev" "$OPTS_ARG"; then
36                 case $prev in
37                         --signal|-s)
38                                 comps=$(compgen -A signal)
39                         ;;
40                         --kill-who)
41                                 comps='all leader'
42                         ;;
43                         --host|-H)
44                                 comps=$(compgen -A hostname)
45                         ;;
46                         --property|-p)
47                                 comps=''
48                         ;;
49                 esac
50                 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
51                 return 0
52         fi
54         if [[ "$cur" = -* ]]; then
55                 COMPREPLY=( $(compgen -W '$OPTS_ARG $OPTS_STANDALONE' -- "$cur") )
56                 return 0
57         fi
59         local VERBS_SESSIONS='session-status show-session activate lock-session unlock-session terminate-session kill-session'
60         local VERBS_USERS='user-status show-user enable-linger disable-linger terminate-user kill-user'
61         local VERBS_SEATS='seat-status show-seat terminate-seat'
62         local VERBS_STANDALONE='list-sessions list-users list-seats flush-devices'
63         local VERBS_ATTACH='attach'
64         local VERBS="$VERBS_SESSIONS $VERBS_USERS $VERBS_SEATS $VERBS_STANDALONE $VERBS_ATTACH"
66         local verb
67         for ((i=0; $i <= $COMP_CWORD; i++)); do
68                 if __contains_word "${COMP_WORDS[i]}" "$VERBS" &&
69                  ! __contains_word "${COMP_WORDS[i-1]}" "$OPTS_ARG"; then
70                         verb=${COMP_WORDS[i]}
71                         break
72                 fi
73         done
75         if   [[ -z $verb ]]; then
76                 comps="$VERBS"
78         elif __contains_word "$verb" "$VERBS_SESSIONS"; then
79                 comps=$( __get_all_sessions )
81         elif __contains_word "$verb" "$VERBS_USERS"; then
82                 comps=$( __get_all_users )
84         elif __contains_word "$verb" "$VERBS_SEATS"; then
85                 comps=$( __get_all_seats )
87         elif __contains_word "$verb" "$VERBS_STANDALONE"; then
88                 comps=''
90         elif __contains_word "$verb" $VERBS_ATTACH; then
91                 if [[ $prev = $verb ]]; then
92                         comps=$( __get_all_seats )
93                 else
94                         comps=$(compgen -o filenames -A file -- "$cur" )
95                 fi
96         fi
98         COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
99         return 0
101 complete -F _loginctl loginctl