blob: c5fb7e6bc57bf71ff148721ff180807849ac3b55 [file] [log] [blame]
Craig Silversteinc79c32d2008-07-22 23:29:39 +00001#!/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
50completion_word_index="$(($# - 1))"
51completion_word="${!completion_word_index}"
52
Craig Silverstein917f4e72011-07-29 04:26:49 +000053# TODO(user): Replace this once gflags_completions.cc has
Craig Silversteinc79c32d2008-07-22 23:29:39 +000054# a bool parameter indicating unambiguously to hijack the process for
55# completion purposes.
56if [ -z "$completion_word" ]; then
57 # Until an empty value for the completion word stops being misunderstood
Craig Silverstein917f4e72011-07-29 04:26:49 +000058 # by binaries, don't actually execute the binary or the process
Craig Silversteinc79c32d2008-07-22 23:29:39 +000059 # won't be hijacked!
60 exit 0
61fi
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
66binary_index="$(($# - 2))"
67binary="${!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.
75if [ "$binary" == "time" ] || [ "$binary" == "env" ]; then
76 # we'll assume that the first 'argument' is actually the
Craig Silversteinc79c32d2008-07-22 23:29:39 +000077 # binary
78
Craig Silversteinc79c32d2008-07-22 23:29:39 +000079
Craig Silverstein917f4e72011-07-29 04:26:49 +000080 # TODO(user): This is not perfect - the 'env' command, for instance,
Craig Silversteinc79c32d2008-07-22 23:29:39 +000081 # 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
88 # work.
89
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} )
93 binary=${parts[1]}
94fi
95
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
99# the arguments.
100params=""
101for ((i=1; i<=$(($# - 3)); ++i)); do
102 params="$params \"${!i}\"";
103done
104params="$params --tab_completion_word \"$completion_word\""
105
Craig Silverstein917f4e72011-07-29 04:26:49 +0000106# TODO(user): Perhaps stash the output in a temporary file somewhere
Craig Silversteinc79c32d2008-07-22 23:29:39 +0000107# in /tmp, and only cat it to stdout if the command returned a success
108# code, to prevent false positives
109
110# If we think we have a reasonable command to execute, then execute it
111# and hope for the best.
Craig Silverstein5a3c7f82009-04-15 21:57:04 +0000112candidate=$(type -p "$binary")
113if [ ! -z "$candidate" ]; then
114 eval "$candidate 2>/dev/null $params"
115elif [ -f "$binary" ] && [ -x "$binary" ]; then
Craig Silversteinc79c32d2008-07-22 23:29:39 +0000116 eval "$binary 2>/dev/null $params"
117fi