[Pkg-cryptsetup-devel] Bug#423591: cryptsetup: Please include bash_completion script

Kevin Locke kwl7 at cornell.edu
Sun May 13 05:44:07 UTC 2007


Package: cryptsetup
Version: 2:1.0.4+svn26-1
Severity: wishlist
Tags: patch

I have created a Bash programmable completion script for cryptsetup
which will complete based on option name, action, and device/mapping
name.  The file can be shipped in /etc/bash_completion.d/ and it will be
sourced by /etc/bash_completion if users have Bash completion turned on.
I have found the script to be quite useful for quickly completing
commands and I hope you find it useful as well.  

Cheers,
Kevin

-- System Information:
Debian Release: lenny/sid
  APT prefers testing
  APT policy: (990, 'testing'), (500, 'unstable'), (500, 'stable'), (1, 'experimental')
Architecture: i386 (i686)

Kernel: Linux 2.6.18.20070225a (SMP w/2 CPU cores; PREEMPT)
Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash

Versions of packages cryptsetup depends on:
ii  dmsetup  2:1.02.08-1                     The Linux Kernel Device Mapper use
ii  libc6    2.5-7                           GNU C Library: Shared libraries
ii  libdevma 2:1.02.08-1                     The Linux Kernel Device Mapper use
ii  libgcryp 1.2.4-2                         LGPL Crypto library - runtime libr
ii  libgpg-e 1.4-2                           library for common error values an
ii  libpopt0 1.10-3                          lib for parsing cmdline parameters
ii  libuuid1 1.39+1.40-WIP-2006.11.14+dfsg-2 universally unique id library

cryptsetup recommends no packages.

-- no debconf information
-------------- next part --------------
# Bash command completion for cryptsetup

have cryptsetup &&
_cryptsetup()
{
    local cmd cur prev action actions luksactions argopts noargopts

    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    actions="create remove status reload resize"
    luksactions="luksFormat luksOpen luksClose luksAddKey luksDelKey luksUUID isLuks luksDump"
    actions="$luksactions $actions"

    argopts="-c --cipher -h --hash -d --key-file -s --key-size -b --size"
    argopts="$argopts -o --offset -p --skip -i --iter-time -q --batch-mode"
    argopts="$argopts -t --timeout -T --tries"
    noargopts="-y --verify-passphrase --readonly --version --align-payload"

    # If previous word was an option requiring an argument, can't complete
    for argopt in $argopts ; do
	if [ "$argopt" = "$prev" ] ; then
	    return
	fi
    done

    # If user typing an option, complete it
    if [[ $cur == -* ]] ; then
	COMPREPLY=( $(compgen -W "$argopts $noargopts" -- "$cur") )
	return
    fi

    # See if we already have an action
    action=""
    for word in "${COMP_WORDS[@]}" ; do
	for act in $actions ; do
	    if [ "$word" == "$act" ] ; then
		action=$act
		break
	    fi
	done

	if [ -n "$action" ] ; then
	    break
	fi
    done

    # No action yet, complete it
    if [ -z "$action" ] ; then
	COMPREPLY=( $(compgen -W "$actions" -- "$cur") )
	return
    fi

    # Completion based on action
    case "$action" in
	"create")
	    # create <name> <device>
	    if [ $COMP_CWORD -gt 1 ] &&
		[ ${COMP_WORDS[COMP_CWORD-2]} == "create" ] ; then
		COMPREPLY=( $(compgen -f -X '!/dev*' -- "$cur") )
	    fi
	;;

	"reload"|"remove"|"resize"|"status"|"luksClose")
	    # action <name>
	    MAPPINGS="$(ls /dev/mapper | fgrep --invert-match control)"
	    OLDIFS="$IFS"
	    IFS="
"
	    COMPREPLY=( $(compgen -W "$MAPPINGS" -- "$cur") )
	    IFS="$OLDIFS"
	;;

	"luksDelKey")
	    # luksDelKey <name> <key slot number>
	    if [ ${COMP_WORDS[COMP_CWORD-1]} == "luksDelKey" ] ; then
		# Get name
		MAPPINGS="$(ls /dev/mapper | fgrep --invert-match control)"
		OLDIFS="$IFS"
	    	IFS="
"
	    	COMPREPLY=( $(compgen -W "$MAPPINGS" -- "$cur") )
	    	IFS="$OLDIFS"
	    fi
	;;

	"luksAddKey"|"luksFormat")
	    # action <name> [<key file>]
	    if [ ${COMP_WORDS[COMP_CWORD-1]} == "luksFormat" ] ; then
		# Get name
		MAPPINGS="$(ls /dev/mapper | fgrep --invert-match control)"
		OLDIFS="$IFS"
	    	IFS="
"
	    	COMPREPLY=( $(compgen -W "$MAPPINGS" -- "$cur") )
	    	IFS="$OLDIFS"
	    elif [ ${COMP_WORDS[COMP_CWORD-2]} == "luksFormat" ] ; then
		# Get key file
		COMPREPLY=( $(compgen -f -- "$cur") )
	    fi
	;;

	"luksOpen")
	    # luksOpen <device> <name>
	    if [ ${COMP_WORDS[COMP_CWORD-1]} == "luksOpen" ] ; then
		COMPREPLY=( $(compgen -f -X '!/dev*' -- "$cur") )
	    fi
	;;

	"isLuks"|"luksDump"|"luksUUID")
	    # action <device>
	    COMPREPLY=( $(compgen -f -X '!/dev*' -- "$cur") )
	;;
    esac
}
[ "$have" ] && complete -o filenames -F _cryptsetup cryptsetup

# vim:set filetype=sh sts=4 sw=4:


More information about the Pkg-cryptsetup-devel mailing list