1# systemd-run(1) completion                       -*- shell-script -*-
2# SPDX-License-Identifier: LGPL-2.1-or-later
3#
4# This file is part of systemd.
5#
6# systemd is free software; you can redistribute it and/or modify it
7# under the terms of the GNU Lesser General Public License as published by
8# the Free Software Foundation; either version 2.1 of the License, or
9# (at your option) any later version.
10#
11# systemd is distributed in the hope that it will be useful, but
12# WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14# General Public License for more details.
15#
16# You should have received a copy of the GNU Lesser General Public License
17# along with systemd; If not, see <http://www.gnu.org/licenses/>.
18
19__systemctl() {
20    local mode=$1; shift 1
21    systemctl $mode --full --no-legend --no-pager --plain "$@"
22}
23
24__get_slice_units () { __systemctl $1 list-units --all -t slice \
25                           | { while read -r a b c d; do echo " $a"; done; }; }
26
27__get_machines() {
28    local a b
29    machinectl list --full --no-legend --no-pager | { while read a b; do echo " $a"; done; };
30}
31
32_systemd_run() {
33    local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
34    local OPTS='--no-ask-password --scope -u --unit -p --property --description --slice --slice-inherit
35                -r --remain-after-exit --send-sighup --service-type --uid --gid --nice
36                --working-directory -d --same-dir -E --setenv -t --pty -P --pipe -S --shell -q --quiet
37                --on-active --on-boot --on-startup --on-unit-active --on-unit-inactive --on-calendar
38                --on-clock-change --on-timezone-change --path-property --socket-property
39                --timer-property --no-block --wait -G --collect --user --system -H --host -M --machine
40                -h --help --version'
41
42    local mode=--system
43    local i
44    local opts_with_values=(
45        --unit -p --property --slice --description --service-type --uid --gid --nice --working-directory
46        -E --setenv --on-active --on-boot --on-startup --on-unit-active --on-unit-inactive --on-calendar
47        --path-property --socket-property --timer-property -H --host -M --machine
48    )
49    for (( i=1; i <= COMP_CWORD; i++ )); do
50        if [[ ${COMP_WORDS[i]} != -* ]]; then
51            local root_command=${COMP_WORDS[i]}
52            _command_offset $i
53            return
54        fi
55
56        [[ ${COMP_WORDS[i]} == "--user" ]] && mode=--user
57
58        [[ $i -lt $COMP_CWORD && " ${opts_with_values[@]} " =~ " ${COMP_WORDS[i]} " ]] && ((i++))
59    done
60
61    case "$prev" in
62        --unit|--description|--on-active|--on-boot|--on-startup|--on-unit-active|--on-unit-inactive|--on-calendar)
63            # argument required but no completions available
64            return
65            ;;
66        --slice)
67            local comps=$(__get_slice_units $mode)
68
69            COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
70            return 0
71            ;;
72        --service-type)
73            local comps='simple forking oneshot dbus notify idle'
74
75            COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
76            return 0
77            ;;
78        -p|--property)
79            local comps='CPUAccounting= MemoryAccounting= BlockIOAccounting= SendSIGHUP=
80                         SendSIGKILL= MemoryLimit= CPUShares= BlockIOWeight= User= Group=
81                         DevicePolicy= KillMode= ExitType= DeviceAllow= BlockIOReadBandwidth=
82                         BlockIOWriteBandwidth= BlockIODeviceWeight= Nice= Environment=
83                         KillSignal= RestartKillSignal= FinalKillSignal= LimitCPU= LimitFSIZE= LimitDATA=
84                         LimitSTACK= LimitCORE= LimitRSS= LimitNOFILE= LimitAS= LimitNPROC=
85                         LimitMEMLOCK= LimitLOCKS= LimitSIGPENDING= LimitMSGQUEUE=
86                         LimitNICE= LimitRTPRIO= LimitRTTIME= PrivateTmp= PrivateDevices=
87                         PrivateNetwork= NoNewPrivileges= WorkingDirectory= RootDirectory=
88                         TTYPath= SyslogIdentifier= SyslogLevelPrefix= SyslogLevel=
89                         SyslogFacility= TimerSlackNSec= OOMScoreAdjust= ReadWritePaths=
90                         ReadOnlyPaths= InaccessiblePaths= EnvironmentFile=
91                         ProtectSystem= ProtectHome= RuntimeDirectory= PassEnvironment='
92
93            COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
94            return 0
95            ;;
96        -H|--host)
97            local comps=$(compgen -A hostname)
98
99            COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
100            return 0
101            ;;
102        -M|--machine)
103            local comps=$( __get_machines )
104
105            COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
106            return 0
107            ;;
108        --timer-property)
109            local comps='AccuracySec= WakeSystem='
110            COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
111            return 0
112            ;;
113        --working-directory)
114            local comps
115            if [[ -z $cur ]]; then
116                comps=$(compgen -A directory -- "/" )
117            else
118                comps=$(compgen -A directory -- "$cur" )
119            fi
120            compopt -o filenames
121            COMPREPLY=( $(compgen -W '$comps' -- "$cur" ) )
122            return 0
123            ;;
124    esac
125
126    COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
127    return 0
128}
129
130complete -F _systemd_run systemd-run
131