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
|
# (C) 2010-2011 magicant
# Completion script for the "sh" built-in command.
# Completion function "completion/sh" is used for the "bash", "dash" and "mksh"
# commands as well. Supports POSIX 2008, bash 4.1, dash 0.5.5.1, mksh R39c.
function completion/sh {
typeset compoptopts=
typeset prog="${WORDS[1]##*/}"
typeset OPTIONS FOPTIONS OOPTIONS ARGOPT PREFIX
FOPTIONS=( #>#
"c; execute the first operand as a shell script"
"i; work in the interactive mode"
"o:; specify an option"
) #<#
OOPTIONS=( #>#
"a --allexport; export all variables when assigned"
"b --notify; print job status immediately when done"
"C --noclobber; disallow >-redirection to overwrite an existing file"
"e --errexit; exit immediately when a command's exit status is non-zero"
"f --noglob; disable pathname expansion (globbing)"
"m --monitor; enable job control"
"n --noexec; don't execute any commands"
"u --nounset; disallow expansion of undefined variables"
"v --verbose; echo commands entered to the shell"
"x --xtrace; print a command line before executing it"
"--ignoreeof; don't exit when an end-of-file is entered"
"--vi; vi-like line-editing"
) #<#
case $prog in
(bash)
OOPTIONS=("$OOPTIONS" #>#
"h --hashall; cache full paths of commands in a function when defined"
) #<#
;;
(mksh)
OOPTIONS=("$OOPTIONS" #>#
"h --trackall; cache full paths of commands when entered"
) #<#
;;
(*)
OOPTIONS=("$OOPTIONS" #>#
"h; cache full paths of commands in a function when defined"
) #<#
;;
esac
case $prog in
(bash)
FOPTIONS=("$FOPTIONS" #>#
"l --login; work as a login shell"
) #<#
;;
(dash)
FOPTIONS=("$FOPTIONS" #>#
"l; work as a login shell"
) #<#
;;
(mksh)
OOPTIONS=("$OOPTIONS" #>#
"l --login; work as a login shell"
) #<#
;;
esac
case $prog in
(mksh)
OOPTIONS=("$OOPTIONS" #>#
"s --stdin; read commands from the standard input"
) #<#
;;
(*)
FOPTIONS=("$FOPTIONS" #>#
"s; read commands from the standard input"
) #<#
;;
esac
case $prog in ([bd]ash|mksh)
OOPTIONS=("$OOPTIONS" #>#
"--emacs; emacs-like line-editing"
) #<#
esac
case $prog in (bash|mksh)
OOPTIONS=("$OOPTIONS" #>#
"--posix; force strict POSIX conformance"
"p --privileged; work in the privileged mode"
"P --physical; make the -P option the default in the cd commands etc."
) #<#
esac
case $prog in (bash)
FOPTIONS=("$FOPTIONS" #>#
"--help"
"--version"
"--noprofile; don't read profile file"
"--debugger; enable extended debugging mode"
"--dump-po-strings; extract translatable strings in po file format"
"D --dump-strings; extract translatable strings"
"--noediting; don't use the Readline library"
"--norc; don't read rc file"
"--rcfile: --init-file:; specify the rc file"
"r --restricted; work in the restricted mode"
"--rpm-requires; print package names required to run the script"
"O:; specify a shopt option"
) #<#
OOPTIONS=("$OOPTIONS" #>#
"H --histexpand; enable !-expansion on the command line"
"k --keyword; allow assignments in the middle of command arguments"
"--pipefail; return last non-zero exit status of commands in a pipe"
"B --braceexpand; enable brace expansion"
"E --errtrace; don't reset ERR trap in subshells"
"T --functrace; don't reset DEBUG and RETURN traps in subshells"
"t --onecmd; execute one command only"
"--history; enable command history"
) #<#
compoptopts="$compoptopts -e"
esac
case $prog in (mksh)
OOPTIONS=("$OOPTIONS" #>#
"--braceexpand; enable brace expansion"
"r --restricted; work in the restricted mode"
"--bgnice; run background jobs at lower priorities"
"--gmacs; gmacs-like line-editing"
"U --utf8-mode; enable UTF-8 support"
"X --markdirs; append a slash to directory names after pathname expansion"
"--arc4random; use arc4random for random number generation"
"--nohup; don't kill running jobs when exiting"
"--sh; force very strict POSIX conformance"
"--vi-esccomplete; use the Escape key for completion in the vi mode"
"--vi-tabcomplete; use the Tab key for completion in the vi mode"
) #<#
esac
OPTIONS=("$FOPTIONS" "$OOPTIONS")
# convert plus-prefixed options into hyphen-prefixed options
typeset i=2
while [ $i -le ${WORDS[#]} ]; do
case ${WORDS[i]} in
(-|--)
break
;;
([+-]*o)
i=$((i+2))
;;
([+-]?*)
i=$((i+1))
;;
(*)
break
;;
esac
done
WORDS=("${WORDS[1]}" "${WORDS[2,i-1]/#+/-}" "${WORDS[i,-1]}")
# the same for $TARGETWORD
typeset SAVETARGETWORD="$TARGETWORD"
if [ $i -gt ${WORDS[#]} ]; then
TARGETWORD=${TARGETWORD/#+/-}
fi
command -f completion//parseoptions
case $ARGOPT in
(-)
case $prog in
(set|yash|ksh)
;;
(*)
OPTIONS=("$OOPTIONS")
command -f completion/set::removelongopts
OPTIONS=("$FOPTIONS" "$OPTIONS")
;;
esac
case $SAVETARGETWORD in
(-*)
command -f completion//completeoptions $compoptopts
;;
(+*)
TARGETWORD=$SAVETARGETWORD
command -f completion/set::option short
;;
esac
;;
(o)
PREFIX=${SAVETARGETWORD%"${TARGETWORD#"$PREFIX"}"}
TARGETWORD=$SAVETARGETWORD
OPTIONS=("$OOPTIONS")
command -f completion/set::option long
;;
(O)
typeset shopt value
while read -r shopt value; do
complete -P "$PREFIX" -- "$shopt"
done 2>/dev/null <(bash -O </dev/null)
;;
(*)
complete -P "$PREFIX" -f
;;
esac
}
function completion/set::removelongopts {
typeset IFS="
"
OPTIONS=($(printf '%s\n' "$OPTIONS" | sed 's/--[^;[:blank:]]*//g'))
} 2>/dev/null
function completion/set::option {
if [ "${1-}" != long ]; then
typeset single=true
else
typeset single=false
fi
typeset opts desc
typeset generated=false
for opts in "$OPTIONS"; do
# get description
case $opts in
(*\;*)
desc=${opts#*;}
while true; do # trim surrounding spaces
case $desc in
([[:space:]]*) desc=${desc#[[:space:]]} ;;
(*[[:space:]]) desc=${desc%[[:space:]]} ;;
(*) break ;;
esac
done
;;
(*)
desc=
;;
esac
if $single; then
# generate single-character option
for opt in ${opts%%;*}; do
case $opt in ([[:alnum:]]|[[:alnum:]]:)
complete -O -P "$TARGETWORD" \
${desc:+-D "$desc"} "${opt%:}" &&
generated=true
break
esac
done
else
# generate candidate for first match
for opt in ${opts%%;*}; do
case $opt in (--*)
complete -P "$PREFIX" \
${desc:+-D "$desc"} \
-- "${opt#--}" &&
generated=true &&
break
esac
done
fi
done
$generated
}
# vim: set ft=sh ts=8 sts=8 sw=8 et:
|