summary refs log tree commit diff
path: root/Completion/Unix/Command/_pgrep
blob: f06d26e5222a407355645a1c49ea46bd8e544d8d (plain) (blame)
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
#compdef pgrep pkill

local context state line ret=1 expl
typeset -A opt_args
typeset -a arguments

arguments=('-P[parent process id]:parent process id:->ppid'
     '-F[match only in process in pidfile]:files:_files'
     '-g[match only in process group ids]:group:->pgid'
     '-G[match only real group id]:group:_groups'
     '-j[match only in processes inside jails]:jail id:->jid'
     '-M[extract the name list from the specified core]:files:_files'
     '-N[extract the name list from the specified system]:files:_files'
     '-s[match only session id]:session id:->sid'
     '-t[match only controlled by terminal]:terminal device:->tty'
     '-T[match only in processes specified routing table in rtable]'
     '-u[match only effective user id]:user:_users'
     '-U[match only real user id]:user:_users'
     '(-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[confirmation before attempting to single each process]'
     '-L[given pidfile must be locked]'
     '-q[do not write anything to standard output]'
     '-S[search also in system processes]'
     '-v[negate matching]'
     '-x[match exactly]')

if [[ $service == 'pkill' ]]; then
  arguments+=('-'${^signals}'[signal]')
elif [[ $service == 'pgrep' ]]; then
  arguments+=('-d[output delimiter]:delimiter:compadd ${(s\:\:)IFS}'
        '-l[list name in addition to id]')
fi

local optchars
case "$OSTYPE" in
  linux*)
    optchars="cflvxdnoPgsuUGt"
    ;;
  freebsd*)
    optchars="LSafilnoqvxFGMNPUdgjstu"
    ;;
  openbsd*)
    optchars="flnoqvxdGgPsTtUu"
    ;;
  darwin*)
    optchars="LafilnoqvxFGPUdgtu"
    ;;
  *)
    optchars="flvxdnoPgsuUGt"
    ;;
esac
arguments=( ${(M)arguments:#(|\*)(|\(*\))-[$optchars]*}
     '*:process name:->pname')

_arguments -s -w $arguments && ret=0

case $state in
  (tty)
    compset -P '*,'

    local -a used ttys
    used=(${(s:,:)IPREFIX})

    ttys=( /dev/tty*(N) /dev/pts/*(N) )
    _wanted tty expl 'terminal device' compadd -S ',' -q -F used ${ttys#/dev/}
    ;;

  (sid)
    if [[ $OSTYPE == openbsd* ]]; then
      break
    fi

    compset -P '*,'

    local -a used sid
    used=(${(s:,:)IPREFIX})
    if [[ $OSTYPE == freebsd* ]]; then
      sid=(${(uon)$(ps -ax -o sid=)})
    else
      sid=(${(uon)$(ps -A o sid=)})
    fi

    _wanted sid expl 'session id' compadd -S ',' -q -F used $sid
    ;;

  (jid)
    compset -P '*,'

    local -a used jid
    used=(${(s:,:)IPREFIX})
    jid=(${(uon)$(ps -ax -o jid=)})

    _wanted jid expl 'jail id' compadd -S ',' -q -F used $jid
    ;;

  (ppid)
    compset -P '*,'

    local -a used ppid
    used=(${(s:,:)IPREFIX})
    if [[ $OSTYPE == (freebsd|openbsd|darwin)* ]]; then
      ppid=(${(uon)$(ps -ax -o ppid=)})
    else
      ppid=(${(uon)$(ps -A o ppid=)})
    fi

    _wanted ppid expl 'parent process id' compadd -S ',' -q -F used $ppid
    ;;

  (pgid)
    compset -P '*,'

    local -a used pgid
    used=(${(s:,:)IPREFIX})
    if [[ $OSTYPE == (freebsd|openbsd|darwin)* ]]; then
      pgid=(${(uon)$(ps -ax -o pgid=)})
    else
      pgid=(${(uon)$(ps -A o pgid=)})
    fi

    _wanted pgid expl 'process group id' compadd -S ',' -q -F used $pgid
    ;;

  (pname)
    local ispat="pattern matching "
    if (( ${+opt_args[-x]} )); then
      ispat=""
    fi

    local command
    if (( ${+opt_args[-f]} )); then
      if [[ "$OSTYPE" == freebsd* ]] && (( ${+opt_args[-S]} )); then
        command="$(ps -axH -o command=)"
      elif [[ "$OSTYPE" == (freebsd|openbsd|darwin)* ]]; then
        command="$(ps -ax -o command=)"
      else
        command="$(ps -A o cmd=)"
      fi
      _wanted pname expl $ispat'process command line' compadd ${(u)${(f)${command}}}
    else
      if [[ "$OSTYPE" == freebsd* ]] && (( ${+opt_args[-S]} )); then
        command="$(ps -axcH -o command=)"
      elif [[ "$OSTYPE" == (freebsd|openbsd|darwin)* ]]; then
        command="$(ps -axc -o command=)"
      else
        command="$(ps -A co cmd=)"
      fi
      _wanted pname expl $ispat'process name' compadd ${(u)${(f)${command}}}
    fi
    ;;

esac && ret=0

return ret