• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# kmod completion                                          -*- shell-script -*-
2#
3# This file is part of kmod.
4#
5# Copyright 2010 Ran Benita
6# Copyright (C) 2013  Intel Corporation. All rights reserved.
7#
8# This program 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# This program 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 this program; if not, see <http://www.gnu.org/licenses/>.
20
21__contains_word () {
22        local word=$1; shift
23        for w in "$@"; do [[ "$w" = "$word" ]] && return 0; done
24        return 1
25}
26
27__is_opt () {
28        local prevprev=${COMP_WORDS[COMP_CWORD-2]}
29        local short="$1" long="$2"
30
31        if [[ "$prev" = "$short" || "$prev" = "$long" ]]; then
32                declare -g cur=${cur#=}
33                return 0
34        elif [[ "$prev" = "=" && "$prevprev" = "$long" ]]; then
35                return 0
36        fi
37
38        return 1
39}
40
41_kmod_static_nodes () {
42        local OPTS='-o -f -h --help'
43        local OPTS_EQUAL='--output --format'
44        local GROUP_FORMAT='human tmpfiles devname'
45
46        if __is_opt '-o' '--output'; then
47                compopt -o filenames
48                COMPREPLY=( $(compgen -f -- "$cur") )
49                return 0
50        elif __is_opt '-f' '--format'; then
51                COMPREPLY=( $(compgen -W "$GROUP_FORMAT" -- "$cur" ) )
52                return 0
53        fi
54
55        local cur=${COMP_WORDS[COMP_CWORD]}
56
57        compopt -o nospace
58        COMPREPLY=( $(compgen -W "$OPTS" -- "$cur") )
59        COMPREPLY+=( $(compgen -W "$OPTS_EQUAL" -S= -- "$cur") )
60}
61
62_kmod() {
63        local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
64        local VERBS=(help list static-nodes)
65        local OPTS='-h --help -V --version'
66        local verb
67
68        # standalone options, no other option or action allowed
69        for ((i=0; $i < $COMP_CWORD; i++)); do
70                if __contains_word "${COMP_WORDS[i]}" ${OPTS}; then
71                        return 0
72                fi
73        done
74
75        # find the action
76        for ((i=0; $i <= $COMP_CWORD; i++)); do
77                if __contains_word "${COMP_WORDS[i]}" "${VERBS[@]}"; then
78                        verb=${COMP_WORDS[i]}
79                        break
80                fi
81        done
82
83        if [[ -z $verb ]]; then
84                COMPREPLY=( $(compgen -W '${OPTS[*]} ${VERBS[*]}' -- "$cur") )
85                return 0
86        fi
87
88        local func=${verb//-/_}
89
90        if declare -F _kmod_${func} > /dev/null; then
91                _kmod_${func}
92        fi
93
94        # allow the space if there's only one completion and it doesn't end with
95        # '='
96        if [[ ${#COMPREPLY[@]} == 1 && ${COMPREPLY[0]} != *"=" ]] ; then
97                compopt +o nospace
98        fi
99
100        return 0
101}
102
103complete -F _kmod kmod
104