3 # Copyright (c) 2008, Google Inc.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are
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
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.
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.
33 # Author: Dave Nicponski
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.
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 [...]
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}"
53 # TODO(user): Replace this once gflags_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 binaries, don't actually execute the binary or the process
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}"
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
80 # TODO(user): This is not perfect - the 'env' command, for instance,
81 # is allowed to have options between the 'env' and 'the command to
82 # be executed'. For example, consider:
83 # $ env FOO="bar" bin/do_something --help<TAB>
84 # In this case, we'll mistake the FOO="bar" portion as the binary.
85 # Perhaps we should continuing consuming leading words until we
86 # either run out of words, or find a word that is a valid file
87 # marked as executable. I can't think of any reason this wouldn't
90 # Break up the 'original command line' (not this script's command line,
91 # rather the one the <TAB> was pressed on) and find the second word.
92 parts=( ${COMP_LINE} )
96 # Build the command line to use for completion. Basically it involves
97 # passing through all the arguments given to this script (except the 3
98 # that bash added), and appending a '--tab_completion_word "WORD"' to
101 for ((i=1; i<=$(($# - 3)); ++i)); do
102 params="$params \"${!i}\"";
104 params="$params --tab_completion_word \"$completion_word\""
106 # TODO(user): Perhaps stash the output in a temporary file somewhere
107 # in /tmp, and only cat it to stdout if the command returned a success
108 # code, to prevent false positives
110 # If we think we have a reasonable command to execute, then execute it
111 # and hope for the best.
112 candidate=$(type -p "$binary")
113 if [ ! -z "$candidate" ]; then
114 eval "$candidate 2>/dev/null $params"
115 elif [ -f "$binary" ] && [ -x "$binary" ]; then
116 eval "$binary 2>/dev/null $params"