man_to_bashcompletion.sh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/bin/bash
  2. commands=""
  3. generate_bashcompletion() {
  4. k=`basename $1 .1.md`
  5. commands+="`echo $k |sed 's/tpm2_//g'` "
  6. shortop="$(grep -oP '(?<=\* \*\*\-)([a-zA-Z])' $1)"
  7. longoptwithshortop="$(grep -oP '(?<=\* \*\*\-[a-zA-Z]\*\*, \*\*\\--)([a-zA-Z\-]+)' $1)"
  8. longopwithoutshortop="$(grep -oP '(?<=\* \*\*\\\-\-)([a-zA-Z]{1,})' $1)"
  9. echo "# bash completion for $k -*- shell-script -*-"
  10. echo \
  11. "_$k()
  12. {
  13. local auth_methods=(str: hex: file: file:- session: pcr:)
  14. local hash_methods=(sha1 sha256 sha384 sha512)
  15. local format_methods=(tss plain)
  16. local signing_scheme=(rsassa rsapss ecdsa ecdaa sm2 ecshnorr hmac)
  17. local key_object=(rsa ecc aes camellia hmac xor keyedhash)
  18. local key_attributes=(\| fixedtpm stclear fixedparent \\
  19. sensitivedataorigin userwithauth adminwithpolicy noda \\
  20. encrypteddupplication restricted decrypt sign)
  21. local nv_attributes=(\| ppwrite ownerwrite authwrite policywrite \\
  22. policydelete writelocked writeall writedefine write_stclear \\
  23. globallock ppread ownerread authread policyread no_da orderly \\
  24. clear_stclear readlocked written platformcreate read_stclear)
  25. local cur prev words cword split
  26. _init_completion -s || return
  27. case \$prev in
  28. -h | --help)
  29. COMPREPLY=( \$(compgen -W \"man no-man\" -- \"\$cur\") )
  30. return;;
  31. -T | --tcti)
  32. COMPREPLY=( \$(compgen -W \"tabrmd mssim device none\" -- \"\$cur\") )
  33. return;;"
  34. shortop_ctr=0
  35. longoptwithshortop_ctr=0
  36. for shortop_index in $shortop;do
  37. for longoptwithshortop_index in $longoptwithshortop;do
  38. if [ $shortop_ctr == $longoptwithshortop_ctr ];then
  39. echo " -$shortop_index | --$longoptwithshortop_index)"
  40. if [[ "$longoptwithshortop_index" == *"auth"* ]];then
  41. echo " COMPREPLY=(\$(compgen -W \"\${auth_methods[*]}\" -- \"\$cur\"))"
  42. elif [[ "$shortop_index" == "g" ]];then
  43. echo " COMPREPLY=(\$(compgen -W \"\${hash_methods[*]}\" -- \"\$cur\"))"
  44. elif [[ "$shortop_index" == "G" ]];then
  45. echo " COMPREPLY=(\$(compgen -W \"\${key_object[*]}\" -- \"\$cur\"))"
  46. elif [[ "$longoptwithshortop_index" == *"attributes"* && "$k" != "tpm2_nvdefine" ]];then
  47. echo " COMPREPLY=(\$(compgen -W \"\${key_attributes[*]}\" -- \"\$cur\"))"
  48. elif [[ "$longoptwithshortop_index" == *"attributes"* && "$k" == "tpm2_nvdefine" ]];then
  49. echo " COMPREPLY=(\$(compgen -W \"\${nv_attributes[*]}\" -- \"\$cur\"))"
  50. elif [[ "$longoptwithshortop_index" == *"format"* && "$k" != "tpm2_verifysignature" ]];then
  51. echo " COMPREPLY=(\$(compgen -W \"\${format_methods[*]}\" -- \"\$cur\"))"
  52. elif [[ "$longoptwithshortop_index" == *"scheme"* ]];then
  53. echo " COMPREPLY=(\$(compgen -W \"\${signing_scheme[*]}\" -- \"\$cur\"))"
  54. else
  55. echo " _filedir"
  56. fi
  57. echo " return;;"
  58. fi
  59. longoptwithshortop_ctr=$((longoptwithshortop_ctr + 1))
  60. done
  61. shortop_ctr=$((shortop_ctr + 1))
  62. longoptwithshortop_ctr=0
  63. done
  64. echo -n " esac
  65. COMPREPLY=(\$(compgen -W \"-h --help -v --version -V --verbose -Q --quiet \\
  66. -Z --enable-erata -T --tcti \\"
  67. echo ""
  68. echo -n " "
  69. for j in $shortop; do echo -n "-$j "; done
  70. for j in $longoptwithshortop; do echo -n "--$j "; done
  71. for j in $longopwithoutshortop; do echo -n "--$j "; done
  72. echo "\" \\"
  73. echo -n " "
  74. echo "-- \"\$cur\"))
  75. } &&
  76. complete -F _$k $k"
  77. echo "# ex: filetype=sh"
  78. }
  79. generate_bashcompletion_for_tpm2() {
  80. echo "_tpm2() {
  81. local cur prev words cword split
  82. _init_completion -s || return
  83. if ((cword == 1)); then
  84. COMPREPLY=(\$(compgen -W \"$commands\" -- \"\$cur\"))
  85. else
  86. tpmcommand=_tpm2_\$prev
  87. type \$tpmcommand &>/dev/null && \$tpmcommand
  88. if [ \$? == 1 ];then
  89. COMPREPLY=(\$(compgen -W \${words[1]} -- \"\$cur\"))
  90. fi
  91. fi
  92. } &&
  93. complete -F _tpm2 tpm2"
  94. }
  95. current_dir="$(readlink -e "$(dirname "$0")")"
  96. mandir="$(readlink -e "${current_dir}"/../../man)"
  97. bashcompletiondir="$(readlink -e "${current_dir}"/../../dist/bash-completion/tpm2-tools)"
  98. rm -f $bashcompletiondir/tpm2_completion.bash
  99. for man_file in `ls $mandir/tpm2*.1.md`
  100. do
  101. generate_bashcompletion $man_file >> $bashcompletiondir/tpm2_completion.bash
  102. done
  103. generate_bashcompletion_for_tpm2 >> $bashcompletiondir/tpm2_completion.bash