Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: e47beef0ec
Fetching contributors…

Cannot retrieve contributors at this time

file 273 lines (235 sloc) 8.784 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
#!/bin/bash

[[ -r /usr/share/bash-completion/completions/git ]] &&
    . /usr/share/bash-completion/completions/git

# git-commit override
_git_commit() {
  local cur ng_set

  # do nothing if we're not in a repo
  git rev-parse 2>/dev/null || return 0

  _get_comp_words_by_ref cur prev

  # complete uncommitted files
  if [[ $prev = "commit" ]]; then
    COMPREPLY=( $(compgen -W "$( git status --porcelain | cut -d' ' -f3-)" -- $cur ) )
  fi
}

_git_archprojects()
{
  local cur prev
  _get_comp_words_by_ref -n =: cur prev

  local projects="initscripts mkinitcpio netcfg pyalpm"

  if [[ $prev = git-archprojects ]]; then
    COMPREPLY=( $(compgen -W "$projects" -- $cur ) )
    return
  fi

  case "$cur" in
  --confirm=*)
    __gitcomp "
      $__git_send_email_confirm_options
      " "" "${cur##--confirm=}"
    return
    ;;
  --suppress-cc=*)
    __gitcomp "
      $__git_send_email_suppresscc_options
      " "" "${cur##--suppress-cc=}"

    return
    ;;
  --smtp-encryption=*)
    __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
    return
    ;;
  --*)
    __gitcomp "--annotate --bcc --cc --cc-cmd --chain-reply-to
      --compose --confirm= --dry-run --envelope-sender
      --from --identity
      --in-reply-to --no-chain-reply-to --no-signed-off-by-cc
      --no-suppress-from --no-thread --quiet
      --signed-off-by-cc --smtp-pass --smtp-server
      --smtp-server-port --smtp-encryption= --smtp-user
      --subject --suppress-cc= --suppress-from --thread --to
      --validate --no-validate"
    return
    ;;
  esac
  COMPREPLY=()
}
complete -F _git_archprojects git-archprojects


