summaryrefslogtreecommitdiffstats
path: root/completions/valgrind
blob: 7043aa32fdb3cf71401e4c53bc209b2dde3d0737 (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
# valgrind(1) completion                                   -*- shell-script -*-

_comp_cmd_valgrind()
{
    local cur prev words cword was_split comp_args
    _comp_initialize -s -- "$@" || return

    local i
    for ((i = 1; i <= cword; i++)); do
        if [[ ${words[i]} != @([-=])* ]]; then
            _comp_command_offset $i
            return
        fi
    done

    local word tool=""
    for word in "${words[@]:1}"; do
        if [[ $word == --tool=?* ]]; then
            tool=$word
            break
        fi
    done

    case $prev in
        -h | --help | --help-debug | --version)
            return
            ;;
        --tool)
            # Tools seem to be named e.g. like memcheck-amd64-linux from which
            # we want to grab memcheck.
            local -a files
            if _comp_expand_glob files '/usr{,/local}/lib{,64,exec}{/*-linux-gnu,}/valgrind/*'; then
                _comp_compgen_split -- "$(
                    for f in "${files[@]}"; do
                        [[ $f != *.so && -x $f && $f =~ ^.*/(.*)-[^-]+-[^-]+ ]] &&
                            printf '%s\n' "${BASH_REMATCH[1]}"
                    done
                )"
            fi
            return
            ;;
        --sim-hints)
            _comp_compgen -- -W 'lax-ioctls enable-outer'
            return
            ;;
        --soname-synonyms)
            _comp_compgen -- -W 'somalloc' -S =
            [[ ${COMPREPLY-} == *= ]] && compopt -o nospace
            return
            ;;
        --kernel-variant)
            _comp_compgen -- -W 'bproc'
            return
            ;;
        # callgrind:
        --callgrind-out-file)
            _comp_compgen_filedir
            return
            ;;
        # exp-dhat:
        --sort-by)
            _comp_compgen -- -W 'max-bytes-live tot-bytes-allocd
                max-blocks-live'
            return
            ;;
        # massif:
        --time-unit)
            _comp_compgen -- -W 'i ms B'
            return
            ;;
            # generic cases parsed from --help output
        --+([-A-Za-z0-9_]))
            # shellcheck disable=SC2086
            local value=$("$1" --help-debug $tool 2>/dev/null |
                command sed \
                    -ne "s|^[[:blank:]]*$prev=\([^[:blank:]]\{1,\}\).*|\1|p")
            case $value in
                \<file*\>)
                    _comp_compgen_filedir
                    return
                    ;;
                \<command\>)
                    _comp_compgen_commands
                    return
                    ;;
                \<+([0-9])..+([0-9])\>)
                    _comp_compgen -- -W "{${value:1:${#value}-2}}"
                    return
                    ;;
                # "yes", "yes|no", etc (but not "string", "STR",
                # "hint1,hint2,...")
                yes | +([-a-z0-9])\|+([-a-z0-9\|]))
                    _comp_compgen -F '|' -- -W '$value'
                    return
                    ;;
            esac
            ;;
    esac

    [[ $was_split ]] && return

    if [[ $cur == -* ]]; then
        _comp_compgen_help -- --help ${tool:+"$tool"}
        [[ ${COMPREPLY-} == *= ]] && compopt -o nospace
        return
    fi
} &&
    complete -F _comp_cmd_valgrind valgrind

# ex: filetype=sh