Received: from mailman by lists.gnu.org with archive (Exim 4.43) id 1OqmcB-0006IE-Pn for mharc-grub-devel@gnu.org; Wed, 01 Sep 2010 08:40:31 -0400 Received: from [140.186.70.92] (port=43911 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Oqmc8-0006H2-1k for grub-devel@gnu.org; Wed, 01 Sep 2010 08:40:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1Oqmc6-00009o-6M for grub-devel@gnu.org; Wed, 01 Sep 2010 08:40:27 -0400 Received: from smtpout01s1.x-echo.com ([193.252.149.37]:58803) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1Oqmc5-00009O-LQ for grub-devel@gnu.org; Wed, 01 Sep 2010 08:40:26 -0400 Received: from colinux.localdomain (localhost.localdomain [127.0.0.1]) by colinux.localdomain (SMTP Server) with ESMTP id 1167C1000E for ; Wed, 1 Sep 2010 14:40:22 +0200 (CEST) Mime-Version: 1.0 Content-Type: multipart/mixed; boundary=MIMEBOUNDARY To: grub-devel@gnu.org From: Yves Blusseau Date: Wed, 01 Sep 2010 14:40:22 +0200 Message-ID: <20100901124021.8378.90579.stgit@colinux.localdomain> User-Agent: StGit/0.15-97-g9680 X-AV-Checked: ClamAV using ClamSMTP X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) Subject: [PATCH] Bash completion script for util commands X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: The development of GNU GRUB List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 01 Sep 2010 12:40:29 -0000 This is a MIME message. --MIMEBOUNDARY Content-Disposition: inline MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit * Makefile.am: Add util/bash-completion.d directory * configure.ac: Likewise. * util/bash-completion.d/Makefile.am: New file. * util/bash-completion.d/grub-completion.bash.in: Likewise. --- ChangeLog.completion.bash | 6 Makefile.am | 2 configure.ac | 1 util/bash-completion.d/Makefile.am | 15 + util/bash-completion.d/grub-completion.bash.in | 450 ++++++++++++++++++++++++ 5 files changed, 473 insertions(+), 1 deletions(-) create mode 100644 ChangeLog.completion.bash create mode 100644 util/bash-completion.d/Makefile.am create mode 100644 util/bash-completion.d/grub-completion.bash.in --MIMEBOUNDARY Content-Disposition: attachment; filename=bash-completion.patch MIME-Version: 1.0 Content-Type: text/plain; name=bash-completion.patch; charset="utf-8" Content-Transfer-Encoding: 7bit diff --git a/ChangeLog.completion.bash b/ChangeLog.completion.bash new file mode 100644 index 0000000..e57374a --- /dev/null +++ b/ChangeLog.completion.bash @@ -0,0 +1,6 @@ +Bash completion script for util commands + + * Makefile.am: Add util/bash-completion.d directory + * configure.ac: Likewise. + * util/bash-completion.d/Makefile.am: New file. + * util/bash-completion.d/grub-completion.bash.in: Likewise. diff --git a/Makefile.am b/Makefile.am index 1cf2297..c05d352 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,7 +1,7 @@ AUTOMAKE_OPTIONS = subdir-objects DEPDIR = .deps-util -SUBDIRS = . grub-core po docs +SUBDIRS = . grub-core po docs util/bash-completion.d include $(top_srcdir)/conf/Makefile.common include $(top_srcdir)/conf/Makefile.extra-dist diff --git a/configure.ac b/configure.ac index b167408..fa96af7 100644 --- a/configure.ac +++ b/configure.ac @@ -933,6 +933,7 @@ AC_CONFIG_FILES([Makefile]) AC_CONFIG_FILES([grub-core/Makefile]) AC_CONFIG_FILES([po/Makefile]) AC_CONFIG_FILES([docs/Makefile]) +AC_CONFIG_FILES([util/bash-completion.d/Makefile]) AC_CONFIG_FILES([stamp-h], [echo timestamp > stamp-h]) AC_OUTPUT diff --git a/util/bash-completion.d/Makefile.am b/util/bash-completion.d/Makefile.am new file mode 100644 index 0000000..774e1a3 --- /dev/null +++ b/util/bash-completion.d/Makefile.am @@ -0,0 +1,15 @@ + +bash_completion_source = grub-completion.bash.in +bash_completion_script = $(grubname) + +EXTRA_DIST = $(bash_completion_source) + +CLEANFILES = $(bash_completion_script) + +DISTCLEANFILES = *~ + +bashcompletiondir = $(sysconfdir)/bash_completion.d +bashcompletion_DATA = $(bash_completion_script) + +$(bash_completion_script): $(bash_completion_source) $(top_builddir)/config.status + $(top_builddir)/config.status --file=$@:$< diff --git a/util/bash-completion.d/grub-completion.bash.in b/util/bash-completion.d/grub-completion.bash.in new file mode 100644 index 0000000..4d9a96f --- /dev/null +++ b/util/bash-completion.d/grub-completion.bash.in @@ -0,0 +1,450 @@ +# +# Bash completion for grub +# +# Copyright (C) 2010 Free Software Foundation, Inc. +# +# GRUB is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# GRUB is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GRUB. If not, see . +# bash completion for grub + +__@grubname@_dir() { + local i c=1 root_dir boot_dir + + for (( c=1; c <= ${#COMP_WORDS[@]}; c++ )); do + i="${COMP_WORDS[c]}" + case "$i" in + --root-directory) + c=$((++c)) + i="${COMP_WORDS[c]}" + root_dir="${i##*=}"; + break + ;; + esac + done + root_dir=${root_dir-/} + if [[ -d "$root_dir/boot" ]]; then + boot_dir="$root_dir/boot" + else + boot_dir="$root_dir" + fi + echo "${boot_dir%/}/grub" +} + + +# This function generates completion reply with compgen +# - arg: accepts 1, 2, 3, or 4 arguments +# $1 wordlist separate by space, tab or newline +# $2 (optional) prefix to add +# $3 (optional) current word to complete +# $4 (optional) suffix to add +__@grubname@comp () { + local cur="${COMP_WORDS[COMP_CWORD]}" + if [ $# -gt 2 ]; then + cur="$3" + fi + case "$cur" in + --*=) + COMPREPLY=() + ;; + *) + local IFS=' '$'\t'$'\n' + COMPREPLY=($(compgen -P "${2-}" -W "${1-}" -S "${4-}" -- "$cur")) + ;; + esac +} + +# Function that return long options from the help +# - arg: $1 command to get the long options from +__@grubname@_get_options_from_help () { + local i IFS=" "$'\t'$'\n' + for i in $($1 --help) + do + case $i in + --*) echo "${i%=*}";; + esac + done +} + +__@grubname@_get_last_option () { + local i + for (( i=$COMP_CWORD-1; i > 0; i-- )); do + if [[ "${COMP_WORDS[i]}" == -* ]]; then + echo "${COMP_WORDS[i]}" + break; + fi + done +} + +__@grubname@_list_menuentries () { + local cur="${COMP_WORDS[COMP_CWORD]}" + local config_file=$(__@grubname@_dir)/grub.cfg + + if [ -f "$config_file" ];then + local IFS=$'\n' + COMPREPLY=( $(compgen \ + -W "$( awk -F "[\"']" '/menuentry/ { print $2 }' $config_file )" \ + -- "$cur" )) #'# Help emacs syntax highlighting + fi +} + +__@grubname@_list_modules () { + local grub_dir=$(__@grubname@_dir) + local IFS=$'\n' + COMPREPLY=( $( compgen -f -X '!*/*.mod' -- "${grub_dir}/$cur" | { + while read -r tmp; do + [ -n $tmp ] && { + tmp=${tmp##*/} + printf '%s\n' ${tmp%.mod} + } + done + } + )) +} + +# +# @grubname@-set-default & @grubname@-reboot +# +_@grubname@_set_entry () { + local cur prev split=false + + COMPREPLY=() + cur=`_get_cword` + prev=${COMP_WORDS[COMP_CWORD-1]} + + _split_longopt && split=true + + case "$prev" in + --root-directory) + _filedir -d + return + ;; + esac + + $split && return 0 + + if [[ "$cur" == -* ]]; then + __@grubname@comp "--help --version --root-directory" + else + # Default complete with a menuentry + __@grubname@_list_menuentries + fi +} + +have @grubname@-set-default && \ + complete -F _@grubname@_set_entry -o filenames @grubname@-set-default +have @grubname@-reboot && \ + complete -F _@grubname@_set_entry -o filenames @grubname@-reboot + +# +# @grubname@-editenv +# +_@grubname@_editenv () { + local cur prev + + COMPREPLY=() + cur=`_get_cword` + prev=${COMP_WORDS[COMP_CWORD-1]} + + case "$prev" in + create|list|set|unset) + COMPREPLY=( "" ) + return + ;; + esac + + __@grubname@comp "$(__@grubname@_get_options_from_help '@grubname@-editenv') + create list set unset" +} +have @grubname@-editenv && \ + complete -F _@grubname@_editenv -o filenames @grubname@-editenv + +# +# @grubname@-mkconfig +# +_@grubname@_mkconfig () { + local cur prev + + COMPREPLY=() + cur=`_get_cword` + + if [[ "$cur" == -* ]]; then + __@grubname@comp "$(__@grubname@_get_options_from_help '@grubname@-mkconfig')" + else + _filedir + fi +} +have @grubname@-mkconfig && \ + complete -F _@grubname@_mkconfig -o filenames @grubname@-mkconfig + +# +# @grubname@-setup +# +_@grubname@_setup () { + local cur prev split=false + + COMPREPLY=() + cur=`_get_cword` + prev=${COMP_WORDS[COMP_CWORD-1]} + + _split_longopt && split=true + + case "$prev" in + -d|--directory) + _filedir -d + return + ;; + esac + + $split && return 0 + + if [[ "$cur" == -* ]]; then + __@grubname@comp "$(__@grubname@_get_options_from_help '@grubname@-setup')" + else + # Default complete with a filename + _filedir + fi +} +have @grubname@-setup && \ + complete -F _@grubname@_setup -o filenames @grubname@-setup + +# +# @grubname@-install +# +_@grubname@_install () { + local cur prev last split=false + + COMPREPLY=() + cur=`_get_cword` + prev=${COMP_WORDS[COMP_CWORD-1]} + last=$(__@grubname@_get_last_option) + + _split_longopt && split=true + + case "$prev" in + --root-directory) + _filedir -d + return + ;; + --disk-module) + __@grubname@comp "biosdisk ata" + return + ;; + esac + + $split && return 0 + + if [[ "$cur" == -* ]]; then + __@grubname@comp "$(__@grubname@_get_options_from_help '@grubname@-install')" + else + case "$last" in + --modules) + __@grubname@_list_modules + return + ;; + esac + + # Default complete with a filename + _filedir + fi +} +have @grubname@-install && \ + complete -F _@grubname@_install -o filenames @grubname@-install + +# +# @grubname@-mkfont +# +_@grubname@_mkfont () { + local cur + + COMPREPLY=() + cur=`_get_cword` + + if [[ "$cur" == -* ]]; then + __@grubname@comp "$(__@grubname@_get_options_from_help '@grubname@-mkfont')" + else + # Default complete with a filename + _filedir + fi +} +have @grubname@-mkfont && \ + complete -F _@grubname@_mkfont -o filenames @grubname@-mkfont + +# +# @grubname@-mkrescue +# +_@grubname@_mkrescue () { + local cur prev last + + COMPREPLY=() + cur=`_get_cword` + prev=${COMP_WORDS[COMP_CWORD-1]} + last=$(__@grubname@_get_last_option) + + if [[ "$cur" == -* ]]; then + __@grubname@comp "$(__@grubname@_get_options_from_help '@grubname@-mkrescue')" + else + case "$last" in + --modules) + __@grubname@_list_modules + return + ;; + esac + + # Default complete with a filename + _filedir + fi +} +have @grubname@-mkrescue && \ + complete -F _@grubname@_mkrescue -o filenames @grubname@-mkrescue + +# +# @grubname@-mkelfimage +# +_@grubname@_mkelfimage () { + local cur prev split=false + + COMPREPLY=() + cur=`_get_cword` + prev=${COMP_WORDS[COMP_CWORD-1]} + + _split_longopt && split=true + + case "$prev" in + -d|--directory|-p|--prefix) + _filedir -d + return + ;; + esac + + $split && return 0 + + if [[ "$cur" == -* ]]; then + __@grubname@comp "$(__@grubname@_get_options_from_help '@grubname@-mkelfimage')" + else + # Default complete with a filename + _filedir + fi +} +have @grubname@-mkelfimage && \ + complete -F _@grubname@_mkelfimage -o filenames @grubname@-mkelfimage + +# +# @grubname@-mkimage +# +_@grubname@_mkimage () { + local cur prev split=false + + COMPREPLY=() + cur=`_get_cword` + prev=${COMP_WORDS[COMP_CWORD-1]} + + _split_longopt && split=true + + case "$prev" in + -d|--directory|-p|--prefix) + _filedir -d + return + ;; + esac + + $split && return 0 + + if [[ "$cur" == -* ]]; then + __@grubname@comp "$(__@grubname@_get_options_from_help '@grubname@-mkimage')" + else + # Default complete with a filename + _filedir + fi +} +have @grubname@-mkimage && \ + complete -F _@grubname@_mkimage -o filenames @grubname@-mkimage + +# +# @grubname@-mkpasswd-pbkdf2 +# +_@grubname@_mkpasswd-pbkdf2 () { + local cur + + COMPREPLY=() + cur=`_get_cword` + + if [[ "$cur" == -* ]]; then + __@grubname@comp "$(__@grubname@_get_options_from_help '@grubname@-mkpasswd-pbkdf2')" + else + # Default complete with a filename + _filedir + fi +} +have @grubname@-mkpasswd-pbkdf2 && \ + complete -F _@grubname@_mkpasswd-pbkdf2 -o filenames @grubname@-mkpasswd-pbkdf2 + +# +# @grubname@-probe +# +_@grubname@_probe () { + local cur prev split=false + + COMPREPLY=() + cur=`_get_cword` + prev=${COMP_WORDS[COMP_CWORD-1]} + + _split_longopt && split=true + + case "$prev" in + -t|--target) + # Get target type from help + __@grubname@comp "$(@grubname@-probe --help | \ + awk -F "[()]" '/--target=/ { print $2 }' | \ + sed 's/|/ /g')" + return + ;; + esac + + $split && return 0 + + if [[ "$cur" == -* ]]; then + __@grubname@comp "$(__@grubname@_get_options_from_help '@grubname@-probe')" + else + # Default complete with a filename + _filedir + fi +} +have @grubname@-probe && \ + complete -F _@grubname@_probe -o filenames @grubname@-probe + +# +# @grubname@-script-check +# +_@grubname@_script-check () { + local cur + + COMPREPLY=() + cur=`_get_cword` + + if [[ "$cur" == -* ]]; then + __@grubname@comp "$(__@grubname@_get_options_from_help '@grubname@-script-check')" + else + # Default complete with a filename + _filedir + fi +} +have @grubname@-script-check && \ + complete -F _@grubname@_script-check -o filenames @grubname@-script-check + +# Local variables: +# mode: shell-script +# sh-basic-offset: 4 +# sh-indent-comment: t +# indent-tabs-mode: nil +# End: +# ex: ts=4 sw=4 et filetype=sh --MIMEBOUNDARY--