summaryrefslogtreecommitdiffstats
path: root/completions/tshark
blob: 249670f87adc5ceb606ef01c6b9927c4e5283d01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# tshark(1) completion                                     -*- shell-script -*-

_comp_cmd_tshark()
{
    local cur prev words cword comp_args prefix
    _comp_initialize -n : -- "$@" || return

    case $cur in
        -o*)
            prefix=-o
            ;;
        -X*)
            prefix=-X
            ;;
    esac

    case ${prefix:-$prev} in
        --*)
            # Fallback to completion of long options below.
            ;;
        -o*)
            if [[ $cur == *:* ]]; then
                _comp_compgen -c "${cur#*:}" filedir
            else
                [[ -v _comp_cmd_tshark__prefs ]] ||
                    _comp_cmd_tshark__prefs="$("$1" -G defaultprefs 2>/dev/null | command sed -ne 's/^#\{0,1\}\([a-z0-9_.-]\{1,\}:\).*/\1/p' |
                        tr '\n' ' ')"
                : ${prefix:=}
                _comp_compgen -c "${cur:${#prefix}}" -- -P "$prefix" \
                    -W "$_comp_cmd_tshark__prefs"
                [[ ${COMPREPLY-} == *: ]] && compopt -o nospace
            fi
            return
            ;;
        -*[fsBDLcRNdCeEzhvoK])
            return
            ;;
        -*i)
            _comp_compgen_split -- "$("$1" -D 2>/dev/null | _comp_awk '{print $2}')"
            return
            ;;
        -*y)
            local opts i
            for ((i = ${#words[@]} - 1; i > 0; i--)); do
                if [[ ${words[i]} == -i ]]; then
                    opts+="-i ${words[i + 1]}"
                    break
                fi
            done
            # shellcheck disable=SC2086
            _comp_compgen_split -- "$("$1" $opts -L 2>/dev/null |
                _comp_awk '/^  / { print $1 }')"
            return
            ;;
        -*[ab])
            _comp_compgen -- -W 'duration: filesize: files:'
            [[ ${COMPREPLY-} == *: ]] && compopt -o nospace
            return
            ;;
        -*[rH])
            # -r accepts a lot of different file types
            _comp_compgen_filedir
            return
            ;;
        -*w)
            _comp_compgen_filedir
            [[ $cur == @(|-) ]] && COMPREPLY+=(-)
            return
            ;;
        -*F)
            _comp_compgen_split -- "$("$1" -F 2>&1 | _comp_awk '/^  / { print $1 }')"
            return
            ;;
        -*O)
            [[ -v _comp_cmd_tshark__protocols ]] ||
                _comp_cmd_tshark__protocols="$("$1" -G protocols 2>/dev/null |
                    cut -f 3 | tr '\n' ' ')"
            _comp_delimited , -W "$_comp_cmd_tshark__protocols"
            return
            ;;
        -*T)
            # Parse from: tshark -T . 2>&1 | _comp_awk -F \" '/^\t*"/ { print $2 }'
            _comp_compgen -- -W 'pdml ps psml json jsonraw ek tabs text fields'
            return
            ;;
        -*t)
            # Parse from: tshark -t . 2>&1 | _comp_awk -F \" '/^\t*"/ { print $2 }'
            _comp_compgen -- -W 'a ad adoy d dd e r u ud udoy'
            return
            ;;
        -*u)
            # TODO: could be parsed from "-u ." output
            _comp_compgen -- -W 's hms'
            return
            ;;
        -*W)
            _comp_compgen -- -W 'n'
            return
            ;;
        -*X)
            if [[ ${cur:${#prefix}} == lua_script:* ]]; then
                _comp_compgen -c "${cur#*:}" filedir lua
            else
                _comp_compgen -c "${cur:${#prefix}}" -- -P "$prefix" \
                    -W 'lua_script:'
                [[ ${COMPREPLY-} == *: ]] && compopt -o nospace
            fi
            return
            ;;
        -*G)
            _comp_compgen_split -- "$("$1" -G \? 2>/dev/null |
                _comp_awk '/^[ \t]*-G / {
                    sub("^[[]","",$2); sub("[]]$","",$2); print $2
                }')"
            return
            ;;

    esac

    if [[ $cur == -* ]]; then
        _comp_compgen_help -- -h
        return
    fi
} &&
    complete -F _comp_cmd_tshark tshark

# ex: filetype=sh