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