]> git.ipfire.org Git - thirdparty/systemd.git/blob - shell-completion/bash/coredumpctl
Merge pull request #7379 from yuwata/follow-up-7309
[thirdparty/systemd.git] / shell-completion / bash / coredumpctl
1 # coredumpctl(1) completion -*- shell-script -*-
2 # SPDX-License-Identifier: LGPL-2.1+
3 #
4 # This file is part of systemd.
5 #
6 # Copyright 2010 Ran Benita
7 #
8 # systemd is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU Lesser General Public License as published by
10 # the Free Software Foundation; either version 2.1 of the License, or
11 # (at your option) any later version.
12 #
13 # systemd is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public License
19 # along with systemd; If not, see <http://www.gnu.org/licenses/>.
20
21 __contains_word () {
22 local w word=$1; shift
23 for w in "$@"; do
24 [[ $w = "$word" ]] && return
25 done
26 }
27
28 __journal_fields=(MESSAGE{,_ID} PRIORITY CODE_{FILE,LINE,FUNC}
29 ERRNO SYSLOG_{FACILITY,IDENTIFIER,PID} COREDUMP_EXE
30 _{P,U,G}ID _COMM _EXE _CMDLINE
31 _AUDIT_{SESSION,LOGINUID}
32 _SYSTEMD_{CGROUP,SESSION,UNIT,OWNER_UID}
33 _SELINUX_CONTEXT _SOURCE_REALTIME_TIMESTAMP
34 _{BOOT,MACHINE}_ID _HOSTNAME _TRANSPORT
35 _KERNEL_{DEVICE,SUBSYSTEM}
36 _UDEV_{SYSNAME,DEVNODE,DEVLINK}
37 __CURSOR __{REALTIME,MONOTONIC}_TIMESTAMP)
38 _coredumpctl() {
39 local i verb comps
40 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
41 local OPTS='-h --help --version --no-pager --no-legend -o --output -F --field -1
42 -r --reverse -S --since -U --until'
43
44 local -A VERBS=(
45 [LIST]='list'
46 [DUMP]='dump gdb'
47 )
48
49 if __contains_word "$prev" '--output -o'; then
50 comps=$( compgen -A file -- "$cur" )
51 compopt -o filenames
52 elif __contains_word "$prev" '--FIELD -F'; then
53 comps=$( compgen -W '${__journal_fields[*]}' -- "$cur" )
54 elif [[ $cur = -* ]]; then
55 comps=${OPTS}
56 elif __contains_word "$prev" ${VERBS[*]} &&
57 ! __contains_word ${COMP_WORDS[COMP_CWORD-2]} '--output -o -F --field'; then
58 compopt -o nospace
59 COMPREPLY=( $(compgen -W '${__journal_fields[*]}' -S= -- "$cur") )
60 return 0
61 elif [[ $cur = *=* ]]; then
62 mapfile -t field_vals < <(coredumpctl -F "${prev%=}" 2>/dev/null)
63 COMPREPLY=( $(compgen -W '${field_vals[*]}' -- "${cur#=}") )
64 return 0
65 elif [[ $prev = '=' ]]; then
66 mapfile -t field_vals < <(coredumpctl -F "${COMP_WORDS[COMP_CWORD-2]}" 2>/dev/null)
67 comps=${field_vals[*]}
68 else
69 for ((i=0; i <= COMP_CWORD; i++)); do
70 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]}; then
71 verb=${COMP_WORDS[i]}
72 break
73 fi
74 done
75
76 if [[ -z $verb ]]; then
77 comps=${VERBS[*]}
78 elif __contains_word "$verb" ${VERBS[LIST]} ${VERBS[DUMP]}; then
79 comps=''
80 fi
81 fi
82
83 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
84 return 0
85 }
86
87 complete -F _coredumpctl coredumpctl