Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 73 lines (66 sloc) 2.626 kb
1893079 @muhqu initial version
authored
1 #!/bin/bash
2
16dc1bb @muhqu added usage and examples
authored
3 if [ "$#" != "1" ]; then
4 echo "Usage: $0 \"command to complete...\""
5 echo ""
6 echo "Examples:"
7 echo " $0 \"ssh\" - will list commands like: ssh, ssh-add, ssh-keygen"
8 echo " $0 \"ssh \" - will list known hosts"
9 echo " $0 \"ssh -\" - will list ssh command flags"
10 echo " $0 \"ssh -o \" - will list available ssh options"
11 echo ""
12 exit 1;
13 fi
1893079 @muhqu initial version
authored
14
15 #
16 # simulate some user input and pipe that into an interactive bash
17 # AWESOME!
18 #
16dc1bb @muhqu added usage and examples
authored
19 CMD="$1"
20 BNDRY="XYZXYZXYZXYZXYZXYZXYZXYZXYZ"
1893079 @muhqu initial version
authored
21 OUTPUT=$(echo -ne "false && ($CMD\t\tyyyy) && echo $BNDRY" | COLUMNS=1024 PS1="$BNDRY " /bin/bash -i 2>&1)
22
23 #
24 # try to read the list of completions and return them as a
25 # sorted list one per line.
26 #
27 LIST=$(echo "$OUTPUT" \
28 | awk '/'$BNDRY' false/, /'$BNDRY' false.*echo '$BNDRY'/' \
29 | awk 'substr($0,0,'${#BNDRY}') != "'${BNDRY}'" && $0 !~ /\(y or n\)$|--More--|^> / { for (i=1; i<=NF; i++) print $i }' \
30 | sort | uniq )
31 if [ -n "$LIST" ]; then
32 echo "$LIST";
33 exit 0;
34 fi
35
36 #
37 # if completion did not resulted in a list of completions,
38 # it was propably a direct word completion, so we need to
39 # extract the completed word from the expanded commandline.
40 #
41 L=${#CMD}
42 CMDLW=$(echo "$CMD" | awk '{print $NF}')
43 [ -n "$CMDLW" ] && L=$(($L-${#CMDLW}+1))
44 echo "$OUTPUT" \
45 | awk '$0 ~ /'$BNDRY' false && \(/ { split($0,a,/'$BNDRY' false && \(|yyyy\) && echo '$BNDRY'/); print a[2] }' \
46 | awk '{print substr($0,'$L');}' \
47 | cat;
48 exit 0;
49
50
51 #
52 # The MIT License
53 #
54 # Copyright (c) 2011 Mathias Leppich <mleppich@muhqu.de>
55 #
56 # Permission is hereby granted, free of charge, to any person obtaining a copy
57 # of this software and associated documentation files (the "Software"), to deal
58 # in the Software without restriction, including without limitation the rights
59 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
60 # copies of the Software, and to permit persons to whom the Software is
61 # furnished to do so, subject to the following conditions:
62 #
63 # The above copyright notice and this permission notice shall be included in
64 # all copies or substantial portions of the Software.
65 #
66 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
67 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
68 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
69 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
70 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
71 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
72 # THE SOFTWARE.
Something went wrong with that request. Please try again.