chiark / gitweb /
journalctl: Add support for showing messages from a previous boot
[elogind.git] / shell-completion / bash / journalctl
1 # journalctl(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 __contains_word () {
21         local word=$1; shift
22         for w in $*; do [[ $w = $word ]] && return 0; done
23         return 1
24 }
25
26 __journal_fields=(MESSAGE{,_ID} PRIORITY CODE_{FILE,LINE,FUNC}
27                   ERRNO SYSLOG_{FACILITY,IDENTIFIER,PID} COREDUMP_EXE
28                   _{P,U,G}ID _COMM _EXE _CMDLINE
29                   _AUDIT_{SESSION,LOGINUID}
30                   _SYSTEMD_{CGROUP,SESSION,UNIT,OWNER_UID}
31                   _SELINUX_CONTEXT _SOURCE_REALTIME_TIMESTAMP
32                   _{BOOT,MACHINE}_ID _HOSTNAME _TRANSPORT
33                   _KERNEL_{DEVICE,SUBSYSTEM}
34                   _UDEV_{SYSNAME,DEVNODE,DEVLINK}
35                   __CURSOR __{REALTIME,MONOTONIC}_TIMESTAMP)
36
37 _journalctl() {
38         local field_vals= cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
39         local -A OPTS=(
40                 [STANDALONE]='-a --all --full --system --user
41                               --disk-usage -f --follow --header
42                               -h --help -l --local --new-id128 -m --merge --no-pager
43                               --no-tail -q --quiet --setup-keys --this-boot --verify
44                               --version --list-catalog --update-catalog'
45                        [ARG]='-b --boot --this-boot -D --directory -F --field
46                               -o --output -u --unit --user-unit'
47                 [ARGUNKNOWN]='-c --cursor --interval -n --lines -p --priority --since --until
48                               --verify-key'
49         )
50
51         if __contains_word "$prev" ${OPTS[ARG]} ${OPTS[ARGUNKNOWN]}; then
52                 case $prev in
53                         --boot|--this-boot|-b)
54                                 comps=$(journalctl -F '_BOOT_ID' 2>/dev/null)
55                         ;;
56                         --directory|-D)
57                                 comps=$(compgen -d -- "$cur")
58                                 compopt -o filenames
59                         ;;
60                         --output|-o)
61                                 comps='short short-monotonic verbose export json cat'
62                         ;;
63                         --field|-F)
64                                 comps=${__journal_fields[*]}
65                         ;;
66                         --unit|-u)
67                                 comps=$(journalctl -F '_SYSTEMD_UNIT' 2>/dev/null)
68                         ;;
69                         --user-unit)
70                                 comps=$(journalctl -F '_SYSTEMD_USER_UNIT' 2>/dev/null)
71                         ;;
72                         *)
73                                 return 0
74                         ;;
75                 esac
76                 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
77                 return 0
78         fi
79
80         if [[ $cur = -* ]]; then
81                 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
82                 return 0
83         elif [[ $cur = *=* ]]; then
84                 mapfile -t field_vals < <(journalctl -F "${prev%=}" 2>/dev/null)
85                 COMPREPLY=( $(compgen -W '${field_vals[*]}' -- "${cur#=}") )
86         elif [[ $cur = /dev* ]]; then
87                 compopt -o filenames
88                 COMPREPLY=( $(compgen -f -- "${cur}") )
89         elif [[ $cur = /* ]]; then
90                 # Append /dev/ to the list of completions, so that
91                 # after typing /<TAB><TAB> the user sees /dev/ as one
92                 # of the alternatives. Later on the rule above will
93                 # take care of showing device files in /dev/.
94                 mapfile -t field_vals < <(journalctl -F "_EXE" 2>/dev/null; echo '/dev/')
95                 COMPREPLY=( $(compgen -W '${field_vals[*]}' -- "${cur}") )
96                 if [[ "${COMPREPLY[@]}" = '/dev/' ]]; then
97                     compopt -o filenames
98                     COMPREPLY=( $(compgen -f -- "${cur}") )
99                 fi
100         elif [[ $prev = '=' ]]; then
101                 mapfile -t field_vals < <(journalctl -F "${COMP_WORDS[COMP_CWORD-2]}" 2>/dev/null)
102                 COMPREPLY=( $(compgen -W '${field_vals[*]}' -- "$cur") )
103         else
104                 compopt -o nospace
105                 COMPREPLY=( $(compgen -W '${__journal_fields[*]}' -S= -- "$cur") )
106         fi
107 }
108
109 complete -F _journalctl journalctl