Debian Codegraph @

shell-completion/bash/journalctl

raw
1 # shellcheck shell=bash
2 # journalctl(1) completion -*- shell-script -*-
3 # SPDX-License-Identifier: LGPL-2.1-or-later
4 #
5 # This file is part of systemd.
6 #
7 # Copyright © 2010 Ran Benita
8 #
9 # systemd is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU Lesser General Public License as published by
11 # the Free Software Foundation; either version 2.1 of the License, or
12 # (at your option) any later version.
13 #
14 # systemd is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # General Public License for more details.
18 #
19 # You should have received a copy of the GNU Lesser General Public License
20 # along with systemd; If not, see <https://www.gnu.org/licenses/>.
21
22 __contains_word () {
23 local w word=$1; shift
24 for w in "$@"; do
25 [[ $w = "$word" ]] && return
26 done
27 }
28
29 __get_machines() {
30 local a b
31 { machinectl list --full --max-addresses=0 --no-legend --no-pager 2>/dev/null; echo ".host"; } |
32 { while read -r a b; do echo " $a"; done; } |
33 sort -u
34 }
35
36 __syslog_priorities=(emerg alert crit err warning notice info debug)
37 __syslog_facilities=(kern user mail daemon auth syslog lpr news uucp cron authpriv ftp local0 local1 local2 local3 local4 local5 local6 local7)
38
39 _journalctl() {
40 local field_vals= cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
41 local -A OPTS=(
42 [STANDALONE]='-a --all --full --system --user
43 --disk-usage -f --follow --header
44 -h --help -l --local -m --merge --no-pager
45 --no-tail -q --quiet --setup-keys --verify
46 --version --list-catalog --update-catalog --list-boots
47 --show-cursor --dmesg -k --pager-end -e -r --reverse
48 --utc -x --catalog --no-full --force --dump-catalog
49 --flush --rotate --sync --relinquish-var
50 --smart-relinquish-var --no-hostname -W -N --fields
51 --list-namespaces --list-invocations -I -v --verbose'
52 [ARG]='-b --boot -D --directory -i --file -F --field -t --identifier
53 -T --exclude-identifier --facility -M --machine -o --output
54 -u --unit --user-unit -p --priority --root --case-sensitive
55 --namespace --invocation'
56 [ARGUNKNOWN]='-c --cursor --interval -n --lines -S --since -U --until
57 --after-cursor --cursor-file --verify-key -g --grep
58 --vacuum-size --vacuum-time --vacuum-files --output-fields'
59 )
60
61 # Use the default completion for shell redirect operators
62 if __contains_word "$prev" '>' '>>' '&>'; then
63 compopt -o filenames
64 COMPREPLY=( $(compgen -f -- "$cur") )
65 return 0
66 fi
67
68 # If the previous word looks like a short options group,
69 # handle it as if it were just the last short option.
70 # e.g. "-aefu" -> "-u"
71 if [[ "$prev" =~ ^-[a-zA-Z]{2,}$ ]]; then
72 prev="-${prev:(-1)}"
73 fi
74
75 if __contains_word "$prev" ${OPTS[ARG]} ${OPTS[ARGUNKNOWN]}; then
76 case $prev in
77 --boot|-b)
78 comps=$(journalctl -F '_BOOT_ID' 2>/dev/null)
79 ;;
80 --directory|-D|--root)
81 comps=$(compgen -d -- "$cur")
82 compopt -o filenames
83 ;;
84 --file|-i)
85 comps=$(compgen -f -- "$cur")
86 compopt -o filenames
87 ;;
88 --output|-o)
89 comps=$( journalctl --output=help 2>/dev/null )
90 ;;
91 --field|-F)
92 comps=$(journalctl --fields 2>/dev/null | sort)
93 ;;
94 --machine|-M)
95 comps=$( __get_machines )
96 ;;
97 --facility)
98 comps=${__syslog_facilities[*]}
99 ;;
100 --priority|-p)
101 comps=${__syslog_priorities[*]}
102 compopt -o nosort
103 ;;
104 --unit|-u)
105 if __contains_word "--user" ${COMP_WORDS[*]}; then
106 comps=$(journalctl -F '_SYSTEMD_USER_UNIT' 2>/dev/null)
107 else
108 comps=$(journalctl -F '_SYSTEMD_UNIT' 2>/dev/null)
109 fi
110 # Similarly to systemctl, we need to distinguish between
111 # escaped and unescaped names in order to be able to correctly
112 # complete them. In this particular case, if the name we're
113 # trying to complete is unescaped (i.e. foo\x2dbaz), escape
114 # it first, so the compgen below works as expected. For more
115 # information about these shenanigans see the systemctl
116 # completion file
117 if ! [[ $cur =~ '\\' ]]; then
118 cur="$(printf '%q' $cur)"
119 fi
120 compopt -o filenames
121 ;;
122 --user-unit)
123 comps=$(journalctl -F '_SYSTEMD_USER_UNIT' 2>/dev/null)
124 if ! [[ $cur =~ '\\' ]]; then
125 cur="$(printf '%q' $cur)"
126 fi
127 compopt -o filenames
128 ;;
129 --identifier|-t|--exclude-identifier|-T)
130 comps=$(journalctl -F 'SYSLOG_IDENTIFIER' 2>/dev/null)
131 ;;
132 --case-sensitive)
133 comps='yes no'
134 ;;
135 --namespace)
136 comps=$(journalctl --list-namespaces --output=cat 2>/dev/null)
137 ;;
138 *)
139 return 0
140 ;;
141 esac
142 COMPREPLY=( $(compgen -o filenames -W '$comps' -- "$cur") )
143 return 0
144 fi
145
146 if [[ $cur = -* ]]; then
147 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
148 return 0
149 elif [[ $cur = *=* ]]; then
150 mapfile -t field_vals < <(journalctl -F "${prev%=}" 2>/dev/null)
151 COMPREPLY=( $(compgen -W '${field_vals[*]}' -- "${cur#=}") )
152 elif [[ $cur = /dev* ]]; then
153 compopt -o filenames
154 COMPREPLY=( $(compgen -f -- "${cur}") )
155 elif [[ $cur = /* ]]; then
156 # Append /dev/ to the list of completions, so that
157 # after typing /<TAB><TAB> the user sees /dev/ as one
158 # of the alternatives. Later on the rule above will
159 # take care of showing device files in /dev/.
160 mapfile -t field_vals < <(journalctl -F "_EXE" 2>/dev/null; echo '/dev/')
161 COMPREPLY=( $(compgen -W '${field_vals[*]}' -- "${cur}") )
162 if [[ "${COMPREPLY[@]}" = '/dev/' ]]; then
163 compopt -o filenames
164 COMPREPLY=( $(compgen -f -- "${cur}") )
165 fi
166 elif [[ $prev = '=' ]]; then
167 mapfile -t field_vals < <(journalctl -F "${COMP_WORDS[COMP_CWORD-2]}" 2>/dev/null)
168 COMPREPLY=( $(compgen -W '${field_vals[*]}' -- "$cur") )
169 else
170 mapfile -t field_vals < <(journalctl --fields 2>/dev/null)
171 compopt -o nospace
172 COMPREPLY=( $(compgen -W '${field_vals[*]}' -S= -- "$cur") )
173 fi
174 }
175
176 complete -F _journalctl journalctl