grub-completion.bash.in 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 line IFS=' '$'\t'$'\n'
  52. COMPREPLY=()
  53. while read -r line; do
  54. COMPREPLY+=("${line}")
  55. done < <(compgen -P "${2-}" -W "${1-}" -S "${4-}" -- "$cur")
  56. ;;
  57. esac
  58. }
  59. # Function that return long options from the help of the command
  60. # - arg: $1 (optional) command to get the long options from
  61. # shellcheck disable=SC2120
  62. __grub_get_options_from_help () {
  63. local prog
  64. if [ $# -ge 1 ]; then
  65. prog="$1"
  66. else
  67. prog="${COMP_WORDS[0]}"
  68. fi
  69. local i IFS=" "$'\t'$'\n'
  70. for i in $(LC_ALL=C $prog --help)
  71. do
  72. case $i in
  73. --*) echo "${i%=*}";;
  74. esac
  75. done
  76. }
  77. # Function that return long options from the usage of the command
  78. # - arg: $1 (optional) command to get the long options from
  79. __grub_get_options_from_usage () {
  80. local prog
  81. if [ $# -ge 1 ]; then
  82. prog="$1"
  83. else
  84. prog="${COMP_WORDS[0]}"
  85. fi
  86. local i IFS=" "$'\t'$'\n'
  87. for i in $(LC_ALL=C $prog --usage)
  88. do
  89. case $i in
  90. \[--*\]) i=${i#[} # Remove leading [
  91. echo ${i%%?(=*)]} # Remove optional value and trailing ]
  92. ;;
  93. esac
  94. done
  95. }
  96. __grub_get_last_option () {
  97. local i
  98. for (( i=$COMP_CWORD-1; i > 0; i-- )); do
  99. if [[ "${COMP_WORDS[i]}" == -* ]]; then
  100. echo "${COMP_WORDS[i]}"
  101. break;
  102. fi
  103. done
  104. }
  105. __grub_list_menuentries () {
  106. local cur="${COMP_WORDS[COMP_CWORD]}"
  107. local config_file
  108. config_file=$(__grub_dir)/grub.cfg
  109. if [ -f "$config_file" ];then
  110. local line IFS=$'\n'
  111. COMPREPLY=()
  112. while read -r line; do
  113. COMPREPLY+=("${line}")
  114. done < <(compgen \
  115. -W "$( awk -F "[\"']" '/menuentry/ { print $2 }' $config_file )" \
  116. -- "$cur" ) #'# Help emacs syntax highlighting
  117. fi
  118. }
  119. __grub_list_modules () {
  120. local grub_dir
  121. grub_dir=$(__grub_dir)
  122. local line tmp IFS=$'\n'
  123. COMPREPLY=()
  124. while read -r line; do
  125. COMPREPLY+=("${line}")
  126. done < <(compgen -f -X '!*/*.mod' -- "${grub_dir}/$cur" | {
  127. while read -r tmp; do
  128. [ -n "$tmp" ] && {
  129. tmp=${tmp##*/}
  130. printf '%s\n' ${tmp%.mod}
  131. }
  132. done
  133. })
  134. }
  135. #
  136. # grub-set-default & grub-reboot
  137. #
  138. __grub_set_entry () {
  139. local cur prev words cword split
  140. _init_completion -s || return
  141. COMPREPLY=()
  142. case "$prev" in
  143. --boot-directory)
  144. _filedir -d
  145. return
  146. ;;
  147. esac
  148. $split && return 0
  149. if [[ "$cur" == -* ]]; then
  150. __grubcomp "$(__grub_get_options_from_help)"
  151. else
  152. # Default complete with a menuentry
  153. __grub_list_menuentries
  154. fi
  155. }
  156. #
  157. # grub-editenv
  158. #
  159. __grub_editenv () {
  160. local cur prev words cword
  161. _init_completion || return
  162. COMPREPLY=()
  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. #
  173. # grub-mkconfig
  174. #
  175. __grub_mkconfig () {
  176. local cur prev words cword
  177. _init_completion || return
  178. COMPREPLY=()
  179. if [[ "$cur" == -* ]]; then
  180. __grubcomp "$(__grub_get_options_from_help)"
  181. else
  182. _filedir
  183. fi
  184. }
  185. #
  186. # grub-setup
  187. #
  188. __grub_setup () {
  189. local cur prev words cword split
  190. _init_completion -s || return
  191. COMPREPLY=()
  192. case "$prev" in
  193. -d|--directory)
  194. _filedir -d
  195. return
  196. ;;
  197. esac
  198. $split && return 0
  199. if [[ "$cur" == -* ]]; then
  200. __grubcomp "$(__grub_get_options_from_help)"
  201. else
  202. # Default complete with a filename
  203. _filedir
  204. fi
  205. }
  206. #
  207. # grub-install
  208. #
  209. __grub_install () {
  210. local cur prev words cword split last
  211. _init_completion -s || return
  212. COMPREPLY=()
  213. last=$(__grub_get_last_option)
  214. case "$prev" in
  215. --boot-directory)
  216. _filedir -d
  217. return
  218. ;;
  219. --disk-module)
  220. __grubcomp "biosdisk ata"
  221. return
  222. ;;
  223. esac
  224. $split && return 0
  225. if [[ "$cur" == -* ]]; then
  226. __grubcomp "$(__grub_get_options_from_help)"
  227. else
  228. case "$last" in
  229. --modules)
  230. __grub_list_modules
  231. return
  232. ;;
  233. esac
  234. # Default complete with a filename
  235. _filedir
  236. fi
  237. }
  238. #
  239. # grub-mkfont
  240. #
  241. __grub_mkfont () {
  242. local cur prev words cword
  243. _init_completion || return
  244. COMPREPLY=()
  245. if [[ "$cur" == -* ]]; then
  246. __grubcomp "$(__grub_get_options_from_help)"
  247. else
  248. # Default complete with a filename
  249. _filedir
  250. fi
  251. }
  252. #
  253. # grub-mkrescue
  254. #
  255. __grub_mkrescue () {
  256. local cur prev words cword last
  257. _init_completion || return
  258. COMPREPLY=()
  259. last=$(__grub_get_last_option)
  260. if [[ "$cur" == -* ]]; then
  261. __grubcomp "$(__grub_get_options_from_help)"
  262. else
  263. case "$last" in
  264. --modules)
  265. __grub_list_modules
  266. return
  267. ;;
  268. esac
  269. # Default complete with a filename
  270. _filedir
  271. fi
  272. }
  273. #
  274. # grub-mkimage
  275. #
  276. __grub_mkimage () {
  277. local cur prev words cword split
  278. _init_completion -s || return
  279. COMPREPLY=()
  280. case "$prev" in
  281. -d|--directory|-p|--prefix)
  282. _filedir -d
  283. return
  284. ;;
  285. -O|--format)
  286. # Get available format from help
  287. local prog=${COMP_WORDS[0]}
  288. __grubcomp "$(LC_ALL=C $prog --help | \
  289. awk -F ":" '/available formats/ { print $2 }' | \
  290. sed 's/, / /g')"
  291. return
  292. ;;
  293. esac
  294. $split && return 0
  295. if [[ "$cur" == -* ]]; then
  296. __grubcomp "$(__grub_get_options_from_help)"
  297. else
  298. # Default complete with a filename
  299. _filedir
  300. fi
  301. }
  302. #
  303. # grub-mkpasswd-pbkdf2
  304. #
  305. __grub_mkpasswd_pbkdf2 () {
  306. local cur prev words cword
  307. _init_completion || return
  308. COMPREPLY=()
  309. if [[ "$cur" == -* ]]; then
  310. __grubcomp "$(__grub_get_options_from_help)"
  311. else
  312. # Default complete with a filename
  313. _filedir
  314. fi
  315. }
  316. #
  317. # grub-probe
  318. #
  319. __grub_probe () {
  320. local cur prev words cword split
  321. _init_completion -s || return
  322. COMPREPLY=()
  323. case "$prev" in
  324. -t|--target)
  325. # Get target type from help
  326. local prog=${COMP_WORDS[0]}
  327. __grubcomp "$(LC_ALL=C $prog --help | \
  328. awk -F "[()]" '/--target=/ { print $2 }' | \
  329. sed 's/|/ /g')"
  330. return
  331. ;;
  332. esac
  333. $split && return 0
  334. if [[ "$cur" == -* ]]; then
  335. __grubcomp "$(__grub_get_options_from_help)"
  336. else
  337. # Default complete with a filename
  338. _filedir
  339. fi
  340. }
  341. #
  342. # grub-script-check
  343. #
  344. __grub_script_check () {
  345. local cur prev words cword
  346. _init_completion || return
  347. COMPREPLY=()
  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. # Local variables:
  356. # mode: shell-script
  357. # sh-basic-offset: 4
  358. # sh-indent-comment: t
  359. # indent-tabs-mode: nil
  360. # End:
  361. # ex: ts=4 sw=4 et filetype=sh