Debian Codegraph @

shell-completion/bash/coredumpctl

raw
1 # shellcheck shell=bash
2 # coredumpctl(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 __journal_fields=(MESSAGE{,_ID} PRIORITY CODE_{FILE,LINE,FUNC}
30 ERRNO SYSLOG_{FACILITY,IDENTIFIER,PID} COREDUMP_EXE
31 _{P,U,G}ID _COMM _EXE _CMDLINE
32 _AUDIT_{SESSION,LOGINUID}
33 _SYSTEMD_{CGROUP,SESSION,UNIT,OWNER_UID}
34 _SELINUX_CONTEXT _SOURCE_REALTIME_TIMESTAMP
35 _{BOOT,MACHINE}_ID _HOSTNAME _TRANSPORT
36 _KERNEL_{DEVICE,SUBSYSTEM}
37 _UDEV_{SYSNAME,DEVNODE,DEVLINK}
38 __CURSOR __{REALTIME,MONOTONIC}_TIMESTAMP)
39 _coredumpctl() {
40 local i verb comps
41 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
42 local OPTS='-h --help --version --no-pager --no-legend -o --output -F --field -1
43 -r --reverse -S --since -U --until -D --directory -q --quiet --debugger
44 -A --debugger-arguments --json -n --all --file --root --image'
45
46 local -A VERBS=(
47 [LIST]='list info'
48 [DUMP]='dump debug'
49 )
50
51 if __contains_word "$prev" --output -o --file --image; then
52 comps=$( compgen -A file -- "$cur" )
53 compopt -o filenames
54 elif __contains_word "$prev" -D --directory --root; then
55 comps=$( compgen -A directory -- "$cur" )
56 compopt -o filenames
57 elif __contains_word "$prev" '--debugger'; then
58 comps=$( compgen -A command -- "$cur" )
59 compopt -o filenames
60 elif __contains_word "$prev" --field -F; then
61 comps=$( compgen -W '${__journal_fields[*]}' -- "$cur" )
62 elif __contains_word "$prev" '--json'; then
63 comps=$( compgen -W 'pretty short off' -- "$cur" )
64 elif [[ $cur = -* ]]; then
65 comps=${OPTS}
66 elif __contains_word "$prev" ${VERBS[*]} &&
67 ! __contains_word ${COMP_WORDS[COMP_CWORD-2]} \
68 '--output -o -D --directory -F --field --debugger'; then
69 compopt -o nospace
70 COMPREPLY=( $(compgen -W '${__journal_fields[*]}' -S= -- "$cur") )
71 return 0
72 elif [[ $cur = *=* ]]; then
73 mapfile -t field_vals < <(coredumpctl -F "${prev%=}" 2>/dev/null)
74 COMPREPLY=( $(compgen -W '${field_vals[*]}' -- "${cur#=}") )
75 return 0
76 elif [[ $prev = '=' ]]; then
77 mapfile -t field_vals < <(coredumpctl -F "${COMP_WORDS[COMP_CWORD-2]}" 2>/dev/null)
78 comps=${field_vals[*]}
79 else
80 for ((i=0; i <= COMP_CWORD; i++)); do
81 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]}; then
82 verb=${COMP_WORDS[i]}
83 break
84 fi
85 done
86
87 if [[ -z ${verb-} ]]; then
88 comps=${VERBS[*]}
89 elif __contains_word "$verb" ${VERBS[LIST]} ${VERBS[DUMP]}; then
90 comps=''
91 fi
92 fi
93
94 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
95 return 0
96 }
97
98 complete -F _coredumpctl coredumpctl