Lucas De Marchi | ac6573a | 2013-07-29 13:23:54 -0300 | [diff] [blame] | 1 | # kmod completion -*- shell-script -*- |
| 2 | # |
Marco d'Itri | 1008a2d | 2014-05-04 21:00:43 +0200 | [diff] [blame] | 3 | # This file is part of kmod. |
Lucas De Marchi | ac6573a | 2013-07-29 13:23:54 -0300 | [diff] [blame] | 4 | # |
| 5 | # Copyright 2010 Ran Benita |
| 6 | # Copyright (C) 2013 Intel Corporation. All rights reserved. |
| 7 | # |
Marco d'Itri | 1008a2d | 2014-05-04 21:00:43 +0200 | [diff] [blame] | 8 | # This program is free software; you can redistribute it and/or modify it |
Lucas De Marchi | ac6573a | 2013-07-29 13:23:54 -0300 | [diff] [blame] | 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 | # |
Marco d'Itri | 1008a2d | 2014-05-04 21:00:43 +0200 | [diff] [blame] | 13 | # This program is distributed in the hope that it will be useful, but |
Lucas De Marchi | ac6573a | 2013-07-29 13:23:54 -0300 | [diff] [blame] | 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 |
Marco d'Itri | 1008a2d | 2014-05-04 21:00:43 +0200 | [diff] [blame] | 19 | # along with this program; if not, see <http://www.gnu.org/licenses/>. |
Lucas De Marchi | ac6573a | 2013-07-29 13:23:54 -0300 | [diff] [blame] | 20 | |
| 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 Marchi | 48a4096 | 2013-08-02 12:07:39 -0300 | [diff] [blame] | 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 | |
Lucas De Marchi | ec6d026 | 2013-07-30 03:28:41 -0300 | [diff] [blame] | 41 | _kmod_static_nodes () { |
Lucas De Marchi | 48a4096 | 2013-08-02 12:07:39 -0300 | [diff] [blame] | 42 | local OPTS='-o -f -h --help' |
| 43 | local OPTS_EQUAL='--output --format' |
Lucas De Marchi | ec6d026 | 2013-07-30 03:28:41 -0300 | [diff] [blame] | 44 | local GROUP_FORMAT='human tmpfiles devname' |
| 45 | |
Lucas De Marchi | 48a4096 | 2013-08-02 12:07:39 -0300 | [diff] [blame] | 46 | if __is_opt '-o' '--output'; then |
Lucas De Marchi | ec6d026 | 2013-07-30 03:28:41 -0300 | [diff] [blame] | 47 | compopt -o filenames |
| 48 | COMPREPLY=( $(compgen -f -- "$cur") ) |
| 49 | return 0 |
Lucas De Marchi | 48a4096 | 2013-08-02 12:07:39 -0300 | [diff] [blame] | 50 | elif __is_opt '-f' '--format'; then |
Lucas De Marchi | ec6d026 | 2013-07-30 03:28:41 -0300 | [diff] [blame] | 51 | COMPREPLY=( $(compgen -W "$GROUP_FORMAT" -- "$cur" ) ) |
| 52 | return 0 |
Lucas De Marchi | 48a4096 | 2013-08-02 12:07:39 -0300 | [diff] [blame] | 53 | fi |
Lucas De Marchi | ec6d026 | 2013-07-30 03:28:41 -0300 | [diff] [blame] | 54 | |
Lucas De Marchi | 48a4096 | 2013-08-02 12:07:39 -0300 | [diff] [blame] | 55 | local cur=${COMP_WORDS[COMP_CWORD]} |
| 56 | |
| 57 | compopt -o nospace |
Lucas De Marchi | ec6d026 | 2013-07-30 03:28:41 -0300 | [diff] [blame] | 58 | COMPREPLY=( $(compgen -W "$OPTS" -- "$cur") ) |
Lucas De Marchi | 48a4096 | 2013-08-02 12:07:39 -0300 | [diff] [blame] | 59 | COMPREPLY+=( $(compgen -W "$OPTS_EQUAL" -S= -- "$cur") ) |
Lucas De Marchi | ec6d026 | 2013-07-30 03:28:41 -0300 | [diff] [blame] | 60 | } |
| 61 | |
Lucas De Marchi | ac6573a | 2013-07-29 13:23:54 -0300 | [diff] [blame] | 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 | |
Lucas De Marchi | ec6d026 | 2013-07-30 03:28:41 -0300 | [diff] [blame] | 88 | local func=${verb//-/_} |
| 89 | |
| 90 | if declare -F _kmod_${func} > /dev/null; then |
| 91 | _kmod_${func} |
| 92 | fi |
| 93 | |
Lucas De Marchi | 48a4096 | 2013-08-02 12:07:39 -0300 | [diff] [blame] | 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 | |
Lucas De Marchi | ac6573a | 2013-07-29 13:23:54 -0300 | [diff] [blame] | 100 | return 0 |
| 101 | } |
| 102 | |
| 103 | complete -F _kmod kmod |