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 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
# (C) 2010 magicant
# Completion script for the "ps" command.
# Supports POSIX 2008, procps 3.2.8, FreeBSD 8.1, OpenBSD 4.8, NetBSD 5.0,
# Mac OS X 10.6.4, SunOS 5.10, HP-UX 11i v3.
function completion/ps {
typeset -x PS_PERSONALITY=posix
case $("${WORDS[1]}" --version 2>/dev/null) in
(*'procps'*) typeset type=procps ;;
(*) typeset type="$(uname 2>/dev/null)" ;;
esac
case $type in
(procps) typeset long=true ;;
(*) typeset long= ;;
esac
typeset OPTIONS ARGOPT PREFIX
OPTIONS=( #>#
"A; print all processes"
"a; print all processes with terminals"
"G: ${long:+--Group:}; specify real group IDs of processes to print"
"o: ${long:+--format:}; specify format items to print"
"p: ${long:+--pid:}; specify process IDs of processes to print"
"t: ${long:+--tty:}; specify terminals of processes to print"
"U: ${long:+--User:}; specify real user IDs of processes to print"
) #<#
case $type in (procps|*BSD|Darwin|SunOS|HP-UX)
OPTIONS=("$OPTIONS" #>#
"j; print job info"
"l; print in the long format"
) #<#
case $type in (procps|Darwin|SunOS|HP-UX)
OPTIONS=("$OPTIONS" #>#
"d; print all processes but session leaders"
"e; print all processes"
"f; print in the full format"
"u: ${long:+--user:}; specify effective user IDs of processes to print"
) #<#
case $type in
(procps)
OPTIONS=("$OPTIONS" #>#
"g:; specify session IDs or effective group IDs of processes to print"
"--group:; specify effective group IDs of processes to print"
) #<#
;;
(*)
OPTIONS=("$OPTIONS" #>#
"g:; specify process group IDs of processes to print"
) #<#
;;
esac
esac
esac
case $type in (procps)
OPTIONS=("$OPTIONS" #>#
"F; print in the extra full format"
"m; print threads after each process"
"N --deselect; print processes not selected by other options"
"n:; specify the name list file"
"T; print threads rather than processes"
"Z M --context; print security contexts"
"V --version; print version info"
"--columns: --cols: --width:; specify the screen width"
"--cumulative; include child processes' CPU time in their parent's"
"--headers; print a header line for each screen page"
"--info; print debugging info"
"--lines: --rows:; specify the screen height"
"--no-headers; don't print the header line"
"--ppid:; specify parent process IDs of processes to print"
"--sort:; specify sort keys"
"--help"
) #<#
esac
case $type in (procps|*BSD|Darwin)
OPTIONS=("$OPTIONS" #>#
"O:; specify format items to print in addition to the default ones"
"w; wide format"
) #<#
case $type in (*BSD|Darwin)
OPTIONS=("$OPTIONS" #>#
"C; use the raw CPU percentage"
"c; print only the command name for the command column"
"L; print available format items"
"m; sort by memory usage"
"N:; specify the name list file"
"r; sort by current CPU usage"
"S; include child processes' CPU time in their parent's"
"T; print processes associated with the current terminal"
"v; print in the virtual memory format"
"x; print processes with terminals as well"
) #<#
case $type in
(FreeBSD)
OPTIONS=("$OPTIONS" #>#
"d; print process family trees"
"f; print data even for swapped-out processes"
"H; print thread info"
"X; don't print processes with terminals"
"Z; print MAC labels as well"
) #<#
;;
(OpenBSD)
OPTIONS=("$OPTIONS" #>#
"k; print thread info"
"W:; specify a file to extract swap info from"
) #<#
;;
(NetBSD)
OPTIONS=("$OPTIONS" #>#
"k:; specify sort keys"
"s; print threads rather than processes"
"W:; specify a file to extract swap info from"
) #<#
;;
esac
case $type in
(Darwin)
OPTIONS=("$OPTIONS" #>#
"E; print environment variables as well"
"M; print thread info"
"X; don't print processes with terminals"
) #<#
;;
(*)
OPTIONS=("$OPTIONS" #>#
"e; print environment variables as well"
"M:; specify a core file to print"
"u; print in the user-oriented format"
) #<#
;;
esac
esac
esac
case $type in (procps|SunOS|HP-UX)
OPTIONS=("$OPTIONS" #>#
"s: ${long:+--sid:}; specify session IDs of processes to print"
) #<#
case $type in (procps|SunOS)
OPTIONS=("$OPTIONS" #>#
"L; print thread info"
"P; print running processor numbers (PSR) as well"
"y; print RSS instead of F and ADDR (with -l)"
) #<#
case $type in (SunOS)
OPTIONS=("$OPTIONS" #>#
"Z; print zone info as well"
"z:; specify zones of processes to print"
) #<#
esac
esac
case $type in (procps|HP-UX)
OPTIONS=("$OPTIONS" #>#
"C:; specify the name of process to print"
"H ${long:+--forest}; print process family trees"
"P; print process resource manager (PRM) info as well"
) #<#
case $type in (HP-UX)
OPTIONS=("$OPTIONS" #>#
"R:; specify PRM process group IDs of processes to print"
"x; print in the extended format"
"Z:; specify processor set IDs of processes to print"
) #<#
esac
esac
esac
command -f completion//parseoptions -e
case $ARGOPT in
(-)
command -f completion//completeoptions
;;
(C)
typeset word="${TARGETWORD#"$PREFIX"}"
PREFIX=${TARGETWORD%"${word##*[ ,]}"}
complete -P "$PREFIX" -- $(ps -A -o comm= 2>/dev/null)
;;
(G|--[Gg]roup)
complete -P "$PREFIX" -g
;;
(g)
typeset word="${TARGETWORD#"$PREFIX"}"
PREFIX=${TARGETWORD%"${word##*[ ,]}"}
case $type in
(procps)
case $word in
(*[![:digit:]\ ,]*)
;;
(*)
# complete a session ID
typeset sid tty
while read -r sid tty; do
complete -P "$PREFIX" -R 0 -D "$tty" -- "$sid"
done 2>/dev/null <(ps -A -o sid= -o tty= | sort -u)
;;
esac
complete -P "$PREFIX" -g
;;
(*)
;;
esac
;;
([MNnW])
complete -P "$PREFIX" -f
;;
([kOo]|--format|--sort)
# complete a format item
typeset word="${TARGETWORD#"$PREFIX"}"
PREFIX=${TARGETWORD%"${word##*[ ,+-]}"}
case $type in
(procps)
typeset key name
while read -r key name; do
complete -P "$PREFIX" -- "$key"
done <(ps L)
;;
(*BSD|Darwin)
complete -P "$PREFIX" -- $(ps -L 2>/dev/null)
;;
(*)
case $type in
(SunOS)
complete -P "$PREFIX" -- addr class ctid gid f \
fname lwp nlwp osz pmem pri project \
projid pset psr rgid rss ruid s sid \
stime taskid uid wchan zone zoneid
;;
(HP-UX)
complete -P "$PREFIX" -- addr cls cpu flags \
gid pri prmgrp prmid pset rgid ruid \
sid state stime sz uid wchan
;;
esac
complete -P "$PREFIX" -- args comm etime group nice \
pcpu pgid pid ppid rgroup ruser time tty user vsz
;;
esac
;;
(p|--pid|--ppid)
# complete a process ID
typeset pid args
while read -r pid args; do
complete -P "$PREFIX" -D "$args" -- "$pid"
done <(ps -A -o pid= -o args= 2>/dev/null)
;;
(R)
#TODO
;;
(s|--sid)
# complete a session ID
typeset sid tty
while read -r sid tty; do
complete -P "$PREFIX" -R 0 -D "$tty" -- "$sid"
done 2>/dev/null <(ps -A -o sid= -o tty= | sort -u)
;;
(t|--tty)
typeset ttys
ttys=($(ps -A -o tty=))
complete -P "$PREFIX" -A '*[[:alnum:]]*' -- "$ttys" "${ttys#tty}"
;;
([Uu]|--[Uu]ser)
complete -P "$PREFIX" -u
;;
(Z)
#TODO
;;
(z)
#TODO
;;
(--columns|--cols|--width|--lines|--rows)
;;
(*)
command -f completion/ps::bsd
;;
esac
}
# parse BSD style options
function completion/ps::bsd {
#TODO
}
# vim: set ft=sh ts=8 sts=8 sw=8 et:
|