* 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
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 <http://www.gnu.org/licenses/>.
+# bash completion for grub
+
+...@grubname@_dir() {
+    local i c=1 root_dir boot_dir
+
+    for (( c=1; c <= ${#comp_wor...@]}; 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
+         }
+        ))
+}
+
+#
+# @grubn...@-set-default & @grubn...@-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 @grubn...@-set-default && \
+    complete -F _...@grubname@_set_entry -o filenames @grubn...@-set-default
+have @grubn...@-reboot && \
+ complete -F _...@grubname@_set_entry -o filenames @grubn...@-reboot
+
+#
+# @grubn...@-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 
'@grubn...@-editenv')
+                create list set unset"
+}
+have @grubn...@-editenv && \
+ complete -F _...@grubname@_editenv -o filenames @grubn...@-editenv
+
+#
+# @grubn...@-mkconfig
+#
+...@grubname@_mkconfig () {
+    local cur prev
+
+    COMPREPLY=()
+    cur=`_get_cword`
+
+    if [[ "$cur" == -* ]]; then
+        _...@grubname@comp "$(_...@grubname@_get_options_from_help 
'@grubn...@-mkconfig')"
+    else
+        _filedir
+    fi
+}
+have @grubn...@-mkconfig && \
+ complete -F _...@grubname@_mkconfig -o filenames @grubn...@-mkconfig
+
+#
+# @grubn...@-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 
'@grubn...@-setup')"
+    else
+        # Default complete with a filename
+        _filedir
+    fi
+}
+have @grubn...@-setup && \
+ complete -F _...@grubname@_setup -o filenames @grubn...@-setup
+
+#
+# @grubn...@-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 
'@grubn...@-install')"
+    else
+        case "$last" in
+            --modules)
+                _...@grubname@_list_modules
+                return
+                ;;
+        esac
+
+        # Default complete with a filename
+        _filedir
+    fi
+}
+have @grubn...@-install && \
+ complete -F _...@grubname@_install -o filenames @grubn...@-install
+
+#
+# @grubn...@-mkfont
+#
+...@grubname@_mkfont () {
+    local cur
+
+    COMPREPLY=()
+    cur=`_get_cword`
+
+    if [[ "$cur" == -* ]]; then
+        _...@grubname@comp "$(_...@grubname@_get_options_from_help 
'@grubn...@-mkfont')"
+    else
+        # Default complete with a filename
+        _filedir
+    fi
+}
+have @grubn...@-mkfont && \
+ complete -F _...@grubname@_mkfont -o filenames @grubn...@-mkfont
+
+#
+# @grubn...@-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 
'@grubn...@-mkrescue')"
+    else
+        case "$last" in
+            --modules)
+                _...@grubname@_list_modules
+                return
+                ;;
+        esac
+
+        # Default complete with a filename
+        _filedir
+    fi
+}
+have @grubn...@-mkrescue && \
+ complete -F _...@grubname@_mkrescue -o filenames @grubn...@-mkrescue
+
+#
+# @grubn...@-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 
'@grubn...@-mkelfimage')"
+    else
+        # Default complete with a filename
+        _filedir
+    fi
+}
+have @grubn...@-mkelfimage && \
+ complete -F _...@grubname@_mkelfimage -o filenames @grubn...@-mkelfimage
+
+#
+# @grubn...@-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 
'@grubn...@-mkimage')"
+    else
+        # Default complete with a filename
+        _filedir
+    fi
+}
+have @grubn...@-mkimage && \
+ complete -F _...@grubname@_mkimage -o filenames @grubn...@-mkimage
+
+#
+# @grubn...@-mkpasswd-pbkdf2
+#
+...@grubname@_mkpasswd-pbkdf2 () {
+    local cur
+
+    COMPREPLY=()
+    cur=`_get_cword`
+
+    if [[ "$cur" == -* ]]; then
+        _...@grubname@comp "$(_...@grubname@_get_options_from_help 
'@grubn...@-mkpasswd-pbkdf2')"
+    else
+        # Default complete with a filename
+        _filedir
+    fi
+}
+have @grubn...@-mkpasswd-pbkdf2 && \
+ complete -F _...@grubname@_mkpasswd-pbkdf2 -o filenames 
@grubn...@-mkpasswd-pbkdf2
+
+#
+# @grubn...@-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 "$(@grubn...@-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 
'@grubn...@-probe')"
+    else
+        # Default complete with a filename
+        _filedir
+    fi
+}
+have @grubn...@-probe && \
+ complete -F _...@grubname@_probe -o filenames @grubn...@-probe
+
+#
+# @grubn...@-script-check
+#
+...@grubname@_script-check () {
+    local cur
+
+    COMPREPLY=()
+    cur=`_get_cword`
+
+    if [[ "$cur" == -* ]]; then
+        _...@grubname@comp "$(_...@grubname@_get_options_from_help 
'@grubn...@-script-check')"
+    else
+        # Default complete with a filename
+        _filedir
+    fi
+}
+have @grubn...@-script-check && \
+ complete -F _...@grubname@_script-check -o filenames @grubn...@-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
_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/grub-devel

Reply via email to