1# systemd-analyze(1) completion                      -*- shell-script -*-
2# SPDX-License-Identifier: LGPL-2.1-or-later
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__get_machines() {
29    local a b
30    machinectl list --full --no-legend --no-pager | { while read a b; do echo " $a"; done; };
31}
32
33__get_units_all() {
34    systemctl list-units --no-legend --no-pager --plain --all | \
35        { while read -r a b c; do echo " $a"; done }
36}
37
38__get_services() {
39    systemctl list-units --no-legend --no-pager --plain -t service --all $1 | \
40        { while read -r a b c; do [[ $b == "loaded" ]]; echo " $a"; done }
41}
42
43__get_syscall_sets() {
44    local line
45    systemd-analyze syscall-filter --no-pager | while IFS= read -r line; do
46        if [[ $line == @* ]]; then
47            printf '%s\n' "$line"
48        fi
49    done
50}
51
52_systemd_analyze() {
53    local i verb comps mode
54    local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} words cword
55
56    local -A OPTS=(
57        [STANDALONE]='-h --help --version --system --user --global --order --require --no-pager
58                             --man=no --generators=yes --quiet'
59        [ARG]='-H --host -M --machine --fuzz --from-pattern --to-pattern --root'
60    )
61
62    local -A VERBS=(
63        [STANDALONE]='time blame plot dump unit-paths exit-status calendar timestamp timespan'
64        [CRITICAL_CHAIN]='critical-chain'
65        [DOT]='dot'
66        [VERIFY]='verify'
67        [SECCOMP_FILTER]='syscall-filter'
68        [CAT_CONFIG]='cat-config'
69        [SECURITY]='security'
70        [CONDITION]='condition'
71        [INSPECT_ELF]='inspect-elf'
72    )
73
74    local CONFIGS='systemd/bootchart.conf systemd/coredump.conf systemd/journald.conf
75                       systemd/journal-remote.conf systemd/journal-upload.conf systemd/logind.conf
76                       systemd/resolved.conf systemd/networkd.conf systemd/resolved.conf
77                       systemd/sleep.conf systemd/system.conf systemd/timedated.conf
78                       systemd/timesyncd.conf systemd/user.conf udev/udev.conf'
79
80    _init_completion || return
81
82    for ((i=0; i < COMP_CWORD; i++)); do
83        if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
84                ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
85            verb=${COMP_WORDS[i]}
86            break
87        fi
88    done
89
90    if __contains_word "$prev" ${OPTS[ARG]}; then
91        case $prev in
92            --host|-H)
93                comps=$(compgen -A hostname)
94                ;;
95            --machine|-M)
96                comps=$( __get_machines )
97                ;;
98        esac
99        COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
100        return 0
101    fi
102
103    if [[ -z ${verb-}  && $cur = -* ]]; then
104        COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
105        return 0
106    fi
107
108    if [[ -z ${verb-} ]]; then
109        comps=${VERBS[*]}
110
111    elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
112        if [[ $cur = -* ]]; then
113            comps='--help --version --system --user --global --no-pager'
114        fi
115
116    elif __contains_word "$verb" ${VERBS[CRITICAL_CHAIN]}; then
117        if [[ $cur = -* ]]; then
118            comps='--help --version --system --user --fuzz --no-pager'
119        else
120            comps=$( __get_units_all )
121        fi
122
123    elif __contains_word "$verb" ${VERBS[DOT]}; then
124        if [[ $cur = -* ]]; then
125            comps='--help --version --system --user --global --from-pattern --to-pattern --order --require'
126        fi
127
128    elif __contains_word "$verb" ${VERBS[SECCOMP_FILTER]}; then
129        if [[ $cur = -* ]]; then
130            comps='--help --version --no-pager'
131        else
132            comps=$( __get_syscall_sets )
133        fi
134
135    elif __contains_word "$verb" ${VERBS[VERIFY]}; then
136        if [[ $cur = -* ]]; then
137            comps='--help --version --system --user --global --man=no --generators=yes --root --image --recursive-errors=no --recursive-errors=yes --recursive-errors=one'
138        else
139            comps=$( compgen -A file -- "$cur" )
140            compopt -o filenames
141        fi
142
143    elif __contains_word "$verb" ${VERBS[CAT_CONFIG]}; then
144        if [[ $cur = -* ]]; then
145            comps='--help --version --root --no-pager'
146        elif [[ -z $cur ]]; then
147            comps="$CONFIGS"
148            compopt -o filenames
149        else
150            comps="$CONFIGS $( compgen -A file -- "$cur" )"
151            compopt -o filenames
152        fi
153
154    elif __contains_word "$verb" ${VERBS[SECURITY]}; then
155        if [[ $cur = -* ]]; then
156            comps='--help --version --no-pager --system --user -H --host -M --machine --offline --threshold --security-policy --json=off --json=pretty --json=short --root --image --profile=default --profile=nonetwork --profile=strict --profile=trusted'
157        elif ! __contains_word "--offline" ${COMP_WORDS[*]}; then
158            if __contains_word "--user" ${COMP_WORDS[*]}; then
159                mode=--user
160            else
161                mode=--system
162            fi
163            comps=$( __get_services $mode )
164        else
165            comps="$CONFIGS $( compgen -A file -- "$cur" )"
166            compopt -o filenames
167        fi
168
169    elif __contains_word "$verb" ${VERBS[CONDITION]}; then
170        if [[ $cur = -* ]]; then
171            comps='--help --version --system --user --global --no-pager --root --image'
172        elif [[ $prev = "-u" ]] || [[ $prev = "--unit" ]]; then
173            if __contains_word "--user" ${COMP_WORDS[*]}; then
174                mode=--user
175            else
176                mode=--system
177            fi
178            comps=$( __get_services $mode )
179        fi
180
181    elif __contains_word "$verb" ${VERBS[INSPECT_ELF]}; then
182        if [[ $cur = -* ]]; then
183            comps='--help --version --json=off --json=pretty --json=short'
184        else
185            comps=$( compgen -A file -- "$cur" )
186            compopt -o filenames
187        fi
188    fi
189
190    COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
191    return 0
192}
193
194complete -F _systemd_analyze systemd-analyze
195