From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 14568 invoked by alias); 19 Jul 2018 10:06:25 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: List-Unsubscribe: X-Seq: 43192 Received: (qmail 18320 invoked by uid 1010); 19 Jul 2018 10:06:25 -0000 X-Qmail-Scanner-Diagnostics: from rcpt-expgw.biglobe.ne.jp by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.99.2/21882. spamassassin: 3.4.1. Clear:RC:0(133.208.98.4):SA:0(-2.6/5.0):. Processed in 1.268777 secs); 19 Jul 2018 10:06:25 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham autolearn_force=no version=3.4.1 X-Envelope-From: takimoto-j@kba.biglobe.ne.jp X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | X-Biglobe-Sender: From: Jun T Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (Mac OS X Mail 10.3 \(3273\)) Subject: [PATCH] _pgrep: simplify the processing of '->state' actions Message-Id: <064FE4EC-2987-49F5-8BA0-666E1D1568B2@kba.biglobe.ne.jp> Date: Thu, 19 Jul 2018 19:06:20 +0900 To: zsh-workers@zsh.org X-Mailer: Apple Mail (2.3273) X-Biglobe-Spnum: 51101 Simplify 'case $state in ... esac' in _pgrep (and fix a few bugs). 'ps -A' works on all systems (including FreeBSD, although not yet documented), and 'ps -o args=3D' can be used to get the list of full command lines on all systems. # I didn't check on solaris, but '-o args=3D' has been used in solaris # before this patch and I assume it works. When completing a comma-separated list of ID's, commands like compset -P '*,' used=3D(${(s:,:)IPREFIX}) compadd -F used were used to ignore the ID's already in the list. But this was not working if there was no space between the option letter and the list, for example 'pgrep -P'. I think it is better to use _sequence. On FreeBSD, if the option -S (search also in system processes) is on the command line, 'ps -axH' was used to get the list of process names. But the "system processes" are already included by 'ps -A'; 'ps -H' includes all the threads in multi-threaded processes, and introduces lots of wrong completions. I just removed the special treatment of "FreeBSD with option -S". diff --git a/Completion/Unix/Command/_pgrep = b/Completion/Unix/Command/_pgrep index 86aef3462..3b4d082a7 100644 --- a/Completion/Unix/Command/_pgrep +++ b/Completion/Unix/Command/_pgrep @@ -1,7 +1,7 @@ #compdef pgrep pkill =20 # Notes: -# - We assume that Linux systems use procps-ng =E2=80=94 specifically, = procps-ng >=3D3.3.4 +# - We assume that Linux systems use procps-ng - specifically, = procps-ng >=3D3.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 @@ -26,7 +26,6 @@ arguments=3D( '(: * -)'{-h,--help}'[display help information]' '-I[request confirmation before signalling each process]' '-i[ignore case distinctions]' - '-J+[match only on specified project IDs]: :->projid' '-j+[match only on specified jail IDs]:jail:_sequence _jails -0 -o = jid' '(-L --logpidfile)'{-L,--logpidfile}'[fail if PID file not locked = (with -F)]' '(-N)-M+[extract name list from specified core]:core file:_files' @@ -37,7 +36,7 @@ arguments=3D( '(-l)-q[suppress normal output]' '-S[search also in system processes (kernel threads)]' '(-s --session)'{-s+,--session=3D}'[match only on specified process = session IDs]: :->sid' - # _signals is OK here =E2=80=94 we do it differently below + # _signals is OK here - we do it differently below '(ss)--signal=3D[specify signal to send to process]: :_signals -s' '-T+[match only on specified routing table]:routing table' '(-t --terminal)'{-t+,--terminal=3D}'[match only on specified = controlling terminals]: :_sequence _ttys -do' @@ -87,11 +86,12 @@ case $OSTYPE in pkopts=3DaFfGgIiLlnoPtUuvx ;; solaris*) - pgopts=3DcdfGgJlnoPsTtUuvxz - pkopts=3DcfGgJnoPsTtUuvxz + pgopts=3DcdfGglnoPsTtUuvxz + pkopts=3DcfGgnoPsTtUuvxz arguments=3D( ${arguments:#((#s)|*\))(\*|)-[cT]*} '-c+[match only on specified contract IDs]: :->contract' + '-J+[match only on specified project IDs]: :->projid' '-T+[match only on specified task IDs]: :->task' ) ;; @@ -130,115 +130,48 @@ arguments+=3D( $sig_arguments + o '*: :->pname' ) [[ $OSTYPE =3D=3D linux* ]] || aopts+=3D( -A '*-' ) _arguments -C -s -S $aopts : $arguments && ret=3D0 =20 +# complete comma-separated list of various IDs +# $1: tag, $2: description, $3: keyword for 'ps -o' +_pgrep_sequence () { + _sequence _wanted $1 expl "$2" \ + compadd - ${(un)$(_call_program $1 ps -A -o $3=3D)} +} + case $state in (sid) if [[ $OSTYPE =3D=3D openbsd* ]]; then - break - fi - - compset -P '*,' - - local -a used sid - used=3D(${(s:,:)IPREFIX}) - if [[ $OSTYPE =3D=3D freebsd* ]]; then - sid=3D(${(uon)$(ps -ax -o sid=3D)}) + _message 'session ID' else - sid=3D(${(uon)$(ps -A -o sid=3D)}) + _pgrep_sequence session-ids 'session ID' sid fi - - _wanted sid expl 'session ID' compadd -S ',' -q -F used $sid ;; - (ppid) - compset -P '*,' - - local -a used ppid - used=3D(${(s:,:)IPREFIX}) - if [[ $OSTYPE =3D=3D (freebsd|openbsd|darwin)* ]]; then - ppid=3D(${(uon)$(ps -ax -o ppid=3D)}) - else - ppid=3D(${(uon)$(ps -A -o ppid=3D)}) - fi - - _wanted ppid expl 'parent process ID' compadd -S ',' -q -F used = $ppid + _pgrep_sequence ppids 'parent process ID' ppid ;; - (pgid) - compset -P '*,' - - local -a used pgid - used=3D(${(s:,:)IPREFIX}) - if [[ $OSTYPE =3D=3D (freebsd|openbsd|darwin)* ]]; then - pgid=3D(${(uon)$(ps -ax -o pgid=3D)}) - else - pgid=3D(${(uon)$(ps -A -o pgid=3D)}) - fi - - _wanted pgid expl 'process group ID' compadd -S ',' -q -F used = $pgid + _pgrep_sequence pgids 'process group ID' pgid ;; - (projid) - compset -P '*,' - - local -a used projid - used=3D(${(s:,:)IPREFIX}) - projid=3D(${(uon)$(ps -A -o project=3D)}) - - _wanted projid expl 'project ID' compadd -S ',' -q -F used $projid + _pgrep_sequence project-ids 'project ID' project ;; - (contract) - compset -P '*,' - - local -a used ctid - used=3D(${(s:,:)IPREFIX}) - ctid=3D(${(uon)$(ps -A -o ctid=3D)}) - - _wanted ctid expl 'contract ID' compadd -S ',' -q -F used $ctid + _pgrep_sequence contract-ids 'contract ID' ctid ;; - (task) - compset -P '*,' - - local -a used taskid - used=3D(${(s:,:)IPREFIX}) - taskid=3D(${(uon)$(ps -A -o project=3D)}) - - _wanted taskid expl 'task ID' compadd -S ',' -q -F used $taskid + _pgrep_sequence task-ids 'task ID' taskid ;; - (pname) local ispat=3D"pattern matching " if (( ${+opt_args[-x]} )); then ispat=3D"" fi - - local command if (( ${+opt_args[-f]} )); then - if [[ "$OSTYPE" =3D=3D freebsd* ]] && (( ${+opt_args[-S]} )); = then - command=3D"$(ps -axH -o command=3D)" - elif [[ "$OSTYPE" =3D=3D (freebsd|openbsd|darwin)* ]]; then - command=3D"$(ps -ax -o command=3D)" - elif [[ "$OSTYPE" =3D=3D solaris* ]]; then - command=3D"$(ps -A -o args=3D)" - else - command=3D"$(ps -A o cmd=3D)" - fi - _wanted pname expl $ispat'process command line' compadd = ${(u)${(f)${command}}} + _wanted process-args expl $ispat'process command line' \ + compadd ${${(f)"$(_call_program process-args ps -A -o args=3D)"}% = *} else - if [[ "$OSTYPE" =3D=3D freebsd* ]] && (( ${+opt_args[-S]} )); = then - command=3D"$(ps -axcH -o command=3D)" - elif [[ "$OSTYPE" =3D=3D (freebsd|openbsd|darwin)* ]]; then - command=3D"$(ps -axc -o command=3D)" - elif [[ "$OSTYPE" =3D=3D solaris* ]]; then - command=3D"$(ps -A -o comm=3D)" - else - command=3D"$(ps -A co cmd=3D)" - fi - _wanted pname expl $ispat'process name' compadd = ${(u)${(f)${command}}} + _wanted processes-names expl $ispat'process name' _process_names = -a -t fi ;; - esac && ret=3D0 =20 return ret