Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / util / bash-completion.d / grub-completion.bash.in
blob44bf135b9f83e9201150fb36d96d8ce451e7c804
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
20 __grub_dir() {
21 local i c=1 boot_dir
23 for (( c=1; c <= ${#COMP_WORDS[@]}; c++ )); do
24 i="${COMP_WORDS[c]}"
25 case "$i" in
26 --boot-directory)
27 c=$((++c))
28 i="${COMP_WORDS[c]}"
29 boot_dir="${i##*=}";
30 break
32 esac
33 done
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
45 __grubcomp () {
46 local cur="${COMP_WORDS[COMP_CWORD]}"
47 if [ $# -gt 2 ]; then
48 cur="$3"
50 case "$cur" in
51 --*=)
52 COMPREPLY=()
55 local IFS=' '$'\t'$'\n'
56 COMPREPLY=($(compgen -P "${2-}" -W "${1-}" -S "${4-}" -- "$cur"))
58 esac
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 () {
64 local prog
66 if [ $# -ge 1 ]; then
67 prog="$1"
68 else
69 prog="${COMP_WORDS[0]}"
72 local i IFS=" "$'\t'$'\n'
73 for i in $(LC_ALL=C $prog --help)
75 case $i in
76 --*) echo "${i%=*}";;
77 esac
78 done
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 () {
84 local prog
86 if [ $# -ge 1 ]; then
87 prog="$1"
88 else
89 prog="${COMP_WORDS[0]}"
92 local i IFS=" "$'\t'$'\n'
93 for i in $(LC_ALL=C $prog --usage)
95 case $i in
96 \[--*\]) i=${i#[} # Remove leading [
97 echo ${i%%?(=*)]} # Remove optional value and trailing ]
99 esac
100 done
103 __grub_get_last_option () {
104 local i
105 for (( i=$COMP_CWORD-1; i > 0; i-- )); do
106 if [[ "${COMP_WORDS[i]}" == -* ]]; then
107 echo "${COMP_WORDS[i]}"
108 break;
110 done
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
118 local IFS=$'\n'
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)
127 local IFS=$'\n'
128 COMPREPLY=( $( compgen -f -X '!*/*.mod' -- "${grub_dir}/$cur" | {
129 while read -r tmp; do
130 [ -n $tmp ] && {
131 tmp=${tmp##*/}
132 printf '%s\n' ${tmp%.mod}
134 done
140 # grub-set-default & grub-reboot
142 _grub_set_entry () {
143 local cur prev split=false
145 COMPREPLY=()
146 cur=`_get_cword`
147 prev=${COMP_WORDS[COMP_CWORD-1]}
149 _split_longopt && split=true
151 case "$prev" in
152 --boot-directory)
153 _filedir -d
154 return
156 esac
158 $split && return 0
160 if [[ "$cur" == -* ]]; then
161 __grubcomp "$(__grub_get_options_from_help)"
162 else
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
180 # grub-editenv
182 _grub_editenv () {
183 local cur prev
185 COMPREPLY=()
186 cur=`_get_cword`
187 prev=${COMP_WORDS[COMP_CWORD-1]}
189 case "$prev" in
190 create|list|set|unset)
191 COMPREPLY=( "" )
192 return
194 esac
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
207 # grub-mkconfig
209 _grub_mkconfig () {
210 local cur prev
212 COMPREPLY=()
213 cur=`_get_cword`
215 if [[ "$cur" == -* ]]; then
216 __grubcomp "$(__grub_get_options_from_help)"
217 else
218 _filedir
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
228 # grub-setup
230 _grub_setup () {
231 local cur prev split=false
233 COMPREPLY=()
234 cur=`_get_cword`
235 prev=${COMP_WORDS[COMP_CWORD-1]}
237 _split_longopt && split=true
239 case "$prev" in
240 -d|--directory)
241 _filedir -d
242 return
244 esac
246 $split && return 0
248 if [[ "$cur" == -* ]]; then
249 __grubcomp "$(__grub_get_options_from_help)"
250 else
251 # Default complete with a filename
252 _filedir
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
268 # grub-install
270 _grub_install () {
271 local cur prev last split=false
273 COMPREPLY=()
274 cur=`_get_cword`
275 prev=${COMP_WORDS[COMP_CWORD-1]}
276 last=$(__grub_get_last_option)
278 _split_longopt && split=true
280 case "$prev" in
281 --boot-directory)
282 _filedir -d
283 return
285 --disk-module)
286 __grubcomp "biosdisk ata"
287 return
289 esac
291 $split && return 0
293 if [[ "$cur" == -* ]]; then
294 __grubcomp "$(__grub_get_options_from_help)"
295 else
296 case "$last" in
297 --modules)
298 __grub_list_modules
299 return
301 esac
303 # Default complete with a filename
304 _filedir
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
314 # grub-mkfont
316 _grub_mkfont () {
317 local cur
319 COMPREPLY=()
320 cur=`_get_cword`
322 if [[ "$cur" == -* ]]; then
323 __grubcomp "$(__grub_get_options_from_help)"
324 else
325 # Default complete with a filename
326 _filedir
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
336 # grub-mkrescue
338 _grub_mkrescue () {
339 local cur prev last
341 COMPREPLY=()
342 cur=`_get_cword`
343 prev=${COMP_WORDS[COMP_CWORD-1]}
344 last=$(__grub_get_last_option)
346 if [[ "$cur" == -* ]]; then
347 __grubcomp "$(__grub_get_options_from_help)"
348 else
349 case "$last" in
350 --modules)
351 __grub_list_modules
352 return
354 esac
356 # Default complete with a filename
357 _filedir
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
367 # grub-mkimage
369 _grub_mkimage () {
370 local cur prev split=false
372 COMPREPLY=()
373 cur=`_get_cword`
374 prev=${COMP_WORDS[COMP_CWORD-1]}
376 _split_longopt && split=true
378 case "$prev" in
379 -d|--directory|-p|--prefix)
380 _filedir -d
381 return
383 -O|--format)
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 }' | \
388 sed 's/, / /g')"
389 return
391 esac
393 $split && return 0
395 if [[ "$cur" == -* ]]; then
396 __grubcomp "$(__grub_get_options_from_help)"
397 else
398 # Default complete with a filename
399 _filedir
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 () {
412 local cur
414 COMPREPLY=()
415 cur=`_get_cword`
417 if [[ "$cur" == -* ]]; then
418 __grubcomp "$(__grub_get_options_from_help)"
419 else
420 # Default complete with a filename
421 _filedir
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
431 # grub-probe
433 _grub_probe () {
434 local cur prev split=false
436 COMPREPLY=()
437 cur=`_get_cword`
438 prev=${COMP_WORDS[COMP_CWORD-1]}
440 _split_longopt && split=true
442 case "$prev" in
443 -t|--target)
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 }' | \
448 sed 's/|/ /g')"
449 return
451 esac
453 $split && return 0
455 if [[ "$cur" == -* ]]; then
456 __grubcomp "$(__grub_get_options_from_help)"
457 else
458 # Default complete with a filename
459 _filedir
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
469 # grub-script-check
471 _grub_script_check () {
472 local cur
474 COMPREPLY=()
475 cur=`_get_cword`
477 if [[ "$cur" == -* ]]; then
478 __grubcomp "$(__grub_get_options_from_help)"
479 else
480 # Default complete with a filename
481 _filedir
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}
489 # Local variables:
490 # mode: shell-script
491 # sh-basic-offset: 4
492 # sh-indent-comment: t
493 # indent-tabs-mode: nil
494 # End:
495 # ex: ts=4 sw=4 et filetype=sh