]>
Commit | Line | Data |
---|---|---|
d611dadc MB |
1 | # systemctl(1) completion -*- shell-script -*- |
2 | # | |
3 | # This file is part of systemd. | |
4 | # | |
5 | # Copyright 2010 Ran Benita | |
6 | # | |
7 | # systemd is free software; you can redistribute it and/or modify it | |
8 | # under the terms of the GNU Lesser General Public License as published by | |
9 | # the Free Software Foundation; either version 2.1 of the License, or | |
10 | # (at your option) any later version. | |
11 | # | |
12 | # systemd is distributed in the hope that it will be useful, but | |
13 | # WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | # General Public License for more details. | |
16 | # | |
17 | # You should have received a copy of the GNU Lesser General Public License | |
18 | # along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
19 | ||
20 | __systemctl() { | |
9e542e0b ZJS |
21 | local mode=$1; shift 1 |
22 | systemctl $mode --full --no-legend "$@" | |
d611dadc MB |
23 | } |
24 | ||
caffaf58 ZJS |
25 | __systemd_properties() { |
26 | local mode=$1 | |
c0a67aef ZJS |
27 | { __systemctl $mode show --all; |
28 | @rootlibexecdir@/systemd --dump-configuration-items; } | | |
caffaf58 ZJS |
29 | while IFS='=' read -r key value; do |
30 | [[ $value ]] && echo "$key" | |
31 | done | |
32 | } | |
33 | ||
d611dadc | 34 | __contains_word () { |
a72d698d DR |
35 | local w word=$1; shift |
36 | for w in "$@"; do | |
37 | [[ $w = "$word" ]] && return | |
38 | done | |
d611dadc MB |
39 | } |
40 | ||
41 | __filter_units_by_property () { | |
9e542e0b | 42 | local mode=$1 property=$2 value=$3 ; shift 3 |
d611dadc MB |
43 | local units=("$@") |
44 | local props | |
45 | IFS=$'\n' read -rd '' -a props < \ | |
9e542e0b | 46 | <(__systemctl $mode show --property "$property" -- "${units[@]}") |
d611dadc MB |
47 | for ((i=0; $i < ${#units[*]}; i++)); do |
48 | if [[ "${props[i]}" = "$property=$value" ]]; then | |
79c16383 | 49 | echo " ${units[i]}" |
d611dadc MB |
50 | fi |
51 | done | |
52 | } | |
53 | ||
c6a373a2 | 54 | __get_all_units () { { __systemctl $1 list-unit-files; __systemctl $1 list-units --all; } \ |
9ff8af54 | 55 | | { while read -r a b; do [[ $a =~ @\. ]] || echo " $a"; done; }; } |
e9a19bd8 ZJS |
56 | __get_template_names () { __systemctl $1 list-unit-files \ |
57 | | { while read -r a b; do [[ $a =~ @\. ]] && echo " ${a%%@.*}@"; done; }; } | |
58 | ||
9e542e0b | 59 | __get_active_units () { __systemctl $1 list-units \ |
79c16383 | 60 | | { while read -r a b; do echo " $a"; done; }; } |
f29c77bc | 61 | __get_startable_units () { |
9ff8af54 | 62 | # find startable inactive units |
372b2211 ZJS |
63 | __filter_units_by_property $mode ActiveState inactive $( |
64 | __filter_units_by_property $mode CanStart yes $( | |
65 | __systemctl $mode list-unit-files --state enabled,disabled,static | \ | |
66 | { while read -r a b; do [[ $a =~ @\. ]] || echo " $a"; done; } | |
67 | __systemctl $mode list-units --state inactive,failed | \ | |
68 | { while read -r a b; do echo " $a"; done; } )) | |
9ff8af54 | 69 | } |
f29c77bc | 70 | __get_restartable_units () { |
9ff8af54 | 71 | # filter out masked and not-found |
372b2211 ZJS |
72 | __filter_units_by_property $mode CanStart yes $( |
73 | __systemctl $mode list-unit-files --state enabled,disabled,static | \ | |
74 | { while read -r a b; do [[ $a =~ @\. ]] || echo " $a"; done; } | |
75 | __systemctl $mode list-units | \ | |
76 | { while read -r a b; do echo " $a"; done; } ) | |
9ff8af54 | 77 | } |
9e542e0b | 78 | __get_failed_units () { __systemctl $1 list-units \ |
79c16383 | 79 | | { while read -r a b c d; do [[ $c == "failed" ]] && echo " $a"; done; }; } |
9e542e0b | 80 | __get_enabled_units () { __systemctl $1 list-unit-files \ |
79c16383 | 81 | | { while read -r a b c ; do [[ $b == "enabled" ]] && echo " $a"; done; }; } |
9e542e0b | 82 | __get_disabled_units () { __systemctl $1 list-unit-files \ |
79c16383 | 83 | | { while read -r a b c ; do [[ $b == "disabled" ]] && echo " $a"; done; }; } |
9e542e0b | 84 | __get_masked_units () { __systemctl $1 list-unit-files \ |
79c16383 | 85 | | { while read -r a b c ; do [[ $b == "masked" ]] && echo " $a"; done; }; } |
8fc5cd71 | 86 | __get_all_unit_files () { { __systemctl $1 list-unit-files; } | { while read -r a b; do echo " $a"; done; }; } |
d611dadc | 87 | |
3a221b5d EV |
88 | __get_machines() { |
89 | local a b | |
4ed2f1b1 ZJS |
90 | { machinectl list-images --no-legend --no-pager; machinectl list --no-legend --no-pager; } | \ |
91 | { while read a b; do echo " $a"; done; } | |
3a221b5d EV |
92 | } |
93 | ||
d611dadc MB |
94 | _systemctl () { |
95 | local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} | |
9e542e0b | 96 | local i verb comps mode |
d611dadc MB |
97 | |
98 | local -A OPTS=( | |
903e7c37 | 99 | [STANDALONE]='--all -a --reverse --after --before --defaults --failed --force -f --full -l --global |
d611dadc | 100 | --help -h --no-ask-password --no-block --no-legend --no-pager --no-reload --no-wall |
3a221b5d EV |
101 | --quiet -q --privileged -P --system --user --version --runtime --recursive -r --firmware-setup |
102 | --show-types -i --ignore-inhibitors --plain' | |
103 | [ARG]='--host -H --kill-who --property -p --signal -s --type -t --state --job-mode --root | |
104 | --preset-mode -n --lines -o --output -M --machine' | |
d611dadc MB |
105 | ) |
106 | ||
caffaf58 ZJS |
107 | if __contains_word "--user" ${COMP_WORDS[*]}; then |
108 | mode=--user | |
93557fcb EV |
109 | elif __contains_word "--global" ${COMP_WORDS[*]}; then |
110 | mode=--user | |
caffaf58 ZJS |
111 | else |
112 | mode=--system | |
113 | fi | |
114 | ||
d611dadc MB |
115 | if __contains_word "$prev" ${OPTS[ARG]}; then |
116 | case $prev in | |
117 | --signal|-s) | |
118 | comps=$(compgen -A signal) | |
119 | ;; | |
120 | --type|-t) | |
adb0c25e | 121 | comps=$(__systemctl $mode -t help) |
d611dadc | 122 | ;; |
631c922c | 123 | --state) |
840b2c0e | 124 | comps=$(__systemctl $mode --state=help) |
631c922c | 125 | ;; |
903e7c37 ZJS |
126 | --job-mode) |
127 | comps='fail replace replace-irreversibly isolate | |
128 | ignore-dependencies ignore-requirements flush' | |
129 | ;; | |
d611dadc MB |
130 | --kill-who) |
131 | comps='all control main' | |
132 | ;; | |
d611dadc MB |
133 | --root) |
134 | comps=$(compgen -A directory -- "$cur" ) | |
135 | compopt -o filenames | |
136 | ;; | |
137 | --host|-H) | |
138 | comps=$(compgen -A hostname) | |
139 | ;; | |
140 | --property|-p) | |
caffaf58 | 141 | comps=$(__systemd_properties $mode) |
d611dadc | 142 | ;; |
3a221b5d EV |
143 | --preset-mode) |
144 | comps='full enable-only disable-only' | |
145 | ;; | |
146 | --output|-o) | |
147 | comps='short short-iso short-precise short-monotonic verbose export json | |
148 | json-pretty json-sse cat' | |
149 | ;; | |
150 | --machine|-M) | |
151 | comps=$( __get_machines ) | |
152 | ;; | |
d611dadc MB |
153 | esac |
154 | COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) | |
155 | return 0 | |
156 | fi | |
157 | ||
d611dadc MB |
158 | if [[ "$cur" = -* ]]; then |
159 | COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") ) | |
160 | return 0 | |
161 | fi | |
162 | ||
163 | local -A VERBS=( | |
d82fabdb | 164 | [ALL_UNITS]='is-active is-failed is-enabled status show cat mask preset help list-dependencies edit set-property' |
c2e09812 | 165 | [ENABLED_UNITS]='disable' |
d611dadc | 166 | [DISABLED_UNITS]='enable' |
c2e09812 | 167 | [REENABLABLE_UNITS]='reenable' |
d611dadc MB |
168 | [FAILED_UNITS]='reset-failed' |
169 | [STARTABLE_UNITS]='start' | |
170 | [STOPPABLE_UNITS]='stop condstop kill try-restart condrestart' | |
171 | [ISOLATABLE_UNITS]='isolate' | |
172 | [RELOADABLE_UNITS]='reload condreload reload-or-try-restart force-reload' | |
173 | [RESTARTABLE_UNITS]='restart reload-or-restart' | |
8fc5cd71 | 174 | [TARGET_AND_UNITS]='add-wants add-requires' |
d611dadc MB |
175 | [MASKED_UNITS]='unmask' |
176 | [JOBS]='cancel' | |
d611dadc | 177 | [ENVS]='set-environment unset-environment' |
299c397c | 178 | [STANDALONE]='daemon-reexec daemon-reload default |
d611dadc | 179 | emergency exit halt hibernate hybrid-sleep kexec list-jobs |
d1811159 | 180 | list-sockets list-timers list-units list-unit-files poweroff |
1cf3c30c ZJS |
181 | reboot rescue show-environment suspend get-default |
182 | is-system-running' | |
7b742b31 | 183 | [FILE]='link switch-root' |
99504dd4 | 184 | [TARGETS]='set-default' |
d611dadc MB |
185 | ) |
186 | ||
3ce09b7d | 187 | for ((i=0; i < COMP_CWORD; i++)); do |
d611dadc MB |
188 | if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} && |
189 | ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then | |
190 | verb=${COMP_WORDS[i]} | |
191 | break | |
192 | fi | |
193 | done | |
194 | ||
86cb0691 | 195 | if [[ -z $verb ]]; then |
d611dadc MB |
196 | comps="${VERBS[*]}" |
197 | ||
198 | elif __contains_word "$verb" ${VERBS[ALL_UNITS]}; then | |
9e542e0b | 199 | comps=$( __get_all_units $mode ) |
c317a1a1 | 200 | compopt -o filenames |
d611dadc MB |
201 | |
202 | elif __contains_word "$verb" ${VERBS[ENABLED_UNITS]}; then | |
9e542e0b | 203 | comps=$( __get_enabled_units $mode ) |
c317a1a1 | 204 | compopt -o filenames |
d611dadc MB |
205 | |
206 | elif __contains_word "$verb" ${VERBS[DISABLED_UNITS]}; then | |
e9a19bd8 ZJS |
207 | comps=$( __get_disabled_units $mode; |
208 | __get_template_names $mode) | |
c317a1a1 | 209 | compopt -o filenames |
d611dadc | 210 | |
c2e09812 ZJS |
211 | elif __contains_word "$verb" ${VERBS[REENABLABLE_UNITS]}; then |
212 | comps=$( __get_disabled_units $mode; | |
e9a19bd8 ZJS |
213 | __get_enabled_units $mode; |
214 | __get_template_names $mode) | |
c317a1a1 | 215 | compopt -o filenames |
c2e09812 | 216 | |
d611dadc | 217 | elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then |
9ff8af54 ZJS |
218 | comps=$( __get_startable_units $mode; |
219 | __get_template_names $mode) | |
c317a1a1 | 220 | compopt -o filenames |
d611dadc MB |
221 | |
222 | elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then | |
9ff8af54 ZJS |
223 | comps=$( __get_restartable_units $mode; |
224 | __get_template_names $mode) | |
c317a1a1 | 225 | compopt -o filenames |
d611dadc MB |
226 | |
227 | elif __contains_word "$verb" ${VERBS[STOPPABLE_UNITS]}; then | |
9e542e0b ZJS |
228 | comps=$( __filter_units_by_property $mode CanStop yes \ |
229 | $( __get_active_units $mode ) ) | |
c317a1a1 | 230 | compopt -o filenames |
d611dadc MB |
231 | |
232 | elif __contains_word "$verb" ${VERBS[RELOADABLE_UNITS]}; then | |
9e542e0b ZJS |
233 | comps=$( __filter_units_by_property $mode CanReload yes \ |
234 | $( __get_active_units $mode ) ) | |
c317a1a1 | 235 | compopt -o filenames |
d611dadc MB |
236 | |
237 | elif __contains_word "$verb" ${VERBS[ISOLATABLE_UNITS]}; then | |
9e542e0b ZJS |
238 | comps=$( __filter_units_by_property $mode AllowIsolate yes \ |
239 | $( __get_all_units $mode ) ) | |
c317a1a1 | 240 | compopt -o filenames |
d611dadc MB |
241 | |
242 | elif __contains_word "$verb" ${VERBS[FAILED_UNITS]}; then | |
9e542e0b | 243 | comps=$( __get_failed_units $mode ) |
c317a1a1 | 244 | compopt -o filenames |
d611dadc MB |
245 | |
246 | elif __contains_word "$verb" ${VERBS[MASKED_UNITS]}; then | |
9e542e0b | 247 | comps=$( __get_masked_units $mode ) |
c317a1a1 | 248 | compopt -o filenames |
d611dadc | 249 | |
8fc5cd71 LN |
250 | elif __contains_word "$verb" ${VERBS[TARGET_AND_UNITS]}; then |
251 | if __contains_word "$prev" ${VERBS[TARGET_AND_UNITS]} \ | |
252 | || __contains_word "$prev" ${OPTS[STANDALONE]}; then | |
253 | comps=$( __systemctl $mode list-unit-files --type target --all \ | |
254 | | { while read -r a b; do echo " $a"; done; } ) | |
255 | else | |
256 | comps=$( __get_all_unit_files $mode ) | |
257 | fi | |
258 | compopt -o filenames | |
259 | ||
36b4a7ba | 260 | elif __contains_word "$verb" ${VERBS[STANDALONE]}; then |
d611dadc MB |
261 | comps='' |
262 | ||
263 | elif __contains_word "$verb" ${VERBS[JOBS]}; then | |
79c16383 | 264 | comps=$( __systemctl $mode list-jobs | { while read -r a b; do echo " $a"; done; } ) |
d611dadc | 265 | |
d611dadc | 266 | elif __contains_word "$verb" ${VERBS[ENVS]}; then |
9e542e0b | 267 | comps=$( __systemctl $mode show-environment \ |
79c16383 | 268 | | while read -r line; do echo " ${line%%=*}=";done ) |
d611dadc MB |
269 | compopt -o nospace |
270 | ||
271 | elif __contains_word "$verb" ${VERBS[FILE]}; then | |
272 | comps=$( compgen -A file -- "$cur" ) | |
273 | compopt -o filenames | |
99504dd4 VP |
274 | elif __contains_word "$verb" ${VERBS[TARGETS]}; then |
275 | comps=$( __systemctl $mode list-unit-files --type target --full --all \ | |
276 | | { while read -r a b; do echo " $a"; done; } ) | |
d611dadc MB |
277 | fi |
278 | ||
c317a1a1 | 279 | COMPREPLY=( $(compgen -o filenames -W '$comps' -- "$cur") ) |
d611dadc MB |
280 | return 0 |
281 | } | |
282 | ||
283 | complete -F _systemctl systemctl |