blob: f530836fdef7e367b5153f044e050e3a983b7647 [file] [log] [blame]
Lucas De Marchiac6573a2013-07-29 13:23:54 -03001# kmod completion -*- shell-script -*-
2#
Marco d'Itri1008a2d2014-05-04 21:00:43 +02003# This file is part of kmod.
Lucas De Marchiac6573a2013-07-29 13:23:54 -03004#
5# Copyright 2010 Ran Benita
6# Copyright (C) 2013 Intel Corporation. All rights reserved.
7#
Marco d'Itri1008a2d2014-05-04 21:00:43 +02008# This program is free software; you can redistribute it and/or modify it
Lucas De Marchiac6573a2013-07-29 13:23:54 -03009# 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#
Marco d'Itri1008a2d2014-05-04 21:00:43 +020013# This program is distributed in the hope that it will be useful, but
Lucas De Marchiac6573a2013-07-29 13:23:54 -030014# 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
Marco d'Itri1008a2d2014-05-04 21:00:43 +020019# along with this program; if not, see <http://www.gnu.org/licenses/>.
Lucas De Marchiac6573a2013-07-29 13:23:54 -030020
21__contains_word () {
22 local word=$1; shift
23 for w in "$@"; do [[ "$w" = "$word" ]] && return 0; done
24 return 1
25}
26
Lucas De Marchi48a40962013-08-02 12:07:39 -030027__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
Lucas De Marchiec6d0262013-07-30 03:28:41 -030041_kmod_static_nodes () {
Lucas De Marchi48a40962013-08-02 12:07:39 -030042 local OPTS='-o -f -h --help'
43 local OPTS_EQUAL='--output --format'
Lucas De Marchiec6d0262013-07-30 03:28:41 -030044 local GROUP_FORMAT='human tmpfiles devname'
45
Lucas De Marchi48a40962013-08-02 12:07:39 -030046 if __is_opt '-o' '--output'; then
Lucas De Marchiec6d0262013-07-30 03:28:41 -030047 compopt -o filenames
48 COMPREPLY=( $(compgen -f -- "$cur") )
49 return 0
Lucas De Marchi48a40962013-08-02 12:07:39 -030050 elif __is_opt '-f' '--format'; then
Lucas De Marchiec6d0262013-07-30 03:28:41 -030051 COMPREPLY=( $(compgen -W "$GROUP_FORMAT" -- "$cur" ) )
52 return 0
Lucas De Marchi48a40962013-08-02 12:07:39 -030053 fi
Lucas De Marchiec6d0262013-07-30 03:28:41 -030054
Lucas De Marchi48a40962013-08-02 12:07:39 -030055 local cur=${COMP_WORDS[COMP_CWORD]}
56
57 compopt -o nospace
Lucas De Marchiec6d0262013-07-30 03:28:41 -030058 COMPREPLY=( $(compgen -W "$OPTS" -- "$cur") )
Lucas De Marchi48a40962013-08-02 12:07:39 -030059 COMPREPLY+=( $(compgen -W "$OPTS_EQUAL" -S= -- "$cur") )
Lucas De Marchiec6d0262013-07-30 03:28:41 -030060}
61
Lucas De Marchiac6573a2013-07-29 13:23:54 -030062_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
Lucas De Marchiec6d0262013-07-30 03:28:41 -030088 local func=${verb//-/_}
89
90 if declare -F _kmod_${func} > /dev/null; then
91 _kmod_${func}
92 fi
93
Lucas De Marchi48a40962013-08-02 12:07:39 -030094 # 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
Lucas De Marchiac6573a2013-07-29 13:23:54 -0300100 return 0
101}
102
103complete -F _kmod kmod