chiark / gitweb /
4398aaaf562481e3f7c077bfb9f69ef695456d36
[elogind.git] / bash-completion / systemd-bash-completion.sh
1 # This file is part of systemd.
2 #
3 # Copyright 2010 Ran Benita
4 #
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.
9 #
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.
14 #
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/>.
17
18 __systemctl() {
19         systemctl --full --no-legend "$@"
20 }
21
22 __contains_word () {
23         local word=$1; shift
24         for w in $*; do [[ $w = $word ]] && return 0; done
25         return 1
26 }
27
28 __filter_units_by_property () {
29         local property=$1 value=$2 ; shift 2
30         local units=("$@")
31         local props
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]}"
37                 fi
38         done
39 }
40
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; }; }
55
56 _systemctl () {
57         local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
58         local i verb comps
59
60         local -A OPTS=(
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                              --order --require --quiet -q --privileged -P --system --user --version --runtime'
64                       [ARG]='--host -H --kill-mode --kill-who --property -p --signal -s --type -t --root'
65         )
66
67         if __contains_word "$prev" ${OPTS[ARG]}; then
68                 case $prev in
69                         --signal|-s)
70                                 comps=$(compgen -A signal)
71                         ;;
72                         --type|-t)
73                                 comps='automount device mount path service snapshot socket swap target timer'
74                         ;;
75                         --kill-who)
76                                 comps='all control main'
77                         ;;
78                         --kill-mode)
79                                 comps='control-group process'
80                         ;;
81                         --root)
82                                 comps=$(compgen -A directory -- "$cur" )
83                                 compopt -o filenames
84                         ;;
85                         --host|-H)
86                                 comps=$(compgen -A hostname)
87                         ;;
88                         --property|-p)
89                                 comps=''
90                         ;;
91                 esac
92                 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
93                 return 0
94         fi
95
96
97         if [[ "$cur" = -* ]]; then
98                 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
99                 return 0
100         fi
101
102         local -A VERBS=(
103                 [ALL_UNITS]='is-active 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'
113                      [JOBS]='cancel'
114                 [SNAPSHOTS]='delete'
115                      [ENVS]='set-environment unset-environment'
116                [STANDALONE]='daemon-reexec daemon-reload default dot dump
117                              emergency exit halt kexec list-jobs list-units
118                              list-unit-files poweroff reboot rescue show-environment'
119                      [NAME]='snapshot load'
120                      [FILE]='link'
121         )
122
123         for ((i=0; $i <= $COMP_CWORD; i++)); do
124                 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
125                  ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
126                         verb=${COMP_WORDS[i]}
127                         break
128                 fi
129         done
130
131         if   [[ -z $verb ]]; then
132                 comps="${VERBS[*]}"
133
134         elif __contains_word "$verb" ${VERBS[ALL_UNITS]}; then
135                 comps=$( __get_all_units )
136
137         elif __contains_word "$verb" ${VERBS[ENABLED_UNITS]}; then
138                 comps=$( __get_enabled_units )
139
140         elif __contains_word "$verb" ${VERBS[DISABLED_UNITS]}; then
141                 comps=$( __get_disabled_units )
142
143         elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then
144                 comps=$( __filter_units_by_property CanStart yes \
145                       $( __get_inactive_units \
146                         | while read -r line; do \
147                                 [[ "$line" =~ \.(device|snapshot)$ ]] || printf "%s\n" "$line"; \
148                         done ))
149
150         elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then
151                 comps=$( __filter_units_by_property CanStart yes \
152                       $( __get_all_units \
153                         | while read -r line; do \
154                                 [[ "$line" =~ \.(device|snapshot|socket|timer)$ ]] || printf "%s\n" "$line"; \
155                         done ))
156
157         elif __contains_word "$verb" ${VERBS[STOPPABLE_UNITS]}; then
158                 comps=$( __filter_units_by_property CanStop yes \
159                       $( __get_active_units ) )
160
161         elif __contains_word "$verb" ${VERBS[RELOADABLE_UNITS]}; then
162                 comps=$( __filter_units_by_property CanReload yes \
163                       $( __get_active_units ) )
164
165         elif __contains_word "$verb" ${VERBS[ISOLATABLE_UNITS]}; then
166                 comps=$( __filter_units_by_property AllowIsolate yes \
167                       $( __get_all_units ) )
168
169         elif __contains_word "$verb" ${VERBS[FAILED_UNITS]}; then
170                 comps=$( __get_failed_units )
171
172         elif __contains_word "$verb" ${VERBS[MASKED_UNITS]}; then
173                 comps=$( __get_masked_units )
174
175         elif __contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[NAME]}; then
176                 comps=''
177
178         elif __contains_word "$verb" ${VERBS[JOBS]}; then
179                 comps=$( __systemctl list-jobs | { while read -r a b; do printf "%s\n" "$a"; done; } )
180
181         elif __contains_word "$verb" ${VERBS[SNAPSHOTS]}; then
182                 comps=$( __systemctl list-units --type snapshot --full --all \
183                         | { while read -r a b; do printf "%s\n" "$a"; done; } )
184
185         elif __contains_word "$verb" ${VERBS[ENVS]}; then
186                 comps=$( __systemctl show-environment \
187                     | while read -r line; do printf "%s\n" "${line%%=*}=";done )
188                 compopt -o nospace
189
190         elif __contains_word "$verb" ${VERBS[FILE]}; then
191                 comps=$( compgen -A file -- "$cur" )
192                 compopt -o filenames
193         fi
194
195         COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
196         return 0
197 }
198 complete -F _systemctl systemctl
199
200 __get_all_sessions () { loginctl list-sessions | { while read -r a b; do printf "%s\n" "$a"; done; } ; }
201 __get_all_users    () { loginctl list-users    | { while read -r a b; do printf "%s\n" "$b"; done; } ; }
202 __get_all_seats    () { loginctl list-seats    | { while read -r a b; do printf "%s\n" "$a"; done; } ; }
203
204 _loginctl () {
205         local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
206         local i verb comps
207
208         local -A OPTS=(
209                [STANDALONE]='--all -a --help -h --no-pager --privileged -P --version'
210                       [ARG]='--host -H --kill-who --property -p --signal -s'
211         )
212
213         if __contains_word "$prev" ${OPTS[ARG]}; then
214                 case $prev in
215                         --signal|-s)
216                                 comps=$(compgen -A signal)
217                         ;;
218                         --kill-who)
219                                 comps='all leader'
220                         ;;
221                         --host|-H)
222                                 comps=$(compgen -A hostname)
223                         ;;
224                         --property|-p)
225                                 comps=''
226                         ;;
227                 esac
228                 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
229                 return 0
230         fi
231
232
233         if [[ "$cur" = -* ]]; then
234                 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
235                 return 0
236         fi
237
238         local -A VERBS=(
239                 [SESSIONS]='session-status show-session activate lock-session unlock-session terminate-session kill-session'
240                 [USERS]='user-status show-user enable-linger disable-linger terminate-user kill-user'
241                 [SEATS]='seat-status show-seat terminate-seat'
242                 [STANDALONE]='list-sessions list-users list-seats flush-devices'
243                 [ATTACH]='attach'
244         )
245
246         for ((i=0; $i <= $COMP_CWORD; i++)); do
247                 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
248                  ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
249                         verb=${COMP_WORDS[i]}
250                         break
251                 fi
252         done
253
254         if   [[ -z $verb ]]; then
255                 comps="${VERBS[*]}"
256
257         elif __contains_word "$verb" ${VERBS[SESSIONS]}; then
258                 comps=$( __get_all_sessions )
259
260         elif __contains_word "$verb" ${VERBS[USERS]}; then
261                 comps=$( __get_all_users )
262
263         elif __contains_word "$verb" ${VERBS[SEATS]}; then
264                 comps=$( __get_all_seats )
265
266         elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
267                 comps=''
268
269         elif __contains_word "$verb" ${VERBS[ATTACH]}; then
270                 if [[ $prev = $verb ]]; then
271                         comps=$( __get_all_seats )
272                 else
273                         comps=$(compgen -A file -- "$cur" )
274                         compopt -o filenames
275                 fi
276         fi
277
278         COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
279         return 0
280 }
281 complete -F _loginctl loginctl
282
283 _journalctl() {
284         local field_vals= cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
285         local -A OPTS=(
286                 [STANDALONE]='-a --all -b --this-boot --disk-usage -f --follow --header
287                               -h --help -l --local --new-id128 -m --merge --no-pager
288                               --no-tail -q --quiet --setup-keys --this-boot --verify
289                               --version'
290                        [ARG]='-D --directory -F --field -o --output -u --unit'
291                 [ARGUNKNOWN]='-c --cursor --interval -n --lines -p --priority --since --until
292                               --verify-key'
293         )
294         local journal_fields=(MESSAGE{,_ID} PRIORITY CODE_{FILE,LINE,FUNC}
295                               ERRNO SYSLOG_{FACILITY,IDENTIFIER,PID}
296                               _{P,U,G}ID _COMM _EXE _CMDLINE
297                               _AUDIT_{SESSION,LOGINUID}
298                               _SYSTEMD_{CGROUP,SESSION,UNIT,OWNER_UID}
299                               _SELINUX_CONTEXT _SOURCE_REALTIME_TIMESTAMP
300                               _{BOOT,MACHINE}_ID _HOSTNAME _TRANSPORT
301                               _KERNEL_{DEVICE,SUBSYSTEM}
302                               _UDEV_{SYSNAME,DEVNODE,DEVLINK}
303                               __CURSOR __{REALTIME,MONOTONIC}_TIMESTAMP)
304
305
306         if __contains_word "$prev" ${OPTS[ARG]} ${OPTS[ARGUNKNOWN]}; then
307                 case $prev in
308                         --directory|-D)
309                                 comps=$(compgen -d -- "$cur")
310                                 compopt -o filenames
311                         ;;
312                         --output|-o)
313                                 comps='short short-monotonic verbose export json cat'
314                         ;;
315                         --field|-F)
316                                 comps=${journal_fields[*]}
317                         ;;
318                         --unit|-u)
319                                 comps=$(journalctl -F '_SYSTEMD_UNIT')
320                         ;;
321                         *)
322                                 return 0
323                         ;;
324                 esac
325                 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
326                 return 0
327         fi
328
329         if [[ $cur = -* ]]; then
330                 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
331                 return 0
332         elif [[ $cur = *=* ]]; then
333                 mapfile -t field_vals < <(journalctl -F "${prev%=}" 2>/dev/null)
334                 COMPREPLY=( $(compgen -W '${field_vals[*]}' -- "${cur#=}") )
335         elif [[ $prev = '=' ]]; then
336                 mapfile -t field_vals < <(journalctl -F "${COMP_WORDS[COMP_CWORD-2]}" 2>/dev/null)
337                 COMPREPLY=( $(compgen -W '${field_vals[*]}' -- "$cur") )
338         else
339                 compopt -o nospace
340                 COMPREPLY=( $(compgen -W '${journal_fields[*]}' -S= -- "$cur") )
341         fi
342 }
343 complete -F _journalctl journalctl
344
345 _timedatectl() {
346         local i verb comps
347         local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
348         local OPTS='-h --help --version --adjust-system-clock --no-pager
349                     --no-ask-password -H --host'
350
351         if __contains_word "$prev" $OPTS; then
352                 case $prev in
353                         --host|-H)
354                                 comps=''
355                         ;;
356                 esac
357                 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
358                 return 0
359         fi
360
361         if [[ $cur = -* ]]; then
362                 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
363                 return 0
364         fi
365
366         local -A VERBS=(
367                   [BOOLEAN]='set-local-rtc set-ntp'
368                [STANDALONE]='status set-time list-timezones'
369                 [TIMEZONES]='set-timezone'
370                      [TIME]='set-time'
371         )
372
373         for ((i=0; i <= COMP_CWORD; i++)); do
374                 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]}; then
375                         verb=${COMP_WORDS[i]}
376                         break
377                 fi
378         done
379
380         if [[ -z $verb ]]; then
381                 comps=${VERBS[*]}
382         elif __contains_word "$verb" ${VERBS[BOOLEAN]}; then
383                 comps='true false'
384         elif __contains_word "$verb" ${VERBS[TIMEZONES]}; then
385                 comps=$(command timedatectl list-timezones)
386         elif __contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[TIME]}; then
387                 comps=''
388         fi
389
390         COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
391         return 0
392 }
393 complete -F _timedatectl timedatectl
394
395 _localectl() {
396         local i verb comps
397         local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
398         local OPTS='-h --help --version --no-convert --no-pager --no-ask-password
399                     -H --host'
400
401         if __contains_word "$prev" $OPTS; then
402                 case $prev in
403                         --host|-H)
404                                 comps=''
405                         ;;
406                 esac
407                 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
408                 return 0
409         fi
410
411         if [[ $cur = -* ]]; then
412                 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
413                 return 0
414         fi
415
416         local -A VERBS=(
417                [STANDALONE]='status list-locales list-keymaps'
418                   [LOCALES]='set-locale'
419                   [KEYMAPS]='set-keymap'
420                       [X11]='set-x11-keymap'
421         )
422
423         for ((i=0; i <= COMP_CWORD; i++)); do
424                 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]}; then
425                         verb=${COMP_WORDS[i]}
426                         break
427                 fi
428         done
429
430         if [[ -z $verb ]]; then
431                 comps=${VERBS[*]}
432         elif __contains_word "$verb" ${VERBS[LOCALES]}; then
433                 comps=$(command localectl list-locales)
434         elif __contains_word "$verb" ${VERBS[KEYMAPS]}; then
435                 comps=$(command localectl list-keymaps)
436         elif __contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[X11]}; then
437                 comps=''
438         fi
439
440         COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
441         return 0
442 }
443 complete -F _localectl localectl
444
445 _hostnamectl() {
446         local i verb comps
447         local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
448         local OPTS='-h --help --version --transient --static --pretty
449                     --no-ask-password -H --host'
450
451         if [[ $cur = -* ]]; then
452                 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
453                 return 0
454         fi
455
456         local -A VERBS=(
457                 [STANDALONE]='status'
458                      [ICONS]='set-icon-name'
459                       [NAME]='set-hostname'
460         )
461
462         for ((i=0; i <= COMP_CWORD; i++)); do
463                 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]}; then
464                         verb=${COMP_WORDS[i]}
465                         break
466                 fi
467         done
468
469         if [[ -z $verb ]]; then
470                 comps=${VERBS[*]}
471         elif __contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[ICONS]} ${VERBS[NAME]}; then
472                 comps=''
473         fi
474
475         COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
476         return 0
477 }
478 complete -F _hostnamectl hostnamectl
479
480 __get_all_sysdevs() { ls -1 -d /sys/bus/*/devices/* /sys/class/*/*; }
481
482 _udevadm() {
483         local i verb comps
484         local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
485         local OPTS='-h --help --version --debug'
486
487         local -A VERBS=(
488                 [INFO]='info'
489                 [TRIGGER]='trigger'
490                 [SETTLE]='settle'
491                 [CONTROL]='control'
492                 [MONITOR]='monitor'
493                 [HWDB]='hwdb'
494                 [TESTBUILTIN]='test-builtin'
495                 [TEST]='test'
496         )
497
498         for ((i=0; $i <= $COMP_CWORD; i++)); do
499                 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
500                  ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
501                         verb=${COMP_WORDS[i]}
502                         break
503                 fi
504         done
505
506         if [[ -z $verb  && $cur = -* ]]; then
507                 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
508                 return 0
509         fi
510
511         if [[ -z $verb ]]; then
512                 comps=${VERBS[*]}
513
514         elif __contains_word "$verb" ${VERBS[INFO]}; then
515                 if [[ $cur = -* ]]; then
516                         comps='--help --query= --path= --name= --root --attribute-walk --export-db --cleanup-db'
517                 else
518                         comps=$( __get_all_sysdevs )
519                 fi
520
521         elif __contains_word "$verb" ${VERBS[TRIGGER]}; then
522                 comps='--help --verbose --dry-run --type= --action= --subsystem-match=
523                        --subsystem-nomatch= --attr-match= --attr-nomatch= --property-match=
524                        --tag-match= --sysname-match= --parent-match='
525
526         elif __contains_word "$verb" ${VERBS[SETTLE]}; then
527                 comps='--help --timeout= --seq-start= --seq-end= --exit-if-exists= --quiet'
528
529         elif __contains_word "$verb" ${VERBS[CONTROL]}; then
530                 comps='--help --exit --log-priority= --stop-exec-queue --start-exec-queue
531                        --reload --property= --children-max= --timeout='
532
533         elif __contains_word "$verb" ${VERBS[MONITOR]}; then
534                 comps='--help --kernel --udev --property --subsystem-match= --tag-match='
535
536         elif __contains_word "$verb" ${VERBS[HWDB]}; then
537                 comps='--help --update --test='
538
539         elif __contains_word "$verb" ${VERBS[TEST]}; then
540                 if [[ $cur = -* ]]; then
541                         comps='--help --action='
542                 else
543                         comps=$( __get_all_sysdevs )
544                 fi
545
546         elif __contains_word "$verb" ${VERBS[TESTBUILTIN]}; then
547                       comps='blkid btrfs firmware hwdb input_id kmod path_id usb_id uaccess'
548         fi
549
550         COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
551         return 0
552 }
553 complete -F _udevadm udevadm