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

local context state line
typeset -A opt_args
typeset -a arguments

arguments=('-P[parent process id]:parent process id:->ppid' 
	   '-g[match only in process group ids]:group:->pgid' 
	   '-G[match only real group id]:group:->group' 
	   '-s[match only session id]:session id:->sid' 
	   '-t[match only controlled by terminal]:terminal device:->tty'
	   '-u[match only effective user id]:user:->user' 
	   '-U[match only real user id]:user:->user' 
           '(-n)-o[oldest process]' 
	   '(-o)-n[newest process]' 
	   '-f[match against full command line]' 
	   '-v[negate matching]' 
	   '-x[match exactly]' 
	   '*:process name:->pname')

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

_arguments -s -w $arguments

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

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

		compadd -S ',' -q -F used /dev/tty*(:t)
		;;
		
	(sid)
		compset -P '*,'

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

		compadd -S ',' -q -F used $sid
		;;
	
	(ppid)
		compset -P '*,'

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

		compadd -S ',' -q -F used $ppid
		;;

	(pgid)
		compset -P '*,'

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

		compadd -S ',' -q -F used $pgid
		;;
	
	(pname)
		if (( ${+opt_args[-x]} )) && (( ${+opt_args[-f]} ))
		then
			compadd ${(u)${(f)"$(ps -A o cmd=)"}}
		else
			compadd ${(u)${(f)"$(ps -A co cmd=)"}}
		fi
		;;
	
	(group)
		compset -P '*,'

		local group
		group=$(getent group)

		local -a groups ids
		groups=(${${(f)group}%%:*})
		ids=(${${${(f)group}#*:*:}%%:*})

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

		compadd -S ',' -q -F used -d ids $groups $groups
		;;

	(user)
		compset -P '*,'

		local passwd
		passwd=$(getent passwd)

		local -a users ids
		users=(${${(f)passwd}%%:*})
		ids=(${${${(f)passwd}#*:*:}%%:*})
		
		local -a used
		used=(${(s:,:)IPREFIX})

		compadd -S ',' -q -F used -d ids $users $users
		;;
esac