]>
Commit | Line | Data |
---|---|---|
d611dadc | 1 | # systemctl(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 | __systemctl() { | |
9e542e0b | 9 | local mode=$1; shift 1 |
aedd4816 | 10 | systemctl $mode --full --no-legend --no-pager "$@" 2>/dev/null |
d611dadc MB |
11 | } |
12 | ||
caffaf58 | 13 | __systemd_properties() { |
4f150c40 | 14 | @rootlibexecdir@/systemd --dump-bus-properties |
caffaf58 ZJS |
15 | } |
16 | ||
d611dadc | 17 | __contains_word () { |
a72d698d DR |
18 | local w word=$1; shift |
19 | for w in "$@"; do | |
20 | [[ $w = "$word" ]] && return | |
21 | done | |
d611dadc MB |
22 | } |
23 | ||
b1bdb649 | 24 | __filter_units_by_properties () { |
f28255e2 | 25 | local mode=$1 properties=$2; shift 2 |
b1bdb649 | 26 | local units=("$@") |
f28255e2 YW |
27 | local props i p n |
28 | local names= count=0 | |
29 | ||
30 | IFS=$',' read -r -a p < <(echo "Names,$properties") | |
31 | n=${#p[*]} | |
32 | readarray -t props < \ | |
33 | <(__systemctl $mode show --property "Names,$properties" -- "${units[@]}") | |
34 | ||
35 | for ((i=0; i < ${#props[*]}; i++)); do | |
36 | if [[ -z ${props[i]} ]]; then | |
37 | if (( count == n )) && [[ -n $names ]]; then | |
38 | echo $names | |
b1bdb649 | 39 | fi |
f28255e2 YW |
40 | names= |
41 | count=0 | |
42 | else | |
43 | (( count++ )) | |
44 | if [[ ${props[i]%%=*} == 'Names' ]]; then | |
45 | names=${props[i]#*=} | |
b1bdb649 | 46 | fi |
b1bdb649 YW |
47 | fi |
48 | done | |
f28255e2 YW |
49 | if (( count == n )) && [[ -n $names ]]; then |
50 | echo $names | |
51 | fi | |
b1bdb649 YW |
52 | } |
53 | ||
98476dc8 | 54 | __get_all_units () { { __systemctl $1 list-unit-files "$2*"; __systemctl $1 list-units --all "$2*"; } \ |
aea447c9 | 55 | | { while read -r a b; do echo " $a"; done; }; } |
98476dc8 | 56 | __get_non_template_units() { { __systemctl $1 list-unit-files "$2*"; __systemctl $1 list-units --all "$2*"; } \ |
9ff8af54 | 57 | | { while read -r a b; do [[ $a =~ @\. ]] || echo " $a"; done; }; } |
c839b729 | 58 | __get_template_names () { __systemctl $1 list-unit-files "$2*" \ |
e9a19bd8 | 59 | | { while read -r a b; do [[ $a =~ @\. ]] && echo " ${a%%@.*}@"; done; }; } |
c839b729 | 60 | __get_active_units () { __systemctl $1 list-units "$2*" \ |
79c16383 | 61 | | { while read -r a b; do echo " $a"; done; }; } |
f28255e2 YW |
62 | |
63 | __get_not_masked_unit_files() { | |
64 | # filter out masked, not-found, or template units. | |
65 | __systemctl $1 list-unit-files --state enabled,enabled-runtime,linked,linked-runtime,static,indirect,disabled,generated,transient "$2*" | \ | |
66 | { while read -r a b; do [[ $a =~ @\. ]] || echo " $a"; done; } | |
67 | } | |
68 | ||
f29c77bc | 69 | __get_startable_units () { |
f28255e2 YW |
70 | __filter_units_by_properties $1 ActiveState=inactive,CanStart=yes $( |
71 | { __get_not_masked_unit_files $1 $2 | |
72 | # get inactive template units | |
73 | __systemctl $1 list-units --state inactive,failed "$2*" | \ | |
b1bdb649 | 74 | { while read -r a b c; do [[ $b == "loaded" ]] && echo " $a"; done; } |
f28255e2 | 75 | } | sort -u ) |
9ff8af54 | 76 | } |
f29c77bc | 77 | __get_restartable_units () { |
9ff8af54 | 78 | # filter out masked and not-found |
f28255e2 YW |
79 | __filter_units_by_properties $1 CanStart=yes $( |
80 | { __get_not_masked_unit_files $1 $2 | |
81 | __get_active_units $1 $2 | |
82 | } | sort -u ) | |
83 | } | |
84 | ||
85 | __get_stoppable_units () { | |
86 | # filter out masked and not-found | |
87 | __filter_units_by_properties $1 ActiveState=active,CanStop=yes $( | |
88 | { __get_not_masked_unit_files $1 $2 | |
89 | __get_active_units $1 $2 | |
90 | } | sort -u ) | |
9ff8af54 | 91 | } |
f28255e2 YW |
92 | |
93 | __get_reloadable_units () { | |
94 | # filter out masked and not-found | |
95 | __filter_units_by_properties $1 ActiveState=active,CanReload=yes $( | |
96 | { __get_not_masked_unit_files $1 $2 | |
97 | __get_active_units $1 $2 | |
98 | } | sort -u ) | |
99 | } | |
100 | ||
c839b729 | 101 | __get_failed_units () { __systemctl $1 list-units "$2*" \ |
79c16383 | 102 | | { while read -r a b c d; do [[ $c == "failed" ]] && echo " $a"; done; }; } |
c839b729 | 103 | __get_enabled_units () { __systemctl $1 list-unit-files "$2*" \ |
79c16383 | 104 | | { while read -r a b c ; do [[ $b == "enabled" ]] && echo " $a"; done; }; } |
c839b729 | 105 | __get_disabled_units () { __systemctl $1 list-unit-files "$2*" \ |
79c16383 | 106 | | { while read -r a b c ; do [[ $b == "disabled" ]] && echo " $a"; done; }; } |
c839b729 | 107 | __get_masked_units () { __systemctl $1 list-unit-files "$2*" \ |
79c16383 | 108 | | { while read -r a b c ; do [[ $b == "masked" ]] && echo " $a"; done; }; } |
98476dc8 | 109 | __get_all_unit_files () { { __systemctl $1 list-unit-files "$2*"; } | { while read -r a b; do echo " $a"; done; }; } |
d611dadc | 110 | |
3a221b5d EV |
111 | __get_machines() { |
112 | local a b | |
4ed2f1b1 ZJS |
113 | { machinectl list-images --no-legend --no-pager; machinectl list --no-legend --no-pager; } | \ |
114 | { while read a b; do echo " $a"; done; } | |
3a221b5d EV |
115 | } |
116 | ||
d611dadc MB |
117 | _systemctl () { |
118 | local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} | |
9e542e0b | 119 | local i verb comps mode |
d611dadc MB |
120 | |
121 | local -A OPTS=( | |
ed119049 | 122 | [STANDALONE]='--all -a --reverse --after --before --defaults --force -f --full -l --global |
0067c7b2 | 123 | --help -h --no-ask-password --no-block --no-legend --no-pager --no-reload --no-wall --now |
035dd8c0 YW |
124 | --quiet -q --system --user --version --runtime --recursive -r --firmware-setup |
125 | --show-types -i --ignore-inhibitors --plain --failed --value --fail --dry-run --wait' | |
3a221b5d | 126 | [ARG]='--host -H --kill-who --property -p --signal -s --type -t --state --job-mode --root |
035dd8c0 | 127 | --preset-mode -n --lines -o --output -M --machine --message' |
d611dadc MB |
128 | ) |
129 | ||
caffaf58 ZJS |
130 | if __contains_word "--user" ${COMP_WORDS[*]}; then |
131 | mode=--user | |
93557fcb EV |
132 | elif __contains_word "--global" ${COMP_WORDS[*]}; then |
133 | mode=--user | |
caffaf58 ZJS |
134 | else |
135 | mode=--system | |
136 | fi | |
137 | ||
d611dadc MB |
138 | if __contains_word "$prev" ${OPTS[ARG]}; then |
139 | case $prev in | |
140 | --signal|-s) | |
c2af8101 EV |
141 | _signals |
142 | return | |
d611dadc MB |
143 | ;; |
144 | --type|-t) | |
adb0c25e | 145 | comps=$(__systemctl $mode -t help) |
d611dadc | 146 | ;; |
631c922c | 147 | --state) |
840b2c0e | 148 | comps=$(__systemctl $mode --state=help) |
631c922c | 149 | ;; |
903e7c37 ZJS |
150 | --job-mode) |
151 | comps='fail replace replace-irreversibly isolate | |
152 | ignore-dependencies ignore-requirements flush' | |
153 | ;; | |
d611dadc MB |
154 | --kill-who) |
155 | comps='all control main' | |
156 | ;; | |
d611dadc MB |
157 | --root) |
158 | comps=$(compgen -A directory -- "$cur" ) | |
159 | compopt -o filenames | |
160 | ;; | |
161 | --host|-H) | |
162 | comps=$(compgen -A hostname) | |
163 | ;; | |
164 | --property|-p) | |
4f150c40 | 165 | comps=$(__systemd_properties) |
d611dadc | 166 | ;; |
3a221b5d EV |
167 | --preset-mode) |
168 | comps='full enable-only disable-only' | |
169 | ;; | |
170 | --output|-o) | |
2e68a49d | 171 | comps=$( systemctl --output=help 2>/dev/null ) |
3a221b5d EV |
172 | ;; |
173 | --machine|-M) | |
174 | comps=$( __get_machines ) | |
175 | ;; | |
d611dadc MB |
176 | esac |
177 | COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) | |
178 | return 0 | |
179 | fi | |
180 | ||
d611dadc MB |
181 | if [[ "$cur" = -* ]]; then |
182 | COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") ) | |
183 | return 0 | |
184 | fi | |
185 | ||
186 | local -A VERBS=( | |
aea447c9 YW |
187 | [ALL_UNITS]='cat mask' |
188 | [NONTEMPLATE_UNITS]='is-active is-failed is-enabled status show preset help list-dependencies edit set-property revert' | |
c2e09812 | 189 | [ENABLED_UNITS]='disable' |
d611dadc | 190 | [DISABLED_UNITS]='enable' |
c2e09812 | 191 | [REENABLABLE_UNITS]='reenable' |
d611dadc MB |
192 | [FAILED_UNITS]='reset-failed' |
193 | [STARTABLE_UNITS]='start' | |
194 | [STOPPABLE_UNITS]='stop condstop kill try-restart condrestart' | |
195 | [ISOLATABLE_UNITS]='isolate' | |
aabf5d42 | 196 | [RELOADABLE_UNITS]='reload condreload try-reload-or-restart force-reload' |
d611dadc | 197 | [RESTARTABLE_UNITS]='restart reload-or-restart' |
8fc5cd71 | 198 | [TARGET_AND_UNITS]='add-wants add-requires' |
d611dadc MB |
199 | [MASKED_UNITS]='unmask' |
200 | [JOBS]='cancel' | |
035dd8c0 | 201 | [ENVS]='set-environment unset-environment import-environment' |
299c397c | 202 | [STANDALONE]='daemon-reexec daemon-reload default |
c58493c0 | 203 | emergency exit halt hibernate hybrid-sleep |
e68c79db | 204 | suspend-then-hibernate kexec list-jobs list-sockets |
c58493c0 | 205 | list-timers list-units list-unit-files poweroff |
1cf3c30c | 206 | reboot rescue show-environment suspend get-default |
035dd8c0 | 207 | is-system-running preset-all' |
7b742b31 | 208 | [FILE]='link switch-root' |
99504dd4 | 209 | [TARGETS]='set-default' |
035dd8c0 | 210 | [MACHINES]='list-machines' |
d611dadc MB |
211 | ) |
212 | ||
3ce09b7d | 213 | for ((i=0; i < COMP_CWORD; i++)); do |
d611dadc MB |
214 | if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} && |
215 | ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then | |
216 | verb=${COMP_WORDS[i]} | |
217 | break | |
218 | fi | |
219 | done | |
220 | ||
86cb0691 | 221 | if [[ -z $verb ]]; then |
d611dadc MB |
222 | comps="${VERBS[*]}" |
223 | ||
224 | elif __contains_word "$verb" ${VERBS[ALL_UNITS]}; then | |
98476dc8 | 225 | comps=$( __get_all_units $mode "$cur" ) |
c317a1a1 | 226 | compopt -o filenames |
d611dadc | 227 | |
aea447c9 | 228 | elif __contains_word "$verb" ${VERBS[NONTEMPLATE_UNITS]}; then |
98476dc8 | 229 | comps=$( __get_non_template_units $mode "$cur" ) |
aea447c9 YW |
230 | compopt -o filenames |
231 | ||
d611dadc | 232 | elif __contains_word "$verb" ${VERBS[ENABLED_UNITS]}; then |
c839b729 | 233 | comps=$( __get_enabled_units $mode "$cur" ) |
c317a1a1 | 234 | compopt -o filenames |
d611dadc MB |
235 | |
236 | elif __contains_word "$verb" ${VERBS[DISABLED_UNITS]}; then | |
c839b729 EV |
237 | comps=$( __get_disabled_units $mode "$cur"; |
238 | __get_template_names $mode "$cur") | |
c317a1a1 | 239 | compopt -o filenames |
d611dadc | 240 | |
c2e09812 | 241 | elif __contains_word "$verb" ${VERBS[REENABLABLE_UNITS]}; then |
c839b729 EV |
242 | comps=$( __get_disabled_units $mode "$cur"; |
243 | __get_enabled_units $mode "$cur"; | |
244 | __get_template_names $mode "$cur") | |
c317a1a1 | 245 | compopt -o filenames |
c2e09812 | 246 | |
d611dadc | 247 | elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then |
9b536b1a | 248 | comps=$( __get_startable_units $mode "$cur" ) |
c317a1a1 | 249 | compopt -o filenames |
d611dadc MB |
250 | |
251 | elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then | |
9b536b1a | 252 | comps=$( __get_restartable_units $mode "$cur" ) |
c317a1a1 | 253 | compopt -o filenames |
d611dadc MB |
254 | |
255 | elif __contains_word "$verb" ${VERBS[STOPPABLE_UNITS]}; then | |
f28255e2 | 256 | comps=$( __get_stoppable_units $mode "$cur" ) |
c317a1a1 | 257 | compopt -o filenames |
d611dadc MB |
258 | |
259 | elif __contains_word "$verb" ${VERBS[RELOADABLE_UNITS]}; then | |
f28255e2 | 260 | comps=$( __get_reloadable_units $mode "$cur" ) |
c317a1a1 | 261 | compopt -o filenames |
d611dadc MB |
262 | |
263 | elif __contains_word "$verb" ${VERBS[ISOLATABLE_UNITS]}; then | |
f28255e2 | 264 | comps=$( __filter_units_by_properties $mode AllowIsolate=yes \ |
98476dc8 | 265 | $( __get_all_units $mode "$cur" ) ) |
c317a1a1 | 266 | compopt -o filenames |
d611dadc MB |
267 | |
268 | elif __contains_word "$verb" ${VERBS[FAILED_UNITS]}; then | |
c839b729 | 269 | comps=$( __get_failed_units $mode "$cur" ) |
c317a1a1 | 270 | compopt -o filenames |
d611dadc MB |
271 | |
272 | elif __contains_word "$verb" ${VERBS[MASKED_UNITS]}; then | |
c839b729 | 273 | comps=$( __get_masked_units $mode "$cur" ) |
c317a1a1 | 274 | compopt -o filenames |
d611dadc | 275 | |
8fc5cd71 LN |
276 | elif __contains_word "$verb" ${VERBS[TARGET_AND_UNITS]}; then |
277 | if __contains_word "$prev" ${VERBS[TARGET_AND_UNITS]} \ | |
278 | || __contains_word "$prev" ${OPTS[STANDALONE]}; then | |
98476dc8 | 279 | comps=$( __systemctl $mode list-unit-files --type target --all "$cur*" \ |
8fc5cd71 LN |
280 | | { while read -r a b; do echo " $a"; done; } ) |
281 | else | |
98476dc8 | 282 | comps=$( __get_all_unit_files $mode "$cur" ) |
8fc5cd71 LN |
283 | fi |
284 | compopt -o filenames | |
285 | ||
36b4a7ba | 286 | elif __contains_word "$verb" ${VERBS[STANDALONE]}; then |
d611dadc MB |
287 | comps='' |
288 | ||
289 | elif __contains_word "$verb" ${VERBS[JOBS]}; then | |
79c16383 | 290 | comps=$( __systemctl $mode list-jobs | { while read -r a b; do echo " $a"; done; } ) |
d611dadc | 291 | |
d611dadc | 292 | elif __contains_word "$verb" ${VERBS[ENVS]}; then |
9e542e0b | 293 | comps=$( __systemctl $mode show-environment \ |
035dd8c0 | 294 | | while read -r line; do echo " ${line%%=*}="; done ) |
d611dadc MB |
295 | compopt -o nospace |
296 | ||
297 | elif __contains_word "$verb" ${VERBS[FILE]}; then | |
298 | comps=$( compgen -A file -- "$cur" ) | |
299 | compopt -o filenames | |
035dd8c0 | 300 | |
99504dd4 | 301 | elif __contains_word "$verb" ${VERBS[TARGETS]}; then |
98476dc8 | 302 | comps=$( __systemctl $mode list-unit-files --type target --full --all "$cur*" \ |
99504dd4 | 303 | | { while read -r a b; do echo " $a"; done; } ) |
d611dadc MB |
304 | fi |
305 | ||
c317a1a1 | 306 | COMPREPLY=( $(compgen -o filenames -W '$comps' -- "$cur") ) |
d611dadc MB |
307 | return 0 |
308 | } | |
309 | ||
310 | complete -F _systemctl systemctl |