# aurcomm
_aurcomm() {
  local cur
  _get_comp_words_by_ref cur

  (( ${#cur} < 2 )) && return

  COMPREPLY=( $(compgen -W "$(cower -sq -- $cur)" -- $cur) )
}
complete -F _aurcomm aurcomm

_ghclone() {
  local cur
  _get_comp_words_by_ref cur

  if [[ ${COMP_WORDS[1]} = -p ]]; then
    # we can't complete private repos, so don't even try
    return 0
  fi

  if ! type -P json_reformat >/dev/null; then
    return 0
  fi

  COMPREPLY=( $(compgen -W "$(
    curl --compressed -sf "http://github.com/api/v2/json/repos/show/${COMP_WORDS[1]}" |
    json_reformat 2>/dev/null |
    sed -n '/"name":/s|.*: "\([^"]\+\)",|\1|p')" -- $cur)
  )
}
complete -F _ghclone ghclone

_known_hosts_real()
{
    local configfile flag prefix
    local cur curd awkcur user suffix aliases i host
    local -a kh khd config

    local OPTIND=1
    while getopts "acF:p:" flag "$@"; do
        case $flag in
            a) aliases='yes' ;;
            c) suffix=':' ;;
            F) configfile=$OPTARG ;;
            p) prefix=$OPTARG ;;
        esac
    done
    [ $# -lt $OPTIND ] && echo "error: $FUNCNAME: missing mandatory argument CWORD"
    cur=${!OPTIND}; let "OPTIND += 1"
    [ $# -ge $OPTIND ] && echo "error: $FUNCNAME("$@"): unprocessed arguments:"\
    $(while [ $# -ge $OPTIND ]; do printf '%s\n' ${!OPTIND}; shift; done)

    [[ $cur == *@* ]] && user=${cur%@*}@ && cur=${cur#*@}
    kh=()

    # ssh config files
    if [ -n "$configfile" ]; then
        [ -r "$configfile" ] &&
        config=( "${config[@]}" "$configfile" )
    else
        for i in /etc/ssh/ssh_config "${HOME}/.ssh/config" \
            "${HOME}/.ssh2/config"; do
            [ -r $i ] && config=( "${config[@]}" "$i" )
        done
    fi

    # Known hosts files from configs
    if [ ${#config[@]} -gt 0 ]; then
        local OIFS=$IFS IFS=$'\n'
        local -a tmpkh
        # expand paths (if present) to global and user known hosts files
        # TODO(?): try to make known hosts files with more than one consecutive
        # spaces in their name work (watch out for ~ expansion
        # breakage! Alioth#311595)
        tmpkh=( $( awk 'sub("^[ \t]*([Gg][Ll][Oo][Bb][Aa][Ll]|[Uu][Ss][Ee][Rr])[Kk][Nn][Oo][Ww][Nn][Hh][Oo][Ss][Tt][Ss][Ff][Ii][Ll][Ee][ \t]+", "") { print $0 }' "${config[@]}" | sort -u ) )
        for i in "${tmpkh[@]}"; do
            # Remove possible quotes
            i=${i//\"}
            # Eval/expand possible `~' or `~user'
            __expand_tilde_by_ref i
            [ -r "$i" ] && kh=( "${kh[@]}" "$i" )
        done
        IFS=$OIFS
    fi

    if [ -z "$configfile" ]; then
        # Global and user known_hosts files
        for i in /etc/ssh/ssh_known_hosts /etc/ssh/ssh_known_hosts2 \
            /etc/known_hosts /etc/known_hosts2 ~/.ssh/known_hosts \
            ~/.ssh/known_hosts2; do
            [ -r $i ] && kh=( "${kh[@]}" $i )
        done
        for i in /etc/ssh2/knownhosts ~/.ssh2/hostkeys; do
            [ -d $i ] && khd=( "${khd[@]}" $i/*pub )
        done
    fi

    # If we have known_hosts files to use
    if [[ ${#kh[@]} -gt 0 || ${#khd[@]} -gt 0 ]]; then
        # Escape slashes and dots in paths for awk
        awkcur=${cur//\//\\\/}
        awkcur=${awkcur//\./\\\.}
        curd=$awkcur

        if [[ "$awkcur" == [0-9]*[.:]* ]]; then
            # Digits followed by a dot or a colon - just search for that
            awkcur="^$awkcur[.:]*"
        elif [[ "$awkcur" == [0-9]* ]]; then
            # Digits followed by no dot or colon - search for digits followed
            # by a dot or a colon
            awkcur="^$awkcur.*[.:]"
        elif [ -z "$awkcur" ]; then
            # A blank - search for a dot, a colon, or an alpha character
            awkcur="[a-z.:]"
        else
            awkcur="^$awkcur"
        fi

        if [ ${#kh[@]} -gt 0 ]; then
            # FS needs to look for a comma separated list
            COMPREPLY=( "${COMPREPLY[@]}" $( awk 'BEGIN {FS=","}
            /^\s*[^|\#]/ {for (i=1; i<=2; ++i) { \
            sub(" .*$", "", $i); \
            sub("^\\[", "", $i); sub("\\](:[0-9]+)?$", "", $i); \
            if ($i ~ /'"$awkcur"'/) {print $i} \
            }}' "${kh[@]}" 2>/dev/null ) )
        fi
        if [ ${#khd[@]} -gt 0 ]; then
            # Needs to look for files called
            # .../.ssh2/key_22_<hostname>.pub
            # dont fork any processes, because in a cluster environment,
            # there can be hundreds of hostkeys
            for i in "${khd[@]}" ; do
                if [[ "$i" == *key_22_$curd*.pub && -r "$i" ]]; then
                    host=${i/#*key_22_/}
                    host=${host/%.pub/}
                    COMPREPLY=( "${COMPREPLY[@]}" $host )
                fi
            done
        fi

        # apply suffix and prefix
        for (( i=0; i < ${#COMPREPLY[@]}; i++ )); do
            COMPREPLY[i]=$prefix$user${COMPREPLY[i]}$suffix
        done
    fi

    # add completion from dnsmasq's lease file
    local leasefile='/var/lib/misc/dnsmasq.leases'
    if [[ -f $leasefile ]]; then
      COMPREPLY+=( $(compgen -P "$prefix$user" \
                             -S "$suffix" \
                             -W "$(cut -d' ' -f3,4 "$leasefile")" -- $cur) )
    fi

    # append any available aliases from config files
    if [[ ${#config[@]} -gt 0 && -n "$aliases" ]]; then
        local hosts=$( sed -ne 's/^[ \t]*[Hh][Oo][Ss][Tt]\([Nn][Aa][Mm][Ee]\)\{0,1\}['"$'\t '"']\{1,\}\([^#*?]*\)\(#.*\)\{0,1\}$/\2/p' "${config[@]}" )
        COMPREPLY=( "${COMPREPLY[@]}" $( compgen -P "$prefix$user" \
            -S "$suffix" -W "$hosts" -- "$cur" ) )
    fi

    # Add hosts reported by avahi-browse, if desired and it's available.
    if [[ ${COMP_KNOWN_HOSTS_WITH_AVAHI:-} ]] && \
        type avahi-browse &>/dev/null; then
        # The original call to avahi-browse also had "-k", to avoid lookups
        # into avahi's services DB. We don't need the name of the service, and
        # if it contains ";", it may mistify the result. But on Gentoo (at
        # least), -k wasn't available (even if mentioned in the manpage) some
        # time ago, so...
        COMPREPLY=( "${COMPREPLY[@]}" $( \
            compgen -P "$prefix$user" -S "$suffix" -W \
            "$( avahi-browse -cpr _workstation._tcp 2>/dev/null | \
                 awk -F';' '/^=/ { print $7 }' | sort -u )" -- "$cur" ) )
    fi

    # Add results of normal hostname completion, unless
    # `COMP_KNOWN_HOSTS_WITH_HOSTFILE' is set to an empty value.
    if [ -n "${COMP_KNOWN_HOSTS_WITH_HOSTFILE-1}" ]; then
        COMPREPLY=( "${COMPREPLY[@]}"
            $( compgen -A hostname -P "$prefix$user" -S "$suffix" -- "$cur" ) )
    fi

    __ltrim_colon_completions "$prefix$user$cur"

    return 0
}

_modules () {
  local modpath
  modpath=({/usr,}/lib/modules/$1)
  COMPREPLY=($( compgen -W "$(command find "${modpath[@]}" -name '*.ko*' -printf '%f\n' 2>/dev/null |
      sed -r 's/\.ko(\..z)?$//')" -- "$cur" ))
}

# pacman wrappers
complete -o filenames -F _pacman pp pm

# go
complete -f -X '!*.8' 8l
complete -f -X '!*.6' 6l
complete -f -X '!*.5' 5l
complete -f -X '!*.go' 8g 6g 5g gofmt gccgo

complete -F _known_hosts kssh
Something went wrong with that request. Please try again.