2N/A# Bash completion for grub
2N/A# Copyright (C) 2010 Free Software Foundation, Inc.
2N/A# GRUB is free software: you can redistribute it
and/or modify
2N/A# it under the terms of the GNU General Public License as published by
2N/A# the Free Software Foundation, either version 3 of the License, or
2N/A# (at your option) any later version.
2N/A# GRUB is distributed in the hope that it will be useful,
2N/A# but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A# GNU General Public License for more details.
2N/A# You should have received a copy of the GNU General Public License
2N/A# bash completion for grub
2N/A local i c=1 boot_dir
2N/A for (( c=1; c <= ${#COMP_WORDS[@]}; c++ )); do
2N/A i="${COMP_WORDS[c]}"
2N/A i="${COMP_WORDS[c]}"
2N/A boot_dir="${i##*=}";
2N/A boot_dir=${boot_dir-/@bootdirname@}
2N/A echo "${boot_dir%/}/@grubdirname@"
2N/A# This function generates completion reply with compgen
2N/A# - arg: accepts 1, 2, 3, or 4 arguments
2N/A# $1 wordlist separate by space, tab or newline
2N/A# $2 (optional) prefix to add
2N/A# $3 (optional) current word to complete
2N/A# $4 (optional) suffix to add
2N/A local cur="${COMP_WORDS[COMP_CWORD]}"
2N/A if [ $# -gt 2 ]; then
2N/A local IFS=' '$'\t'$'\n'
2N/A COMPREPLY=($(compgen -P "${2-}" -W "${1-}" -S "${4-}" -- "$cur"))
2N/A# Function that return long options from the help of the command
2N/A# - arg: $1 (optional) command to get the long options from
2N/A__grub_get_options_from_help () {
2N/A if [ $# -ge 1 ]; then
2N/A prog="${COMP_WORDS[0]}"
2N/A local i IFS=" "$'\t'$'\n'
2N/A for i in $(LC_ALL=C $prog --help)
2N/A --*) echo "${i%=*}";;
2N/A# Function that return long options from the usage of the command
2N/A# - arg: $1 (optional) command to get the long options from
2N/A__grub_get_options_from_usage () {
2N/A if [ $# -ge 1 ]; then
2N/A prog="${COMP_WORDS[0]}"
2N/A local i IFS=" "$'\t'$'\n'
2N/A for i in $(LC_ALL=C $prog --usage)
2N/A \[--*\]) i=${i#[} # Remove leading [
2N/A echo ${i%%?(=*)]} # Remove optional value and trailing ]
2N/A__grub_get_last_option () {
2N/A for (( i=$COMP_CWORD-1; i > 0; i-- )); do
2N/A if [[ "${COMP_WORDS[i]}" == -* ]]; then
2N/A echo "${COMP_WORDS[i]}"
2N/A__grub_list_menuentries () {
2N/A local cur="${COMP_WORDS[COMP_CWORD]}"
2N/A if [ -f "$config_file" ];then
2N/A COMPREPLY=( $(compgen \
2N/A -W "$( awk -F "[\"']" '/menuentry/ { print $2 }' $config_file )" \
2N/A -- "$cur" )) #'# Help emacs syntax highlighting
2N/A__grub_list_modules () {
2N/A local grub_dir=$(__grub_dir)
2N/A COMPREPLY=( $( compgen -f -X '!*/*.mod' -- "${grub_dir}/$cur" | {
2N/A while read -r tmp; do
2N/A printf '%s\n' ${tmp%.mod}
2N/A# grub-set-default & grub-reboot
2N/A local cur prev split=false
2N/A prev=${COMP_WORDS[COMP_CWORD-1]}
2N/A _split_longopt && split=true
2N/A if [[ "$cur" == -* ]]; then
2N/A __grubcomp "$(__grub_get_options_from_help)"
2N/A # Default complete with a menuentry
2N/A __grub_list_menuentries
2N/A__grub_set_default_program=$( echo grub-set-default | sed "@program_transform_name@" )
2N/Ahave ${__grub_set_default_program} && \
2N/A complete -F _grub_set_entry -o filenames ${__grub_set_default_program}
2N/Aunset __grub_set_default_program
2N/A__grub_reboot_program=$( echo grub-reboot | sed "@program_transform_name@" )
2N/Ahave ${__grub_reboot_program} && \
2N/A complete -F _grub_set_entry -o filenames ${__grub_reboot_program}
2N/Aunset __grub_reboot_program
2N/A prev=${COMP_WORDS[COMP_CWORD-1]}
2N/A create|list|set|unset)
2N/A __grubcomp "$(__grub_get_options_from_help)
2N/A create list set unset"
2N/A__grub_editenv_program=$( echo grub-editenv | sed "@program_transform_name@" )
2N/Ahave ${__grub_editenv_program} && \
2N/A complete -F _grub_editenv -o filenames ${__grub_editenv_program}
2N/Aunset __grub_editenv_program
2N/A if [[ "$cur" == -* ]]; then
2N/A __grubcomp "$(__grub_get_options_from_help)"
2N/A__grub_mkconfig_program=$( echo grub-mkconfig | sed "@program_transform_name@" )
2N/Ahave ${__grub_mkconfig_program} && \
2N/A complete -F _grub_mkconfig -o filenames ${__grub_mkconfig_program}
2N/Aunset __grub_mkconfig_program
2N/A local cur prev split=false
2N/A prev=${COMP_WORDS[COMP_CWORD-1]}
2N/A _split_longopt && split=true
2N/A if [[ "$cur" == -* ]]; then
2N/A __grubcomp "$(__grub_get_options_from_help)"
2N/A # Default complete with a filename
2N/A__grub_setup_program=$( echo grub-setup | sed "@program_transform_name@" )
2N/Ahave ${__grub_setup_program} && \
2N/A complete -F _grub_setup -o filenames ${__grub_setup_program}
2N/Aunset __grub_setup_program
2N/A local cur prev last split=false
2N/A prev=${COMP_WORDS[COMP_CWORD-1]}
2N/A last=$(__grub_get_last_option)
2N/A _split_longopt && split=true
2N/A __grubcomp "biosdisk ata"
2N/A if [[ "$cur" == -* ]]; then
2N/A __grubcomp "$(__grub_get_options_from_help)"
2N/A # Default complete with a filename
2N/A__grub_install_program=$( echo grub-install | sed "@program_transform_name@" )
2N/Ahave ${__grub_install_program} && \
2N/A complete -F _grub_install -o filenames ${__grub_install_program}
2N/Aunset __grub_install_program
2N/A if [[ "$cur" == -* ]]; then
2N/A __grubcomp "$(__grub_get_options_from_help)"
2N/A # Default complete with a filename
2N/A__grub_mkfont_program=$( echo grub-mkfont | sed "@program_transform_name@" )
2N/Ahave ${__grub_mkfont_program} && \
2N/A complete -F _grub_mkfont -o filenames ${__grub_mkfont_program}
2N/Aunset __grub_mkfont_program
2N/A prev=${COMP_WORDS[COMP_CWORD-1]}
2N/A last=$(__grub_get_last_option)
2N/A if [[ "$cur" == -* ]]; then
2N/A __grubcomp "$(__grub_get_options_from_help)"
2N/A # Default complete with a filename
2N/A__grub_mkrescue_program=$( echo grub-mkrescue | sed "@program_transform_name@" )
2N/Ahave ${__grub_mkrescue_program} && \
2N/A complete -F _grub_mkrescue -o filenames ${__grub_mkrescue_program}
2N/Aunset __grub_mkrescue_program
2N/A local cur prev split=false
2N/A prev=${COMP_WORDS[COMP_CWORD-1]}
2N/A _split_longopt && split=true
2N/A -d|--directory|-p|--prefix)
2N/A # Get available format from help
2N/A local prog=${COMP_WORDS[0]}
2N/A __grubcomp "$(LC_ALL=C $prog --help | \
2N/A awk -F ":" '/available formats/ { print $2 }' | \
2N/A if [[ "$cur" == -* ]]; then
2N/A __grubcomp "$(__grub_get_options_from_help)"
2N/A # Default complete with a filename
2N/A__grub_mkimage_program=$( echo grub-mkimage | sed "@program_transform_name@" )
2N/Ahave ${__grub_mkimage_program} && \
2N/A complete -F _grub_mkimage -o filenames ${__grub_mkimage_program}
2N/Aunset __grub_mkimage_program
2N/A# grub-mkpasswd-pbkdf2
2N/A_grub_mkpasswd_pbkdf2 () {
2N/A if [[ "$cur" == -* ]]; then
2N/A __grubcomp "$(__grub_get_options_from_help)"
2N/A # Default complete with a filename
2N/A__grub_mkpasswd_pbkdf2_program=$( echo grub-mkpasswd-pbkdf2 | sed "@program_transform_name@" )
2N/Ahave ${__grub_mkpasswd_pbkdf2_program} && \
2N/A complete -F _grub_mkpasswd_pbkdf2 -o filenames ${__grub_mkpasswd_pbkdf2_program}
2N/Aunset __grub_mkpasswd_pbkdf2_program
2N/A local cur prev split=false
2N/A prev=${COMP_WORDS[COMP_CWORD-1]}
2N/A _split_longopt && split=true
2N/A # Get target type from help
2N/A local prog=${COMP_WORDS[0]}
2N/A __grubcomp "$(LC_ALL=C $prog --help | \
2N/A awk -F "[()]" '/--target=/ { print $2 }' | \
2N/A if [[ "$cur" == -* ]]; then
2N/A __grubcomp "$(__grub_get_options_from_help)"
2N/A # Default complete with a filename
2N/A__grub_probe_program=$( echo grub-probe | sed "@program_transform_name@" )
2N/Ahave ${__grub_probe_program} && \
2N/A complete -F _grub_probe -o filenames ${__grub_probe_program}
2N/Aunset __grub_probe_program
2N/A_grub_script_check () {
2N/A if [[ "$cur" == -* ]]; then
2N/A __grubcomp "$(__grub_get_options_from_help)"
2N/A # Default complete with a filename
2N/A__grub_script_check_program=$( echo grub-script-check | sed "@program_transform_name@" )
2N/Ahave ${__grub_script_check_program} && \
2N/A complete -F _grub_script_check -o filenames ${__grub_script_check_program}
2N/A# sh-indent-comment: t
2N/A# indent-tabs-mode: nil
2N/A# ex: ts=4 sw=4 et filetype=sh