complete.sh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # shellcheck shell=bash
  2. #
  3. # Copyright (C) 2017 Canonical Ltd
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License version 3 as
  7. # published by the Free Software Foundation.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. # _complete_from_snap performs the tab completion request by calling the
  17. # appropriate 'snap run --command=complete' with serialized args, and
  18. # deserializes the response into the usual tab completion result.
  19. #
  20. # How snap command completion works is:
  21. # 1. snapd's complete.sh is sourced into the user's shell environment
  22. # 2. user performs '<command> <tab>'. If '<command>' is a snap command,
  23. # proceed to step '3', otherwise perform normal bash completion
  24. # 3. run 'snap run --command=complete ...', converting bash completion
  25. # environment into serialized command line arguments
  26. # 4. 'snap run --command=complete ...' exec()s 'etelpmoc.sh' within the snap's
  27. # runtime environment and confinement
  28. # 5. 'etelpmoc.sh' takes the serialized command line arguments from step '3'
  29. # and puts them back into the bash completion environment variables
  30. # 6. 'etelpmoc.sh' sources the snap's 'completer' script, performs the bash
  31. # completion and serializes the resulting completion environment variables
  32. # by printing to stdout the results in a format that snapd's complete.sh
  33. # will understand, then exits
  34. # 7. control returns to snapd's 'complete.sh' and it deserializes the output
  35. # from 'etelpmoc.sh', validates the results and puts the validated results
  36. # into the bash completion environment variables
  37. # 8. bash displays the results to the user
  38. type -t _complete_from_snap > /dev/null ||
  39. _complete_from_snap() {
  40. {
  41. # De-serialize the output of 'snap run --command=complete ...' into the format
  42. # bash expects:
  43. read -r -a opts
  44. # opts is expected to be a series of compopt options
  45. if [[ ${#opts[@]} -gt 0 ]]; then
  46. if [[ "${opts[0]}" == "cannot" ]]; then
  47. # older snap-execs sent errors over stdout :-(
  48. return 1
  49. fi
  50. for i in "${opts[@]}"; do
  51. if ! [[ "$i" =~ ^[a-z]+$ ]]; then
  52. # only lowercase alpha characters allowed
  53. return 2
  54. fi
  55. done
  56. fi
  57. read -r bounced
  58. case "$bounced" in
  59. ""|"alias"|"export"|"job"|"variable")
  60. # OK
  61. ;;
  62. *)
  63. # unrecognised bounce
  64. return 2
  65. ;;
  66. esac
  67. read -r sep
  68. if [ -n "$sep" ]; then
  69. # non-blank separator? madness!
  70. return 2
  71. fi
  72. local oldIFS="$IFS"
  73. if [ ! "$bounced" ]; then
  74. local IFS=$'\n'
  75. # Ignore any suspicious results that are uncommon in filenames and that
  76. # might be used to trick the user. A whitelist approach would be better
  77. # but is impractical with UTF-8 and common characters like quotes.
  78. COMPREPLY=( $( command grep -v '[[:cntrl:];&?*{}]' ) )
  79. IFS="$oldIFS"
  80. fi
  81. if [[ ${#opts[@]} -gt 0 ]]; then
  82. # shellcheck disable=SC2046
  83. # (we *want* word splitting to happen here)
  84. compopt $(printf " -o %s" "${opts[@]}")
  85. fi
  86. if [ "$bounced" ]; then
  87. # We validated '$bounced' above and '${COMP_WORDS[$COMP_CWORD]}' is
  88. # coming from the user's session, not the snap so skip input
  89. # validation: we aren't trying to protect the user from themselves.
  90. COMPREPLY+=(compgen -A "$bounced" -- "${COMP_WORDS[$COMP_CWORD]}")
  91. fi
  92. } < <(
  93. snap run --command=complete "$1" "$COMP_TYPE" "$COMP_KEY" "$COMP_POINT" "$COMP_CWORD" "$COMP_WORDBREAKS" "$COMP_LINE" "${COMP_WORDS[@]}" 2>/dev/null || return 1
  94. )
  95. }
  96. # this file can be sourced directly as e.g. /usr/lib/snapd/complete.sh, or via
  97. # a symlink from /usr/share/bash-completion/completions/. In the first case we
  98. # want to load the default loader; in the second, the specific one.
  99. #
  100. if [[ "${BASH_SOURCE[0]}" =~ ^/usr/share/bash-completion/completions/ ]]; then
  101. complete -F _complete_from_snap "$1"
  102. else
  103. # _complete_from_snap_maybe calls _complete_from_snap if the command is in
  104. # bin/snap, and otherwise does bash-completion's _completion_loader (which is
  105. # what -D would've done before).
  106. type -t _complete_from_snap_maybe > /dev/null ||
  107. _complete_from_snap_maybe() {
  108. local etel=snap/core/current/usr/lib/snapd/etelpmoc.sh
  109. # catch /snap/bin and /var/lib/snapd/snap/bin
  110. if [[ "$(command -v "$1")" =~ ^(/var/lib/snapd)?/snap/bin/ && ( -e "/var/lib/snapd/$etel" || -e "/$etel" ) ]]; then
  111. complete -F _complete_from_snap "$1"
  112. return 124
  113. fi
  114. # fallback to the old -D
  115. _completion_loader "$1"
  116. }
  117. complete -D -F _complete_from_snap_maybe
  118. fi