1
0
Fork 0
mirror of git://git.code.sf.net/p/zsh/code synced 2025-09-13 13:41:01 +02:00

43105: Improve pgrep/pkill completion

Contains minor changes to signal completion compared to the patch as originally
posted
This commit is contained in:
dana 2018-07-03 20:04:59 -05:00
parent 2c243e45c7
commit 6fe0c24fdb
2 changed files with 123 additions and 59 deletions

View file

@ -1,3 +1,8 @@
2018-07-03 dana <dana@dana.is>
* 43105 (tweaked): Completion/Unix/Command/_pgrep: Add full procps
support, other improvements
2018-07-03 Peter Stephenson <p.stephenson@samsung.com> 2018-07-03 Peter Stephenson <p.stephenson@samsung.com>
* dana: 43134: Completion/Unix/Type/_signals: don't complete SIG * dana: 43134: Completion/Unix/Type/_signals: don't complete SIG

View file

@ -1,75 +1,134 @@
#compdef pgrep pkill #compdef pgrep pkill
local curcontext="$curcontext" state line ret=1 expl # Notes:
# - We assume that Linux systems use procps-ng — specifically, procps-ng >=3.3.4
# (which changed the behaviour of -f and added -a)
# - We don't really need to keep pgopts and pkopts separate, but it seems like
# it should make things a bit easier to follow
# - @todo We could complete log-in classes given to -c
# - @todo We could complete routing tables given to -T
local curcontext="$curcontext" state line ret=1 expl pgopts pkopts no
typeset -A opt_args typeset -A opt_args
typeset -a arguments typeset -a arguments sig_arguments aopts
arguments=('-P[parent process id]:parent process id:->ppid' # These arguments (a) are common to all variants (like -x), (b) are the most
'-F[match only in process in pidfile]:files:_files' # common amongst all variants (like -a), or (c) have a single unambiguous
'-g[match only in process group ids]:group:->pgid' # meaning amongst all variants (like --help). Many of them are filtered out or
'-G[match only real group id]:group:_groups' # overridden below
'-j[match only in processes inside jails]:jail id:_sequence _jails -0 -o jid' arguments=(
'-J[match only in project ids]:project id:->projid' '-a[include process ancestors in match list]'
'-M[extract the name list from the specified core]:files:_files' '-c+[match only on specified login class]:login class'
'-N[extract the name list from the specified system]:files:_files' '(-F --pidfile)'{-F+,--pidfile=}'[match only processes in specified PID file]:PID file:_files'
'-s[match only session id]:session id:->sid' '(-f --full)'{-f,--full}'[match against full command line]'
'-t[match only controlled by terminal]:terminal device:_sequence _ttys -d' '(-G --group)'{-G+,--group=}'[match only on specified real group IDs]: :_sequence _groups'
'-T[match only in processes specified routing table in rtable]' '(-g --pgroup)'{-g+,--pgroup=}'[match only on specified process group IDs]: :->pgid'
'-u[match only effective user id]:user:_users' '(: * -)'{-h,--help}'[display help information]'
'-U[match only real user id]:user:_users' '-I[request confirmation before signalling each process]'
'(-n)-o[oldest process]'
'(-o)-n[newest process]'
'-a[include process ancestors in the match list]'
'-c[print a count of matching processes]'
'-f[match against full command line]'
'-i[ignore case distinctions]' '-i[ignore case distinctions]'
'-I[confirmation before attempting to single each process]' '-J+[match only on specified project IDs]: :->projid'
'-L[given pidfile must be locked]' '-j+[match only on specified jail IDs]:jail:_sequence _jails -0 -o jid'
'-q[do not write anything to standard output]' '(-L --logpidfile)'{-L,--logpidfile}'[fail if PID file not locked (with -F)]'
'-S[search also in system processes]' '(-N)-M+[extract name list from specified core]:core file:_files'
'-v[negate matching]' '(-M)-N+[extract name list from specified system]:system file:_files'
'-w[show thread ids instead of pids]' '(-o -n --oldest --newest)'{-n,--newest}'[match newest process]'
'-x[match exactly]' '(-o -n --oldest --newest)'{-o,--oldest}'[match oldest process]'
'-z[match only in zones]:zone:_zones') '(-P --parent)'{-P+,--parent=}'[match only on specified parent process IDs]: :->ppid'
'(-l)-q[suppress normal output]'
'-S[search also in system processes (kernel threads)]'
'(-s --session)'{-s+,--session=}'[match only on specified process session IDs]: :->sid'
# _signals is OK here — we do it differently below
'(ss)--signal=[specify signal to send to process]: :_signals -s'
'-T+[match only on specified routing table]:routing table'
'(-t --terminal)'{-t+,--terminal=}'[match only on specified controlling terminals]:terminal device:_sequence _ttys -d'
'(-U --uid)'{-U+,--uid=}'[match only on specified real user IDs]: :_sequence _users'
'(-u --euid)'{-u+,--euid=}'[match only on specified effective user IDs]: :_sequence _users'
'(-v --inverse)'{-v,--inverse}'[negate matching]'
'(-x --exact)'{-x,--exact}'[match process name or command line (with -f) exactly]'
'--ns=[match only on same namespaces as specified PID]: :_pids'
'--nslist=[match only on specified namespaces (with --ns)]:namespace:(ipc mnt net pid user uts)'
'(: * -)'{-V,--version}'[display version information]'
'-z+[match only on specified zone IDs]:zone:_sequence _zones'
)
[[ $service == pgrep ]] && arguments+=(
'(-d --delimiter)'{-d+,--delimiter=}'[specify output delimiter]:delimiter:compadd ${(s<>)IFS}'
'(-q)-l[display process name (and arguments with -f)]'
'(-w --lightweight)'{-w,--lightweight}'[show all thread IDs instead of PID]'
)
[[ $service == pkill ]] && arguments+=(
'(-e --echo)'{-e,--echo}'[display signalled process]'
'-l[display kill command]'
)
if [[ $service == 'pgrep' ]]; then case $OSTYPE in
arguments+=('-d[output delimiter]:delimiter:compadd ${(s\:\:)IFS}'
'-l[list name in addition to id]')
fi
local optchars
case "$OSTYPE" in
linux*) linux*)
optchars="cflvxdnoPgsuUGtw" # Note: We deliberately exclude -v but not --inverse from pkill
pgopts=acdFfGghLlnoPstUuVvwx-
pkopts=ceFfGghLnoPstUuVx-
arguments=(
${arguments:#((#s)|*\))(\*|)-[acl]*}
'(-c --count)'{-c,--count}'[display count of matching processes]'
)
[[ $service == pgrep ]] && arguments+=(
'(-a -l --list-full --list-name)'{-a,--list-full}'[display full command line]'
'(-a -l --list-full --list-name)'{-l,--list-name}'[display process name]'
)
;; ;;
freebsd*) dragonfly*|freebsd*)
optchars="LSafilnoqvxFGMNPUdgjstu" pgopts=acdFfGgijLlMNnoPqSstUuvx
pkopts=acFfGgIijLlMNnoPstUuvx
;; ;;
openbsd*) openbsd*)
optchars="flnoqvxdGgPsTtUu" pgopts=dfGglnoPqsTtUuvx
pkopts=fGgIlnoPqsTtUuvx
;; ;;
darwin*) darwin*)
optchars="LafilnoqvxFGPUdgtu" pgopts=adFfGgiLlnoPqtUuvx
pkopts=aFfGgIiLlnoPtUuvx
;; ;;
solaris*) solaris*)
optchars="flvxdnoPgsuUGJtTcz" pgopts=cdfGgJlnoPsTtUuvxz
arguments=( ${arguments##-T*} ) pkopts=cfGgJnoPsTtUuvxz
arguments=( ${arguments##-c*} ) arguments=(
arguments+=( '-T[match only processes in task ids]:taskid:->task' ) ${arguments:#((#s)|*\))(\*|)-[cT]*}
arguments+=( '-c[match only processes in contract ids]:taskid:->contract' ) '-c+[match only on specified contract IDs]: :->contract'
'-T+[match only on specified task IDs]: :->task'
)
;; ;;
*) *)
optchars="flvxdnoPgsuUGt" pgopts=dfGgilnPstUuvx
pkopts=fGgilnPstUuvx
;; ;;
esac esac
# Only keep relevant arguments according to $optchars.
arguments=( ${(M)arguments:#(|\*)(|\(*\))-[$optchars]*} if [[ $service == pgrep ]]; then
'*:process name:->pname') arguments=( ${(M)arguments:#((#s)|*\))(\*|)-[$pgopts]*} )
if [[ $service == 'pkill' ]]; then else
arguments+=('-'${^signals}'[signal]') arguments=( ${(M)arguments:#((#s)|*\))(\*|)-[$pkopts]*} )
# Signals on non-Linux systems can only be completed as the first argument
(( CURRENT != 2 )) && [[ $OSTYPE != linux* ]] && no='!'
# This is used for exclusion with --signal
sig_arguments=( + '(ss)' )
# This is very similar to _signals, but i've avoided it here because it
# doesn't behave the way i want it to
sig_arguments+=( $no'(--signal)-'${^signals[2,-3]} )
sig_arguments+=( '!(--signal)-'{0..$(( $#signals - 3 ))} )
# Complete the -SIG* variant if it's requested
if [[ $PREFIX$SUFFIX == -S* ]]; then
sig_arguments+=( '(--signal)-SIG'${^${(@)signals[2,-3]:#<->}} )
else
sig_arguments+=( '!(--signal)-SIG'${^${(@)signals[2,-3]:#<->}} )
fi
fi fi
_arguments -C -s -w $arguments && ret=0 arguments+=( $sig_arguments + o '*: :->pname' )
[[ $OSTYPE == linux* ]] || aopts+=( -A '*-' )
_arguments -C -s -S $aopts : $arguments && ret=0
case $state in case $state in
(sid) (sid)
@ -87,7 +146,7 @@ case $state in
sid=(${(uon)$(ps -A -o sid=)}) sid=(${(uon)$(ps -A -o sid=)})
fi fi
_wanted sid expl 'session id' compadd -S ',' -q -F used $sid _wanted sid expl 'session ID' compadd -S ',' -q -F used $sid
;; ;;
(ppid) (ppid)
@ -101,7 +160,7 @@ case $state in
ppid=(${(uon)$(ps -A -o ppid=)}) ppid=(${(uon)$(ps -A -o ppid=)})
fi fi
_wanted ppid expl 'parent process id' compadd -S ',' -q -F used $ppid _wanted ppid expl 'parent process ID' compadd -S ',' -q -F used $ppid
;; ;;
(pgid) (pgid)
@ -115,7 +174,7 @@ case $state in
pgid=(${(uon)$(ps -A -o pgid=)}) pgid=(${(uon)$(ps -A -o pgid=)})
fi fi
_wanted pgid expl 'process group id' compadd -S ',' -q -F used $pgid _wanted pgid expl 'process group ID' compadd -S ',' -q -F used $pgid
;; ;;
(projid) (projid)
@ -125,7 +184,7 @@ case $state in
used=(${(s:,:)IPREFIX}) used=(${(s:,:)IPREFIX})
projid=(${(uon)$(ps -A -o project=)}) projid=(${(uon)$(ps -A -o project=)})
_wanted projid expl 'project id' compadd -S ',' -q -F used $projid _wanted projid expl 'project ID' compadd -S ',' -q -F used $projid
;; ;;
(contract) (contract)
@ -135,7 +194,7 @@ case $state in
used=(${(s:,:)IPREFIX}) used=(${(s:,:)IPREFIX})
ctid=(${(uon)$(ps -A -o ctid=)}) ctid=(${(uon)$(ps -A -o ctid=)})
_wanted ctid expl 'contract id' compadd -S ',' -q -F used $ctid _wanted ctid expl 'contract ID' compadd -S ',' -q -F used $ctid
;; ;;
(task) (task)
@ -145,7 +204,7 @@ case $state in
used=(${(s:,:)IPREFIX}) used=(${(s:,:)IPREFIX})
taskid=(${(uon)$(ps -A -o project=)}) taskid=(${(uon)$(ps -A -o project=)})
_wanted taskid expl 'task id' compadd -S ',' -q -F used $taskid _wanted taskid expl 'task ID' compadd -S ',' -q -F used $taskid
;; ;;
(pname) (pname)