OLD | NEW |
(Empty) | |
| 1 #!/bin/bash |
| 2 |
| 3 # Copyright (c) 2008, Google Inc. |
| 4 # All rights reserved. |
| 5 # |
| 6 # Redistribution and use in source and binary forms, with or without |
| 7 # modification, are permitted provided that the following conditions are |
| 8 # met: |
| 9 # |
| 10 # * Redistributions of source code must retain the above copyright |
| 11 # notice, this list of conditions and the following disclaimer. |
| 12 # * Redistributions in binary form must reproduce the above |
| 13 # copyright notice, this list of conditions and the following disclaimer |
| 14 # in the documentation and/or other materials provided with the |
| 15 # distribution. |
| 16 # * Neither the name of Google Inc. nor the names of its |
| 17 # contributors may be used to endorse or promote products derived from |
| 18 # this software without specific prior written permission. |
| 19 # |
| 20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 # |
| 32 # --- |
| 33 # Author: Dave Nicponski |
| 34 # |
| 35 # This script is invoked by bash in response to a matching compspec. When |
| 36 # this happens, bash calls this script using the command shown in the -C |
| 37 # block of the complete entry, but also appends 3 arguments. They are: |
| 38 # - The command being used for completion |
| 39 # - The word being completed |
| 40 # - The word preceding the completion word. |
| 41 # |
| 42 # Here's an example of how you might use this script: |
| 43 # $ complete -o bashdefault -o default -o nospace -C \ |
| 44 # '/usr/local/bin/gflags_completions.sh --tab_completion_columns $COLUMNS' \ |
| 45 # time env binary_name another_binary [...] |
| 46 |
| 47 # completion_word_index gets the index of the (N-1)th argument for |
| 48 # this command line. completion_word gets the actual argument from |
| 49 # this command line at the (N-1)th position |
| 50 completion_word_index="$(($# - 1))" |
| 51 completion_word="${!completion_word_index}" |
| 52 |
| 53 # TODO(daven): Replace this once commandlineflags_completions.cc has |
| 54 # a bool parameter indicating unambiguously to hijack the process for |
| 55 # completion purposes. |
| 56 if [ -z "$completion_word" ]; then |
| 57 # Until an empty value for the completion word stops being misunderstood |
| 58 # by google3 binaries, don't actuall execute the binary or the process |
| 59 # won't be hijacked! |
| 60 exit 0 |
| 61 fi |
| 62 |
| 63 # binary_index gets the index of the command being completed (which bash |
| 64 # places in the (N-2)nd position. binary gets the actual command from |
| 65 # this command line at that (N-2)nd position |
| 66 binary_index="$(($# - 2))" |
| 67 binary="${!binary_index}" |
| 68 |
| 69 # For completions to be universal, we may have setup the compspec to |
| 70 # trigger on 'harmless pass-through' commands, like 'time' or 'env'. |
| 71 # If the command being completed is one of those two, we'll need to |
| 72 # identify the actual command being executed. To do this, we need |
| 73 # the actual command line that the <TAB> was pressed on. Bash helpfully |
| 74 # places this in the $COMP_LINE variable. |
| 75 if [ "$binary" == "time" ] || [ "$binary" == "env" ]; then |
| 76 # we'll assume that the first 'argument' is actually the |
| 77 # binary to be run, if we think it looks like a google3 |
| 78 # binary |
| 79 |
| 80 # TODO(daven): Decide what 'looks' like a google3 binary. =) |
| 81 |
| 82 # TODO(daven): This is not perfect - the 'env' command, for instance, |
| 83 # is allowed to have options between the 'env' and 'the command to |
| 84 # be executed'. For example, consider: |
| 85 # $ env FOO="bar" bin/do_something --help<TAB> |
| 86 # In this case, we'll mistake the FOO="bar" portion as the binary. |
| 87 # Perhaps we should continuing consuming leading words until we |
| 88 # either run out of words, or find a word that is a valid file |
| 89 # marked as executable. I can't think of any reason this wouldn't |
| 90 # work. |
| 91 |
| 92 # Break up the 'original command line' (not this script's command line, |
| 93 # rather the one the <TAB> was pressed on) and find the second word. |
| 94 parts=( ${COMP_LINE} ) |
| 95 binary=${parts[1]} |
| 96 fi |
| 97 |
| 98 # Build the command line to use for completion. Basically it involves |
| 99 # passing through all the arguments given to this script (except the 3 |
| 100 # that bash added), and appending a '--tab_completion_word "WORD"' to |
| 101 # the arguments. |
| 102 params="" |
| 103 for ((i=1; i<=$(($# - 3)); ++i)); do |
| 104 params="$params \"${!i}\""; |
| 105 done |
| 106 params="$params --tab_completion_word \"$completion_word\"" |
| 107 |
| 108 # TODO(daven): Perhaps stash the output in a temporary file somewhere |
| 109 # in /tmp, and only cat it to stdout if the command returned a success |
| 110 # code, to prevent false positives |
| 111 |
| 112 # If we think we have a reasonable command to execute, then execute it |
| 113 # and hope for the best. |
| 114 if [ -f "$binary" ] && [ -x "$binary" ]; then |
| 115 eval "$binary 2>/dev/null $params" |
| 116 fi |
| 117 |
OLD | NEW |