2 # Bash completion for grub
4 # Copyright (C) 2010 Free Software Foundation, Inc.
6 # GRUB is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # GRUB is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with GRUB. If not, see <http://www.gnu.org/licenses/>.
18 # bash completion for grub
23 for (( c
=1; c
<= ${#COMP_WORDS[@]}; c
++ )); do
34 boot_dir
=${boot_dir-/@bootdirname@}
35 echo "${boot_dir%/}/@grubdirname@"
39 # This function generates completion reply with compgen
40 # - arg: accepts 1, 2, 3, or 4 arguments
41 # $1 wordlist separate by space, tab or newline
42 # $2 (optional) prefix to add
43 # $3 (optional) current word to complete
44 # $4 (optional) suffix to add
46 local cur
="${COMP_WORDS[COMP_CWORD]}"
55 local IFS
=' '$
'\t'$
'\n'
56 COMPREPLY
=($
(compgen
-P "${2-}" -W "${1-}" -S "${4-}" -- "$cur"))
61 # Function that return long options from the help of the command
62 # - arg: $1 (optional) command to get the long options from
63 __grub_get_options_from_help
() {
69 prog
="${COMP_WORDS[0]}"
72 local i IFS
=" "$
'\t'$
'\n'
73 for i
in $
(LC_ALL
=C
$prog --help)
81 # Function that return long options from the usage of the command
82 # - arg: $1 (optional) command to get the long options from
83 __grub_get_options_from_usage
() {
89 prog
="${COMP_WORDS[0]}"
92 local i IFS
=" "$
'\t'$
'\n'
93 for i
in $
(LC_ALL
=C
$prog --usage)
96 \
[--*\
]) i
=${i#[} # Remove leading [
97 echo ${i%%?(=*)]} # Remove optional value and trailing ]
103 __grub_get_last_option
() {
105 for (( i
=$COMP_CWORD-1; i
> 0; i--
)); do
106 if [[ "${COMP_WORDS[i]}" == -* ]]; then
107 echo "${COMP_WORDS[i]}"
113 __grub_list_menuentries
() {
114 local cur
="${COMP_WORDS[COMP_CWORD]}"
115 local config_file
=$
(__grub_dir
)/grub.cfg
117 if [ -f "$config_file" ];then
119 COMPREPLY
=( $
(compgen \
120 -W "$( awk -F "[\"']" '/menuentry
/ { print
$2 }' $config_file )" \
121 -- "$cur" )) #'# Help emacs syntax highlighting
125 __grub_list_modules
() {
126 local grub_dir
=$
(__grub_dir
)
128 COMPREPLY
=( $
( compgen
-f -X '!*/*.mod' -- "${grub_dir}/$cur" |
{
129 while read -r tmp
; do
132 printf '%s\n' ${tmp%.mod}
140 # grub-set-default & grub-reboot
143 local cur prev
split=false
147 prev
=${COMP_WORDS[COMP_CWORD-1]}
149 _split_longopt
&& split=true
160 if [[ "$cur" == -* ]]; then
161 __grubcomp
"$(__grub_get_options_from_help)"
163 # Default complete with a menuentry
164 __grub_list_menuentries
168 __grub_set_default_program
="@grub_set_default@"
169 have
${__grub_set_default_program} && \
170 complete
-F _grub_set_entry
-o filenames
${__grub_set_default_program}
171 unset __grub_set_default_program
173 __grub_reboot_program
="@grub_reboot@"
174 have
${__grub_reboot_program} && \
175 complete
-F _grub_set_entry
-o filenames
${__grub_reboot_program}
176 unset __grub_reboot_program
187 prev
=${COMP_WORDS[COMP_CWORD-1]}
190 create|list|
set|
unset)
196 __grubcomp
"$(__grub_get_options_from_help)
197 create list set unset"
200 __grub_editenv_program
="@grub_editenv@"
201 have
${__grub_editenv_program} && \
202 complete
-F _grub_editenv
-o filenames
${__grub_editenv_program}
203 unset __grub_editenv_program
215 if [[ "$cur" == -* ]]; then
216 __grubcomp
"$(__grub_get_options_from_help)"
221 __grub_mkconfig_program
="@grub_mkconfig@"
222 have
${__grub_mkconfig_program} && \
223 complete
-F _grub_mkconfig
-o filenames
${__grub_mkconfig_program}
224 unset __grub_mkconfig_program
231 local cur prev
split=false
235 prev
=${COMP_WORDS[COMP_CWORD-1]}
237 _split_longopt
&& split=true
248 if [[ "$cur" == -* ]]; then
249 __grubcomp
"$(__grub_get_options_from_help)"
251 # Default complete with a filename
256 __grub_bios_setup_program
="@grub_bios_setup@"
257 have
${__grub_bios_setup_program} && \
258 complete
-F _grub_setup
-o filenames
${__grub_bios_setup_program}
259 unset __grub_bios_setup_program
261 __grub_sparc64_setup_program
="@grub_sparc64_setup@"
262 have
${__grub_sparc64_setup_program} && \
263 complete
-F _grub_setup
-o filenames
${__grub_sparc64_setup_program}
264 unset __grub_sparc64_setup_program
271 local cur prev last
split=false
275 prev
=${COMP_WORDS[COMP_CWORD-1]}
276 last
=$
(__grub_get_last_option
)
278 _split_longopt
&& split=true
286 __grubcomp
"biosdisk ata"
293 if [[ "$cur" == -* ]]; then
294 __grubcomp
"$(__grub_get_options_from_help)"
303 # Default complete with a filename
307 __grub_install_program
="@grub_install@"
308 have
${__grub_install_program} && \
309 complete
-F _grub_install
-o filenames
${__grub_install_program}
310 unset __grub_install_program
322 if [[ "$cur" == -* ]]; then
323 __grubcomp
"$(__grub_get_options_from_help)"
325 # Default complete with a filename
329 __grub_mkfont_program
="@grub_mkfont@"
330 have
${__grub_mkfont_program} && \
331 complete
-F _grub_mkfont
-o filenames
${__grub_mkfont_program}
332 unset __grub_mkfont_program
343 prev
=${COMP_WORDS[COMP_CWORD-1]}
344 last
=$
(__grub_get_last_option
)
346 if [[ "$cur" == -* ]]; then
347 __grubcomp
"$(__grub_get_options_from_help)"
356 # Default complete with a filename
360 __grub_mkrescue_program
="@grub_mkrescue@"
361 have
${__grub_mkrescue_program} && \
362 complete
-F _grub_mkrescue
-o filenames
${__grub_mkrescue_program}
363 unset __grub_mkrescue_program
370 local cur prev
split=false
374 prev
=${COMP_WORDS[COMP_CWORD-1]}
376 _split_longopt
&& split=true
379 -d|
--directory|
-p|
--prefix)
384 # Get available format from help
385 local prog
=${COMP_WORDS[0]}
386 __grubcomp
"$(LC_ALL=C $prog --help | \
387 awk -F ":" '/available formats/ { print $2 }' | \
395 if [[ "$cur" == -* ]]; then
396 __grubcomp
"$(__grub_get_options_from_help)"
398 # Default complete with a filename
402 __grub_mkimage_program
="@grub_mkimage@"
403 have
${__grub_mkimage_program} && \
404 complete
-F _grub_mkimage
-o filenames
${__grub_mkimage_program}
405 unset __grub_mkimage_program
409 # grub-mkpasswd-pbkdf2
411 _grub_mkpasswd_pbkdf2
() {
417 if [[ "$cur" == -* ]]; then
418 __grubcomp
"$(__grub_get_options_from_help)"
420 # Default complete with a filename
424 __grub_mkpasswd_pbkdf2_program
="@grub_mkpasswd_pbkdf2@"
425 have
${__grub_mkpasswd_pbkdf2_program} && \
426 complete
-F _grub_mkpasswd_pbkdf2
-o filenames
${__grub_mkpasswd_pbkdf2_program}
427 unset __grub_mkpasswd_pbkdf2_program
434 local cur prev
split=false
438 prev
=${COMP_WORDS[COMP_CWORD-1]}
440 _split_longopt
&& split=true
444 # Get target type from help
445 local prog
=${COMP_WORDS[0]}
446 __grubcomp
"$(LC_ALL=C $prog --help | \
447 awk -F "[()]" '/--target=/ { print $2 }' | \
455 if [[ "$cur" == -* ]]; then
456 __grubcomp
"$(__grub_get_options_from_help)"
458 # Default complete with a filename
462 __grub_probe_program
="@grub_probe@"
463 have
${__grub_probe_program} && \
464 complete
-F _grub_probe
-o filenames
${__grub_probe_program}
465 unset __grub_probe_program
471 _grub_script_check
() {
477 if [[ "$cur" == -* ]]; then
478 __grubcomp
"$(__grub_get_options_from_help)"
480 # Default complete with a filename
484 __grub_script_check_program
="@grub_script_check@"
485 have
${__grub_script_check_program} && \
486 complete
-F _grub_script_check
-o filenames
${__grub_script_check_program}
492 # sh-indent-comment: t
493 # indent-tabs-mode: nil
495 # ex: ts=4 sw=4 et filetype=sh