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