1 # This file is part of systemd.
3 # Copyright 2010 Ran Benita
5 # systemd is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as published by
7 # the Free Software Foundation; either version 2.1 of the License, or
8 # (at your option) any later version.
10 # systemd is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 systemctl --full --no-legend "$@"
24 for w in $*; do [[ $w = $word ]] && return 0; done
28 __filter_units_by_property () {
29 local property=$1 value=$2 ; shift 2
32 IFS=$'\n' read -rd '' -a props < \
33 <(__systemctl show --property "$property" -- "${units[@]}")
34 for ((i=0; $i < ${#units[*]}; i++)); do
35 if [[ "${props[i]}" = "$property=$value" ]]; then
36 printf "%s\n" "${units[i]}"
41 __get_all_units () { __systemctl list-units --all \
42 | { while read -r a b; do printf "%s\n" "$a"; done; }; }
43 __get_active_units () { __systemctl list-units \
44 | { while read -r a b; do printf "%s\n" "$a"; done; }; }
45 __get_inactive_units () { __systemctl list-units --all \
46 | { while read -r a b c d; do [[ $c == "inactive" ]] && printf "%s\n" "$a"; done; }; }
47 __get_failed_units () { __systemctl list-units \
48 | { while read -r a b c d; do [[ $c == "failed" ]] && printf "%s\n" "$a"; done; }; }
49 __get_enabled_units () { __systemctl list-unit-files \
50 | { while read -r a b c ; do [[ $b == "enabled" ]] && printf "%s\n" "$a"; done; }; }
51 __get_disabled_units () { __systemctl list-unit-files \
52 | { while read -r a b c ; do [[ $b == "disabled" ]] && printf "%s\n" "$a"; done; }; }
53 __get_masked_units () { __systemctl list-unit-files \
54 | { while read -r a b c ; do [[ $b == "masked" ]] && printf "%s\n" "$a"; done; }; }
57 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
61 [STANDALONE]='--all -a --defaults --fail --ignore-dependencies --failed --force -f --full --global
62 --help -h --no-ask-password --no-block --no-legend --no-pager --no-reload --no-wall
63 --quiet -q --privileged -P --system --user --version --runtime'
64 [ARG]='--host -H --kill-mode --kill-who --property -p --signal -s --type -t --root'
67 if __contains_word "$prev" ${OPTS[ARG]}; then
70 comps=$(compgen -A signal)
73 comps='automount device mount path service snapshot socket swap target timer'
76 comps='all control main'
79 comps='control-group process'
82 comps=$(compgen -A directory -- "$cur" )
86 comps=$(compgen -A hostname)
92 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
97 if [[ "$cur" = -* ]]; then
98 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
103 [ALL_UNITS]='is-active is-failed is-enabled status show mask preset'
104 [ENABLED_UNITS]='disable reenable'
105 [DISABLED_UNITS]='enable'
106 [FAILED_UNITS]='reset-failed'
107 [STARTABLE_UNITS]='start'
108 [STOPPABLE_UNITS]='stop condstop kill try-restart condrestart'
109 [ISOLATABLE_UNITS]='isolate'
110 [RELOADABLE_UNITS]='reload condreload reload-or-try-restart force-reload'
111 [RESTARTABLE_UNITS]='restart reload-or-restart'
112 [MASKED_UNITS]='unmask'
115 [ENVS]='set-environment unset-environment'
116 [STANDALONE]='daemon-reexec daemon-reload default dump
117 emergency exit halt hibernate hybrid-sleep kexec list-jobs
118 list-units list-unit-files poweroff reboot rescue
119 show-environment suspend'
120 [NAME]='snapshot load'
124 for ((i=0; $i <= $COMP_CWORD; i++)); do
125 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
126 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
127 verb=${COMP_WORDS[i]}
132 if [[ -z $verb ]]; then
135 elif __contains_word "$verb" ${VERBS[ALL_UNITS]}; then
136 comps=$( __get_all_units )
138 elif __contains_word "$verb" ${VERBS[ENABLED_UNITS]}; then
139 comps=$( __get_enabled_units )
141 elif __contains_word "$verb" ${VERBS[DISABLED_UNITS]}; then
142 comps=$( __get_disabled_units )
144 elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then
145 comps=$( __filter_units_by_property CanStart yes \
146 $( __get_inactive_units \
147 | while read -r line; do \
148 [[ "$line" =~ \.(device|snapshot)$ ]] || printf "%s\n" "$line"; \
151 elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then
152 comps=$( __filter_units_by_property CanStart yes \
154 | while read -r line; do \
155 [[ "$line" =~ \.(device|snapshot|socket|timer)$ ]] || printf "%s\n" "$line"; \
158 elif __contains_word "$verb" ${VERBS[STOPPABLE_UNITS]}; then
159 comps=$( __filter_units_by_property CanStop yes \
160 $( __get_active_units ) )
162 elif __contains_word "$verb" ${VERBS[RELOADABLE_UNITS]}; then
163 comps=$( __filter_units_by_property CanReload yes \
164 $( __get_active_units ) )
166 elif __contains_word "$verb" ${VERBS[ISOLATABLE_UNITS]}; then
167 comps=$( __filter_units_by_property AllowIsolate yes \
168 $( __get_all_units ) )
170 elif __contains_word "$verb" ${VERBS[FAILED_UNITS]}; then
171 comps=$( __get_failed_units )
173 elif __contains_word "$verb" ${VERBS[MASKED_UNITS]}; then
174 comps=$( __get_masked_units )
176 elif __contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[NAME]}; then
179 elif __contains_word "$verb" ${VERBS[JOBS]}; then
180 comps=$( __systemctl list-jobs | { while read -r a b; do printf "%s\n" "$a"; done; } )
182 elif __contains_word "$verb" ${VERBS[SNAPSHOTS]}; then
183 comps=$( __systemctl list-units --type snapshot --full --all \
184 | { while read -r a b; do printf "%s\n" "$a"; done; } )
186 elif __contains_word "$verb" ${VERBS[ENVS]}; then
187 comps=$( __systemctl show-environment \
188 | while read -r line; do printf "%s\n" "${line%%=*}=";done )
191 elif __contains_word "$verb" ${VERBS[FILE]}; then
192 comps=$( compgen -A file -- "$cur" )
196 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
199 complete -F _systemctl systemctl
201 __get_all_sessions () { loginctl list-sessions | { while read -r a b; do printf "%s\n" "$a"; done; } ; }
202 __get_all_users () { loginctl list-users | { while read -r a b; do printf "%s\n" "$b"; done; } ; }
203 __get_all_seats () { loginctl list-seats | { while read -r a b; do printf "%s\n" "$a"; done; } ; }
206 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
210 [STANDALONE]='--all -a --help -h --no-pager --privileged -P --version'
211 [ARG]='--host -H --kill-who --property -p --signal -s'
214 if __contains_word "$prev" ${OPTS[ARG]}; then
217 comps=$(compgen -A signal)
223 comps=$(compgen -A hostname)
229 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
234 if [[ "$cur" = -* ]]; then
235 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
240 [SESSIONS]='session-status show-session activate lock-session unlock-session terminate-session kill-session'
241 [USERS]='user-status show-user enable-linger disable-linger terminate-user kill-user'
242 [SEATS]='seat-status show-seat terminate-seat'
243 [STANDALONE]='list-sessions list-users list-seats flush-devices'
247 for ((i=0; $i <= $COMP_CWORD; i++)); do
248 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
249 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
250 verb=${COMP_WORDS[i]}
255 if [[ -z $verb ]]; then
258 elif __contains_word "$verb" ${VERBS[SESSIONS]}; then
259 comps=$( __get_all_sessions )
261 elif __contains_word "$verb" ${VERBS[USERS]}; then
262 comps=$( __get_all_users )
264 elif __contains_word "$verb" ${VERBS[SEATS]}; then
265 comps=$( __get_all_seats )
267 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
270 elif __contains_word "$verb" ${VERBS[ATTACH]}; then
271 if [[ $prev = $verb ]]; then
272 comps=$( __get_all_seats )
274 comps=$(compgen -A file -- "$cur" )
279 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
282 complete -F _loginctl loginctl
284 __journal_fields=(MESSAGE{,_ID} PRIORITY CODE_{FILE,LINE,FUNC}
285 ERRNO SYSLOG_{FACILITY,IDENTIFIER,PID} COREDUMP_EXE
286 _{P,U,G}ID _COMM _EXE _CMDLINE
287 _AUDIT_{SESSION,LOGINUID}
288 _SYSTEMD_{CGROUP,SESSION,UNIT,OWNER_UID}
289 _SELINUX_CONTEXT _SOURCE_REALTIME_TIMESTAMP
290 _{BOOT,MACHINE}_ID _HOSTNAME _TRANSPORT
291 _KERNEL_{DEVICE,SUBSYSTEM}
292 _UDEV_{SYSNAME,DEVNODE,DEVLINK}
293 __CURSOR __{REALTIME,MONOTONIC}_TIMESTAMP)
296 local field_vals= cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
298 [STANDALONE]='-a --all --full
299 -b --this-boot --disk-usage -f --follow --header
300 -h --help -l --local --new-id128 -m --merge --no-pager
301 --no-tail -q --quiet --setup-keys --this-boot --verify
302 --version --list-catalog --update-catalog'
303 [ARG]='-D --directory -F --field -o --output -u --unit --user-unit'
304 [ARGUNKNOWN]='-c --cursor --interval -n --lines -p --priority --since --until
308 if __contains_word "$prev" ${OPTS[ARG]} ${OPTS[ARGUNKNOWN]}; then
311 comps=$(compgen -d -- "$cur")
315 comps='short short-monotonic verbose export json cat'
318 comps=${__journal_fields[*]}
321 comps=$(journalctl -F '_SYSTEMD_UNIT')
324 comps=$(journalctl -F '_SYSTEMD_USER_UNIT')
330 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
334 if [[ $cur = -* ]]; then
335 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
337 elif [[ $cur = *=* ]]; then
338 mapfile -t field_vals < <(journalctl -F "${prev%=}" 2>/dev/null)
339 COMPREPLY=( $(compgen -W '${field_vals[*]}' -- "${cur#=}") )
340 elif [[ $cur = /dev* ]]; then
342 COMPREPLY=( $(compgen -f -- "${cur}") )
343 elif [[ $cur = /* ]]; then
344 # Append /dev/ to the list of completions, so that
345 # after typing /<TAB><TAB> the user sees /dev/ as one
346 # of the alternatives. Later on the rule above will
347 # take care of showing device files in /dev/.
348 mapfile -t field_vals < <(journalctl -F "_EXE" 2>/dev/null; echo '/dev/')
349 COMPREPLY=( $(compgen -W '${field_vals[*]}' -- "${cur}") )
350 if [[ "${COMPREPLY[@]}" = '/dev/' ]]; then
352 COMPREPLY=( $(compgen -f -- "${cur}") )
354 elif [[ $prev = '=' ]]; then
355 mapfile -t field_vals < <(journalctl -F "${COMP_WORDS[COMP_CWORD-2]}" 2>/dev/null)
356 COMPREPLY=( $(compgen -W '${field_vals[*]}' -- "$cur") )
359 COMPREPLY=( $(compgen -W '${__journal_fields[*]}' -S= -- "$cur") )
362 complete -F _journalctl journalctl
366 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
367 local OPTS='-h --help --version --no-pager --no-legend -o --output -F --field'
374 if __contains_word "$prev" '--output -o'; then
375 comps=$( compgen -A file -- "$cur" )
377 elif __contains_word "$prev" '--FIELD -F'; then
378 comps=$( compgen -W '${__journal_fields[*]}' -- "$cur" )
379 elif [[ $cur = -* ]]; then
381 elif __contains_word "$prev" ${VERBS[*]} &&
382 ! __contains_word ${COMP_WORDS[COMP_CWORD-2]} '--output -o -F --field'; then
384 COMPREPLY=( $(compgen -W '${__journal_fields[*]}' -S= -- "$cur") )
386 elif [[ $cur = *=* ]]; then
387 mapfile -t field_vals < <(systemd-coredumpctl -F "${prev%=}" 2>/dev/null)
388 COMPREPLY=( $(compgen -W '${field_vals[*]}' -- "${cur#=}") )
390 elif [[ $prev = '=' ]]; then
391 mapfile -t field_vals < <(systemd-coredumpctl -F "${COMP_WORDS[COMP_CWORD-2]}" 2>/dev/null)
392 comps=${field_vals[*]}
394 for ((i=0; i <= COMP_CWORD; i++)); do
395 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]}; then
396 verb=${COMP_WORDS[i]}
401 if [[ -z $verb ]]; then
403 elif __contains_word "$verb" ${VERBS[LIST]} ${VERBS[DUMP]}; then
408 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
411 complete -F _coredumpctl systemd-coredumpctl
415 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
416 local OPTS='-h --help --version --adjust-system-clock --no-pager
417 --no-ask-password -H --host'
419 if __contains_word "$prev" $OPTS; then
425 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
429 if [[ $cur = -* ]]; then
430 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
435 [BOOLEAN]='set-local-rtc set-ntp'
436 [STANDALONE]='status set-time list-timezones'
437 [TIMEZONES]='set-timezone'
441 for ((i=0; i <= COMP_CWORD; i++)); do
442 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]}; then
443 verb=${COMP_WORDS[i]}
448 if [[ -z $verb ]]; then
450 elif __contains_word "$verb" ${VERBS[BOOLEAN]}; then
452 elif __contains_word "$verb" ${VERBS[TIMEZONES]}; then
453 comps=$(command timedatectl list-timezones)
454 elif __contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[TIME]}; then
458 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
461 complete -F _timedatectl timedatectl
465 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
466 local OPTS='-h --help --version --no-convert --no-pager --no-ask-password
469 if __contains_word "$prev" $OPTS; then
475 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
479 if [[ $cur = -* ]]; then
480 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
485 [STANDALONE]='status list-locales list-keymaps'
486 [LOCALES]='set-locale'
487 [KEYMAPS]='set-keymap'
488 [X11]='set-x11-keymap'
491 for ((i=0; i <= COMP_CWORD; i++)); do
492 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]}; then
493 verb=${COMP_WORDS[i]}
498 if [[ -z $verb ]]; then
500 elif __contains_word "$verb" ${VERBS[LOCALES]}; then
501 comps=$(command localectl list-locales)
502 elif __contains_word "$verb" ${VERBS[KEYMAPS]}; then
503 comps=$(command localectl list-keymaps)
504 elif __contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[X11]}; then
508 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
511 complete -F _localectl localectl
515 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
516 local OPTS='-h --help --version --transient --static --pretty
517 --no-ask-password -H --host'
519 if [[ $cur = -* ]]; then
520 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
525 [STANDALONE]='status'
526 [ICONS]='set-icon-name'
527 [NAME]='set-hostname'
530 for ((i=0; i <= COMP_CWORD; i++)); do
531 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]}; then
532 verb=${COMP_WORDS[i]}
537 if [[ -z $verb ]]; then
539 elif __contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[ICONS]} ${VERBS[NAME]}; then
543 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
546 complete -F _hostnamectl hostnamectl
548 __get_all_sysdevs() {
549 local -a devs=(/sys/bus/*/devices/*/ /sys/class/*/*/)
550 printf '%s\n' "${devs[@]%/}"
555 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
556 local OPTS='-h --help --version --debug'
565 [TESTBUILTIN]='test-builtin'
569 for ((i=0; $i <= $COMP_CWORD; i++)); do
570 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
571 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
572 verb=${COMP_WORDS[i]}
577 if [[ -z $verb && $cur = -* ]]; then
578 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
582 if [[ -z $verb ]]; then
585 elif __contains_word "$verb" ${VERBS[INFO]}; then
586 if [[ $cur = -* ]]; then
587 comps='--help --query= --path= --name= --root --attribute-walk --export-db --cleanup-db'
589 comps=$( __get_all_sysdevs )
592 elif __contains_word "$verb" ${VERBS[TRIGGER]}; then
593 comps='--help --verbose --dry-run --type= --action= --subsystem-match=
594 --subsystem-nomatch= --attr-match= --attr-nomatch= --property-match=
595 --tag-match= --sysname-match= --parent-match='
597 elif __contains_word "$verb" ${VERBS[SETTLE]}; then
598 comps='--help --timeout= --seq-start= --seq-end= --exit-if-exists= --quiet'
600 elif __contains_word "$verb" ${VERBS[CONTROL]}; then
601 comps='--help --exit --log-priority= --stop-exec-queue --start-exec-queue
602 --reload --property= --children-max= --timeout='
604 elif __contains_word "$verb" ${VERBS[MONITOR]}; then
605 comps='--help --kernel --udev --property --subsystem-match= --tag-match='
607 elif __contains_word "$verb" ${VERBS[HWDB]}; then
608 comps='--help --update --test='
610 elif __contains_word "$verb" ${VERBS[TEST]}; then
611 if [[ $cur = -* ]]; then
612 comps='--help --action='
614 comps=$( __get_all_sysdevs )
617 elif __contains_word "$verb" ${VERBS[TESTBUILTIN]}; then
618 comps='blkid btrfs firmware hwdb input_id kmod net_id path_id usb_id uaccess'
621 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
624 complete -F _udevadm udevadm