grub-completion.bash.in 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. #
  2. # Bash completion for grub
  3. #
  4. # Copyright (C) 2010 Free Software Foundation, Inc.
  5. #
  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.
  10. #
  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.
  15. #
  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
  19. __grub_dir() {
  20. local i c=1 boot_dir
  21. for (( c=1; c <= ${#COMP_WORDS[@]}; c++ )); do
  22. i="${COMP_WORDS[c]}"
  23. case "$i" in
  24. --boot-directory)
  25. c=$((++c))
  26. i="${COMP_WORDS[c]}"
  27. boot_dir="${i##*=}";
  28. break
  29. ;;
  30. esac
  31. done
  32. boot_dir=${boot_dir-/@bootdirname@}
  33. echo "${boot_dir%/}/@grubdirname@"
  34. }
  35. # This function generates completion reply with compgen
  36. # - arg: accepts 1, 2, 3, or 4 arguments
  37. # $1 wordlist separate by space, tab or newline
  38. # $2 (optional) prefix to add
  39. # $3 (optional) current word to complete
  40. # $4 (optional) suffix to add
  41. __grubcomp () {
  42. local cur="${COMP_WORDS[COMP_CWORD]}"
  43. if [ $# -gt 2 ]; then
  44. cur="$3"
  45. fi
  46. case "$cur" in
  47. --*=)
  48. COMPREPLY=()
  49. ;;
  50. *)
  51. local IFS=' '$'\t'$'\n'
  52. COMPREPLY=($(compgen -P "${2-}" -W "${1-}" -S "${4-}" -- "$cur"))
  53. ;;
  54. esac
  55. }
  56. # Function that return long options from the help of the command
  57. # - arg: $1 (optional) command to get the long options from
  58. __grub_get_options_from_help () {
  59. local prog
  60. if [ $# -ge 1 ]; then
  61. prog="$1"
  62. else
  63. prog="${COMP_WORDS[0]}"
  64. fi
  65. local i IFS=" "$'\t'$'\n'
  66. for i in $(LC_ALL=C $prog --help)
  67. do
  68. case $i in
  69. --*) echo "${i%=*}";;
  70. esac
  71. done
  72. }
  73. # Function that return long options from the usage of the command
  74. # - arg: $1 (optional) command to get the long options from
  75. __grub_get_options_from_usage () {
  76. local prog
  77. if [ $# -ge 1 ]; then
  78. prog="$1"
  79. else
  80. prog="${COMP_WORDS[0]}"
  81. fi
  82. local i IFS=" "$'\t'$'\n'
  83. for i in $(LC_ALL=C $prog --usage)
  84. do
  85. case $i in
  86. \[--*\]) i=${i#[} # Remove leading [
  87. echo ${i%%?(=*)]} # Remove optional value and trailing ]
  88. ;;
  89. esac
  90. done
  91. }
  92. __grub_get_last_option () {
  93. local i
  94. for (( i=$COMP_CWORD-1; i > 0; i-- )); do
  95. if [[ "${COMP_WORDS[i]}" == -* ]]; then
  96. echo "${COMP_WORDS[i]}"
  97. break;
  98. fi
  99. done
  100. }
  101. __grub_list_menuentries () {
  102. local cur="${COMP_WORDS[COMP_CWORD]}"
  103. local config_file=$(__grub_dir)/grub.cfg
  104. if [ -f "$config_file" ];then
  105. local IFS=$'\n'
  106. COMPREPLY=( $(compgen \
  107. -W "$( awk -F "[\"']" '/menuentry/ { print $2 }' $config_file )" \
  108. -- "$cur" )) #'# Help emacs syntax highlighting
  109. fi
  110. }
  111. __grub_list_modules () {
  112. local grub_dir=$(__grub_dir)
  113. local IFS=$'\n'
  114. COMPREPLY=( $( compgen -f -X '!*/*.mod' -- "${grub_dir}/$cur" | {
  115. while read -r tmp; do
  116. [ -n $tmp ] && {
  117. tmp=${tmp##*/}
  118. printf '%s\n' ${tmp%.mod}
  119. }
  120. done
  121. }
  122. ))
  123. }
  124. #
  125. # grub-set-default & grub-reboot
  126. #
  127. _grub_set_entry () {
  128. local cur prev split=false
  129. COMPREPLY=()
  130. cur=`_get_cword`
  131. prev=${COMP_WORDS[COMP_CWORD-1]}
  132. _split_longopt && split=true
  133. case "$prev" in
  134. --boot-directory)
  135. _filedir -d
  136. return
  137. ;;
  138. esac
  139. $split && return 0
  140. if [[ "$cur" == -* ]]; then
  141. __grubcomp "$(__grub_get_options_from_help)"
  142. else
  143. # Default complete with a menuentry
  144. __grub_list_menuentries
  145. fi
  146. }
  147. __grub_set_default_program="@grub_set_default@"
  148. have ${__grub_set_default_program} && \
  149. complete -F _grub_set_entry -o filenames ${__grub_set_default_program}
  150. unset __grub_set_default_program
  151. __grub_reboot_program="@grub_reboot@"
  152. have ${__grub_reboot_program} && \
  153. complete -F _grub_set_entry -o filenames ${__grub_reboot_program}
  154. unset __grub_reboot_program
  155. #
  156. # grub-editenv
  157. #
  158. _grub_editenv () {
  159. local cur prev
  160. COMPREPLY=()
  161. cur=`_get_cword`
  162. prev=${COMP_WORDS[COMP_CWORD-1]}
  163. case "$prev" in
  164. create|list|set|unset)
  165. COMPREPLY=( "" )
  166. return
  167. ;;
  168. esac
  169. __grubcomp "$(__grub_get_options_from_help)
  170. create list set unset"
  171. }
  172. __grub_editenv_program="@grub_editenv@"
  173. have ${__grub_editenv_program} && \
  174. complete -F _grub_editenv -o filenames ${__grub_editenv_program}
  175. unset __grub_editenv_program
  176. #
  177. # grub-mkconfig
  178. #
  179. _grub_mkconfig () {
  180. local cur prev
  181. COMPREPLY=()
  182. cur=`_get_cword`
  183. if [[ "$cur" == -* ]]; then
  184. __grubcomp "$(__grub_get_options_from_help)"
  185. else
  186. _filedir
  187. fi
  188. }
  189. __grub_mkconfig_program="@grub_mkconfig@"
  190. have ${__grub_mkconfig_program} && \
  191. complete -F _grub_mkconfig -o filenames ${__grub_mkconfig_program}
  192. unset __grub_mkconfig_program
  193. #
  194. # grub-setup
  195. #
  196. _grub_setup () {
  197. local cur prev split=false
  198. COMPREPLY=()
  199. cur=`_get_cword`
  200. prev=${COMP_WORDS[COMP_CWORD-1]}
  201. _split_longopt && split=true
  202. case "$prev" in
  203. -d|--directory)
  204. _filedir -d
  205. return
  206. ;;
  207. esac
  208. $split && return 0
  209. if [[ "$cur" == -* ]]; then
  210. __grubcomp "$(__grub_get_options_from_help)"
  211. else
  212. # Default complete with a filename
  213. _filedir
  214. fi
  215. }
  216. __grub_bios_setup_program="@grub_bios_setup@"
  217. have ${__grub_bios_setup_program} && \
  218. complete -F _grub_setup -o filenames ${__grub_bios_setup_program}
  219. unset __grub_bios_setup_program
  220. __grub_sparc64_setup_program="@grub_sparc64_setup@"
  221. have ${__grub_sparc64_setup_program} && \
  222. complete -F _grub_setup -o filenames ${__grub_sparc64_setup_program}
  223. unset __grub_sparc64_setup_program
  224. #
  225. # grub-install
  226. #
  227. _grub_install () {
  228. local cur prev last split=false
  229. COMPREPLY=()
  230. cur=`_get_cword`
  231. prev=${COMP_WORDS[COMP_CWORD-1]}
  232. last=$(__grub_get_last_option)
  233. _split_longopt && split=true
  234. case "$prev" in
  235. --boot-directory)
  236. _filedir -d
  237. return
  238. ;;
  239. --disk-module)
  240. __grubcomp "biosdisk ata"
  241. return
  242. ;;
  243. esac
  244. $split && return 0
  245. if [[ "$cur" == -* ]]; then
  246. __grubcomp "$(__grub_get_options_from_help)"
  247. else
  248. case "$last" in
  249. --modules)
  250. __grub_list_modules
  251. return
  252. ;;
  253. esac
  254. # Default complete with a filename
  255. _filedir
  256. fi
  257. }
  258. __grub_install_program="@grub_install@"
  259. have ${__grub_install_program} && \
  260. complete -F _grub_install -o filenames ${__grub_install_program}
  261. unset __grub_install_program
  262. #
  263. # grub-mkfont
  264. #
  265. _grub_mkfont () {
  266. local cur
  267. COMPREPLY=()
  268. cur=`_get_cword`
  269. if [[ "$cur" == -* ]]; then
  270. __grubcomp "$(__grub_get_options_from_help)"
  271. else
  272. # Default complete with a filename
  273. _filedir
  274. fi
  275. }
  276. __grub_mkfont_program="@grub_mkfont@"
  277. have ${__grub_mkfont_program} && \
  278. complete -F _grub_mkfont -o filenames ${__grub_mkfont_program}
  279. unset __grub_mkfont_program
  280. #
  281. # grub-mkrescue
  282. #
  283. _grub_mkrescue () {
  284. local cur prev last
  285. COMPREPLY=()
  286. cur=`_get_cword`
  287. prev=${COMP_WORDS[COMP_CWORD-1]}
  288. last=$(__grub_get_last_option)
  289. if [[ "$cur" == -* ]]; then
  290. __grubcomp "$(__grub_get_options_from_help)"
  291. else
  292. case "$last" in
  293. --modules)
  294. __grub_list_modules
  295. return
  296. ;;
  297. esac
  298. # Default complete with a filename
  299. _filedir
  300. fi
  301. }
  302. __grub_mkrescue_program="@grub_mkrescue@"
  303. have ${__grub_mkrescue_program} && \
  304. complete -F _grub_mkrescue -o filenames ${__grub_mkrescue_program}
  305. unset __grub_mkrescue_program
  306. #
  307. # grub-mkimage
  308. #
  309. _grub_mkimage () {
  310. local cur prev split=false
  311. COMPREPLY=()
  312. cur=`_get_cword`
  313. prev=${COMP_WORDS[COMP_CWORD-1]}
  314. _split_longopt && split=true
  315. case "$prev" in
  316. -d|--directory|-p|--prefix)
  317. _filedir -d
  318. return
  319. ;;
  320. -O|--format)
  321. # Get available format from help
  322. local prog=${COMP_WORDS[0]}
  323. __grubcomp "$(LC_ALL=C $prog --help | \
  324. awk -F ":" '/available formats/ { print $2 }' | \
  325. sed 's/, / /g')"
  326. return
  327. ;;
  328. esac
  329. $split && return 0
  330. if [[ "$cur" == -* ]]; then
  331. __grubcomp "$(__grub_get_options_from_help)"
  332. else
  333. # Default complete with a filename
  334. _filedir
  335. fi
  336. }
  337. __grub_mkimage_program="@grub_mkimage@"
  338. have ${__grub_mkimage_program} && \
  339. complete -F _grub_mkimage -o filenames ${__grub_mkimage_program}
  340. unset __grub_mkimage_program
  341. #
  342. # grub-mkpasswd-pbkdf2
  343. #
  344. _grub_mkpasswd_pbkdf2 () {
  345. local cur
  346. COMPREPLY=()
  347. cur=`_get_cword`
  348. if [[ "$cur" == -* ]]; then
  349. __grubcomp "$(__grub_get_options_from_help)"
  350. else
  351. # Default complete with a filename
  352. _filedir
  353. fi
  354. }
  355. __grub_mkpasswd_pbkdf2_program="@grub_mkpasswd_pbkdf2@"
  356. have ${__grub_mkpasswd_pbkdf2_program} && \
  357. complete -F _grub_mkpasswd_pbkdf2 -o filenames ${__grub_mkpasswd_pbkdf2_program}
  358. unset __grub_mkpasswd_pbkdf2_program
  359. #
  360. # grub-probe
  361. #
  362. _grub_probe () {
  363. local cur prev split=false
  364. COMPREPLY=()
  365. cur=`_get_cword`
  366. prev=${COMP_WORDS[COMP_CWORD-1]}
  367. _split_longopt && split=true
  368. case "$prev" in
  369. -t|--target)
  370. # Get target type from help
  371. local prog=${COMP_WORDS[0]}
  372. __grubcomp "$(LC_ALL=C $prog --help | \
  373. awk -F "[()]" '/--target=/ { print $2 }' | \
  374. sed 's/|/ /g')"
  375. return
  376. ;;
  377. esac
  378. $split && return 0
  379. if [[ "$cur" == -* ]]; then
  380. __grubcomp "$(__grub_get_options_from_help)"
  381. else
  382. # Default complete with a filename
  383. _filedir
  384. fi
  385. }
  386. __grub_probe_program="@grub_probe@"
  387. have ${__grub_probe_program} && \
  388. complete -F _grub_probe -o filenames ${__grub_probe_program}
  389. unset __grub_probe_program
  390. #
  391. # grub-script-check
  392. #
  393. _grub_script_check () {
  394. local cur
  395. COMPREPLY=()
  396. cur=`_get_cword`
  397. if [[ "$cur" == -* ]]; then
  398. __grubcomp "$(__grub_get_options_from_help)"
  399. else
  400. # Default complete with a filename
  401. _filedir
  402. fi
  403. }
  404. __grub_script_check_program="@grub_script_check@"
  405. have ${__grub_script_check_program} && \
  406. complete -F _grub_script_check -o filenames ${__grub_script_check_program}
  407. # Local variables:
  408. # mode: shell-script
  409. # sh-basic-offset: 4
  410. # sh-indent-comment: t
  411. # indent-tabs-mode: nil
  412. # End:
  413. # ex: ts=4 sw=4 et filetype=sh